1 /* 2 * Copyright (c) 2021-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 #ifndef AUDIO_SERVICE_DUMP_H 17 #define AUDIO_SERVICE_DUMP_H 18 19 #include <pulse/pulseaudio.h> 20 #include <audio_info.h> 21 #include <audio_timer.h> 22 #include <audio_errors.h> 23 #include <vector> 24 #include <list> 25 #include <pwd.h> 26 #include <map> 27 #include "securec.h" 28 #include "audio_log.h" 29 #include "nocopyable.h" 30 #include "audio_effect_manager.h" 31 #include "audio_volume_config.h" 32 33 namespace OHOS { 34 namespace AudioStandard { 35 36 static const int32_t AUDIO_DUMP_SUCCESS = 0; 37 static const int32_t AUDIO_DUMP_INIT_ERR = -1; 38 39 typedef struct { 40 DeviceType deviceType; 41 DeviceRole deviceRole; 42 ConnectType conneceType; 43 } DevicesInfo; 44 45 typedef struct { 46 std::string name; 47 pa_sample_spec sampleSpec; 48 } SinkSourceInfo; 49 50 typedef struct { 51 uint32_t userId; 52 uint32_t corked; // status 53 std::string sessionId; 54 std::string sessionStartTime; 55 std::string applicationName; 56 std::string processId; 57 pa_sample_spec sampleSpec; 58 }InputOutputInfo; 59 60 typedef struct { 61 std::vector<SinkSourceInfo> sinkDevices; 62 std::vector<SinkSourceInfo> sourceDevices; 63 std::vector<InputOutputInfo> sinkInputs; 64 std::vector<InputOutputInfo> sourceOutputs; 65 } StreamData; 66 67 typedef struct { 68 ConnectType type; 69 std::string groupName; 70 std::int32_t groupId; 71 } GroupInfo; 72 73 typedef struct { 74 std::vector<DevicesInfo> inputDevices; 75 std::vector<DevicesInfo> outputDevices; 76 DeviceType priorityOutputDevice; 77 DeviceType priorityInputDevice; 78 std::map<AudioStreamType, int32_t> streamVolumes; 79 AudioRingerMode ringerMode; 80 AudioScene callStatus; 81 AudioInterrupt audioFocusInfo; 82 std::list<std::pair<AudioInterrupt, AudioFocuState>> audioFocusInfoList; 83 std::vector<GroupInfo> groupInfos; 84 OriginalEffectConfig oriEffectConfig; 85 std::vector<Effect> availableEffects; 86 StreamVolumeInfoMap streamVolumeInfos; 87 } PolicyData; 88 89 typedef struct { 90 StreamData streamData; 91 PolicyData policyData; 92 } AudioData; 93 94 class AudioServiceDump : public AudioTimer { 95 public: 96 DISALLOW_COPY_AND_MOVE(AudioServiceDump); 97 98 AudioServiceDump(); 99 ~AudioServiceDump(); 100 int32_t Initialize(); 101 void AudioDataDump(PolicyData &policyData, std::string &dumpString); 102 static bool IsStreamSupported(AudioStreamType streamType); 103 virtual void OnTimeOut(); 104 105 private: 106 pa_threaded_mainloop *mainLoop; 107 pa_mainloop_api *api; 108 pa_context *context; 109 std::mutex ctrlMutex_; 110 111 bool isMainLoopStarted_; 112 bool isContextConnected_; 113 AudioData audioData_ = {}; 114 115 int32_t ConnectStreamToPA(); 116 void ResetPAAudioDump(); 117 118 void PlaybackStreamDump(std::string &dumpString); 119 void RecordStreamDump(std::string &dumpString); 120 void HDFModulesDump(std::string &dumpString); 121 void CallStatusDump(std::string &dumpString); 122 void RingerModeDump(std::string &dumpString); 123 void StreamVolumesDump(std::string &dumpString); 124 void DevicesInfoDump(std::string &dumpString); 125 void AudioFocusInfoDump(std::string &dumpString); 126 void GroupInfoDump(std::string& dumpString); 127 void EffectManagerInfoDump(std::string& dumpString); 128 void DataDump(std::string &dumpString); 129 void StreamVolumeInfosDump(std::string& dumpString); 130 void DeviceVolumeInfosDump(std::string& dumpString, DeviceVolumeInfoMap &deviceVolumeInfos); 131 static const std::string GetStreamName(AudioStreamType streamType); 132 static const std::string GetSourceName(SourceType sourceType); 133 static const std::string GetStreamUsgaeName(StreamUsage streamUsage); 134 static const std::string GetContentTypeName(ContentType contentType); 135 static const std::string GetDeviceTypeName(DeviceType deviceType); 136 static const std::string GetConnectTypeName(ConnectType connectType); 137 static const std::string GetDeviceVolumeTypeName(DeviceVolumeType deviceType); 138 static bool IsEndWith(const std::string &mainStr, const std::string &toMatch); 139 static bool IsValidModule (const std::string moduleName); 140 141 // Callbacks 142 static void PAContextStateCb(pa_context *context, void *userdata); 143 static void PASinkInfoCallback(pa_context *c, const pa_sink_info *i, int eol, void *userdata); 144 static void PASinkInputInfoCallback(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 145 static void PASourceInfoCallback(pa_context *c, const pa_source_info *i, int eol, void *userdata); 146 static void PASourceOutputInfoCallback(pa_context *c, const pa_source_output_info *i, int eol, void *userdata); 147 }; 148 } // namespace AudioStandard 149 } // namespace OHOS 150 #endif // AUDIO_SERVICE_DUMP_H 151