-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorporate Funnel, Tee and Fake.Sink #922
Open
FelonEkonom
wants to merge
9
commits into
master
Choose a base branch
from
incorporate-funnel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c26812
Incorporate Funnel
FelonEkonom 16c8b75
Incorporate Tees
FelonEkonom 92c9080
Update changelog
FelonEkonom f8f6b36
Merge all Tees into one
FelonEkonom 59e1fca
Add funnel and tee tests
FelonEkonom e0d45ce
Fix warning
FelonEkonom 523cf67
Incorporate Membrane.Fake.Sink
FelonEkonom cd1fa25
Upgrade changelog
FelonEkonom 87ae47b
Refactor Funnel
FelonEkonom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Fake.Sink do | ||
@moduledoc """ | ||
Membrane Sink that ignores incoming data. | ||
""" | ||
|
||
use Membrane.Sink | ||
|
||
def_input_pad :input, accepted_format: _any | ||
|
||
@impl true | ||
def handle_buffer(:input, _buffer, _ctx, state), do: {[], state} | ||
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
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||||||||||||||
defmodule Membrane.Funnel do | ||||||||||||||||||
@moduledoc """ | ||||||||||||||||||
Element that can be used for collecting data from multiple inputs and sending it through one | ||||||||||||||||||
output. When a new input connects in the `:playing` state, the funnel sends | ||||||||||||||||||
`#{inspect(__MODULE__)}.NewInputEvent` via output. | ||||||||||||||||||
Comment on lines
+3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
""" | ||||||||||||||||||
use Membrane.Filter | ||||||||||||||||||
|
||||||||||||||||||
def_input_pad :input, accepted_format: _any, flow_control: :auto, availability: :on_request | ||||||||||||||||||
def_output_pad :output, accepted_format: _any, flow_control: :auto | ||||||||||||||||||
|
||||||||||||||||||
def_options end_of_stream: [spec: :on_last_pad | :on_first_pad | :never, default: :on_last_pad] | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_init(_ctx, opts) do | ||||||||||||||||||
{[], %{end_of_stream: opts.end_of_stream}} | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_buffer(Pad.ref(:input, _id), buffer, _ctx, state) do | ||||||||||||||||||
{[buffer: {:output, buffer}], state} | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_pad_added(Pad.ref(:input, _id), %{playback_state: :playing}, state) do | ||||||||||||||||||
{[event: {:output, %__MODULE__.NewInputEvent{}}], state} | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_pad_added(Pad.ref(:input, _id), _ctx, state) do | ||||||||||||||||||
{[], state} | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_end_of_stream(Pad.ref(:input, _id), _ctx, %{end_of_stream: :never} = state) do | ||||||||||||||||||
{[], state} | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_end_of_stream(Pad.ref(:input, _id), ctx, %{end_of_stream: :on_first_pad} = state) do | ||||||||||||||||||
if ctx.pads.output.end_of_stream? do | ||||||||||||||||||
{[], state} | ||||||||||||||||||
else | ||||||||||||||||||
{[end_of_stream: :output], state} | ||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
@impl true | ||||||||||||||||||
def handle_end_of_stream(Pad.ref(:input, _id), ctx, %{end_of_stream: :on_last_pad} = state) do | ||||||||||||||||||
if ctx |> inputs_data() |> Enum.all?(& &1.end_of_stream?) do | ||||||||||||||||||
{[end_of_stream: :output], state} | ||||||||||||||||||
else | ||||||||||||||||||
{[], state} | ||||||||||||||||||
end | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
defp inputs_data(ctx) do | ||||||||||||||||||
Enum.flat_map(ctx.pads, fn | ||||||||||||||||||
{Pad.ref(:input, _id), data} -> [data] | ||||||||||||||||||
_output -> [] | ||||||||||||||||||
end) | ||||||||||||||||||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Membrane.Funnel.NewInputEvent do | ||
@moduledoc """ | ||
Event sent each time new element is linked (via funnel input pad) after playing pipeline. | ||
""" | ||
@derive Membrane.EventProtocol | ||
|
||
@type t :: %__MODULE__{} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule Membrane.Tee do | ||
@moduledoc """ | ||
Element for forwarding buffers to at least one output pad | ||
|
||
It has one input pad `:input` and 2 output pads: | ||
* `:output` - is a dynamic pad which is always available and works in pull mode | ||
* `:output_copy` - is a dynamic pad that can be linked to any number of elements (including 0) and works | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While |
||
in push mode | ||
|
||
The `:output` pads dictate the speed of processing data and any element (or elements) connected to | ||
`:output_copy` pad will receive the same data as all `:output` instances. | ||
""" | ||
use Membrane.Filter, flow_control_hints?: false | ||
|
||
require Membrane.Logger | ||
|
||
def_input_pad :input, | ||
availability: :always, | ||
flow_control: :auto, | ||
accepted_format: _any | ||
|
||
def_output_pad :output, | ||
availability: :on_request, | ||
flow_control: :auto, | ||
accepted_format: _any | ||
|
||
def_output_pad :output_copy, | ||
availability: :on_request, | ||
flow_control: :push, | ||
accepted_format: _any | ||
|
||
@impl true | ||
def handle_init(_ctx, _opts) do | ||
{[], %{stream_format: nil}} | ||
end | ||
|
||
@impl true | ||
def handle_playing(ctx, state) do | ||
if map_size(ctx.pads) < 2 do | ||
Membrane.Logger.debug(""" | ||
#{inspect(__MODULE__)} enters :playing playback without any output (:output or :output_copy) \ | ||
pads linked. | ||
""") | ||
end | ||
|
||
{[], state} | ||
end | ||
|
||
@impl true | ||
def handle_stream_format(:input, stream_format, _ctx, state) do | ||
{[forward: stream_format], %{state | stream_format: stream_format}} | ||
end | ||
|
||
@impl true | ||
def handle_pad_added(Pad.ref(name, _ref) = output_pad, ctx, state) | ||
when name in [:output, :output_copy] do | ||
maybe_stream_format = | ||
case state.stream_format do | ||
nil -> [] | ||
stream_format -> [stream_format: {output_pad, stream_format}] | ||
end | ||
|
||
maybe_eos = | ||
if ctx.pads.input.end_of_stream?, | ||
do: [end_of_stream: output_pad], | ||
else: [] | ||
|
||
{maybe_stream_format ++ maybe_eos, state} | ||
end | ||
|
||
@impl true | ||
def handle_buffer(:input, buffer, _ctx, state) do | ||
{[forward: buffer], state} | ||
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
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,33 @@ | ||
defmodule Membrane.Integration.FunnelTest do | ||
use ExUnit.Case | ||
|
||
import Membrane.Testing.Assertions | ||
|
||
alias Membrane.{Buffer, Funnel, Testing} | ||
|
||
test "Collects multiple inputs" do | ||
import Membrane.ChildrenSpec | ||
data = 1..10 | ||
|
||
{:ok, _supervisor_pid, pipeline} = | ||
Testing.Pipeline.start_link( | ||
spec: [ | ||
child(:funnel, Funnel), | ||
child(:src1, %Testing.Source{output: data}) |> get_child(:funnel), | ||
child(:src2, %Testing.Source{output: data}) |> get_child(:funnel), | ||
get_child(:funnel) |> child(:sink, Testing.Sink) | ||
] | ||
) | ||
|
||
data | ||
|> Enum.flat_map(&[&1, &1]) | ||
|> Enum.each(fn payload -> | ||
assert_sink_buffer(pipeline, :sink, %Buffer{payload: ^payload}) | ||
end) | ||
|
||
assert_end_of_stream(pipeline, :sink) | ||
refute_sink_buffer(pipeline, :sink, _buffer, 0) | ||
|
||
Membrane.Pipeline.terminate(pipeline) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Membrane.Integration.TeeTest do | ||
@moduledoc false | ||
use ExUnit.Case, async: true | ||
use Bunch | ||
|
||
import Membrane.ChildrenSpec | ||
import Membrane.Testing.Assertions | ||
|
||
alias Membrane.Buffer | ||
alias Membrane.Testing.{Pipeline, Sink, Source} | ||
|
||
test "forwards input to three all outputs" do | ||
range = 1..100 | ||
sinks = [:sink1, :sink2, :sink3, :sink_4] | ||
|
||
spec = | ||
[ | ||
child(:src, %Source{output: range}) | ||
|> child(:tee, Membrane.Tee) | ||
] ++ | ||
for sink <- sinks do | ||
pad = if sink in [:sink1, :sink2], do: :output, else: :output_copy | ||
|
||
get_child(:tee) | ||
|> via_out(pad) | ||
|> child(sink, %Sink{}) | ||
end | ||
|
||
pipeline = Pipeline.start_link_supervised!(spec: spec) | ||
|
||
for sink <- sinks do | ||
assert_end_of_stream(pipeline, ^sink, :input) | ||
end | ||
|
||
for element <- range, sink <- sinks do | ||
assert_sink_buffer(pipeline, sink, %Buffer{payload: ^element}) | ||
end | ||
|
||
for {pad, sink} <- [output_copy: :sink5, output: :sink6] do | ||
spec = | ||
get_child(:tee) | ||
|> via_out(pad) | ||
|> child(sink, %Sink{}) | ||
|
||
Pipeline.execute_actions(pipeline, spec: spec) | ||
end | ||
|
||
for sink <- [:sink5, :sink6] do | ||
assert_sink_stream_format(pipeline, sink, %Membrane.RemoteStream{}) | ||
assert_end_of_stream(pipeline, ^sink, :input) | ||
end | ||
|
||
Pipeline.terminate(pipeline) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about