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