forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
244 lines (212 loc) · 6.25 KB
/
media.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
package webrtc
import (
"math/rand"
"github.com/servicekit/webrtc/pkg/rtp"
"github.com/pkg/errors"
)
// RTCRtpReceiver allows an application to inspect the receipt of a RTCTrack
type RTCRtpReceiver struct {
Track *RTCTrack
// receiverTrack *RTCTrack
// receiverTransport
// receiverRtcpTransport
}
// TODO: receiving side
// func newRTCRtpReceiver(kind, id string) {
//
// }
// RTCRtpSender allows an application to control how a given RTCTrack is encoded and transmitted to a remote peer
type RTCRtpSender struct {
Track *RTCTrack
// senderTrack *RTCTrack
// senderTransport
// senderRtcpTransport
}
func newRTCRtpSender(track *RTCTrack) *RTCRtpSender {
s := &RTCRtpSender{
Track: track,
}
return s
}
// RTCRtpTransceiverDirection indicates the direction of the RTCRtpTransceiver
type RTCRtpTransceiverDirection int
const (
// RTCRtpTransceiverDirectionSendrecv indicates the RTCRtpSender will offer to send RTP and RTCRtpReceiver the will offer to receive RTP
RTCRtpTransceiverDirectionSendrecv RTCRtpTransceiverDirection = iota + 1
// RTCRtpTransceiverDirectionSendonly indicates the RTCRtpSender will offer to send RTP
RTCRtpTransceiverDirectionSendonly
// RTCRtpTransceiverDirectionRecvonly indicates the RTCRtpReceiver the will offer to receive RTP
RTCRtpTransceiverDirectionRecvonly
// RTCRtpTransceiverDirectionInactive indicates the RTCRtpSender won't offer to send RTP and RTCRtpReceiver the won't offer to receive RTP
RTCRtpTransceiverDirectionInactive
)
func (t RTCRtpTransceiverDirection) String() string {
switch t {
case RTCRtpTransceiverDirectionSendrecv:
return "sendrecv"
case RTCRtpTransceiverDirectionSendonly:
return "sendonly"
case RTCRtpTransceiverDirectionRecvonly:
return "recvonly"
case RTCRtpTransceiverDirectionInactive:
return "inactive"
default:
return "Unknown"
}
}
// RTCRtpTransceiver represents a combination of an RTCRtpSender and an RTCRtpReceiver that share a common mid.
type RTCRtpTransceiver struct {
Mid string
Sender *RTCRtpSender
Receiver *RTCRtpReceiver
Direction RTCRtpTransceiverDirection
// currentDirection RTCRtpTransceiverDirection
// firedDirection RTCRtpTransceiverDirection
// receptive bool
stopped bool
}
func (t *RTCRtpTransceiver) setSendingTrack(track *RTCTrack) {
t.Sender.Track = track
switch t.Direction {
case RTCRtpTransceiverDirectionRecvonly:
t.Direction = RTCRtpTransceiverDirectionSendrecv
case RTCRtpTransceiverDirectionInactive:
t.Direction = RTCRtpTransceiverDirectionSendonly
default:
panic("Invalid state change in RTCRtpTransceiver.setSending")
}
}
func (r *RTCPeerConnection) newRTCRtpTransceiver(
receiver *RTCRtpReceiver,
sender *RTCRtpSender,
direction RTCRtpTransceiverDirection,
) *RTCRtpTransceiver {
t := &RTCRtpTransceiver{
Receiver: receiver,
Sender: sender,
Direction: direction,
}
r.rtpTransceivers = append(r.rtpTransceivers, t)
return t
}
// Stop irreversibly stops the RTCRtpTransceiver
func (t *RTCRtpTransceiver) Stop() error {
panic("TODO")
}
// RTCSample contains media, and the amount of samples in it
type RTCSample struct {
Data []byte
Samples uint32
}
// RTCTrack represents a track that is communicated
type RTCTrack struct {
PayloadType uint8
Kind RTCRtpCodecType
ID string
Label string
Ssrc uint32
Codec *RTCRtpCodec
Packets <-chan *rtp.Packet
Samples chan<- RTCSample
}
// NewRTCTrack is used to create a new RTCTrack
func (r *RTCPeerConnection) NewRTCTrack(payloadType uint8, id, label string) (*RTCTrack, error) {
codec, err := r.mediaEngine.getCodec(payloadType)
if err != nil {
return nil, err
}
if codec.Payloader == nil {
return nil, errors.New("codec payloader not set")
}
trackInput := make(chan RTCSample, 15) // Is the buffering needed?
ssrc := rand.Uint32()
go func() {
packetizer := rtp.NewPacketizer(
1400,
payloadType,
ssrc,
codec.Payloader,
rtp.NewRandomSequencer(),
codec.ClockRate,
)
for {
in := <-trackInput
packets := packetizer.Packetize(in.Data, in.Samples)
for _, p := range packets {
r.networkManager.SendRTP(p)
}
}
}()
t := &RTCTrack{
PayloadType: payloadType,
Kind: codec.Type,
ID: id,
Label: label,
Ssrc: ssrc,
Codec: codec,
Samples: trackInput,
}
return t, nil
}
// AddTrack adds a RTCTrack to the RTCPeerConnection
func (r *RTCPeerConnection) AddTrack(track *RTCTrack) (*RTCRtpSender, error) {
if r.IsClosed {
return nil, &InvalidStateError{Err: ErrConnectionClosed}
}
for _, tranceiver := range r.rtpTransceivers {
if tranceiver.Sender.Track == nil {
continue
}
if track.ID == tranceiver.Sender.Track.ID {
return nil, &InvalidAccessError{Err: ErrExistingTrack}
}
}
var tranciever *RTCRtpTransceiver
for _, t := range r.rtpTransceivers {
if !t.stopped &&
// t.Sender == nil && // TODO: check that the sender has never sent
t.Sender.Track == nil &&
t.Receiver.Track != nil &&
t.Receiver.Track.Kind == track.Kind {
tranciever = t
break
}
}
if tranciever != nil {
tranciever.setSendingTrack(track)
} else {
var receiver *RTCRtpReceiver
sender := newRTCRtpSender(track)
tranciever = r.newRTCRtpTransceiver(
receiver,
sender,
RTCRtpTransceiverDirectionSendonly,
)
}
tranciever.Mid = track.Kind.String() // TODO: Mid generation
return tranciever.Sender, nil
}
// GetSenders returns the RTCRtpSender that are currently attached to this RTCPeerConnection
func (r *RTCPeerConnection) GetSenders() []RTCRtpSender {
result := make([]RTCRtpSender, len(r.rtpTransceivers))
for i, tranceiver := range r.rtpTransceivers {
result[i] = *tranceiver.Sender
}
return result
}
// GetReceivers returns the RTCRtpReceivers that are currently attached to this RTCPeerConnection
func (r *RTCPeerConnection) GetReceivers() []RTCRtpReceiver {
result := make([]RTCRtpReceiver, len(r.rtpTransceivers))
for i, tranceiver := range r.rtpTransceivers {
result[i] = *tranceiver.Receiver
}
return result
}
// GetTransceivers returns the RTCRtpTransceiver that are currently attached to this RTCPeerConnection
func (r *RTCPeerConnection) GetTransceivers() []RTCRtpTransceiver {
result := make([]RTCRtpTransceiver, len(r.rtpTransceivers))
for i, tranceiver := range r.rtpTransceivers {
result[i] = *tranceiver
}
return result
}