-
Notifications
You must be signed in to change notification settings - Fork 110
/
server.go
320 lines (288 loc) · 7.82 KB
/
server.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
package audioinput
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"time"
"unsafe"
"github.com/go-audio/audio"
"github.com/go-audio/transforms"
"github.com/go-audio/wav"
"github.com/pion/mediadevices/pkg/wave"
"github.com/pkg/errors"
"go.opencensus.io/trace"
commonpb "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/audioinput/v1"
"go.viam.com/utils"
"google.golang.org/genproto/googleapis/api/httpbody"
"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/src-d/go-billy.v4/memfs"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
)
// HostEndian indicates the byte ordering this host natively uses.
var HostEndian binary.ByteOrder
func init() {
// from github.com/pion/mediadevices/pkg/wave/decoder.go
//nolint:gosec
switch v := *(*uint16)(unsafe.Pointer(&([]byte{0x12, 0x34}[0]))); v {
case 0x1234:
HostEndian = binary.BigEndian
case 0x3412:
HostEndian = binary.LittleEndian
default:
panic(fmt.Sprintf("failed to determine host endianness: %x", v))
}
}
// serviceServer implements the AudioInputService from audioinput.proto.
type serviceServer struct {
pb.UnimplementedAudioInputServiceServer
coll resource.APIResourceCollection[AudioInput]
}
// NewRPCServiceServer constructs an audio input gRPC service server.
// It is intentionally untyped to prevent use outside of tests.
func NewRPCServiceServer(coll resource.APIResourceCollection[AudioInput]) interface{} {
return &serviceServer{coll: coll}
}
// Chunks returns audio chunks (samples) forever from an audio input of the underlying robot. A specific sampling
// format can be requested but may not necessarily be the same one returned.
func (s *serviceServer) Chunks(req *pb.ChunksRequest, server pb.AudioInputService_ChunksServer) error {
audioInput, err := s.coll.Resource(req.Name)
if err != nil {
return err
}
chunkStream, err := audioInput.Stream(server.Context())
if err != nil {
return err
}
defer func() {
utils.UncheckedError(chunkStream.Close(server.Context()))
}()
firstChunk, release, err := chunkStream.Next(server.Context())
if err != nil {
return err
}
info := firstChunk.ChunkInfo()
release()
var sf wave.SampleFormat
sfProto := req.SampleFormat
switch req.SampleFormat {
case pb.SampleFormat_SAMPLE_FORMAT_UNSPECIFIED:
sfProto = pb.SampleFormat_SAMPLE_FORMAT_INT16_INTERLEAVED
fallthrough
case pb.SampleFormat_SAMPLE_FORMAT_INT16_INTERLEAVED:
sf = wave.Int16SampleFormat
case pb.SampleFormat_SAMPLE_FORMAT_FLOAT32_INTERLEAVED:
sf = wave.Float32SampleFormat
default:
return errors.Errorf("unknown type of audio sample format %v", req.SampleFormat)
}
if err := server.Send(&pb.ChunksResponse{
Type: &pb.ChunksResponse_Info{
Info: &pb.AudioChunkInfo{
SampleFormat: sfProto,
Channels: uint32(info.Channels),
SamplingRate: int64(info.SamplingRate),
},
},
}); err != nil {
return err
}
sendNextChunk := func() error {
chunk, release, err := chunkStream.Next(server.Context())
if err != nil {
return err
}
defer release()
var outBytes []byte
switch c := chunk.(type) {
case *wave.Int16Interleaved:
outBytes = make([]byte, len(c.Data)*2)
buf := bytes.NewBuffer(outBytes[:0])
chunkCopy := c
if sf != chunk.SampleFormat() {
chunkCopy = wave.NewInt16Interleaved(info)
// convert
for i := 0; i < c.Size.Len; i++ {
for j := 0; j < c.Size.Channels; j++ {
chunkCopy.Set(i, j, chunk.At(i, j))
}
}
}
if err := binary.Write(buf, HostEndian, chunkCopy.Data); err != nil {
return err
}
case *wave.Float32Interleaved:
outBytes = make([]byte, len(c.Data)*4)
buf := bytes.NewBuffer(outBytes[:0])
chunkCopy := c
if sf != chunk.SampleFormat() {
chunkCopy = wave.NewFloat32Interleaved(info)
// convert
for i := 0; i < c.Size.Len; i++ {
for j := 0; j < c.Size.Channels; j++ {
chunkCopy.Set(i, j, chunk.At(i, j))
}
}
}
if err := binary.Write(buf, HostEndian, chunkCopy.Data); err != nil {
return err
}
default:
return errors.Errorf("unknown type of audio buffer %T", chunk)
}
return server.Send(&pb.ChunksResponse{
Type: &pb.ChunksResponse_Chunk{
Chunk: &pb.AudioChunk{
Data: outBytes,
Length: uint32(info.Len),
},
},
})
}
for {
if err := sendNextChunk(); err != nil {
return err
}
}
}
// Properties returns properties of an audio input of the underlying robot.
func (s *serviceServer) Properties(
ctx context.Context,
req *pb.PropertiesRequest,
) (*pb.PropertiesResponse, error) {
audioInput, err := s.coll.Resource(req.Name)
if err != nil {
return nil, err
}
props, err := audioInput.MediaProperties(ctx)
if err != nil {
return nil, err
}
return &pb.PropertiesResponse{
ChannelCount: uint32(props.ChannelCount),
Latency: durationpb.New(props.Latency),
SampleRate: uint32(props.SampleRate),
SampleSize: uint32(props.SampleSize),
IsBigEndian: props.IsBigEndian,
IsFloat: props.IsFloat,
IsInterleaved: props.IsInterleaved,
}, nil
}
// Record renders an audio chunk from an audio input of the underlying robot
// to an HTTP response. A specific MIME type cannot be requested and may not necessarily
// be the same one returned each time.
func (s *serviceServer) Record(
ctx context.Context,
req *pb.RecordRequest,
) (*httpbody.HttpBody, error) {
ctx, span := trace.StartSpan(ctx, "audioinput::server::Record")
defer span.End()
audioInput, err := s.coll.Resource(req.Name)
if err != nil {
return nil, err
}
chunkStream, err := audioInput.Stream(ctx)
if err != nil {
return nil, err
}
defer func() {
utils.UncheckedError(chunkStream.Close(ctx))
}()
firstChunk, release, err := chunkStream.Next(ctx)
if err != nil {
return nil, err
}
info := firstChunk.ChunkInfo()
release()
duration := req.Duration.AsDuration()
if duration == 0 {
duration = time.Second
}
if duration > 5*time.Second {
return nil, errors.New("can only record up to 5 seconds")
}
ms := memfs.New()
fd, err := ms.Create("dummy")
if err != nil {
return nil, err
}
wavEnc := wav.NewEncoder(fd,
info.SamplingRate,
24,
info.Channels,
1, // PCM
)
nextChunk := func() error {
chunk, release, err := chunkStream.Next(ctx)
if err != nil {
return err
}
defer release()
switch c := chunk.(type) {
case *wave.Int16Interleaved:
cData := make([]int, len(c.Data))
for i := 0; i < len(c.Data); i++ {
cData[i] = int(c.Data[i])
}
buf := &audio.IntBuffer{
Format: &audio.Format{
NumChannels: info.Channels,
SampleRate: info.SamplingRate,
},
Data: cData,
SourceBitDepth: 16,
}
return wavEnc.Write(buf)
case *wave.Float32Interleaved:
dataCopy := make([]float32, len(c.Data))
copy(dataCopy, c.Data)
buf := &audio.Float32Buffer{
Format: &audio.Format{
NumChannels: info.Channels,
SampleRate: info.SamplingRate,
},
Data: dataCopy,
SourceBitDepth: 32,
}
if err := transforms.PCMScaleF32(buf, 24); err != nil {
return err
}
return wavEnc.Write(buf.AsIntBuffer())
default:
return errors.Errorf("unknown type of audio buffer %T", chunk)
}
}
numChunks := int(duration.Seconds() * float64(info.SamplingRate/info.Len))
for i := 0; i < numChunks; i++ {
if err := nextChunk(); err != nil {
return nil, err
}
}
if err := wavEnc.Close(); err != nil {
return nil, err
}
if _, err := fd.Seek(0, io.SeekStart); err != nil {
return nil, err
}
rd, err := io.ReadAll(fd)
if err != nil {
return nil, err
}
return &httpbody.HttpBody{
ContentType: "audio/wav",
Data: rd,
}, nil
}
// DoCommand receives arbitrary commands.
func (s *serviceServer) DoCommand(ctx context.Context,
req *commonpb.DoCommandRequest,
) (*commonpb.DoCommandResponse, error) {
audioInput, err := s.coll.Resource(req.GetName())
if err != nil {
return nil, err
}
return protoutils.DoFromResourceServer(ctx, audioInput, req)
}