• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef CACHE_BUFFER_H
16 #define CACHE_BUFFER_H
17 
18 #include <deque>
19 #include "audio_renderer.h"
20 #include "audio_info.h"
21 #include "audio_stream_info.h"
22 #include "audio_errors.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 #include "soundpool_xcollie.h"
30 #include "stream_id_manager.h"
31 
32 namespace OHOS {
33 namespace Media {
34 using namespace MediaAVCodec;
35 class StreamIDManager;
36 
37 struct AudioBufferEntry {
AudioBufferEntryAudioBufferEntry38     AudioBufferEntry(uint8_t *buf, int32_t length) : buffer(std::move(buf)), size(length) {}
~AudioBufferEntryAudioBufferEntry39     ~AudioBufferEntry()
40     {
41         if (buffer != nullptr) {
42             delete[] buffer;
43             buffer = nullptr;
44         }
45     }
46     uint8_t *buffer;
47     int32_t size;
48 };
49 
50 class CacheBuffer :
51     public AudioStandard::AudioRendererWriteCallback,
52     public AudioStandard::AudioRendererFirstFrameWritingCallback,
53     public AudioStandard::AudioRendererCallback,
54     public std::enable_shared_from_this<CacheBuffer> {
55 public:
56     CacheBuffer(const Format &trackFormat, const int32_t &soundID, const int32_t &streamID,
57         std::shared_ptr<ThreadPool> cacheBufferStopThreadPool);
58     ~CacheBuffer();
59     void SetSoundData(const std::shared_ptr<AudioBufferEntry> &cacheData, const size_t &cacheDataTotalSize);
60     void SetManager(std::weak_ptr<OHOS::Media::StreamIDManager> streamIDManager);
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 AudioStandard::AudioRendererInfo &audioRendererInfo, const PlayParams &playParams);
67     void GetAvailableAudioRenderer(const AudioStandard::AudioRendererInfo &audioRendererInfo,
68         const PlayParams &playParams);
69     int32_t DoPlay(const int32_t streamID);
70     int32_t Release();
71     int32_t Stop(const int32_t streamID);
72     int32_t SetVolume(const int32_t streamID, const float leftVolume, const float rightVolume);
73     int32_t SetRate(const int32_t streamID, const AudioStandard::AudioRendererRate renderRate);
74     int32_t SetPriority(const int32_t streamID, const int32_t priority);
75     int32_t SetLoop(const int32_t streamID, const int32_t loop);
76     int32_t SetParallelPlayFlag(const int32_t streamID, const bool parallelPlayFlag);
77     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
78     int32_t SetCacheBufferCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
79     int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback);
80     void SetSourceDuration(int64_t durationMs);
81 
IsRunning()82     bool IsRunning() const
83     {
84         return isRunning_.load();
85     }
GetSoundID()86     int32_t GetSoundID() const
87     {
88         return soundID_;
89     }
GetStreamID()90     int32_t GetStreamID() const
91     {
92         return streamID_;
93     }
GetPriority()94     int32_t GetPriority() const
95     {
96         return priority_;
97     }
98 
99 private:
100     static constexpr int32_t NORMAL_PLAY_RENDERER_FLAGS = 0;
101     static constexpr int32_t LOW_LATENCY_PLAY_RENDERER_FLAGS = 1;
102 
103     std::unique_ptr<AudioStandard::AudioRenderer> CreateAudioRenderer(
104         const AudioStandard::AudioRendererInfo &audioRendererInfo, const PlayParams &playParams);
105     void PrepareAudioRenderer(std::unique_ptr<AudioStandard::AudioRenderer> &audioRenderer);
106     void DealAudioRendererParams(AudioStandard::AudioRendererOptions &rendererOptions,
107         const AudioStandard::AudioRendererInfo &audioRendererInfo);
108     void DealPlayParamsBeforePlay(const PlayParams &playParams);
109     static AudioStandard::AudioRendererRate CheckAndAlignRendererRate(const int32_t rate);
110     void DealWriteData(size_t length);
111     bool IsAudioRendererCanMix(const AudioStandard::AudioRendererInfo &audioRendererInfo);
112     int32_t PreparePlayInner(const AudioStandard::AudioRendererInfo &audioRendererInfo,
113         const PlayParams &playParams);
114     int32_t GetGlobalId(int32_t soundID);
115     void DelGlobalId(int32_t globalId);
116     void SetGlobalId(int32_t soundID, int32_t globalId);
117     int32_t HandleRendererNotStart(const int32_t streamID);
118     void FadeInAudioBuffer(const AudioStandard::BufferDesc &bufDesc);
119 
120     Format trackFormat_;
121     std::deque<std::shared_ptr<AudioBufferEntry>> cacheData_;
122     std::shared_ptr<AudioBufferEntry> fullCacheData_;
123     size_t cacheDataTotalSize_ = 0;
124     int32_t soundID_ = 0;
125     int32_t streamID_ = 0;
126     AudioStandard::AudioSampleFormat sampleFormat_ = AudioStandard::AudioSampleFormat::INVALID_WIDTH;
127     AudioStandard::AudioChannel audioChannel_ = AudioStandard::AudioChannel::MONO;
128     PlayParams playParameters_;
129     AudioStandard::AudioRendererInfo audioRendererInfo_;
130 
131     // use for save audiobuffer
132     std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_;
133     std::atomic<bool> isRunning_ = false;
134     std::atomic<bool> isReadyToStopAudioRenderer_ = false;
135     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
136     std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr;
137     std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr;
138     ffrt::mutex cacheBufferLock_;
139     std::weak_ptr<ThreadPool> cacheBufferStopThreadPool_;
140     std::weak_ptr<OHOS::Media::StreamIDManager> manager_;
141 
142     int32_t loop_ = 0;
143     int32_t priority_ = 0;
144     int32_t rendererFlags_ = NORMAL_PLAY_RENDERER_FLAGS;
145     bool isNeedFadeIn_ = false;
146 
147     size_t cacheDataFrameIndex_ = 0;
148     int32_t havePlayedCount_ = 0;
149     int64_t sourceDurationMs_{};
150 };
151 } // namespace Media
152 } // namespace OHOS
153 #endif // CACHE_BUFFER_H
154