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