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

Pts integrity warn #59

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 18 additions & 4 deletions lib/membrane_opus/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ defmodule Membrane.Opus.Encoder do

{:ok, encoded_buffers, state} ->
# something was encoded
if check_pts_integrity? and length(encoded_buffers) >= 2 and
Enum.at(encoded_buffers, 1).pts > input_pts do
Membrane.Logger.warning("PTS values are overlapping")
end
validate_pts_integrity(check_pts_integrity?, encoded_buffers, input_pts)

{[buffer: {:output, encoded_buffers}], state}
end
Expand Down Expand Up @@ -250,4 +247,21 @@ defmodule Membrane.Opus.Encoder do

Map.update!(state, :current_pts, &(&1 + duration))
end

defp validate_pts_integrity(true = _check_pts_integrity?, encoded_buffers, input_pts) do
cond do
length(encoded_buffers) >= 2 and Enum.at(encoded_buffers, 1).pts > input_pts ->
Membrane.Logger.warning("PTS values are overlapping")

length(encoded_buffers) >= 2 and Enum.at(encoded_buffers, 1).pts < input_pts ->
Membrane.Logger.warning("PTS values are not continous")

true ->
:ok
end
end

defp validate_pts_integrity(_check_pts_integrity?, _encoded_buffers, _input_pts) do
:ok
end
end
22 changes: 18 additions & 4 deletions lib/membrane_opus/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ defmodule Membrane.Opus.Parser do
set_current_pts(state, input_pts)
)

if check_pts_integrity? and length(packets) >= 2 and
Enum.at(packets, 1).pts > input_pts do
Membrane.Logger.warning("PTS values are overlapping")
end
validate_pts_integrity(check_pts_integrity?, packets, input_pts)

stream_format = %Opus{
self_delimiting?: self_delimiting?,
Expand Down Expand Up @@ -219,4 +216,21 @@ defmodule Membrane.Opus.Parser do
present_frames = frame_lengths |> Enum.count(fn length -> length > 0 end)
present_frames * frame_duration
end

defp validate_pts_integrity(true = _check_pts_integrity?, packets, input_pts) do
cond do
length(packets) >= 2 and Enum.at(packets, 1).pts > input_pts ->
Membrane.Logger.warning("PTS values are overlapping")

length(packets) >= 2 and Enum.at(packets, 1).pts < input_pts ->
Membrane.Logger.warning("PTS values are not continous")

true ->
:ok
end
end
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
defp validate_pts_integrity(true = _check_pts_integrity?, packets, input_pts) do
cond do
length(packets) >= 2 and Enum.at(packets, 1).pts > input_pts ->
Membrane.Logger.warning("PTS values are overlapping")
length(packets) >= 2 and Enum.at(packets, 1).pts < input_pts ->
Membrane.Logger.warning("PTS values are not continous")
true ->
:ok
end
end
defp validate_pts_integrity(packets, input_pts) do
cond do
length(packets) < 2 or Enum.at(packets, 1).pts == input_pts ->
:ok
Enum.at(packets, 1).pts > input_pts ->
Membrane.Logger.warning("PTS values are overlapping")
:ok
Enum.at(packets, 1).pts < input_pts ->
Membrane.Logger.warning("PTS values are not continous")
:ok
end
end
  1. I would avoid calling length(packets) two times, because this function has linear complexity
  2. Move it to Util module, to stop reapeating yourself and delete this function from Encoder
  3. IMO it would be prettier to remove flag from function args and wrap function call into an if statement, eg. if check_pts_integrity?, do: Util.validate..


defp validate_pts_integrity(_check_pts_integrity?, _packets, _input_pts) do
:ok
end
end