forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaengine.go
216 lines (188 loc) · 5.38 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
package webrtc
import (
"strconv"
"github.com/pions/webrtc/internal/sdp"
"github.com/pions/webrtc/pkg/rtp"
"github.com/pions/webrtc/pkg/rtp/codecs"
"github.com/pkg/errors"
)
// RegisterCodec is used to register a codec with the DefaultMediaEngine
func RegisterCodec(codec *RTCRtpCodec) {
DefaultMediaEngine.RegisterCodec(codec)
}
// TODO: Phase out DefaultPayloadTypes in favor or dynamic assignment in 96-127 range
// PayloadTypes for the default codecs
const (
DefaultPayloadTypeOpus = 111
DefaultPayloadTypeVP8 = 96
DefaultPayloadTypeVP9 = 98
DefaultPayloadTypeH264 = 100
)
// RegisterDefaultCodecs is a helper that registers the default codecs supported by pions-webrtc
func RegisterDefaultCodecs() {
RegisterCodec(NewRTCRtpOpusCodec(DefaultPayloadTypeOpus, 48000, 2))
RegisterCodec(NewRTCRtpVP8Codec(DefaultPayloadTypeVP8, 90000))
RegisterCodec(NewRTCRtpH264Codec(DefaultPayloadTypeH264, 90000))
RegisterCodec(NewRTCRtpVP9Codec(DefaultPayloadTypeVP9, 90000))
}
// DefaultMediaEngine is the default MediaEngine used by RTCPeerConnections
var DefaultMediaEngine = NewMediaEngine()
// NewMediaEngine creates a new MediaEngine
func NewMediaEngine() *MediaEngine {
return &MediaEngine{}
}
// MediaEngine defines the codecs supported by a RTCPeerConnection
type MediaEngine struct {
codecs []*RTCRtpCodec
}
// RegisterCodec registers a codec to a media engine
func (m *MediaEngine) RegisterCodec(codec *RTCRtpCodec) uint8 {
// TODO: generate PayloadType if not set
m.codecs = append(m.codecs, codec)
return codec.PayloadType
}
func (m *MediaEngine) getCodec(payloadType uint8) (*RTCRtpCodec, error) {
for _, codec := range m.codecs {
if codec.PayloadType == payloadType {
return codec, nil
}
}
return nil, errors.New("Codec not found")
}
func (m *MediaEngine) getCodecSDP(sdpCodec sdp.Codec) (*RTCRtpCodec, 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, errors.New("Codec not found")
}
func (m *MediaEngine) getCodecsByKind(kind RTCRtpCodecType) []*RTCRtpCodec {
var codecs []*RTCRtpCodec
for _, codec := range m.codecs {
if codec.Type == kind {
codecs = append(codecs, codec)
}
}
return codecs
}
// Names for the default codecs supported by pions-webrtc
const (
Opus = "opus"
VP8 = "VP8"
VP9 = "VP9"
H264 = "H264"
)
// NewRTCRtpOpusCodec is a helper to create an Opus codec
func NewRTCRtpOpusCodec(payloadType uint8, clockrate uint32, channels uint16) *RTCRtpCodec {
c := NewRTCRtpCodec(RTCRtpCodecTypeAudio,
Opus,
clockrate,
channels,
"minptime=10;useinbandfec=1",
payloadType,
&codecs.OpusPayloader{})
return c
}
// NewRTCRtpVP8Codec is a helper to create an VP8 codec
func NewRTCRtpVP8Codec(payloadType uint8, clockrate uint32) *RTCRtpCodec {
c := NewRTCRtpCodec(RTCRtpCodecTypeVideo,
VP8,
clockrate,
0,
"",
payloadType,
&codecs.VP8Payloader{})
return c
}
// NewRTCRtpVP9Codec is a helper to create an VP9 codec
func NewRTCRtpVP9Codec(payloadType uint8, clockrate uint32) *RTCRtpCodec {
c := NewRTCRtpCodec(RTCRtpCodecTypeVideo,
VP9,
clockrate,
0,
"",
payloadType,
nil) // TODO
return c
}
// NewRTCRtpH264Codec is a helper to create an H264 codec
func NewRTCRtpH264Codec(payloadType uint8, clockrate uint32) *RTCRtpCodec {
c := NewRTCRtpCodec(RTCRtpCodecTypeVideo,
H264,
clockrate,
0,
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f",
payloadType,
&codecs.H264Payloader{})
return c
}
// RTCRtpCodecType determines the type of a codec
type RTCRtpCodecType int
const (
// RTCRtpCodecTypeAudio indicates this is an audio codec
RTCRtpCodecTypeAudio RTCRtpCodecType = iota + 1
// RTCRtpCodecTypeVideo indicates this is a video codec
RTCRtpCodecTypeVideo
)
func (t RTCRtpCodecType) String() string {
switch t {
case RTCRtpCodecTypeAudio:
return "audio"
case RTCRtpCodecTypeVideo:
return "video"
default:
return ErrUnknownType.Error()
}
}
// RTCRtpCodec represents a codec supported by the PeerConnection
type RTCRtpCodec struct {
RTCRtpCodecCapability
Type RTCRtpCodecType
Name string
PayloadType uint8
Payloader rtp.Payloader
}
// NewRTCRtpCodec is used to define a new codec
func NewRTCRtpCodec(
codecType RTCRtpCodecType,
name string,
clockrate uint32,
channels uint16,
fmtp string,
payloadType uint8,
payloader rtp.Payloader,
) *RTCRtpCodec {
return &RTCRtpCodec{
RTCRtpCodecCapability: RTCRtpCodecCapability{
MimeType: codecType.String() + "/" + name,
ClockRate: clockrate,
Channels: channels,
SdpFmtpLine: fmtp,
},
PayloadType: payloadType,
Payloader: payloader,
Type: codecType,
Name: name,
}
}
// RTCRtpCodecCapability provides information about codec capabilities.
type RTCRtpCodecCapability struct {
MimeType string
ClockRate uint32
Channels uint16
SdpFmtpLine string
}
// RTCRtpHeaderExtensionCapability is used to define a RFC5285 RTP header extension supported by the codec.
type RTCRtpHeaderExtensionCapability struct {
URI string
}
// RTCRtpCapabilities represents the capabilities of a transceiver
type RTCRtpCapabilities struct {
Codecs []RTCRtpCodecCapability
HeaderExtensions []RTCRtpHeaderExtensionCapability
}