forked from microsoft/Azure-Kinect-Sensor-SDK
-
Notifications
You must be signed in to change notification settings - Fork 7
/
k4a.c
1291 lines (1117 loc) · 51.6 KB
/
k4a.c
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// This library
#include <k4a/k4a.h>
// Dependent libraries
#include <k4ainternal/common.h>
#include <k4ainternal/capture.h>
#include <k4ainternal/depth.h>
#include <k4ainternal/imu.h>
#include <k4ainternal/color.h>
#include <k4ainternal/color_mcu.h>
#include <k4ainternal/depth_mcu.h>
#include <k4ainternal/calibration.h>
#include <k4ainternal/capturesync.h>
#include <k4ainternal/transformation.h>
#include <k4ainternal/logging.h>
#include <azure_c_shared_utility/tickcounter.h>
// System dependencies
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
char K4A_ENV_VAR_LOG_TO_A_FILE[] = K4A_ENABLE_LOG_TO_A_FILE;
typedef struct _k4a_context_t
{
TICK_COUNTER_HANDLE tick_handle;
calibration_t calibration;
depthmcu_t depthmcu;
colormcu_t colormcu;
capturesync_t capturesync;
imu_t imu;
color_t color;
depth_t depth;
bool depth_started;
bool color_started;
bool imu_started;
} k4a_context_t;
K4A_DECLARE_CONTEXT(k4a_device_t, k4a_context_t);
#define DEPTH_CAPTURE (false)
#define COLOR_CAPTURE (true)
#define TRANSFORM_ENABLE_GPU_OPTIMIZATION (true)
#define K4A_DEPTH_MODE_TO_STRING_CASE(depth_mode) \
case depth_mode: \
return #depth_mode
#define K4A_COLOR_RESOLUTION_TO_STRING_CASE(color_resolution) \
case color_resolution: \
return #color_resolution
#define K4A_IMAGE_FORMAT_TO_STRING_CASE(image_format) \
case image_format: \
return #image_format
#define K4A_FPS_TO_STRING_CASE(fps) \
case fps: \
return #fps
uint32_t k4a_device_get_installed_count(void)
{
uint32_t device_count = 0;
usb_cmd_get_device_count(&device_count);
return device_count;
}
k4a_result_t k4a_set_debug_message_handler(k4a_logging_message_cb_t *message_cb,
void *message_cb_context,
k4a_log_level_t min_level)
{
return logger_register_message_callback(message_cb, message_cb_context, min_level);
}
k4a_result_t k4a_set_allocator(k4a_memory_allocate_cb_t allocate, k4a_memory_destroy_cb_t free)
{
return allocator_set_allocator(allocate, free);
}
depth_cb_streaming_capture_t depth_capture_ready;
color_cb_streaming_capture_t color_capture_ready;
void depth_capture_ready(k4a_result_t result, k4a_capture_t capture_handle, void *callback_context)
{
k4a_device_t device_handle = (k4a_device_t)callback_context;
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
capturesync_add_capture(device->capturesync, result, capture_handle, DEPTH_CAPTURE);
}
void color_capture_ready(k4a_result_t result, k4a_capture_t capture_handle, void *callback_context)
{
k4a_device_t device_handle = (k4a_device_t)callback_context;
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
capturesync_add_capture(device->capturesync, result, capture_handle, COLOR_CAPTURE);
}
k4a_result_t k4a_device_open(uint32_t index, k4a_device_t *device_handle)
{
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, device_handle == NULL);
k4a_context_t *device = NULL;
k4a_result_t result = K4A_RESULT_SUCCEEDED;
k4a_device_t handle = NULL;
const guid_t *container_id = NULL;
char serial_number[MAX_SERIAL_NUMBER_LENGTH];
size_t serial_number_size = sizeof(serial_number);
allocator_initialize();
device = k4a_device_t_create(&handle);
result = K4A_RESULT_FROM_BOOL(device != NULL);
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL((device->tick_handle = tickcounter_create()) != NULL);
}
// Create MCU modules
if (K4A_SUCCEEDED(result))
{
// This will block until the depth process is ready to receive commands
result = TRACE_CALL(depthmcu_create(index, &device->depthmcu));
}
if (K4A_SUCCEEDED(result))
{
result = K4A_RESULT_FROM_BOOL((container_id = depthmcu_get_container_id(device->depthmcu)) != NULL);
}
if (K4A_SUCCEEDED(result))
{
if (TRACE_BUFFER_CALL(depthmcu_get_serialnum(device->depthmcu, serial_number, &serial_number_size) !=
K4A_BUFFER_RESULT_SUCCEEDED))
{
result = K4A_RESULT_FAILED;
}
}
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(colormcu_create(container_id, &device->colormcu));
}
// Create calibration module - ensure we can read calibration before proceeding
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(calibration_create(device->depthmcu, &device->calibration));
}
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(capturesync_create(&device->capturesync));
}
// Open Depth Module
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(
depth_create(device->depthmcu, device->calibration, depth_capture_ready, handle, &device->depth));
}
// Create color Module
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(color_create(
device->tick_handle, container_id, serial_number, color_capture_ready, handle, &device->color));
}
// Create imu Module
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(imu_create(device->tick_handle, device->colormcu, device->calibration, &device->imu));
}
if (K4A_FAILED(result))
{
k4a_device_close(handle);
handle = NULL;
}
else
{
*device_handle = handle;
}
return result;
}
void k4a_device_close(k4a_device_t device_handle)
{
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
if (device->capturesync)
{
// Stop capturesync first so that imu, depth, and color can destroy cleanly
capturesync_stop(device->capturesync);
}
// Destroy modules in the reverse order they were created
if (device->imu)
{
imu_destroy(device->imu);
device->imu = NULL;
}
if (device->color)
{
color_destroy(device->color);
device->color = NULL;
}
if (device->depth)
{
depth_destroy(device->depth);
device->depth = NULL;
}
// depth & color call into capturesync, so they need to be destroyed first.
if (device->capturesync)
{
capturesync_destroy(device->capturesync);
device->capturesync = NULL;
}
// calibration rely's on depthmcu, so it needs to be destroyed first.
if (device->calibration)
{
calibration_destroy(device->calibration);
device->calibration = NULL;
}
if (device->depthmcu)
{
depthmcu_destroy(device->depthmcu);
device->depthmcu = NULL;
}
if (device->colormcu)
{
colormcu_destroy(device->colormcu);
device->colormcu = NULL;
}
if (device->tick_handle)
{
tickcounter_destroy(device->tick_handle);
device->tick_handle = NULL;
}
k4a_device_t_destroy(device_handle);
allocator_deinitialize();
}
k4a_wait_result_t k4a_device_get_capture(k4a_device_t device_handle,
k4a_capture_t *capture_handle,
int32_t timeout_in_ms)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_WAIT_RESULT_FAILED, k4a_device_t, device_handle);
RETURN_VALUE_IF_ARG(K4A_WAIT_RESULT_FAILED, capture_handle == NULL);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_WAIT_CALL(capturesync_get_capture(device->capturesync, capture_handle, timeout_in_ms));
}
k4a_wait_result_t k4a_device_get_imu_sample(k4a_device_t device_handle,
k4a_imu_sample_t *imu_sample,
int32_t timeout_in_ms)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_WAIT_RESULT_FAILED, k4a_device_t, device_handle);
RETURN_VALUE_IF_ARG(K4A_WAIT_RESULT_FAILED, imu_sample == NULL);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_WAIT_CALL(imu_get_sample(device->imu, imu_sample, timeout_in_ms));
}
k4a_result_t k4a_device_start_imu(k4a_device_t device_handle)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_device_t, device_handle);
k4a_result_t result = K4A_RESULT_SUCCEEDED;
k4a_context_t *device = k4a_device_t_get_context(device_handle);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, device->imu_started == true);
if (device->depth_started == false && device->color_started == false)
{
// Color camera resets the IMU timestamp so we avoid letting the IMU run without the camera already running.
LOG_ERROR("k4a_device_start_imu called while the color/depth camera is not running is not supported", 0);
result = K4A_RESULT_FAILED;
}
if (K4A_SUCCEEDED(result))
{
LOG_TRACE("k4a_device_start_imu starting", 0);
result = TRACE_CALL(imu_start(device->imu, color_get_sensor_start_time_tick(device->color)));
}
if (K4A_SUCCEEDED(result))
{
device->imu_started = true;
}
if (K4A_FAILED(result) && device->imu_started == true)
{
k4a_device_stop_imu(device_handle);
}
LOG_INFO("k4a_device_start_imu started", 0);
return result;
}
void k4a_device_stop_imu(k4a_device_t device_handle)
{
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
LOG_INFO("k4a_device_stop_imu stopping", 0);
if (device->imu)
{
imu_stop(device->imu);
device->imu_started = false;
}
LOG_TRACE("k4a_device_stop_imu stopped", 0);
}
k4a_result_t k4a_capture_create(k4a_capture_t *capture_handle)
{
return capture_create(capture_handle);
}
void k4a_capture_release(k4a_capture_t capture_handle)
{
capture_dec_ref(capture_handle);
}
void k4a_capture_reference(k4a_capture_t capture_handle)
{
capture_inc_ref(capture_handle);
}
float k4a_capture_get_temperature_c(k4a_capture_t capture_handle)
{
return capture_get_temperature_c(capture_handle);
}
k4a_image_t k4a_capture_get_color_image(k4a_capture_t capture_handle)
{
return capture_get_color_image(capture_handle);
}
k4a_image_t k4a_capture_get_depth_image(k4a_capture_t capture_handle)
{
return capture_get_depth_image(capture_handle);
}
k4a_image_t k4a_capture_get_ir_image(k4a_capture_t capture_handle)
{
return capture_get_ir_image(capture_handle);
}
void k4a_capture_set_color_image(k4a_capture_t capture_handle, k4a_image_t image_handle)
{
capture_set_color_image(capture_handle, image_handle);
}
void k4a_capture_set_depth_image(k4a_capture_t capture_handle, k4a_image_t image_handle)
{
capture_set_depth_image(capture_handle, image_handle);
}
void k4a_capture_set_ir_image(k4a_capture_t capture_handle, k4a_image_t image_handle)
{
capture_set_ir_image(capture_handle, image_handle);
}
void k4a_capture_set_temperature_c(k4a_capture_t capture_handle, float temperature_c)
{
capture_set_temperature_c(capture_handle, temperature_c);
}
k4a_result_t k4a_image_create(k4a_image_format_t format,
int width_pixels,
int height_pixels,
int stride_bytes,
k4a_image_t *image_handle)
{
return image_create(format, width_pixels, height_pixels, stride_bytes, ALLOCATION_SOURCE_USER, image_handle);
}
k4a_result_t k4a_image_create_from_buffer(k4a_image_format_t format,
int width_pixels,
int height_pixels,
int stride_bytes,
uint8_t *buffer,
size_t buffer_size,
k4a_memory_destroy_cb_t *buffer_release_cb,
void *buffer_release_cb_context,
k4a_image_t *image_handle)
{
return image_create_from_buffer(format,
width_pixels,
height_pixels,
stride_bytes,
buffer,
buffer_size,
buffer_release_cb,
buffer_release_cb_context,
image_handle);
}
uint8_t *k4a_image_get_buffer(k4a_image_t image_handle)
{
return image_get_buffer(image_handle);
}
size_t k4a_image_get_size(k4a_image_t image_handle)
{
return image_get_size(image_handle);
}
k4a_image_format_t k4a_image_get_format(k4a_image_t image_handle)
{
return image_get_format(image_handle);
}
int k4a_image_get_width_pixels(k4a_image_t image_handle)
{
return image_get_width_pixels(image_handle);
}
int k4a_image_get_height_pixels(k4a_image_t image_handle)
{
return image_get_height_pixels(image_handle);
}
int k4a_image_get_stride_bytes(k4a_image_t image_handle)
{
return image_get_stride_bytes(image_handle);
}
// Deprecated
uint64_t k4a_image_get_timestamp_usec(k4a_image_t image_handle)
{
return image_get_device_timestamp_usec(image_handle);
}
uint64_t k4a_image_get_device_timestamp_usec(k4a_image_t image_handle)
{
return image_get_device_timestamp_usec(image_handle);
}
uint64_t k4a_image_get_system_timestamp_nsec(k4a_image_t image_handle)
{
return image_get_system_timestamp_nsec(image_handle);
}
uint64_t k4a_image_get_exposure_usec(k4a_image_t image_handle)
{
return image_get_exposure_usec(image_handle);
}
uint32_t k4a_image_get_white_balance(k4a_image_t image_handle)
{
return image_get_white_balance(image_handle);
}
uint32_t k4a_image_get_iso_speed(k4a_image_t image_handle)
{
return image_get_iso_speed(image_handle);
}
void k4a_image_set_device_timestamp_usec(k4a_image_t image_handle, uint64_t timestamp_usec)
{
image_set_device_timestamp_usec(image_handle, timestamp_usec);
}
// Deprecated
void k4a_image_set_timestamp_usec(k4a_image_t image_handle, uint64_t timestamp_usec)
{
image_set_device_timestamp_usec(image_handle, timestamp_usec);
}
void k4a_image_set_system_timestamp_nsec(k4a_image_t image_handle, uint64_t timestamp_nsec)
{
image_set_system_timestamp_nsec(image_handle, timestamp_nsec);
}
// Deprecated
void k4a_image_set_exposure_time_usec(k4a_image_t image_handle, uint64_t exposure_usec)
{
image_set_exposure_usec(image_handle, exposure_usec);
}
void k4a_image_set_exposure_usec(k4a_image_t image_handle, uint64_t exposure_usec)
{
image_set_exposure_usec(image_handle, exposure_usec);
}
void k4a_image_set_white_balance(k4a_image_t image_handle, uint32_t white_balance)
{
image_set_white_balance(image_handle, white_balance);
}
void k4a_image_set_iso_speed(k4a_image_t image_handle, uint32_t iso_speed)
{
image_set_iso_speed(image_handle, iso_speed);
}
void k4a_image_reference(k4a_image_t image_handle)
{
image_inc_ref(image_handle);
}
void k4a_image_release(k4a_image_t image_handle)
{
image_dec_ref(image_handle);
}
static const char *k4a_depth_mode_to_string(k4a_depth_mode_t depth_mode)
{
switch (depth_mode)
{
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_OFF);
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_NFOV_2X2BINNED);
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_NFOV_UNBINNED);
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_WFOV_2X2BINNED);
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_WFOV_UNBINNED);
K4A_DEPTH_MODE_TO_STRING_CASE(K4A_DEPTH_MODE_PASSIVE_IR);
}
return "Unexpected k4a_depth_mode_t value.";
}
static const char *k4a_color_resolution_to_string(k4a_color_resolution_t resolution)
{
switch (resolution)
{
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_OFF);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_720P);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_1080P);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_1440P);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_1536P);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_2160P);
K4A_COLOR_RESOLUTION_TO_STRING_CASE(K4A_COLOR_RESOLUTION_3072P);
}
return "Unexpected k4a_color_resolution_t value.";
}
static const char *k4a_image_format_to_string(k4a_image_format_t image_format)
{
switch (image_format)
{
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_COLOR_MJPG);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_COLOR_NV12);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_COLOR_YUY2);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_COLOR_BGRA32);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_DEPTH16);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_IR16);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_CUSTOM8);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_CUSTOM16);
K4A_IMAGE_FORMAT_TO_STRING_CASE(K4A_IMAGE_FORMAT_CUSTOM);
}
return "Unexpected k4a_image_format_t value.";
}
static const char *k4a_fps_to_string(k4a_fps_t fps)
{
switch (fps)
{
K4A_FPS_TO_STRING_CASE(K4A_FRAMES_PER_SECOND_5);
K4A_FPS_TO_STRING_CASE(K4A_FRAMES_PER_SECOND_15);
K4A_FPS_TO_STRING_CASE(K4A_FRAMES_PER_SECOND_30);
}
return "Unexpected k4a_fps_t value.";
}
static k4a_result_t validate_configuration(k4a_context_t *device, const k4a_device_configuration_t *config)
{
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, config == NULL);
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, device == NULL);
k4a_result_t result = K4A_RESULT_SUCCEEDED;
bool depth_enabled = false;
bool color_enabled = false;
if (config->color_format != K4A_IMAGE_FORMAT_COLOR_MJPG && config->color_format != K4A_IMAGE_FORMAT_COLOR_YUY2 &&
config->color_format != K4A_IMAGE_FORMAT_COLOR_NV12 && config->color_format != K4A_IMAGE_FORMAT_COLOR_BGRA32)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured color_format is not a valid k4a_color_format_t value.", 0);
}
if (K4A_SUCCEEDED(result))
{
if (config->color_resolution < K4A_COLOR_RESOLUTION_OFF ||
config->color_resolution > K4A_COLOR_RESOLUTION_3072P)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured color_resolution is not a valid k4a_color_resolution_t value.", 0);
}
}
if (K4A_SUCCEEDED(result))
{
if (config->depth_mode < K4A_DEPTH_MODE_OFF || config->depth_mode > K4A_DEPTH_MODE_PASSIVE_IR)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured depth_mode is not a valid k4a_depth_mode_t value.", 0);
}
}
if (K4A_SUCCEEDED(result))
{
if (config->camera_fps != K4A_FRAMES_PER_SECOND_5 && config->camera_fps != K4A_FRAMES_PER_SECOND_15 &&
config->camera_fps != K4A_FRAMES_PER_SECOND_30)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured camera_fps is not a valid k4a_fps_t value.", 0);
}
}
if (K4A_SUCCEEDED(result))
{
if (config->wired_sync_mode < K4A_WIRED_SYNC_MODE_STANDALONE ||
config->wired_sync_mode > K4A_WIRED_SYNC_MODE_SUBORDINATE)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured wired_sync_mode is not a valid k4a_wired_sync_mode_t value.", 0);
}
}
if (K4A_SUCCEEDED(result))
{
if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE ||
config->wired_sync_mode == K4A_WIRED_SYNC_MODE_MASTER)
{
bool sync_in_cable_present;
bool sync_out_cable_present;
result = colormcu_get_external_sync_jack_state(device->colormcu,
&sync_in_cable_present,
&sync_out_cable_present);
if (K4A_SUCCEEDED(result))
{
if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE && !sync_in_cable_present)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("Failure to detect presence of sync in cable with wired sync mode "
"K4A_WIRED_SYNC_MODE_SUBORDINATE.",
0);
}
if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_MASTER)
{
if (!sync_out_cable_present)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("Failure to detect presence of sync out cable with wired sync mode "
"K4A_WIRED_SYNC_MODE_MASTER.",
0);
}
if (config->color_resolution == K4A_COLOR_RESOLUTION_OFF)
{
result = K4A_RESULT_FAILED;
LOG_ERROR(
"Device wired_sync_mode is set to K4A_WIRED_SYNC_MODE_MASTER, so color camera must be used "
"on master device. Color_resolution can not be set to K4A_COLOR_RESOLUTION_OFF.",
0);
}
}
}
}
}
if (K4A_SUCCEEDED(result))
{
if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE &&
config->subordinate_delay_off_master_usec != 0)
{
uint32_t fps_in_usec = HZ_TO_PERIOD_US(k4a_convert_fps_to_uint(config->camera_fps));
if (config->subordinate_delay_off_master_usec > fps_in_usec)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured subordinate device delay from the master device cannot exceed one frame "
"interval of %d. User requested %d",
fps_in_usec,
config->subordinate_delay_off_master_usec);
}
}
if (config->wired_sync_mode != K4A_WIRED_SYNC_MODE_SUBORDINATE &&
config->subordinate_delay_off_master_usec != 0)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("When wired_sync_mode is K4A_WIRED_SYNC_MODE_STANDALONE or K4A_WIRED_SYNC_MODE_MASTER, the "
"subordinate_delay_off_master_usec must be 0.",
0);
}
}
if (K4A_SUCCEEDED(result))
{
if (config->depth_mode != K4A_DEPTH_MODE_OFF)
{
depth_enabled = true;
}
if (config->color_resolution != K4A_COLOR_RESOLUTION_OFF)
{
color_enabled = true;
}
if (depth_enabled && color_enabled)
{
int64_t fps = HZ_TO_PERIOD_US(k4a_convert_fps_to_uint(config->camera_fps));
if (config->depth_delay_off_color_usec < -fps || config->depth_delay_off_color_usec > fps)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured depth_delay_off_color_usec must be within +/- one frame interval of %d. User "
"requested %d",
fps,
config->depth_delay_off_color_usec);
}
}
else if (!depth_enabled && !color_enabled)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("Neither depth camera nor color camera are enabled in the configuration, at least one needs to "
"be enabled.",
0);
}
else
{
if (config->depth_delay_off_color_usec != 0)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("If depth_delay_off_color_usec is not 0, both depth camera and color camera must be enabled.",
0);
}
if (config->synchronized_images_only)
{
result = K4A_RESULT_FAILED;
LOG_ERROR(
"To enable synchronized_images_only, both depth camera and color camera must also be enabled.", 0);
}
}
}
if (K4A_SUCCEEDED(result))
{
if (depth_enabled)
{
struct _depth_configuration
{
k4a_depth_mode_t mode;
k4a_fps_t max_fps;
} supported_depth_configs[] = {
{ K4A_DEPTH_MODE_NFOV_2X2BINNED, K4A_FRAMES_PER_SECOND_30 },
{ K4A_DEPTH_MODE_NFOV_UNBINNED, K4A_FRAMES_PER_SECOND_30 },
{ K4A_DEPTH_MODE_WFOV_2X2BINNED, K4A_FRAMES_PER_SECOND_30 },
{ K4A_DEPTH_MODE_WFOV_UNBINNED, K4A_FRAMES_PER_SECOND_15 },
{ K4A_DEPTH_MODE_PASSIVE_IR, K4A_FRAMES_PER_SECOND_30 },
};
bool depth_fps_and_mode_supported = false;
for (unsigned int x = 0; x < COUNTOF(supported_depth_configs); x++)
{
if (supported_depth_configs[x].mode == config->depth_mode &&
supported_depth_configs[x].max_fps >= config->camera_fps)
{
depth_fps_and_mode_supported = true;
break;
}
}
if (!depth_fps_and_mode_supported)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The configured depth_mode %s does not support the configured camera_fps %s.",
k4a_depth_mode_to_string(config->depth_mode),
k4a_fps_to_string(config->camera_fps));
}
}
}
if (K4A_SUCCEEDED(result))
{
if (color_enabled)
{
struct _color_configuration
{
k4a_color_resolution_t res;
k4a_image_format_t format;
k4a_fps_t max_fps;
} supported_color_configs[] = {
{ K4A_COLOR_RESOLUTION_2160P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_1440P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_1080P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_YUY2, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_NV12, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_3072P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_15 },
{ K4A_COLOR_RESOLUTION_1536P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_2160P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_1440P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_1080P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
{ K4A_COLOR_RESOLUTION_3072P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_15 },
{ K4A_COLOR_RESOLUTION_1536P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
};
bool color_fps_and_res_and_format_supported = false;
for (unsigned int x = 0; x < COUNTOF(supported_color_configs); x++)
{
if (supported_color_configs[x].res == config->color_resolution &&
supported_color_configs[x].max_fps >= config->camera_fps &&
supported_color_configs[x].format == config->color_format)
{
color_fps_and_res_and_format_supported = true;
break;
}
}
if (!color_fps_and_res_and_format_supported)
{
result = K4A_RESULT_FAILED;
LOG_ERROR("The combination of color_resolution at %s, color_format at %s, and camera_fps at %s is not "
"supported.",
k4a_color_resolution_to_string(config->color_resolution),
k4a_image_format_to_string(config->color_format),
k4a_fps_to_string(config->camera_fps));
}
}
}
return result;
}
k4a_result_t k4a_device_start_cameras(k4a_device_t device_handle, const k4a_device_configuration_t *config)
{
RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, config == NULL);
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_device_t, device_handle);
k4a_result_t result = K4A_RESULT_SUCCEEDED;
k4a_context_t *device = k4a_device_t_get_context(device_handle);
LOG_TRACE("k4a_device_start_cameras starting", 0);
if (device->depth_started == true || device->color_started == true)
{
LOG_ERROR("k4a_device_start_cameras called while one of the sensors are running, depth:%d color:%d",
device->depth_started,
device->color_started);
result = K4A_RESULT_FAILED;
}
if (device->imu_started == true)
{
// Color camera resets the IMU timestamp so we avoid that condition.
LOG_ERROR("k4a_device_start_cameras called while the IMU is running is not supported, stop the IMU", 0);
result = K4A_RESULT_FAILED;
}
if (K4A_SUCCEEDED(result))
{
LOG_INFO("Starting camera's with the following config.", 0);
LOG_INFO(" color_format:%d", config->color_format);
LOG_INFO(" color_resolution:%d", config->color_resolution);
LOG_INFO(" depth_mode:%d", config->depth_mode);
LOG_INFO(" camera_fps:%d", config->camera_fps);
LOG_INFO(" synchronized_images_only:%d", config->synchronized_images_only);
LOG_INFO(" depth_delay_off_color_usec:%d", config->depth_delay_off_color_usec);
LOG_INFO(" wired_sync_mode:%d", config->wired_sync_mode);
LOG_INFO(" subordinate_delay_off_master_usec:%d", config->subordinate_delay_off_master_usec);
LOG_INFO(" disable_streaming_indicator:%d", config->disable_streaming_indicator);
result = TRACE_CALL(validate_configuration(device, config));
}
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(colormcu_set_multi_device_mode(device->colormcu, config));
}
if (K4A_SUCCEEDED(result))
{
result = TRACE_CALL(capturesync_start(device->capturesync, config));
}
if (K4A_SUCCEEDED(result))
{
if (config->depth_mode != K4A_DEPTH_MODE_OFF)
{
result = TRACE_CALL(depth_start(device->depth, config));
}
if (K4A_SUCCEEDED(result))
{
device->depth_started = true;
}
}
if (K4A_SUCCEEDED(result))
{
if (config->color_resolution != K4A_COLOR_RESOLUTION_OFF)
{
// NOTE: Color must be started before depth and IMU as it triggers the sync of PTS. If it starts after
// depth or IMU, the user will see timestamps reset back to zero when the color camera is started.
result = TRACE_CALL(color_start(device->color, config));
}
if (K4A_SUCCEEDED(result))
{
device->color_started = true;
}
}
LOG_INFO("k4a_device_start_cameras started", 0);
if (K4A_FAILED(result))
{
k4a_device_stop_cameras(device_handle);
}
return result;
}
void k4a_device_stop_cameras(k4a_device_t device_handle)
{
RETURN_VALUE_IF_HANDLE_INVALID(VOID_VALUE, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
LOG_INFO("k4a_device_stop_cameras stopping", 0);
// Capturesync needs to stop before color so that all queues will purged
if (device->capturesync)
{
capturesync_stop(device->capturesync);
}
if (device->depth)
{
depth_stop(device->depth);
device->depth_started = false;
}
if (device->color)
{
// This call will block waiting for all outstanding allocations to be released
color_stop(device->color);
device->color_started = false;
}
LOG_INFO("k4a_device_stop_cameras stopped", 0);
}
k4a_buffer_result_t k4a_device_get_serialnum(k4a_device_t device_handle,
char *serial_number,
size_t *serial_number_size)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_BUFFER_RESULT_FAILED, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_BUFFER_CALL(depth_get_device_serialnum(device->depth, serial_number, serial_number_size));
}
k4a_result_t k4a_device_get_version(k4a_device_t device_handle, k4a_hardware_version_t *version)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_CALL(depth_get_device_version(device->depth, version));
}
k4a_result_t k4a_device_get_sync_jack(k4a_device_t device_handle,
bool *sync_in_jack_connected,
bool *sync_out_jack_connected)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_CALL(
colormcu_get_external_sync_jack_state(device->colormcu, sync_in_jack_connected, sync_out_jack_connected));
}
k4a_result_t k4a_device_get_color_control_capabilities(k4a_device_t device_handle,
k4a_color_control_command_t command,
bool *supports_auto,
int32_t *min_value,
int32_t *max_value,
int32_t *step_value,
int32_t *default_value,
k4a_color_control_mode_t *default_mode)
{
RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_device_t, device_handle);
k4a_context_t *device = k4a_device_t_get_context(device_handle);
return TRACE_CALL(color_get_control_capabilities(
device->color, command, supports_auto, min_value, max_value, step_value, default_value, default_mode));
}
k4a_result_t k4a_device_get_color_control(k4a_device_t device_handle,