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 "audio_system_manager.h" 28 29 namespace OHOS { 30 namespace Media { 31 using namespace MediaAVCodec; 32 33 struct AudioBufferEntry { AudioBufferEntryOHOS::Media::AudioBufferEntry34 AudioBufferEntry(uint8_t *buf, int32_t length) : buffer(std::move(buf)), size(length) {} ~AudioBufferEntryOHOS::Media::AudioBufferEntry35 ~AudioBufferEntry() 36 { 37 if (buffer != nullptr) { 38 delete[] buffer; 39 buffer = nullptr; 40 } 41 } 42 uint8_t *buffer; 43 int32_t size; 44 }; 45 46 class CacheBuffer : 47 public AudioStandard::AudioRendererWriteCallback, 48 public AudioStandard::AudioRendererFirstFrameWritingCallback, 49 public std::enable_shared_from_this<CacheBuffer> { 50 public: 51 CacheBuffer(const Format &trackFormat, 52 const std::deque<std::shared_ptr<AudioBufferEntry>> &cacheData, 53 const size_t &cacheDataTotalSize, 54 const int32_t &soundID, const int32_t &streamID); 55 ~CacheBuffer(); 56 void OnWriteData(size_t length) override; 57 void OnFirstFrameWriting(uint64_t latency) override; 58 int32_t PreparePlay(const int32_t streamID, const AudioStandard::AudioRendererInfo audioRendererInfo, 59 const PlayParams playParams); 60 int32_t DoPlay(const int32_t streamID); 61 int32_t Release(); 62 int32_t Stop(const int32_t streamID); 63 int32_t SetVolume(const int32_t streamID, const float leftVolume, const float rightVolume); 64 int32_t SetRate(const int32_t streamID, const AudioStandard::AudioRendererRate renderRate); 65 int32_t SetPriority(const int32_t streamID, const int32_t priority); 66 int32_t SetLoop(const int32_t streamID, const int32_t loop); 67 int32_t SetParallelPlayFlag(const int32_t streamID, const bool parallelPlayFlag); 68 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 69 int32_t SetCacheBufferCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 70 int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback); 71 IsRunning() const72 bool IsRunning() const 73 { 74 return isRunning_.load(); 75 } GetSoundID() const76 int32_t GetSoundID() const 77 { 78 return soundID_; 79 } GetStreamID() const80 int32_t GetStreamID() const 81 { 82 return streamID_; 83 } GetPriority() const84 int32_t GetPriority() const 85 { 86 return priority_; 87 } 88 89 private: 90 static constexpr int32_t NORMAL_PLAY_RENDERER_FLAGS = 0; 91 static constexpr int32_t LOW_LATENCY_PLAY_RENDERER_FLAGS = 1; 92 93 std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(const int32_t streamID, 94 const AudioStandard::AudioRendererInfo audioRendererInfo, const PlayParams playParams); 95 int32_t ReCombineCacheData(); 96 int32_t DealPlayParamsBeforePlay(const int32_t streamID, const PlayParams playParams); 97 static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate); 98 void DealWriteData(size_t length); 99 bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo); 100 101 Format trackFormat_; 102 std::deque<std::shared_ptr<AudioBufferEntry>> cacheData_; 103 std::shared_ptr<AudioBufferEntry> fullCacheData_; 104 size_t cacheDataTotalSize_; 105 int32_t soundID_; 106 int32_t streamID_; 107 108 // use for save audiobuffer 109 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_; 110 std::atomic<bool> isRunning_ = false; 111 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 112 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 113 std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr; 114 ffrt::mutex cacheBufferLock_; 115 116 int32_t loop_ = 0; 117 int32_t priority_ = 0; 118 int32_t rendererFlags_ = NORMAL_PLAY_RENDERER_FLAGS; 119 120 size_t cacheDataFrameIndex_; 121 int32_t havePlayedCount_; 122 }; 123 } // namespace Media 124 } // namespace OHOS 125 #endif // CACHE_BUFFER_H 126