• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 MEDIA_MONITOR_H
17 #define MEDIA_MONITOR_H
18 
19 #include <thread>
20 #include <mutex>
21 #include <queue>
22 #include <condition_variable>
23 #include "ipc_skeleton.h"
24 #include "iremote_stub.h"
25 #include "system_ability.h"
26 #include "media_monitor_stub.h"
27 #include "event_aggregate.h"
28 #include "audio_buffer_cache.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace MediaMonitor {
33 
34 class EventAggregate;
35 
36 struct MessageSignal {
37     std::mutex messageMutex_;
38     std::condition_variable messageCond_;
39     std::queue<std::shared_ptr<EventBean>> messageQueue_;
40     std::atomic<bool> isRunning_ = false;
41 };
42 
43 struct DumpSignal {
44     std::mutex dumpMutex_;
45     std::condition_variable dumpCond_;
46     std::queue<std::pair<std::string, std::shared_ptr<DumpBuffer>>> dumpQueue_;
47     std::atomic<bool> isRunning_ = false;
48 };
49 
50 const mode_t FILE_MODE = 0775;
51 const int FILE_MAX_SIZE = 20480000 * 8;
52 const int MAX_FILE_COUNT = 20;
53 const int MAX_DUMP_TIME = 90;
54 
55 const std::string DEFAULT_DUMP_DIR = "/data/audio_debug/";
56 const std::string BETA_DUMP_DIR = "/data/log/audiodump/";
57 
58 class MediaMonitorService : public SystemAbility, public MediaMonitorStub {
59     DECLARE_SYSTEM_ABILITY(MediaMonitorService);
60 
61 public:
62     DISALLOW_COPY_AND_MOVE(MediaMonitorService);
63     explicit MediaMonitorService(int32_t systemAbilityId, bool runOnCreate = true);
64     virtual ~MediaMonitorService() = default;
65     void OnDump() override;
66     void OnStart() override;
67     void OnStop() override;
68 
69     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
70     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
71 
72     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
73 
74     void WriteLogMsg(std::shared_ptr<EventBean> &bean)  override;
75 
76     int32_t GetAudioRouteMsg(std::map<PreferredType, std::shared_ptr<MonitorDeviceInfo>> &preferredDevices) override;
77 
78     int32_t SetMediaParameters(const std::string &dumpType, const std::string &dumpEnable) override;
79 
80     int32_t WriteAudioBuffer(const std::string &fileName, void *ptr, size_t size) override;
81 
82     int32_t GetInputBuffer(std::shared_ptr<DumpBuffer> &buffer, int32_t size) override;
83 
84     int32_t InputBufferFilled(const std::string &fileName, uint64_t bufferId, int32_t size) override;
85 
86     int32_t GetPcmDumpStatus(int32_t &dumpEnable) override;
87 
88     int32_t ErasePreferredDeviceByType(const PreferredType preferredType) override;
89 
90     int32_t GetAudioExcludedDevicesMsg(std::map<AudioDeviceUsage,
91         std::vector<std::shared_ptr<MonitorDeviceInfo>>> &excludedDevices) override;
92 private:
93     MediaMonitorService();
94     void MessageLoopFunc();
95     void GetMessageFromQueue(std::shared_ptr<EventBean> &message);
96     void AddMessageToQueue(std::shared_ptr<EventBean> &message);
97     void AudioEncodeDump();
98 
99     EventAggregate& eventAggregate_;
100     AudioMemo& audioMemo_;
101     std::unique_ptr<std::thread> messageLoopThread_ = nullptr;
102     std::shared_ptr<MessageSignal> signal_ = nullptr;
103     bool isExit_ = false;
104 
105     bool VerifyIsAudio();
106     bool IsNeedDump();
107     int32_t DumpThreadProcess();
108     void DumpThreadStart();
109     void DumpThreadStop();
110     void DumpThreadExit();
111     void DumpLoopFunc();
112     void DumpFileClear();
113     void DumpBufferClear();
114     void HistoryFilesHandle();
115     void AudioBufferRelease(std::shared_ptr<DumpBuffer> &buffer);
116     bool DeleteHistoryFile(const std::string &filePath);
117     void DumpBufferWrite(std::queue<std::pair<std::string, std::shared_ptr<DumpBuffer>>> &bufferQueue);
118     void AddBufferToQueue(const std::string &fileName, std::shared_ptr<DumpBuffer> &buffer);
119     void WriteBufferFromQueue(const std::string &fileName, std::shared_ptr<DumpBuffer> &buffer);
120     bool isDumpExit_ = false;
121     bool dumpEnable_ = false;
122     std::mutex paramMutex_;
123     std::mutex bufferMutex_;
124     std::string dumpType_ = DEFAULT_DUMP_TYPE;
125     std::string versionType_ = COMMERCIAL_VERSION;
126     std::string fileFloader_ = DEFAULT_DUMP_DIR;
127     std::unique_ptr<std::thread> dumpLoopThread_ = nullptr;
128     std::shared_ptr<DumpSignal> dumpSignal_ = nullptr;
129     std::shared_ptr<AudioBufferCache> audioBufferCache_ = nullptr;
130     std::time_t dumpThreadTime_ = 0;
131 };
132 
133 } // namespace MediaMonitor
134 } // namespace Media
135 } // namespace OHOS
136 #endif // MEDIA_MONITOR_H