1 /* 2 * Copyright (c) 2023-2025 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 "time_statistician.h" 16 #include "distributed_hardware_log.h" 17 #include "dcamera_utils_tools.h" 18 19 namespace OHOS { 20 namespace DistributedHardware { CalProcessTime(const std::shared_ptr<IFeedableData> & data)21void TimeStatistician::CalProcessTime(const std::shared_ptr<IFeedableData>& data) 22 { 23 CHECK_AND_RETURN_LOG(data == nullptr, "data is nullptr"); 24 int64_t feedTime = GetNowTimeStampUs(); 25 int64_t timeStamp = data->GetTimeStamp(); 26 CalAverFeedInterval(feedTime); 27 CalAverTimeStampInterval(timeStamp); 28 } 29 CalAverFeedInterval(const int64_t feedTime)30void TimeStatistician::CalAverFeedInterval(const int64_t feedTime) 31 { 32 feedTime_ = feedTime; 33 feedInterval_ = feedTime_ - lastFeedTime_; 34 if (lastFeedTime_ == 0) { 35 lastFeedTime_ = feedTime_; 36 return; 37 } 38 feedIndex_++; 39 if (feedIntervalSum_ > INT64_MAX - feedInterval_) { 40 DHLOGE("feedIntervalSum_ overflow"); 41 return; 42 } 43 feedIntervalSum_ += feedInterval_; 44 averFeedInterval_ = feedIntervalSum_ / static_cast<int64_t>(feedIndex_); 45 lastFeedTime_ = feedTime_; 46 } 47 CalAverTimeStampInterval(const int64_t timeStamp)48void TimeStatistician::CalAverTimeStampInterval(const int64_t timeStamp) 49 { 50 timeStamp_ = timeStamp; 51 timeStampInterval_ = timeStamp_ - lastTimeStamp_; 52 if (lastTimeStamp_ == 0) { 53 lastTimeStamp_ = timeStamp_; 54 return; 55 } 56 timeStampIndex_++; 57 if (timeStampIntervalSum_ > INT64_MAX - timeStampInterval_) { 58 DHLOGE("timeStampIntervalSum_ overflow"); 59 return; 60 } 61 timeStampIntervalSum_ += timeStampInterval_; 62 averTimeStampInterval_ = timeStampIntervalSum_ / static_cast<int64_t>(timeStampIndex_); 63 lastTimeStamp_ = timeStamp_; 64 } 65 GetAverFeedInterval()66int64_t TimeStatistician::GetAverFeedInterval() 67 { 68 return averFeedInterval_; 69 } 70 GetAverTimeStampInterval()71int64_t TimeStatistician::GetAverTimeStampInterval() 72 { 73 return averTimeStampInterval_; 74 } 75 GetFeedInterval()76int64_t TimeStatistician::GetFeedInterval() 77 { 78 return feedInterval_; 79 } 80 GetTimeStampInterval()81int64_t TimeStatistician::GetTimeStampInterval() 82 { 83 return timeStampInterval_; 84 } 85 } // namespace DistributedHardware 86 } // namespace OHOS