Skip to content

Commit 35eecce

Browse files
committed
Add test wip
1 parent 503f1c6 commit 35eecce

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

lib/membrane/forwarding_filter.ex

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ defmodule Membrane.ForwardingFilter do
1313

1414
def_options notify_on_stream_format?: [default: false], notify_on_event?: [default: false]
1515

16-
defguardp flowing?(ctx, state) when ctx.playback == :playing and state.input != nil and state.output != nil
16+
defguardp flowing?(ctx, state)
17+
when ctx.playback == :playing and state.input != nil and state.output != nil
1718

1819
@impl true
1920
def handle_init(_ctx, opts) do
@@ -39,13 +40,15 @@ defmodule Membrane.ForwardingFilter do
3940
@impl true
4041
def unquote(callback)(pad, item, ctx, state) when flowing?(ctx, state) do
4142
actions = [{unquote(action), {opposite_pad(pad, state), item}}]
43+
actions = actions ++ maybe_notify_parent(unquote(action), pad, item, state)
4244
{actions, state}
4345
end
4446

4547
@impl true
4648
def unquote(callback)(pad, item, _ctx, state) do
4749
state = unquote(action) |> store_in_queue(pad, item, state)
48-
{[], state}
50+
actions = unquote(action) |> maybe_notify_parent(pad, item, state)
51+
{actions, state}
4952
end
5053
end)
5154

@@ -81,4 +84,12 @@ defmodule Membrane.ForwardingFilter do
8184

8285
defp opposite_pad(Pad.ref(:input, _ref), state), do: state.output
8386
defp opposite_pad(Pad.ref(:output, _ref), state), do: state.input
87+
88+
defp maybe_notify_parent(:event, pad, event, %{notify_on_event?: true}),
89+
do: [notiy_parent: {:event, pad, event}]
90+
91+
defp maybe_notify_parent(:stream_format, pad, format, %{notify_on_stream_format?: true}),
92+
do: [notiy_parent: {:stream_format, pad, format}]
93+
94+
defp maybe_notify_parent(_type, _pad, _item, _state), do: []
8495
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
defmodule Membrane.Integration.ForwardingFilterTest do
2+
use ExUnit.Case, async: true
3+
4+
import Membrane.ChildrenSpec
5+
import Membrane.Testing.Assertions
6+
7+
alias Membrane.Buffer
8+
alias Membrane.ForwardingFilter
9+
alias Membrane.Testing
10+
11+
defmodule Format do
12+
defstruct [:field]
13+
end
14+
15+
defmodule Event do
16+
@derive Membrane.EventProtocol
17+
defstruct [:field]
18+
end
19+
20+
defmodule Source do
21+
use Membrane.Source
22+
def_output_pad :output, accepted_format: _any, flow_control: :push
23+
24+
@impl true
25+
def handle_parent_notifications({action, item}, _ctx, state),
26+
do: {[{action, {:output, item}}], state}
27+
end
28+
29+
test "ForwardingFilter buffers data until output pad is linked" do
30+
pipeline =
31+
Testing.Pipeline.start_link_supervised!(
32+
spec:
33+
child(:source, Source)
34+
|> child(:filter, %ForwardingFilter{
35+
notify_on_event?: true,
36+
notify_on_stream_format?: true
37+
})
38+
)
39+
40+
data = generate_data(100, [:stream_format, :buffer, :event])
41+
42+
data
43+
|> Enum.each(fn {type, item} ->
44+
Testing.Pipeline.notify_child(pipeline, :source, {type, item})
45+
assert_pipeline_notified(pipeline, :filter, {^type, :input, ^item})
46+
end)
47+
48+
Testing.Pipeline.execute_actions(
49+
spec:
50+
get_child(:filter)
51+
|> child(:sink, Testing.Sink)
52+
)
53+
54+
data
55+
|> Enum.each(fn {type, item} ->
56+
case type do
57+
:buffer -> assert_sink_buffer(pipeline, :sink, ^item)
58+
:event -> assert_sink_event(pipeline, :sink, ^item)
59+
:stream_format -> assert_sink_stream_format(pipeline, :sink, ^item)
60+
end
61+
end)
62+
63+
data = generate_data(100, [:stream_format, :buffer, :event], 200)
64+
end
65+
66+
defp generate_data(number, types, pts_offset \\ 0) do
67+
data =
68+
1..(number - 1)
69+
|> Enum.map(fn i ->
70+
case Enum.random(types) do
71+
:stream_format -> {:stream_format, %Format{field: i}}
72+
:event -> {:event, %Event{field: i}}
73+
:buffer -> {:buffer, %Buffer{pts: i + pts_offset, payload: <<>>}}
74+
end
75+
end)
76+
77+
[stream_format: %Format{field: 0}] ++ data
78+
end
79+
end

0 commit comments

Comments
 (0)