Skip to content

Commit

Permalink
Handle VP8.Payload.parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Jun 27, 2024
1 parent 838af94 commit b0fc650
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test,examples}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: ["examples/*"]
]
87 changes: 50 additions & 37 deletions lib/ex_webrtc/rtp/vp8/depayloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,64 @@ defmodule ExWebRTC.RTP.VP8.Depayloader do
def write(depayloader, %ExRTP.Packet{payload: <<>>, padding: true}), do: {:ok, depayloader}

def write(depayloader, packet) do
with {:ok, vp8_payload} <- Payload.parse(packet.payload) do
depayloader =
case {depayloader.current_frame, vp8_payload} do
{nil, %Payload{s: 1, pid: 0}} ->
%{
depayloader
| current_frame: vp8_payload.payload,
current_timestamp: packet.timestamp
}

{nil, _vp8_payload} ->
Logger.debug("Dropping vp8 payload as it doesn't start a new frame")
depayloader
case Payload.parse(packet.payload) do
{:ok, vp8_payload} ->
do_write(depayloader, packet, vp8_payload)

{_current_frame, %Payload{s: 1, pid: 0}} ->
Logger.debug("""
Received packet that starts a new frame without finishing the previous frame. \
Dropping previous frame.\
""")
{:error, reason} ->
Logger.warning("""
Couldn't parse payload, reason: #{reason}. \
Resetting depayloader state. Payload: #{inspect(packet.payload)}.\
""")

%{
depayloader
| current_frame: vp8_payload.payload,
current_timestamp: packet.timestamp
}
{:ok, %{depayloader | current_frame: nil, current_timestamp: nil}}
end
end

_ when packet.timestamp != depayloader.current_timestamp ->
Logger.debug("""
Received packet with timestamp from a new frame that is not a beginning of this frame \
and without finishing the previous frame. Dropping both.\
""")
defp do_write(depayloader, packet, vp8_payload) do
depayloader =
case {depayloader.current_frame, vp8_payload} do
{nil, %Payload{s: 1, pid: 0}} ->
%{
depayloader
| current_frame: vp8_payload.payload,
current_timestamp: packet.timestamp
}

%{depayloader | current_frame: nil, current_timestamp: nil}
{nil, _vp8_payload} ->
Logger.debug("Dropping vp8 payload as it doesn't start a new frame")
depayloader

{current_frame, vp8_payload} ->
%{depayloader | current_frame: current_frame <> vp8_payload.payload}
end
{_current_frame, %Payload{s: 1, pid: 0}} ->
Logger.debug("""
Received packet that starts a new frame without finishing the previous frame. \
Dropping previous frame.\
""")

case {depayloader.current_frame, packet.marker} do
{current_frame, true} when current_frame != nil ->
{:ok, current_frame, %{depayloader | current_frame: nil, current_timestamp: nil}}
%{
depayloader
| current_frame: vp8_payload.payload,
current_timestamp: packet.timestamp
}

_ when packet.timestamp != depayloader.current_timestamp ->
Logger.debug("""
Received packet with timestamp from a new frame that is not a beginning of this frame \
and without finishing the previous frame. Dropping both.\
""")

%{depayloader | current_frame: nil, current_timestamp: nil}

_ ->
{:ok, depayloader}
{current_frame, vp8_payload} ->
%{depayloader | current_frame: current_frame <> vp8_payload.payload}
end

case {depayloader.current_frame, packet.marker} do
{current_frame, true} when current_frame != nil ->
{:ok, current_frame, %{depayloader | current_frame: nil, current_timestamp: nil}}

_ ->
{:ok, depayloader}
end
end
end

0 comments on commit b0fc650

Please sign in to comment.