-
Notifications
You must be signed in to change notification settings - Fork 110
/
client.go
576 lines (511 loc) · 18 KB
/
client.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
package camera
import (
"bytes"
"context"
"fmt"
"image"
"io"
"slices"
"sync"
"time"
"github.com/google/uuid"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"go.uber.org/multierr"
pb "go.viam.com/api/component/camera/v1"
streampb "go.viam.com/api/stream/v1"
goutils "go.viam.com/utils"
goprotoutils "go.viam.com/utils/protoutils"
"go.viam.com/utils/rpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
"go.viam.com/rdk/components/camera/rtppassthrough"
"go.viam.com/rdk/data"
"go.viam.com/rdk/gostream"
rdkgrpc "go.viam.com/rdk/grpc"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage"
"go.viam.com/rdk/rimage/transform"
"go.viam.com/rdk/utils"
)
var (
// ErrNoPeerConnection indicates there was no peer connection.
ErrNoPeerConnection = errors.New("No PeerConnection")
// ErrNoSharedPeerConnection indicates there was no shared peer connection.
ErrNoSharedPeerConnection = errors.New("No Shared PeerConnection")
// ErrUnknownStreamSubscriptionID indicates that a StreamSubscriptionID is unknown.
ErrUnknownStreamSubscriptionID = errors.New("StreamSubscriptionID Unknown")
unsubscribeAllRemoveStreamTimeout = time.Second
)
type (
singlePacketCallback func(*rtp.Packet) error
subAndCallback struct {
cb singlePacketCallback
sub *rtppassthrough.StreamSubscription
}
subAndCallbackByID map[rtppassthrough.SubscriptionID]subAndCallback
)
// client implements CameraServiceClient.
type client struct {
resource.Named
resource.TriviallyReconfigurable
name string
conn rpc.ClientConn
client pb.CameraServiceClient
streamClient streampb.StreamServiceClient
logger logging.Logger
activeBackgroundWorkers sync.WaitGroup
mu sync.Mutex
healthyClientCh chan struct{}
subAndCallbackByID subAndCallbackByID
}
// NewClientFromConn constructs a new Client from connection passed in.
func NewClientFromConn(
ctx context.Context,
conn rpc.ClientConn,
remoteName string,
name resource.Name,
logger logging.Logger,
) (Camera, error) {
c := pb.NewCameraServiceClient(conn)
streamClient := streampb.NewStreamServiceClient(conn)
return &client{
Named: name.PrependRemote(remoteName).AsNamed(),
name: name.ShortName(),
conn: conn,
streamClient: streamClient,
client: c,
subAndCallbackByID: map[rtppassthrough.SubscriptionID]subAndCallback{},
logger: logger,
}, nil
}
func getExtra(ctx context.Context) (*structpb.Struct, error) {
ext := &structpb.Struct{}
if extra, ok := FromContext(ctx); ok {
var err error
if ext, err = goprotoutils.StructToStructPb(extra); err != nil {
return nil, err
}
}
dataExt, err := data.GetExtraFromContext(ctx)
if err != nil {
return nil, err
}
proto.Merge(ext, dataExt)
return ext, nil
}
func (c *client) Read(ctx context.Context) (image.Image, func(), error) {
ctx, span := trace.StartSpan(ctx, "camera::client::Read")
defer span.End()
mimeType := gostream.MIMETypeHint(ctx, "")
expectedType, _ := utils.CheckLazyMIMEType(mimeType)
ext, err := getExtra(ctx)
if err != nil {
return nil, nil, err
}
resp, err := c.client.GetImage(ctx, &pb.GetImageRequest{
Name: c.name,
MimeType: expectedType,
Extra: ext,
})
if err != nil {
return nil, nil, err
}
if resp.MimeType != expectedType {
c.logger.CDebugw(ctx, "got different MIME type than what was asked for", "sent", expectedType, "received", resp.MimeType)
} else {
resp.MimeType = mimeType
}
resp.MimeType = utils.WithLazyMIMEType(resp.MimeType)
img, err := rimage.DecodeImage(ctx, resp.Image, resp.MimeType)
if err != nil {
return nil, nil, err
}
return img, func() {}, nil
}
func (c *client) Stream(
ctx context.Context,
errHandlers ...gostream.ErrorHandler,
) (gostream.VideoStream, error) {
ctx, span := trace.StartSpan(ctx, "camera::client::Stream")
// RSDK-6340: The resource manager closes remote resources when the underlying
// connection goes bad. However, when the connection is re-established, the client
// objects these resources represent are not re-initialized/marked "healthy".
// `healthyClientCh` helps track these transitions between healthy and unhealthy
// states.
//
// When a new `client.Stream()` is created we will either use the existing
// `healthyClientCh` or create a new one.
//
// The goroutine a `Stream()` method spins off will listen to its version of the
// `healthyClientCh` to be notified when the connection has died so it can gracefully
// terminate.
//
// When a connection becomes unhealthy, the resource manager will call `Close` on the
// camera client object. Closing the client will:
// 1. close its `client.healthyClientCh` channel
// 2. wait for existing "stream" goroutines to drain
// 3. nil out the `client.healthyClientCh` member variable
//
// New streams concurrent with closing cannot start until this drain completes. There
// will never be stream goroutines from the old "generation" running concurrently
// with those from the new "generation".
c.mu.Lock()
if c.healthyClientCh == nil {
c.healthyClientCh = make(chan struct{})
}
healthyClientCh := c.healthyClientCh
c.mu.Unlock()
ctxWithMIME := gostream.WithMIMETypeHint(context.Background(), gostream.MIMETypeHint(ctx, ""))
streamCtx, stream, frameCh := gostream.NewMediaStreamForChannel[image.Image](ctxWithMIME)
c.activeBackgroundWorkers.Add(1)
goutils.PanicCapturingGo(func() {
streamCtx = trace.NewContext(streamCtx, span)
defer span.End()
defer c.activeBackgroundWorkers.Done()
defer close(frameCh)
for {
if streamCtx.Err() != nil {
return
}
frame, release, err := c.Read(streamCtx)
if err != nil {
for _, handler := range errHandlers {
handler(streamCtx, err)
}
}
select {
case <-streamCtx.Done():
return
case <-healthyClientCh:
if err := stream.Close(ctxWithMIME); err != nil {
c.logger.CWarnw(ctx, "error closing stream", "err", err)
}
return
case frameCh <- gostream.MediaReleasePairWithError[image.Image]{
Media: frame,
Release: release,
Err: err,
}:
}
}
})
return stream, nil
}
func (c *client) Images(ctx context.Context) ([]NamedImage, resource.ResponseMetadata, error) {
ctx, span := trace.StartSpan(ctx, "camera::client::Images")
defer span.End()
resp, err := c.client.GetImages(ctx, &pb.GetImagesRequest{
Name: c.name,
})
if err != nil {
return nil, resource.ResponseMetadata{}, errors.Wrap(err, "camera client: could not gets images from the camera")
}
images := make([]NamedImage, 0, len(resp.Images))
// keep everything lazy encoded by default, if type is unknown, attempt to decode it
for _, img := range resp.Images {
var rdkImage image.Image
switch img.Format {
case pb.Format_FORMAT_RAW_RGBA:
rdkImage = rimage.NewLazyEncodedImage(img.Image, utils.MimeTypeRawRGBA)
case pb.Format_FORMAT_RAW_DEPTH:
rdkImage = rimage.NewLazyEncodedImage(img.Image, utils.MimeTypeRawDepth)
case pb.Format_FORMAT_JPEG:
rdkImage = rimage.NewLazyEncodedImage(img.Image, utils.MimeTypeJPEG)
case pb.Format_FORMAT_PNG:
rdkImage = rimage.NewLazyEncodedImage(img.Image, utils.MimeTypePNG)
case pb.Format_FORMAT_UNSPECIFIED:
rdkImage, _, err = image.Decode(bytes.NewReader(img.Image))
if err != nil {
return nil, resource.ResponseMetadata{}, err
}
}
images = append(images, NamedImage{rdkImage, img.SourceName})
}
return images, resource.ResponseMetadataFromProto(resp.ResponseMetadata), nil
}
func (c *client) NextPointCloud(ctx context.Context) (pointcloud.PointCloud, error) {
ctx, span := trace.StartSpan(ctx, "camera::client::NextPointCloud")
defer span.End()
ctx, getPcdSpan := trace.StartSpan(ctx, "camera::client::NextPointCloud::GetPointCloud")
ext, err := data.GetExtraFromContext(ctx)
if err != nil {
return nil, err
}
resp, err := c.client.GetPointCloud(ctx, &pb.GetPointCloudRequest{
Name: c.name,
MimeType: utils.MimeTypePCD,
Extra: ext,
})
getPcdSpan.End()
if err != nil {
return nil, err
}
if resp.MimeType != utils.MimeTypePCD {
return nil, fmt.Errorf("unknown pc mime type %s", resp.MimeType)
}
return func() (pointcloud.PointCloud, error) {
_, span := trace.StartSpan(ctx, "camera::client::NextPointCloud::ReadPCD")
defer span.End()
return pointcloud.ReadPCD(bytes.NewReader(resp.PointCloud))
}()
}
func (c *client) Projector(ctx context.Context) (transform.Projector, error) {
var proj transform.Projector
props, err := c.Properties(ctx)
if err != nil {
return nil, err
}
intrinsics := props.IntrinsicParams
err = intrinsics.CheckValid()
if err != nil {
return nil, err
}
proj = intrinsics
return proj, nil
}
func (c *client) Properties(ctx context.Context) (Properties, error) {
result := Properties{}
resp, err := c.client.GetProperties(ctx, &pb.GetPropertiesRequest{
Name: c.name,
})
if err != nil {
return Properties{}, err
}
if intrinsics := resp.IntrinsicParameters; intrinsics != nil {
result.IntrinsicParams = &transform.PinholeCameraIntrinsics{
Width: int(intrinsics.WidthPx),
Height: int(intrinsics.HeightPx),
Fx: intrinsics.FocalXPx,
Fy: intrinsics.FocalYPx,
Ppx: intrinsics.CenterXPx,
Ppy: intrinsics.CenterYPx,
}
}
result.MimeTypes = resp.MimeTypes
result.SupportsPCD = resp.SupportsPcd
// if no distortion model present, return result with no model
if resp.DistortionParameters == nil {
return result, nil
}
if resp.DistortionParameters.Model == "" { // same as if nil
return result, nil
}
// switch distortion model based on model name
model := transform.DistortionType(resp.DistortionParameters.Model)
distorter, err := transform.NewDistorter(model, resp.DistortionParameters.Parameters)
if err != nil {
return Properties{}, err
}
result.DistortionParams = distorter
return result, nil
}
func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
return protoutils.DoFromResourceClient(ctx, c.client, c.name, cmd)
}
// TODO(RSDK-6433): This method can be called more than once during a client's lifecycle.
// For example, consider a case where a remote camera goes offline and then back online.
// We will call `Close` on the camera client when we detect the disconnection to remove
// active streams but then reuse the client when the connection is re-established.
func (c *client) Close(ctx context.Context) error {
_, span := trace.StartSpan(ctx, "camera::client::Close")
defer span.End()
c.mu.Lock()
// NOTE: we are intentionally releasing the lock before we wait for
// background goroutines to terminate as some of them need to be able
// to take the lock to terminate
defer c.activeBackgroundWorkers.Wait()
defer c.mu.Unlock()
if c.healthyClientCh != nil {
close(c.healthyClientCh)
}
c.healthyClientCh = nil
// unsubscribe from all video streams that have been established with modular cameras
if err := c.unsubscribeAll(); err != nil {
c.logger.CErrorw(ctx, "Close > unsubscribeAll", "err", err, "name", c.Name())
return err
}
return nil
}
func (c *client) SubscribeRTP(
ctx context.Context,
bufferSize int,
packetsCB rtppassthrough.PacketCallback,
) (rtppassthrough.SubscriptionID, error) {
ctx, span := trace.StartSpan(ctx, "camera::client::SubscribeRTP")
defer span.End()
c.mu.Lock()
defer c.mu.Unlock()
onError := func(err error) { c.logger.CErrorw(ctx, "StreamSubscription returned hit an error", "err", err) }
sub, err := rtppassthrough.NewStreamSubscription(bufferSize, onError)
if err != nil {
return uuid.Nil, err
}
if c.conn.PeerConn() == nil {
return uuid.Nil, ErrNoPeerConnection
}
// check if we have established a connection that can be shared by multiple clients asking for cameras streams from viam server.
sc, ok := c.conn.(*rdkgrpc.SharedConn)
if !ok {
return uuid.Nil, ErrNoSharedPeerConnection
}
c.logger.CDebugw(ctx, "SubscribeRTP", "subID", sub.ID().String(), "name", c.Name(), "subAndCallbackByID", c.subAndCallbackByID.String())
defer func() {
c.logger.CDebugw(ctx, "SubscribeRTP after", "subID", sub.ID().String(),
"name", c.Name(), "subAndCallbackByID", c.subAndCallbackByID.String())
}()
// add the subscription to subAndCallbackByID so the goroutine spawned by
// addOnTrackSubFunc can forward the packets it receives from the modular camera
// over WebRTC to the SubscribeRTP caller via the packetsCB callback
c.subAndCallbackByID[sub.ID()] = subAndCallback{
cb: func(p *rtp.Packet) error { return packetsCB([]*rtp.Packet{p}) },
sub: sub,
}
// Start the subscription to process calls to sub.Publish
sub.Start()
// add the camera model's addOnTrackSubFunc to the shared peer connection's
// slice of OnTrack callbacks. This is what allows
// all the subAndCallbackByID's callback functions to be called with the
// RTP packets from the module's peer connection's track
sc.AddOnTrackSub(c.Name(), c.addOnTrackSubFunc)
// B/c there is only ever either 0 or 1 peer connections between a module & a viam-server
// once AddStream is called on the module for a given camera model instance & succeeds, we shouldn't
// call it again until RemoveStream is called for a few reasons:
// 1. doing so would result in 2 webrtc tracks for the same camera sending the exact same RTP packets which would
// needlessly waste resources
// 2. b/c the signature of RemoveStream just takes the camera name, if there are 2 streams for the same camera
// & the module receives a call to RemoveStream, there is no way for the module to know which camera stream
// should be removed
if len(c.subAndCallbackByID) == 1 {
if _, err := c.streamClient.AddStream(ctx, &streampb.AddStreamRequest{Name: c.Name().String()}); err != nil {
c.logger.CDebugw(ctx, "SubscribeRTP AddStream hit error", "subID", sub.ID().String(), "name", c.Name(), "err", err)
sub.Close()
delete(c.subAndCallbackByID, sub.ID())
sc.RemoveOnTrackSub(c.Name())
return uuid.Nil, err
}
}
c.logger.CInfow(ctx, "SubscribeRTP succeeded", "subID", sub.ID().String(),
"name", c.Name(), "subAndCallbackByID", c.subAndCallbackByID.String())
return sub.ID(), nil
}
func (c *client) addOnTrackSubFunc(tr *webrtc.TrackRemote, r *webrtc.RTPReceiver) {
c.mu.Lock()
defer c.mu.Unlock()
if c.Name().String() != tr.StreamID() {
c.logger.Warnw("SubscribeRTP: PeerConn().OnTrack: ", "name", c.Name(), "!= streamID", tr.StreamID())
return
}
c.activeBackgroundWorkers.Add(1)
goutils.ManagedGo(func() {
defer func() {
c.mu.Lock()
defer c.mu.Unlock()
if err := c.unsubscribeAll(); err != nil {
c.logger.Errorw("unsubscribeAll", "err", err, "name", c.Name())
}
}()
for {
pkt, _, err := tr.ReadRTP()
if err != nil {
if !errors.Is(err, io.EOF) {
c.logger.Errorw("SubscribeRTP: camera client", "name ", c.Name(),
"OnTrack callback hit unexpected error from ReadRTP err:", err.Error())
}
c.logger.Infow("SubscribeRTP: camera client", "name ", c.Name(),
"OnTrack callback terminating ReadRTP loop due to ", err.Error())
return
}
c.mu.Lock()
if len(c.subAndCallbackByID) == 0 {
c.mu.Unlock()
c.logger.Infow("SubscribeRTP: camera client", "name", c.Name(),
"OnTrack callback terminating as the camera client no longer has any subscribers")
return
}
for _, subAndCB := range c.subAndCallbackByID {
if err := subAndCB.sub.Publish(func() error { return subAndCB.cb(pkt) }); err != nil {
c.logger.Infow("SubscribeRTP: camera client", "name", c.Name(), "dropped an RTP packet dropped due to", "err", err.Error())
}
}
c.mu.Unlock()
}
}, c.activeBackgroundWorkers.Done)
}
func (c *client) Unsubscribe(ctx context.Context, id rtppassthrough.SubscriptionID) error {
ctx, span := trace.StartSpan(ctx, "camera::client::Unsubscribe")
defer span.End()
c.mu.Lock()
defer c.mu.Unlock()
if c.conn.PeerConn() == nil {
return ErrNoPeerConnection
}
sc, ok := c.conn.(*rdkgrpc.SharedConn)
if !ok {
return ErrNoSharedPeerConnection
}
c.logger.CInfow(ctx, "Unsubscribe called with", "name", c.Name(), "subID", id.String())
subAndCB, ok := c.subAndCallbackByID[id]
if !ok {
c.logger.CWarnw(ctx, "Unsubscribe called with unknown subID ", "name", c.Name(), "subID", id.String())
return ErrUnknownStreamSubscriptionID
}
subAndCB.sub.Close()
delete(c.subAndCallbackByID, id)
if len(c.subAndCallbackByID) == 0 {
sc.RemoveOnTrackSub(c.Name())
c.logger.CInfow(ctx, "Unsubscribe calling RemoveStream", "name", c.Name(), "subID", id.String())
if _, err := c.streamClient.RemoveStream(ctx, &streampb.RemoveStreamRequest{Name: c.Name().String()}); err != nil {
c.logger.CWarnw(ctx, "Unsubscribe RemoveStream returned err", "name", c.Name(), "subID", id.String(), "err", err)
return err
}
}
return nil
}
func (c *client) unsubscribeAll() error {
sc, ok := c.conn.(*rdkgrpc.SharedConn)
if !ok {
return nil
}
var errAgg error
if len(c.subAndCallbackByID) > 0 {
for id, subAndCB := range c.subAndCallbackByID {
c.logger.Infow("unsubscribeAll", "name", c.Name(), "subID", id.String())
subAndCB.sub.Close()
delete(c.subAndCallbackByID, id)
}
sc.RemoveOnTrackSub(c.Name())
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), unsubscribeAllRemoveStreamTimeout)
c.logger.Infow("unsubscribeAll calling RemoveStream", "name", c.Name())
_, err := c.streamClient.RemoveStream(timeoutCtx, &streampb.RemoveStreamRequest{Name: c.Name().String()})
timeoutCancel()
if err != nil {
c.logger.Warnw("unsubscribeAll RemoveStream returned err", "name", c.Name(), "err", err)
errAgg = multierr.Combine(errAgg, errors.Wrapf(err, "error calling RemoveStream with name: %s", c.Name()))
}
}
return errAgg
}
func (s subAndCallbackByID) String() string {
if len(s) == 0 {
return "len: 0"
}
strIds := []string{}
strIdsToCB := map[string]subAndCallback{}
for id, cb := range s {
strID := id.String()
strIds = append(strIds, strID)
strIdsToCB[strID] = cb
}
slices.Sort(strIds)
ret := fmt.Sprintf("len: %d, ", len(s))
for _, strID := range strIds {
ret += fmt.Sprintf("%s: %v, ", strID, strIdsToCB[strID])
}
return ret
}