Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
09ac83f
Add Demuxer.ISOM.Engine and use it to write a DemuxingSource. Add tes…
varsill Jul 21, 2025
45f61bd
Move Demuxer.CMAF.Engine.Sample to Demuxer namespace and use it in th…
varsill Jul 21, 2025
d33c466
Add typedoc
varsill Jul 21, 2025
13f971a
Add typedoc
varsill Jul 21, 2025
678520a
Fix dialyzer
varsill Jul 21, 2025
2a6f92b
Add more tests for demuxing source
varsill Jul 21, 2025
52ad823
Add more tests for demuxing source
varsill Jul 22, 2025
19189ae
Fix credo warnings
varsill Jul 22, 2025
8fdfbf8
Fix doc
varsill Jul 22, 2025
5693db3
Add kind option to output pad of the DemuxingSource
varsill Jul 22, 2025
123c165
Modify test to make sure audio kind is also properly resolved
varsill Jul 22, 2025
b12023d
Add provider_state
varsill Jul 22, 2025
2334b24
Improve test
varsill Jul 22, 2025
dc43cbc
rename start_at_ms to start_at
varsill Jul 24, 2025
397693f
Specify demand_unit
varsill Jul 25, 2025
55568c8
Add debug logs
varsill Jul 25, 2025
8a59982
Add debug logs
varsill Jul 25, 2025
58f6c67
Improve read_sample
varsill Jul 25, 2025
46331da
Properly handle seeking in samples
varsill Jul 25, 2025
0a4ad46
Remove debug logs
varsill Jul 25, 2025
c4b2e89
Properly handle updating of the last_dts
varsill Jul 25, 2025
7314e44
Rename last_dts to next_dts
varsill Jul 25, 2025
fcd6a03
Remove unused pos field in engine state. Fix dialyzer warning
varsill Jul 28, 2025
c744c24
Format the code
varsill Jul 28, 2025
d88695d
Update lib/membrane_mp4/demuxer/demuxing_source.ex
varsill Jul 29, 2025
d448116
Update lib/membrane_mp4/demuxer/demuxing_source.ex
varsill Jul 29, 2025
e31de40
Update lib/membrane_mp4/demuxer/isom/engine.ex
varsill Jul 29, 2025
591a586
Update lib/membrane_mp4/demuxer/isom/engine.ex
varsill Jul 29, 2025
fba8629
Perform formatting
varsill Jul 29, 2025
0c3c562
Improve moduledoc
varsill Jul 29, 2025
325d356
Fix tests
varsill Jul 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/membrane_mp4/demuxer/cmaf/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Membrane.MP4.Demuxer.CMAF.Engine do

alias Membrane.MP4.Container
alias Membrane.MP4.Demuxer.CMAF.SamplesInfo
alias Membrane.MP4.Demuxer.Sample

defstruct [
:samples_to_pop,
Expand Down Expand Up @@ -82,11 +83,11 @@ defmodule Membrane.MP4.Demuxer.CMAF.Engine do
Returns a tuple with `:ok` and a list of samples, and the updated demuxer engine
state.

The samples are instances of `Membrane.MP4.Demuxer.CMAF.Sample`.
The samples are instances of `Membrane.MP4.Demuxer.Sample`.

If no samples are available, it returns an empty list.
"""
@spec pop_samples(t()) :: {:ok, [__MODULE__.Sample.t()], t()}
@spec pop_samples(t()) :: {:ok, [Sample.t()], t()}
def pop_samples(%__MODULE__{} = engine) do
{:ok, engine.samples_to_pop, %{engine | samples_to_pop: []}}
end
Expand Down Expand Up @@ -204,7 +205,7 @@ defmodule Membrane.MP4.Demuxer.CMAF.Engine do
|> Ratio.mult(1000)
|> Ratio.floor()

%__MODULE__.Sample{track_id: sample.track_id, payload: payload, pts: pts, dts: dts}
%Sample{track_id: sample.track_id, payload: payload, pts: pts, dts: dts}
end)

{samples, %{engine | samples_info: rest_of_samples_info}}
Expand Down
185 changes: 185 additions & 0 deletions lib/membrane_mp4/demuxer/demuxing_source.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
defmodule Membrane.MP4.Demuxer.DemuxingSource do
@moduledoc """
A Membrane Source capable of reading streams from the MP4 file.

It requires specifying `provide_data_callback` - a function that will be called
each time data from MP4 needs to be read.
If you know in advance what kind of track you want to read from the MP4, you can
link the element's `Pad.ref(:output, id)` (with any desired `id`) pad with the
`kind: :audio | :video` option provided.
Alternatively, you can spawn the element without linking its pads and wait until
the tracks are resolved. Once the Demuxer identifies the tracks in the MP4, `t:new_tracks_t/0`
notification is sent with description of each of the tracks and a corresponding `track_id`.
The parent can then link `Pad.ref(:output, track_id)` for desired tracks.
"""
use Membrane.Source
alias Membrane.MP4.Demuxer.ISOM.Engine

@typedoc """
Notification sent when the tracks are identified in the MP4.

Upon receiving the notification, `Pad.ref(:output, track_id)` pads should be linked
for the desired `track_id`s in the list.
The `content` field contains the stream format describing given track.
"""
@type new_tracks_t() ::
{:new_tracks, [{track_id :: integer(), content :: struct()}]}

def_output_pad :output,
accepted_format:
any_of(
%Membrane.AAC{config: {:esds, _esds}},
%Membrane.H264{
stream_structure: {_avc, _dcr},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change _avc to :avc? This same in H265 below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that _avc stands for either :avc1 or :avc3, I can add a guard

alignment: :au
},
%Membrane.H265{
stream_structure: {_hevc, _dcr},
alignment: :au
},
%Membrane.Opus{self_delimiting?: false}
),
availability: :on_request,
flow_control: :manual,
options: [
kind: [
spec: :video | :audio | nil,
default: nil,
description: """
Specifies the decoding timestamp of
the first sample that should be read from each of the tracks.
If there is no sample with exactly such a timestamp, that sample
will be the first sample with DTS greater than provided timestamp.
"""
]
],
demand_unit: :buffers

def_options provide_data_cb: [
spec: Engine.provide_data_cb(),
description: """
A function that will be called each time the `#{inspect(__MODULE__)}`
needs data. It should read desired number of bytes from MP4 file,
starting at given position.
"""
],
start_at: [
spec: non_neg_integer(),
default: 0,
description: """
Specifies the decoding timestamp of
the first sample that should be read from each of the tracks.

If there is no sample with exactly such a timestamp, that sample
will be the first sample with DTS greater than provided timestamp.
"""
]

@impl true
def handle_init(_ctx, opts) do
state =
Map.from_struct(opts)
|> Map.merge(%{
engine: nil,
pad_to_track_id: %{}
})

{[], state}
end

@impl true
def handle_setup(_ctx, state) do
engine = Engine.new(state.provide_data_cb)
track_ids = Engine.get_tracks_info(engine) |> Map.keys()

engine =
Enum.reduce(
track_ids,
engine,
&Engine.seek_in_samples(&2, &1, Membrane.Time.as_milliseconds(state.start_at, :round))
)

{[], %{state | engine: engine}}
end

@impl true
def handle_playing(_ctx, state) do
new_tracks =
state.engine
|> Engine.get_tracks_info()
|> reject_unsupported_sample_types()
|> Enum.map(fn {track_id, table} ->
{track_id, table.sample_description}
end)

{[notify_parent: {:new_tracks, new_tracks}], state}
end

@impl true
def handle_demand(pad, _demand_size, _demand_unit, ctx, state) do
track_id = state.pad_to_track_id[pad]

case Engine.read_sample(state.engine, track_id) do
:end_of_stream ->
{[end_of_stream: pad], state}

{:ok, sample, engine} ->
buffer = %Membrane.Buffer{
payload: sample.payload,
pts: Ratio.new(sample.pts) |> Membrane.Time.milliseconds(),
dts: Ratio.new(sample.dts) |> Membrane.Time.milliseconds()
}

state = %{state | engine: engine}

maybe_send_stream_format =
if ctx.pads[pad].stream_format == nil do
stream_format =
state.engine
|> Engine.get_tracks_info()
|> Map.get(track_id)
|> Map.get(:sample_description)

[{:stream_format, {pad, stream_format}}]
else
[]
end

{maybe_send_stream_format ++ [buffer: {pad, buffer}, redemand: pad], state}
end
end

@impl true
def handle_pad_added(Pad.ref(:output, pad_id) = pad, ctx, state) do
tracks_info = Engine.get_tracks_info(state.engine)

track_id =
cond do
ctx.pad_options.kind != nil ->
{track_id, _track} =
Enum.find(tracks_info, fn {_track_id, track} ->
resolve_kind(track.sample_description) == ctx.pad_options.kind
end)

track_id

pad_id in Map.keys(tracks_info) ->
pad_id

true ->
raise "Couldn't find track corresponding to pad: #{inspect(pad)}. The available tracks are: #{Map.keys(tracks_info) |> inspect()}"
end

state = put_in(state.pad_to_track_id[pad], track_id)
{[], state}
end

defp resolve_kind(%Membrane.AAC{}), do: :audio
defp resolve_kind(%Membrane.Opus{}), do: :audio
defp resolve_kind(%Membrane.H264{}), do: :video
defp resolve_kind(%Membrane.H265{}), do: :video

defp reject_unsupported_sample_types(sample_tables) do
Map.reject(sample_tables, fn {_track_id, table} -> table.sample_description == nil end)
end
end
Loading