This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
forked from edaniels/gostream
-
Notifications
You must be signed in to change notification settings - Fork 4
/
source_stream_utils.go
102 lines (95 loc) · 3.01 KB
/
source_stream_utils.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
package gostream
import (
"context"
"github.com/edaniels/golog"
"go.viam.com/utils"
)
// StreamVideoSource streams the given video source to the stream forever until context signals cancellation.
func StreamVideoSource(ctx context.Context, vs VideoSource, stream Stream) error {
return streamMediaSource(ctx, vs, stream, func(ctx context.Context, frameErr error) {
golog.Global().Debugw("error getting frame", "error", frameErr)
}, stream.InputVideoFrames)
}
// StreamAudioSource streams the given video source to the stream forever until context signals cancellation.
func StreamAudioSource(ctx context.Context, as AudioSource, stream Stream) error {
return streamMediaSource(ctx, as, stream, func(ctx context.Context, frameErr error) {
golog.Global().Debugw("error getting frame", "error", frameErr)
}, stream.InputAudioChunks)
}
// StreamVideoSourceWithErrorHandler streams the given video source to the stream forever
// until context signals cancellation, frame errors are sent via the error handler.
func StreamVideoSourceWithErrorHandler(
ctx context.Context, vs VideoSource, stream Stream, errHandler ErrorHandler,
) error {
return streamMediaSource(ctx, vs, stream, errHandler, stream.InputVideoFrames)
}
// StreamAudioSourceWithErrorHandler streams the given audio source to the stream forever
// until context signals cancellation, audio errors are sent via the error handler.
func StreamAudioSourceWithErrorHandler(
ctx context.Context, as AudioSource, stream Stream, errHandler ErrorHandler,
) error {
return streamMediaSource(ctx, as, stream, errHandler, stream.InputAudioChunks)
}
// streamMediaSource will stream a source of media forever to the stream until the given context tells it to cancel.
func streamMediaSource[T, U any](
ctx context.Context,
ms MediaSource[T],
stream Stream,
errHandler ErrorHandler,
inputChan func(props U) (chan<- MediaReleasePair[T], error),
) error {
streamLoop := func() error {
readyCh, readyCtx := stream.StreamingReady()
select {
case <-ctx.Done():
return ctx.Err()
case <-readyCh:
}
var props U
if provider, ok := ms.(MediaPropertyProvider[U]); ok {
var err error
props, err = provider.MediaProperties(ctx)
if err != nil {
golog.Global().Debugw("no properties found for media; will assume empty", "error", err)
}
} else {
golog.Global().Debug("no properties found for media; will assume empty")
}
input, err := inputChan(props)
if err != nil {
return err
}
mediaStream, err := ms.Stream(ctx, errHandler)
if err != nil {
return err
}
defer func() {
utils.UncheckedError(mediaStream.Close(ctx))
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-readyCtx.Done():
return nil
default:
}
media, release, err := mediaStream.Next(ctx)
if err != nil {
continue
}
select {
case <-ctx.Done():
return ctx.Err()
case <-readyCtx.Done():
return nil
case input <- MediaReleasePair[T]{media, release}:
}
}
}
for {
if err := streamLoop(); err != nil {
return err
}
}
}