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
// Copyright (c) Orbbec Inc. All Rights Reserved.
// Licensed under the MIT License.

#include "Pipeline.hpp"

#include "DevicePids.hpp"
#include "context/Context.hpp"
#include "stream/StreamProfileFactory.hpp"
#include "logger/LoggerInterval.hpp"
#include "logger/LoggerHelper.hpp"
#include "utils/Utils.hpp"
#include "IAlgParamManager.hpp"
#include "frameprocessor/FrameProcessor.hpp"

#include <cmath>
#include <algorithm>

namespace libobsensor {
Pipeline::Pipeline(std::shared_ptr<IDevice> dev) : device_(dev), config_(nullptr), streamState_(STREAM_STATE_STOPPED), pipelineCallback_(nullptr) {
    LOG_DEBUG("Pipeline init ...");
    auto sensorTypeList = device_->getSensorTypeList();
    if(sensorTypeList.empty()) {
        throw std::runtime_error("This device has no valid sensor!");
    }

    loadFrameQueueSizeConfig();
    loadMaxFrameDelayConfig();

    outputFrameQueue_ = std::make_shared<FrameQueue<const Frame>>(maxFrameQueueSize_);

    frameAggregator_  = std::make_shared<FrameAggregator>(maxFrameDelay_);
    frameAggregator_->setCallback([&](std::shared_ptr<const Frame> frame) { outputFrame(frame); });

    TRY_EXECUTE(enableFrameSync());

    auto deviceInfo = device_->getInfo();
    LOG_INFO("Pipeline created with device: {{name: {0}, sn: {1}}}, @0x{2:X}", deviceInfo->name_, deviceInfo->deviceSn_, (uint64_t)this);
}

Pipeline::~Pipeline() noexcept {
    LOG_DEBUG("Pipeline deInit start!");

    if(streamState_ == STREAM_STATE_STARTING || streamState_ == STREAM_STATE_STREAMING) {
        TRY_EXECUTE(stop());
    }

    outputFrameQueue_->reset();
    LOG_INFO("Pipeline destroyed! @0x{:X}", (uint64_t)this);
}

void Pipeline::applyConfig(std::shared_ptr<const Config> cfg) {
    if(!cfg) {
        loadDefaultConfig();
        return;
    }
    config_ = checkAndSetConfig(cfg);
    checkHardwareD2CConfig();
}

void Pipeline::switchConfig(std::shared_ptr<const Config> cfg) {
    if(!cfg) {
        throw libobsensor::invalid_value_exception("Null pointer config!");
    }

    if(config_ && *cfg == *config_) {
        LOG_INFO("Noting will be execute due to pipeline config have no been changed!");
        return;
    }

    bool restartRequired = streamState_ == STREAM_STATE_STREAMING || streamState_ == STREAM_STATE_STARTING;
    if(restartRequired) {
        stopStream();
        std::unique_lock<std::mutex> lk(streamMutex_);
        start(cfg);
    }
    else {
        std::unique_lock<std::mutex> lk(streamMutex_);
        applyConfig(cfg);
    }
}

void Pipeline::loadDefaultConfig() {
    auto config = std::make_shared<Config>();

    auto envConfig      = EnvConfig::getInstance();
    auto sensorTypeList = device_->getSensorTypeList();

    bool loaded = false;
    for(auto &sensorType: sensorTypeList) {
        auto        sensorTypeName = utils::obSensorToStr(sensorType);
        std::string nodeName       = std::string("Pipeline.Stream.") + sensorTypeName;
        if(!envConfig->isNodeContained(nodeName)) {
            continue;
        }

        std::shared_ptr<const StreamProfile> profile;

        bool UseDefaultStreamProfile = true;
        envConfig->getBooleanValue(nodeName + ".UseDefaultStreamProfile", UseDefaultStreamProfile);
        if(!UseDefaultStreamProfile) {
            profile = StreamProfileFactory::getStreamProfileFromEnvConfig(nodeName, sensorType);
        }

        if(profile) {
            config->enableStream(profile);
        }
        else {
            auto streamType = utils::mapSensorTypeToStreamType(sensorType);
            config->enableStream(streamType);
        }
        loaded = true;
    }

    if(!loaded) {
        if(std::find(sensorTypeList.begin(), sensorTypeList.end(), OB_SENSOR_DEPTH) != sensorTypeList.end()) {
            config->enableStream(OB_STREAM_DEPTH);
        }
        if(std::find(sensorTypeList.begin(), sensorTypeList.end(), OB_SENSOR_COLOR) != sensorTypeList.end()) {
            config->enableStream(OB_STREAM_COLOR);
        }
    }
    config_ = checkAndSetConfig(config);
}

void Pipeline::loadFrameQueueSizeConfig() {
    auto envConfig = EnvConfig::getInstance();

    envConfig->getIntValue("Memory.PipelineFrameQueueSize", maxFrameQueueSize_);
    if(maxFrameQueueSize_ <= 0) {
        LOG_WARN("Read xml config:pipeline frame queue size is invalid!");
        maxFrameQueueSize_ = 10;
    }

    LOG_DEBUG("loadFrameQueueSizeConfig() config queue size: {}", maxFrameQueueSize_);
}

void Pipeline::loadMaxFrameDelayConfig() {
    auto envConfig = EnvConfig::getInstance();

    std::string nodePath                    = "Device." + device_->getInfo()->name_;
    nodePath                                = utils::string::removeSpace(nodePath);
    envConfig->getFloatValue(nodePath + ".MaxFrameDelay", maxFrameDelay_);
    LOG_DEBUG("loadMaxFrameDelayConfig() config max frame delay: {}", maxFrameDelay_);
}

StreamProfileList Pipeline::getEnabledStreamProfileList() {
    if(!config_) {
        return {};
    }
    return config_->getEnabledStreamProfileList();
}

std::shared_ptr<Config> Pipeline::checkAndSetConfig(std::shared_ptr<const Config> cfg) {
    LOG_DEBUG("Check and set config start!");
    std::shared_ptr<Config> config = cfg->clone();
    config->disableAllStream();
    const auto enabledStreamProfiles = cfg->getEnabledStreamProfileList();
    for(auto sp: enabledStreamProfiles) {
        auto streamType = sp->getType();
        auto sensorType = utils::mapStreamTypeToSensorType(streamType);
        auto sensor     = device_->getSensor(sensorType);
        if(!sensor) {
            throw invalid_value_exception(utils::string::to_string() << "No matched sensor found for:" << sensorType);
        }
        auto sensorSpList = sensor->getStreamProfileList();
        if(sensorType == OB_SENSOR_ACCEL) {
            auto profile            = sp->as<AccelStreamProfile>();
            auto matchedProfileList = matchAccelStreamProfile(sensorSpList, profile->getFullScaleRange(), profile->getSampleRate());
            if(matchedProfileList.empty()) {
                throw invalid_value_exception(utils::string::to_string() << "No matched profile found for:" << sp);
            }
            config->enableStream(matchedProfileList.front());
        }
        else if(sensorType == OB_SENSOR_GYRO) {
            auto profile            = sp->as<GyroStreamProfile>();
            auto matchedProfileList = matchGyroStreamProfile(sensorSpList, profile->getFullScaleRange(), profile->getSampleRate());
            if(matchedProfileList.empty()) {
                throw invalid_value_exception(utils::string::to_string() << "No matched profile found for:" << sp);
            }
            config->enableStream(matchedProfileList.front());
        }
        else {
            auto profile            = sp->as<VideoStreamProfile>();
            auto matchedProfileList = matchVideoStreamProfile(sensorSpList, profile->getWidth(), profile->getHeight(), profile->getFps(), profile->getFormat());
            if(matchedProfileList.empty()) {
                throw invalid_value_exception(utils::string::to_string() << "No matched profile found for: " << sp);
            }
            config->enableStream(matchedProfileList.front());
        }
    }
    LOG_INFO("Check and set config done!");
    return config;
}

void Pipeline::start(std::shared_ptr<const Config> cfg) {
    LOG_DEBUG("Pipeline start() start!");

    applyConfig(cfg);

    if(config_->isStreamEnabled(OB_STREAM_DEPTH) || config_->isStreamEnabled(OB_STREAM_COLOR)) {
        configAlignMode();
    }

    frameAggregator_->updateConfig(config_, true);

    streamState_ = STREAM_STATE_STARTING;
    BEGIN_TRY_EXECUTE({ startStream(); })
    CATCH_EXCEPTION_AND_EXECUTE({
        stop();
        throw;
    })

    LOG_INFO("Pipeline start done!");
}

void Pipeline::start(std::shared_ptr<const Config> cfg, FrameCallback callback) {
    pipelineCallback_ = callback;
    start(cfg);
}

std::shared_ptr<const Config> Pipeline::getConfig() {
    if(!config_) {
        loadDefaultConfig();
    }
    return config_;
}

void Pipeline::startStream() {
    LOG_INFO("Try to start streams!");

    outputFrameQueue_->reset();  // reset output frame queue before restart streams

    auto spList = config_->getEnabledStreamProfileList();
    for(const auto &sp: spList) {
        auto streamType = sp->getType();
        auto sensorType = utils::mapStreamTypeToSensorType(streamType);
        auto sensor     = device_->getSensor(sensorType);
        if(!sensor) {
            throw std::runtime_error("No sensor matched!");
        }
        sensor->start(sp, [&](std::shared_ptr<const Frame> frame) { onFrameCallback(frame); });

        LOG_DEBUG("Sensor stream started, sensorType={}", sensor->getSensorType());
    }
    LOG_INFO("Start streams done!");
}

void Pipeline::onFrameCallback(std::shared_ptr<const Frame> frame) {
    std::unique_lock<std::mutex> lk(streamMutex_);
    if(streamState_ != STREAM_STATE_STOPPED && streamState_ != STREAM_STATE_STOPPING) {
        if(streamState_ == STREAM_STATE_STARTING) {
            streamState_ = STREAM_STATE_STREAMING;
        }
        frameAggregator_->pushFrame(frame);
    }
    auto frameType = frame->getType();
    LOG_INTVL(LOG_INTVL_OBJECT_TAG + std::to_string(frameType), DEF_MIN_LOG_INTVL, spdlog::level::debug, "Frame received on pipeline! type={}", frameType);
}

void Pipeline::outputFrame(std::shared_ptr<const Frame> frame) {
    auto deviceInfo = device_->getInfo();
    LOG_FREQ_CALC(DEBUG, 5000, "{}({}): Pipeline {}, frameset output rate={freq}fps", deviceInfo->name_, deviceInfo->deviceSn_, STREAM_STATE_STR(streamState_));
    if(streamState_ == STREAM_STATE_STREAMING) {
        if(pipelineCallback_ != nullptr) {
            pipelineCallback_(frame);
            return;
        }

        if(outputFrameQueue_->fulled()) {
            LOG_WARN_INTVL("Output frameset queue is full, drop oldest frameset!");
            outputFrameQueue_->dequeue();
        }
        outputFrameQueue_->enqueue(std::move(frame));
    }
}

std::shared_ptr<const Frame> Pipeline::waitForFrame(uint32_t timeout_ms) {
    auto frame = outputFrameQueue_->dequeue(timeout_ms);
    if(!frame) {
        LOG_WARN_INTVL("Wait for frame timeout, you can try to increase the wait time! current timeout={}", timeout_ms);
        return nullptr;
    }
    return frame;
}

void Pipeline::stopStream() {
    LOG_INFO("Try to stop streams!");
    if(!config_) {
        LOG_WARN("The config is null!");
        return;
    }

    auto cfgStreams = config_->getEnabledStreamProfileList();
    auto streamIter = cfgStreams.end();
    while(cfgStreams.size()) {
        streamIter--;
        auto streamType = (*streamIter)->getType();
        auto sensorType = utils::mapStreamTypeToSensorType(streamType);
        auto sensor     = device_->getSensor(sensorType);
        TRY_EXECUTE({ sensor->stop(); })
        LOG_INFO("Sensor stream stopped, sensorType={}", sensor->getSensorType());
        if(streamIter == cfgStreams.begin()) {
            break;
        }
    }

    LOG_INFO("Stop streams done!");
}

void Pipeline::stop() {
    LOG_INFO("Try to stop pipeline!");
    if(streamState_ != STREAM_STATE_STOPPED) {
        stopStream();
    }

    if(config_ && (config_->isStreamEnabled(OB_STREAM_DEPTH) || config_->isStreamEnabled(OB_STREAM_COLOR))) {
        resetAlignMode();
    }
    if(frameAggregator_) {
        frameAggregator_->clearAllFrameQueue();
    }
    enableHardwareD2C(false);

    // flush output frame queue
    outputFrameQueue_->flush();

    // clear callback
    pipelineCallback_ = nullptr;

    streamState_ = STREAM_STATE_STOPPED;
    LOG_INFO("Stop pipeline done!");
}

std::shared_ptr<IDevice> Pipeline::getDevice() {
    if(device_) {
        return device_;
    }
    return nullptr;
}

void Pipeline::configAlignMode() {
    // todo: implement this function
    // if(!config_) {
    //     return;
    // }
    // auto alignMode = config_->getAlignMode();
    // device_->configAlignMode(config_);
}

void Pipeline::resetAlignMode() {
    // todo: implement this function
    // if(device_) {
    //     device_->resetAlignMode();
    // }
}

StreamProfileList Pipeline::getD2CDepthProfileList(std::shared_ptr<const StreamProfile> colorProfile, OBAlignMode alignMode) {
    StreamProfileList d2cDepthProfileList;
    d2cDepthProfileList.clear();
    if(!device_) {
        return d2cDepthProfileList;
    }

    auto algParamManager         = device_->getComponentT<IAlgParamManager>(OB_DEV_COMPONENT_ALG_PARAM_MANAGER, false);
    auto d2cProfileList          = algParamManager->getD2CProfileList();
    auto colorVideoStreamProfile = colorProfile->as<VideoStreamProfile>();
    auto depthSensor             = device_->getSensor(OB_SENSOR_DEPTH);
    auto depthProfiles           = depthSensor->getStreamProfileList();
    if(alignMode == ALIGN_DISABLE) {
        return depthProfiles;
    }

    for(const auto &sp: d2cProfileList) {
        if(colorVideoStreamProfile->getWidth() == sp.colorWidth && colorVideoStreamProfile->getHeight() == sp.colorHeight) {
            for(auto profile: depthProfiles) {
                auto depthProfile = profile->as<VideoStreamProfile>();
                auto width        = depthProfile->getWidth();
                auto height       = depthProfile->getHeight();
                if(sp.depthWidth == width && sp.depthHeight == height
                   && ((alignMode == ALIGN_D2C_HW_MODE && (sp.alignType & ALIGN_D2C_HW)) || (alignMode == ALIGN_D2C_SW_MODE && (sp.alignType & ALIGN_D2C_SW))))

                {
                    d2cDepthProfileList.push_back(depthProfile);
                }
            }
        }
    }

    return d2cDepthProfileList;
}

OBCameraParam Pipeline::getCameraParam() {
    OBCameraParam curCameraParam = {};
    if(!device_ || !config_) {
        return curCameraParam;
    }

    auto colorStreamProfile = config_->getEnabledStreamProfile(OB_STREAM_COLOR);
    auto depthStreamProfile = config_->getEnabledStreamProfile(OB_STREAM_DEPTH);
    if(colorStreamProfile) {
        curCameraParam.rgbIntrinsic  = colorStreamProfile->as<VideoStreamProfile>()->getIntrinsic();
        curCameraParam.rgbDistortion = colorStreamProfile->as<VideoStreamProfile>()->getDistortion();
    }
    if(depthStreamProfile) {
        curCameraParam.depthIntrinsic  = depthStreamProfile->as<VideoStreamProfile>()->getIntrinsic();
        curCameraParam.depthDistortion = depthStreamProfile->as<VideoStreamProfile>()->getDistortion();
    }
    if(colorStreamProfile && depthStreamProfile) {
        curCameraParam.transform = depthStreamProfile->getExtrinsicTo(colorStreamProfile);
    }

    return curCameraParam;
}

OBCameraParam Pipeline::getCameraParam(uint32_t colorWidth, uint32_t colorHeight, uint32_t depthWidth, uint32_t depthHeight) {
    OBCameraParam curCameraParam = {};
    if(!device_) {
        return curCameraParam;
    }
    auto colorSensor = device_->getSensor(OB_SENSOR_COLOR);
    if(!colorSensor) {
        throw invalid_value_exception(utils::string::to_string() << "No matched color sensor found");
    }
    auto depthSensor = device_->getSensor(OB_SENSOR_DEPTH);
    if(!depthSensor) {
        throw invalid_value_exception(utils::string::to_string() << "No matched depth sensor found");
    }

    auto colorSensorSpList       = colorSensor->getStreamProfileList();
    auto matchedColorProfileList = matchVideoStreamProfile(colorSensorSpList, colorWidth, colorHeight, OB_FPS_ANY, OB_FORMAT_ANY);
    if(matchedColorProfileList.empty()) {
        throw invalid_value_exception(utils::string::to_string() << "No matched color profile found");
    }
    auto colorStreamProfile = matchedColorProfileList.front();

    auto depthSensorSpList       = depthSensor->getStreamProfileList();
    auto matchedDepthProfileList = matchVideoStreamProfile(depthSensorSpList, depthWidth, depthHeight, OB_FPS_ANY, OB_FORMAT_ANY);
    if(matchedDepthProfileList.empty()) {
        throw invalid_value_exception(utils::string::to_string() << "No matched depth profile found");
    }
    auto depthStreamProfile = matchedDepthProfileList.front();

    curCameraParam.rgbIntrinsic    = colorStreamProfile->getIntrinsic();
    curCameraParam.rgbDistortion   = colorStreamProfile->getDistortion();
    curCameraParam.depthIntrinsic  = depthStreamProfile->getIntrinsic();
    curCameraParam.depthDistortion = depthStreamProfile->getDistortion();
    curCameraParam.transform       = depthStreamProfile->getExtrinsicTo(colorStreamProfile);

    return curCameraParam;
}

OBCalibrationParam Pipeline::getCalibrationParam(std::shared_ptr<Config> cfg) {
    OBCalibrationParam calibrationParam = {};
    if(!device_ || !cfg) {
        return calibrationParam;
    }

    // convert stream profile on config to real stream profile on sensors
    auto config = checkAndSetConfig(cfg);

    memset(calibrationParam.intrinsics, 0, sizeof(OBCameraIntrinsic) * OB_SENSOR_TYPE_COUNT);
    memset(calibrationParam.distortion, 0, sizeof(OBCameraDistortion) * OB_SENSOR_TYPE_COUNT);
    for(int i = 0; i < OB_SENSOR_TYPE_COUNT; i++) {
        for(int j = 0; j < OB_SENSOR_TYPE_COUNT; j++) {
            float rot[9] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
            memset(calibrationParam.extrinsics[i][j].trans, 0, sizeof(float) * 3);
            memcpy(calibrationParam.extrinsics[i][j].rot, rot, sizeof(float) * 9);
        }
    }

    // Intrinsic (cameras only)
    for(int i = 0; i < OB_SENSOR_TYPE_COUNT; i++) {
        auto sensorType = static_cast<OBSensorType>(i);
        if(!device_->isSensorExists(sensorType)) {
            continue;
        }
        auto streamType = utils::mapSensorTypeToStreamType(sensorType);
        if(streamType == OB_STREAM_ACCEL || streamType == OB_STREAM_GYRO) {
            continue;
        }

        auto streamProfile = config->getEnabledStreamProfile(streamType);
        if(!streamProfile) {
            auto sensor = device_->getSensor(sensorType);
            auto spList = sensor->getStreamProfileList();
            if(spList.empty()) {
                continue;
            }
            streamProfile = spList.front();
        }
        auto intrinsic  = streamProfile->as<VideoStreamProfile>()->getIntrinsic();
        auto distortion = streamProfile->as<VideoStreamProfile>()->getDistortion();
        memcpy(&calibrationParam.intrinsics[sensorType], &intrinsic, sizeof(OBCameraIntrinsic));
        memcpy(&calibrationParam.distortion[sensorType], &distortion, sizeof(OBCameraDistortion));
    }

    // Extrinsic
    for(int source = 0; source < OB_SENSOR_TYPE_COUNT; source++) {
        for(int target = 0; target < OB_SENSOR_TYPE_COUNT; target++) {
            if(source == target) {
                continue;
            }

            auto sourceSensorType = static_cast<OBSensorType>(source);
            auto targetSensorType = static_cast<OBSensorType>(target);
            if(!device_->isSensorExists(sourceSensorType) || !device_->isSensorExists(targetSensorType)) {
                continue;
            }

            auto                                 sourceStreamType = utils::mapSensorTypeToStreamType(sourceSensorType);
            auto                                 targetStreamType = utils::mapSensorTypeToStreamType(targetSensorType);
            std::shared_ptr<const StreamProfile> sourceStreamProfile;
            std::shared_ptr<const StreamProfile> targetStreamProfile;

            sourceStreamProfile = config->getEnabledStreamProfile(sourceStreamType);
            if(!sourceStreamProfile) {
                auto sensor = device_->getSensor(sourceSensorType);
                auto spList = sensor->getStreamProfileList();
                if(spList.empty()) {
                    continue;
                }
                sourceStreamProfile = spList.front();
            }
            targetStreamProfile = config->getEnabledStreamProfile(targetStreamType);
            if(!targetStreamProfile) {
                auto sensor = device_->getSensor(targetSensorType);
                auto spList = sensor->getStreamProfileList();
                if(spList.empty()) {
                    continue;
                }
                targetStreamProfile = spList.front();
            }

            auto sourceToTargetExtrinsic = sourceStreamProfile->getExtrinsicTo(targetStreamProfile);
            memcpy(&calibrationParam.extrinsics[source][target], &sourceToTargetExtrinsic, sizeof(OBExtrinsic));
            auto targetToSourceExtrinsic = targetStreamProfile->getExtrinsicTo(sourceStreamProfile);
            memcpy(&calibrationParam.extrinsics[target][source], &targetToSourceExtrinsic, sizeof(OBExtrinsic));
        }
    }

    return calibrationParam;
}

void Pipeline::enableFrameSync() {
    if(device_->getExtensionInfo("AllSensorsUsingSameClock") == "true") {
        frameAggregator_->enableFrameSync(FrameSyncModeSyncAccordingFrameTimestamp);
    }
    else {
        LOG_WARN("Frame sync is not supported for sensors with different clocks! Use system timestamp instead, the accuracy may be lower!");
        frameAggregator_->enableFrameSync(FrameSyncModeSyncAccordingSystemTimestamp);
    }
}

void Pipeline::disableFrameSync() {
    frameAggregator_->enableFrameSync(FrameSyncModeDisable);
}

void Pipeline::checkHardwareD2CConfig() {
    auto frameProcessor      = device_->getComponentT<FrameProcessor>(OB_DEV_COMPONENT_DEPTH_FRAME_PROCESSOR, false);
    auto depthFrameProcessor = std::dynamic_pointer_cast<DepthFrameProcessor>(frameProcessor.get());
    if(!depthFrameProcessor) {
        return;
    }

    if(config_->getAlignMode() != ALIGN_D2C_HW_MODE) {
        enableHardwareD2C(false);
        return;
    }

    auto algParamManager = device_->getComponentT<IAlgParamManager>(OB_DEV_COMPONENT_ALG_PARAM_MANAGER, false);
    auto colorProfile    = config_->getEnabledStreamProfile(OB_STREAM_COLOR);
    auto depthProfile    = config_->getEnabledStreamProfile(OB_STREAM_DEPTH);
    if(algParamManager && colorProfile && depthProfile) {
        auto colorVideoStreamProfile = colorProfile->as<VideoStreamProfile>();
        auto depthVideoStreamProfile = depthProfile->as<VideoStreamProfile>();
        auto calibrationCameraParams = algParamManager->getCalibrationCameraParamList();
        auto d2cProfileList          = algParamManager->getD2CProfileList();
        depthFrameProcessor->setHardwareD2CProcessParams(colorVideoStreamProfile,depthVideoStreamProfile, calibrationCameraParams,d2cProfileList, config_->getDepthScaleAfterAlignRequire());
        enableHardwareD2C(true);
    }
}

void Pipeline::enableHardwareD2C(bool enable) {
    auto frameProcessor      = device_->getComponentT<FrameProcessor>(OB_DEV_COMPONENT_DEPTH_FRAME_PROCESSOR, false);
    auto depthFrameProcessor = std::dynamic_pointer_cast<DepthFrameProcessor>(frameProcessor.get());
    if(!depthFrameProcessor) {
        return;
    }
    depthFrameProcessor->enableHardwareD2CProcess(enable);
}

}  // namespace libobsensor