-
Notifications
You must be signed in to change notification settings - Fork 110
/
audioinput.go
78 lines (67 loc) · 2.1 KB
/
audioinput.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
//go:build !no_cgo
package inject
import (
"context"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pkg/errors"
"go.viam.com/rdk/components/audioinput"
"go.viam.com/rdk/gostream"
"go.viam.com/rdk/resource"
)
// AudioInput is an injected audio input.
type AudioInput struct {
audioinput.AudioInput
name resource.Name
DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error)
StreamFunc func(
ctx context.Context,
errHandlers ...gostream.ErrorHandler,
) (gostream.AudioStream, error)
MediaPropertiesFunc func(ctx context.Context) (prop.Audio, error)
CloseFunc func(ctx context.Context) error
}
// NewAudioInput returns a new injected audio input.
func NewAudioInput(name string) *AudioInput {
return &AudioInput{name: audioinput.Named(name)}
}
// Name returns the name of the resource.
func (ai *AudioInput) Name() resource.Name {
return ai.name
}
// Stream calls the injected Stream or the real version.
func (ai *AudioInput) Stream(
ctx context.Context,
errHandlers ...gostream.ErrorHandler,
) (gostream.AudioStream, error) {
if ai.StreamFunc == nil {
return ai.AudioInput.Stream(ctx, errHandlers...)
}
return ai.StreamFunc(ctx, errHandlers...)
}
// MediaProperties calls the injected MediaProperties or the real version.
func (ai *AudioInput) MediaProperties(ctx context.Context) (prop.Audio, error) {
if ai.MediaPropertiesFunc != nil {
return ai.MediaPropertiesFunc(ctx)
}
if ai.AudioInput != nil {
return ai.AudioInput.MediaProperties(ctx)
}
return prop.Audio{}, errors.Wrap(ctx.Err(), "media properties unavailable")
}
// Close calls the injected Close or the real version.
func (ai *AudioInput) Close(ctx context.Context) error {
if ai.CloseFunc == nil {
if ai.AudioInput == nil {
return nil
}
return ai.AudioInput.Close(ctx)
}
return ai.CloseFunc(ctx)
}
// DoCommand calls the injected DoCommand or the real version.
func (ai *AudioInput) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
if ai.DoFunc == nil {
return ai.AudioInput.DoCommand(ctx, cmd)
}
return ai.DoFunc(ctx, cmd)
}