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 *
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
16 #include "avsession_sysevent.h"
17 #include "avsession_log.h"
18
19 namespace OHOS::AVSession {
20 #ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
GetInstance()21 AVSessionSysEvent& AVSessionSysEvent::GetInstance()
22 {
23 static AVSessionSysEvent avSessionSysEvent;
24 return avSessionSysEvent;
25 }
26
AVSessionSysEvent()27 AVSessionSysEvent::AVSessionSysEvent() : optCounts_ {},
28 timer_(nullptr), timerId_(0), lifeCycleInfos_ {}, controllerCommandInfos_ {}, lowQualityInfos_ {}
29 {
30 }
31
AddOperationCount(Operation operation)32 void AVSessionSysEvent::AddOperationCount(Operation operation)
33 {
34 std::lock_guard lockGuard(lock_);
35 auto it = optCounts_.find(operation);
36 if (it == optCounts_.end()) {
37 optCounts_.insert(std::make_pair(operation, 0));
38 it = optCounts_.find(operation);
39 }
40 it->second++;
41 }
42
Reset()43 void AVSessionSysEvent::Reset()
44 {
45 std::lock_guard lockGuard(lock_);
46 optCounts_.clear();
47 lifeCycleInfos_.clear();
48 controllerCommandInfos_.clear();
49 audioStatuses_.clear();
50 lowQualityInfos_.clear();
51 }
52
ReportLowQuality()53 void AVSessionSysEvent::ReportLowQuality()
54 {
55 for (auto it = lowQualityInfos_.begin(); it != lowQualityInfos_.end(); it++) {
56 SLOGD("report low quality for %{public}s", it->second.bundleName_.c_str());
57 HiSysWriteStatistic("PLAYING_COMBIND_AVSESSION_STATIS",
58 "BUNDLE_NAME", it->second.bundleName_,
59 "PLAY_DURATION", it->second.playDuration_,
60 "STREAM_USAGE", it->second.streamUsage_,
61 "PLAY_BACK", it->second.isBack_,
62 "AUDIO_ACTIVE", it->second.isAudioActive_,
63 "AVSESSION_META_QUALITY", it->second.metaDataQuality_,
64 "AVSESSION_COMMAND_QUALITY", it->second.cmdQuality_,
65 "AVSESSION_PLAYBACK_STATE", it->second.playbackState_);
66 }
67 }
68
Regiter()69 void AVSessionSysEvent::Regiter()
70 {
71 if (timer_ != nullptr) {
72 return;
73 }
74
75 timer_ = std::make_unique<OHOS::Utils::Timer>("EventStatisticTimer");
76 auto timeCallback = [this]() {
77 std::lock_guard lockGuard(lock_);
78 HiSysWriteStatistic("CONTROL_COMMAND_STATISTICS", "PLAY_COUNT", optCounts_[Operation::OPT_PLAY],
79 "PAUSE_COUNT", optCounts_[Operation::OPT_PAUSE], "STOP_COUNT", optCounts_[Operation::OPT_STOP],
80 "PLAY_NEXT_COUNT", optCounts_[Operation::OPT_PLAY_NEXT],
81 "PLAY_PREVIOUS_COUNT", optCounts_[Operation::OPT_PLAY_PREVIOUS],
82 "FAST_FORWARD_COUNT", optCounts_[Operation::OPT_FAST_FORWARD],
83 "REWIND_COUNT", optCounts_[Operation::OPT_REWIND],
84 "SEEK_COUNT", optCounts_[Operation::OPT_SEEK],
85 "SET_SPEED_COUNT", optCounts_[Operation::OPT_SET_SPEED],
86 "SET_LOOP_MODE_COUNT", optCounts_[Operation::OPT_SET_LOOP_MODE],
87 "SET_TARGET_LOOP_MODE_COUNT", optCounts_[Operation::OPT_SET_TARGET_LOOP_MODE],
88 "TOGGLE_FAVORITE_COUNT", optCounts_[Operation::OPT_TOGGLE_FAVORITE]);
89
90 for (const auto& it : lifeCycleInfos_) {
91 HiSysWriteStatistic("SESSION_LIFECYCLE_STATISTICS", "BUNDLE_NAME", it.bundleName_,
92 "APP_STATUS", it.appStatus_, "SESSION_TYPE", it.sessionType_,
93 "SESSION_LIFE_CYCLE", it.isCreateSession_);
94 }
95
96 for (const auto& it : controllerCommandInfos_) {
97 HiSysWriteStatistic("CONTROL_COMMAND_STATISTICS", "BUNDLE_NAME", it.bundleName_,
98 "CONTROLLER_PID", it.controllerPid_, "CMD", it.controllerCmd_,
99 "DETAILED_MSG", "receiver handle command");
100 }
101
102 ReportLowQuality();
103
104 HiSysWriteStatistic("SESSION_LIFECYCLE_STATISTICS",
105 "CREATE_SESSION_COUNT", optCounts_[Operation::OPT_CREATE_SESSION],
106 "DELETE_SESSION_COUNT", optCounts_[Operation::OPT_DELETE_SESSION]);
107
108 uint32_t allCtrlCmdCount = optCounts_[Operation::OPT_ALL_CTRL_COMMAND];
109 uint32_t allSuccCmdCount = optCounts_[Operation::OPT_SUCCESS_CTRL_COMMAND];
110 if ((allCtrlCmdCount != 0) && (allSuccCmdCount <= allCtrlCmdCount)) {
111 // LCOV_EXCL_START
112 float failedRate = (allCtrlCmdCount - allSuccCmdCount) / (allCtrlCmdCount * MULTIPLE);
113 HiSysWriteStatistic("CONTROL_COMMAND_FAILED_RATE", "ALL_CTRL_COMMAND_COUNT", allCtrlCmdCount,
114 "ALL_SUCCESS_CTRL_COMMAND", allSuccCmdCount, "COMMAND_FAILED_RATE", failedRate);
115 } else {
116 HiSysWriteStatistic("CONTROL_COMMAND_FAILED_RATE", "ALL_CTRL_COMMAND_COUNT", allCtrlCmdCount,
117 "ALL_SUCCESS_CTRL_COMMAND", allSuccCmdCount, "COMMAND_FAILED_RATE", 0);
118 // LCOV_EXCL_STOP
119 }
120 Reset();
121 };
122 timerId_ = timer_->Register(timeCallback, NOTIFY_TIME_INTERVAL, false);
123 timer_->Setup();
124 }
125
126 // LCOV_EXCL_START
Unregister()127 void AVSessionSysEvent::Unregister()
128 {
129 if (timer_ != nullptr) {
130 timer_->Shutdown();
131 timer_->Unregister(timerId_);
132 timer_ = nullptr;
133 }
134 }
135 // LCOV_EXCL_STOP
136
AddLifeCycleInfo(const std::string & bundleName,bool appStatus,const int32_t & sessionType,bool isCreateSession)137 void AVSessionSysEvent::AddLifeCycleInfo(const std::string& bundleName, bool appStatus,
138 const int32_t& sessionType, bool isCreateSession)
139 {
140 AVSessionSysEvent::LifeCycleInfo lifeCycleInfo;
141 lifeCycleInfo.bundleName_ = bundleName;
142 lifeCycleInfo.appStatus_ = appStatus;
143 lifeCycleInfo.sessionType_ = sessionType;
144 lifeCycleInfo.isCreateSession_ = isCreateSession;
145 lifeCycleInfos_.push_back(lifeCycleInfo);
146 }
147
AddLowQualityInfo(AVSessionSysEvent::BackControlReportInfo & reportInfo)148 void AVSessionSysEvent::AddLowQualityInfo(AVSessionSysEvent::BackControlReportInfo &reportInfo)
149 {
150 if (lowQualityInfos_.find(reportInfo.bundleName_) == lowQualityInfos_.end()) {
151 lowQualityInfos_.insert(std::make_pair(reportInfo.bundleName_, reportInfo));
152 }
153 }
154
155 // LCOV_EXCL_START
AddControllerCommandInfo(const std::string & bundleName,const pid_t & controllerPid,const int32_t & controllerCmd,const int32_t & sessionType)156 void AVSessionSysEvent::AddControllerCommandInfo(const std::string& bundleName, const pid_t& controllerPid,
157 const int32_t& controllerCmd, const int32_t& sessionType)
158 {
159 AVSessionSysEvent::ControllerCommandInfo controllerCommandInfo;
160 controllerCommandInfo.bundleName_ = bundleName;
161 controllerCommandInfo.controllerPid_ = controllerPid;
162 controllerCommandInfo.controllerCmd_ = controllerCmd;
163 controllerCommandInfos_.push_back(controllerCommandInfo);
164 }
165
GetAudioStatus(pid_t uid)166 int32_t AVSessionSysEvent::GetAudioStatus(pid_t uid)
167 {
168 std::lock_guard lockGuard(lock_);
169 if (audioStatuses_.find(uid) != audioStatuses_.end()) {
170 return audioStatuses_[uid];
171 }
172 return 0;
173 }
174 // LCOV_EXCL_STOP
175
SetAudioStatus(pid_t uid,int32_t rendererState)176 void AVSessionSysEvent::SetAudioStatus(pid_t uid, int32_t rendererState)
177 {
178 std::lock_guard lockGuard(lock_);
179 audioStatuses_[uid] = rendererState;
180 }
181
182 #endif
183 } // namespace OHOS::AVSession