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
18 namespace OHOS::AVSession {
19 #ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
GetInstance()20 AVSessionSysEvent& AVSessionSysEvent::GetInstance()
21 {
22 static AVSessionSysEvent avSessionSysEvent;
23 return avSessionSysEvent;
24 }
25
AVSessionSysEvent()26 AVSessionSysEvent::AVSessionSysEvent() : optCounts_ {},
27 timer_(nullptr), timerId_(0), lifeCycleInfos_ {}, controllerCommandInfos_ {}
28 {
29 }
30
AddOperationCount(Operation operation)31 void AVSessionSysEvent::AddOperationCount(Operation operation)
32 {
33 optCounts_[operation]++;
34 }
35
Reset()36 void AVSessionSysEvent::Reset()
37 {
38 std::lock_guard lockGuard(lock_);
39 optCounts_.clear();
40 lifeCycleInfos_.clear();
41 controllerCommandInfos_.clear();
42 audioStatuses_.clear();
43 }
44
Regiter()45 void AVSessionSysEvent::Regiter()
46 {
47 if (timer_ != nullptr) {
48 return;
49 }
50
51 timer_ = std::make_unique<OHOS::Utils::Timer>("EventStatisticTimer");
52 auto timeCallback = [this]() {
53 HiSysWriteStatistic("CONTROL_COMMAND_STATISTICS", "PLAY_COUNT", optCounts_[Operation::OPT_PLAY],
54 "PAUSE_COUNT", optCounts_[Operation::OPT_PAUSE], "STOP_COUNT", optCounts_[Operation::OPT_STOP],
55 "PLAY_NEXT_COUNT", optCounts_[Operation::OPT_PLAY_NEXT],
56 "PLAY_PREVIOUS_COUNT", optCounts_[Operation::OPT_PLAY_PREVIOUS],
57 "FAST_FORWARD_COUNT", optCounts_[Operation::OPT_FAST_FORWARD],
58 "REWIND_COUNT", optCounts_[Operation::OPT_REWIND],
59 "SEEK_COUNT", optCounts_[Operation::OPT_SEEK],
60 "SET_SPEED_COUNT", optCounts_[Operation::OPT_SET_SPEED],
61 "SET_LOOP_MODE_COUNT", optCounts_[Operation::OPT_SET_LOOP_MODE],
62 "TOGGLE_FAVORITE_COUNT", optCounts_[Operation::OPT_TOGGLE_FAVORITE]);
63
64 for (const auto& it : lifeCycleInfos_) {
65 HiSysWriteStatistic("SESSION_LIFECYCLE_STATISTICS", "BUNDLE_NAME", it.bundleName_,
66 "APP_STATUS", it.appStatus_, "SESSION_TYPE", it.sessionType_,
67 "SESSION_LIFE_CYCLE", it.isCreateSession_);
68 }
69
70 for (const auto& it : controllerCommandInfos_) {
71 HiSysWriteStatistic("CONTROL_COMMAND_STATISTICS", "BUNDLE_NAME", it.bundleName_,
72 "CONTROLLER_PID", it.controllerPid_, "CMD", it.controllerCmd_,
73 "DETAILED_MSG", "receiver handle command");
74 }
75
76 HiSysWriteStatistic("SESSION_LIFECYCLE_STATISTICS",
77 "CREATE_SESSION_COUNT", optCounts_[Operation::OPT_CREATE_SESSION],
78 "DELETE_SESSION_COUNT", optCounts_[Operation::OPT_DELETE_SESSION]);
79
80 uint32_t allCtrlCmdCount = optCounts_[Operation::OPT_ALL_CTRL_COMMAND];
81 uint32_t allSuccCmdCount = optCounts_[Operation::OPT_SUCCESS_CTRL_COMMAND];
82 if ((allCtrlCmdCount != 0) && (allSuccCmdCount <= allCtrlCmdCount)) {
83 float failedRate = (allCtrlCmdCount - allSuccCmdCount) / (allCtrlCmdCount * MULTIPLE);
84 HiSysWriteStatistic("CONTROL_COMMAND_FAILED_RATE", "ALL_CTRL_COMMAND_COUNT", allCtrlCmdCount,
85 "ALL_SUCCESS_CTRL_COMMAND", allSuccCmdCount, "COMMAND_FAILED_RATE", failedRate);
86 } else {
87 HiSysWriteStatistic("CONTROL_COMMAND_FAILED_RATE", "ALL_CTRL_COMMAND_COUNT", allCtrlCmdCount,
88 "ALL_SUCCESS_CTRL_COMMAND", allSuccCmdCount, "COMMAND_FAILED_RATE", 0);
89 }
90 Reset();
91 };
92 timerId_ = timer_->Register(timeCallback, NOTIFY_TIME_INTERVAL, false);
93 timer_->Setup();
94 }
95
Unregister()96 void AVSessionSysEvent::Unregister()
97 {
98 if (timer_ != nullptr) {
99 timer_->Shutdown();
100 timer_->Unregister(timerId_);
101 timer_ = nullptr;
102 }
103 }
104
AddLifeCycleInfo(const std::string & bundleName,bool appStatus,const int32_t & sessionType,bool isCreateSession)105 void AVSessionSysEvent::AddLifeCycleInfo(const std::string& bundleName, bool appStatus,
106 const int32_t& sessionType, bool isCreateSession)
107 {
108 AVSessionSysEvent::LifeCycleInfo lifeCycleInfo;
109 lifeCycleInfo.bundleName_ = bundleName;
110 lifeCycleInfo.appStatus_ = appStatus;
111 lifeCycleInfo.sessionType_ = sessionType;
112 lifeCycleInfo.isCreateSession_ = isCreateSession;
113 lifeCycleInfos_.push_back(lifeCycleInfo);
114 }
115
AddControllerCommandInfo(const std::string & bundleName,const pid_t & controllerPid,const int32_t & controllerCmd,const int32_t & sessionType)116 void AVSessionSysEvent::AddControllerCommandInfo(const std::string& bundleName, const pid_t& controllerPid,
117 const int32_t& controllerCmd, const int32_t& sessionType)
118 {
119 AVSessionSysEvent::ControllerCommandInfo controllerCommandInfo;
120 controllerCommandInfo.bundleName_ = bundleName;
121 controllerCommandInfo.controllerPid_ = controllerPid;
122 controllerCommandInfo.controllerCmd_ = controllerCmd;
123 controllerCommandInfos_.push_back(controllerCommandInfo);
124 }
125
GetAudioStatus(pid_t uid)126 int32_t AVSessionSysEvent::GetAudioStatus(pid_t uid)
127 {
128 std::lock_guard lockGuard(lock_);
129 if (audioStatuses_.find(uid) != audioStatuses_.end()) {
130 return audioStatuses_[uid];
131 }
132 return 0;
133 }
134
SetAudioStatus(pid_t uid,int32_t rendererState)135 void AVSessionSysEvent::SetAudioStatus(pid_t uid, int32_t rendererState)
136 {
137 std::lock_guard lockGuard(lock_);
138 audioStatuses_[uid] = rendererState;
139 }
140
141 #endif
142 } // namespace OHOS::AVSession