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 #ifndef AUDIO_PCM_DUMP_PRIVATE_H 16 #define AUDIO_PCM_DUMP_PRIVATE_H 17 18 #include "audio_dump_pcm.h" 19 20 #include <mutex> 21 #include <deque> 22 #include <atomic> 23 #include <unordered_map> 24 25 #include "singleton.h" 26 #include "event_handler.h" 27 #include "event_runner.h" 28 #include "callback_handler.h" 29 #include "timestamp.h" 30 31 namespace OHOS { 32 namespace AudioStandard { 33 34 struct MemBlock { 35 uint8_t* dataPointer_ = nullptr; 36 size_t dataLength_ = 0; 37 uint32_t dumpFileNameId_ = 0; 38 }; 39 40 class MemChunk { 41 public: 42 MemChunk(); 43 ~MemChunk(); 44 int32_t GetMemBlock(size_t dataLength, std::string &dumpFileName, MemBlock &curMemBlock); 45 std::shared_ptr<std::deque<MemBlock>> GetMemBlockDeque(); 46 void GetMemChunkDuration(int64_t &startTime, int64_t &endTime); 47 int32_t GetCurUsedMemory(size_t &dataLength, size_t &bufferLength, size_t &structLength); 48 void Reset(); 49 public: 50 std::unordered_map<uint16_t, std::string> idFileNameMap_{}; 51 private: 52 std::unordered_map<std::string, uint16_t> fileNameIdMap_{}; 53 std::shared_ptr<std::deque<MemBlock>> memBlockDeque_; 54 55 const size_t totalBufferSize_; 56 size_t pointerOffset_; 57 uint16_t curFileNameId_; 58 int64_t firstMemBlockTime_; 59 int64_t lastMemBlockTime_; 60 uint8_t *bufferPointer_ = nullptr; 61 }; 62 63 class AudioCacheHandler : public IHandler { 64 public: 65 AudioCacheHandler(IHandler* handler); 66 void OnHandle(uint32_t code, int64_t data) override; 67 private: 68 IHandler *handler_ = nullptr; 69 }; 70 71 class AudioCacheMgrInner : public AudioCacheMgr, public IHandler { 72 public: 73 AudioCacheMgrInner(); 74 ~AudioCacheMgrInner() override; 75 void OnHandle(uint32_t code, int64_t data) override; 76 77 bool Init() override; 78 bool DeInit() override; 79 80 void CacheData(std::string &dumpFileName, void* dataPointer, size_t dataLength) override; 81 int32_t DumpAllMemBlock() override; 82 void GetCachedDuration(int64_t &startTime, int64_t &endTime) override; 83 void GetCurMemoryCondition(size_t &dataLength, size_t &bufferLength, size_t &structLength) override; 84 bool SetDumpParameter(const std::vector<std::pair<std::string, std::string>> ¶ms) override; 85 bool GetDumpParameter(const std::vector<std::string> &subKeys, 86 std::vector<std::pair<std::string, std::string>> &result) override; 87 88 private: 89 void InitCallbackHandler(); 90 int32_t GetAvailableMemBlock(size_t dataLength, std::string &dumpFileName, MemBlock &curMemBlock); 91 void SafeSendCallBackEvent(uint32_t eventCode, int64_t data, int64_t delayTime); 92 void ReleaseOverTimeMemBlock(); 93 void PrintCurMemoryCondition(); 94 private: 95 std::atomic<bool> isDumpingData_ = {false}; 96 std::atomic<bool> isInited_ = {false}; 97 size_t totalMemChunkNums_; // MAX_LIMIT_BUFFER_SIZE / EACH_CHUNK_SIZE 98 std::deque<std::shared_ptr<MemChunk>> memChunkDeque_; 99 std::mutex g_Mutex; 100 101 // Event handler 102 std::shared_ptr<AudioCacheHandler> handler_ = nullptr; 103 std::mutex runnerMutex_; 104 std::shared_ptr<CallbackHandler> callbackHandler_ = nullptr; 105 106 enum { 107 RELEASE_OVERTIME_MEMBLOCK = 0, 108 PRINT_MEMORY_CONDITION, 109 }; 110 }; 111 112 } // namespace AudioStandard 113 } // namespace OHOS 114 #endif // AUDIO_PCM_DUMP_PRIVATE_H