1 /*
2 * Copyright (c) 2022 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 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14 #include "stream_statistics.h"
15 #include <sstream>
16
17 namespace OHOS::Camera {
StreamStatistics()18 StreamStatistics::StreamStatistics()
19 {
20 Clear();
21 }
22
StreamStatistics(int32_t streamId)23 StreamStatistics::StreamStatistics(int32_t streamId)
24 {
25 streamId_ = streamId;
26 Clear();
27 }
28
~StreamStatistics()29 StreamStatistics::~StreamStatistics() {}
30
SetStreamId(int32_t streamId)31 void StreamStatistics::SetStreamId(int32_t streamId)
32 {
33 streamId_ = streamId;
34 }
35
RequestBufferSuccess()36 void StreamStatistics::RequestBufferSuccess()
37 {
38 requestBufferSuccessCount_++;
39 }
40
RequestBufferFail()41 void StreamStatistics::RequestBufferFail()
42 {
43 requestBufferFailCount_++;
44 }
45
FlushBufferSuccess()46 void StreamStatistics::FlushBufferSuccess()
47 {
48 flushBufferSuccessCount_++;
49 }
50
FlushBufferFail()51 void StreamStatistics::FlushBufferFail()
52 {
53 flushBufferFailCount_++;
54 }
55
CancelBufferSuccess()56 void StreamStatistics::CancelBufferSuccess()
57 {
58 cancelBufferSuccessCount_++;
59 }
60
CancelBufferFail()61 void StreamStatistics::CancelBufferFail()
62 {
63 cancelBufferFailCount_++;
64 }
65
Clear()66 void StreamStatistics::Clear()
67 {
68 requestBufferSuccessCount_ = 0;
69 requestBufferFailCount_ = 0;
70 flushBufferSuccessCount_ = 0;
71 flushBufferFailCount_ = 0;
72 cancelBufferSuccessCount_ = 0;
73 cancelBufferFailCount_ = 0;
74 }
75
CalculateFps(int interval)76 void StreamStatistics::CalculateFps(int interval)
77 {
78 if (interval > 0) {
79 fpsValue_ = (requestBufferSuccessCount_ - lastRequestBufferCount_) / interval;
80 lastRequestBufferCount_ = requestBufferSuccessCount_;
81 } else {
82 return;
83 }
84 }
85
DumpStats(int interval)86 void StreamStatistics::DumpStats(int interval)
87 {
88 if (clock_gettime(CLOCK_MONOTONIC, ×tamp_) != 0) {
89 CAMERA_LOGE("clock_gettime error");
90 return;
91 }
92
93 if (lastOutputTime_ == 0) {
94 lastOutputTime_ = timestamp_.tv_sec;
95 return;
96 }
97
98 if (timestamp_.tv_sec - lastOutputTime_ > interval) {
99 CalculateFps(timestamp_.tv_sec - lastOutputTime_);
100 std::stringstream ss;
101 ss << "streamId:" << streamId_ << ", buf status(suc/fail) req:" << requestBufferSuccessCount_ <<
102 "/" << requestBufferFailCount_ << ", flush:" <<flushBufferSuccessCount_ << "/" <<
103 flushBufferFailCount_ <<", cancel:" << cancelBufferSuccessCount_ << "/" <<
104 cancelBufferFailCount_ << ", fps:" << fpsValue_;
105 streamInfo_ = ss.str();
106 CAMERA_LOGE("%{public}s", streamInfo_.c_str());
107 lastOutputTime_ = timestamp_.tv_sec;
108 }
109 }
110
CancelBufferResult(int32_t ret)111 void StreamStatistics::CancelBufferResult(int32_t ret)
112 {
113 if (ret != 0) {
114 CancelBufferFail();
115 } else {
116 CancelBufferSuccess();
117 }
118 }
119
FlushBufferResult(int32_t ret)120 void StreamStatistics::FlushBufferResult(int32_t ret)
121 {
122 if (ret != 0) {
123 FlushBufferFail();
124 } else {
125 FlushBufferSuccess();
126 }
127 }
128
RequestBufferResult(OHOS::SurfaceError sfError)129 void StreamStatistics::RequestBufferResult(OHOS::SurfaceError sfError)
130 {
131 if (sfError != OHOS::SURFACE_ERROR_OK) {
132 RequestBufferFail();
133 } else {
134 RequestBufferSuccess();
135 }
136 }
137
RequestBufferResult(OHOS::SurfaceBuffer * sbuffer)138 void StreamStatistics::RequestBufferResult(OHOS::SurfaceBuffer *sbuffer)
139 {
140 if (sbuffer == nullptr) {
141 RequestBufferFail();
142 } else {
143 RequestBufferSuccess();
144 }
145 }
146 } // end namespace OHOS::Camera
147