forked from gBillal/membrane_rtp_h265_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepayloader.ex
156 lines (124 loc) · 4.89 KB
/
depayloader.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
defmodule Membrane.RTP.H265.Depayloader do
@moduledoc """
Depayloads H265 RTP payloads into H265 NAL Units.
Based on [RFC 7798](https://tools.ietf.org/html/rfc7798).
Supported types: Single NALU, Fragmentation Unit, Aggegration Packets.
"""
use Membrane.Filter
require Membrane.Logger
alias Membrane.Buffer
alias Membrane.Event.Discontinuity
alias Membrane.H265
alias Membrane.RTP
alias Membrane.RTP.H265.{AP, FU, NAL}
@frame_prefix <<1::32>>
def_input_pad :input, accepted_format: RTP, flow_control: :auto
def_output_pad :output,
accepted_format: %H265{alignment: :nalu, stream_structure: :annexb},
flow_control: :auto
def_options sprop_max_don_diff: [
spec: 0..32_767,
default: 0,
description: """
Specify the maximum absolute difference between the decoding order number (i.e. AbsDon)
values of any two NAL units naluA and naluB, where naluA follows naluB in decoding order
and precedes naluB in transmission order.
If this value is greater than 0, then two additional fields `DONL` and `DOND` will
be included in the RTP payload. A `decoding_order_number` field will be added to the
buffer metadata.
"""
]
defmodule State do
@moduledoc false
defstruct parser_acc: nil, sprop_max_don_diff: 0
end
@impl true
def handle_init(_ctx, opts) do
{[], %State{sprop_max_don_diff: opts.sprop_max_don_diff}}
end
@impl true
def handle_stream_format(:input, _stream_format, _context, state) do
{[stream_format: {:output, %H265{alignment: :nalu}}], state}
end
@impl true
def handle_buffer(:input, %Buffer{payload: ""}, _ctx, state) do
Membrane.Logger.debug("Received empty RTP packet. Ignoring")
{[], state}
end
@impl true
def handle_buffer(:input, buffer, _ctx, state) do
with {:ok, {header, _payload} = nal} <- NAL.Header.parse_unit_header(buffer.payload),
unit_type = NAL.Header.decode_type(header),
{:ok, {actions, state}} <- handle_unit_type(unit_type, nal, buffer, state) do
{actions, state}
else
{:error, reason} ->
log_malformed_buffer(buffer, reason)
{[], %State{state | parser_acc: nil}}
end
end
@impl true
def handle_event(:input, %Discontinuity{} = event, _ctx, %State{parser_acc: %FU{}} = state),
do: {[forward: event], %State{state | parser_acc: nil}}
@impl true
def handle_event(pad, event, context, state), do: super(pad, event, context, state)
defp handle_unit_type(:single_nalu, _nalu, buffer, state) do
{don, buffer} =
if state.sprop_max_don_diff > 0 do
<<don::16, payload::binary>> = buffer.payload
{don, %Buffer{buffer | payload: payload}}
else
{nil, buffer}
end
result = buffer_output(buffer.payload, buffer, don, state)
{:ok, result}
end
defp handle_unit_type(:fu, {header, data}, buffer, state) do
%Buffer{metadata: %{rtp: %{sequence_number: seq_num}}} = buffer
case FU.parse(data, seq_num, map_state_to_fu(state)) do
{:ok, {data, type, don}} ->
data =
NAL.Header.add_header(data, 0, type, header.nuh_layer_id, header.nuh_temporal_id_plus1)
result = buffer_output(data, buffer, don, %State{state | parser_acc: nil})
{:ok, result}
{:incomplete, fu} ->
result = {[], %State{state | parser_acc: fu}}
{:ok, result}
{:error, _reason} = error ->
error
end
end
defp handle_unit_type(:ap, {_header, data}, buffer, state) do
with {:ok, nalus} <- AP.parse(data, state.sprop_max_don_diff > 0) do
buffers =
Enum.map(nalus, fn {nalu, don} ->
metadata = put_if(not is_nil(don), buffer.metadata, :decoding_order_number, don)
%Buffer{buffer | payload: add_prefix(nalu), metadata: metadata}
end)
result = {[buffer: {:output, buffers}], state}
{:ok, result}
end
end
defp buffer_output(data, buffer, don, state) do
{action_from_data(data, buffer, don), state}
end
defp action_from_data(data, buffer, nil) do
[buffer: {:output, %Buffer{buffer | payload: add_prefix(data)}}]
end
defp action_from_data(data, buffer, don) do
metadata = Map.put(buffer.metadata, :decoding_order_number, don)
[buffer: {:output, %Buffer{buffer | payload: add_prefix(data), metadata: metadata}}]
end
defp add_prefix(data), do: @frame_prefix <> data
defp map_state_to_fu(%State{parser_acc: %FU{} = fu}), do: fu
defp map_state_to_fu(state), do: %FU{donl?: state.sprop_max_don_diff > 0}
defp log_malformed_buffer(packet, reason) do
Membrane.Logger.warning("""
An error occurred while parsing H265 RTP payload.
Reason: #{reason}
Packet: #{inspect(packet, limit: :infinity)}
""")
end
defp put_if(true, map, key, value), do: Map.put(map, key, value)
defp put_if(false, map, _key, _value), do: map
end