This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediaengine.go
434 lines (390 loc) · 11.2 KB
/
mediaengine.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// +build !js
package webrtc
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/sdp/v3"
)
// PayloadTypes for the default codecs
const (
DefaultPayloadTypePCMU = 0
DefaultPayloadTypePCMA = 8
DefaultPayloadTypeG722 = 9
DefaultPayloadTypeOpus = 111
DefaultPayloadTypeVP8 = 96
DefaultPayloadTypeVP9 = 98
DefaultPayloadTypeH264 = 102
mediaNameAudio = "audio"
mediaNameVideo = "video"
)
// A MediaEngine defines the codecs supported by a PeerConnection.
// MediaEngines populated using RegisterCodec (and RegisterDefaultCodecs)
// may be set up once and reused, including concurrently,
// as long as no other codecs are added subsequently.
// MediaEngines populated using PopulateFromSDP should be used
// only for that session.
type MediaEngine struct {
codecs []*RTPCodec
}
// RegisterCodec adds codec to m.
// RegisterCodec is not safe for concurrent use.
func (m *MediaEngine) RegisterCodec(codec *RTPCodec) uint8 {
// nolint:godox
// TODO: dynamically generate a payload type in the range 96-127 if one wasn't provided.
// See https://github.com/pion/webrtc/issues/43
m.codecs = append(m.codecs, codec)
return codec.PayloadType
}
// RegisterDefaultCodecs registers the default codecs supported by Pion WebRTC.
// RegisterDefaultCodecs is not safe for concurrent use.
func (m *MediaEngine) RegisterDefaultCodecs() {
// Audio Codecs in descending order of preference
m.RegisterCodec(NewRTPOpusCodec(DefaultPayloadTypeOpus, 48000))
m.RegisterCodec(NewRTPPCMUCodec(DefaultPayloadTypePCMU, 8000))
m.RegisterCodec(NewRTPPCMACodec(DefaultPayloadTypePCMA, 8000))
m.RegisterCodec(NewRTPG722Codec(DefaultPayloadTypeG722, 8000))
// Video Codecs in descending order of preference
m.RegisterCodec(NewRTPVP8Codec(DefaultPayloadTypeVP8, 90000))
m.RegisterCodec(NewRTPVP9Codec(DefaultPayloadTypeVP9, 90000))
m.RegisterCodec(NewRTPH264Codec(DefaultPayloadTypeH264, 90000))
}
// PopulateFromSDP finds all codecs in sd and adds them to m, using the dynamic
// payload types and parameters from sd.
// PopulateFromSDP is intended for use when answering a request.
// The offerer sets the PayloadTypes for the connection.
// PopulateFromSDP allows an answerer to properly match the PayloadTypes from the offerer.
// A MediaEngine populated by PopulateFromSDP should be used only for a single session.
func (m *MediaEngine) PopulateFromSDP(sd SessionDescription) error {
sdp := sdp.SessionDescription{}
if err := sdp.Unmarshal([]byte(sd.SDP)); err != nil {
return err
}
for _, md := range sdp.MediaDescriptions {
if md.MediaName.Media != mediaNameAudio && md.MediaName.Media != mediaNameVideo {
continue
}
for _, format := range md.MediaName.Formats {
pt, err := strconv.Atoi(format)
if err != nil {
return errMediaEngineParseError
}
payloadType := uint8(pt)
payloadCodec, err := sdp.GetCodecForPayloadType(payloadType)
if err != nil {
return fmt.Errorf("%w: codec for payload type %d", errMediaEngineCodecNotFound, payloadType)
}
var codec *RTPCodec
switch {
case strings.EqualFold(payloadCodec.Name, PCMA):
codec = NewRTPPCMACodec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, PCMU):
codec = NewRTPPCMUCodec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, G722):
codec = NewRTPG722Codec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, Opus):
codec = NewRTPOpusCodec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, VP8):
codec = NewRTPVP8Codec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, VP9):
codec = NewRTPVP9Codec(payloadType, payloadCodec.ClockRate)
case strings.EqualFold(payloadCodec.Name, H264):
codec = NewRTPH264Codec(payloadType, payloadCodec.ClockRate)
default:
// ignoring other codecs
continue
}
codec.SDPFmtpLine = payloadCodec.Fmtp
m.RegisterCodec(codec)
}
}
return nil
}
// GetCodecsByName returns all codecs by name that are supported by m.
// The returned codecs should not be modified.
func (m *MediaEngine) GetCodecsByName(codecName string) []*RTPCodec {
var codecs []*RTPCodec
for _, codec := range m.codecs {
if strings.EqualFold(codec.Name, codecName) {
codecs = append(codecs, codec)
}
}
return codecs
}
func (m *MediaEngine) getCodec(payloadType uint8) (*RTPCodec, error) {
for _, codec := range m.codecs {
if codec.PayloadType == payloadType {
return codec, nil
}
}
return nil, ErrCodecNotFound
}
func (m *MediaEngine) getCodecSDP(sdpCodec sdp.Codec) (*RTPCodec, error) {
for _, codec := range m.codecs {
if strings.EqualFold(codec.Name, sdpCodec.Name) &&
codec.ClockRate == sdpCodec.ClockRate &&
(sdpCodec.EncodingParameters == "" ||
strconv.Itoa(int(codec.Channels)) == sdpCodec.EncodingParameters) &&
codec.SDPFmtpLine == sdpCodec.Fmtp { // pion/webrtc#43
return codec, nil
}
}
return nil, ErrCodecNotFound
}
// GetCodecsByKind returns all codecs of kind kind that are supported by m.
// The returned codecs should not be modified.
func (m *MediaEngine) GetCodecsByKind(kind RTPCodecType) []*RTPCodec {
var codecs []*RTPCodec
for _, codec := range m.codecs {
if codec.Type == kind {
codecs = append(codecs, codec)
}
}
return codecs
}
// Names for the default codecs supported by Pion WebRTC
const (
PCMU = "PCMU"
PCMA = "PCMA"
G722 = "G722"
Opus = "opus"
VP8 = "VP8"
VP9 = "VP9"
H264 = "H264"
)
// NewRTPPCMUCodec is a helper to create a PCMU codec
func NewRTPPCMUCodec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeAudio,
PCMU,
clockrate,
0,
"",
payloadType,
&codecs.G711Payloader{})
return c
}
// NewRTPPCMACodec is a helper to create a PCMA codec
func NewRTPPCMACodec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeAudio,
PCMA,
clockrate,
0,
"",
payloadType,
&codecs.G711Payloader{})
return c
}
// NewRTPG722Codec is a helper to create a G722 codec
func NewRTPG722Codec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeAudio,
G722,
clockrate,
0,
"",
payloadType,
&codecs.G722Payloader{})
return c
}
// NewRTPOpusCodec is a helper to create an Opus codec
func NewRTPOpusCodec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeAudio,
Opus,
clockrate,
2, // According to RFC7587, Opus RTP streams must have exactly 2 channels.
"minptime=10;useinbandfec=1",
payloadType,
&codecs.OpusPayloader{})
return c
}
// NewRTPVP8Codec is a helper to create an VP8 codec
func NewRTPVP8Codec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeVideo,
VP8,
clockrate,
0,
"",
payloadType,
&codecs.VP8Payloader{})
return c
}
// NewRTPVP8CodecExt is a helper to create an VP8 codec
func NewRTPVP8CodecExt(payloadType uint8, clockrate uint32, rtcpfb []RTCPFeedback, fmtp string) *RTPCodec {
c := NewRTPCodecExt(RTPCodecTypeVideo,
VP8,
clockrate,
0,
fmtp,
payloadType,
rtcpfb,
&codecs.VP8Payloader{})
return c
}
// NewRTPVP9Codec is a helper to create an VP9 codec
func NewRTPVP9Codec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeVideo,
VP9,
clockrate,
0,
"",
payloadType,
&codecs.VP9Payloader{})
return c
}
// NewRTPVP9CodecExt is a helper to create an VP8 codec
func NewRTPVP9CodecExt(payloadType uint8, clockrate uint32, rtcpfb []RTCPFeedback, fmtp string) *RTPCodec {
c := NewRTPCodecExt(RTPCodecTypeVideo,
VP9,
clockrate,
0,
fmtp,
payloadType,
rtcpfb,
&codecs.VP9Payloader{})
return c
}
// NewRTPH264Codec is a helper to create an H264 codec
func NewRTPH264Codec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeVideo,
H264,
clockrate,
0,
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f",
payloadType,
&codecs.H264Payloader{})
return c
}
// NewRTPH264CodecExt is a helper to create an H264 codec
func NewRTPH264CodecExt(payloadType uint8, clockrate uint32, rtcpfb []RTCPFeedback, fmtp string) *RTPCodec {
c := NewRTPCodecExt(RTPCodecTypeVideo,
H264,
clockrate,
0,
fmtp,
payloadType,
rtcpfb,
&codecs.H264Payloader{})
return c
}
// RTPCodecType determines the type of a codec
type RTPCodecType int
const (
// RTPCodecTypeAudio indicates this is an audio codec
RTPCodecTypeAudio RTPCodecType = iota + 1
// RTPCodecTypeVideo indicates this is a video codec
RTPCodecTypeVideo
)
func (t RTPCodecType) String() string {
switch t {
case RTPCodecTypeAudio:
return "audio"
case RTPCodecTypeVideo:
return "video"
default:
return ErrUnknownType.Error()
}
}
// NewRTPCodecType creates a RTPCodecType from a string
func NewRTPCodecType(r string) RTPCodecType {
switch {
case strings.EqualFold(r, "audio"):
return RTPCodecTypeAudio
case strings.EqualFold(r, "video"):
return RTPCodecTypeVideo
default:
return RTPCodecType(0)
}
}
// RTPCodec represents a codec supported by the PeerConnection
type RTPCodec struct {
RTPCodecCapability
Type RTPCodecType
Name string
PayloadType uint8
Payloader rtp.Payloader
statsID string
}
// NewRTPCodec is used to define a new codec
func NewRTPCodec(
codecType RTPCodecType,
name string,
clockrate uint32,
channels uint16,
fmtp string,
payloadType uint8,
payloader rtp.Payloader,
) *RTPCodec {
return &RTPCodec{
RTPCodecCapability: RTPCodecCapability{
MimeType: codecType.String() + "/" + name,
ClockRate: clockrate,
Channels: channels,
SDPFmtpLine: fmtp,
},
PayloadType: payloadType,
Payloader: payloader,
Type: codecType,
Name: name,
statsID: fmt.Sprintf("RTPCodec-%d", time.Now().UnixNano()),
}
}
// NewRTPCodecExt is used to define a new codec
func NewRTPCodecExt(
codecType RTPCodecType,
name string,
clockrate uint32,
channels uint16,
fmtp string,
payloadType uint8,
rtcpfb []RTCPFeedback,
payloader rtp.Payloader,
) *RTPCodec {
return &RTPCodec{
RTPCodecCapability: RTPCodecCapability{
MimeType: codecType.String() + "/" + name,
ClockRate: clockrate,
Channels: channels,
SDPFmtpLine: fmtp,
RTCPFeedback: rtcpfb,
},
PayloadType: payloadType,
Payloader: payloader,
Type: codecType,
Name: name,
}
}
// RTPCodecCapability provides information about codec capabilities.
type RTPCodecCapability struct {
MimeType string
ClockRate uint32
Channels uint16
SDPFmtpLine string
RTCPFeedback []RTCPFeedback
}
// RTPHeaderExtensionCapability is used to define a RFC5285 RTP header extension supported by the codec.
type RTPHeaderExtensionCapability struct {
URI string
}
// RTPCapabilities represents the capabilities of a transceiver
type RTPCapabilities struct {
Codecs []RTPCodecCapability
HeaderExtensions []RTPHeaderExtensionCapability
}
func (m *MediaEngine) collectStats(collector *statsReportCollector) {
for _, codec := range m.codecs {
collector.Collecting()
stats := CodecStats{
Timestamp: statsTimestampFrom(time.Now()),
Type: StatsTypeCodec,
ID: codec.statsID,
PayloadType: codec.PayloadType,
MimeType: codec.MimeType,
ClockRate: codec.ClockRate,
Channels: uint8(codec.Channels),
SDPFmtpLine: codec.SDPFmtpLine,
}
collector.Collect(stats.ID, stats)
}
}