-
Notifications
You must be signed in to change notification settings - Fork 6
Create DemuxingSource element to simplify demuxing of ISOM MP4 files #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 45f61bd
Move Demuxer.CMAF.Engine.Sample to Demuxer namespace and use it in th…
varsill d33c466
Add typedoc
varsill 13f971a
Add typedoc
varsill 678520a
Fix dialyzer
varsill 2a6f92b
Add more tests for demuxing source
varsill 52ad823
Add more tests for demuxing source
varsill 19189ae
Fix credo warnings
varsill 8fdfbf8
Fix doc
varsill 5693db3
Add kind option to output pad of the DemuxingSource
varsill 123c165
Modify test to make sure audio kind is also properly resolved
varsill b12023d
Add provider_state
varsill 2334b24
Improve test
varsill dc43cbc
rename start_at_ms to start_at
varsill 397693f
Specify demand_unit
varsill 55568c8
Add debug logs
varsill 8a59982
Add debug logs
varsill 58f6c67
Improve read_sample
varsill 46331da
Properly handle seeking in samples
varsill 0a4ad46
Remove debug logs
varsill c4b2e89
Properly handle updating of the last_dts
varsill 7314e44
Rename last_dts to next_dts
varsill fcd6a03
Remove unused pos field in engine state. Fix dialyzer warning
varsill c744c24
Format the code
varsill d88695d
Update lib/membrane_mp4/demuxer/demuxing_source.ex
varsill d448116
Update lib/membrane_mp4/demuxer/demuxing_source.ex
varsill e31de40
Update lib/membrane_mp4/demuxer/isom/engine.ex
varsill 591a586
Update lib/membrane_mp4/demuxer/isom/engine.ex
varsill fba8629
Perform formatting
varsill 0c3c562
Improve moduledoc
varsill 325d356
Fix tests
varsill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}, | ||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we change
_avcto:avc? This same inH265belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that
_avcstands for either:avc1or:avc3, I can add a guard