• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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_PREPARING, "preparing" },
43     { AVPlaybackState::PLAYBACK_STATE_PLAYING, "playing" },
44     { AVPlaybackState::PLAYBACK_STATE_PAUSED, "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         std::string isRemote = descriptor.outputDeviceInfo_.isRemote_ ? "true" : "false";
172         std::vector<std::string> deviceIds = descriptor.outputDeviceInfo_.deviceIds_;
173         result.append("outputdevice\n")
174             .append("        outputdevice is remote       : " + isRemote + "\n")
175             .append("        the count of devices         : " + std::to_string(deviceIds.size()) +
176             "\n        device id                    : ");
177         for (const auto& deviceId : deviceIds) {
178             result.append(deviceId + "  ");
179         }
180         result.append("\n        device name                  : ");
181         std::vector<std::string> deviceNames = descriptor.outputDeviceInfo_.deviceNames_;
182         for (const auto& deviceName : deviceNames) {
183             result.append(deviceName + "  ");
184         }
185         result.append("\n\nRelated Controllers:\n")
186             .append("the count of controllers     : " + std::to_string(session->controllers_.size()) + "\n")
187             .append("pid of controllers           : ");
188         for (const auto& it : session->controllers_) {
189             result.append(std::to_string(it.first) + "  ");
190         }
191     }
192 }
193 
ShowControllerInfo(std::string & result,const AVSessionService & sessionService)194 void AVSessionDumper::ShowControllerInfo(std::string& result, const AVSessionService& sessionService)
195 {
196     AVPlaybackState playbackState;
197     std::string temp;
198     int32_t controllerCount = 0;
199     for (const auto& it : sessionService.controllers_) {
200         for (const auto& controller : it.second) {
201             controllerCount++;
202             controller->GetAVPlaybackState(playbackState);
203 
204             int32_t state = playbackState.GetState();
205             double speed = playbackState.GetSpeed();
206             AVPlaybackState::Position position = playbackState.GetPosition();
207             int64_t bufferedTime = playbackState.GetBufferedTime();
208             int32_t loopMode = playbackState.GetLoopMode();
209             std::string isFavorite = playbackState.GetFavorite() ? "true" : "false";
210 
211             temp.append("\n\ncurretn controller pid       : " + std::to_string(controller->GetPid()) + "\n")
212                 .append("State:\n")
213                 .append("state                        : " + playBackStates_.find(state)->second + "\n")
214                 .append("speed                        : " + std::to_string(speed) + "\n")
215                 .append("position\n")
216                 .append("        elapsed time                 : " + std::to_string(position.elapsedTime_) + "\n")
217                 .append("        update time                  : " + std::to_string(position.updateTime_) + "\n")
218                 .append("buffered time                : " + std::to_string(bufferedTime) + "\n")
219                 .append("loopmode                     : " + loopMode_.find(loopMode)->second + "\n")
220                 .append("is favorite                  : " + isFavorite + "\n")
221                 .append("\nRelated Sessionid            : " + controller->GetSessionId());
222         }
223     }
224     result.append("Controller Information:\n\n")
225         .append("Count                        : " + std::to_string(controllerCount))
226         .append(temp);
227 }
228 
SetErrorInfo(const std::string & inErrMsg)229 void AVSessionDumper::SetErrorInfo(const std::string& inErrMsg)
230 {
231     time_t now = time(nullptr);
232     if (now == -1) {
233         SLOGE("get time failed");
234         return;
235     }
236     struct tm *locTime = localtime(&now);
237     if (locTime == nullptr) {
238         SLOGE("get localtime failed");
239         return;
240     }
241     char tempTime [TIME_MAX];
242     auto ret = strftime(tempTime, sizeof(tempTime), "%Y-%m-%d %H:%M:%S", locTime);
243     if (ret == 0) {
244         SLOGE("strftime failed");
245         return;
246     }
247     auto time  = tempTime;
248     std::string msgInfo;
249     msgInfo.append(time + inErrMsg);
250     errMessage_.push_back(msgInfo);
251 }
252 
ShowErrorInfo(std::string & result,const AVSessionService & sessionService)253 void AVSessionDumper::ShowErrorInfo(std::string& result, const AVSessionService& sessionService)
254 {
255     if (errMessage_.empty()) {
256         result.append("No Error Information!\n");
257         return;
258     }
259 
260     int32_t errMsgCount = 0;
261     result.append("Error Information:\n\n");
262     for (auto iter = errMessage_.begin(); iter != errMessage_.end(); iter++) {
263         result.append(errMessage_.at(errMsgCount++) + "\n");
264     }
265 }
266 
ProcessParameter(const std::string & arg,std::string & result,const AVSessionService & sessionService) const267 void AVSessionDumper::ProcessParameter(const std::string& arg, std::string& result,
268                                        const AVSessionService& sessionService) const
269 {
270     if (arg == ARGS_HELP) {
271         ShowHelp(result);
272     } else {
273         auto it = funcMap_.find(arg);
274         if (it != funcMap_.end()) {
275             it->second(result, sessionService);
276         } else {
277             ShowIllegalInfo(result);
278         }
279     }
280 }
281 
ShowIllegalInfo(std::string & result) const282 void AVSessionDumper::ShowIllegalInfo(std::string& result) const
283 {
284     result.append(ILLEGAL_INFORMATION);
285 }
286 
Dump(const std::vector<std::string> & args,std::string & result,const AVSessionService & sessionService) const287 void AVSessionDumper::Dump(const std::vector<std::string>& args, std::string& result,
288     const AVSessionService& sessionService) const
289 {
290     result.clear();
291     auto argsSize = args.size();
292     if (argsSize == 1) {
293         ProcessParameter(args[0], result, sessionService);
294     } else {
295         ShowIllegalInfo(result);
296     }
297 }
298 } // namespace OHOS::AVSession