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