1 /*
2 * Copyright (c) 2022-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
16 #include <iostream>
17 #include <ctime>
18
19 #include "avsession_errors.h"
20 #include "avsession_dumper.h"
21
22 namespace OHOS::AVSession {
23 const int32_t TIME_MAX = 64;
24 const std::string ARGS_HELP = "-h";
25 const std::string ILLEGAL_INFORMATION = "AVSession service, enter '-h' for usage.\n";
26 const std::string ARGS_SHOW_METADATA = "-show_metadata";
27 const std::string ARGS_SHOW_SESSION_INFO = "-show_session_info";
28 const std::string ARGS_SHOW_CONTROLLER_INFO = "-show_controller_info";
29 const std::string ARGS_SHOW_ERROR_INFO = "-show_error_info";
30 const std::string ARGS_TRUSTED_DEVICES_INFO = "-show_trusted_devices_Info";
31
32 std::map<std::string, AVSessionDumper::DumpActionType> AVSessionDumper::funcMap_ = {
33 { ARGS_SHOW_METADATA, AVSessionDumper::ShowMetaData },
34 { ARGS_SHOW_SESSION_INFO, AVSessionDumper::ShowSessionInfo },
35 { ARGS_SHOW_CONTROLLER_INFO, AVSessionDumper::ShowControllerInfo },
36 { ARGS_SHOW_ERROR_INFO, AVSessionDumper::ShowErrorInfo },
37 { ARGS_TRUSTED_DEVICES_INFO, AVSessionDumper::ShowTrustedDevicesInfo },
38 };
39
40 std::map<int32_t, std::string> AVSessionDumper::playBackStates_ = {
41 { AVPlaybackState::PLAYBACK_STATE_INITIAL, "initial" },
42 { AVPlaybackState::PLAYBACK_STATE_PREPARE, "preparing" },
43 { AVPlaybackState::PLAYBACK_STATE_PLAY, "playing" },
44 { AVPlaybackState::PLAYBACK_STATE_PAUSE, "paused" },
45 { AVPlaybackState::PLAYBACK_STATE_FAST_FORWARD, "fast_forward" },
46 { AVPlaybackState::PLAYBACK_STATE_REWIND, "rewind" },
47 { AVPlaybackState::PLAYBACK_STATE_STOP, "stop" },
48 };
49
50 std::map<int32_t, std::string> AVSessionDumper::deviceTypeId_ = {
51 {DistributedHardware::DEVICE_TYPE_UNKNOWN, "unknown" },
52 {DistributedHardware::DEVICE_TYPE_WIFI_CAMERA, "camera" },
53 {DistributedHardware::DEVICE_TYPE_AUDIO, "audio" },
54 {DistributedHardware::DEVICE_TYPE_PC, "pc" },
55 {DistributedHardware::DEVICE_TYPE_PHONE, "phone" },
56 {DistributedHardware::DEVICE_TYPE_PAD, "pad" },
57 {DistributedHardware::DEVICE_TYPE_WATCH, "watch" },
58 {DistributedHardware::DEVICE_TYPE_CAR, "car" },
59 {DistributedHardware::DEVICE_TYPE_TV, "tv" },
60 };
61
62 std::map<int32_t, std::string> AVSessionDumper::loopMode_ = {
63 { AVPlaybackState::LOOP_MODE_SEQUENCE, "sequence" },
64 { AVPlaybackState::LOOP_MODE_SINGLE, "single" },
65 { AVPlaybackState::LOOP_MODE_LIST, "list" },
66 { AVPlaybackState::LOOP_MODE_SHUFFLE, "shuffle" },
67 };
68
69 std::vector<std::string> AVSessionDumper::errMessage_ = {};
70
ShowHelp(std::string & result) const71 void AVSessionDumper::ShowHelp(std::string& result) const
72 {
73 result.append("Usage:dump <command> [options]\n")
74 .append("Description:\n")
75 .append("-show_metadata :show all avsession metadata in the system\n")
76 .append("-show_session_info :show information of all sessions\n")
77 .append("-show_controller_info :show information of all controllers \n")
78 .append("-show_error_info :show error information about avsession\n")
79 .append("-show_trusted_devices_Info :show trusted devices Info\n");
80 }
81
ShowMetaData(std::string & result,const AVSessionService & sessionService)82 void AVSessionDumper::ShowMetaData(std::string& result, const AVSessionService& sessionService)
83 {
84 int32_t controllerIndex = 0;
85 int32_t itemIndex = 0;
86 for (const auto& it : sessionService.controllers_) {
87 result.append("ControllerIndex: " + std::to_string(++controllerIndex) + "\n");
88 for (const auto& item : it.second) {
89 result.append("ItemIndex: " + std::to_string(++itemIndex)+ "\n");
90 AVMetaData metaData;
91 item->GetAVMetaData(metaData);
92
93 result.append("Metadata:\n");
94 result.append(" assetid : " + metaData.GetAssetId() + "\n");
95 result.append(" title : " + metaData.GetTitle() + "\n");
96 result.append(" artist : " + metaData.GetArtist() + "\n");
97 result.append(" author : " + metaData.GetAuthor() + "\n");
98 result.append(" album : " + metaData.GetAlbum() + "\n");
99 result.append(" writer : " + metaData.GetWriter() + "\n");
100 result.append(" composer : " + metaData.GetComposer() + "\n");
101 result.append(" duration : " + std::to_string(metaData.GetDuration()) + "\n");
102 result.append(" media image url : " + metaData.GetMediaImageUri() + "\n");
103 result.append(" publish date : " + std::to_string(metaData.GetPublishDate()) + "\n");
104 result.append(" subtitle : " + metaData.GetSubTitle() + "\n");
105 result.append(" description : " + metaData.GetDescription() + "\n");
106 result.append(" lyric : " + metaData.GetLyric() + "\n");
107 result.append(" previous assetid : " + metaData.GetPreviousAssetId() + "\n");
108 result.append(" next assetid : " + metaData.GetNextAssetId() + "\n");
109 }
110 }
111 }
112
ShowTrustedDevicesInfo(std::string & result,const AVSessionService & sessionService)113 void AVSessionDumper::ShowTrustedDevicesInfo(std::string& result, const AVSessionService& sessionService)
114 {
115 std::vector<OHOS::DistributedHardware::DmDeviceInfo> deviceList;
116 DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList("av_session", "", deviceList);
117
118 std::string buff;
119 result.append("Trusted Devices Info:\n\n")
120 .append("Count : " + std::to_string(deviceList.size()) + "\n");
121 for (const auto& device : deviceList) {
122 buff=device.deviceId;
123 result.append(" device id : ");
124 result.append(buff + " ");
125
126 buff=device.deviceName;
127 result.append("\n device name : ");
128 result.append(buff + " ");
129
130 result.append("\n device type id : " + deviceTypeId_.find(device.deviceTypeId)->second);
131
132 buff=device.networkId;
133 result.append("\n network id : ");
134 result.append(buff + " ");
135
136 result.append("\n range : " + std::to_string(device.range));
137 result.append("\n");
138 }
139 }
140
ShowSessionInfo(std::string & result,const AVSessionService & sessionService)141 void AVSessionDumper::ShowSessionInfo(std::string& result, const AVSessionService& sessionService)
142 {
143 std::vector<sptr<AVSessionItem>> sessions = sessionService.GetContainer().GetAllSessions();
144 result.append("Session Information:\n\n")
145 .append("Count : " + std::to_string(sessions.size()));
146
147 AVSessionDescriptor descriptor;
148 for (const auto& session : sessions) {
149 descriptor = session->GetDescriptor();
150 std::string isActive = descriptor.isActive_ ? "true" : "false";
151 std::string isTopSession = descriptor.isTopSession_ ? "true" : "false";
152 result.append("\n\ncurrent session id : " + descriptor.sessionId_ + "\n")
153 .append("State:\n")
154 .append("is active : " + isActive + "\n")
155 .append("is the topsession : " + isTopSession)
156 .append("\n\nConfiguration:\n")
157 .append("pid : " + std::to_string(session->GetPid()) + "\n")
158 .append("uid : " + std::to_string(session->GetUid()) + "\n");
159 if (descriptor.sessionType_ == AVSession::SESSION_TYPE_AUDIO) {
160 result.append("session type : audio\n");
161 } else if (descriptor.sessionType_ == AVSession::SESSION_TYPE_VIDEO) {
162 result.append("session type : video\n");
163 } else {
164 result.append("session type is invalid.\n");
165 }
166
167 result.append("session tag : " + descriptor.sessionTag_ + "\n")
168 .append("bundle name : " + descriptor.elementName_.GetBundleName() + "\n")
169 .append("ability name : " + descriptor.elementName_.GetAbilityName() + "\n");
170
171 result.append("outputdevice\n")
172 .append(" the count of devices : " +
173 std::to_string(descriptor.outputDeviceInfo_.deviceInfos_.size()) +
174 "\n device id : ");
175 for (const auto& deviceInfo : descriptor.outputDeviceInfo_.deviceInfos_) {
176 result.append(deviceInfo.deviceId_ + " ");
177 }
178 result.append("\n device name : ");
179 for (const auto& deviceInfo : descriptor.outputDeviceInfo_.deviceInfos_) {
180 result.append(deviceInfo.deviceName_ + " ");
181 }
182 result.append("\n\nRelated Controllers:\n")
183 .append("the count of controllers : " + std::to_string(session->controllers_.size()) + "\n")
184 .append("pid of controllers : ");
185 for (const auto& it : session->controllers_) {
186 result.append(std::to_string(it.first) + " ");
187 }
188 }
189 }
190
ShowControllerInfo(std::string & result,const AVSessionService & sessionService)191 void AVSessionDumper::ShowControllerInfo(std::string& result, const AVSessionService& sessionService)
192 {
193 AVPlaybackState playbackState;
194 std::string temp;
195 int32_t controllerCount = 0;
196 for (const auto& it : sessionService.controllers_) {
197 for (const auto& controller : it.second) {
198 controllerCount++;
199 controller->GetAVPlaybackState(playbackState);
200
201 int32_t state = playbackState.GetState();
202 double speed = playbackState.GetSpeed();
203 AVPlaybackState::Position position = playbackState.GetPosition();
204 int64_t bufferedTime = playbackState.GetBufferedTime();
205 int32_t loopMode = playbackState.GetLoopMode();
206 std::string isFavorite = playbackState.GetFavorite() ? "true" : "false";
207
208 temp.append("\n\ncurretn controller pid : " + std::to_string(controller->GetPid()) + "\n")
209 .append("State:\n")
210 .append("state : " + playBackStates_.find(state)->second + "\n")
211 .append("speed : " + std::to_string(speed) + "\n")
212 .append("position\n")
213 .append(" elapsed time : " + std::to_string(position.elapsedTime_) + "\n")
214 .append(" update time : " + std::to_string(position.updateTime_) + "\n")
215 .append("buffered time : " + std::to_string(bufferedTime) + "\n")
216 .append("loopmode : " + loopMode_.find(loopMode)->second + "\n")
217 .append("is favorite : " + isFavorite + "\n")
218 .append("\nRelated Sessionid : " + controller->GetSessionId());
219 }
220 }
221 result.append("Controller Information:\n\n")
222 .append("Count : " + std::to_string(controllerCount))
223 .append(temp);
224 }
225
SetErrorInfo(const std::string & inErrMsg)226 void AVSessionDumper::SetErrorInfo(const std::string& inErrMsg)
227 {
228 time_t now = time(nullptr);
229 if (now == -1) {
230 SLOGE("get time failed");
231 return;
232 }
233 struct tm *locTime = localtime(&now);
234 if (locTime == nullptr) {
235 SLOGE("get localtime failed");
236 return;
237 }
238 char tempTime [TIME_MAX];
239 auto ret = strftime(tempTime, sizeof(tempTime), "%Y-%m-%d %H:%M:%S", locTime);
240 if (ret == 0) {
241 SLOGE("strftime failed");
242 return;
243 }
244 auto time = tempTime;
245 std::string msgInfo;
246 msgInfo.append(time + inErrMsg);
247 errMessage_.push_back(msgInfo);
248 }
249
ShowErrorInfo(std::string & result,const AVSessionService & sessionService)250 void AVSessionDumper::ShowErrorInfo(std::string& result, const AVSessionService& sessionService)
251 {
252 if (errMessage_.empty()) {
253 result.append("No Error Information!\n");
254 return;
255 }
256
257 int32_t errMsgCount = 0;
258 result.append("Error Information:\n\n");
259 for (auto iter = errMessage_.begin(); iter != errMessage_.end(); iter++) {
260 result.append(errMessage_.at(errMsgCount++) + "\n");
261 }
262 }
263
ProcessParameter(const std::string & arg,std::string & result,const AVSessionService & sessionService) const264 void AVSessionDumper::ProcessParameter(const std::string& arg, std::string& result,
265 const AVSessionService& sessionService) const
266 {
267 if (arg == ARGS_HELP) {
268 ShowHelp(result);
269 } else {
270 auto it = funcMap_.find(arg);
271 if (it != funcMap_.end()) {
272 it->second(result, sessionService);
273 } else {
274 ShowIllegalInfo(result);
275 }
276 }
277 }
278
ShowIllegalInfo(std::string & result) const279 void AVSessionDumper::ShowIllegalInfo(std::string& result) const
280 {
281 result.append(ILLEGAL_INFORMATION);
282 }
283
Dump(const std::vector<std::string> & args,std::string & result,const AVSessionService & sessionService) const284 void AVSessionDumper::Dump(const std::vector<std::string>& args, std::string& result,
285 const AVSessionService& sessionService) const
286 {
287 result.clear();
288 auto argsSize = args.size();
289 if (argsSize == 1) {
290 ProcessParameter(args[0], result, sessionService);
291 } else {
292 ShowIllegalInfo(result);
293 }
294 }
295 } // namespace OHOS::AVSession