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 "audio_errors.h" 24 #include "isoundpool.h" 25 #include "media_description.h" 26 #include "cpp/mutex.h" 27 #include "media_dfx.h" 28 #include "thread_pool.h" 29 #include "audio_system_manager.h" 30 #include "soundpool_xcollie.h" 31 32 namespace OHOS { 33 namespace Media { 34 using namespace MediaAVCodec; 35 36 struct AudioBufferEntry { AudioBufferEntryAudioBufferEntry37 AudioBufferEntry(uint8_t *buf, int32_t length) : buffer(std::move(buf)), size(length) {} ~AudioBufferEntryAudioBufferEntry38 ~AudioBufferEntry() 39 { 40 if (buffer != nullptr) { 41 delete[] buffer; 42 buffer = nullptr; 43 } 44 } 45 uint8_t *buffer; 46 int32_t size; 47 }; 48 49 class CacheBuffer : 50 public AudioStandard::AudioRendererWriteCallback, 51 public AudioStandard::AudioRendererFirstFrameWritingCallback, 52 public AudioStandard::AudioRendererCallback, 53 public std::enable_shared_from_this<CacheBuffer> { 54 public: 55 CacheBuffer(const Format &trackFormat, 56 const std::deque<std::shared_ptr<AudioBufferEntry>> &cacheData, 57 const size_t &cacheDataTotalSize, 58 const int32_t &soundID, const int32_t &streamID, 59 std::shared_ptr<ThreadPool> cacheBufferStopThreadPool); 60 ~CacheBuffer(); 61 void OnWriteData(size_t length) override; 62 void OnFirstFrameWriting(uint64_t latency) override; 63 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 64 void OnStateChange(const AudioStandard::RendererState state, 65 const AudioStandard::StateChangeCmdType cmdType) override; 66 int32_t PreparePlay(const int32_t streamID, const AudioStandard::AudioRendererInfo audioRendererInfo, 67 const PlayParams playParams); 68 int32_t DoPlay(const int32_t streamID); 69 int32_t Release(); 70 int32_t Stop(const int32_t streamID); 71 int32_t SetVolume(const int32_t streamID, const float leftVolume, const float rightVolume); 72 int32_t SetRate(const int32_t streamID, const AudioStandard::AudioRendererRate renderRate); 73 int32_t SetPriority(const int32_t streamID, const int32_t priority); 74 int32_t SetLoop(const int32_t streamID, const int32_t loop); 75 int32_t SetParallelPlayFlag(const int32_t streamID, const bool parallelPlayFlag); 76 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 77 int32_t SetCacheBufferCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 78 int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback); 79 IsRunning()80 bool IsRunning() const 81 { 82 return isRunning_.load(); 83 } GetSoundID()84 int32_t GetSoundID() const 85 { 86 return soundID_; 87 } GetStreamID()88 int32_t GetStreamID() const 89 { 90 return streamID_; 91 } GetPriority()92 int32_t GetPriority() const 93 { 94 return priority_; 95 } 96 97 private: 98 static constexpr int32_t NORMAL_PLAY_RENDERER_FLAGS = 0; 99 static constexpr int32_t LOW_LATENCY_PLAY_RENDERER_FLAGS = 1; 100 101 std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(const int32_t streamID, 102 const AudioStandard::AudioRendererInfo audioRendererInfo, const PlayParams playParams); 103 void PrepareAudioRenderer(std::unique_ptr<AudioStandard::AudioRenderer> &audioRenderer); 104 void DealAudioRendererParams(AudioStandard::AudioRendererOptions &rendererOptions, 105 const AudioStandard::AudioRendererInfo &audioRendererInfo); 106 int32_t ReCombineCacheData(); 107 int32_t DealPlayParamsBeforePlay(const int32_t streamID, const PlayParams playParams); 108 static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate); 109 void DealWriteData(size_t length); 110 bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo); 111 112 Format trackFormat_; 113 std::deque<std::shared_ptr<AudioBufferEntry>> cacheData_; 114 std::shared_ptr<AudioBufferEntry> fullCacheData_; 115 size_t cacheDataTotalSize_; 116 int32_t soundID_; 117 int32_t streamID_; 118 AudioStandard::AudioSampleFormat sampleFormat_ = AudioStandard::AudioSampleFormat::INVALID_WIDTH; 119 120 // use for save audiobuffer 121 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_; 122 std::atomic<bool> isRunning_ = false; 123 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 124 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 125 std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr; 126 ffrt::mutex cacheBufferLock_; 127 std::weak_ptr<ThreadPool> cacheBufferStopThreadPool_; 128 129 int32_t loop_ = 0; 130 int32_t priority_ = 0; 131 int32_t rendererFlags_ = NORMAL_PLAY_RENDERER_FLAGS; 132 133 size_t cacheDataFrameIndex_; 134 int32_t havePlayedCount_; 135 }; 136 } // namespace Media 137 } // namespace OHOS 138 #endif // CACHE_BUFFER_H 139