Skip to content

Commit

Permalink
Add resampler and sample rate constraints for AAC. Make sure redundan…
Browse files Browse the repository at this point in the history
…t specs are not created when second stream format arrives
  • Loading branch information
varsill committed Nov 14, 2024
1 parent cd0b064 commit d013850
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/transcoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ defmodule Membrane.Transcoder do
end

@impl true
def handle_child_notification({:stream_format, format}, :forwarding_filter, _ctx, state) do
def handle_child_notification(
{:stream_format, format},
:forwarding_filter,
_ctx,
%{input_stream_format: nil} = state
) do
state =
%{state | input_stream_format: format}
|> resolve_output_stream_format()
Expand Down
36 changes: 33 additions & 3 deletions lib/transcoder/audio.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
defmodule Boombox.Transcoder.Audio do
defmodule Membrane.Transcoder.Audio do
@moduledoc false

import Membrane.ChildrenSpec
require Membrane.Logger
alias Membrane.{AAC, ChildrenSpec, Opus, RawAudio, RemoteStream}

@opus_sample_rate 48_000
@aac_sample_rates [
96_000,
88_200,
64_000,
48_000,
44_100,
32_000,
24_000,
22_050,
16_000,
12_000,
11_025,
8000
]

@type audio_stream_format :: AAC.t() | Opus.t() | RawAudio.t()

defguard is_audio_format(format)
when is_struct(format) and
(format.__struct__ in [AAC, Opus, RawAudio] or
(format.__struct__ == RemoteStream and format.content_format == Opus and
(format.__struct__ == RemoteStream and format.content_format in [Opus, AAC] and
format.type == :packetized))

@spec plug_audio_transcoding(
Expand All @@ -34,7 +48,11 @@ defmodule Boombox.Transcoder.Audio do
end

defp do_plug_audio_transcoding(builder, %RemoteStream{content_format: Opus}, %Opus{}) do
builder |> child(:opus_parser, Opus.Parser)
builder
end

defp do_plug_audio_transcoding(builder, %RemoteStream{content_format: AAC}, %AAC{}) do
builder
end

defp do_plug_audio_transcoding(builder, input_format, output_format) do
Expand Down Expand Up @@ -72,6 +90,18 @@ defmodule Boombox.Transcoder.Audio do
})
end

defp maybe_plug_resampler(builder, %{sample_rate: sample_rate} = input_format, %AAC{})
when sample_rate not in @aac_sample_rates do
builder
|> child(:resampler, %Membrane.FFmpeg.SWResample.Converter{
output_stream_format: %RawAudio{
sample_format: :s16le,
sample_rate: 44_100,
channels: input_format.channels
}
})
end

defp maybe_plug_resampler(builder, _input_format, _output_format) do
builder
end
Expand Down

0 comments on commit d013850

Please sign in to comment.