forked from n0rt0nthec4t/Nest_accfactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexusstreamer.js
996 lines (863 loc) · 48 KB
/
nexusstreamer.js
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
// Code taken from https://github.com/Brandawg93/homebridge-nest-cam
// all credit for this due there
//
// Converted back from typescript and combined into single file
// Cleaned up/recoded
//
// Mark Hulskamp
// 2/8/2022
//
// done
// -- switching camera stream on/off - going from off to on doesn't restart stream from Nest
// -- buffering of stream ie: const streaming from nexus
// -- routing of data to multiple connected ffmpeg processing streams. Allows single connection to nexus for the source stream
// -- restart connection when dropped if buffering
// -- support both Nest and Google accounts
// -- Modification to sending buffer before recording starts. Should result in cleaner ffmpeg process output and more reliable HKSV recordings
// -- further fixes for restarting streams
// -- fixed switching camera off/offline image frames to streams
// -- fixes in buffering code. Will now correctly output requested buffer to multiple streams
// -- refactor class definition
// -- Changes to buffering timestamping
// -- get snapshot image from buffer if active
// -- re-connection fixes when playback has ended with error
//
// todo
// -- When camera goes offline, we don't get notified straight away and video stream stops. Perhaps timer to go to camera off image if no data receieve in past 15 seconds?
// -- When first called after starting, get a green screen for about 1 second. Everything is fine after that <- not seen in ages
// **Think know what this issue is. When outputting a new stream, need to align to H264 SPS frame
// -- audio echo with return audio
// -- speed up live image stream starting when have a buffer active. Should almost start straight away
// -- dynamic audio switching on/off from camera
"use strict";
// Define external lbrary requirements
var protoBuf = require("pbf"); // Proto buffer
// Define nodejs module requirements
var util = require("util");
var fs = require("fs");
var tls = require("tls");
var EventEmitter = require("events");
var {spawn} = require("child_process");
// Define constants
const DEFAULTBUFFERTIME = 15000; // Default time in milliseconds to hold in buffer
const PINGINTERVAL = 15000; // 15 seconds between each ping to nexus server while stream active
const TIMERINTERVAL = 1000; // 1 second
const CAMERAOFFLINEH264FILE = "Nest_camera_offline.h264"; // Camera offline H264 frame file
const CAMERAOFFH264FILE = "Nest_camera_off.h264"; // Camera off H264 frame file
const CAMERACONNECTING264FILE = "Nest_camera_connecting.h264"; // Camera connecting H264 frame file
const CodecType = {
SPEEX : 0,
PCM_S16_LE : 1,
H264 : 2,
AAC : 3,
OPUS : 4,
META : 5,
DIRECTORS_CUT : 6,
};
const StreamProfile = {
AVPROFILE_MOBILE_1 : 1,
AVPROFILE_HD_MAIN_1 : 2,
AUDIO_AAC : 3,
AUDIO_SPEEX : 4,
AUDIO_OPUS : 5,
VIDEO_H264_50KBIT_L12 : 6,
VIDEO_H264_530KBIT_L31 : 7,
VIDEO_H264_100KBIT_L30 : 8,
VIDEO_H264_2MBIT_L40 : 9,
VIDEO_H264_50KBIT_L12_THUMBNAIL : 10,
META : 11,
DIRECTORS_CUT : 12,
AUDIO_OPUS_LIVE : 13,
VIDEO_H264_L31 : 14,
VIDEO_H264_L40 : 15
};
const ErrorCode = {
ERROR_CAMERA_NOT_CONNECTED : 1,
ERROR_ILLEGAL_PACKET : 2,
ERROR_AUTHORIZATION_FAILED : 3,
ERROR_NO_TRANSCODER_AVAILABLE : 4,
ERROR_TRANSCODE_PROXY_ERROR : 5,
ERROR_INTERNAL : 6,
};
const Reason = {
ERROR_TIME_NOT_AVAILABLE : 1,
ERROR_PROFILE_NOT_AVAILABLE : 2,
ERROR_TRANSCODE_NOT_AVAILABLE : 3,
ERROR_UKNOWN1 : 4, // Redirect???
PLAY_END_SESSION_COMPLETE : 128,
};
const PacketType = {
PING : 1,
HELLO : 100,
PING_CAMERA : 101,
AUDIO_PAYLOAD : 102,
START_PLAYBACK : 103,
STOP_PLAYBACK : 104,
CLOCK_SYNC_ECHO : 105,
LATENCY_MEASURE : 106,
TALKBACK_LATENCY : 107,
METADATA_REQUEST : 108,
OK : 200,
ERROR : 201,
PLAYBACK_BEGIN : 202,
PLAYBACK_END : 203,
PLAYBACK_PACKET : 204,
LONG_PLAYBACK_PACKET : 205,
CLOCK_SYNC : 206,
REDIRECT : 207,
TALKBACK_BEGIN : 208,
TALKBACK_END : 209,
METADATA : 210,
METADATA_ERROR : 211,
AUTHORIZE_REQUEST : 212,
};
const ProtocolVersion = {
VERSION_1 : 1,
VERSION_2 : 2,
VERSION_3 : 3
};
const ClientType = {
ANDROID : 1,
IOS : 2,
WEB : 3
};
const H264FrameTypes = {
STAP_A : 24,
FU_A : 28,
NON_IDR : 1,
IDR : 5,
SEI : 6,
SPS : 7,
PPS : 8,
AUD : 9
};
const H264NALUnit = Buffer.from([0x00, 0x00, 0x00, 0x01]);
// Blank audio in AAC format, mono channel @48000
const AACMONO48000BLANK = Buffer.from([
0xFF, 0xF1, 0x4C, 0x40, 0x03, 0x9F, 0xFC, 0xDE, 0x02, 0x00, 0x4C, 0x61,
0x76, 0x63, 0x35, 0x39, 0x2E, 0x31, 0x38, 0x2E, 0x31, 0x30, 0x30, 0x00,
0x02, 0x30, 0x40, 0x0E, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01,
0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18,
0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20,
0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07,
0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF,
0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1,
0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C,
0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40,
0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01,
0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F,
0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC,
0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01,
0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18,
0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20,
0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07,
0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF,
0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1,
0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C,
0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40,
0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01,
0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F,
0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC,
0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01,
0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18,
0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20,
0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07,
0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF,
0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1,
0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C,
0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40,
0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01,
0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F,
0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC,
0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01,
0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18,
0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20,
0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07,
0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF,
0xF1, 0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1,
0x4C, 0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C,
0x40, 0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40,
0x01, 0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01,
0x7F, 0xFC, 0x01, 0x18, 0x20, 0x07, 0xFF, 0xF1, 0x4C, 0x40, 0x01, 0x7F,
0xFC, 0x01, 0x18, 0x20, 0x07
]);
// NeuxsStreamer object
class NexusStreamer {
constructor(HomeKitAccessoryUUID, cameraToken, tokenType, deviceData, enableDebugging) {
this.camera = deviceData; // Current camera data
this.buffer = {active: false, size: 0, buffer: [], streams: []}; // Buffer and stream details
this.tcpSocket = null;
this.host = null; // No intial host to connect to
this.pendingHost = null;
this.nexusvideo = {channel_id: -1, start_time: 0, sample_rate: 0, packet_time: 0};
this.nexusaudio = {channel_id: -1, start_time: 0, sample_rate: 0, packet_time: 0};
this.pendingMessages = [];
this.pendingBuffer = null;
this.authorised = false;
this.weDidClose = true; // Flag if we did teh socket close gracefully
this.timer = null; // Internal timer handle
this.pingtimer = null; // Ping timer handle
this.sessionID = null; // no session ID yet.. We'll assign a random one when we connect to the nexus stream
this.HomeKitAccessoryUUID = HomeKitAccessoryUUID; // HomeKit accessory UUID
// Get access token and set token type
this.cameraToken = cameraToken;
this.tokenType = tokenType;
this.playingBack = false; // If we're playing back nexus data
this.talking = false; // If "talk" is happening
this.enableDebugging = typeof enableDebugging == "boolean" ? enableDebugging : false; // debug status
// buffer for camera offline image in .h264 frame
this.camera_offline_h264_frame = null;
if (fs.existsSync(__dirname + "/" + CAMERAOFFLINEH264FILE)) {
this.camera_offline_h264_frame = fs.readFileSync(__dirname + "/" + CAMERAOFFLINEH264FILE);
// remove any H264 NALU from beginning of any video data. We do this as they are added later when output by our ffmpeg router
if (this.camera_offline_h264_frame.indexOf(H264NALUnit) == 0) {
this.camera_offline_h264_frame = this.camera_offline_h264_frame.slice(H264NALUnit.length);
}
}
// buffer for camera stream off image in .h264 frame
this.camera_off_h264_frame = null;
if (fs.existsSync(__dirname + "/" + CAMERAOFFH264FILE)) {
this.camera_off_h264_frame = fs.readFileSync(__dirname + "/" + CAMERAOFFH264FILE);
// remove any H264 NALU from beginning of any video data. We do this as they are added later when output by our ffmpeg router
if (this.camera_off_h264_frame.indexOf(H264NALUnit) == 0) {
this.camera_off_h264_frame = this.camera_off_h264_frame.slice(H264NALUnit.length);
}
}
// buffer for camera stream connecting image in .h264 frame
this.camera_connecting_h264_frame = null;
if (fs.existsSync(__dirname + "/" + CAMERACONNECTING264FILE)) {
this.camera_connecting_h264_frame = fs.readFileSync(__dirname + "/" + CAMERACONNECTING264FILE);
// remove any H264 NALU from beginning of any video data. We do this as they are added later when output by our ffmpeg router
if (this.camera_connecting_h264_frame.indexOf(H264NALUnit) == 0) {
this.camera_connecting_h264_frame = this.camera_connecting_h264_frame.slice(H264NALUnit.length);
}
}
}
// Class functions
startBuffering(bufferingTimeMilliseconds) {
// We only support one buffering stream per Nexus object ie: per camera
if (typeof bufferingTimeMilliseconds == "undefined") {
bufferingTimeMilliseconds = DEFAULTBUFFERTIME; // Wasnt specified how much streaming time we hold in our buffer, so default to 15 seconds
}
this.buffer.maxTime = bufferingTimeMilliseconds;
this.buffer.active = (bufferingTimeMilliseconds > 0 ? true : false); // Start the buffer if buffering size > 0
this.buffer.buffer = []; // empty buffer
if (this.tcpSocket == null) {
this.#connect(this.camera.direct_nexustalk_host);
}
this.#outputLogging("Nest", true, "Started buffering from '%s' with size of '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host), bufferingTimeMilliseconds);
}
startLiveStream(sessionID, videoStream, audioStream, alignToSPSFrame) {
// Setup error catching for video/audio streams
videoStream && videoStream.on("error", (error) => {
// EPIPE errors??
});
audioStream && audioStream.on("error", (error) => {
// EPIPE errors??
});
if (this.buffer.active == false && this.tcpSocket == null) {
// We not doing any buffering and there isnt an active socket connection, so startup connection to nexus
this.#connect(this.camera.direct_nexustalk_host);
}
// Add video/audio streams for our ffmpeg router to handle outputting to
this.buffer.streams.push({type: "live", id: sessionID, video: videoStream, audio: audioStream, aligned: (typeof alignToSPSFrame == "undefined" || alignToSPSFrame == true ? false : true)});
// finally, we've started live stream
this.#outputLogging("Nest", true, "Started live stream from '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host));
}
startRecordStream(sessionID, ffmpegRecord, videoStream, audioStream, alignToSPSFrame, fromTime) {
// Setup error catching for video/audio streams
videoStream && videoStream.on("error", (error) => {
// EPIPE errors??
});
audioStream && audioStream.on("error", (error) => {
// EPIPE errors??
});
if (this.buffer.active == false && this.tcpSocket == null) {
// We not doing any buffering and/or there isnt an active socket connection, so startup connection to nexus
this.#connect(this.camera.direct_nexustalk_host);
}
// Output from the requested time position in the buffer until one index before the end of buffer
if (this.buffer.active == true) {
var sentElements = 0;
var doneAlign = (typeof alignToSPSFrame == "undefined" || alignToSPSFrame == true ? false : true);
for (var bufferIndex = 0; bufferIndex < this.buffer.buffer.length; bufferIndex++) {
if (fromTime == 0 || (fromTime != 0 && this.buffer.buffer[bufferIndex].synctime >= fromTime)) {
if (doneAlign == false && this.buffer.buffer[bufferIndex].type == "video" && (this.buffer.buffer[bufferIndex].data && this.buffer.buffer[bufferIndex].data[0] & 0x1f) == H264FrameTypes.SPS) {
doneAlign = true;
}
if (doneAlign == true) {
// This is a recording streaming stream, and we have been initally aligned to a h264 SPS frame, so send on data now
if (this.buffer.buffer[bufferIndex].type == "video" && videoStream != null) {
// H264 NAL Units "0001" are required to be added to beginning of any video data we output
videoStream.write(Buffer.concat([H264NALUnit, this.buffer.buffer[bufferIndex].data]));
}
if (this.buffer.buffer[bufferIndex].type == "audio" && audioStream != null) {
audioStream.write(this.buffer.buffer[bufferIndex].data);
}
sentElements++; // Increment the number of elements we output from the buffer
}
}
}
this.#outputLogging("Nest", true, "Recording stream '%s' requested buffered data first. Sent '%s' buffered elements", sessionID, sentElements);
}
// Add video/audio streams for our ffmpeg router to handle outputting to
this.buffer.streams.push({type: "record", id: sessionID, record: ffmpegRecord, video: videoStream, audio: audioStream, aligned: doneAlign});
// Finally we've started the recording stream
this.#outputLogging("Nest", true, "Started recording stream from '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host));
}
startTalkStream(sessionID, talkbackStream) {
// Setup talkback audio stream if configured
if (talkbackStream == null) {
return;
}
var index = this.buffer.streams.findIndex(({ id }) => id == sessionID);
if (index != -1) {
this.buffer.streams[index].audioTimeout = null; // NO timeout
talkbackStream.on("error", (error) => {
// EPIPE errors??
});
talkbackStream.on("data", (data) => {
// Received audio data to send onto nexus for output to doorbell/camera
this.#AudioPayload(data);
clearTimeout(this.buffer.streams[index].audioTimeout); // Clear return audio timeout
this.buffer.streams[index].audioTimeout = setTimeout(() => {
// no audio received in 500ms, so mark end of stream
this.#AudioPayload(Buffer.from([]));
}, 500);
});
}
}
stopTalkStream(sessionID) {
var index = this.buffer.streams.findIndex(({ type, id }) => id == sessionID);
if (index != -1) {
this.buffer.streams[index].timeout && clearTimeout(this.buffer.streams[index].timeout); // Clear any active return audio timer
}
}
stopRecordStream(sessionID) {
// Request to stop a recording stream
var index = this.buffer.streams.findIndex(({ type, id }) => type == "record" && id == sessionID);
if (index != -1) {
this.#outputLogging("Nest", true, "Stopped recording stream from '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host));
this.buffer.streams.splice(index, 1); // remove this object
}
// If we have no more streams active, we'll close the socket to nexus
if (this.buffer.streams.length == 0 && this.buffer.active == false) {
clearInterval(this.timer);
this.#close(true);
}
}
stopLiveStream(sessionID) {
// Request to stop an active live stream
var index = this.buffer.streams.findIndex(({ type, id }) => type == "live" && id == sessionID);
if (index != -1) {
this.#outputLogging("Nest", true, "Stopped live stream from '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host));
this.buffer.streams[index].timeout && clearTimeout(this.buffer.streams[index].timeout); // Clear any active return audio timer
this.buffer.streams.splice(index, 1); // remove this object
}
// If we have no more streams active, we'll close the socket to nexus
if (this.buffer.streams.length == 0 && this.buffer.active == false) {
clearInterval(this.timer);
this.#close(true);
}
}
stopBuffering() {
if (this.buffer.active == true) {
// we have a buffer session, so close it down
this.#outputLogging("Nest", true, "Stopped buffering from '%s'", (this.host == null ? this.camera.direct_nexustalk_host : this.host));
this.buffer.buffer = null; // Clean up first
this.buffer.active = false; // No buffer running now
}
// If we have no more streams active, we'll close the socket to nexus
if (this.buffer.streams.length == 0) {
clearInterval(this.timer);
this.#close(true);
}
}
update(cameraToken, tokenType, updatedDeviceData) {
if (typeof updatedDeviceData != "object") {
return;
}
if (cameraToken != this.cameraToken || tokenType != this.tokenType) {
// access token has changed and/or token type has changed, so re-authorise
this.tokenType = tokenType; // Update token type
this.cameraToken = cameraToken; // Update token
this.#Authenticate(true); // Update authorisation only
}
if ((this.camera.online != updatedDeviceData.online) || (this.camera.streaming_enabled != updatedDeviceData.streaming_enabled)) {
// Online status or streaming status has changed has changed
this.camera.online = updatedDeviceData.online;
this.camera.streaming_enabled = updatedDeviceData.streaming_enabled;
this.camera.direct_nexustalk_host = updatedDeviceData.direct_nexustalk_host
if (this.camera.online == false || this.camera.streaming_enabled == false) {
this.#close(true); // as offline or streaming not enabled, close socket
}
if ((this.camera.online == true && this.camera.streaming_enabled == true) && (this.tcpSocket == null && (this.buffer.active == true || this.buffer.streams.length > 0))) {
this.#connect(this.camera.direct_nexustalk_host); // Connect to Nexus for stream
}
}
if (this.camera.direct_nexustalk_host != updatedDeviceData.direct_nexustalk_host) {
this.#outputLogging("Nest", true, "Updated Nexusstreamer host '%s'", updatedDeviceData.direct_nexustalk_host);
this.pendingHost = updatedDeviceData.direct_nexustalk_host;
}
this.camera = updatedDeviceData; // Update our internally stored copy of the camera details
}
async getBufferSnapshot(pathToFFMPEG) {
if (this.buffer.active == false) {
return Buffer.alloc(0); // Empty buffer;
};
// Setup our ffmpeg process for conversion of h264 image frame to jpg image
var imageSnapshot = Buffer.alloc(0); // Empty buffer
var commandLine = "-hide_banner -f h264 -i pipe:0 -vframes 1 -f image2pipe pipe:1";
var ffmpegProcess = spawn(pathToFFMPEG || "ffmpeg", commandLine.split(" "), { env: process.env });
ffmpegProcess.stdout.on("data", (data) => {
imageSnapshot = Buffer.concat([imageSnapshot, data]); // Append image data to return buffer
});
var done = false;
for (var index = this.buffer.buffer.length - 1; index >= 0 && done == false; index--) {
if (this.buffer.buffer[index].type == "video" && this.buffer.buffer[index].data[0] && ((this.buffer.buffer[index].data[0] & 0x1f) == H264FrameTypes.SPS) == true) {
// Found last H264 SPS frame from end of buffer
// The buffer should now have a buffer sequence of SPS, PPS and IDR
// Maybe need to refine to search from this position for the PPS and then from there, to the IDR?
if (index <= this.buffer.buffer.length - 3) {
ffmpegProcess.stdin.write(Buffer.concat([H264NALUnit, this.buffer.buffer[index].data])); // SPS
ffmpegProcess.stdin.write(Buffer.concat([H264NALUnit, this.buffer.buffer[index + 1].data])); // PPS assuming
ffmpegProcess.stdin.write(Buffer.concat([H264NALUnit, this.buffer.buffer[index + 2].data])); // IDR assuming
done = true; // finished outputting to ffmpeg process
}
}
}
ffmpegProcess.stdin.end(); // No more output from our search loop, so mark end to ffmpeg
await EventEmitter.once(ffmpegProcess, "exit"); // Wait until childprocess (ffmpeg) has issued exit event
return imageSnapshot;
}
#connect(host) {
clearInterval(this.pingtimer); // Clear ping timer if was running
this.sessionID = null; // No session ID yet
if (this.camera.streaming_enabled == true && this.camera.online == true) {
if (typeof host == "undefined" || host == null) {
// No host parameter passed in, so we'll set this to our internally stored host
host = this.host;
}
if (this.pendingHost != null) {
host = this.pendingHost;
this.pendingHost = null;
}
this.#outputLogging("Nest", true, "Starting connection to '%s'", host);
this.tcpSocket = tls.connect({host: host, port: 1443}, () => {
// Opened connection to Nexus server, so now need to authenticate ourselves
this.host = host; // update internal host name since we've connected
this.#outputLogging("Nest", true, "Connection established to '%s'", host);
this.tcpSocket.setKeepAlive(true); // Keep socket connection alive
this.#Authenticate(false);
this.pingtimer = setInterval(() => {
// Periodically send PING message to keep stream alive
// Doesnt seem to work???
this.#sendMessage(PacketType.PING, Buffer.alloc(0));
}, PINGINTERVAL);
});
this.tcpSocket.on("error", (error) => {
// Catch any socket errors to avoid code quitting
// Our "close" handler will try reconnecting if needed
//this.#outputLogging("Nest", true, "Stocket error", error);
});
this.tcpSocket.on("end", () => {
//this.#outputLogging("Nest", true, "Stocket ended", this.playingBack);
});
this.tcpSocket.on("data", (data) => {
this.#handleNexusData(data);
});
this.tcpSocket.on("close", (hadError) => {
var normalClose = this.weDidClose; // Cache this, so can reset it below before we take action
clearInterval(this.pingtimer); // Clear ping timer
this.playingBack = false; // Playback ended as socket is closed
this.authorised = false; // Since connection close, we can't be authorised anymore
this.tcpSocket = null; // Clear socket object
this.sessionID = null; // Not an active session anymore
this.weDidClose = false; // Reset closed flag
this.#outputLogging("Nest", true, "Connection closed to '%s'", host);
if (normalClose == false && (this.buffer.active == true || this.buffer.streams.length > 0)) {
// We still have either active buffering occuring or output streams running
// so attempt to restart connection to existing host
this.#connect(host);
}
});
}
// Setup timer for when camera video is off or camera is offline, so loop our appropriate messages to the video stream
clearInterval(this.timer);
this.timer = setInterval(() => {
if (this.camera_offline_h264_frame && this.camera.online == false) {
// Camera is offline, so feed in our custom h264 frame for playback
this.#ffmpegRouter("video", this.camera_offline_h264_frame);
this.#ffmpegRouter("audio", AACMONO48000BLANK);
}
if (this.camera_off_h264_frame && this.camera.streaming_enabled == false && this.camera.online == true) {
// Camera video is turned off so feed in our custom h264 frame for playback
this.#ffmpegRouter("video", this.camera_off_h264_frame);
this.#ffmpegRouter("audio", AACMONO48000BLANK);
}
if (this.camera_connecting_h264_frame && this.playingBack == false && this.camera.streaming_enabled == true && this.camera.online == true) {
// Connecting to camera video so feed in our custom h264 frame for playback
// Not sure worth enabling, but its here!
//this.#ffmpegRouter("video", this.camera_connecting_h264_frame);
//this.#ffmpegRouter("audio", AACMONO48000BLANK);
}
if (this.camera_offline_h264_frame && this.tcpSocket == null) {
// Seems we cant access the video stream as we have an empty connection, so feed in our custom h264 frame for playback
// We'll use the camera off h264 frame
//this.#ffmpegRouter("video", this.camera_offline_h264_frame);
//this.#ffmpegRouter("audio", AACMONO48000BLANK);
}
}, (TIMERINTERVAL / 30)); // output at 30 fps?
}
#close(sendStop) {
// Close an authenicated socket stream gracefully
if (this.tcpSocket != null) {
if (sendStop == true) {
// Send a notifcation to nexus we're finished playback
this.#stopNexusData();
}
this.tcpSocket.destroy();
}
this.tcpSocket = null;
this.sessionID = null; // Not an active session anymore
this.pendingMessages = []; // No more pending messages
this.weDidClose = true; // Flag we did the socket close
}
#startNexusData() {
if (this.camera.streaming_enabled == false || this.camera.online == false) {
return;
}
// Attempt to use camera's stream profile or use default
var otherProfiles = [];
this.camera.capabilities.forEach((element) => {
if (element.startsWith("streaming.cameraprofile")) {
var profile = element.replace("streaming.cameraprofile.", "");
if (otherProfiles.indexOf(profile, 0) == -1 && StreamProfile.VIDEO_H264_2MBIT_L40 != StreamProfile[profile]) {
// Profile isn't the primary profile, and isn't in the others list, so add it
otherProfiles.push(StreamProfile[profile]);
}
}
});
if (this.camera.audio_enabled == true) {
otherProfiles.push(StreamProfile.AUDIO_AAC); // Include AAC if audio enabled on camera
}
var startBuffer = new protoBuf();
startBuffer.writeVarintField(1, Math.floor(Math.random() * (100 - 1) + 1)); // Random session ID bwteen 1 and 100); // Session ID
startBuffer.writeVarintField(2, StreamProfile.VIDEO_H264_2MBIT_L40); // Default profile. ie: high quality
otherProfiles.forEach(otherProfile => {
startBuffer.writeVarintField(6, otherProfile); // Other supported profiles
});
this.#sendMessage(PacketType.START_PLAYBACK, startBuffer.finish());
}
#stopNexusData() {
var stopBuffer = new protoBuf();
stopBuffer.writeVarintField(1, this.sessionID); // Session ID
this.#sendMessage(PacketType.STOP_PLAYBACK, stopBuffer.finish());
}
#ffmpegRouter(type, data, time) {
// Send out our nexus data to any streams we're managing, including performing any buffering as required
if (typeof time == "undefined") time = Date.now(); // If we haven't passed in a timestamp, use the current time in milliseconds
// Add the data to the buffer if its active first up
if (this.buffer.active == true) {
// Ensure we only have the specified milliseconds of data buffered also
while (this.buffer.buffer.length > 0 && this.buffer.buffer[0].time < (Date.now() - this.buffer.maxTime)) {
this.buffer.buffer.shift(); // Remove the element from the tail of the buffer
}
this.buffer.buffer.push({time: Date.now(), synctime: time, type: type, data: data});
}
// Output the current data to any streams running, either a "live" or "recording" stream
for (var streamsIndex = 0; streamsIndex < this.buffer.streams.length; streamsIndex++) {
// Now output the current data to the stream, either a "live" or "recording" stream
if (this.buffer.streams[streamsIndex].aligned == false && type == "video" && (data && data[0] & 0x1f) == H264FrameTypes.SPS) this.buffer.streams[streamsIndex].aligned = true;
if (this.buffer.streams[streamsIndex].aligned == true) {
// We have been initally aligned to a h264 SPS frame, so send on data now
if (type == "video" && this.buffer.streams[streamsIndex].video != null) {
// H264 NAL Units "0001" are required to be added to beginning of any video data we output
this.buffer.streams[streamsIndex].video.write(Buffer.concat([H264NALUnit, data]));
}
if (type == "audio" && this.buffer.streams[streamsIndex].audio != null) {
this.buffer.streams[streamsIndex].audio.write(data);
}
}
}
}
#processMessages() {
// Send any pending messages that might have accumulated while socket pending etc
if (typeof this.pendingMessages != "object" || this.pendingMessages.length == 0) {
return;
}
for (let pendingMessage = this.pendingMessages.shift(); pendingMessage; pendingMessage = this.pendingMessages.shift()) {
this.#sendMessage(pendingMessage.messageType, pendingMessage.messageData);
}
}
#sendMessage(messageType, messageData) {
if (this.tcpSocket == null || this.tcpSocket.readyState != "open" || (messageType !== PacketType.HELLO && this.authorised == false)) {
this.pendingMessages.push({messageType, messageData});
return;
}
if (messageType !== PacketType.LONG_PLAYBACK_PACKET) {
var messageHeader = Buffer.alloc(3);
messageHeader[0] = messageType;
messageHeader.writeUInt16BE(messageData.length, 1);
}
if (messageType === PacketType.LONG_PLAYBACK_PACKET) {
var messageHeader = Buffer.alloc(5);
messageHeader[0] = messageType;
messageHeader.writeUInt32BE(messageData.length, 1);
}
// write our composed message to the socket
this.tcpSocket.write(Buffer.concat([messageHeader, Buffer.from(messageData)]), () => {
// Message sent. Dont do anything?
});
}
#Authenticate(reauthorise) {
// Authenticate over created socket connection
var tokenBuffer = new protoBuf();
var helloBuffer = new protoBuf();
this.authorised = false; // We're nolonger authorised
if (this.tokenType == "nest") {
tokenBuffer.writeStringField(1, this.cameraToken); // Tag 1, session token, Nest auth accounts
helloBuffer.writeStringField(4, this.cameraToken); // Tag 4, session token, Nest auth accounts
}
if (this.tokenType == "google") {
tokenBuffer.writeStringField(4, this.cameraToken); // Tag 4, olive token, Google auth accounts
helloBuffer.writeBytesField(12, tokenBuffer.finish()); // Tag 12, olive token, Google auth accounts
}
if (typeof reauthorise == "boolean" && reauthorise == true) {
// Request to re-authorise only
this.#outputLogging("Nest", true, "Re-authentication requested to '%s'", this.host);
this.#sendMessage(PacketType.AUTHORIZE_REQUEST, tokenBuffer.finish());
} else {
// This isnt a re-authorise request, so perform "Hello" packet
this.#outputLogging("Nest", true, "Performing authentication to '%s'", this.host);
helloBuffer.writeVarintField(1, ProtocolVersion.VERSION_3);
helloBuffer.writeStringField(2, this.camera.device_uuid.split(".")[1]); // UUID should be "quartz.xxxxxx". We want the xxxxxx part
helloBuffer.writeBooleanField(3, false); // Doesnt required a connected camera
helloBuffer.writeStringField(6, this.HomeKitAccessoryUUID); // UUID v4 device ID
helloBuffer.writeStringField(7, "Nest/5.69.0 (iOScom.nestlabs.jasper.release) os=15.6");
helloBuffer.writeVarintField(9, ClientType.IOS);
//helloBuffer.writeStringField(7, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Safari/605.1.15");
//helloBuffer.writeVarintField(9, ClientType.WEB);
this.#sendMessage(PacketType.HELLO, helloBuffer.finish());
}
}
#AudioPayload(payload) {
// Encode audio packet for sending to camera
var audioBuffer = new protoBuf();
audioBuffer.writeBytesField(1, payload); // audio data
audioBuffer.writeVarintField(2, this.sessionID); // session ID
audioBuffer.writeVarintField(3, CodecType.SPEEX); // codec
audioBuffer.writeVarintField(4, 16000); // sample rate, 16k
//audioBuffer.writeVarintField(5, ????); // Latency measure tag. What does this do?
this.#sendMessage(PacketType.AUDIO_PAYLOAD, audioBuffer.finish());
}
#handleRedirect(payload) {
if (typeof payload == "object") {
// Payload parameter is an object, we'll assume its a payload packet
// Decode redirect packet to determine new host
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.new_host = protoBuf.readString(); // new host
else if (tag === 2) obj.is_transcode = protoBuf.readBoolean();
}, {new_host: "", is_transcode: false});
var redirectToHost = packet.new_host;
}
if (typeof payload == "string") {
// Payload parameter is a string, we'll assume this is a direct hostname
var redirectToHost = payload;
}
if (typeof redirectToHost != "string" || redirectToHost == "") {
return;
}
this.#outputLogging("Nest", true, "Redirect requested from '%s' to '%s'", this.host, redirectToHost);
// Setup listener for socket close event. Once socket is closed, we'll perform the redirect
this.tcpSocket && this.tcpSocket.on("close", (hasError) => {
this.#connect(redirectToHost); // Connect to new host
});
this.#close(true); // Close existing socket
}
#handlePlaybackBegin(payload) {
// Decode playback begin packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.session_id = protoBuf.readVarint();
else if (tag === 2) obj.channels.push(protoBuf.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.channel_id = protoBuf.readVarint();
else if (tag === 2) obj.codec_type = protoBuf.readVarint();
else if (tag === 3) obj.sample_rate = protoBuf.readVarint();
else if (tag === 4) obj.private_data.push(protoBuf.readBytes());
else if (tag === 5) obj.start_time = protoBuf.readDouble();
else if (tag === 6) obj.udp_ssrc = protoBuf.readVarint();
else if (tag === 7) obj.rtp_start_time = protoBuf.readVarint();
else if (tag === 8) obj.profile = protoBuf.readVarint();
}, {channel_id: 0, codec_type: 0, sample_rate: 0, private_data: [], start_time: 0, udp_ssrc: 0, rtp_start_time: 0, profile: 3}, protoBuf.readVarint() + protoBuf.pos));
else if (tag === 3) obj.srtp_master_key = protoBuf.readBytes();
else if (tag === 4) obj.srtp_master_salt = protoBuf.readBytes();
else if (tag === 5) obj.fec_k_val = protoBuf.readVarint();
else if (tag === 6) obj.fec_n_val = protoBuf.readVarint();
}, {session_id: 0, channels: [], srtp_master_key: null, srtp_master_salt: null, fec_k_val: 0, fec_n_val: 0});
packet.channels && packet.channels.forEach(stream => {
// Find which channels match our video and audio streams
if (stream.codec_type == CodecType.H264) {
this.nexusvideo = {channel_id: stream.channel_id, start_time: (stream.start_time * 1000), sample_rate: stream.sample_rate, packet_time: (stream.start_time * 1000)};
}
if (stream.codec_type == CodecType.AAC) {
this.nexusaudio = {channel_id: stream.channel_id, start_time: (stream.start_time * 1000), sample_rate: stream.sample_rate, packet_time: (stream.start_time * 1000)};
}
});
// Since this is the beginning of playback, clear any active buffers contents
this.buffer.buffer = [];
this.playingBack = true;
this.sessionID = packet.session_id;
this.#outputLogging("Nest", true, "Playback started from '%s' with session ID '%s'", this.host, this.sessionID);
}
#handlePlaybackPacket(payload) {
// Decode playback packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.session_id = protoBuf.readVarint();
else if (tag === 2) obj.channel_id = protoBuf.readVarint();
else if (tag === 3) obj.timestamp_delta = protoBuf.readSVarint();
else if (tag === 4) obj.payload = protoBuf.readBytes();
else if (tag === 5) obj.latency_rtp_sequence = protoBuf.readVarint();
else if (tag === 6) obj.latency_rtp_ssrc = protoBuf.readVarint();
else if (tag === 7) obj.directors_cut_regions.push(protoBuf.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.id = protoBuf.readVarint();
else if (tag === 2) obj.left = protoBuf.readVarint();
else if (tag === 3) obj.right = protoBuf.readVarint();
else if (tag === 4) obj.top = protoBuf.readVarint();
else if (tag === 5) obj.bottom = protoBuf.readVarint();
}, { id: 0, left: 0, right: 0, top: 0, bottom: 0 }, protoBuf.readVarint() + protoBuf.pos));
}, {session_id: 0, channel_id: 0, timestamp_delta: 0, payload: null, latency_rtp_sequence: 0, latency_rtp_ssrc: 0, directors_cut_regions: []});
// Handle video packet
if (packet.channel_id === this.nexusvideo.channel_id) {
this.nexusvideo.packet_time = (this.nexusvideo.start_time + (Date.now() - this.nexusvideo.start_time));
this.#ffmpegRouter("video", Buffer.from(packet.payload), this.nexusvideo.packet_time);
}
// Handle audio packet
if (packet.channel_id === this.nexusaudio.channel_id) {
this.nexusaudio.packet_time = (this.nexusaudio.start_time + (Date.now() - this.nexusaudio.start_time));
this.#ffmpegRouter("audio", Buffer.from(packet.payload), this.nexusaudio.packet_time);
}
}
#handlePlaybackEnd(payload) {
// Decode playpack ended packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.session_id = protoBuf.readVarint();
else if (tag === 2) obj.reason = protoBuf.readVarint();
}, {session_id: 0, reason: 0});
if (this.playingBack == true && packet.reason == 0) {
// Normal playback ended ie: when we stopped playback
this.#outputLogging("Nest", true, "Playback ended on '%s'", this.host);
}
if (packet.reason != 0) {
// Error during playback, so we'll attempt to restart by reconnection to host
this.#outputLogging("Nest", true, "Playback ended on '%s' with error '%s'. Attempting reconnection", this.host, packet.reason);
// Setup listener for socket close event. Once socket is closed, we'll perform the re-connection
this.tcpSocket && this.tcpSocket.on("close", (hasError) => {
this.#connect(this.host); // try reconnection to existing host
});
this.#close(false); // Close existing socket
}
this.playingBack = false; // Playback ended
}
#handleNexusError(payload) {
// Decode error packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.code = protoBuf.readVarint();
else if (tag === 2) obj.message = protoBuf.readString();
}, {code: 1, message: ""});
if (packet.code === ErrorCode.ERROR_AUTHORIZATION_FAILED) {
// NexusStreamer Updating authentication
this.#Authenticate(true); // Update authorisation only
} else {
// NexusStreamer Error, packet.message contains the message
this.#outputLogging("Nest", true, "Error", packet.message);
}
}
#handleTalkbackBegin(payload) {
// Decode talk begin packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.user_id = protoBuf.readString();
else if (tag === 2) obj.session_id = protoBuf.readVarint();
else if (tag === 3) obj.quick_action_id = protoBuf.readVarint();
else if (tag === 4) obj.device_id = protoBuf.readString();
}, {user_id: "", session_id: 0, quick_action_id: 0, device_id: ""});
this.#outputLogging("Nest", true, "Talkback started on '%s'", packet.device_id);
this.talking = true; // Talk back has started
}
#handleTalkbackEnd(payload) {
// Decode talk end packet
var packet = payload.readFields(function(tag, obj, protoBuf) {
if (tag === 1) obj.user_id = protoBuf.readString();
else if (tag === 2) obj.session_id = protoBuf.readVarint();
else if (tag === 3) obj.quick_action_id = protoBuf.readVarint();
else if (tag === 4) obj.device_id = protoBuf.readString();
}, {user_id: "", session_id: 0, quick_action_id: 0, device_id: ""});
this.#outputLogging("Nest", true, "Talkback ended on '%s'", packet.device_id);
this.talking = false; // Talk back has stopped
}
#handleNexusData(data) {
// Process the rawdata from our socket connection and convert into nexus packets to take action against
this.pendingBuffer = (this.pendingBuffer == null ? data : Buffer.concat([this.pendingBuffer, data]));
if (this.pendingBuffer.length < 3) {
// Ensure we have a minimun length in the buffer to read header details
return;
}
var packetType = this.pendingBuffer.readUInt8();
var headerSizeInBytes = 3;
var dataSizeInBytes = this.pendingBuffer.readUInt16BE(1);
if (packetType == PacketType.LONG_PLAYBACK_PACKET) {
headerSizeInBytes = 5;
dataSizeInBytes = this.pendingBuffer.readUInt32BE(1);
}
var protoBufPayloadSize = headerSizeInBytes + dataSizeInBytes;
if (this.pendingBuffer.length < protoBufPayloadSize) {
return;
}
var protoBufPayload = new protoBuf(this.pendingBuffer.slice(headerSizeInBytes, protoBufPayloadSize));
if (packetType == PacketType.OK) {
this.authorised = true; // OK message, means we're connected and authorised to Nexus
this.#processMessages(); // process any pending messages
this.#startNexusData(); // start processing data
}
if (packetType == PacketType.ERROR) {
this.#handleNexusError(protoBufPayload);
}
if (packetType == PacketType.PLAYBACK_BEGIN) {
this.#handlePlaybackBegin(protoBufPayload);
}
if (packetType == PacketType.PLAYBACK_END) {
this.#handlePlaybackEnd(protoBufPayload);
}
if (packetType == PacketType.PLAYBACK_PACKET || packetType == PacketType.LONG_PLAYBACK_PACKET) {
this.#handlePlaybackPacket(protoBufPayload);
}
if (packetType == PacketType.REDIRECT) {
this.#handleRedirect(protoBufPayload);
}
if (packetType == PacketType.TALKBACK_BEGIN) {
this.#handleTalkbackBegin(protoBufPayload);
}
if (packetType == PacketType.TALKBACK_END) {
this.#handleTalkbackEnd(protoBufPayload);
}
if (packetType == PacketType.PING) {
}
var remainingData = this.pendingBuffer.slice(protoBufPayloadSize);
this.pendingBuffer = null;
if (remainingData.length > 0) {
this.#handleNexusData(remainingData); // Maybe not do this recursive???
}
}
#outputLogging(accessoryName, useConsoleDebug, ...outputMessage) {
if (this.enableDebugging == false) {
return;
}
var timeStamp = String(new Date().getFullYear()).padStart(4, "0") + "-" + String(new Date().getMonth() + 1).padStart(2, "0") + "-" + String(new Date().getDate()).padStart(2, "0") + " " + String(new Date().getHours()).padStart(2, "0") + ":" + String(new Date().getMinutes()).padStart(2, "0") + ":" + String(new Date().getSeconds()).padStart(2, "0");
if (useConsoleDebug == false) {
console.log(timeStamp + " [" + accessoryName + "] " + util.format(...outputMessage));
}
if (useConsoleDebug == true) {
console.debug(timeStamp + " [" + accessoryName + "] " + util.format(...outputMessage));
}
}
}
module.exports = NexusStreamer;