1 2 /* 3 * Copyright (C) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef CACHE_BUFFER_H 17 #define CACHE_BUFFER_H 18 19 #include <deque> 20 #include "audio_renderer.h" 21 #include "audio_info.h" 22 #include "audio_stream_info.h" 23 #include "isoundpool.h" 24 #include "media_description.h" 25 #include "cpp/mutex.h" 26 #include "media_dfx.h" 27 #include "thread_pool.h" 28 #include "audio_system_manager.h" 29 30 namespace OHOS { 31 namespace Media { 32 using namespace MediaAVCodec; 33 34 struct AudioBufferEntry { AudioBufferEntryAudioBufferEntry35 AudioBufferEntry(uint8_t *buf, int32_t length) : buffer(std::move(buf)), size(length) {} ~AudioBufferEntryAudioBufferEntry36 ~AudioBufferEntry() 37 { 38 if (buffer != nullptr) { 39 delete[] buffer; 40 buffer = nullptr; 41 } 42 } 43 uint8_t *buffer; 44 int32_t size; 45 }; 46 47 class CacheBuffer : 48 public AudioStandard::AudioRendererWriteCallback, 49 public AudioStandard::AudioRendererFirstFrameWritingCallback, 50 public std::enable_shared_from_this<CacheBuffer> { 51 public: 52 CacheBuffer(const Format &trackFormat, 53 const std::deque<std::shared_ptr<AudioBufferEntry>> &cacheData, 54 const size_t &cacheDataTotalSize, 55 const int32_t &soundID, const int32_t &streamID, 56 std::shared_ptr<ThreadPool> cacheBufferStopThreadPool); 57 ~CacheBuffer(); 58 void OnWriteData(size_t length) override; 59 void OnFirstFrameWriting(uint64_t latency) override; 60 int32_t PreparePlay(const int32_t streamID, const AudioStandard::AudioRendererInfo audioRendererInfo, 61 const PlayParams playParams); 62 int32_t DoPlay(const int32_t streamID); 63 int32_t Release(); 64 int32_t Stop(const int32_t streamID); 65 int32_t SetVolume(const int32_t streamID, const float leftVolume, const float rightVolume); 66 int32_t SetRate(const int32_t streamID, const AudioStandard::AudioRendererRate renderRate); 67 int32_t SetPriority(const int32_t streamID, const int32_t priority); 68 int32_t SetLoop(const int32_t streamID, const int32_t loop); 69 int32_t SetParallelPlayFlag(const int32_t streamID, const bool parallelPlayFlag); 70 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 71 int32_t SetCacheBufferCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 72 int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback); 73 IsRunning()74 bool IsRunning() const 75 { 76 return isRunning_.load(); 77 } GetSoundID()78 int32_t GetSoundID() const 79 { 80 return soundID_; 81 } GetStreamID()82 int32_t GetStreamID() const 83 { 84 return streamID_; 85 } GetPriority()86 int32_t GetPriority() const 87 { 88 return priority_; 89 } 90 91 private: 92 static constexpr int32_t NORMAL_PLAY_RENDERER_FLAGS = 0; 93 static constexpr int32_t LOW_LATENCY_PLAY_RENDERER_FLAGS = 1; 94 95 std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(const int32_t streamID, 96 const AudioStandard::AudioRendererInfo audioRendererInfo, const PlayParams playParams); 97 int32_t ReCombineCacheData(); 98 int32_t DealPlayParamsBeforePlay(const int32_t streamID, const PlayParams playParams); 99 static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate); 100 void DealWriteData(size_t length); 101 bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo); 102 103 Format trackFormat_; 104 std::deque<std::shared_ptr<AudioBufferEntry>> cacheData_; 105 std::shared_ptr<AudioBufferEntry> fullCacheData_; 106 size_t cacheDataTotalSize_; 107 int32_t soundID_; 108 int32_t streamID_; 109 110 // use for save audiobuffer 111 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_; 112 std::atomic<bool> isRunning_ = false; 113 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 114 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 115 std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr; 116 ffrt::mutex cacheBufferLock_; 117 std::weak_ptr<ThreadPool> cacheBufferStopThreadPool_; 118 119 int32_t loop_ = 0; 120 int32_t priority_ = 0; 121 int32_t rendererFlags_ = NORMAL_PLAY_RENDERER_FLAGS; 122 123 size_t cacheDataFrameIndex_; 124 int32_t havePlayedCount_; 125 }; 126 } // namespace Media 127 } // namespace OHOS 128 #endif // CACHE_BUFFER_H 129