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 <pwd.h>
25 #include <map>
26 #include "securec.h"
27 #include "media_log.h"
28 #include "nocopyable.h"
29
30 namespace OHOS {
31 namespace AudioStandard {
32 constexpr int STRING_BUFFER_SIZE = 4096;
33 template <typename...Args>
34
AppendFormat(std::string & out,const char * fmt,Args &&...args)35 void AppendFormat(std::string& out, const char* fmt, Args&& ... args)
36 {
37 char buf[STRING_BUFFER_SIZE] = {0};
38 int len = ::sprintf_s(buf, sizeof(buf), fmt, args...);
39 if (len <= 0) {
40 MEDIA_ERR_LOG("snprintf_s error : buffer allocation fails");
41 return;
42 }
43 out += buf;
44 }
45
46 static const int32_t AUDIO_DUMP_SUCCESS = 0;
47 static const int32_t AUDIO_DUMP_INIT_ERR = -1;
48
49 typedef struct {
50 DeviceType deviceType;
51 DeviceRole deviceRole;
52 } DevicesInfo;
53
54 typedef struct {
55 std::string name;
56 pa_sample_spec sampleSpec;
57 } SinkSourceInfo;
58
59 typedef struct {
60 uint32_t userId;
61 uint32_t corked; // status
62 std::string sessionId;
63 std::string sessionStartTime;
64 std::string applicationName;
65 std::string processId;
66 pa_sample_spec sampleSpec;
67 }InputOutputInfo;
68
69 typedef struct {
70 std::vector<SinkSourceInfo> sinkDevices;
71 std::vector<SinkSourceInfo> sourceDevices;
72 std::vector<InputOutputInfo> sinkInputs;
73 std::vector<InputOutputInfo> sourceOutputs;
74 } StreamData;
75
76 typedef struct {
77 std::vector<DevicesInfo> inputDevices;
78 std::vector<DevicesInfo> outputDevices;
79 std::map<AudioStreamType, int32_t> streamVolumes;
80 AudioRingerMode ringerMode;
81 AudioScene callStatus;
82 AudioInterrupt audioFocusInfo;
83 } PolicyData;
84
85 typedef struct {
86 StreamData streamData;
87 PolicyData policyData;
88 } AudioData;
89
90 class AudioServiceDump : public AudioTimer {
91 public:
92 DISALLOW_COPY_AND_MOVE(AudioServiceDump);
93
94 AudioServiceDump();
95 ~AudioServiceDump();
96 int32_t Initialize();
97 void AudioDataDump(PolicyData &policyData, std::string &dumpString);
98 static bool IsStreamSupported(AudioStreamType streamType);
99 virtual void OnTimeOut();
100
101 private:
102 pa_threaded_mainloop *mainLoop;
103 pa_mainloop_api *api;
104 pa_context *context;
105 std::mutex ctrlMutex;
106
107 bool isMainLoopStarted;
108 bool isContextConnected;
109 AudioData audioData_;
110
111 int32_t ConnectStreamToPA();
112 void ResetPAAudioDump();
113
114 void PlaybackStreamDump(std::string &dumpString);
115 void RecordStreamDump(std::string &dumpString);
116 void HDFModulesDump(std::string &dumpString);
117 void CallStatusDump(std::string &dumpString);
118 void RingerModeDump(std::string &dumpString);
119 void StreamVolumesDump(std::string &dumpString);
120 void DevicesInfoDump(std::string &dumpString);
121 void AudioFocusInfoDump(std::string &dumpString);
122 void DataDump(std::string &dumpString);
123 static const std::string GetStreamName(AudioStreamType audioType);
124 static const std::string GetStreamUsgaeName(StreamUsage streamUsage);
125 static const std::string GetContentTypeName(ContentType contentType);
126 static const std::string GetDeviceTypeName(DeviceType deviceType);
127 static bool IsEndWith(const std::string &mainStr, const std::string &toMatch);
128 static bool IsValidModule (const std::string moduleName);
129
130 // Callbacks
131 static void PAContextStateCb(pa_context *context, void *userdata);
132 static void PASinkInfoCallback(pa_context *c, const pa_sink_info *i, int eol, void *userdata);
133 static void PASinkInputInfoCallback(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata);
134 static void PASourceInfoCallback(pa_context *c, const pa_source_info *i, int eol, void *userdata);
135 static void PASourceOutputInfoCallback(pa_context *c, const pa_source_output_info *i, int eol, void *userdata);
136 };
137 } // namespace AudioStandard
138 } // namespace OHOS
139 #endif // AUDIO_SERVICE_DUMP_H
140