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