orbbec-sdk-sys 0.1.2+2.5.5

Low-level Rust bindings for https://github.com/orbbec/OrbbecSDK_v2
Documentation
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
// Copyright (c) Orbbec Inc. All Rights Reserved.
// Licensed under the MIT License.

#pragma once
#include <stdint.h>
#include <algorithm>
#include <set>

#include "IFrame.hpp"
#include "G330MetadataTypes.hpp"
#include "exception/ObException.hpp"
#include "logger/LoggerInterval.hpp"
#include "utils/Utils.hpp"
#include "G330FrameTimestampCalculator.hpp"
#include "sensor/video/VideoSensor.hpp"
#include "stream/StreamProfile.hpp"
#include "metadata/FrameMedatadaParser.hpp"

namespace libobsensor {
template <typename T> class G330MetadataTimestampParser : public IFrameMetadataParser {
public:
    G330MetadataTimestampParser(){};
    virtual ~G330MetadataTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain timestamp!");
            return -1;
        }
        auto md = reinterpret_cast<const T *>(metadata);
        return (int64_t)md->timestamp_sof_sec * 1000000 + md->timestamp_sof_nsec / 1000 - md->timestamp_offset_usec;
    }

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        (void)metadata;
        return dataSize >= sizeof(T);
    }
};

// for depth and ir sensor
class G330MetadataSensorTimestampParser : public IFrameMetadataParser {
public:
    G330MetadataSensorTimestampParser(){};
    explicit G330MetadataSensorTimestampParser(FrameMetadataModifier exp_to_usec) : exp_to_usec_(exp_to_usec){};
    virtual ~G330MetadataSensorTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain timestamp!");
            return -1;
        }
        auto md          = reinterpret_cast<const G330CommonUvcMetadata *>(metadata);
        auto exp_in_usec = exp_to_usec_ ? exp_to_usec_(md->exposure) : md->exposure;
        return (int64_t)md->timestamp_sof_sec * 1000000 + md->timestamp_sof_nsec / 1000 - md->timestamp_offset_usec - exp_in_usec / 2;
    }

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        (void)metadata;
        return dataSize >= sizeof(G330CommonUvcMetadata);
    }

private:
    FrameMetadataModifier exp_to_usec_ = nullptr;
};

class G330ColorMetadataSensorTimestampParser : public IFrameMetadataParser {
public:
    G330ColorMetadataSensorTimestampParser(){};
    explicit G330ColorMetadataSensorTimestampParser(FrameMetadataModifier exp_to_usec) : exp_to_usec_(exp_to_usec){};
    virtual ~G330ColorMetadataSensorTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain timestamp!");
            return -1;
        }
        auto md = reinterpret_cast<const G330ColorUvcMetadata *>(metadata);
        // auto exp_in_usec = exp_to_usec_ ? exp_to_usec_(md->exposure) : md->exposure;
        return (int64_t)md->timestamp_sof_sec * 1000000 + md->timestamp_sof_nsec / 1000 - md->timestamp_offset_usec + md->sensor_timestamp_offset_usec;
    }

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        utils::unusedVar(metadata);
        return dataSize >= sizeof(G330CommonUvcMetadata);
    }

private:
    FrameMetadataModifier exp_to_usec_ = nullptr;
};

class G330ScrMetadataParserBase : public IFrameMetadataParser {
public:
    G330ScrMetadataParserBase(){};
    virtual ~G330ScrMetadataParserBase() noexcept override = default;

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        utils::unusedVar(metadata);
        return dataSize >= sizeof(StandardUvcFramePayloadHeader::scrSourceClock);
    }
};

class G330PayloadHeadMetadataTimestampParser : public G330ScrMetadataParserBase {
public:
    G330PayloadHeadMetadataTimestampParser(IDevice *device, uint64_t deviceTimeFreq, uint64_t frameTimeFreq)
        : device_(device), deviceTimeFreq_(deviceTimeFreq), frameTimeFreq_(frameTimeFreq) {
        timestampCalculator_ = std::make_shared<G330FrameTimestampCalculatorBaseDeviceTime>(device, deviceTimeFreq_, frameTimeFreq_);
    }
    virtual ~G330PayloadHeadMetadataTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain timestamp!");
            return -1;
        }
        auto     standardUvcMetadata  = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint64_t presentationTime     = standardUvcMetadata.dwPresentationTime;
        uint64_t transformedTimestamp = ((presentationTime >> 24) & 0xFF) * 1000000 + ((presentationTime & 0xFFFFFF) << 8) / 1000;
        auto     calculatedTimestamp  = timestampCalculator_->calculate(transformedTimestamp);

        return calculatedTimestamp;
    }

private:
    IDevice *device_;

    uint64_t deviceTimeFreq_;

    uint64_t frameTimeFreq_;

    std::shared_ptr<G330FrameTimestampCalculatorBaseDeviceTime> timestampCalculator_;
};

class G330PayloadHeadMetadataColorSensorTimestampParser : public G330PayloadHeadMetadataTimestampParser {
public:
    G330PayloadHeadMetadataColorSensorTimestampParser(IDevice *device, uint64_t deviceTimeFreq, uint64_t frameTimeFreq)
        : G330PayloadHeadMetadataTimestampParser(device, deviceTimeFreq, frameTimeFreq) {}
    virtual ~G330PayloadHeadMetadataColorSensorTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain color sensor timestamp!");
            return -1;
        }

        auto calculatedTimestamp = G330PayloadHeadMetadataTimestampParser::getValue(metadata, dataSize);
        // get frame offset,unit 100us
        auto    standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint16_t rawValue            = (((standardUvcMetadata.scrSourceClock[1] & 0xF8) >> 3) | ((standardUvcMetadata.scrSourceClock[2] & 0x7F) << 5));
        int32_t  frameOffset         = 0;
        // 12bit offset value check sign bit
        if(rawValue & 0x800) {
            rawValue    = ((~rawValue) & 0xFFF) + 1;
            frameOffset = (-static_cast<int32_t>(rawValue) * 100);
        }
        else {
            frameOffset = (static_cast<int32_t>(rawValue) * 100);
        }

        calculatedTimestamp += frameOffset;

        return calculatedTimestamp;
    }
};

class G330ColorScrMetadataExposureParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain color exposure!");
            return -1;
        }
        auto     standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint32_t exposure            = (standardUvcMetadata.scrSourceClock[0] | ((standardUvcMetadata.scrSourceClock[1] & 0x07) << 8));

        return static_cast<int64_t>(exposure);
    }
};

class G330ColorScrMetadataActualFrameRateParser : public G330ColorScrMetadataExposureParser {
public:
    G330ColorScrMetadataActualFrameRateParser(IDevice *device) : device_(device) {}
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain color actual frame rate!");
            return -1;
        }
        auto exposure = G330ColorScrMetadataExposureParser::getValue(metadata, dataSize) * 100;  // color exposure unit 100us
        auto fps      = static_cast<uint32_t>(1000000.f / exposure);

        auto colorSensor              = device_->getComponentT<VideoSensor>(OB_DEV_COMPONENT_COLOR_SENSOR);
        auto colorActiveStreamProfile = colorSensor->getActivatedStreamProfile();
        if(!colorActiveStreamProfile) {
            return -1;
        }
        auto colorCurrentStreamProfileFps = colorActiveStreamProfile->as<VideoStreamProfile>()->getFps();
        return static_cast<int64_t>((std::min)(fps, colorCurrentStreamProfileFps));
    }

private:
    IDevice *device_;
};

class G330ColorScrMetadataGainParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain color gain!");
            return -1;
        }
        auto     standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint16_t gain                = standardUvcMetadata.scrSourceClock[3];

        return static_cast<int64_t>(gain);
    }
};

class G330PayloadHeadMetadataDepthSensorTimestampParser : public G330PayloadHeadMetadataTimestampParser {
public:
    G330PayloadHeadMetadataDepthSensorTimestampParser(IDevice *device, uint64_t deviceTimeFreq, uint64_t frameTimeFreq)
        : G330PayloadHeadMetadataTimestampParser(device, deviceTimeFreq, frameTimeFreq) {}
    virtual ~G330PayloadHeadMetadataDepthSensorTimestampParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain color sensor timestamp!");
            return -1;
        }

        auto calculatedTimestamp = G330PayloadHeadMetadataTimestampParser::getValue(metadata, dataSize);
        // get depth exposure,unit 1us
        auto     standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint32_t exposure =
            standardUvcMetadata.scrSourceClock[0] | (standardUvcMetadata.scrSourceClock[1] << 8) | ((standardUvcMetadata.scrSourceClock[2] & 0x03) << 16);
        calculatedTimestamp = calculatedTimestamp - (exposure / 2);

        return calculatedTimestamp;
    }
};

class G330DepthScrMetadataExposureParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain depth exposure!");
            return -1;
        }
        auto     standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint32_t exposure =
            standardUvcMetadata.scrSourceClock[0] | (standardUvcMetadata.scrSourceClock[1] << 8) | ((standardUvcMetadata.scrSourceClock[2] & 0x03) << 16);

        return static_cast<int64_t>(exposure);
    }
};

class G330DepthScrMetadataActualFrameRateParser : public G330DepthScrMetadataExposureParser {
public:
    G330DepthScrMetadataActualFrameRateParser(IDevice *device) : device_(device) {}
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain depth actual frame rate!");
            return -1;
        }
        auto exposure = G330DepthScrMetadataExposureParser::getValue(metadata, dataSize);
        auto fps      = static_cast<uint32_t>(1000000.f / exposure);

        static std::vector<DeviceComponentId> sensorComponentIds = { OB_DEV_COMPONENT_DEPTH_SENSOR, OB_DEV_COMPONENT_LEFT_IR_SENSOR,
                                                                     OB_DEV_COMPONENT_RIGHT_IR_SENSOR };

        StreamProfileList streamProfileList;
        uint32_t          activatedStreamProfileFps = 0;
        for(const auto &id: sensorComponentIds) {
            auto sensor                   = device_->getComponentT<VideoSensor>(id);
            auto currentStreamProfileList = sensor->getStreamProfileList();
            auto activeStreamProfile      = sensor->getActivatedStreamProfile();
            if(!activeStreamProfile) {
                continue;
            }
            else {
                streamProfileList         = currentStreamProfileList;
                activatedStreamProfileFps = activeStreamProfile->as<VideoStreamProfile>()->getFps();
                break;
            }
        }

        if(activatedStreamProfileFps == 0) {
            return -1;
        }

        std::vector<uint32_t> currentStreamProfileFpsVector;
        for(const auto &profile: streamProfileList) {
            auto videoStreamProfile = profile->as<VideoStreamProfile>();
            if(!videoStreamProfile) {
                continue;
            }

            auto curFps = videoStreamProfile->getFps();
            if(std::find(currentStreamProfileFpsVector.begin(), currentStreamProfileFpsVector.end(), curFps) != currentStreamProfileFpsVector.end()) {
                continue;
            }

            currentStreamProfileFpsVector.push_back(curFps);
        }
        std::sort(currentStreamProfileFpsVector.begin(), currentStreamProfileFpsVector.end());

        if(fps >= currentStreamProfileFpsVector.back()) {
            fps = currentStreamProfileFpsVector.back();
        }
        else {
            for(size_t i = 0; i < currentStreamProfileFpsVector.size() - 1; i++) {
                if(fps < currentStreamProfileFpsVector[i + 1]) {
                    fps = currentStreamProfileFpsVector[i];
                    break;
                }
            }
        }

        return static_cast<int64_t>((std::min)(fps, activatedStreamProfileFps));
    }

private:
    IDevice *device_;
};

class G330DepthScrMetadataLaserStatusParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain laser status!");
            return -1;
        }
        auto    standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint8_t laserStatus         = (standardUvcMetadata.scrSourceClock[2] & 0x04) >> 2;
        return static_cast<int64_t>(laserStatus);
    }
};

class G330DepthScrMetadataLaserPowerLevelParser : public G330ScrMetadataParserBase {
public:
    G330DepthScrMetadataLaserPowerLevelParser(FrameMetadataModifier modifier = nullptr) : modifier_(modifier) {}
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain laser power level!");
            return -1;
        }
        auto    standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        int64_t laserPowerLevel     = (standardUvcMetadata.scrSourceClock[2] & 0x38) >> 3;
        if(modifier_) {
            laserPowerLevel = modifier_(laserPowerLevel);
        }
        return laserPowerLevel;
    }

private:
    FrameMetadataModifier modifier_;
};

class G330DepthScrMetadataHDRSequenceIDParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain hdr sequence id!");
            return -1;
        }
        auto    standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint8_t hdrSequenceId       = (standardUvcMetadata.scrSourceClock[2] & 0x40) >> 6;
        return static_cast<int64_t>(hdrSequenceId);
    }
};

class G330DepthScrMetadataGainParser : public G330ScrMetadataParserBase {
public:
    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            LOG_WARN_INTVL("Current metadata does not contain depth gain!");
            return -1;
        }
        auto    standardUvcMetadata = *(reinterpret_cast<const StandardUvcFramePayloadHeader *>(metadata));
        uint8_t gain                = standardUvcMetadata.scrSourceClock[3];
        return static_cast<int64_t>(gain);
    }
};

class G330MetadataParserBase : public IFrameMetadataParser {
public:
    G330MetadataParserBase(IDevice *device, OBFrameMetadataType type, FrameMetadataModifier modifier,
                           const std::multimap<OBPropertyID, std::vector<OBFrameMetadataType>> metadataTypeIdMap)
        : device_(device), metadataType_(type), data_(0), modifier_(modifier), initPropertyValue_(true) {
        for(const auto &item: metadataTypeIdMap) {
            const std::vector<OBFrameMetadataType> &types = item.second;
            if(std::find(types.begin(), types.end(), type) != types.end()) {
                propertyId_ = item.first;
                break;
            }
        }

        auto propertyServer = device_->getPropertyServer();
        if (propertyServer) {
            propertyServer_ = propertyServer.get();
        }
        if(propertyServer_ && propertyServer_->isPropertySupported(propertyId_, PROP_OP_READ, PROP_ACCESS_INTERNAL)) {
            propertyServer_->registerAccessCallback(
                propertyId_, [this, type](uint32_t propertyId, const uint8_t *data, size_t dataSize, PropertyOperationType operationType) {
                    utils::unusedVar(dataSize);
                    utils::unusedVar(operationType);
                    if(propertyId != static_cast<uint32_t>(propertyId_)) {
                        return;
                    }
                    auto propertyItem   = propertyServer_->getPropertyItem(propertyId_, PROP_ACCESS_USER);
                    if(propertyItem.type == OB_STRUCT_PROPERTY) {
                        data_ = parseStructurePropertyValue(type, propertyId, data);
                    }
                    else {
                        data_ = parsePropertyValue(propertyId, data);
                    }
                });
        }
    }

    virtual ~G330MetadataParserBase() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        if(!isSupported(metadata, dataSize)) {
            return -1;
        }

        // first time get value,should sync property value
        if(initPropertyValue_) {
            PropertyAccessType accessType     = PROP_ACCESS_USER;
            auto               propertyServer = device_->getPropertyServer();
            auto               propertyItem   = propertyServer->getPropertyItem(propertyId_, accessType);
            if(propertyItem.type == OB_STRUCT_PROPERTY) {
                auto structValue = propertyServer->getStructureData(propertyId_, accessType);
                data_            = parseStructurePropertyValue(metadataType_, static_cast<uint32_t>(propertyId_), structValue.data());
            }
            else {
                OBPropertyValue value = {};
                propertyServer->getPropertyValue(propertyId_, &value, accessType);
                void *valueData = nullptr;
                if(propertyItem.type == OB_FLOAT_PROPERTY) {
                    valueData = &value.floatValue;
                }
                else {
                    valueData = &value.intValue;
                }
                data_ = parsePropertyValue(static_cast<uint32_t>(propertyId_), (const uint8_t *)valueData);
            }
            registerSensorStateCallback();
            initPropertyValue_ = false;
        }

        if(modifier_) {
            data_ = modifier_(data_);
        }
        return data_;
    }

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        utils::unusedVar(metadata);
        utils::unusedVar(dataSize);
        return propertyServer_->isPropertySupported(propertyId_, PROP_OP_READ, PROP_ACCESS_USER);
    }

private:
    int64_t parsePropertyValue(uint32_t propertyId, const uint8_t *data) {
        int64_t parsedData     = 0;
        auto    value          = reinterpret_cast<const OBPropertyValue *>(data);
        auto    propertyServer = device_->getPropertyServer();
        auto    propertyItem   = propertyServer->getPropertyItem(propertyId, PROP_ACCESS_USER);
        if(propertyItem.type == OB_INT_PROPERTY || propertyItem.type == OB_BOOL_PROPERTY) {
            parsedData = static_cast<int64_t>(value->intValue);
        }
        else if(propertyItem.type == OB_FLOAT_PROPERTY) {
            parsedData = static_cast<int64_t>(value->floatValue);
        }

        return parsedData;
    }

    int64_t parseStructurePropertyValue(OBFrameMetadataType type, uint32_t propertyId, const uint8_t *data) {
        int64_t parsedData = 0;
        if(propertyId == OB_STRUCT_COLOR_AE_ROI || propertyId == OB_STRUCT_DEPTH_AE_ROI) {
            auto roi = *(reinterpret_cast<const OBRegionOfInterest *>(data));
            if(type == OB_FRAME_METADATA_TYPE_AE_ROI_LEFT) {
                parsedData = static_cast<int64_t>(roi.x0_left);
            }
            else if(type == OB_FRAME_METADATA_TYPE_AE_ROI_RIGHT) {
                parsedData = static_cast<int64_t>(roi.x1_right);
            }
            else if(type == OB_FRAME_METADATA_TYPE_AE_ROI_TOP) {
                parsedData = static_cast<int64_t>(roi.y0_top);
            }
            else if(type == OB_FRAME_METADATA_TYPE_AE_ROI_BOTTOM) {
                parsedData = static_cast<int64_t>(roi.y1_bottom);
            }
        }
        else if(propertyId == OB_STRUCT_DEPTH_HDR_CONFIG) {
            auto hdrConfig = *(reinterpret_cast<const OBHdrConfig *>(data));
            if(type == OB_FRAME_METADATA_TYPE_HDR_SEQUENCE_NAME) {
                parsedData = static_cast<int64_t>(hdrConfig.sequence_name);
            }
            else if(type == OB_FRAME_METADATA_TYPE_HDR_SEQUENCE_SIZE) {
                parsedData = static_cast<int64_t>(hdrConfig.enable);
            }
        }

        return parsedData;
    }

    void registerSensorStateCallback() {
        std::vector<DeviceComponentId> compentIds;
        if(propertyId_ == OB_STRUCT_COLOR_AE_ROI && metadataType_ == OB_FRAME_METADATA_TYPE_AE_ROI_LEFT) {
            compentIds.push_back(OB_DEV_COMPONENT_COLOR_SENSOR);
        }
        else if(propertyId_ == OB_STRUCT_DEPTH_AE_ROI && metadataType_ == OB_FRAME_METADATA_TYPE_AE_ROI_LEFT) {
            compentIds.push_back(OB_DEV_COMPONENT_DEPTH_SENSOR);
            compentIds.push_back(OB_DEV_COMPONENT_LEFT_IR_SENSOR);
            compentIds.push_back(OB_DEV_COMPONENT_RIGHT_IR_SENSOR);
        }

        if(compentIds.empty()) {
            return;
        }
        for(auto componentId: compentIds) {
            auto sensor = device_->getComponentT<VideoSensor>(componentId);
            sensor->registerStreamStateChangedCallback([&](OBStreamState state, const std::shared_ptr<const StreamProfile> &profile) {
                utils::unusedVar(profile);
                if(state == STREAM_STATE_STREAMING) {
                    auto propertyServer = device_->getPropertyServer();
                    propertyServer->getStructureDataT<OBRegionOfInterest>(propertyId_);
                }
            });
        }
    }

private:
    IDevice *device_;

    OBFrameMetadataType metadataType_;

    OBPropertyID propertyId_;

    int64_t data_;

    FrameMetadataModifier modifier_;

    std::atomic<bool> initPropertyValue_;
    std::shared_ptr<IPropertyServer> propertyServer_;
};

class G330ColorMetadataParser : public G330MetadataParserBase {
public:
    G330ColorMetadataParser(IDevice *device, OBFrameMetadataType type, FrameMetadataModifier modifier = nullptr)
        : G330MetadataParserBase(device, type, modifier, initMetadataTypeIdMap(OB_SENSOR_COLOR)) {}
    virtual ~G330ColorMetadataParser() override = default;
};

class G330DepthMetadataParser : public G330MetadataParserBase {
public:
    G330DepthMetadataParser(IDevice *device, OBFrameMetadataType type, FrameMetadataModifier modifier = nullptr)
        : G330MetadataParserBase(device, type, modifier, initMetadataTypeIdMap(OB_SENSOR_DEPTH)) {}
    virtual ~G330DepthMetadataParser() override = default;
};

class G330DepthMetadataHdrSequenceSizeParser : public IFrameMetadataParser {
public:
    G330DepthMetadataHdrSequenceSizeParser(IDevice *device) : device_(device), inited_(false), frameInterleaveEnabled_(false), hdrEnabled_(false) {
        auto propertyServer = device_->getPropertyServer();
        if(propertyServer->isPropertySupported(OB_STRUCT_DEPTH_HDR_CONFIG, PROP_OP_WRITE, PROP_ACCESS_USER)) {
            propertyServer->registerAccessCallback(OB_STRUCT_DEPTH_HDR_CONFIG,
                                                   [this](uint32_t propertyId, const uint8_t *data, size_t dataSize, PropertyOperationType operationType) {
                                                       utils::unusedVar(propertyId);
                                                       utils::unusedVar(dataSize);
                                                       utils::unusedVar(operationType);
                                                       auto hdrConfig = *(reinterpret_cast<const OBHdrConfig *>(data));
                                                       hdrEnabled_    = hdrConfig.enable;
                                                       inited_        = true;
                                                   });
        }
        if(propertyServer->isPropertySupported(OB_PROP_FRAME_INTERLEAVE_ENABLE_BOOL, PROP_OP_WRITE, PROP_ACCESS_USER)) {
            propertyServer->registerAccessCallback(OB_PROP_FRAME_INTERLEAVE_ENABLE_BOOL,
                                                   [this](uint32_t propertyId, const uint8_t *data, size_t dataSize, PropertyOperationType operationType) {
                                                       utils::unusedVar(propertyId);

                                                       utils::unusedVar(dataSize);
                                                       utils::unusedVar(operationType);
                                                       frameInterleaveEnabled_ = *(reinterpret_cast<const bool *>(data));
                                                       inited_                 = true;
                                                   });
        }
    }

    virtual ~G330DepthMetadataHdrSequenceSizeParser() noexcept override = default;

    int64_t getValue(const uint8_t *metadata, size_t dataSize) override {
        utils::unusedVar(metadata);
        utils::unusedVar(dataSize);
        if(!inited_) {
            auto propertyServer = device_->getPropertyServer();
            if(propertyServer->isPropertySupported(OB_STRUCT_DEPTH_HDR_CONFIG, PROP_OP_READ, PROP_ACCESS_INTERNAL)) {
                auto hdrConfig = propertyServer->getStructureDataT<OBHdrConfig>(OB_STRUCT_DEPTH_HDR_CONFIG);
                hdrEnabled_    = hdrConfig.enable;
            }
            if(propertyServer->isPropertySupported(OB_PROP_FRAME_INTERLEAVE_ENABLE_BOOL, PROP_OP_READ, PROP_ACCESS_INTERNAL)) {
                frameInterleaveEnabled_ = propertyServer->getPropertyValueT<bool>(OB_PROP_FRAME_INTERLEAVE_ENABLE_BOOL);
            }
            inited_ = true;
        }
        return (hdrEnabled_ || frameInterleaveEnabled_) ? 2 : 0;
    }

    bool isSupported(const uint8_t *metadata, size_t dataSize) override {
        utils::unusedVar(metadata);
        utils::unusedVar(dataSize);
        return true;
    }

private:
    IDevice *device_;

    bool inited_;
    bool frameInterleaveEnabled_;
    bool hdrEnabled_;
};

}  // namespace libobsensor