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 AudioStandard::AudioRendererCallback, 51 public std::enable_shared_from_this<CacheBuffer> { 52 public: 53 CacheBuffer(const Format &trackFormat, 54 const std::deque<std::shared_ptr<AudioBufferEntry>> &cacheData, 55 const size_t &cacheDataTotalSize, 56 const int32_t &soundID, const int32_t &streamID, 57 std::shared_ptr<ThreadPool> cacheBufferStopThreadPool); 58 ~CacheBuffer(); 59 void OnWriteData(size_t length) override; 60 void OnFirstFrameWriting(uint64_t latency) override; 61 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 62 void OnStateChange(const AudioStandard::RendererState state, 63 const AudioStandard::StateChangeCmdType cmdType) override; 64 int32_t PreparePlay(const int32_t streamID, const AudioStandard::AudioRendererInfo audioRendererInfo, 65 const PlayParams playParams); 66 int32_t DoPlay(const int32_t streamID); 67 int32_t Release(); 68 int32_t Stop(const int32_t streamID); 69 int32_t SetVolume(const int32_t streamID, const float leftVolume, const float rightVolume); 70 int32_t SetRate(const int32_t streamID, const AudioStandard::AudioRendererRate renderRate); 71 int32_t SetPriority(const int32_t streamID, const int32_t priority); 72 int32_t SetLoop(const int32_t streamID, const int32_t loop); 73 int32_t SetParallelPlayFlag(const int32_t streamID, const bool parallelPlayFlag); 74 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 75 int32_t SetCacheBufferCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 76 int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback); 77 IsRunning()78 bool IsRunning() const 79 { 80 return isRunning_.load(); 81 } GetSoundID()82 int32_t GetSoundID() const 83 { 84 return soundID_; 85 } GetStreamID()86 int32_t GetStreamID() const 87 { 88 return streamID_; 89 } GetPriority()90 int32_t GetPriority() const 91 { 92 return priority_; 93 } 94 95 private: 96 static constexpr int32_t NORMAL_PLAY_RENDERER_FLAGS = 0; 97 static constexpr int32_t LOW_LATENCY_PLAY_RENDERER_FLAGS = 1; 98 99 std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(const int32_t streamID, 100 const AudioStandard::AudioRendererInfo audioRendererInfo, const PlayParams playParams); 101 void PrepareAudioRenderer(std::unique_ptr<AudioStandard::AudioRenderer> &audioRenderer); 102 int32_t ReCombineCacheData(); 103 int32_t DealPlayParamsBeforePlay(const int32_t streamID, const PlayParams playParams); 104 static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate); 105 void DealWriteData(size_t length); 106 bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo); 107 108 Format trackFormat_; 109 std::deque<std::shared_ptr<AudioBufferEntry>> cacheData_; 110 std::shared_ptr<AudioBufferEntry> fullCacheData_; 111 size_t cacheDataTotalSize_; 112 int32_t soundID_; 113 int32_t streamID_; 114 115 // use for save audiobuffer 116 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_; 117 std::atomic<bool> isRunning_ = false; 118 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 119 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 120 std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr; 121 ffrt::mutex cacheBufferLock_; 122 std::weak_ptr<ThreadPool> cacheBufferStopThreadPool_; 123 124 int32_t loop_ = 0; 125 int32_t priority_ = 0; 126 int32_t rendererFlags_ = NORMAL_PLAY_RENDERER_FLAGS; 127 128 size_t cacheDataFrameIndex_; 129 int32_t havePlayedCount_; 130 }; 131 } // namespace Media 132 } // namespace OHOS 133 #endif // CACHE_BUFFER_H 134