Skip to content

Commit 37022e6

Browse files
committed
Change option and stop ignoring input format
1 parent ce5eda3 commit 37022e6

2 files changed

Lines changed: 56 additions & 28 deletions

File tree

lib/membrane_opus/parser.ex

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,26 @@ defmodule Membrane.Opus.Parser do
2121
spec: delimitation_t(),
2222
default: :keep,
2323
description: """
24-
If input is delimitted? (as indicated by the `self_delimiting?`
25-
field in %Opus) and `:undelimit` is selected, will remove delimiting.
24+
If input is self-delimiting and `:undelimit` is selected, delimiting will be removed.
2625
27-
If input is not delimitted? and `:delimit` is selected, will add delimiting.
26+
If input is not self-delimiting and `:delimit` is selected, delimiting will be added.
2827
29-
If `:keep` is selected, will not change delimiting.
30-
31-
Otherwise will act like `:keep`.
28+
If `:keep` is selected, delimiting will stay unchanged.
3229
3330
See https://tools.ietf.org/html/rfc6716#appendix-B for details
3431
on the self-delimiting Opus format.
3532
"""
3633
],
37-
input_delimitted?: [
34+
# Remote
35+
# Opus undel
36+
# Opus del
37+
assume_input_self_delimiting?: [
3838
spec: boolean(),
3939
default: false,
4040
description: """
41-
If you know that the input is self-delimitted? but you're reading from
42-
some element that isn't sending the correct structure, you can set this
43-
to true to force the Parser to assume the input is self-delimitted? and
44-
ignore upstream stream_format information on self-delimitation.
41+
If an input stream format is a `Membrane.RemoteStream`, then the
42+
delimitation provided with this option will be assumed.
43+
If it's `Membrane.Opus`, then this option will be ignored.
4544
"""
4645
],
4746
generate_best_effort_timestamps?: [
@@ -60,23 +59,49 @@ defmodule Membrane.Opus.Parser do
6059

6160
def_output_pad :output, accepted_format: Opus
6261

62+
defmodule State do
63+
@moduledoc false
64+
65+
alias Membrane.Opus.Parser
66+
67+
@type t :: %__MODULE__{
68+
delimitation: Parser.delimitation_t(),
69+
assume_input_self_delimiting?: boolean(),
70+
generate_best_effort_timestamps?: boolean(),
71+
current_pts: Membrane.Time.t() | nil,
72+
queue: binary(),
73+
input_self_delimiting?: boolean()
74+
}
75+
76+
@enforce_keys [
77+
:delimitation,
78+
:assume_input_self_delimiting?,
79+
:generate_best_effort_timestamps?
80+
]
81+
82+
defstruct @enforce_keys ++
83+
[
84+
current_pts: nil,
85+
queue: <<>>,
86+
input_self_delimiting?: false
87+
]
88+
end
89+
6390
@impl true
64-
def handle_init(_ctx, %__MODULE__{} = options) do
65-
state =
66-
options
67-
|> Map.from_struct()
68-
|> Map.merge(%{
69-
current_pts: nil,
70-
queue: <<>>
71-
})
91+
def handle_init(_ctx, opts) do
92+
state = struct!(State, Map.from_struct(opts))
7293

7394
{[], state}
7495
end
7596

7697
@impl true
77-
def handle_stream_format(:input, _stream_format, _ctx, state) do
78-
# ignore stream_formats, they will be sent in handle_buffer
79-
{[], state}
98+
def handle_stream_format(:input, %RemoteStream{}, _ctx, state) do
99+
{[], %{state | input_self_delimiting?: state.assume_input_self_delimiting?}}
100+
end
101+
102+
@impl true
103+
def handle_stream_format(:input, %Opus{self_delimiting?: self_delimiting?}, _ctx, state) do
104+
{[], %{state | input_self_delimiting?: self_delimiting?}}
80105
end
81106

82107
defp set_current_pts(
@@ -95,7 +120,7 @@ defmodule Membrane.Opus.Parser do
95120
@impl true
96121
def handle_buffer(:input, %Buffer{payload: data, pts: input_pts}, ctx, state) do
97122
{delimitation_processor, self_delimiting?} =
98-
Delimitation.get_processor(state.delimitation, state.input_delimitted?)
123+
Delimitation.get_processor(state.delimitation, state.input_self_delimiting?)
99124

100125
check_pts_integrity? = state.queue != <<>> and not state.generate_best_effort_timestamps?
101126

@@ -153,7 +178,7 @@ defmodule Membrane.Opus.Parser do
153178
{:ok, _mode, _bandwidth, frame_duration} <-
154179
Util.parse_configuration(configuration_number),
155180
{:ok, header_size, frame_lengths, padding_size} <-
156-
FrameLengths.parse(frame_packing, data, state.input_delimitted?),
181+
FrameLengths.parse(frame_packing, data, state.input_self_delimiting?),
157182
expected_packet_size <- header_size + Enum.sum(frame_lengths) + padding_size,
158183
{:ok, raw_packet, rest} <- rest_of_packet(data, expected_packet_size) do
159184
duration = packet_duration(frame_lengths, frame_duration)

test/membrane_opus/parser_test.exs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ defmodule Membrane.Opus.Parser.ParserTest do
148148
output: buffers_from_fixtures(@fixtures, true),
149149
stream_format: %RemoteStream{type: :bytestream}
150150
})
151-
|> child(:parser, %Parser{input_delimitted?: true})
151+
|> child(:parser, %Parser{assume_input_self_delimiting?: true})
152152
|> child(:sink, Sink)
153153
]
154154

@@ -163,7 +163,7 @@ defmodule Membrane.Opus.Parser.ParserTest do
163163
output: buffers_from_fixtures(@fixtures, true),
164164
stream_format: %RemoteStream{type: :bytestream}
165165
})
166-
|> child(:parser, %Parser{delimitation: :undelimit, input_delimitted?: true})
166+
|> child(:parser, %Parser{delimitation: :undelimit, assume_input_self_delimiting?: true})
167167
|> child(:sink, Sink)
168168
]
169169

@@ -206,7 +206,10 @@ defmodule Membrane.Opus.Parser.ParserTest do
206206
output: buffers_from_fixtures(@fixtures, true, true),
207207
stream_format: %RemoteStream{type: :bytestream}
208208
})
209-
|> child(:parser, %Parser{input_delimitted?: true, generate_best_effort_timestamps?: true})
209+
|> child(:parser, %Parser{
210+
assume_input_self_delimiting?: true,
211+
generate_best_effort_timestamps?: true
212+
})
210213
|> child(:sink, Sink)
211214
]
212215

@@ -223,7 +226,7 @@ defmodule Membrane.Opus.Parser.ParserTest do
223226
})
224227
|> child(:parser, %Parser{
225228
delimitation: :undelimit,
226-
input_delimitted?: true,
229+
assume_input_self_delimiting?: true,
227230
generate_best_effort_timestamps?: true
228231
})
229232
|> child(:sink, Sink)

0 commit comments

Comments
 (0)