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<PerferredType, std::shared_ptr<MonitorDeviceInfo>> &perferredDevices) 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 ErasePreferredDeviceByType(const PerferredType preferredType) override;
87 private:
88     MediaMonitorService();
89     void MessageLoopFunc();
90     void GetMessageFromQueue(std::shared_ptr<EventBean> &message);
91     void AddMessageToQueue(std::shared_ptr<EventBean> &message);
92     void AudioEncodeDump();
93 
94     EventAggregate& eventAggregate_;
95     AudioMemo& audioMemo_;
96     std::unique_ptr<std::thread> messageLoopThread_ = nullptr;
97     std::shared_ptr<MessageSignal> signal_ = nullptr;
98     bool isExit_ = false;
99 
100     bool VerifyIsAudio();
101     bool IsNeedDump();
102     int32_t DumpThreadProcess();
103     void DumpThreadStart();
104     void DumpThreadStop();
105     void DumpThreadExit();
106     void DumpLoopFunc();
107     void DumpFileClear();
108     void DumpBufferClear();
109     void HistoryFilesHandle();
110     void AudioBufferRelease(std::shared_ptr<DumpBuffer> &buffer);
111     bool DeleteHistoryFile(const std::string &filePath);
112     void DumpBufferWrite(std::queue<std::pair<std::string, std::shared_ptr<DumpBuffer>>> &bufferQueue);
113     void AddBufferToQueue(const std::string &fileName, std::shared_ptr<DumpBuffer> &buffer);
114     void WriteBufferFromQueue(const std::string &fileName, std::shared_ptr<DumpBuffer> &buffer);
115     bool isDumpExit_ = false;
116     bool dumpEnable_ = false;
117     std::mutex paramMutex_;
118     std::mutex bufferMutex_;
119     std::string dumpType_ = DEFAULT_DUMP_TYPE;
120     std::string versionType_ = COMMERCIAL_VERSION;
121     std::string fileFloader_ = DEFAULT_DUMP_DIR;
122     std::unique_ptr<std::thread> dumpLoopThread_ = nullptr;
123     std::shared_ptr<DumpSignal> dumpSignal_ = nullptr;
124     std::shared_ptr<AudioBufferCache> audioBufferCache_ = nullptr;
125     std::time_t dumpThreadTime_ = 0;
126 };
127 
128 } // namespace MediaMonitor
129 } // namespace Media
130 } // namespace OHOS
131 #endif // MEDIA_MONITOR_H