• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #ifndef AUDIO_BUFFER_CACHE_H
17 #define AUDIO_BUFFER_CACHE_H
18 
19 #include <list>
20 #include <map>
21 #include <string>
22 #include <mutex>
23 #include <condition_variable>
24 
25 #include "dump_buffer_wrap.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace MediaMonitor {
30 
31 using AudioBuffer = DumpBuffer;
32 using AudioBufferElement = struct AudioBufferElement {
33     int32_t size {0};
34     std::shared_ptr<AudioBuffer> buffer {nullptr};
35 };
36 
37 class AudioBufferCache {
38 public:
39     AudioBufferCache(std::shared_ptr<DumpBufferWrap> wrap);
40     ~AudioBufferCache();
41     int32_t RequestBuffer(std::shared_ptr<AudioBuffer> &buffer, int32_t size);
42     int32_t ReleaseBuffer(std::shared_ptr<AudioBuffer> &buffer);
43     int32_t Clear();
44     int32_t GetBufferById(std::shared_ptr<AudioBuffer> &buffer, uint64_t bufferId);
45     int32_t SetBufferSize(std::shared_ptr<AudioBuffer> &buffer, int32_t size);
46 private:
47     int32_t RequestCacheBuffer(std::shared_ptr<AudioBuffer> &buffer, int32_t size);
48     int32_t AllocAudioBuffer(std::shared_ptr<AudioBuffer> &buffer, int32_t size);
49     int32_t DeleteAudioBuffer(uint64_t bufferId, int32_t size);
50     int32_t AllocBuffer(std::shared_ptr<AudioBuffer> &buffer, int32_t size);
51     std::mutex mutex_;
52     std::list<uint64_t> freeBufferList_;
53     std::map<uint64_t, AudioBufferElement> bufferMap_;
54     int32_t bufferSize_ = 0;
55     std::shared_ptr<DumpBufferWrap> dumpBufferWrap_ = nullptr;
56 };
57 
58 } // namespace MediaMonitor
59 } // namespace Media
60 } // namespace OHOS
61 #endif // AUDIO_BUFFER_CACHE_H