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

Add reset_timestamps option #93

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The package can be installed by adding `membrane_rtmp_plugin` to your list of de
```elixir
def deps do
[
{:membrane_rtmp_plugin, "~> 0.23.3"}
{:membrane_rtmp_plugin, "~> 0.23.4"}
]
end
```
Expand Down
34 changes: 28 additions & 6 deletions lib/membrane_rtmp_plugin/rtmp/sink/sink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ defmodule Membrane.RTMP.Sink do
description: """
A list of tracks, which will be sent. Can be `:audio`, `:video` or both.
"""
],
reset_timestamps: [
spec: boolean(),
default: true,
description: """
If enabled, this feature adjusts the timing of outgoing FLV packets so that they begin from zero.
Leaves it untouched otherwise.
"""
]

@impl true
Expand Down Expand Up @@ -89,7 +97,8 @@ defmodule Membrane.RTMP.Sink do
# remaining pad are simply forwarded to the output.
# Always on if a single track is connected
forward_mode?: single_track?,
video_base_dts: nil
video_base_dts: nil,
reset_timestampts: options.reset_timestamps
})

{[], state}
Expand Down Expand Up @@ -303,15 +312,13 @@ defmodule Membrane.RTMP.Sink do
end

defp write_frame(state, Pad.ref(:video, 0), buffer) do
dts = buffer.dts || buffer.pts
pts = buffer.pts || buffer.dts
{base_dts, state} = Bunch.Map.get_updated!(state, :video_base_dts, &(&1 || dts))
{{dts, pts}, state} = buffer_timings(buffer, state)

case Native.write_video_frame(
state.native,
buffer.payload,
dts - base_dts,
pts - base_dts,
dts,
pts,
buffer.metadata.h264.key_frame?
) do
{:ok, native} ->
Expand All @@ -321,4 +328,19 @@ defmodule Membrane.RTMP.Sink do
raise "writing video frame failed with reason: #{inspect(reason)}"
end
end

defp buffer_timings(buffer, state) do
dts = buffer.dts || buffer.pts
pts = buffer.pts || buffer.dts
correct_timings(dts, pts, state)
end

defp correct_timings(dts, pts, %{reset_timestamps: false} = state) do
{{dts, pts}, state}
end

defp correct_timings(dts, pts, state) do
{base_dts, state} = Bunch.Map.get_updated!(state, :video_base_dts, &(&1 || dts))
{{dts - base_dts, pts - base_dts}, state}
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Membrane.RTMP.Mixfile do
use Mix.Project

@version "0.23.3"
@version "0.23.4"
@github_url "https://github.com/membraneframework/membrane_rtmp_plugin"

def project do
Expand Down