Skip to content

Commit

Permalink
Separate muxers
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgiuliani committed Oct 2, 2024
1 parent 443658c commit 752736f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
43 changes: 11 additions & 32 deletions lib/membrane/hls/sink_bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ defmodule Membrane.HLS.SinkBin do
bin_input(pad)
# |> child({:shifter, track_id}, %Membrane.HLS.Shifter{duration: max_pts})
|> via_in(Pad.ref(:input, track_id))
|> audio_muxer(state)
|> child({:muxer, track_id}, %Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration_audio(state)
})
|> via_out(Pad.ref(:output), options: [tracks: [track_id]])
|> child({:sink, track_id}, %Membrane.HLS.CMAFSink{
packager_pid: state.packager_pid,
Expand All @@ -143,33 +145,17 @@ defmodule Membrane.HLS.SinkBin do
@impl true
def handle_pad_added(
Pad.ref(:input, track_id) = pad,
%{pad_options: %{encoding: :H264} = pad_opts} = ctx,
%{pad_options: %{encoding: :H264} = pad_opts},
state
) do
{_max_pts, _track_pts} = resume_info(state.packager_pid, track_id)

had_video_input? =
Enum.any?(ctx.pads, fn {Pad.ref(:input, id), data} ->
id != track_id and data.options.encoding == :H264
end)

muxer = fn spec ->
if had_video_input? do
child(spec, {:muxer, track_id}, %Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration(state)
})
else
spec
|> via_in(Pad.ref(:input, track_id))
|> audio_muxer(state)
|> via_out(Pad.ref(:output), options: [tracks: [track_id]])
end
end

spec =
bin_input(pad)
# |> child({:shifter, track_id}, %Membrane.HLS.Shifter{duration: max_pts})
|> muxer.()
|> child({:muxer, track_id}, %Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration_video(state)
})
|> child({:sink, track_id}, %Membrane.HLS.CMAFSink{
packager_pid: state.packager_pid,
track_id: track_id,
Expand Down Expand Up @@ -259,19 +245,12 @@ defmodule Membrane.HLS.SinkBin do
|> MapSet.equal?(ended_sinks)
end

defp audio_muxer(spec, state) do
child(
spec,
{:muxer, :audio},
%Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration(state)
},
get_if_exists: true
)
defp segment_min_duration_video(state) do
state.opts.target_segment_duration - Membrane.Time.seconds(2)
end

defp segment_min_duration(state) do
state.opts.target_segment_duration - Membrane.Time.seconds(2)
defp segment_min_duration_audio(state) do
state.opts.target_segment_duration - Membrane.Time.seconds(1)
end

defp resume_info(packager_pid, track_id) do
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"bunch": {:hex, :bunch, "1.6.1", "5393d827a64d5f846092703441ea50e65bc09f37fd8e320878f13e63d410aec7", [:mix], [], "hexpm", "286cc3add551628b30605efbe2fca4e38cc1bea89bcd0a1a7226920b3364fe4a"},
"coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
"heap": {:hex, :heap, "2.0.2", "d98cb178286cfeb5edbcf17785e2d20af73ca57b5a2cf4af584118afbcf917eb", [:mix], [], "hexpm", "ba9ea2fe99eb4bcbd9a8a28eaf71cbcac449ca1d8e71731596aace9028c9d429"},
"kim_hls": {:git, "https://github.com/kim-company/kim_hls.git", "349d56e5d7d22ba69a3cc10bfca647bef5639d3c", []},
"kim_hls": {:git, "https://github.com/kim-company/kim_hls.git", "8d9526dd0ed72aad6ac8a54348bb75080a14b037", []},
"kim_q": {:hex, :kim_q, "1.0.0", "17cfc45e9f7e65485f0f31bbf09893d6ff35cc2fbefc39aed146a3c29740584e", [:mix], [{:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7a8ee76a2c2e774c34345df3c7a234a8effeedc3f3aea845feb7c09030097278"},
"kim_subtitle": {:git, "https://github.com/kim-company/kim_subtitle.git", "8239e1bcea938167829a6b8bd2a9678c63c7bdd4", []},
"logger_backends": {:hex, :logger_backends, "1.0.0", "09c4fad6202e08cb0fbd37f328282f16539aca380f512523ce9472b28edc6bdf", [:mix], [], "hexpm", "1faceb3e7ec3ef66a8f5746c5afd020e63996df6fd4eb8cdb789e5665ae6c9ce"},
Expand Down

0 comments on commit 752736f

Please sign in to comment.