Skip to content
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

Forward timestamps #57

Merged
merged 14 commits into from
Apr 3, 2024
16 changes: 12 additions & 4 deletions lib/membrane_ffmpeg_swresample_plugin/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
end

@impl true
def handle_buffer(:input, %Buffer{payload: payload}, _ctx, state) do
def handle_buffer(:input, %Buffer{payload: payload, pts: pts}, _ctx, state) do
state =
state
|> Map.merge(%{
current_pts: pts
})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
|> Map.merge(%{
current_pts: pts
})
|> Map.put(:current_pts, pts)

Copy link
Member

Choose a reason for hiding this comment

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

@bartkrak actually, the right approach would be to add this field in handle_init and do %{state | current_pts: pts}. Adding fields to state in various callbacks makes it hard to follow.


conversion_result =
convert!(state.native, RawAudio.frame_size(state.input_stream_format), payload, state.queue)

Expand All @@ -131,7 +137,7 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
{[], %{state | queue: queue}}

{converted, queue} ->
{[buffer: {:output, %Buffer{payload: converted}}], %{state | queue: queue}}
{[buffer: {:output, %Buffer{payload: converted, pts: pts}}], %{state | queue: queue}}
end
end

Expand All @@ -154,8 +160,10 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
{[end_of_stream: :output], %{state | queue: <<>>}}

converted ->
{[buffer: {:output, %Buffer{payload: converted}}, end_of_stream: :output],
%{state | queue: <<>>}}
{[
buffer: {:output, %Buffer{payload: converted, pts: state.current_pts}},
end_of_stream: :output
], %{state | queue: <<>>}}
end
end

Expand Down
21 changes: 20 additions & 1 deletion test/membrane_ffmpeg_swresample_plugin/converter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ defmodule Membrane.FFmpeg.SWResample.ConverterTest do
output_stream_format: @u8_format,
frames_per_buffer: 2048,
native: nil,
queue: <<>>
queue: <<>>,
current_pts: nil
}
}
end
Expand Down Expand Up @@ -178,5 +179,23 @@ defmodule Membrane.FFmpeg.SWResample.ConverterTest do
assert actions == [buffer: {:output, %Membrane.Buffer{payload: result}}]
assert new_state == %{state | queue: <<0::2*8>>}
end

test "timestamps forward test", %{state: initial_state} do
state = %{
initial_state
| queue: <<250, 250, 0>>,
native: :mock_handle,
input_stream_format: @s16le_format
}

payload = <<0::7*8>>
pts = 1000
buffer = %Membrane.Buffer{payload: payload, pts: pts}
result = <<250, 0, 0, 0>>
mock(@native, [convert: 2], {:ok, result})

assert {[buffer: {:output, %Membrane.Buffer{pts: ^pts}}], _state} =
@module.handle_buffer(:input, buffer, nil, state)
end
end
end
Loading