-
Notifications
You must be signed in to change notification settings - Fork 19
/
input.go
693 lines (578 loc) · 18 KB
/
input.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"image"
"io"
"math"
"os"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/pion/interceptor"
"github.com/pion/mediadevices"
"github.com/pion/mediadevices/pkg/codec"
"github.com/pion/mediadevices/pkg/io/audio"
"github.com/pion/mediadevices/pkg/io/video"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/mediadevices/pkg/wave"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
)
func GetInputMediaStream(audio string, video string, codecSelector *CodecSelector) (mediadevices.MediaStream, error) {
tracks := make([]mediadevices.Track, 0)
if len(audio) > 0 {
track, err := GetAudioTrack(audio, codecSelector)
if err != nil {
return nil, err
}
tracks = append(tracks, track)
}
if len(video) > 0 {
track, err := GetVideoTrack(video, codecSelector)
if err != nil {
return nil, err
}
tracks = append(tracks, track)
}
stream, err := mediadevices.NewMediaStream(tracks...)
if err != nil {
return nil, err
}
return stream, nil
}
func GetAudioTrack(name string, codecSelector *CodecSelector) (mediadevices.Track, error) {
pipe, _ := os.Open(name)
data := make([]byte, 480*2)
chunkInfo := wave.ChunkInfo{
Len: 480,
Channels: 1,
SamplingRate: 48000,
}
reader := audio.ReaderFunc(func() (chunk wave.Audio, release func(), err error) {
_, err = io.ReadFull(pipe, data)
buffer := wave.NewInt16Interleaved(chunkInfo)
binary.Read(bytes.NewReader(data), binary.LittleEndian, buffer.Data)
chunk = buffer
return chunk, func() {}, err
})
track := newAudioTrackFromReader(reader, codecSelector)
return track, nil
}
func GetVideoTrack(name string, codecSelector *CodecSelector) (mediadevices.Track, error) {
pipe, _ := os.Open(name)
area := 1280 * 720
data := make([]byte, 1280*720*1.5)
reader := video.ReaderFunc(func() (img image.Image, release func(), err error) {
_, err = io.ReadFull(pipe, data)
yuv := image.NewYCbCr(image.Rect(0, 0, 1280, 720), image.YCbCrSubsampleRatio420)
copy(yuv.Y, data[0:area])
copy(yuv.Cb, data[area:area+area/4])
copy(yuv.Cr, data[area+area/4:area+area/4+area/4])
img = yuv
return img, func() {}, err
})
track := newVideoTrackFromReader(reader, codecSelector)
return track, nil
}
type baseTrack struct {
mediadevices.Source
err error
onErrorHandler func(error)
mu sync.Mutex
endOnce sync.Once
kind mediadevices.MediaDeviceType
selector *CodecSelector
activePeerConnections map[string]chan<- chan<- struct{}
}
func newBaseTrack(kind mediadevices.MediaDeviceType, selector *CodecSelector) *baseTrack {
return &baseTrack{
Source: NewSource(),
kind: kind,
selector: selector,
activePeerConnections: make(map[string]chan<- chan<- struct{}),
}
}
type InputSource struct {
}
func NewSource() InputSource {
return InputSource{}
}
func (source InputSource) Close() error {
return nil
}
func (source InputSource) ID() string {
generator, err := uuid.NewRandom()
if err != nil {
panic(err)
}
return generator.String()
}
type AudioTrack struct {
*baseTrack
*audio.Broadcaster
}
type VideoTrack struct {
*baseTrack
*video.Broadcaster
shouldCopyFrames bool
}
const (
rtpOutboundMTU = 1200
rtcpInboundMTU = 1500
)
// Kind returns track's kind
func (track *baseTrack) Kind() webrtc.RTPCodecType {
switch track.kind {
case mediadevices.VideoInput:
return webrtc.RTPCodecTypeVideo
case mediadevices.AudioInput:
return webrtc.RTPCodecTypeAudio
default:
panic("invalid track kind: only support VideoInput and AudioInput")
}
}
func (track *baseTrack) StreamID() string {
// TODO: StreamID should be used to group multiple tracks. Should get this information from mediastream instead.
generator, err := uuid.NewRandom()
if err != nil {
panic(err)
}
return generator.String()
}
// RID is only relevant if you wish to use Simulcast
func (track *baseTrack) RID() string {
return ""
}
// OnEnded sets an error handler. When a track has been created and started, if an
// error occurs, handler will get called with the error given to the parameter.
func (track *baseTrack) OnEnded(handler func(error)) {
track.mu.Lock()
track.onErrorHandler = handler
err := track.err
track.mu.Unlock()
if err != nil && handler != nil {
// Already errored.
track.endOnce.Do(func() {
handler(err)
})
}
}
// onError is a callback when an error occurs
func (track *baseTrack) onError(err error) {
track.mu.Lock()
track.err = err
handler := track.onErrorHandler
track.mu.Unlock()
if handler != nil {
track.endOnce.Do(func() {
handler(err)
})
}
}
func (track *VideoTrack) Bind(ctx webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
return track.bind(ctx, track)
}
func (track *VideoTrack) Unbind(ctx webrtc.TrackLocalContext) error {
return track.unbind(ctx)
}
func (track *AudioTrack) Bind(ctx webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
return track.bind(ctx, track)
}
func (track *AudioTrack) Unbind(ctx webrtc.TrackLocalContext) error {
return track.unbind(ctx)
}
func (track *baseTrack) bind(ctx webrtc.TrackLocalContext, specializedTrack mediadevices.Track) (webrtc.RTPCodecParameters, error) {
track.mu.Lock()
defer track.mu.Unlock()
signalCh := make(chan chan<- struct{})
var stopRead chan struct{}
track.activePeerConnections[ctx.ID()] = signalCh
var encodedReader mediadevices.RTPReadCloser
var selectedCodec webrtc.RTPCodecParameters
var err error
var errReasons []string
for _, wantedCodec := range ctx.CodecParameters() {
// logger.Debugf("trying to build %s rtp reader", wantedCodec.MimeType)
encodedReader, err = specializedTrack.NewRTPReader(wantedCodec.MimeType, uint32(ctx.SSRC()), rtpOutboundMTU)
if err == nil {
selectedCodec = wantedCodec
break
}
errReasons = append(errReasons, fmt.Sprintf("%s: %s", wantedCodec.MimeType, err))
}
if encodedReader == nil {
return webrtc.RTPCodecParameters{}, errors.New(strings.Join(errReasons, "\n\n"))
}
go func() {
var doneCh chan<- struct{}
writer := ctx.WriteStream()
defer func() {
close(stopRead)
encodedReader.Close()
// When there's another call to unbind, it won't block since we remove the current ctx from active connections
track.removeActivePeerConnection(ctx.ID())
close(signalCh)
if doneCh != nil {
close(doneCh)
}
}()
for {
select {
case doneCh = <-signalCh:
return
default:
}
pkts, _, err := encodedReader.Read()
if err != nil {
// explicitly ignore this error since the higher level should've reported this
return
}
for _, pkt := range pkts {
_, err = writer.WriteRTP(&pkt.Header, pkt.Payload)
if err != nil {
track.onError(err)
return
}
}
}
}()
keyFrameController, ok := encodedReader.Controller().(codec.KeyFrameController)
if ok {
stopRead = make(chan struct{})
go track.rtcpReadLoop(ctx.RTCPReader(), keyFrameController, stopRead)
}
return selectedCodec, nil
}
func (track *baseTrack) rtcpReadLoop(reader interceptor.RTCPReader, keyFrameController codec.KeyFrameController, stopRead chan struct{}) {
readerBuffer := make([]byte, rtcpInboundMTU)
readLoop:
for {
select {
case <-stopRead:
return
default:
}
readLength, _, err := reader.Read(readerBuffer, interceptor.Attributes{})
if err != nil {
if errors.Is(err, io.EOF) {
return
}
// logger.Warnf("failed to read rtcp packet: %s", err)
continue
}
pkts, err := rtcp.Unmarshal(readerBuffer[:readLength])
if err != nil {
// logger.Warnf("failed to unmarshal rtcp packet: %s", err)
continue
}
for _, pkt := range pkts {
switch pkt.(type) {
case *rtcp.PictureLossIndication, *rtcp.FullIntraRequest:
if err := keyFrameController.ForceKeyFrame(); err != nil {
// logger.Warnf("failed to force key frame: %s", err)
continue readLoop
}
}
}
}
}
func (track *baseTrack) unbind(ctx webrtc.TrackLocalContext) error {
ch := track.removeActivePeerConnection(ctx.ID())
// If there isn't a registered chanel for this ctx, it means it has already been unbound
if ch == nil {
return nil
}
doneCh := make(chan struct{})
ch <- doneCh
<-doneCh
return nil
}
func (track *baseTrack) removeActivePeerConnection(id string) chan<- chan<- struct{} {
track.mu.Lock()
defer track.mu.Unlock()
ch, ok := track.activePeerConnections[id]
if !ok {
return nil
}
delete(track.activePeerConnections, id)
return ch
}
func (track *AudioTrack) newEncodedReader(codecNames ...string) (mediadevices.EncodedReadCloser, *codec.RTPCodec, error) {
reader := track.NewReader(false)
inputProp, err := detectCurrentAudioProp(track.Broadcaster)
if err != nil {
return nil, nil, err
}
encodedReader, selectedCodec, err := track.selector.selectAudioCodecByNames(reader, inputProp, codecNames...)
if err != nil {
return nil, nil, err
}
sample := newAudioSampler(selectedCodec.ClockRate, selectedCodec.Latency)
return &encodedReadCloserImpl{
readFn: func() (mediadevices.EncodedBuffer, func(), error) {
data, release, err := encodedReader.Read()
buffer := mediadevices.EncodedBuffer{
Data: data,
Samples: sample(),
}
return buffer, release, err
},
closeFn: encodedReader.Close,
controllerFn: encodedReader.Controller,
}, selectedCodec, nil
}
func (track *AudioTrack) NewEncodedReader(codecName string) (mediadevices.EncodedReadCloser, error) {
reader, _, err := track.newEncodedReader(codecName)
return reader, err
}
func (track *AudioTrack) NewEncodedIOReader(codecName string) (io.ReadCloser, error) {
encodedReader, _, err := track.newEncodedReader(codecName)
if err != nil {
return nil, err
}
return newEncodedIOReadCloserImpl(encodedReader), nil
}
func (track *AudioTrack) NewRTPReader(codecName string, ssrc uint32, mtu int) (mediadevices.RTPReadCloser, error) {
encodedReader, selectedCodec, err := track.newEncodedReader(codecName)
if err != nil {
return nil, err
}
packetizer := rtp.NewPacketizer(uint16(mtu), uint8(selectedCodec.PayloadType), ssrc, selectedCodec.Payloader, rtp.NewRandomSequencer(), selectedCodec.ClockRate)
return &rtpReadCloserImpl{
readFn: func() ([]*rtp.Packet, func(), error) {
encoded, release, err := encodedReader.Read()
if err != nil {
encodedReader.Close()
track.onError(err)
return nil, func() {}, err
}
defer release()
pkts := packetizer.Packetize(encoded.Data, encoded.Samples)
return pkts, release, err
},
closeFn: encodedReader.Close,
controllerFn: encodedReader.Controller,
}, nil
}
func detectCurrentAudioProp(broadcaster *audio.Broadcaster) (prop.Media, error) {
var currentProp prop.Media
// Since broadcaster has a ring buffer internally, a new reader will either read the last
// buffered frame or a new frame from the source. This also implies that no frame will be lost
// in any case.
metaReader := broadcaster.NewReader(false)
metaReader = audio.DetectChanges(0, func(p prop.Media) { currentProp = p })(metaReader)
_, _, err := metaReader.Read()
return currentProp, err
}
func detectCurrentVideoProp(broadcaster *video.Broadcaster) (prop.Media, error) {
var currentProp prop.Media
// Since broadcaster has a ring buffer internally, a new reader will either read the last
// buffered frame or a new frame from the source. This also implies that no frame will be lost
// in any case.
metaReader := broadcaster.NewReader(false)
metaReader = video.DetectChanges(0, 0, func(p prop.Media) { currentProp = p })(metaReader)
_, _, err := metaReader.Read()
return currentProp, err
}
type samplerFunc func() uint32
func newAudioSampler(clockRate uint32, latency time.Duration) samplerFunc {
samples := uint32(math.Round(float64(clockRate) * latency.Seconds()))
return samplerFunc(func() uint32 {
return samples
})
}
// newVideoSampler creates a video sampler that uses the actual video frame rate and
// the codec's clock rate to come up with a duration for each sample.
func newVideoSampler(clockRate uint32) samplerFunc {
clockRateFloat := float64(clockRate)
lastTimestamp := time.Now()
return samplerFunc(func() uint32 {
now := time.Now()
duration := now.Sub(lastTimestamp).Seconds()
samples := uint32(math.Round(clockRateFloat * duration))
lastTimestamp = now
return samples
})
}
func (track *VideoTrack) newEncodedReader(codecNames ...string) (mediadevices.EncodedReadCloser, *codec.RTPCodec, error) {
reader := track.NewReader(track.shouldCopyFrames)
inputProp, err := detectCurrentVideoProp(track.Broadcaster)
if err != nil {
return nil, nil, err
}
encodedReader, selectedCodec, err := selectVideoCodecByNames(track.selector, reader, inputProp, codecNames...)
if err != nil {
return nil, nil, err
}
sample := newVideoSampler(selectedCodec.ClockRate)
return &encodedReadCloserImpl{
readFn: func() (mediadevices.EncodedBuffer, func(), error) {
data, release, err := encodedReader.Read()
buffer := mediadevices.EncodedBuffer{
Data: data,
Samples: sample(),
}
return buffer, release, err
},
closeFn: encodedReader.Close,
controllerFn: encodedReader.Controller,
}, selectedCodec, nil
}
func (track *VideoTrack) NewEncodedReader(codecName string) (mediadevices.EncodedReadCloser, error) {
reader, _, err := track.newEncodedReader(codecName)
return reader, err
}
func (track *VideoTrack) NewEncodedIOReader(codecName string) (io.ReadCloser, error) {
panic("not implemented NewEncodedIOReader")
}
func (track *VideoTrack) NewRTPReader(codecName string, ssrc uint32, mtu int) (mediadevices.RTPReadCloser, error) {
encodedReader, selectedCodec, err := track.newEncodedReader(codecName)
if err != nil {
return nil, err
}
packetizer := rtp.NewPacketizer(uint16(mtu), uint8(selectedCodec.PayloadType), ssrc, selectedCodec.Payloader, rtp.NewRandomSequencer(), selectedCodec.ClockRate)
return &rtpReadCloserImpl{
readFn: func() ([]*rtp.Packet, func(), error) {
encoded, release, err := encodedReader.Read()
if err != nil {
encodedReader.Close()
track.onError(err)
return nil, func() {}, err
}
defer release()
pkts := packetizer.Packetize(encoded.Data, encoded.Samples)
return pkts, release, err
},
closeFn: encodedReader.Close,
controllerFn: encodedReader.Controller,
}, nil
}
func newAudioTrackFromReader(reader audio.Reader, selector *CodecSelector) mediadevices.Track {
base := newBaseTrack(mediadevices.AudioInput, selector)
wrappedReader := audio.ReaderFunc(func() (chunk wave.Audio, release func(), err error) {
chunk, _, err = reader.Read()
if err != nil {
// base.onError(err)
}
return chunk, func() {}, err
})
// TODO: Allow users to configure broadcaster
broadcaster := audio.NewBroadcaster(wrappedReader, nil)
return &AudioTrack{
baseTrack: base,
Broadcaster: broadcaster,
}
}
func newVideoTrackFromReader(reader video.Reader, selector *CodecSelector) mediadevices.Track {
base := newBaseTrack(mediadevices.VideoInput, selector)
wrappedReader := video.ReaderFunc(func() (img image.Image, release func(), err error) {
img, _, err = reader.Read()
if err != nil {
// base.onError(err)
}
return img, func() {}, err
})
// TODO: Allow users to configure broadcaster
broadcaster := video.NewBroadcaster(wrappedReader, nil)
return &VideoTrack{
baseTrack: base,
Broadcaster: broadcaster,
}
}
// rtpreader.go
type rtpReadCloserImpl struct {
readFn func() ([]*rtp.Packet, func(), error)
closeFn func() error
controllerFn func() codec.EncoderController
}
func (r *rtpReadCloserImpl) Read() ([]*rtp.Packet, func(), error) {
return r.readFn()
}
func (r *rtpReadCloserImpl) Close() error {
return r.closeFn()
}
func (r *rtpReadCloserImpl) Controller() codec.EncoderController {
return r.controllerFn()
}
// ioreader.go
type encodedReadCloserImpl struct {
readFn func() (mediadevices.EncodedBuffer, func(), error)
closeFn func() error
controllerFn func() codec.EncoderController
}
func (r *encodedReadCloserImpl) Read() (mediadevices.EncodedBuffer, func(), error) {
return r.readFn()
}
func (r *encodedReadCloserImpl) Close() error {
return r.closeFn()
}
func (r *encodedReadCloserImpl) Controller() codec.EncoderController {
return r.controllerFn()
}
type encodedIOReadCloserImpl struct {
readFn func([]byte) (int, error)
closeFn func() error
controller func() codec.EncoderController
}
func newEncodedIOReadCloserImpl(reader mediadevices.EncodedReadCloser) *encodedIOReadCloserImpl {
var encoded mediadevices.EncodedBuffer
release := func() {}
return &encodedIOReadCloserImpl{
readFn: func(b []byte) (int, error) {
var err error
if len(encoded.Data) == 0 {
release()
encoded, release, err = reader.Read()
if err != nil {
reader.Close()
return 0, err
}
}
n := copy(b, encoded.Data)
encoded.Data = encoded.Data[n:]
return n, nil
},
closeFn: reader.Close,
controller: reader.Controller,
}
}
func (r *encodedIOReadCloserImpl) Read(b []byte) (int, error) {
return r.readFn(b)
}
func (r *encodedIOReadCloserImpl) Close() error {
return r.closeFn()
}
func (r *encodedIOReadCloserImpl) Controller() codec.EncoderController {
return r.controller()
}
// codec.go
func selectVideoCodecByNames(selector *CodecSelector, reader video.Reader, inputProp prop.Media, codecNames ...string) (codec.ReadCloser, *codec.RTPCodec, error) {
var selectedEncoder codec.VideoEncoderBuilder
var encodedReader codec.ReadCloser
var errReasons []string
var err error
outer:
for _, wantCodec := range codecNames {
wantCodecLower := strings.ToLower(wantCodec)
for _, encoder := range selector.videoEncoders {
// MimeType is formated as "video/<codecName>"
if strings.HasSuffix(strings.ToLower(encoder.RTPCodec().MimeType), wantCodecLower) {
encodedReader, err = encoder.BuildVideoEncoder(reader, inputProp)
if err == nil {
selectedEncoder = encoder
break outer
}
}
errReasons = append(errReasons, fmt.Sprintf("%s: %s", encoder.RTPCodec().MimeType, err))
}
}
if selectedEncoder == nil {
return nil, nil, errors.New(strings.Join(errReasons, "\n\n"))
}
return encodedReader, selectedEncoder.RTPCodec(), nil
}