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 if (pushIndex_ == 0) {
63 AVTRANS_LOGE("pushIndex_ is zero");
64 return;
65 }
66 averPushInterval_ = pushIntervalSum_ / pushIndex_;
67
68 lastPushTime_ = pushTime_;
69 AVTRANS_LOGD("Statistic pushInterval: %{public}lld, pushIndex: %{public}" PRIu32 ", averPushInterval: %{public}lld",
70 pushInterval_, pushIndex_, averPushInterval_);
71 }
72
CalAverTimeStampInterval(const int64_t timeStamp)73 void TimeStatistician::CalAverTimeStampInterval(const int64_t timeStamp)
74 {
75 timeStamp_ = timeStamp;
76 timeStampInterval_ = timeStamp_ - lastTimeStamp_;
77 if (lastTimeStamp_ == 0) {
78 lastTimeStamp_ = timeStamp_;
79 return;
80 }
81 timeStampIndex_++;
82 timeStampIntervalSum_ += timeStampInterval_;
83 averTimeStampInterval_ = timeStampIntervalSum_ / timeStampIndex_;
84 lastTimeStamp_ = timeStamp_;
85 AVTRANS_LOGD("Statistic timeStampInterval: %{public}lld, timeStampIndex: %{public}" PRIu32
86 ", averTimeStampInterval: %{public}lld", timeStampInterval_, timeStampIndex_, averTimeStampInterval_);
87 }
88
GetAverPushInterval()89 int64_t TimeStatistician::GetAverPushInterval()
90 {
91 return averPushInterval_;
92 }
93
GetAverTimeStampInterval()94 int64_t TimeStatistician::GetAverTimeStampInterval()
95 {
96 return averTimeStampInterval_;
97 }
98
GetPushInterval()99 int64_t TimeStatistician::GetPushInterval()
100 {
101 return pushInterval_;
102 }
103
GetTimeStampInterval()104 int64_t TimeStatistician::GetTimeStampInterval()
105 {
106 return timeStampInterval_;
107 }
108 } // namespace DistributedHardware
109 } // namespace OHOS