-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathhl2ss.py
2633 lines (1986 loc) · 88.4 KB
/
hl2ss.py
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
import numpy as np
import socket
import struct
import cv2
import av
# Stream TCP Ports
class StreamPort:
RM_VLC_LEFTFRONT = 3800
RM_VLC_LEFTLEFT = 3801
RM_VLC_RIGHTFRONT = 3802
RM_VLC_RIGHTRIGHT = 3803
RM_DEPTH_AHAT = 3804
RM_DEPTH_LONGTHROW = 3805
RM_IMU_ACCELEROMETER = 3806
RM_IMU_GYROSCOPE = 3807
RM_IMU_MAGNETOMETER = 3808
PERSONAL_VIDEO = 3810
MICROPHONE = 3811
SPATIAL_INPUT = 3812
EXTENDED_EYE_TRACKER = 3817
EXTENDED_AUDIO = 3818
EXTENDED_VIDEO = 3819
EXTENDED_DEPTH = 3821
# IPC TCP Ports
class IPCPort:
REMOTE_CONFIGURATION = 3809
SPATIAL_MAPPING = 3813
SCENE_UNDERSTANDING = 3814
VOICE_INPUT = 3815
UNITY_MESSAGE_QUEUE = 3816
GUEST_MESSAGE_QUEUE = 3820
# Default Chunk Sizes
class ChunkSize:
RM_VLC = 4096
RM_DEPTH_AHAT = 4096
RM_DEPTH_LONGTHROW = 4096
RM_IMU = 4096
PERSONAL_VIDEO = 4096
MICROPHONE = 512
SPATIAL_INPUT = 1024
EXTENDED_EYE_TRACKER = 256
EXTENDED_AUDIO = 512
EXTENDED_VIDEO = 4096
EXTENDED_DEPTH = 4096
SINGLE_TRANSFER = 4096
# Stream Operating Mode
# 0: Device data (e.g. video)
# 1: Device data + location data (e.g. video + camera pose)
# 2: Device constants (e.g. camera intrinsics)
# 3: Reserved
class StreamMode:
MODE_0 = 0
MODE_1 = 1
MODE_2 = 2
MODE_3 = 3
# Video Encoder Profile
# 0: H264 base
# 1: H264 main
# 2: H264 high
# 3: H265 main (HEVC)
# 255: No encoding
class VideoProfile:
H264_BASE = 0
H264_MAIN = 1
H264_HIGH = 2
H265_MAIN = 3
RAW = 0xFF
# Video Encoder Level
class H26xLevel:
H264_1 = 10
H264_1_b = 11
H264_1_1 = 11
H264_1_2 = 12
H264_1_3 = 13
H264_2 = 20
H264_2_1 = 21
H264_2_2 = 22
H264_3 = 30
H264_3_1 = 31
H264_3_2 = 32
H264_4 = 40
H264_4_1 = 41
H264_4_2 = 42
H264_5 = 50
H264_5_1 = 51
H264_5_2 = 52
H265_1 = 30
H265_2 = 60
H265_2_1 = 63
H265_3 = 90
H265_3_1 = 93
H265_4 = 120
H265_4_1 = 123
H265_5 = 150
H265_5_1 = 153
H265_5_2 = 156
H265_6 = 180
H265_6_1 = 183
H265_6_2 = 186
DEFAULT = 255
# Depth Encoder Profile
# 0: Same as AB
# 1: Zdepth
class DepthProfile:
SAME = 0
ZDEPTH = 1
# Audio Encoder Profile
# 0: AAC 12000 bytes/s
# 1: AAC 16000 bytes/s
# 2: AAC 20000 bytes/s
# 3: AAC 24000 bytes/s
# 255: No encoding
class AudioProfile:
AAC_12000 = 0
AAC_16000 = 1
AAC_20000 = 2
AAC_24000 = 3
RAW = 0xFF
# Audio Encoder Level
# 0x29: AAC Profile L2 (default)
# 0x2A: AAC Profile L4
# 0x2B: AAC Profile L5
# 0x2C: High Efficiency v1 AAC Profile L2
# 0x2E: High Efficiency v1 AAC Profile L4
# 0x2F: High Efficiency v1 AAC Profile L5
# 0x30: High Efficiency v2 AAC Profile L2
# 0x31: High Efficiency v2 AAC Profile L3
# 0x32: High Efficiency v2 AAC Profile L4
# 0x33: High Efficiency v2 AAC Profile L5
class AACLevel:
L2 = 0x29
L4 = 0x2A
L5 = 0x2B
HEV1L2 = 0x2C
HEV1L4 = 0x2E
HEV1L5 = 0x2F
HEV2L2 = 0x30
HEV2L3 = 0x31
HEV2L4 = 0x32
HEV2L5 = 0x33
# PNG Filters
class PNGFilterMode:
AUTOMATIC = 0
DISABLE = 1
SUB = 2
UP = 3
AVERAGE = 4
PAETH = 5
ADAPTIVE = 6
class HologramPerspective:
DISPLAY = 0
PV = 1
class MixerMode:
MICROPHONE = 0
SYSTEM = 1
BOTH = 2
QUERY = 3
# RM VLC Parameters
class Parameters_RM_VLC:
WIDTH = 640
HEIGHT = 480
FPS = 30
PIXELS = WIDTH * HEIGHT
SHAPE = (HEIGHT, WIDTH)
PERIOD = 1 / FPS
# RM Depth AHAT Parameters
class Parameters_RM_DEPTH_AHAT:
WIDTH = 512
HEIGHT = 512
FPS = 45
PIXELS = WIDTH * HEIGHT
SHAPE = (HEIGHT, WIDTH)
PERIOD = 1 / FPS
# RM Depth Long Throw Parameters
class Parameters_RM_DEPTH_LONGTHROW:
WIDTH = 320
HEIGHT = 288
FPS = 5
PIXELS = WIDTH * HEIGHT
SHAPE = (HEIGHT, WIDTH)
PERIOD = 1 / FPS
# RM IMU Accelerometer Parameters
class Parameters_RM_IMU_ACCELEROMETER:
BATCH_SIZE = 93
# RM IMU Gyroscope Parameters
class Parameters_RM_IMU_GYROSCOPE:
BATCH_SIZE = 315
# RM IMU Magnetometer Parameters
class Parameters_RM_IMU_MAGNETOMETER:
BATCH_SIZE = 11
# Microphone Parameters
class Parameters_MICROPHONE:
ARRAY_CHANNELS = 5
ARRAY_TOP_LEFT = 0
ARRAY_TOP_CENTER = 1
ARRAY_TOP_RIGHT = 2
ARRAY_BOTTOM_LEFT = 3
ARRAY_BOTTOM_RIGHT = 4
SAMPLE_RATE = 48000
CHANNELS = 2
PERIOD = 1 / SAMPLE_RATE
GROUP_SIZE_RAW = 768
GROUP_SIZE_AAC = 1024
# Spatial Input Parameters
class Parameters_SI:
SAMPLE_RATE = 30
PERIOD = 1 / SAMPLE_RATE
# Time base for all timestamps
class TimeBase:
HUNDREDS_OF_NANOSECONDS = 10*1000*1000
#------------------------------------------------------------------------------
# Network Client
#------------------------------------------------------------------------------
class _SIZEOF:
CHAR = 1
BYTE = 1
SHORT = 2
WORD = 2
HALF = 2
INT = 4
DWORD = 4
FLOAT = 4
LONGLONG = 8
QWORD = 8
DOUBLE = 8
class _RANGEOF:
U8_MAX = 0xFF
U16_MAX = 0xFFFF
U32_MAX = 0xFFFFFFFF
U64_MAX = 0xFFFFFFFFFFFFFFFF
class _client:
def open(self, host, port):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((host, port))
def sendall(self, data):
self._socket.sendall(data)
def recv(self, chunk_size):
chunk = self._socket.recv(chunk_size)
if (len(chunk) <= 0):
raise Exception('connection closed')
return chunk
def download(self, total, chunk_size):
data = bytearray()
while (total > 0):
if (chunk_size > total):
chunk_size = total
chunk = self.recv(chunk_size)
data.extend(chunk)
total -= len(chunk)
return data
def close(self):
self._socket.close()
#------------------------------------------------------------------------------
# Packet Unpacker
#------------------------------------------------------------------------------
class _packet:
def __init__(self, timestamp, payload, pose):
self.timestamp = timestamp
self.payload = payload
self.pose = pose
def pack_packet(packet):
buffer = bytearray()
buffer.extend(struct.pack('<QI', packet.timestamp, len(packet.payload)))
buffer.extend(packet.payload)
if (packet.pose is not None):
buffer.extend(packet.pose.tobytes())
return buffer
def unpack_packet(data):
timestamp, payload_size = struct.unpack('<QI', data[:12])
payload = data[12:(12 + payload_size)]
pose = data[(12 + payload_size):]
return _packet(timestamp, payload, np.frombuffer(pose, dtype=np.float32).reshape((4, 4)) if (len(pose) == 64) else None)
def is_valid_pose(pose):
return pose[3, 3] != 0
class _unpacker:
def reset(self, mode):
self._mode = mode
self._state = 0
self._buffer = bytearray()
self._timestamp = None
self._size = None
self._payload = None
self._pose = None
def extend(self, chunk):
self._buffer.extend(chunk)
def unpack(self):
length = len(self._buffer)
while (True):
if (self._state == 0):
if (length >= 12):
header = struct.unpack('<QI', self._buffer[:12])
self._timestamp = header[0]
self._size = 12 + header[1]
if (self._mode == StreamMode.MODE_1):
self._size += 64
self._state = 1
continue
elif (self._state == 1):
if (length >= self._size):
if (self._mode == StreamMode.MODE_1):
payload_end = self._size - 64
self._pose = np.frombuffer(self._buffer[payload_end:self._size], dtype=np.float32).reshape((4, 4))
else:
payload_end = self._size
self._payload = self._buffer[12:payload_end]
self._buffer = self._buffer[self._size:]
self._state = 0
return True
return False
def get(self):
return _packet(self._timestamp, self._payload, self._pose)
#------------------------------------------------------------------------------
# Packet Gatherer
#------------------------------------------------------------------------------
class _gatherer:
def open(self, host, port, chunk_size, mode):
self._client = _client()
self._unpacker = _unpacker()
self._chunk_size = chunk_size
self._unpacker.reset(mode)
self._client.open(host, port)
def sendall(self, data):
self._client.sendall(data)
def get_next_packet(self):
while (True):
self._unpacker.extend(self._client.recv(self._chunk_size))
if (self._unpacker.unpack()):
return self._unpacker.get()
def close(self):
self._client.close()
#------------------------------------------------------------------------------
# Stream Configuration
#------------------------------------------------------------------------------
class H26xEncoderProperty:
CODECAPI_AVEncCommonRateControlMode = 0
CODECAPI_AVEncCommonQuality = 1
CODECAPI_AVEncAdaptiveMode = 2
CODECAPI_AVEncCommonBufferSize = 3
CODECAPI_AVEncCommonMaxBitRate = 4
CODECAPI_AVEncCommonMeanBitRate = 5
CODECAPI_AVEncCommonQualityVsSpeed = 6
CODECAPI_AVEncH264CABACEnable = 7
CODECAPI_AVEncH264SPSID = 8
CODECAPI_AVEncMPVDefaultBPictureCount = 9
CODECAPI_AVEncMPVGOPSize = 10
CODECAPI_AVEncNumWorkerThreads = 11
CODECAPI_AVEncVideoContentType = 12
CODECAPI_AVEncVideoEncodeQP = 13
CODECAPI_AVEncVideoForceKeyFrame = 14
CODECAPI_AVEncVideoMinQP = 15
CODECAPI_AVLowLatencyMode = 16
CODECAPI_AVEncVideoMaxQP = 17
CODECAPI_VideoEncoderDisplayContentType = 18
HL2SSAPI_VideoMediaIndex = 0xFFFFFFFFFFFFFFFB
HL2SSAPI_VideoStrideMask = 0xFFFFFFFFFFFFFFFC
HL2SSAPI_AcquisitionMode = 0xFFFFFFFFFFFFFFFD
HL2SSAPI_VLCHostTicksOffsetConstant = 0xFFFFFFFFFFFFFFFE
HL2SSAPI_VLCHostTicksOffsetExposure = 0xFFFFFFFFFFFFFFFF
def _create_configuration_for_mode(mode):
return struct.pack('<B', mode)
def _create_configuration_for_video_format(width, height, framerate):
return struct.pack('<HHB', width, height, framerate)
def _create_configuration_for_video_divisor(divisor):
return struct.pack('<B', divisor)
def _create_configuration_for_video_encoding(profile, level, bitrate):
return struct.pack('<BBI', profile, level, bitrate)
def _create_configuration_for_depth_encoding(profile):
return struct.pack('<B', profile)
def _create_configuration_for_audio_encoding(profile, level):
return struct.pack('<BB', profile, level)
def _create_configuration_for_png_encoding(png_filter):
return struct.pack('<I', png_filter)
def _create_configuration_for_h26x_encoding(options):
configuration = bytearray()
configuration.extend(struct.pack('<B', len(options)))
for key, value in options.items():
configuration.extend(struct.pack('<QQ', key, value))
return bytes(configuration)
def _create_configuration_for_framerate(fps):
return struct.pack('<B', fps)
def _create_configuration_for_mrc_video(enable, hologram_composition, recording_indicator, video_stabilization, blank_protected, show_mesh, shared, global_opacity, output_width, output_height, video_stabilization_length, hologram_perspective):
return struct.pack('<BBBBBBBfffII', 1 if (enable) else 0, 1 if (hologram_composition) else 0, 1 if (recording_indicator) else 0, 1 if (video_stabilization) else 0, 1 if (blank_protected) else 0, 1 if (show_mesh) else 0, 1 if (shared) else 0, global_opacity, output_width, output_height, video_stabilization_length, hologram_perspective)
def _create_configuration_for_mrc_audio(mixer_mode, loopback_gain, microphone_gain):
return struct.pack('<Iff', mixer_mode, loopback_gain, microphone_gain)
def _create_configuration_for_rm_vlc(mode, divisor, profile, level, bitrate, options):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_divisor(divisor))
configuration.extend(_create_configuration_for_video_encoding(profile, level, bitrate))
configuration.extend(_create_configuration_for_h26x_encoding(options))
return bytes(configuration)
def _create_configuration_for_rm_depth_ahat(mode, divisor, profile_z, profile_ab, level, bitrate, options):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_divisor(divisor))
configuration.extend(_create_configuration_for_depth_encoding(profile_z))
configuration.extend(_create_configuration_for_video_encoding(profile_ab, level, bitrate))
configuration.extend(_create_configuration_for_h26x_encoding(options))
return bytes(configuration)
def _create_configuration_for_rm_depth_longthrow(mode, divisor, png_filter):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_divisor(divisor))
configuration.extend(_create_configuration_for_png_encoding(png_filter))
return bytes(configuration)
def _create_configuration_for_rm_imu(mode):
return _create_configuration_for_mode(mode)
def _create_configuration_for_pv(mode, width, height, framerate, divisor, profile, level, bitrate, options):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_format(width, height, framerate))
configuration.extend(_create_configuration_for_video_divisor(divisor))
configuration.extend(_create_configuration_for_video_encoding(profile, level, bitrate))
configuration.extend(_create_configuration_for_h26x_encoding(options))
return bytes(configuration)
def _create_configuration_for_microphone(profile, level):
return _create_configuration_for_audio_encoding(profile, level)
def _create_configuration_for_eet(fps):
return _create_configuration_for_framerate(fps)
def _create_configuration_for_extended_audio(mixer_mode, loopback_gain, microphone_gain, profile, level):
configuration = bytearray()
configuration.extend(_create_configuration_for_mrc_audio(mixer_mode, loopback_gain, microphone_gain))
configuration.extend(_create_configuration_for_audio_encoding(profile, level))
return bytes(configuration)
def _create_configuration_for_extended_depth(mode, divisor, profile_z, options):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_divisor(divisor))
configuration.extend(_create_configuration_for_depth_encoding(profile_z))
configuration.extend(_create_configuration_for_h26x_encoding(options))
return bytes(configuration)
def _create_configuration_for_rm_mode2(mode):
return _create_configuration_for_mode(mode)
def _create_configuration_for_pv_mode2(mode, width, height, framerate):
configuration = bytearray()
configuration.extend(_create_configuration_for_mode(mode))
configuration.extend(_create_configuration_for_video_format(width, height, framerate))
return bytes(configuration)
def extended_audio_device_mixer_mode(mixer_mode, device):
DEVICE_BASE = 0x00000004
return mixer_mode | (DEVICE_BASE * (device + 1))
#------------------------------------------------------------------------------
# Mode 0 and Mode 1 Data Acquisition
#------------------------------------------------------------------------------
def _connect_client_rm_vlc(host, port, chunk_size, mode, divisor, profile, level, bitrate, options):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_rm_vlc(mode, divisor, profile, level, bitrate, options))
return c
def _connect_client_rm_depth_ahat(host, port, chunk_size, mode, divisor, profile_z, profile_ab, level, bitrate, options):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_rm_depth_ahat(mode, divisor, profile_z, profile_ab, level, bitrate, options))
return c
def _connect_client_rm_depth_longthrow(host, port, chunk_size, mode, divisor, png_filter):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_rm_depth_longthrow(mode, divisor, png_filter))
return c
def _connect_client_rm_imu(host, port, chunk_size, mode):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_rm_imu(mode))
return c
def _connect_client_pv(host, port, chunk_size, mode, width, height, framerate, divisor, profile, level, bitrate, options):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_pv(mode, width, height, framerate, divisor, profile, level, bitrate, options))
return c
def _connect_client_microphone(host, port, chunk_size, profile, level):
c = _gatherer()
c.open(host, port, chunk_size, StreamMode.MODE_0)
c.sendall(_create_configuration_for_microphone(profile, level))
return c
def _connect_client_si(host, port, chunk_size):
c = _gatherer()
c.open(host, port, chunk_size, StreamMode.MODE_0)
return c
def _connect_client_eet(host, port, chunk_size, fps):
c = _gatherer()
c.open(host, port, chunk_size, StreamMode.MODE_1)
c.sendall(_create_configuration_for_eet(fps))
return c
def _connect_client_extended_audio(host, port, chunk_size, mixer_mode, loopback_gain, microphone_gain, profile, level):
c = _gatherer()
c.open(host, port, chunk_size, StreamMode.MODE_0)
c.sendall(_create_configuration_for_extended_audio(mixer_mode, loopback_gain, microphone_gain, profile, level))
return c
def _connect_client_extended_depth(host, port, chunk_size, mode, divisor, profile_z, options):
c = _gatherer()
c.open(host, port, chunk_size, mode)
c.sendall(_create_configuration_for_extended_depth(mode, divisor, profile_z, options))
return c
class _PVCNT:
START = 0x04
STOP = 0x08
def start_subsystem_pv(host, port, enable_mrc, hologram_composition, recording_indicator, video_stabilization, blank_protected, show_mesh, shared, global_opacity, output_width, output_height, video_stabilization_length, hologram_perspective):
c = _client()
c.open(host, port)
c.sendall(_create_configuration_for_mode(_PVCNT.START | StreamMode.MODE_3))
c.sendall(_create_configuration_for_mrc_video(enable_mrc, hologram_composition, recording_indicator, video_stabilization, blank_protected, show_mesh, shared, global_opacity, output_width, output_height, video_stabilization_length, hologram_perspective))
c.close()
def stop_subsystem_pv(host, port):
c = _client()
c.open(host, port)
c.sendall(_create_configuration_for_mode(_PVCNT.STOP | StreamMode.MODE_3))
c.close()
#------------------------------------------------------------------------------
# Context Manager
#------------------------------------------------------------------------------
class _context_manager:
def __enter__(self):
self.open()
return self
def __exit__(self, *args):
self.close()
#------------------------------------------------------------------------------
# Receiver Wrappers
#------------------------------------------------------------------------------
class rx_rm_vlc(_context_manager):
def __init__(self, host, port, chunk, mode, divisor, profile, level, bitrate, options):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
self.divisor = divisor
self.profile = profile
self.level = level
self.bitrate = bitrate
self.options = options
def open(self):
self._client = _connect_client_rm_vlc(self.host, self.port, self.chunk, self.mode, self.divisor, self.profile, self.level, self.bitrate, self.options)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_rm_depth_ahat(_context_manager):
def __init__(self, host, port, chunk, mode, divisor, profile_z, profile_ab, level, bitrate, options):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
self.divisor = divisor
self.profile_z = profile_z
self.profile_ab = profile_ab
self.level = level
self.bitrate = bitrate
self.options = options
def open(self):
self._client = _connect_client_rm_depth_ahat(self.host, self.port, self.chunk, self.mode, self.divisor, self.profile_z, self.profile_ab, self.level, self.bitrate, self.options)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_rm_depth_longthrow(_context_manager):
def __init__(self, host, port, chunk, mode, divisor, png_filter):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
self.divisor = divisor
self.png_filter = png_filter
def open(self):
self._client = _connect_client_rm_depth_longthrow(self.host, self.port, self.chunk, self.mode, self.divisor, self.png_filter)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_rm_imu(_context_manager):
def __init__(self, host, port, chunk, mode):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
def open(self):
self._client = _connect_client_rm_imu(self.host, self.port, self.chunk, self.mode)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_pv(_context_manager):
def __init__(self, host, port, chunk, mode, width, height, framerate, divisor, profile, level, bitrate, options):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
self.width = width
self.height = height
self.framerate = framerate
self.divisor = divisor
self.profile = profile
self.level = level
self.bitrate = bitrate
self.options = options
def open(self):
self._client = _connect_client_pv(self.host, self.port, self.chunk, self.mode, self.width, self.height, self.framerate, self.divisor, self.profile, self.level, self.bitrate, self.options)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_microphone(_context_manager):
def __init__(self, host, port, chunk, profile, level):
self.host = host
self.port = port
self.chunk = chunk
self.profile = profile
self.level = level
def open(self):
self._client = _connect_client_microphone(self.host, self.port, self.chunk, self.profile, self.level)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_si(_context_manager):
def __init__(self, host, port, chunk):
self.host = host
self.port = port
self.chunk = chunk
def open(self):
self._client = _connect_client_si(self.host, self.port, self.chunk)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_eet(_context_manager):
def __init__(self, host, port, chunk, fps):
self.host = host
self.port = port
self.chunk = chunk
self.fps = fps
def open(self):
self._client = _connect_client_eet(self.host, self.port, self.chunk, self.fps)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_extended_audio:
def __init__(self, host, port, chunk, mixer_mode, loopback_gain, microphone_gain, profile, level):
self.host = host
self.port = port
self.chunk = chunk
self.mixer_mode = mixer_mode
self.loopback_gain = loopback_gain
self.microphone_gain = microphone_gain
self.profile = profile
self.level = level
def open(self):
self._client = _connect_client_extended_audio(self.host, self.port, self.chunk, self.mixer_mode, self.loopback_gain, self.microphone_gain, self.profile, self.level)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
class rx_extended_depth:
def __init__(self, host, port, chunk, mode, divisor, profile_z, options):
self.host = host
self.port = port
self.chunk = chunk
self.mode = mode
self.divisor = divisor
self.profile_z = profile_z
self.options = options
def open(self):
self._client = _connect_client_extended_depth(self.host, self.port, self.chunk, self.mode, self.divisor, self.profile_z, self.options)
def get_next_packet(self):
return self._client.get_next_packet()
def close(self):
self._client.close()
#------------------------------------------------------------------------------
# Codecs
#------------------------------------------------------------------------------
def get_video_codec_name(profile):
if (profile == VideoProfile.H264_BASE):
return 'h264'
if (profile == VideoProfile.H264_MAIN):
return 'h264'
if (profile == VideoProfile.H264_HIGH):
return 'h264'
if (profile == VideoProfile.H265_MAIN):
return 'hevc'
return None
def get_audio_codec_name(profile):
if (profile == AudioProfile.AAC_12000):
return 'aac'
if (profile == AudioProfile.AAC_16000):
return 'aac'
if (profile == AudioProfile.AAC_20000):
return 'aac'
if (profile == AudioProfile.AAC_24000):
return 'aac'
return None
def get_audio_codec_bitrate(profile):
if (profile == AudioProfile.AAC_12000):
return 12000*8
if (profile == AudioProfile.AAC_16000):
return 16000*8
if (profile == AudioProfile.AAC_20000):
return 20000*8
if (profile == AudioProfile.AAC_24000):
return 24000*8
return None
#------------------------------------------------------------------------------
# RM VLC Decoder
#------------------------------------------------------------------------------
class _RM_VLC_Frame:
def __init__(self, image, sensor_ticks, exposure, gain):
self.image = image
self.sensor_ticks = sensor_ticks
self.exposure = exposure
self.gain = gain
def unpack_rm_vlc(payload):
image = payload[:-24]
metadata = payload[-24:]
sensor_ticks = np.frombuffer(metadata, dtype=np.uint64, offset=0, count=1)
exposure = np.frombuffer(metadata, dtype=np.uint64, offset=8, count=1)
gain = np.frombuffer(metadata, dtype=np.uint32, offset=16, count=1)
return _RM_VLC_Frame(image, sensor_ticks, exposure, gain)
class _decode_rm_vlc:
def __init__(self, profile):
self.profile = profile
def create(self):
self._codec = av.CodecContext.create(get_video_codec_name(self.profile), 'r')
def decode(self, payload):
for packet in self._codec.parse(payload):
for frame in self._codec.decode(packet):
return frame.to_ndarray()[:Parameters_RM_VLC.HEIGHT, :Parameters_RM_VLC.WIDTH]
return None
class _unpack_rm_vlc:
def create(self):
pass
def decode(self, payload):
return np.frombuffer(payload, dtype=np.uint8).reshape(Parameters_RM_VLC.SHAPE)
def decode_rm_vlc(profile):
return _unpack_rm_vlc() if (profile == VideoProfile.RAW) else _decode_rm_vlc(profile)
#------------------------------------------------------------------------------
# RM Depth Decoder
#------------------------------------------------------------------------------
class _RM_Depth_Frame:
def __init__(self, depth, ab, sensor_ticks):
self.depth = depth
self.ab = ab
self.sensor_ticks = sensor_ticks
class _Mode0Layout_RM_DEPTH_AHAT_STRUCT:
BASE = 8
class _Mode0Layout_RM_DEPTH_AHAT:
BEGIN_DEPTH_Y = 0
END_DEPTH_Y = BEGIN_DEPTH_Y + Parameters_RM_DEPTH_AHAT.HEIGHT
BEGIN_AB_U_Y = END_DEPTH_Y
END_AB_U_Y = BEGIN_AB_U_Y + (Parameters_RM_DEPTH_AHAT.WIDTH // 4)
BEGIN_AB_V_Y = END_AB_U_Y
END_AB_V_Y = BEGIN_AB_V_Y + (Parameters_RM_DEPTH_AHAT.WIDTH // 4)
def _unpack_rm_depth_ahat_nv12_as_yuv420p(yuv, sensor_ticks):
y = yuv[_Mode0Layout_RM_DEPTH_AHAT.BEGIN_DEPTH_Y : _Mode0Layout_RM_DEPTH_AHAT.END_DEPTH_Y, :]
u = yuv[_Mode0Layout_RM_DEPTH_AHAT.BEGIN_AB_U_Y : _Mode0Layout_RM_DEPTH_AHAT.END_AB_U_Y, :].reshape((Parameters_RM_DEPTH_AHAT.HEIGHT, Parameters_RM_DEPTH_AHAT.WIDTH // 4))
v = yuv[_Mode0Layout_RM_DEPTH_AHAT.BEGIN_AB_V_Y : _Mode0Layout_RM_DEPTH_AHAT.END_AB_V_Y, :].reshape((Parameters_RM_DEPTH_AHAT.HEIGHT, Parameters_RM_DEPTH_AHAT.WIDTH // 4))
depth = np.multiply(y, 4, dtype=np.uint16)