forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaengine.go
231 lines (204 loc) · 5.4 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
// +build !js
package webrtc
import (
"strconv"
"strings"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/sdp/v2"
)
// PayloadTypes for the default codecs
const (
DefaultPayloadTypeG722 = 9
DefaultPayloadTypeOpus = 111
DefaultPayloadTypeVP8 = 96
DefaultPayloadTypeVP9 = 98
DefaultPayloadTypeH264 = 102
)
// MediaEngine defines the codecs supported by a PeerConnection
type MediaEngine struct {
codecs []*RTPCodec
}
// RegisterCodec registers a codec to a media engine
func (m *MediaEngine) RegisterCodec(codec *RTPCodec) uint8 {
// TODO: generate PayloadType if not set
m.codecs = append(m.codecs, codec)
return codec.PayloadType
}
// RegisterDefaultCodecs is a helper that registers the default codecs supported by pion-webrtc
func (m *MediaEngine) RegisterDefaultCodecs() {
m.RegisterCodec(NewRTPOpusCodec(DefaultPayloadTypeOpus, 48000))
m.RegisterCodec(NewRTPG722Codec(DefaultPayloadTypeG722, 8000))
m.RegisterCodec(NewRTPVP8Codec(DefaultPayloadTypeVP8, 90000))
m.RegisterCodec(NewRTPH264Codec(DefaultPayloadTypeH264, 90000))
m.RegisterCodec(NewRTPVP9Codec(DefaultPayloadTypeVP9, 90000))
}
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 codec.Name == sdpCodec.Name &&
codec.ClockRate == sdpCodec.ClockRate &&
(sdpCodec.EncodingParameters == "" ||
strconv.Itoa(int(codec.Channels)) == sdpCodec.EncodingParameters) &&
codec.SDPFmtpLine == sdpCodec.Fmtp { // TODO: Protocol specific matching?
return codec, nil
}
}
return nil, ErrCodecNotFound
}
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 (
G722 = "G722"
Opus = "opus"
VP8 = "VP8"
VP9 = "VP9"
H264 = "H264"
)
// 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
}
// NewRTPVP9Codec is a helper to create an VP9 codec
func NewRTPVP9Codec(payloadType uint8, clockrate uint32) *RTPCodec {
c := NewRTPCodec(RTPCodecTypeVideo,
VP9,
clockrate,
0,
"",
payloadType,
nil) // TODO
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
}
// 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
}
// 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,
}
}
// 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
}