/
twcc_receiver.ex
190 lines (158 loc) · 5.52 KB
/
twcc_receiver.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
defmodule Membrane.RTP.TWCCReceiver do
@moduledoc """
The module defines an element responsible for recording transport-wide statistics of incoming packets
and generating TWCC feedbacks.
"""
use Membrane.Filter
require Bitwise
alias __MODULE__.PacketInfoStore
alias Membrane.RTCP.TransportFeedbackPacket
alias Membrane.RTP.Header
alias Membrane.{RTCPEvent, RTP, Time}
@feedback_count_limit Bitwise.bsl(1, 8)
def_input_pad :input, accepted_format: RTP, availability: :on_request, flow_control: :auto
def_output_pad :output, accepted_format: RTP, availability: :on_request, flow_control: :auto
def_options twcc_id: [
spec: 1..14,
description: "ID of TWCC header extension."
],
report_interval: [
spec: Membrane.Time.t(),
default: Membrane.Time.milliseconds(250),
description: "How often to generate feedback packets."
],
feedback_sender_ssrc: [
spec: RTP.ssrc_t() | nil,
default: nil,
description:
"Sender SSRC for generated feedback packets (will be supplied by `RTP.SessionBin`)."
]
defmodule State do
@moduledoc false
alias Membrane.RTP.TWCCReceiver.PacketInfoStore
@type t :: %__MODULE__{
twcc_id: 1..14,
feedback_sender_ssrc: RTP.ssrc_t() | nil,
report_interval: Time.t(),
packet_info_store: PacketInfoStore.t(),
feedback_packet_count: non_neg_integer(),
media_ssrc: RTP.ssrc_t() | nil,
buffered_actions: %{Pad.ref() => Qex.t()}
}
@enforce_keys [:twcc_id, :report_interval, :feedback_sender_ssrc]
defstruct @enforce_keys ++
[
packet_info_store: %PacketInfoStore{},
feedback_packet_count: 0,
media_ssrc: nil,
buffered_actions: %{}
]
end
@impl true
def handle_init(_ctx, options) do
{[], struct!(State, Map.from_struct(options))}
end
@impl true
def handle_playing(_ctx, state) do
{[start_timer: {:report_timer, state.report_interval}], state}
end
@impl true
def handle_stream_format(Pad.ref(:input, ssrc), stream_format, ctx, state) do
pad = Pad.ref(:output, ssrc)
actions = [stream_format: {pad, stream_format}]
if Map.has_key?(ctx.pads, pad),
do: {actions, state},
else: {[], buffer_actions(actions, pad, state)}
end
@impl true
def handle_pad_added(pad, _ctx, state) do
{actions, buffer} = Map.pop(state.buffered_actions, pad, [])
{Enum.to_list(actions), %{state | buffered_actions: buffer}}
end
@impl true
def handle_event(Pad.ref(direction, ssrc), event, ctx, state) do
opposite_direction = if direction == :input, do: :output, else: :input
pad = Pad.ref(opposite_direction, ssrc)
actions = [event: {pad, event}]
if Map.has_key?(ctx.pads, pad),
do: {actions, state},
else: {[], buffer_actions(actions, pad, state)}
end
@impl true
def handle_pad_removed(Pad.ref(_direction, ssrc), _ctx, %State{media_ssrc: media_ssrc} = state) do
if media_ssrc == ssrc do
{[], %State{state | media_ssrc: nil}}
else
{[], state}
end
end
@impl true
def handle_buffer(Pad.ref(:input, ssrc), buffer, ctx, state) do
{extension, buffer} = Header.Extension.pop(buffer, state.twcc_id)
state =
if extension != nil do
<<seq_num::16>> = extension.data
store = PacketInfoStore.insert_packet_info(state.packet_info_store, seq_num)
%{state | packet_info_store: store, media_ssrc: state.media_ssrc || ssrc}
else
state
end
pad = Pad.ref(:output, ssrc)
actions = [buffer: {pad, buffer}]
if Map.has_key?(ctx.pads, pad),
do: {actions, state},
else: {[], buffer_actions(actions, pad, state)}
end
@impl true
def handle_tick(:report_timer, ctx, %State{packet_info_store: store} = state) do
if PacketInfoStore.empty?(store) or state.media_ssrc == nil do
{[], state}
else
event = make_rtcp_event(state)
state = %{
state
| packet_info_store: %PacketInfoStore{},
feedback_packet_count: rem(state.feedback_packet_count + 1, @feedback_count_limit)
}
pad = Pad.ref(:input, state.media_ssrc)
actions = [event: {pad, event}]
if Map.has_key?(ctx.pads, pad),
do: {actions, state},
else: {[], buffer_actions(actions, pad, state)}
end
end
@impl true
def handle_end_of_stream(Pad.ref(:input, ssrc), ctx, state) do
pad = Pad.ref(:output, ssrc)
actions = [end_of_stream: pad]
if Map.has_key?(ctx.pads, pad),
do: {actions, state},
else: {[], buffer_actions(actions, pad, state)}
end
defp make_rtcp_event(state) do
%State{
packet_info_store: store,
feedback_packet_count: feedback_packet_count,
feedback_sender_ssrc: feedback_sender_ssrc,
media_ssrc: media_ssrc
} = state
stats =
store
|> PacketInfoStore.get_stats()
|> Map.put(:feedback_packet_count, feedback_packet_count)
%RTCPEvent{
rtcp: %TransportFeedbackPacket{
sender_ssrc: feedback_sender_ssrc,
media_ssrc: media_ssrc,
payload: struct!(TransportFeedbackPacket.TWCC, stats)
}
}
end
defp buffer_actions(actions, pad, state) do
actions = Qex.new(actions)
%State{
state
| buffered_actions: Map.update(state.buffered_actions, pad, actions, &Qex.join(&1, actions))
}
end
end