Skip to content

Commit

Permalink
Configure segment lengths for each output
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgiuliani committed Oct 2, 2024
1 parent 752736f commit 4ff9e69
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 3 additions & 0 deletions examples/rtmp.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule Pipeline do
|> via_in(Pad.ref(:input, "audio_128k"),
options: [
encoding: :AAC,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.CMAF.Track{} = format ->
%HLS.AlternativeRendition{
uri: uri,
Expand Down Expand Up @@ -75,6 +76,7 @@ defmodule Pipeline do
|> via_in(Pad.ref(:input, "video_720p"),
options: [
encoding: :H264,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.CMAF.Track{} = format ->
%HLS.VariantStream{
uri: uri,
Expand Down Expand Up @@ -108,6 +110,7 @@ defmodule Pipeline do
|> via_in(Pad.ref(:input, "video_360p"),
options: [
encoding: :H264,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.CMAF.Track{} = format ->
%HLS.VariantStream{
uri: uri,
Expand Down
20 changes: 9 additions & 11 deletions lib/membrane/hls/sink_bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ defmodule Membrane.HLS.SinkBin do
(URI.t(), Membrane.CMAF.Track.t() ->
HLS.VariantStream.t() | HLS.AlternativeRendition.t()),
description: "Build either a `HLS.VariantStream` or a `HLS.AlternativeRendition`."
],
segment_duration: [
spec: Membrane.Time.t(),
description: """
Duration for a HLS segment.
"""
]
]
)
Expand Down Expand Up @@ -129,7 +135,7 @@ defmodule Membrane.HLS.SinkBin do
# |> child({:shifter, track_id}, %Membrane.HLS.Shifter{duration: max_pts})
|> via_in(Pad.ref(:input, track_id))
|> child({:muxer, track_id}, %Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration_audio(state)
segment_min_duration: pad_opts.segment_duration
})
|> via_out(Pad.ref(:output), options: [tracks: [track_id]])
|> child({:sink, track_id}, %Membrane.HLS.CMAFSink{
Expand All @@ -154,7 +160,7 @@ defmodule Membrane.HLS.SinkBin do
bin_input(pad)
# |> child({:shifter, track_id}, %Membrane.HLS.Shifter{duration: max_pts})
|> child({:muxer, track_id}, %Membrane.MP4.Muxer.CMAF{
segment_min_duration: segment_min_duration_video(state)
segment_min_duration: pad_opts.segment_duration
})
|> child({:sink, track_id}, %Membrane.HLS.CMAFSink{
packager_pid: state.packager_pid,
Expand All @@ -179,7 +185,7 @@ defmodule Membrane.HLS.SinkBin do
# |> child({:filler, track_id}, %Membrane.HLS.TextFiller{from: track_pts})
|> child({:cues, track_id}, Membrane.WebVTT.CueBuilderFilter)
|> child({:segments, track_id}, %Membrane.WebVTT.SegmentFilter{
segment_duration: state.opts.target_segment_duration - Membrane.Time.second(),
segment_duration: pad_opts.segment_duration,
headers: [
%Subtitle.WebVTT.HeaderLine{key: :description, original: "WEBVTT"}
]
Expand Down Expand Up @@ -245,14 +251,6 @@ defmodule Membrane.HLS.SinkBin do
|> MapSet.equal?(ended_sinks)
end

defp segment_min_duration_video(state) do
state.opts.target_segment_duration - Membrane.Time.seconds(2)
end

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
Agent.get(packager_pid, fn packager ->
max_pts =
Expand Down
3 changes: 3 additions & 0 deletions test/membrane/hls/sink_bin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Membrane.HLS.SinkBinTest do
|> via_in(Pad.ref(:input, "audio_128k"),
options: [
encoding: :AAC,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.CMAF.Track{} = format ->
%HLS.AlternativeRendition{
uri: uri,
Expand Down Expand Up @@ -79,6 +80,7 @@ defmodule Membrane.HLS.SinkBinTest do
|> via_in(Pad.ref(:input, "subtitles"),
options: [
encoding: :TEXT,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.Text{} = format ->
%HLS.AlternativeRendition{
uri: uri,
Expand All @@ -105,6 +107,7 @@ defmodule Membrane.HLS.SinkBinTest do
|> via_in(Pad.ref(:input, "video_460x720"),
options: [
encoding: :H264,
segment_duration: Membrane.Time.seconds(6),
build_stream: fn uri, %Membrane.CMAF.Track{} = format ->
%HLS.VariantStream{
uri: uri,
Expand Down

0 comments on commit 4ff9e69

Please sign in to comment.