|
1 |
| -defmodule Membrane.Transcoder.ForwardingFilter do |
2 |
| - @moduledoc false |
3 |
| - use Membrane.Filter |
4 |
| - |
5 |
| - alias Membrane.TimestampQueue |
6 |
| - |
7 |
| - def_input_pad :input, |
8 |
| - accepted_format: _any, |
9 |
| - availability: :on_request |
10 |
| - |
11 |
| - def_output_pad :output, |
12 |
| - accepted_format: _any, |
13 |
| - availability: :on_request |
14 |
| - |
15 |
| - defguardp is_input_linked(state) when state.input_pad_ref != nil |
16 |
| - defguardp is_output_linked(state) when state.output_pad_ref != nil |
17 |
| - |
18 |
| - @impl true |
19 |
| - def handle_init(_ctx, _opts) do |
20 |
| - state = %{queue: TimestampQueue.new(), output_pad_ref: nil, input_pad_ref: nil} |
21 |
| - {[], state} |
22 |
| - end |
23 |
| - |
24 |
| - @impl true |
25 |
| - def handle_playing(ctx, state), do: maybe_flush_queue(ctx, state) |
26 |
| - |
27 |
| - @impl true |
28 |
| - def handle_pad_added(Pad.ref(direction, _id) = pad_ref, ctx, state) do |
29 |
| - same_direction_pads_number = |
30 |
| - ctx.pads |
31 |
| - |> Enum.count(fn {_pad_ref, pad_data} -> pad_data.direction == direction end) |
32 |
| - |
33 |
| - if same_direction_pads_number > 1 do |
34 |
| - raise """ |
35 |
| - #{inspect(__MODULE__)} can have only one #{inspect(direction)} pad, but it has \ |
36 |
| - #{same_direction_pads_number} |
37 |
| - """ |
38 |
| - end |
39 |
| - |
40 |
| - state = |
41 |
| - case direction do |
42 |
| - :input -> %{state | input_pad_ref: pad_ref} |
43 |
| - :output -> %{state | output_pad_ref: pad_ref} |
44 |
| - end |
45 |
| - |
46 |
| - maybe_flush_queue(ctx, state) |
47 |
| - end |
48 |
| - |
49 |
| - @impl true |
50 |
| - def handle_stream_format(_input_pad_ref, stream_format, _ctx, state) |
51 |
| - when is_output_linked(state) do |
52 |
| - {[ |
53 |
| - stream_format: {state.output_pad_ref, stream_format}, |
54 |
| - notify_parent: {:stream_format, stream_format} |
55 |
| - ], state} |
56 |
| - end |
57 |
| - |
58 |
| - @impl true |
59 |
| - def handle_stream_format(input_pad_ref, stream_format, _ctx, state) do |
60 |
| - queue = TimestampQueue.push_stream_format(state.queue, input_pad_ref, stream_format) |
61 |
| - {[notify_parent: {:stream_format, stream_format}], %{state | queue: queue}} |
62 |
| - end |
63 |
| - |
64 |
| - @impl true |
65 |
| - def handle_buffer(_input_pad_ref, buffer, _ctx, state) when is_output_linked(state) do |
66 |
| - {[buffer: {state.output_pad_ref, buffer}], state} |
67 |
| - end |
68 |
| - |
69 |
| - @impl true |
70 |
| - def handle_buffer(input_pad_ref, buffer, _ctx, state) do |
71 |
| - {_suggested_actions, queue} = TimestampQueue.push_buffer(state.queue, input_pad_ref, buffer) |
72 |
| - {[], %{state | queue: queue}} |
73 |
| - end |
74 |
| - |
75 |
| - @impl true |
76 |
| - def handle_event(Pad.ref(:input, _id), event, _ctx, state) when is_output_linked(state) do |
77 |
| - {[forward: event], state} |
78 |
| - end |
79 |
| - |
80 |
| - @impl true |
81 |
| - def handle_event(Pad.ref(:output, _id), event, _ctx, state) when is_input_linked(state) do |
82 |
| - {[forward: event], state} |
83 |
| - end |
84 |
| - |
85 |
| - @impl true |
86 |
| - def handle_event(pad_ref, event, _ctx, state) do |
87 |
| - queue = TimestampQueue.push_event(state.queue, pad_ref, event) |
88 |
| - {[], %{state | queue: queue}} |
89 |
| - end |
90 |
| - |
91 |
| - @impl true |
92 |
| - def handle_end_of_stream(_input_pad_ref, _ctx, state) when is_output_linked(state) do |
93 |
| - {[end_of_stream: state.output_pad_ref], state} |
94 |
| - end |
95 |
| - |
96 |
| - @impl true |
97 |
| - def handle_end_of_stream(input_pad_ref, _ctx, state) do |
98 |
| - queue = TimestampQueue.push_end_of_stream(state.queue, input_pad_ref) |
99 |
| - {[], %{state | queue: queue}} |
100 |
| - end |
101 |
| - |
102 |
| - defp maybe_flush_queue(ctx, state) |
103 |
| - when ctx.playback == :playing and is_input_linked(state) and is_output_linked(state) do |
104 |
| - {_suggested_actions, items, queue} = TimestampQueue.flush_and_close(state.queue) |
105 |
| - |
106 |
| - actions = |
107 |
| - Enum.map(items, fn |
108 |
| - {Pad.ref(:input, _id), {item_type, item}} -> {item_type, {state.output_pad_ref, item}} |
109 |
| - {Pad.ref(:input, _id), :end_of_stream} -> {:end_of_stream, state.output_pad_ref} |
110 |
| - {Pad.ref(:output, _id), {:event, item}} -> {:event, {state.input_pad_ref, item}} |
111 |
| - end) |
112 |
| - |
113 |
| - {actions, %{state | queue: queue}} |
114 |
| - end |
115 |
| - |
116 |
| - defp maybe_flush_queue(_ctx, state), do: {[], state} |
117 |
| -end |
| 1 | +# defmodule Membrane.Transcoder.ForwardingFilter do |
| 2 | +# @moduledoc false |
| 3 | +# use Membrane.Filter |
| 4 | + |
| 5 | +# alias Membrane.TimestampQueue |
| 6 | + |
| 7 | +# def_input_pad :input, |
| 8 | +# accepted_format: _any, |
| 9 | +# availability: :on_request |
| 10 | + |
| 11 | +# def_output_pad :output, |
| 12 | +# accepted_format: _any, |
| 13 | +# availability: :on_request |
| 14 | + |
| 15 | +# defguardp is_input_linked(state) when state.input_pad_ref != nil |
| 16 | +# defguardp is_output_linked(state) when state.output_pad_ref != nil |
| 17 | + |
| 18 | +# @impl true |
| 19 | +# def handle_init(_ctx, _opts) do |
| 20 | +# state = %{queue: TimestampQueue.new(), output_pad_ref: nil, input_pad_ref: nil} |
| 21 | +# {[], state} |
| 22 | +# end |
| 23 | + |
| 24 | +# @impl true |
| 25 | +# def handle_playing(ctx, state), do: maybe_flush_queue(ctx, state) |
| 26 | + |
| 27 | +# @impl true |
| 28 | +# def handle_pad_added(Pad.ref(direction, _id) = pad_ref, ctx, state) do |
| 29 | +# same_direction_pads_number = |
| 30 | +# ctx.pads |
| 31 | +# |> Enum.count(fn {_pad_ref, pad_data} -> pad_data.direction == direction end) |
| 32 | + |
| 33 | +# if same_direction_pads_number > 1 do |
| 34 | +# raise """ |
| 35 | +# #{inspect(__MODULE__)} can have only one #{inspect(direction)} pad, but it has \ |
| 36 | +# #{same_direction_pads_number} |
| 37 | +# """ |
| 38 | +# end |
| 39 | + |
| 40 | +# state = |
| 41 | +# case direction do |
| 42 | +# :input -> %{state | input_pad_ref: pad_ref} |
| 43 | +# :output -> %{state | output_pad_ref: pad_ref} |
| 44 | +# end |
| 45 | + |
| 46 | +# maybe_flush_queue(ctx, state) |
| 47 | +# end |
| 48 | + |
| 49 | +# @impl true |
| 50 | +# def handle_stream_format(_input_pad_ref, stream_format, _ctx, state) |
| 51 | +# when is_output_linked(state) do |
| 52 | +# {[ |
| 53 | +# stream_format: {state.output_pad_ref, stream_format}, |
| 54 | +# notify_parent: {:stream_format, stream_format} |
| 55 | +# ], state} |
| 56 | +# end |
| 57 | + |
| 58 | +# @impl true |
| 59 | +# def handle_stream_format(input_pad_ref, stream_format, _ctx, state) do |
| 60 | +# queue = TimestampQueue.push_stream_format(state.queue, input_pad_ref, stream_format) |
| 61 | +# {[notify_parent: {:stream_format, stream_format}], %{state | queue: queue}} |
| 62 | +# end |
| 63 | + |
| 64 | +# @impl true |
| 65 | +# def handle_buffer(_input_pad_ref, buffer, _ctx, state) when is_output_linked(state) do |
| 66 | +# {[buffer: {state.output_pad_ref, buffer}], state} |
| 67 | +# end |
| 68 | + |
| 69 | +# @impl true |
| 70 | +# def handle_buffer(input_pad_ref, buffer, _ctx, state) do |
| 71 | +# {_suggested_actions, queue} = TimestampQueue.push_buffer(state.queue, input_pad_ref, buffer) |
| 72 | +# {[], %{state | queue: queue}} |
| 73 | +# end |
| 74 | + |
| 75 | +# @impl true |
| 76 | +# def handle_event(Pad.ref(:input, _id), event, _ctx, state) when is_output_linked(state) do |
| 77 | +# {[forward: event], state} |
| 78 | +# end |
| 79 | + |
| 80 | +# @impl true |
| 81 | +# def handle_event(Pad.ref(:output, _id), event, _ctx, state) when is_input_linked(state) do |
| 82 | +# {[forward: event], state} |
| 83 | +# end |
| 84 | + |
| 85 | +# @impl true |
| 86 | +# def handle_event(pad_ref, event, _ctx, state) do |
| 87 | +# queue = TimestampQueue.push_event(state.queue, pad_ref, event) |
| 88 | +# {[], %{state | queue: queue}} |
| 89 | +# end |
| 90 | + |
| 91 | +# @impl true |
| 92 | +# def handle_end_of_stream(_input_pad_ref, _ctx, state) when is_output_linked(state) do |
| 93 | +# {[end_of_stream: state.output_pad_ref], state} |
| 94 | +# end |
| 95 | + |
| 96 | +# @impl true |
| 97 | +# def handle_end_of_stream(input_pad_ref, _ctx, state) do |
| 98 | +# queue = TimestampQueue.push_end_of_stream(state.queue, input_pad_ref) |
| 99 | +# {[], %{state | queue: queue}} |
| 100 | +# end |
| 101 | + |
| 102 | +# defp maybe_flush_queue(ctx, state) |
| 103 | +# when ctx.playback == :playing and is_input_linked(state) and is_output_linked(state) do |
| 104 | +# {_suggested_actions, items, queue} = TimestampQueue.flush_and_close(state.queue) |
| 105 | + |
| 106 | +# actions = |
| 107 | +# Enum.map(items, fn |
| 108 | +# {Pad.ref(:input, _id), {item_type, item}} -> {item_type, {state.output_pad_ref, item}} |
| 109 | +# {Pad.ref(:input, _id), :end_of_stream} -> {:end_of_stream, state.output_pad_ref} |
| 110 | +# {Pad.ref(:output, _id), {:event, item}} -> {:event, {state.input_pad_ref, item}} |
| 111 | +# end) |
| 112 | + |
| 113 | +# {actions, %{state | queue: queue}} |
| 114 | +# end |
| 115 | + |
| 116 | +# defp maybe_flush_queue(_ctx, state), do: {[], state} |
| 117 | +# end |
0 commit comments