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 "time_statistician.h"
16 #include "av_trans_log.h"
17 
18 namespace OHOS {
19 namespace DistributedHardware {
CalProcessTime(const std::shared_ptr<Plugin::Buffer> & data)20 void TimeStatistician::CalProcessTime(const std::shared_ptr<Plugin::Buffer>& data)
21 {
22     if (data == nullptr || data->GetBufferMeta() == nullptr) {
23         AVTRANS_LOGE("data or getbuffermeta or getmemory is nullptr.");
24         return;
25     }
26     auto bufferMeta = data->GetBufferMeta();
27     if (!bufferMeta->IsExist(Tag::USER_PUSH_DATA_TIME)) {
28         ClearStatistics();
29         return;
30     }
31     int64_t pushTime = Plugin::AnyCast<int64_t>(bufferMeta->GetMeta(Tag::USER_PUSH_DATA_TIME));
32     int64_t timeStamp = data->pts;
33     CalAverPushInterval(pushTime);
34     CalAverTimeStampInterval(timeStamp);
35 }
ClearStatistics()36 void TimeStatistician::ClearStatistics()
37 {
38     pushIndex_ = 0;
39     averPushInterval_ = 0;
40     lastPushTime_ = 0;
41     pushTime_ = 0;
42     pushIntervalSum_ = 0;
43     pushInterval_ = 0;
44     timeStampIndex_ = 0;
45     averTimeStampInterval_ = 0;
46     lastTimeStamp_ = 0;
47     timeStamp_ = 0;
48     timeStampIntervalSum_ = 0;
49     timeStampInterval_ = 0;
50 }
51 
CalAverPushInterval(const int64_t pushTime)52 void TimeStatistician::CalAverPushInterval(const int64_t pushTime)
53 {
54     pushTime_ = pushTime;
55     pushInterval_ = pushTime_ - lastPushTime_;
56     if (lastPushTime_ == 0) {
57         lastPushTime_ = pushTime_;
58         return;
59     }
60     pushIndex_++;
61     pushIntervalSum_ += pushInterval_;
62     averPushInterval_ = pushIntervalSum_ / pushIndex_;
63     lastPushTime_ = pushTime_;
64     AVTRANS_LOGD("Statistic pushInterval: %{public}lld, pushIndex: %{public}" PRIu32 ", averPushInterval: %{public}lld",
65         pushInterval_, pushIndex_, averPushInterval_);
66 }
67 
CalAverTimeStampInterval(const int64_t timeStamp)68 void TimeStatistician::CalAverTimeStampInterval(const int64_t timeStamp)
69 {
70     timeStamp_ = timeStamp;
71     timeStampInterval_ = timeStamp_ - lastTimeStamp_;
72     if (lastTimeStamp_ == 0) {
73         lastTimeStamp_ = timeStamp_;
74         return;
75     }
76     timeStampIndex_++;
77     timeStampIntervalSum_ += timeStampInterval_;
78     averTimeStampInterval_ = timeStampIntervalSum_ / timeStampIndex_;
79     lastTimeStamp_ = timeStamp_;
80     AVTRANS_LOGD("Statistic timeStampInterval: %{public}lld, timeStampIndex: %{public}" PRIu32
81         ", averTimeStampInterval: %{public}lld", timeStampInterval_, timeStampIndex_, averTimeStampInterval_);
82 }
83 
GetAverPushInterval()84 int64_t TimeStatistician::GetAverPushInterval()
85 {
86     return averPushInterval_;
87 }
88 
GetAverTimeStampInterval()89 int64_t TimeStatistician::GetAverTimeStampInterval()
90 {
91     return averTimeStampInterval_;
92 }
93 
GetPushInterval()94 int64_t TimeStatistician::GetPushInterval()
95 {
96     return pushInterval_;
97 }
98 
GetTimeStampInterval()99 int64_t TimeStatistician::GetTimeStampInterval()
100 {
101     return timeStampInterval_;
102 }
103 } // namespace DistributedHardware
104 } // namespace OHOS