Skip to content

Commit

Permalink
Rewrite the ISOM demuxer so that it uses output pad with :auto flow c…
Browse files Browse the repository at this point in the history
…ontrol
  • Loading branch information
varsill committed Mar 8, 2024
1 parent 8d5cf44 commit d24028e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 48 deletions.
63 changes: 18 additions & 45 deletions lib/membrane_mp4/demuxer/isom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
accepted_format:
%RemoteStream{type: :bytestream, content_format: content_format}
when content_format in [nil, MP4],
flow_control: :manual,
demand_unit: :buffers
flow_control: :auto

def_output_pad :output,
accepted_format:
Expand All @@ -36,7 +35,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
%Membrane.Opus{self_delimiting?: false}
),
availability: :on_request,
flow_control: :manual
flow_control: :auto

def_options optimize_for_non_fast_start?: [
default: false,
Expand Down Expand Up @@ -94,7 +93,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
if state.optimize_for_non_fast_start? do
seek(state, :bof, :infinity, false)
else
{[demand: :input], state}
{[], state}
end
end

Expand All @@ -121,28 +120,6 @@ defmodule Membrane.MP4.Demuxer.ISOM do
{[], state}
end

@impl true
def handle_demand(
Pad.ref(:output, _track_id),
_size,
:buffers,
ctx,
%{fsm_state: :samples_info_present_and_all_pads_connected} = state
) do
size =
Map.values(ctx.pads)
|> Enum.filter(&(&1.direction == :output))
|> Enum.map(& &1.demand)
|> Enum.max()

{[demand: {:input, size}], state}
end

@impl true
def handle_demand(Pad.ref(:output, _track_id), _size, :buffers, _ctx, state) do
{[], state}
end

def handle_buffer(
:input,
_buffer,
Expand All @@ -152,29 +129,25 @@ defmodule Membrane.MP4.Demuxer.ISOM do
} = state
)
when fsm_state in [:mdat_skipping, :going_back_to_mdat] do
{[demand: :input], state}
{[], state}
end

@impl true
def handle_buffer(
:input,
buffer,
ctx,
_ctx,
%{
fsm_state: :samples_info_present_and_all_pads_connected
} = state
) do
{samples, rest, samples_info} =
SamplesInfo.get_samples(state.samples_info, state.partial <> buffer.payload)

# |> IO.inspect(label: :buf2)
buffers = get_buffer_actions(samples)

redemands =
ctx.pads
|> Enum.filter(fn {pad, _pad_data} -> match?(Pad.ref(:output, _ref), pad) end)
|> Enum.flat_map(fn {pad, _pad_data} -> [redemand: pad] end)

{buffers ++ redemands, %{state | samples_info: samples_info, partial: rest}}
{buffers, %{state | samples_info: samples_info, partial: rest}}
end

def handle_buffer(
Expand Down Expand Up @@ -234,7 +207,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
handle_non_fast_start_optimization(state)

true ->
{[demand: :input], state}
{[], state}
end
end

Expand Down Expand Up @@ -317,7 +290,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
end

defp handle_non_fast_start_optimization(state) do
{[demand: :input], state}
{[], state}
end

defp seek(state, start, size_to_read, last?) do
Expand All @@ -326,8 +299,7 @@ defmodule Membrane.MP4.Demuxer.ISOM do
{[
event:
{:input,
%Membrane.File.SeekSourceEvent{start: start, size_to_read: size_to_read, last?: last?}},
demand: :input
%Membrane.File.SeekSourceEvent{start: start, size_to_read: size_to_read, last?: last?}}
], state}
end

Expand Down Expand Up @@ -371,16 +343,17 @@ defmodule Membrane.MP4.Demuxer.ISOM do
{[], store_samples(state, samples)}
end

redemands =
ctx.pads
|> Enum.filter(fn {pad, _pad_data} -> match?(Pad.ref(:output, _ref), pad) end)
|> Enum.flat_map(fn {pad, _pad_data} -> [redemand: pad] end)

notifications = get_track_notifications(state)
stream_format = if all_pads_connected?, do: get_stream_format(state), else: []

state = %{state | all_pads_connected?: all_pads_connected?} |> update_fsm_state()
{seek_events ++ notifications ++ stream_format ++ buffers ++ redemands, state}
state =
%{
state
| all_pads_connected?: all_pads_connected?
}
|> update_fsm_state()

{seek_events ++ notifications ++ stream_format ++ buffers, state}
end

defp store_samples(state, samples) do
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"bimap": {:hex, :bimap, "1.3.0", "3ea4832e58dc83a9b5b407c6731e7bae87458aa618e6d11d8e12114a17afa4b3", [:mix], [], "hexpm", "bf5a2b078528465aa705f405a5c638becd63e41d280ada41e0f77e6d255a10b4"},
"bunch": {:hex, :bunch, "1.6.0", "4775f8cdf5e801c06beed3913b0bd53fceec9d63380cdcccbda6be125a6cfd54", [:mix], [], "hexpm", "ef4e9abf83f0299d599daed3764d19e8eac5d27a5237e5e4d5e2c129cfeb9a22"},
"bunch": {:hex, :bunch, "1.6.1", "5393d827a64d5f846092703441ea50e65bc09f37fd8e320878f13e63d410aec7", [:mix], [], "hexpm", "286cc3add551628b30605efbe2fca4e38cc1bea89bcd0a1a7226920b3364fe4a"},
"bunch_native": {:hex, :bunch_native, "0.5.0", "8ac1536789a597599c10b652e0b526d8833348c19e4739a0759a2bedfd924e63", [:mix], [{:bundlex, "~> 1.0", [hex: :bundlex, repo: "hexpm", optional: false]}], "hexpm", "24190c760e32b23b36edeb2dc4852515c7c5b3b8675b1a864e0715bdd1c8f80d"},
"bundlex": {:hex, :bundlex, "1.2.0", "a89869208a019376a38e8a10e1bd573dcbeae8addd381c2cd74e2817010bef8f", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:req, "~> 0.4.0", [hex: :req, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}, {:zarex, "~> 1.0", [hex: :zarex, repo: "hexpm", optional: false]}], "hexpm", "d2182b91a2a53847baadf4745ad2291853e786ad28671f474a611e7703dbca9b"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
Expand All @@ -24,11 +24,11 @@
"membrane_aac_plugin": {:hex, :membrane_aac_plugin, "0.18.0", "b65697fa31f25ee35520f4f402cfdc6af847bb508f39a3bdd295f117fa1ce59b", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:crc, "~> 0.10.2", [hex: :crc, repo: "hexpm", optional: false]}, {:membrane_aac_format, "~> 0.8.0", [hex: :membrane_aac_format, repo: "hexpm", optional: false]}, {:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "cefaef80e0d1b94ef294127db79f3af0cbc2c691e15c3bb9a4c86bf419839a72"},
"membrane_cmaf_format": {:hex, :membrane_cmaf_format, "0.7.0", "573bfff6acf2371c5046b9174569f6316f4205e3d6e13e814bf7e613e5653a54", [:mix], [], "hexpm", "4ac6a24a33f61347a2714c982a5f84aa6207641f4de2ad5afde68a8b800da8de"},
"membrane_common_c": {:hex, :membrane_common_c, "0.16.0", "caf3f29d2f5a1d32d8c2c122866110775866db2726e4272be58e66dfdf4bce40", [:mix], [{:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:shmex, "~> 0.5.0", [hex: :shmex, repo: "hexpm", optional: false]}, {:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "a3c7e91de1ce1f8b23b9823188a5d13654d317235ea0ca781c05353ed3be9b1c"},
"membrane_core": {:hex, :membrane_core, "1.0.0", "1b543aefd952283be1f2a215a1db213aa4d91222722ba03cd35280622f1905ee", [:mix], [{:bunch, "~> 1.6", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.3", [hex: :qex, repo: "hexpm", optional: false]}, {:ratio, "~> 3.0", [hex: :ratio, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "352c90fd0a29942143c4bf7a727cc05c632e323f50a1a4e99321b1e8982f1533"},
"membrane_core": {:hex, :membrane_core, "1.0.1", "08aa546c0d131c66f8b906b3dfb2b8f2749b56859f6fc52bd3ac846b944b3baa", [:mix], [{:bunch, "~> 1.6", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.3", [hex: :qex, repo: "hexpm", optional: false]}, {:ratio, "~> 3.0", [hex: :ratio, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a35ed68561bdf0a2dbb2f994333be78cf4e1c4d734e4cd927d77d92049bb1273"},
"membrane_fake_plugin": {:hex, :membrane_fake_plugin, "0.11.0", "3a2d26f15ad4940a4d44cee3354dff38fa9a39963e9b2dcb49802e150ff9a9dc", [:mix], [{:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "7c6b6a06eaa4e820d1e4836510ddb4bcb386c8918d0b37542a21caf6b87cbe72"},
"membrane_file_plugin": {:hex, :membrane_file_plugin, "0.16.0", "7917f6682c22b9bcfc2ca20ed960eee0f7d03ad31fd5f59ed850f1fe3ddd545a", [:mix], [{:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "b0727998f75a9b4dab8a2baefdfc13c3eac00a04e061ab1b0e61dc5566927acc"},
"membrane_h264_format": {:hex, :membrane_h264_format, "0.6.1", "44836cd9de0abe989b146df1e114507787efc0cf0da2368f17a10c47b4e0738c", [:mix], [], "hexpm", "4b79be56465a876d2eac2c3af99e115374bbdc03eb1dea4f696ee9a8033cd4b0"},
"membrane_h264_plugin": {:hex, :membrane_h264_plugin, "0.9.0", "4b76079489451794c27993fbe6108f10a34068d3034985dc73d601e46266ef90", [:mix], [{:bunch, "~> 1.4", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:membrane_h264_format, "~> 0.6.0", [hex: :membrane_h264_format, repo: "hexpm", optional: false]}], "hexpm", "7ecb9fdbe26304a577d9e32895aeee963b0a4bcc01fd77ecd9aebabdafbce58d"},
"membrane_h264_plugin": {:hex, :membrane_h264_plugin, "0.9.3", "5b9407c5eaff97b04667778e62f5f851011aeea2494fa2d14fc2448109babe7a", [:mix], [{:bunch, "~> 1.4", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 1.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:membrane_h264_format, "~> 0.6.0", [hex: :membrane_h264_format, repo: "hexpm", optional: false]}], "hexpm", "588e2e36051bc14d9a1e47dc6272fdccd6d24a7307fe7b4e417aa66afa962994"},
"membrane_h265_format": {:hex, :membrane_h265_format, "0.2.0", "1903c072cf7b0980c4d0c117ab61a2cd33e88782b696290de29570a7fab34819", [:mix], [], "hexpm", "6df418bdf242c0d9f7dbf2e5aea4c2d182e34ac9ad5a8b8cef2610c290002e83"},
"membrane_mp4_format": {:hex, :membrane_mp4_format, "0.8.0", "8c6e7d68829228117d333b4fbb030e7be829aab49dd8cb047fdc664db1812e6a", [:mix], [], "hexpm", "148dea678a1f82ccfd44dbde6f936d2f21255f496cb45a22cc6eec427f025522"},
"membrane_opus_format": {:hex, :membrane_opus_format, "0.3.0", "3804d9916058b7cfa2baa0131a644d8186198d64f52d592ae09e0942513cb4c2", [:mix], [], "hexpm", "8fc89c97be50de23ded15f2050fe603dcce732566fe6fdd15a2de01cb6b81afe"},
Expand Down

0 comments on commit d24028e

Please sign in to comment.