Skip to content

Commit 897bc60

Browse files
mickel8Michał Śledź
authored and
Michał Śledź
committed
Handle VP8.Payload.parse errors
1 parent 838af94 commit 897bc60

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

.formatter.exs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter}.exs", "{config,lib,test,examples}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
4+
subdirectories: ["examples/*"]
45
]

lib/ex_webrtc/rtp/vp8/depayloader.ex

+50-37
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,64 @@ defmodule ExWebRTC.RTP.VP8.Depayloader do
2626
def write(depayloader, %ExRTP.Packet{payload: <<>>, padding: true}), do: {:ok, depayloader}
2727

2828
def write(depayloader, packet) do
29-
with {:ok, vp8_payload} <- Payload.parse(packet.payload) do
30-
depayloader =
31-
case {depayloader.current_frame, vp8_payload} do
32-
{nil, %Payload{s: 1, pid: 0}} ->
33-
%{
34-
depayloader
35-
| current_frame: vp8_payload.payload,
36-
current_timestamp: packet.timestamp
37-
}
38-
39-
{nil, _vp8_payload} ->
40-
Logger.debug("Dropping vp8 payload as it doesn't start a new frame")
41-
depayloader
29+
case Payload.parse(packet.payload) do
30+
{:ok, vp8_payload} ->
31+
do_write(depayloader, packet, vp8_payload)
4232

43-
{_current_frame, %Payload{s: 1, pid: 0}} ->
44-
Logger.debug("""
45-
Received packet that starts a new frame without finishing the previous frame. \
46-
Dropping previous frame.\
47-
""")
33+
{:error, reason} ->
34+
Logger.warning("""
35+
Couldn't parse payload, reason: #{reason}. \
36+
Resetting depayloader state. Payload: #{inspect(packet.payload)}.\
37+
""")
4838

49-
%{
50-
depayloader
51-
| current_frame: vp8_payload.payload,
52-
current_timestamp: packet.timestamp
53-
}
39+
{:ok, %{depayloader | current_frame: nil, current_timestamp: nil}}
40+
end
41+
end
5442

55-
_ when packet.timestamp != depayloader.current_timestamp ->
56-
Logger.debug("""
57-
Received packet with timestamp from a new frame that is not a beginning of this frame \
58-
and without finishing the previous frame. Dropping both.\
59-
""")
43+
defp do_write(depayloader, packet, vp8_payload) do
44+
depayloader =
45+
case {depayloader.current_frame, vp8_payload} do
46+
{nil, %Payload{s: 1, pid: 0}} ->
47+
%{
48+
depayloader
49+
| current_frame: vp8_payload.payload,
50+
current_timestamp: packet.timestamp
51+
}
6052

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

63-
{current_frame, vp8_payload} ->
64-
%{depayloader | current_frame: current_frame <> vp8_payload.payload}
65-
end
57+
{_current_frame, %Payload{s: 1, pid: 0}} ->
58+
Logger.debug("""
59+
Received packet that starts a new frame without finishing the previous frame. \
60+
Dropping previous frame.\
61+
""")
6662

67-
case {depayloader.current_frame, packet.marker} do
68-
{current_frame, true} when current_frame != nil ->
69-
{:ok, current_frame, %{depayloader | current_frame: nil, current_timestamp: nil}}
63+
%{
64+
depayloader
65+
| current_frame: vp8_payload.payload,
66+
current_timestamp: packet.timestamp
67+
}
68+
69+
_ when packet.timestamp != depayloader.current_timestamp ->
70+
Logger.debug("""
71+
Received packet with timestamp from a new frame that is not a beginning of this frame \
72+
and without finishing the previous frame. Dropping both.\
73+
""")
74+
75+
%{depayloader | current_frame: nil, current_timestamp: nil}
7076

71-
_ ->
72-
{:ok, depayloader}
77+
{current_frame, vp8_payload} ->
78+
%{depayloader | current_frame: current_frame <> vp8_payload.payload}
7379
end
80+
81+
case {depayloader.current_frame, packet.marker} do
82+
{current_frame, true} when current_frame != nil ->
83+
{:ok, current_frame, %{depayloader | current_frame: nil, current_timestamp: nil}}
84+
85+
_ ->
86+
{:ok, depayloader}
7487
end
7588
end
7689
end

0 commit comments

Comments
 (0)