180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#include <iostream> 1780922886Sopenharmony_ci#include <ctime> 1880922886Sopenharmony_ci 1980922886Sopenharmony_ci#include "avsession_errors.h" 2080922886Sopenharmony_ci#include "avsession_dumper.h" 2180922886Sopenharmony_ci 2280922886Sopenharmony_cinamespace OHOS::AVSession { 2380922886Sopenharmony_ciconst int32_t TIME_MAX = 64; 2480922886Sopenharmony_ciconst std::string ARGS_HELP = "-h"; 2580922886Sopenharmony_ciconst std::string ILLEGAL_INFORMATION = "AVSession service, enter '-h' for usage.\n"; 2680922886Sopenharmony_ciconst std::string ARGS_SHOW_METADATA = "-show_metadata"; 2780922886Sopenharmony_ciconst std::string ARGS_SHOW_SESSION_INFO = "-show_session_info"; 2880922886Sopenharmony_ciconst std::string ARGS_SHOW_CONTROLLER_INFO = "-show_controller_info"; 2980922886Sopenharmony_ciconst std::string ARGS_SHOW_ERROR_INFO = "-show_error_info"; 3080922886Sopenharmony_ciconst std::string ARGS_TRUSTED_DEVICES_INFO = "-show_trusted_devices_Info"; 3180922886Sopenharmony_ci 3280922886Sopenharmony_cistd::map<std::string, AVSessionDumper::DumpActionType> AVSessionDumper::funcMap_ = { 3380922886Sopenharmony_ci { ARGS_SHOW_METADATA, AVSessionDumper::ShowMetaData }, 3480922886Sopenharmony_ci { ARGS_SHOW_SESSION_INFO, AVSessionDumper::ShowSessionInfo }, 3580922886Sopenharmony_ci { ARGS_SHOW_CONTROLLER_INFO, AVSessionDumper::ShowControllerInfo }, 3680922886Sopenharmony_ci { ARGS_SHOW_ERROR_INFO, AVSessionDumper::ShowErrorInfo }, 3780922886Sopenharmony_ci { ARGS_TRUSTED_DEVICES_INFO, AVSessionDumper::ShowTrustedDevicesInfo }, 3880922886Sopenharmony_ci}; 3980922886Sopenharmony_ci 4080922886Sopenharmony_cistd::map<int32_t, std::string> AVSessionDumper::playBackStates_ = { 4180922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_INITIAL, "initial" }, 4280922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_PREPARE, "preparing" }, 4380922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_PLAY, "playing" }, 4480922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_PAUSE, "paused" }, 4580922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_FAST_FORWARD, "fast_forward" }, 4680922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_REWIND, "rewind" }, 4780922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_STOP, "stop" }, 4880922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_COMPLETED, "complete" }, 4980922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_RELEASED, "released" }, 5080922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_ERROR, "error" }, 5180922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_IDLE, "idle" }, 5280922886Sopenharmony_ci { AVPlaybackState::PLAYBACK_STATE_BUFFERING, "buffering" }, 5380922886Sopenharmony_ci}; 5480922886Sopenharmony_ci 5580922886Sopenharmony_cistd::map<int32_t, std::string> AVSessionDumper::deviceTypeId_ = { 5680922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_UNKNOWN, "unknown" }, 5780922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_WIFI_CAMERA, "camera" }, 5880922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_AUDIO, "audio" }, 5980922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_PC, "pc" }, 6080922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_PHONE, "phone" }, 6180922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_PAD, "pad" }, 6280922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_WATCH, "watch" }, 6380922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_CAR, "car" }, 6480922886Sopenharmony_ci {DistributedHardware::DEVICE_TYPE_TV, "tv" }, 6580922886Sopenharmony_ci}; 6680922886Sopenharmony_ci 6780922886Sopenharmony_cistd::map<int32_t, std::string> AVSessionDumper::loopMode_ = { 6880922886Sopenharmony_ci { AVPlaybackState::LOOP_MODE_SEQUENCE, "sequence" }, 6980922886Sopenharmony_ci { AVPlaybackState::LOOP_MODE_SINGLE, "single" }, 7080922886Sopenharmony_ci { AVPlaybackState::LOOP_MODE_LIST, "list" }, 7180922886Sopenharmony_ci { AVPlaybackState::LOOP_MODE_SHUFFLE, "shuffle" }, 7280922886Sopenharmony_ci { AVPlaybackState::LOOP_MODE_CUSTOM, "custom" }, 7380922886Sopenharmony_ci}; 7480922886Sopenharmony_ci 7580922886Sopenharmony_cistd::vector<std::string> AVSessionDumper::errMessage_ = {}; 7680922886Sopenharmony_ci 7780922886Sopenharmony_civoid AVSessionDumper::ShowHelp(std::string& result) const 7880922886Sopenharmony_ci{ 7980922886Sopenharmony_ci result.append("Usage:dump <command> [options]\n") 8080922886Sopenharmony_ci .append("Description:\n") 8180922886Sopenharmony_ci .append("-show_metadata :show all avsession metadata in the system\n") 8280922886Sopenharmony_ci .append("-show_session_info :show information of all sessions\n") 8380922886Sopenharmony_ci .append("-show_controller_info :show information of all controllers \n") 8480922886Sopenharmony_ci .append("-show_error_info :show error information about avsession\n") 8580922886Sopenharmony_ci .append("-show_trusted_devices_Info :show trusted devices Info\n"); 8680922886Sopenharmony_ci} 8780922886Sopenharmony_ci 8880922886Sopenharmony_civoid AVSessionDumper::ShowMetaData(std::string& result, const AVSessionService& sessionService) 8980922886Sopenharmony_ci{ 9080922886Sopenharmony_ci int32_t controllerIndex = 0; 9180922886Sopenharmony_ci int32_t itemIndex = 0; 9280922886Sopenharmony_ci for (const auto& it : sessionService.controllers_) { 9380922886Sopenharmony_ci result.append("ControllerIndex: " + std::to_string(++controllerIndex) + "\n"); 9480922886Sopenharmony_ci for (const auto& item : it.second) { 9580922886Sopenharmony_ci result.append("ItemIndex: " + std::to_string(++itemIndex)+ "\n"); 9680922886Sopenharmony_ci AVMetaData metaData; 9780922886Sopenharmony_ci item->GetAVMetaData(metaData); 9880922886Sopenharmony_ci 9980922886Sopenharmony_ci result.append("Metadata:\n"); 10080922886Sopenharmony_ci result.append(" assetid : " + metaData.GetAssetId() + "\n"); 10180922886Sopenharmony_ci result.append(" title : " + metaData.GetTitle() + "\n"); 10280922886Sopenharmony_ci result.append(" artist : " + metaData.GetArtist() + "\n"); 10380922886Sopenharmony_ci result.append(" author : " + metaData.GetAuthor() + "\n"); 10480922886Sopenharmony_ci result.append(" avqueue name : " + metaData.GetAVQueueName() + "\n"); 10580922886Sopenharmony_ci result.append(" avqueue id : " + metaData.GetAVQueueId() + "\n"); 10680922886Sopenharmony_ci result.append(" avqueue url : " + metaData.GetAVQueueImageUri() + "\n"); 10780922886Sopenharmony_ci result.append(" album : " + metaData.GetAlbum() + "\n"); 10880922886Sopenharmony_ci result.append(" writer : " + metaData.GetWriter() + "\n"); 10980922886Sopenharmony_ci result.append(" composer : " + metaData.GetComposer() + "\n"); 11080922886Sopenharmony_ci result.append(" duration : " + std::to_string(metaData.GetDuration()) + "\n"); 11180922886Sopenharmony_ci result.append(" media image url : " + metaData.GetMediaImageUri() + "\n"); 11280922886Sopenharmony_ci result.append(" publish date : " + std::to_string(metaData.GetPublishDate()) + "\n"); 11380922886Sopenharmony_ci result.append(" subtitle : " + metaData.GetSubTitle() + "\n"); 11480922886Sopenharmony_ci result.append(" description : " + metaData.GetDescription() + "\n"); 11580922886Sopenharmony_ci result.append(" lyric : " + metaData.GetLyric() + "\n"); 11680922886Sopenharmony_ci result.append(" previous assetid : " + metaData.GetPreviousAssetId() + "\n"); 11780922886Sopenharmony_ci result.append(" next assetid : " + metaData.GetNextAssetId() + "\n"); 11880922886Sopenharmony_ci result.append(" skip intervals : " + std::to_string(metaData.GetSkipIntervals()) + "\n"); 11980922886Sopenharmony_ci result.append(" filter : " + std::to_string(metaData.GetFilter()) + "\n"); 12080922886Sopenharmony_ci } 12180922886Sopenharmony_ci } 12280922886Sopenharmony_ci} 12380922886Sopenharmony_ci 12480922886Sopenharmony_civoid AVSessionDumper::ShowTrustedDevicesInfo(std::string& result, const AVSessionService& sessionService) 12580922886Sopenharmony_ci{ 12680922886Sopenharmony_ci std::vector<OHOS::DistributedHardware::DmDeviceInfo> deviceList; 12780922886Sopenharmony_ci DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList("av_session", "", deviceList); 12880922886Sopenharmony_ci 12980922886Sopenharmony_ci std::string buff; 13080922886Sopenharmony_ci result.append("Trusted Devices Info:\n\n") 13180922886Sopenharmony_ci .append("Count : " + std::to_string(deviceList.size()) + "\n"); 13280922886Sopenharmony_ci for (const auto& device : deviceList) { 13380922886Sopenharmony_ci buff=device.deviceId; 13480922886Sopenharmony_ci result.append(" device id : "); 13580922886Sopenharmony_ci result.append(buff + " "); 13680922886Sopenharmony_ci 13780922886Sopenharmony_ci buff=device.deviceName; 13880922886Sopenharmony_ci result.append("\n device name : "); 13980922886Sopenharmony_ci result.append(buff + " "); 14080922886Sopenharmony_ci 14180922886Sopenharmony_ci result.append("\n device type id : " + deviceTypeId_.find(device.deviceTypeId)->second); 14280922886Sopenharmony_ci 14380922886Sopenharmony_ci buff=device.networkId; 14480922886Sopenharmony_ci result.append("\n network id : "); 14580922886Sopenharmony_ci result.append(buff + " "); 14680922886Sopenharmony_ci 14780922886Sopenharmony_ci result.append("\n range : " + std::to_string(device.range)); 14880922886Sopenharmony_ci result.append("\n"); 14980922886Sopenharmony_ci } 15080922886Sopenharmony_ci} 15180922886Sopenharmony_ci 15280922886Sopenharmony_civoid AVSessionDumper::ShowSessionInfo(std::string& result, const AVSessionService& sessionService) 15380922886Sopenharmony_ci{ 15480922886Sopenharmony_ci std::vector<sptr<AVSessionItem>> sessions = sessionService.GetContainer().GetAllSessions(); 15580922886Sopenharmony_ci result.append("Session Information:\n\n") 15680922886Sopenharmony_ci .append("Count : " + std::to_string(sessions.size())); 15780922886Sopenharmony_ci 15880922886Sopenharmony_ci AVSessionDescriptor descriptor; 15980922886Sopenharmony_ci for (const auto& session : sessions) { 16080922886Sopenharmony_ci descriptor = session->GetDescriptor(); 16180922886Sopenharmony_ci std::string isActive = descriptor.isActive_ ? "true" : "false"; 16280922886Sopenharmony_ci std::string isTopSession = descriptor.isTopSession_ ? "true" : "false"; 16380922886Sopenharmony_ci result.append("\n\ncurrent session id : " + descriptor.sessionId_ + "\n") 16480922886Sopenharmony_ci .append("State:\n") 16580922886Sopenharmony_ci .append("is active : " + isActive + "\n") 16680922886Sopenharmony_ci .append("is the topsession : " + isTopSession) 16780922886Sopenharmony_ci .append("\n\nConfiguration:\n") 16880922886Sopenharmony_ci .append("pid : " + std::to_string(session->GetPid()) + "\n") 16980922886Sopenharmony_ci .append("uid : " + std::to_string(session->GetUid()) + "\n"); 17080922886Sopenharmony_ci if (descriptor.sessionType_ == AVSession::SESSION_TYPE_AUDIO) { 17180922886Sopenharmony_ci result.append("session type : audio\n"); 17280922886Sopenharmony_ci } else if (descriptor.sessionType_ == AVSession::SESSION_TYPE_VIDEO) { 17380922886Sopenharmony_ci result.append("session type : video\n"); 17480922886Sopenharmony_ci } else if (descriptor.sessionType_ == AVSession::SESSION_TYPE_VOICE_CALL) { 17580922886Sopenharmony_ci result.append("session type : voice_call\n"); 17680922886Sopenharmony_ci } else if (descriptor.sessionType_ == AVSession::SESSION_TYPE_VIDEO_CALL) { 17780922886Sopenharmony_ci result.append("session type : video_call\n"); 17880922886Sopenharmony_ci } else { 17980922886Sopenharmony_ci result.append("session type is invalid.\n"); 18080922886Sopenharmony_ci } 18180922886Sopenharmony_ci 18280922886Sopenharmony_ci result.append("session tag : " + descriptor.sessionTag_ + "\n") 18380922886Sopenharmony_ci .append("bundle name : " + descriptor.elementName_.GetBundleName() + "\n") 18480922886Sopenharmony_ci .append("ability name : " + descriptor.elementName_.GetAbilityName() + "\n"); 18580922886Sopenharmony_ci 18680922886Sopenharmony_ci result.append("outputdevice\n") 18780922886Sopenharmony_ci .append(" the count of devices : " + 18880922886Sopenharmony_ci std::to_string(descriptor.outputDeviceInfo_.deviceInfos_.size()) + 18980922886Sopenharmony_ci "\n device id : "); 19080922886Sopenharmony_ci for (const auto& deviceInfo : descriptor.outputDeviceInfo_.deviceInfos_) { 19180922886Sopenharmony_ci result.append(deviceInfo.deviceId_ + " "); 19280922886Sopenharmony_ci } 19380922886Sopenharmony_ci result.append("\n device name : "); 19480922886Sopenharmony_ci for (const auto& deviceInfo : descriptor.outputDeviceInfo_.deviceInfos_) { 19580922886Sopenharmony_ci result.append(deviceInfo.deviceName_ + " "); 19680922886Sopenharmony_ci } 19780922886Sopenharmony_ci result.append("\n\nRelated Controllers:\n") 19880922886Sopenharmony_ci .append("the count of controllers : " + std::to_string(session->controllers_.size()) + "\n") 19980922886Sopenharmony_ci .append("pid of controllers : "); 20080922886Sopenharmony_ci for (const auto& it : session->controllers_) { 20180922886Sopenharmony_ci result.append(std::to_string(it.first) + " "); 20280922886Sopenharmony_ci } 20380922886Sopenharmony_ci } 20480922886Sopenharmony_ci} 20580922886Sopenharmony_ci 20680922886Sopenharmony_civoid AVSessionDumper::ShowControllerInfo(std::string& result, const AVSessionService& sessionService) 20780922886Sopenharmony_ci{ 20880922886Sopenharmony_ci AVPlaybackState playbackState; 20980922886Sopenharmony_ci std::string temp; 21080922886Sopenharmony_ci int32_t controllerCount = 0; 21180922886Sopenharmony_ci for (const auto& it : sessionService.controllers_) { 21280922886Sopenharmony_ci for (const auto& controller : it.second) { 21380922886Sopenharmony_ci controllerCount++; 21480922886Sopenharmony_ci controller->GetAVPlaybackState(playbackState); 21580922886Sopenharmony_ci 21680922886Sopenharmony_ci int32_t state = playbackState.GetState(); 21780922886Sopenharmony_ci double speed = playbackState.GetSpeed(); 21880922886Sopenharmony_ci AVPlaybackState::Position position = playbackState.GetPosition(); 21980922886Sopenharmony_ci int64_t bufferedTime = playbackState.GetBufferedTime(); 22080922886Sopenharmony_ci int32_t loopMode = playbackState.GetLoopMode(); 22180922886Sopenharmony_ci std::string isFavorite = playbackState.GetFavorite() ? "true" : "false"; 22280922886Sopenharmony_ci 22380922886Sopenharmony_ci temp.append("\n\ncurretn controller pid : " + std::to_string(controller->GetPid()) + "\n") 22480922886Sopenharmony_ci .append("State:\n") 22580922886Sopenharmony_ci .append("state : " + playBackStates_.find(state)->second + "\n") 22680922886Sopenharmony_ci .append("speed : " + std::to_string(speed) + "\n") 22780922886Sopenharmony_ci .append("position\n") 22880922886Sopenharmony_ci .append(" elapsed time : " + std::to_string(position.elapsedTime_) + "\n") 22980922886Sopenharmony_ci .append(" update time : " + std::to_string(position.updateTime_) + "\n") 23080922886Sopenharmony_ci .append("buffered time : " + std::to_string(bufferedTime) + "\n") 23180922886Sopenharmony_ci .append("loopmode : " + loopMode_.find(loopMode)->second + "\n") 23280922886Sopenharmony_ci .append("is favorite : " + isFavorite + "\n") 23380922886Sopenharmony_ci .append("\nRelated Sessionid : " + controller->GetSessionId()); 23480922886Sopenharmony_ci } 23580922886Sopenharmony_ci } 23680922886Sopenharmony_ci result.append("Controller Information:\n\n") 23780922886Sopenharmony_ci .append("Count : " + std::to_string(controllerCount)) 23880922886Sopenharmony_ci .append(temp); 23980922886Sopenharmony_ci} 24080922886Sopenharmony_ci 24180922886Sopenharmony_civoid AVSessionDumper::SetErrorInfo(const std::string& inErrMsg) 24280922886Sopenharmony_ci{ 24380922886Sopenharmony_ci time_t now = time(nullptr); 24480922886Sopenharmony_ci if (now == -1) { 24580922886Sopenharmony_ci SLOGE("get time failed"); 24680922886Sopenharmony_ci return; 24780922886Sopenharmony_ci } 24880922886Sopenharmony_ci struct tm *locTime = localtime(&now); 24980922886Sopenharmony_ci if (locTime == nullptr) { 25080922886Sopenharmony_ci SLOGE("get localtime failed"); 25180922886Sopenharmony_ci return; 25280922886Sopenharmony_ci } 25380922886Sopenharmony_ci char tempTime [TIME_MAX]; 25480922886Sopenharmony_ci auto ret = strftime(tempTime, sizeof(tempTime), "%Y-%m-%d %H:%M:%S", locTime); 25580922886Sopenharmony_ci if (ret == 0) { 25680922886Sopenharmony_ci SLOGE("strftime failed"); 25780922886Sopenharmony_ci return; 25880922886Sopenharmony_ci } 25980922886Sopenharmony_ci auto time = tempTime; 26080922886Sopenharmony_ci std::string msgInfo; 26180922886Sopenharmony_ci msgInfo.append(time + inErrMsg); 26280922886Sopenharmony_ci errMessage_.push_back(msgInfo); 26380922886Sopenharmony_ci} 26480922886Sopenharmony_ci 26580922886Sopenharmony_civoid AVSessionDumper::ShowErrorInfo(std::string& result, const AVSessionService& sessionService) 26680922886Sopenharmony_ci{ 26780922886Sopenharmony_ci if (errMessage_.empty()) { 26880922886Sopenharmony_ci result.append("No Error Information!\n"); 26980922886Sopenharmony_ci return; 27080922886Sopenharmony_ci } 27180922886Sopenharmony_ci 27280922886Sopenharmony_ci int32_t errMsgCount = 0; 27380922886Sopenharmony_ci result.append("Error Information:\n\n"); 27480922886Sopenharmony_ci for (auto iter = errMessage_.begin(); iter != errMessage_.end(); iter++) { 27580922886Sopenharmony_ci result.append(errMessage_.at(errMsgCount++) + "\n"); 27680922886Sopenharmony_ci } 27780922886Sopenharmony_ci} 27880922886Sopenharmony_ci 27980922886Sopenharmony_civoid AVSessionDumper::ProcessParameter(const std::string& arg, std::string& result, 28080922886Sopenharmony_ci const AVSessionService& sessionService) const 28180922886Sopenharmony_ci{ 28280922886Sopenharmony_ci if (arg == ARGS_HELP) { 28380922886Sopenharmony_ci ShowHelp(result); 28480922886Sopenharmony_ci } else { 28580922886Sopenharmony_ci auto it = funcMap_.find(arg); 28680922886Sopenharmony_ci if (it != funcMap_.end()) { 28780922886Sopenharmony_ci it->second(result, sessionService); 28880922886Sopenharmony_ci } else { 28980922886Sopenharmony_ci ShowIllegalInfo(result); 29080922886Sopenharmony_ci } 29180922886Sopenharmony_ci } 29280922886Sopenharmony_ci} 29380922886Sopenharmony_ci 29480922886Sopenharmony_civoid AVSessionDumper::ShowIllegalInfo(std::string& result) const 29580922886Sopenharmony_ci{ 29680922886Sopenharmony_ci result.append(ILLEGAL_INFORMATION); 29780922886Sopenharmony_ci} 29880922886Sopenharmony_ci 29980922886Sopenharmony_civoid AVSessionDumper::Dump(const std::vector<std::string>& args, std::string& result, 30080922886Sopenharmony_ci const AVSessionService& sessionService) const 30180922886Sopenharmony_ci{ 30280922886Sopenharmony_ci result.clear(); 30380922886Sopenharmony_ci auto argsSize = args.size(); 30480922886Sopenharmony_ci if (argsSize == 1) { 30580922886Sopenharmony_ci ProcessParameter(args[0], result, sessionService); 30680922886Sopenharmony_ci } else { 30780922886Sopenharmony_ci ShowIllegalInfo(result); 30880922886Sopenharmony_ci } 30980922886Sopenharmony_ci} 31080922886Sopenharmony_ci} // namespace OHOS::AVSession