-
Notifications
You must be signed in to change notification settings - Fork 110
/
media.go
663 lines (572 loc) · 18 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
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
package gostream
import (
"context"
"strings"
"sync"
"sync/atomic"
"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/driver/camera"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"go.uber.org/multierr"
"go.viam.com/utils"
)
type (
// A MediaReader is anything that can read and recycle data. It is expected
// that reader can handle multiple reads at the same time. This would ideally only
// happen during streaming when a specific MIME type is requested. In the future,
// we may be able to notice multiple MIME types and either do deferred encode/decode
// or have the reader do it for us.
MediaReader[T any] interface {
Read(ctx context.Context) (data T, release func(), err error)
Close(ctx context.Context) error
}
// A MediaReaderFunc is a helper to turn a function into a MediaReader.
MediaReaderFunc[T any] func(ctx context.Context) (T, func(), error)
// A MediaStream streams media forever until closed.
MediaStream[T any] interface {
// Next returns the next media element in the sequence (best effort).
// Note: This element is mutable and shared globally; it MUST be copied
// before it is mutated.
Next(ctx context.Context) (T, func(), error)
// Close signals this stream is no longer needed and releases associated
// resources.
Close(ctx context.Context) error
}
// A MediaSource can produce Streams of Ts.
MediaSource[T any] interface {
// Stream returns a stream that makes a best effort to return consecutive media elements
// that may have a MIME type hint dictated in the context via WithMIMETypeHint.
Stream(ctx context.Context, errHandlers ...ErrorHandler) (MediaStream[T], error)
// Close cleans up any associated resources with the Source (e.g. a Driver).
Close(ctx context.Context) error
}
// MediaPropertyProvider providers information about a source.
MediaPropertyProvider[U any] interface {
MediaProperties(ctx context.Context) (U, error)
}
)
// Read calls the underlying function to get a media.
func (mrf MediaReaderFunc[T]) Read(ctx context.Context) (T, func(), error) {
ctx, span := trace.StartSpan(ctx, "gostream::MediaReaderFunc::Read")
defer span.End()
return mrf(ctx)
}
// Close does nothing.
func (mrf MediaReaderFunc[T]) Close(ctx context.Context) error {
return nil
}
// A mediaReaderFuncNoCtx is a helper to turn a function into a MediaReader that cannot
// accept a context argument.
type mediaReaderFuncNoCtx[T any] func() (T, func(), error)
// Read calls the underlying function to get a media.
func (mrf mediaReaderFuncNoCtx[T]) Read(ctx context.Context) (T, func(), error) {
_, span := trace.StartSpan(ctx, "gostream::MediaReaderFuncNoCtx::Read")
defer span.End()
return mrf()
}
// Close does nothing.
func (mrf mediaReaderFuncNoCtx[T]) Close(ctx context.Context) error {
return nil
}
// ReadMedia gets a single media from a source. Using this has less of a guarantee
// than MediaSource.Stream that the Nth media element follows the N-1th media element.
func ReadMedia[T any](ctx context.Context, source MediaSource[T]) (T, func(), error) {
ctx, span := trace.StartSpan(ctx, "gostream::ReadMedia")
defer span.End()
if reader, ok := source.(MediaReader[T]); ok {
// more efficient if there is a direct way to read
return reader.Read(ctx)
}
stream, err := source.Stream(ctx)
var zero T
if err != nil {
return zero, nil, err
}
defer func() {
utils.UncheckedError(stream.Close(ctx))
}()
return stream.Next(ctx)
}
type mediaSource[T any, U any] struct {
driver driver.Driver
reader MediaReader[T]
props U
rootCancelCtx context.Context
rootCancel func()
producerConsumers map[string]*producerConsumer[T, U]
producerConsumersMu sync.Mutex
}
type producerConsumer[T any, U any] struct {
rootCancelCtx context.Context
cancelCtx context.Context
interestedConsumers int64
cancelCtxMu *sync.RWMutex
cancel func()
mimeType string
activeBackgroundWorkers sync.WaitGroup
readWrapper MediaReader[T]
current *mediaRefReleasePairWithError[T]
currentMu sync.RWMutex
producerCond *sync.Cond
consumerCond *sync.Cond
condMu *sync.RWMutex
errHandlers map[*mediaStream[T, U]][]ErrorHandler
listeners int
stateMu sync.Mutex
listenersMu sync.Mutex
errHandlersMu sync.Mutex
}
// ErrorHandler receives the error returned by a TSource.Next
// regardless of whether or not the error is nil (This allows
// for error handling logic based on consecutively retrieved errors).
type ErrorHandler func(ctx context.Context, mediaErr error)
// PropertiesFromMediaSource returns properties from underlying driver in the given MediaSource.
func PropertiesFromMediaSource[T, U any](src MediaSource[T]) ([]prop.Media, error) {
d, err := DriverFromMediaSource[T, U](src)
if err != nil {
return nil, err
}
return d.Properties(), nil
}
// LabelsFromMediaSource returns the labels from the underlying driver in the MediaSource.
func LabelsFromMediaSource[T, U any](src MediaSource[T]) ([]string, error) {
d, err := DriverFromMediaSource[T, U](src)
if err != nil {
return nil, err
}
return strings.Split(d.Info().Label, camera.LabelSeparator), nil
}
// DriverFromMediaSource returns the underlying driver from the MediaSource.
func DriverFromMediaSource[T, U any](src MediaSource[T]) (driver.Driver, error) {
if asMedia, ok := src.(*mediaSource[T, U]); ok {
if asMedia.driver != nil {
return asMedia.driver, nil
}
}
return nil, errors.Errorf("cannot convert media source (type %T) to type (%T)", src, (*mediaSource[T, U])(nil))
}
// newMediaSource instantiates a new media read closer and possibly references the given driver.
func newMediaSource[T, U any](d driver.Driver, r MediaReader[T], p U) MediaSource[T] {
if d != nil {
driverRefs.mu.Lock()
defer driverRefs.mu.Unlock()
label := d.Info().Label
if rcv, ok := driverRefs.refs[label]; ok {
rcv.Ref()
} else {
driverRefs.refs[label] = utils.NewRefCountedValue(d)
driverRefs.refs[label].Ref()
}
}
cancelCtx, cancel := context.WithCancel(context.Background())
ms := &mediaSource[T, U]{
driver: d,
reader: r,
props: p,
rootCancelCtx: cancelCtx,
rootCancel: cancel,
producerConsumers: map[string]*producerConsumer[T, U]{},
}
return ms
}
func (pc *producerConsumer[T, U]) start() {
var startLocalCtx context.Context
var span *trace.Span
func() {
pc.cancelCtxMu.RLock()
defer pc.cancelCtxMu.RUnlock()
startLocalCtx, span = trace.StartSpan(pc.cancelCtx, "gostream::producerConsumer::start")
}()
pc.listenersMu.Lock()
defer pc.listenersMu.Unlock()
pc.listeners++
if pc.listeners != 1 {
span.End()
return
}
pc.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
defer span.End()
first := true
for {
pc.cancelCtxMu.RLock()
if pc.cancelCtx.Err() != nil {
pc.cancelCtxMu.RUnlock()
return
}
pc.cancelCtxMu.RUnlock()
waitForNext := func() (int64, bool) {
_, waitForNextSpan := trace.StartSpan(startLocalCtx, "gostream::producerConsumer::waitForNext")
defer waitForNextSpan.End()
for {
pc.producerCond.L.Lock()
requests := atomic.LoadInt64(&pc.interestedConsumers)
if requests == 0 {
pc.cancelCtxMu.RLock()
if err := pc.cancelCtx.Err(); err != nil {
pc.cancelCtxMu.RUnlock()
pc.producerCond.L.Unlock()
return 0, false
}
pc.cancelCtxMu.RUnlock()
pc.producerCond.Wait()
requests = atomic.LoadInt64(&pc.interestedConsumers)
pc.producerCond.L.Unlock()
if requests == 0 {
continue
}
} else {
pc.producerCond.L.Unlock()
}
return requests, true
}
}
requests, cont := waitForNext()
if !cont {
return
}
pc.cancelCtxMu.RLock()
if err := pc.cancelCtx.Err(); err != nil {
pc.cancelCtxMu.RUnlock()
return
}
pc.cancelCtxMu.RUnlock()
func() {
var doReadSpan *trace.Span
startLocalCtx, doReadSpan = trace.StartSpan(startLocalCtx, "gostream::producerConsumer (anonymous function to read)")
defer func() {
pc.producerCond.L.Lock()
atomic.AddInt64(&pc.interestedConsumers, -requests)
pc.consumerCond.Broadcast()
pc.producerCond.L.Unlock()
doReadSpan.End()
}()
var lastRelease func()
if !first {
// okay to not hold a lock because we are the only both reader AND writer;
// other goroutines are just readers.
lastRelease = pc.current.Release
} else {
first = false
}
startLocalCtx, span := trace.StartSpan(startLocalCtx, "gostream::producerConsumer::readWrapper::Read")
media, release, err := pc.readWrapper.Read(startLocalCtx)
span.End()
ref := utils.NewRefCountedValue(struct{}{})
ref.Ref()
// hold write lock long enough to set current but not for lastRelease
// since the reader (who will call ref) will hold a similar read lock
// to ref before unlocking. This ordering makes sure that we only ever
// call a deref of the previous media once a new one can be fetched.
pc.currentMu.Lock()
pc.current = &mediaRefReleasePairWithError[T]{media, ref, func() {
if ref.Deref() {
if release != nil {
release()
}
}
}, err}
pc.currentMu.Unlock()
if lastRelease != nil {
lastRelease()
}
}()
}
}, func() { defer pc.activeBackgroundWorkers.Done(); pc.cancel() })
}
type mediaRefReleasePairWithError[T any] struct {
Media T
Ref utils.RefCountedValue
Release func()
Err error
}
func (pc *producerConsumer[T, U]) Stop() {
pc.stateMu.Lock()
defer pc.stateMu.Unlock()
pc.stop()
}
// assumes stateMu lock is held.
func (pc *producerConsumer[T, U]) stop() {
var span *trace.Span
func() {
pc.cancelCtxMu.RLock()
defer pc.cancelCtxMu.RUnlock()
_, span = trace.StartSpan(pc.cancelCtx, "gostream::producerConsumer::stop")
}()
defer span.End()
pc.cancel()
pc.producerCond.L.Lock()
pc.producerCond.Signal()
pc.producerCond.L.Unlock()
pc.consumerCond.L.Lock()
pc.consumerCond.Broadcast()
pc.consumerCond.L.Unlock()
pc.activeBackgroundWorkers.Wait()
// reset
cancelCtx, cancel := context.WithCancel(WithMIMETypeHint(pc.rootCancelCtx, pc.mimeType))
pc.cancelCtxMu.Lock()
pc.cancelCtx = cancelCtx
pc.cancelCtxMu.Unlock()
pc.cancel = cancel
}
func (pc *producerConsumer[T, U]) stopOne() {
var span *trace.Span
func() {
pc.cancelCtxMu.RLock()
defer pc.cancelCtxMu.RUnlock()
_, span = trace.StartSpan(pc.cancelCtx, "gostream::producerConsumer::stopOne")
}()
defer span.End()
pc.stateMu.Lock()
defer pc.stateMu.Unlock()
pc.listenersMu.Lock()
defer pc.listenersMu.Unlock()
pc.listeners--
if pc.listeners == 0 {
pc.stop()
}
}
func (ms *mediaSource[T, U]) MediaProperties(_ context.Context) (U, error) {
return ms.props, nil
}
// MediaReleasePairWithError contains the result of fetching media.
type MediaReleasePairWithError[T any] struct {
Media T
Release func()
Err error
}
// NewMediaStreamForChannel returns a MediaStream backed by a channel.
func NewMediaStreamForChannel[T any](ctx context.Context) (context.Context, MediaStream[T], chan<- MediaReleasePairWithError[T]) {
cancelCtx, cancel := context.WithCancel(ctx)
ch := make(chan MediaReleasePairWithError[T])
return cancelCtx, &mediaStreamFromChannel[T]{
media: ch,
cancelCtx: cancelCtx,
cancel: cancel,
}, ch
}
type mediaStreamFromChannel[T any] struct {
media chan MediaReleasePairWithError[T]
cancelCtx context.Context
cancel func()
}
func (ms *mediaStreamFromChannel[T]) Next(ctx context.Context) (T, func(), error) {
ctx, span := trace.StartSpan(ctx, "gostream::mediaStreamFromChannel::Next")
defer span.End()
var zero T
if ms.cancelCtx.Err() != nil {
return zero, nil, ms.cancelCtx.Err()
}
if ctx.Err() != nil {
return zero, nil, ctx.Err()
}
select {
case <-ms.cancelCtx.Done():
return zero, nil, ms.cancelCtx.Err()
case <-ctx.Done():
return zero, nil, ctx.Err()
case pair := <-ms.media:
return pair.Media, pair.Release, pair.Err
}
}
func (ms *mediaStreamFromChannel[T]) Close(ctx context.Context) error {
ms.cancel()
return nil
}
type mediaStream[T any, U any] struct {
mu sync.Mutex
ms *mediaSource[T, U]
prodCon *producerConsumer[T, U]
cancelCtx context.Context
cancel func()
}
func (ms *mediaStream[T, U]) Next(ctx context.Context) (T, func(), error) {
ctx, nextSpan := trace.StartSpan(ctx, "gostream::mediaStream::Next")
defer nextSpan.End()
ms.mu.Lock()
defer ms.mu.Unlock()
// lock keeps us sequential and prevents misuse
var zero T
if err := ms.cancelCtx.Err(); err != nil {
return zero, nil, err
}
ms.prodCon.consumerCond.L.Lock()
// Even though interestedConsumers is atomic, this is a critical section!
// That's because if the producer sees zero interested consumers, it's going
// to Wait but we only want it to do that once we are ready to signal it.
// It's also a RLock because we have many consumers (readers) and one producer (writer).
atomic.AddInt64(&ms.prodCon.interestedConsumers, 1)
ms.prodCon.producerCond.Signal()
select {
case <-ms.cancelCtx.Done():
ms.prodCon.consumerCond.L.Unlock()
return zero, nil, ms.cancelCtx.Err()
case <-ctx.Done():
ms.prodCon.consumerCond.L.Unlock()
return zero, nil, ctx.Err()
default:
}
waitForNext := func() error {
_, span := trace.StartSpan(ctx, "gostream::mediaStream::Next::waitForNext")
defer span.End()
ms.prodCon.consumerCond.Wait()
ms.prodCon.consumerCond.L.Unlock()
if err := ms.cancelCtx.Err(); err != nil {
return err
}
if err := ctx.Err(); err != nil {
return err
}
return nil
}
if err := waitForNext(); err != nil {
return zero, nil, err
}
isAvailable := func() bool {
ms.prodCon.currentMu.RLock()
available := ms.prodCon.current != nil
ms.prodCon.currentMu.RUnlock()
return available
}
for !isAvailable() {
ms.prodCon.consumerCond.L.Lock()
if err := waitForNext(); err != nil {
return zero, nil, err
}
}
ctx, prodConLockSpan := trace.StartSpan(ctx, "gostream::mediaStream::Next (waiting for ms.prodCon lock)")
defer prodConLockSpan.End()
// hold a read lock long enough before current.Ref can be dereffed
// due to a new current being set.
ms.prodCon.currentMu.RLock()
defer ms.prodCon.currentMu.RUnlock()
current := ms.prodCon.current
if current.Err != nil {
return zero, nil, current.Err
}
current.Ref.Ref()
return current.Media, current.Release, nil
}
func (ms *mediaStream[T, U]) Close(ctx context.Context) error {
if parentSpan := trace.FromContext(ctx); parentSpan != nil {
func() {
ms.prodCon.cancelCtxMu.Lock()
defer ms.prodCon.cancelCtxMu.Unlock()
ms.prodCon.cancelCtx = trace.NewContext(ms.prodCon.cancelCtx, parentSpan)
}()
}
var span *trace.Span
func() {
ms.prodCon.cancelCtxMu.Lock()
defer ms.prodCon.cancelCtxMu.Unlock()
ms.prodCon.cancelCtx, span = trace.StartSpan(ms.prodCon.cancelCtx, "gostream::mediaStream::Close")
}()
defer span.End()
ms.cancel()
ms.prodCon.errHandlersMu.Lock()
delete(ms.prodCon.errHandlers, ms)
ms.prodCon.errHandlersMu.Unlock()
ms.prodCon.stopOne()
return nil
}
func (ms *mediaSource[T, U]) Stream(ctx context.Context, errHandlers ...ErrorHandler) (MediaStream[T], error) {
ms.producerConsumersMu.Lock()
mimeType := MIMETypeHint(ctx, "")
prodCon, ok := ms.producerConsumers[mimeType]
if !ok {
// TODO(erd): better to have no max like this and instead clean up over time.
if len(ms.producerConsumers)+1 == 256 {
return nil, errors.New("reached max producer consumers of 256")
}
cancelCtx, cancel := context.WithCancel(WithMIMETypeHint(ms.rootCancelCtx, mimeType))
condMu := &sync.RWMutex{}
producerCond := sync.NewCond(condMu)
consumerCond := sync.NewCond(condMu.RLocker())
prodCon = &producerConsumer[T, U]{
rootCancelCtx: ms.rootCancelCtx,
cancelCtx: cancelCtx,
cancelCtxMu: &sync.RWMutex{},
cancel: cancel,
mimeType: mimeType,
producerCond: producerCond,
consumerCond: consumerCond,
condMu: condMu,
errHandlers: map[*mediaStream[T, U]][]ErrorHandler{},
}
prodCon.readWrapper = MediaReaderFunc[T](func(ctx context.Context) (T, func(), error) {
media, release, err := ms.reader.Read(ctx)
if err == nil {
return media, release, nil
}
prodCon.errHandlersMu.Lock()
defer prodCon.errHandlersMu.Unlock()
for _, handlers := range prodCon.errHandlers {
for _, handler := range handlers {
handler(ctx, err)
}
}
var zero T
return zero, nil, err
})
ms.producerConsumers[mimeType] = prodCon
}
ms.producerConsumersMu.Unlock()
prodCon.stateMu.Lock()
defer prodCon.stateMu.Unlock()
if currentSpan := trace.FromContext(ctx); currentSpan != nil {
func() {
prodCon.cancelCtxMu.Lock()
defer prodCon.cancelCtxMu.Unlock()
prodCon.cancelCtx = trace.NewContext(prodCon.cancelCtx, currentSpan)
}()
}
var cancelCtx context.Context
var cancel context.CancelFunc
func() {
prodCon.cancelCtxMu.RLock()
defer prodCon.cancelCtxMu.RUnlock()
cancelCtx, cancel = context.WithCancel(prodCon.cancelCtx)
}()
stream := &mediaStream[T, U]{
ms: ms,
prodCon: prodCon,
cancelCtx: cancelCtx,
cancel: cancel,
}
if len(errHandlers) != 0 {
prodCon.errHandlersMu.Lock()
prodCon.errHandlers[stream] = errHandlers
prodCon.errHandlersMu.Unlock()
}
prodCon.start()
return stream, nil
}
func (ms *mediaSource[T, U]) Close(ctx context.Context) error {
func() {
ms.producerConsumersMu.Lock()
defer ms.producerConsumersMu.Unlock()
for _, prodCon := range ms.producerConsumers {
prodCon.Stop()
}
}()
err := ms.reader.Close(ctx)
if ms.driver == nil {
return err
}
driverRefs.mu.Lock()
defer driverRefs.mu.Unlock()
label := ms.driver.Info().Label
if rcv, ok := driverRefs.refs[label]; ok {
if rcv.Deref() {
delete(driverRefs.refs, label)
return multierr.Combine(err, ms.driver.Close())
}
} else {
return multierr.Combine(err, ms.driver.Close())
}
// Do not close if a driver is being referenced. Client will decide what to do if
// they encounter this error.
return multierr.Combine(err, &DriverInUseError{label})
}