-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathweb.go
1339 lines (1182 loc) · 38 KB
/
web.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package web provides gRPC/REST/GUI APIs to control and monitor a robot.
package web
import (
"context"
"fmt"
"html/template"
"io"
"io/fs"
"math"
"net"
"net/http"
"net/http/pprof"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Masterminds/sprig"
"github.com/NYTimes/gziphandler"
"github.com/edaniels/golog"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/jhump/protoreflect/dynamic"
"github.com/pkg/errors"
"github.com/rs/cors"
"github.com/viamrobotics/gostream"
streampb "github.com/viamrobotics/gostream/proto/stream/v1"
"go.opencensus.io/trace"
pb "go.viam.com/api/robot/v1"
"go.viam.com/utils"
echopb "go.viam.com/utils/proto/rpc/examples/echo/v1"
"go.viam.com/utils/rpc"
echoserver "go.viam.com/utils/rpc/examples/echo/server"
"goji.io"
"goji.io/pat"
googlegrpc "google.golang.org/grpc"
"go.viam.com/rdk/components/audioinput"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/config"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/module"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/robot"
grpcserver "go.viam.com/rdk/robot/server"
weboptions "go.viam.com/rdk/robot/web/options"
webstream "go.viam.com/rdk/robot/web/stream"
rutils "go.viam.com/rdk/utils"
"go.viam.com/rdk/web"
)
// SubtypeName is a constant that identifies the internal web resource subtype string.
const SubtypeName = "web"
// API is the fully qualified API for the internal web service.
var API = resource.APINamespaceRDKInternal.WithServiceType(SubtypeName)
// InternalServiceName is used to refer to/depend on this service internally.
var InternalServiceName = resource.NewName(API, "builtin")
// defaultMethodTimeout is the default context timeout for all inbound gRPC
// methods used when no deadline is set on the context.
var defaultMethodTimeout = 10 * time.Minute
// robotWebApp hosts a web server to interact with a robot in addition to hosting
// a gRPC/REST server.
type robotWebApp struct {
template *template.Template
theRobot robot.Robot
logger golog.Logger
options weboptions.Options
}
// Init does template initialization work.
func (app *robotWebApp) Init() error {
var err error
t := template.New("foo").Funcs(template.FuncMap{
//nolint:gosec
"jsSafe": func(js string) template.JS {
return template.JS(js)
},
//nolint:gosec
"htmlSafe": func(html string) template.HTML {
return template.HTML(html)
},
}).Funcs(sprig.FuncMap())
if app.options.SharedDir != "" {
t, err = t.ParseGlob(fmt.Sprintf("%s/*.html", app.options.SharedDir+"/templates"))
} else {
t, err = t.ParseFS(web.AppFS, "runtime-shared/templates/*.html")
}
if err != nil {
return err
}
app.template = t.Lookup("webappindex.html")
return nil
}
// AppTemplateData is used to render the remote control page.
type AppTemplateData struct {
WebRTCEnabled bool `json:"webrtc_enabled"`
WebRTCSignalingAddress string `json:"webrtc_signaling_address"`
Env string `json:"env"`
Host string `json:"host"`
StaticHost string `json:"static_host"`
SupportedAuthTypes []string `json:"supported_auth_types"`
AuthEntity string `json:"auth_entity"`
BakedAuth map[string]interface{} `json:"baked_auth"`
}
// ServeHTTP serves the UI.
func (app *robotWebApp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
if true {
err := app.Init()
if err != nil {
app.logger.Debugf("couldn't reload template: %s", err)
return
}
}
var data AppTemplateData
data.StaticHost = app.options.StaticHost
if err := r.ParseForm(); err != nil {
app.logger.Debugw("failed to parse form", "error", err)
}
if os.Getenv("ENV") == "development" {
data.Env = "development"
} else {
data.Env = "production"
}
data.Host = app.options.FQDN
if app.options.WebRTC && r.Form.Get("grpc") != "true" {
data.WebRTCEnabled = true
}
if app.options.Managed && hasManagedAuthHandlers(app.options.Auth.Handlers) {
data.BakedAuth = map[string]interface{}{
"authEntity": app.options.BakedAuthEntity,
"creds": app.options.BakedAuthCreds,
}
} else {
for _, handler := range app.options.Auth.Handlers {
data.SupportedAuthTypes = append(data.SupportedAuthTypes, string(handler.Type))
}
}
err := app.template.Execute(w, data)
if err != nil {
app.logger.Debugf("couldn't execute web page: %s", err)
}
}
// Two known auth handlers (LocationSecret, WebOauth).
func hasManagedAuthHandlers(handlers []config.AuthHandlerConfig) bool {
hasLocationSecretHandler := false
for _, h := range handlers {
if h.Type == rutils.CredentialsTypeRobotLocationSecret {
hasLocationSecretHandler = true
}
}
if len(handlers) == 1 && hasLocationSecretHandler {
return true
}
return false
}
// validSDPTrackName returns a valid SDP video/audio track name as defined in RFC 4566 (https://www.rfc-editor.org/rfc/rfc4566)
// where track names should not include colons.
func validSDPTrackName(name string) string {
return strings.ReplaceAll(name, ":", "+")
}
// refreshVideoSources checks and initializes every possible video source that could be viewed from the robot.
func (svc *webService) refreshVideoSources() {
for _, name := range camera.NamesFromRobot(svc.r) {
cam, err := camera.FromRobot(svc.r, name)
if err != nil {
continue
}
existing, ok := svc.videoSources[validSDPTrackName(name)]
if ok {
existing.Swap(cam)
continue
}
newSwapper := gostream.NewHotSwappableVideoSource(cam)
svc.videoSources[validSDPTrackName(name)] = newSwapper
}
}
// refreshAudioSources checks and initializes every possible audio source that could be viewed from the robot.
func (svc *webService) refreshAudioSources() {
for _, name := range audioinput.NamesFromRobot(svc.r) {
input, err := audioinput.FromRobot(svc.r, name)
if err != nil {
continue
}
existing, ok := svc.audioSources[validSDPTrackName(name)]
if ok {
existing.Swap(input)
continue
}
newSwapper := gostream.NewHotSwappableAudioSource(input)
svc.audioSources[validSDPTrackName(name)] = newSwapper
}
}
// A Service controls the web server for a robot.
type Service interface {
resource.Resource
// Start starts the web server
Start(context.Context, weboptions.Options) error
// Stop stops the main web service (but leaves module server socket running.)
Stop()
// StartModule starts the module server socket.
StartModule(context.Context) error
// Returns the address and port the web service listens on.
Address() string
// Returns the unix socket path the module server listens on.
ModuleAddress() string
}
// StreamServer manages streams and displays.
type StreamServer struct {
// Server serves streams
Server gostream.StreamServer
// HasStreams is true if service has streams that require a WebRTC connection.
HasStreams bool
}
// New returns a new web service for the given robot.
func New(r robot.Robot, logger golog.Logger, opts ...Option) Service {
var wOpts options
for _, opt := range opts {
opt.apply(&wOpts)
}
webSvc := &webService{
Named: InternalServiceName.AsNamed(),
r: r,
logger: logger,
rpcServer: nil,
streamServer: nil,
services: map[resource.API]resource.APIResourceCollection[resource.Resource]{},
opts: wOpts,
videoSources: map[string]gostream.HotSwappableVideoSource{},
audioSources: map[string]gostream.HotSwappableAudioSource{},
}
return webSvc
}
type webService struct {
resource.Named
mu sync.Mutex
r robot.Robot
rpcServer rpc.Server
modServer rpc.Server
streamServer *StreamServer
services map[resource.API]resource.APIResourceCollection[resource.Resource]
opts options
addr string
modAddr string
logger golog.Logger
cancelCtx context.Context
cancelFunc func()
isRunning bool
activeBackgroundWorkers sync.WaitGroup
videoSources map[string]gostream.HotSwappableVideoSource
audioSources map[string]gostream.HotSwappableAudioSource
}
var internalWebServiceName = resource.NewName(
resource.APINamespaceRDKInternal.WithServiceType("web"),
"builtin",
)
func (svc *webService) Name() resource.Name {
return internalWebServiceName
}
// Start starts the web server, will return an error if server is already up.
func (svc *webService) Start(ctx context.Context, o weboptions.Options) error {
svc.mu.Lock()
defer svc.mu.Unlock()
if svc.isRunning {
return errors.New("web server already started")
}
svc.isRunning = true
cancelCtx, cancelFunc := context.WithCancel(ctx)
svc.cancelCtx = cancelCtx
svc.cancelFunc = cancelFunc
if err := svc.runWeb(svc.cancelCtx, o); err != nil {
if svc.cancelFunc != nil {
svc.cancelFunc()
}
svc.isRunning = false
return err
}
return nil
}
// RunWeb starts the web server on the robot with web options and blocks until we cancel the context.
func RunWeb(ctx context.Context, r robot.LocalRobot, o weboptions.Options, logger golog.Logger) (err error) {
defer func() {
if err != nil {
err = utils.FilterOutError(err, context.Canceled)
if err != nil {
logger.Errorw("error running web", "error", err)
}
}
}()
if err := r.StartWeb(ctx, o); err != nil {
return err
}
<-ctx.Done()
return ctx.Err()
}
// RunWebWithConfig starts the web server on the robot with a robot config and blocks until we cancel the context.
func RunWebWithConfig(ctx context.Context, r robot.LocalRobot, cfg *config.Config, logger golog.Logger) error {
o, err := weboptions.FromConfig(cfg)
if err != nil {
return err
}
return RunWeb(ctx, r, o, logger)
}
// Address returns the address the service is listening on.
func (svc *webService) Address() string {
svc.mu.Lock()
defer svc.mu.Unlock()
return svc.addr
}
// ModuleAddress returns the unix socket path the module server is listening on.
func (svc *webService) ModuleAddress() string {
svc.mu.Lock()
defer svc.mu.Unlock()
return svc.modAddr
}
// StartModule starts the grpc module server.
func (svc *webService) StartModule(ctx context.Context) error {
svc.mu.Lock()
defer svc.mu.Unlock()
if svc.modServer != nil {
return errors.New("module service already started")
}
var lis net.Listener
var addr string
if err := module.MakeSelfOwnedFilesFunc(func() error {
dir, err := os.MkdirTemp("", "viam-module-*")
if err != nil {
return errors.WithMessage(err, "module startup failed")
}
addr = filepath.ToSlash(filepath.Join(dir, "parent.sock"))
if runtime.GOOS == "windows" {
// on windows, we need to craft a good enough looking URL for gRPC which
// means we need to take out the volume which will have the current drive
// be used. In a client server relationship for windows dialing, this must
// be known. That is, if this is a multi process UDS, then for the purposes
// of dialing without any resolver modifications to gRPC, they must initially
// agree on using the same drive.
addr = addr[2:]
}
if err := module.CheckSocketAddressLength(addr); err != nil {
return err
}
svc.modAddr = addr
lis, err = net.Listen("unix", addr)
if err != nil {
return errors.WithMessage(err, "failed to listen")
}
return nil
}); err != nil {
return err
}
var (
unaryInterceptors []googlegrpc.UnaryServerInterceptor
streamInterceptors []googlegrpc.StreamServerInterceptor
)
unaryInterceptors = append(unaryInterceptors, ensureTimeoutUnaryInterceptor)
opManager := svc.r.OperationManager()
unaryInterceptors = append(unaryInterceptors, opManager.UnaryServerInterceptor)
streamInterceptors = append(streamInterceptors, opManager.StreamServerInterceptor)
// TODO(PRODUCT-343): Add session manager interceptors
svc.modServer = module.NewServer(unaryInterceptors, streamInterceptors)
if err := svc.modServer.RegisterServiceServer(ctx, &pb.RobotService_ServiceDesc, grpcserver.New(svc.r)); err != nil {
return err
}
if err := svc.refreshResources(); err != nil {
return err
}
if err := svc.initAPIResourceCollections(ctx, true); err != nil {
return err
}
svc.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer svc.activeBackgroundWorkers.Done()
svc.logger.Debugw("module server listening", "socket path", lis.Addr())
defer utils.UncheckedErrorFunc(func() error { return os.RemoveAll(filepath.Dir(addr)) })
if err := svc.modServer.Serve(lis); err != nil {
svc.logger.Errorw("failed to serve module service", "error", err)
}
})
return nil
}
func (svc *webService) refreshResources() error {
resources := make(map[resource.Name]resource.Resource)
for _, name := range svc.r.ResourceNames() {
resource, err := svc.r.ResourceByName(name)
if err != nil {
continue
}
resources[name] = resource
}
return svc.updateResources(resources)
}
// Update updates the web service when the robot has changed.
func (svc *webService) Reconfigure(ctx context.Context, deps resource.Dependencies, _ resource.Config) error {
svc.mu.Lock()
defer svc.mu.Unlock()
if err := svc.updateResources(deps); err != nil {
return err
}
if !svc.isRunning {
return nil
}
return svc.addNewStreams(svc.cancelCtx)
}
func (svc *webService) updateResources(resources map[resource.Name]resource.Resource) error {
// so group resources by API
groupedResources := make(map[resource.API]map[resource.Name]resource.Resource)
for n, v := range resources {
r, ok := groupedResources[n.API]
if !ok {
r = make(map[resource.Name]resource.Resource)
}
r[n] = v
groupedResources[n.API] = r
}
apiRegs := resource.RegisteredAPIs()
for a, v := range groupedResources {
apiResColl, ok := svc.services[a]
// TODO(RSDK-144): register new service if it doesn't currently exist
if !ok {
reg, ok := apiRegs[a]
var apiResColl resource.APIResourceCollection[resource.Resource]
if ok {
apiResColl = reg.MakeEmptyCollection()
} else {
// Log a warning here to remind users to register their APIs. Do not warn if the resource is internal to the RDK or
// the resource is handled by a remote with a possibly separate API registration. Modular resources will
// have API registrations already and should not reach this point in the method.
if a.Type.Namespace != resource.APINamespaceRDKInternal {
for n := range v {
if !n.ContainsRemoteNames() {
svc.logger.Warnw(
"missing registration for api, resources with this API will be unreachable through a client", "api", n.API)
break
}
}
}
continue
}
if err := apiResColl.ReplaceAll(v); err != nil {
return err
}
svc.services[a] = apiResColl
} else {
if err := apiResColl.ReplaceAll(v); err != nil {
return err
}
}
}
return nil
}
// Stop stops the main web service prior to actually closing (it leaves the module server running.)
func (svc *webService) Stop() {
svc.mu.Lock()
defer svc.mu.Unlock()
if svc.cancelFunc != nil {
svc.cancelFunc()
}
svc.isRunning = false
}
// Close closes a webService via calls to its Cancel func.
func (svc *webService) Close(ctx context.Context) error {
svc.mu.Lock()
defer svc.mu.Unlock()
var err error
if svc.cancelFunc != nil {
svc.cancelFunc()
}
svc.isRunning = false
if svc.modServer != nil {
err = svc.modServer.Stop()
}
svc.activeBackgroundWorkers.Wait()
return err
}
func (svc *webService) streamInitialized() bool {
return svc.streamServer != nil && svc.streamServer.Server != nil
}
func (svc *webService) addNewStreams(ctx context.Context) error {
if !svc.streamInitialized() {
return nil
}
svc.refreshVideoSources()
svc.refreshAudioSources()
if svc.opts.streamConfig == nil {
if len(svc.videoSources) != 0 || len(svc.audioSources) != 0 {
svc.logger.Debug("not starting streams due to no stream config being set")
}
return nil
}
newStream := func(name string) (gostream.Stream, bool, error) {
// Configure new stream
config := *svc.opts.streamConfig
config.Name = name
stream, err := svc.streamServer.Server.NewStream(config)
// Skip if stream is already registered, otherwise raise any other errors
var registeredError *gostream.StreamAlreadyRegisteredError
if errors.As(err, ®isteredError) {
return nil, true, nil
} else if err != nil {
return nil, false, err
}
if !svc.streamServer.HasStreams {
svc.streamServer.HasStreams = true
}
return stream, false, nil
}
for name, source := range svc.videoSources {
stream, alreadyRegistered, err := newStream(name)
if err != nil {
return err
} else if alreadyRegistered {
continue
}
svc.startVideoStream(ctx, source, stream)
}
for name, source := range svc.audioSources {
stream, alreadyRegistered, err := newStream(name)
if err != nil {
return err
} else if alreadyRegistered {
continue
}
svc.startAudioStream(ctx, source, stream)
}
return nil
}
func (svc *webService) makeStreamServer(ctx context.Context) (*StreamServer, error) {
svc.refreshVideoSources()
svc.refreshAudioSources()
var streams []gostream.Stream
var streamTypes []bool
if svc.opts.streamConfig == nil || (len(svc.videoSources) == 0 && len(svc.audioSources) == 0) {
if len(svc.videoSources) != 0 || len(svc.audioSources) != 0 {
svc.logger.Debug("not starting streams due to no stream config being set")
}
noopServer, err := gostream.NewStreamServer(streams...)
return &StreamServer{noopServer, false}, err
}
addStream := func(streams []gostream.Stream, name string, isVideo bool) ([]gostream.Stream, error) {
config := *svc.opts.streamConfig
config.Name = name
if isVideo {
config.AudioEncoderFactory = nil
// set TargetFrameRate to the framerate of the video source if available
props, err := svc.videoSources[name].MediaProperties(ctx)
if err != nil {
svc.logger.Warnw("failed to get video source properties", "name", name, "error", err)
} else if props.FrameRate > 0.0 {
// round float up to nearest int
config.TargetFrameRate = int(math.Ceil(float64(props.FrameRate)))
}
// default to 60fps if the video source doesn't have a framerate
if config.TargetFrameRate == 0 {
config.TargetFrameRate = 60
}
if runtime.GOOS == "windows" {
// TODO(RSDK-1771): support video on windows
svc.logger.Warnw("not starting video stream since not supported on Windows yet", "name", name)
return streams, nil
}
} else {
config.VideoEncoderFactory = nil
}
stream, err := gostream.NewStream(config)
if err != nil {
return streams, err
}
return append(streams, stream), nil
}
for name := range svc.videoSources {
var err error
streams, err = addStream(streams, name, true)
if err != nil {
return nil, err
}
streamTypes = append(streamTypes, true)
}
for name := range svc.audioSources {
var err error
streams, err = addStream(streams, name, false)
if err != nil {
return nil, err
}
streamTypes = append(streamTypes, false)
}
streamServer, err := gostream.NewStreamServer(streams...)
if err != nil {
return nil, err
}
for idx, stream := range streams {
if streamTypes[idx] {
svc.startVideoStream(ctx, svc.videoSources[stream.Name()], stream)
} else {
svc.startAudioStream(ctx, svc.audioSources[stream.Name()], stream)
}
}
return &StreamServer{streamServer, true}, nil
}
func (svc *webService) startStream(streamFunc func(opts *webstream.BackoffTuningOptions) error) {
waitCh := make(chan struct{})
svc.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer svc.activeBackgroundWorkers.Done()
close(waitCh)
opts := &webstream.BackoffTuningOptions{
BaseSleep: 50 * time.Microsecond,
MaxSleep: 2 * time.Second,
Cooldown: 5 * time.Second,
}
if err := streamFunc(opts); err != nil {
if utils.FilterOutError(err, context.Canceled) != nil {
svc.logger.Errorw("error streaming", "error", err)
}
}
})
<-waitCh
}
func (svc *webService) startVideoStream(ctx context.Context, source gostream.VideoSource, stream gostream.Stream) {
// Honor ctx that may be coming from a Reconfigure.
ctxWithJPEGHint := gostream.WithMIMETypeHint(ctx, rutils.WithLazyMIMEType(rutils.MimeTypeJPEG))
svc.startStream(func(opts *webstream.BackoffTuningOptions) error {
streamVideoCtx, _ := utils.MergeContext(svc.cancelCtx, ctxWithJPEGHint)
return webstream.StreamVideoSource(streamVideoCtx, source, stream, opts, svc.logger)
})
}
func (svc *webService) startAudioStream(ctx context.Context, source gostream.AudioSource, stream gostream.Stream) {
svc.startStream(func(opts *webstream.BackoffTuningOptions) error {
// Merge ctx that may be coming from a Reconfigure.
streamAudioCtx, _ := utils.MergeContext(svc.cancelCtx, ctx)
return webstream.StreamAudioSource(streamAudioCtx, source, stream, opts, svc.logger)
})
}
// installWeb prepares the given mux to be able to serve the UI for the robot.
func (svc *webService) installWeb(mux *goji.Mux, theRobot robot.Robot, options weboptions.Options) error {
app := &robotWebApp{theRobot: theRobot, logger: svc.logger, options: options}
if err := app.Init(); err != nil {
return err
}
var staticDir http.FileSystem
if app.options.SharedDir != "" {
staticDir = http.Dir(app.options.SharedDir + "/static")
} else {
embedFS, err := fs.Sub(web.AppFS, "runtime-shared/static")
if err != nil {
return err
}
matches, err := fs.Glob(embedFS, "*.js")
if err != nil {
return err
}
if len(matches) == 0 {
svc.logger.Warnw("Couldn't find any static files when running RDK. Make sure to run 'make build-web' - using staticrc.viam.com")
app.options.StaticHost = "https://staticrc.viam.com"
}
staticDir = http.FS(embedFS)
}
mux.Handle(pat.Get("/static/*"), gziphandler.GzipHandler(http.StripPrefix("/static", http.FileServer(staticDir))))
mux.Handle(pat.New("/"), app)
return nil
}
// runWeb takes the given robot and options and runs the web server. This function will
// block until the context is done.
func (svc *webService) runWeb(ctx context.Context, options weboptions.Options) (err error) {
if options.Network.BindAddress != "" && options.Network.Listener != nil {
return errors.New("may only set one of network bind address or listener")
}
listener := options.Network.Listener
if listener == nil {
listener, err = net.Listen("tcp", options.Network.BindAddress)
if err != nil {
return err
}
}
listenerTCPAddr, ok := listener.Addr().(*net.TCPAddr)
if !ok {
return errors.Errorf("expected *net.TCPAddr but got %T", listener.Addr())
}
options.Secure = options.Network.TLSConfig != nil || options.Network.TLSCertFile != ""
if options.SignalingAddress == "" && !options.Secure {
options.SignalingDialOpts = append(options.SignalingDialOpts, rpc.WithInsecure())
}
svc.addr = listenerTCPAddr.String()
if options.FQDN == "" {
options.FQDN, err = rpc.InstanceNameFromAddress(svc.addr)
if err != nil {
return err
}
}
rpcOpts, err := svc.initRPCOptions(listenerTCPAddr, options)
if err != nil {
return err
}
svc.rpcServer, err = rpc.NewServer(svc.logger, rpcOpts...)
if err != nil {
return err
}
if options.SignalingAddress == "" {
options.SignalingAddress = svc.addr
}
if err := svc.rpcServer.RegisterServiceServer(
ctx,
&pb.RobotService_ServiceDesc,
grpcserver.New(svc.r),
pb.RegisterRobotServiceHandlerFromEndpoint,
); err != nil {
return err
}
if err := svc.refreshResources(); err != nil {
return err
}
if err := svc.initAPIResourceCollections(ctx, false); err != nil {
return err
}
svc.streamServer, err = svc.makeStreamServer(ctx)
if err != nil {
return err
}
if err := svc.rpcServer.RegisterServiceServer(
ctx,
&streampb.StreamService_ServiceDesc,
svc.streamServer.Server.ServiceServer(),
streampb.RegisterStreamServiceHandlerFromEndpoint,
); err != nil {
return err
}
if svc.streamServer.HasStreams {
// force WebRTC template rendering
options.WebRTC = true
}
if options.Debug {
if err := svc.rpcServer.RegisterServiceServer(
ctx,
&echopb.EchoService_ServiceDesc,
&echoserver.Server{},
echopb.RegisterEchoServiceHandlerFromEndpoint,
); err != nil {
return err
}
}
httpServer, err := svc.initHTTPServer(listenerTCPAddr, options)
if err != nil {
return err
}
// Serve
svc.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer svc.activeBackgroundWorkers.Done()
<-ctx.Done()
defer func() {
if err := httpServer.Shutdown(context.Background()); err != nil {
svc.logger.Errorw("error shutting down", "error", err)
}
}()
defer func() {
if err := svc.rpcServer.Stop(); err != nil {
svc.logger.Errorw("error stopping rpc server", "error", err)
}
}()
if svc.streamServer.Server != nil {
if err := svc.streamServer.Server.Close(); err != nil {
svc.logger.Errorw("error closing stream server", "error", err)
}
}
})
svc.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer svc.activeBackgroundWorkers.Done()
if err := svc.rpcServer.Start(); err != nil {
svc.logger.Errorw("error starting rpc server", "error", err)
}
})
var scheme string
if options.Secure {
scheme = "https"
} else {
scheme = "http"
}
if strings.HasPrefix(svc.addr, "[::]") {
svc.addr = fmt.Sprintf("0.0.0.0:%d", listenerTCPAddr.Port)
}
listenerURL := fmt.Sprintf("%s://%s", scheme, svc.addr)
var urlFields []interface{}
if options.LocalFQDN == "" {
urlFields = append(urlFields, "url", listenerURL)
} else {
localURL := fmt.Sprintf("%s://%s:%d", scheme, options.LocalFQDN, listenerTCPAddr.Port)
urlFields = append(urlFields, "url", localURL, "alt_url", listenerURL)
}
svc.logger.Infow("serving", urlFields...)
svc.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer svc.activeBackgroundWorkers.Done()
var serveErr error
if options.Secure {
serveErr = httpServer.ServeTLS(listener, options.Network.TLSCertFile, options.Network.TLSKeyFile)
} else {
serveErr = httpServer.Serve(listener)
}
if serveErr != nil && !errors.Is(serveErr, http.ErrServerClosed) {
svc.logger.Errorw("error serving http", "error", serveErr)
}
})
return err
}
// Initialize RPC Server options.
func (svc *webService) initRPCOptions(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
hosts := options.GetHosts(listenerTCPAddr)
rpcOpts := []rpc.ServerOption{
rpc.WithAuthIssuer(options.FQDN),
rpc.WithAuthAudience(options.FQDN),
rpc.WithInstanceNames(hosts.Names...),
rpc.WithWebRTCServerOptions(rpc.WebRTCServerOptions{
Enable: true,
EnableInternalSignaling: true,
ExternalSignalingDialOpts: options.SignalingDialOpts,
ExternalSignalingAddress: options.SignalingAddress,
ExternalSignalingHosts: hosts.External,
InternalSignalingHosts: hosts.Internal,
Config: &grpc.DefaultWebRTCConfiguration,
OnPeerAdded: options.WebRTCOnPeerAdded,
OnPeerRemoved: options.WebRTCOnPeerRemoved,
}),
}
if options.DisableMulticastDNS {
rpcOpts = append(rpcOpts, rpc.WithDisableMulticastDNS())
}
var unaryInterceptors []googlegrpc.UnaryServerInterceptor
unaryInterceptors = append(unaryInterceptors, ensureTimeoutUnaryInterceptor)
if options.Debug {
rpcOpts = append(rpcOpts, rpc.WithDebug())
unaryInterceptors = append(unaryInterceptors, func(
ctx context.Context,
req interface{},
info *googlegrpc.UnaryServerInfo,
handler googlegrpc.UnaryHandler,
) (interface{}, error) {
ctx, span := trace.StartSpan(ctx, fmt.Sprintf("%v", req))
defer span.End()
return handler(ctx, req)
})
}
if options.Network.TLSConfig != nil {
rpcOpts = append(rpcOpts, rpc.WithInternalTLSConfig(options.Network.TLSConfig))
}
authOpts, err := svc.initAuthHandlers(listenerTCPAddr, options)
if err != nil {
return nil, err
}
rpcOpts = append(rpcOpts, authOpts...)
var streamInterceptors []googlegrpc.StreamServerInterceptor
opManager := svc.r.OperationManager()
sessManagerInts := svc.r.SessionManager().ServerInterceptors()
if sessManagerInts.UnaryServerInterceptor != nil {
unaryInterceptors = append(unaryInterceptors, sessManagerInts.UnaryServerInterceptor)
}
unaryInterceptors = append(unaryInterceptors, opManager.UnaryServerInterceptor)
if sessManagerInts.StreamServerInterceptor != nil {
streamInterceptors = append(streamInterceptors, sessManagerInts.StreamServerInterceptor)
}
streamInterceptors = append(streamInterceptors, opManager.StreamServerInterceptor)
rpcOpts = append(
rpcOpts,
rpc.WithUnknownServiceHandler(svc.foreignServiceHandler),
)
unaryInterceptor := grpc_middleware.ChainUnaryServer(unaryInterceptors...)
streamInterceptor := grpc_middleware.ChainStreamServer(streamInterceptors...)
rpcOpts = append(rpcOpts,
rpc.WithUnaryServerInterceptor(unaryInterceptor),
rpc.WithStreamServerInterceptor(streamInterceptor),
)
return rpcOpts, nil
}
// Initialize authentication handler options.
func (svc *webService) initAuthHandlers(listenerTCPAddr *net.TCPAddr, options weboptions.Options) ([]rpc.ServerOption, error) {
rpcOpts := []rpc.ServerOption{}
if options.Managed && len(options.Auth.Handlers) == 1 {
if options.BakedAuthEntity == "" || options.BakedAuthCreds.Type == "" {
return nil, errors.New("expected baked in local UI credentials since managed")
}
}