1 /*
2  * Copyright (c) 2023-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 
16 #if defined(VIDEO_SUPPORT)
17 
18 #ifndef HISTREAMER_PLUGIN_CODEC_BUFFER_POOL_H
19 #define HISTREAMER_PLUGIN_CODEC_BUFFER_POOL_H
20 
21 #include "codec_buffer.h"
22 #include "codec_component_if.h"
23 #include "codec_component_type.h"
24 #include "foundation/osal/thread/mutex.h"
25 #include "foundation/utils/blocking_queue.h"
26 #include "plugin/common/plugin_types.h"
27 #include "plugin/common/share_allocator.h"
28 #include "plugin/common/share_memory.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Plugin {
33 namespace CodecAdapter {
34 class CodecBufferPool {
35 public:
36     CodecBufferPool(CodecComponentType* compType, CompVerInfo& verInfo, uint32_t portIndex, uint32_t bufferCnt);
37 
38     ~CodecBufferPool() = default;
39 
40     /**
41      * Init codec buffer and create buffer map
42      * Encoder:
43      * inBuf is surfaceBuf and allocated dynamic, outBuf is sharedBuf (READ_WRITE_TYPE)
44      * Decoder:
45      * inBuf is sharedBuf (READ_ONLY_TYPE),  outBuf is surfaceBuf and pre-allocation
46      */
47     Status UseBuffers(OHOS::Media::BlockingQueue<std::shared_ptr<Buffer>>& bufQue, MemoryType bufMemType,
48                       bool isInput, uint32_t bufferSize);
49 
50     Status FreeBuffers(); // 释放所有buffer
51 
52     uint32_t EmptyBufferCount();
53 
54     Status UseBufferDone(uint32_t bufId); // 根据该bufferId,重置omxBuffer对应的CodecBuffer
55 
56     std::shared_ptr<CodecBuffer> GetBuffer(int32_t bufferId = -1);
57 
58 private:
59     Status ConfigBufType(const MemoryType& bufMemType, bool isInput);
60 
61 private:
62     OSAL::Mutex mutex_;
63     CodecComponentType* codecComp_ {nullptr};
64     CompVerInfo verInfo_;
65 
66     uint32_t portIndex_;
67     OHOS::Media::BlockingQueue<uint32_t> freeBufferId_;
68     std::map<uint32_t, std::shared_ptr<CodecBuffer>> codecBufMap_;  // key is buffer id
69 };
70 } // namespace CodecAdapter
71 } // namespace Plugin
72 } // namespace Media
73 } // namespace OHOS
74 #endif // HISTREAMER_PLUGIN_CODEC_BUFFER_POOL_H
75 #endif