-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SeekSourceEvent. Add handling of SeekSourceEvent in File.Source. Rename SeekEvent into SeekSinkEvent. * Add NewSeekEvent - event send before the buffers from a new seek
- Loading branch information
Showing
12 changed files
with
287 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Membrane.File.NewSeekEvent do | ||
@moduledoc """ | ||
An event sent by the `Membrane.File.Source` with `seekable?: true` option, | ||
right after receiving `Membrane.File.SeekSourceEvent`. | ||
An element that steers the seekable file source with `Membrane.File.SeekSourceEvent` | ||
can assume that all the buffers received after receiving that event are | ||
the buffers ordered by that `Membrane.File.SeekSourceEvent`. | ||
""" | ||
@derive Membrane.EventProtocol | ||
|
||
defstruct [] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Membrane.File.SeekSourceEvent do | ||
@moduledoc """ | ||
Event that triggers seeking and reading in `Membrane.File.Source` working in | ||
`seekable?: true` mode. | ||
When `inspect(__MODULE__)` is received by the source, the source starts reading | ||
data from the given position in file, specified by the `:start` field of the event's | ||
struct. The source reads up to `size_to_read` bytes of the data from file (it can | ||
read less if the file ends). | ||
If the event is set with `last?: true`, once `size_to_read` bytes are read or the | ||
file ends, the source will return `end_of_stream` action on the `:output` pad. | ||
""" | ||
@derive Membrane.EventProtocol | ||
|
||
@type offset_t :: integer() | ||
|
||
@typedoc """ | ||
Specifies the position to which the seek is performed. | ||
The meaning is the same as for the `Location` argument in https://www.erlang.org/doc/man/file.html#position-2. | ||
""" | ||
@type position_t :: offset_t() | {:bof | :cur | :eof, offset_t()} | :bof | :cur | :eof | ||
|
||
@type t :: %__MODULE__{ | ||
start: position_t(), | ||
size_to_read: non_neg_integer() | :infinity, | ||
last?: boolean() | ||
} | ||
@enforce_keys [:start, :size_to_read] | ||
defstruct @enforce_keys ++ [last?: false] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0123456789 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
defmodule Membrane.File.Integration.SourceTest do | ||
use Membrane.File.IntegrationTestCaseTemplate | ||
|
||
import Membrane.Testing.Assertions | ||
import Membrane.ChildrenSpec | ||
|
||
alias Membrane.Testing.Pipeline | ||
alias Membrane.Testing.Sink | ||
alias Membrane.File.Source | ||
alias Membrane.File.SeekSourceEvent | ||
alias Membrane.Buffer | ||
|
||
defmodule Filter do | ||
use Membrane.Filter | ||
|
||
def_input_pad :input, | ||
accepted_format: _, | ||
mode: :pull, | ||
demand_mode: :auto, | ||
demand_unit: :bytes | ||
|
||
def_output_pad :output, accepted_format: _, mode: :pull, demand_mode: :auto | ||
|
||
@impl true | ||
def handle_parent_notification(event, _context, state) do | ||
{[event: {:input, event}], state} | ||
end | ||
|
||
@impl true | ||
def handle_process(:input, buffer, _context, state) do | ||
{[buffer: {:output, buffer}], state} | ||
end | ||
end | ||
|
||
@input_text_file "test/fixtures/input.txt" | ||
|
||
test "if seekable Source sents only the buffers requested" do | ||
spec = [ | ||
child(:source, %Source{ | ||
location: @input_text_file, | ||
chunk_size: 2, | ||
seekable?: true | ||
}) | ||
|> child(:filter, Filter) | ||
|> child(:sink, Sink) | ||
] | ||
|
||
{:ok, _supervisor_pid, pipeline_pid} = Pipeline.start(structure: spec) | ||
refute_sink_buffer(pipeline_pid, :sink, _) | ||
|
||
Pipeline.execute_actions(pipeline_pid, | ||
notify_child: {:filter, %SeekSourceEvent{start: 2, size_to_read: 5}} | ||
) | ||
|
||
assert_sink_buffer(pipeline_pid, :sink, %Buffer{payload: "23456"}) | ||
|
||
Pipeline.execute_actions(pipeline_pid, | ||
notify_child: {:filter, %SeekSourceEvent{start: 0, size_to_read: 3}} | ||
) | ||
|
||
Pipeline.execute_actions(pipeline_pid, | ||
notify_child: {:filter, %SeekSourceEvent{start: 7, size_to_read: 10}} | ||
) | ||
|
||
assert_sink_buffer(pipeline_pid, :sink, %Buffer{payload: "789"}) | ||
|
||
Pipeline.execute_actions(pipeline_pid, | ||
notify_child: {:filter, %SeekSourceEvent{start: 7, size_to_read: 10, last?: true}} | ||
) | ||
|
||
assert_sink_buffer(pipeline_pid, :sink, %Buffer{payload: "789"}) | ||
assert_end_of_stream(pipeline_pid, :sink) | ||
end | ||
|
||
test "if seekable Source sents :end_of_stream for seek event with `last?: true` when all the bytes are supplied" do | ||
Membrane.File.CommonMock.open!("test/fixtures/input.txt", []) | ||
|
||
spec = [ | ||
child(:source, %Source{ | ||
location: @input_text_file, | ||
chunk_size: 2, | ||
seekable?: true | ||
}) | ||
|> child(:filter, Filter) | ||
|> child(:sink, Sink) | ||
] | ||
|
||
{:ok, _supervisor_pid, pipeline_pid} = Pipeline.start(structure: spec) | ||
refute_sink_buffer(pipeline_pid, :sink, _) | ||
|
||
Pipeline.execute_actions(pipeline_pid, | ||
notify_child: {:filter, %SeekSourceEvent{start: 0, size_to_read: 5, last?: true}} | ||
) | ||
|
||
assert_sink_buffer(pipeline_pid, :sink, %Buffer{payload: "01234"}) | ||
assert_end_of_stream(pipeline_pid, :sink) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.