1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "dcamera_feeding_smoother.h"
16 #include "distributed_hardware_log.h"
17 #include <memory>
18 #include "data_buffer.h"
19 #include "dcamera_utils_tools.h"
20 #include "feeding_smoother_listener.h"
21 #include "smoother_constants.h"
22 namespace OHOS {
23 namespace DistributedHardware {
PrepareSmooth()24 void DCameraFeedingSmoother::PrepareSmooth()
25 {
26     SetProcessDynamicBalanceState(false);
27     SetBufferTime(SMOOTH_BUFFER_TIME_US);
28     SetDynamicBalanceThre(DYNAMIC_BALANCE_THRE);
29     SetAverIntervalDiffThre(AVER_INTERVAL_DIFF_THRE_US);
30     SetFeedOnceDiffThre(FEED_ONCE_DIFF_THRE_US);
31 }
32 
InitBaseline(const int64_t timeStampBaseline,const int64_t clockBaseline)33 void DCameraFeedingSmoother::InitBaseline(const int64_t timeStampBaseline, const int64_t clockBaseline)
34 {
35     CHECK_AND_RETURN_LOG(dCameraStatistician_ == nullptr, "dCameraStatistician_ is null.");
36     int64_t clock = dCameraStatistician_->GetRecvTime() + dCameraStatistician_->GetAverRecv2FeedTime()
37         + GetBufferTime();
38     SetTimeStampBaseline(timeStampBaseline);
39     SetClockBaseline(clock);
40 }
41 
InitTimeStatistician()42 void DCameraFeedingSmoother::InitTimeStatistician()
43 {
44     if (statistician_ != nullptr) {
45         return;
46     }
47     dCameraStatistician_ = std::make_shared<DCameraTimeStatistician>();
48     statistician_ = dCameraStatistician_;
49 }
50 
NotifySmoothFinished(const std::shared_ptr<IFeedableData> & data)51 int32_t DCameraFeedingSmoother::NotifySmoothFinished(const std::shared_ptr<IFeedableData>& data)
52 {
53     int64_t finishSmoothT = GetNowTimeStampUs();
54     std::shared_ptr<DataBuffer> buffer = std::reinterpret_pointer_cast<DataBuffer>(data);
55     buffer->frameInfo_.timePonit.finishSmooth = finishSmoothT;
56     CHECK_AND_RETURN_RET_LOG(dCameraStatistician_ == nullptr, NOTIFY_FAILED, "dCameraStatistician_ is null.");
57     dCameraStatistician_->CalWholeProcessTime(buffer);
58     if (listener_ == nullptr) {
59         DHLOGE("Smoother listener is nullptr.");
60         return NOTIFY_FAILED;
61     }
62     return listener_->OnSmoothFinished(buffer);
63 }
64 } // namespace DistributedHardware
65 } // namespace OHOS