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 #if defined(VIDEO_SUPPORT)
16 
17 #ifndef HISTREAMER_PLUGIN_HDI_CODEC_ADAPTER_H
18 #define HISTREAMER_PLUGIN_HDI_CODEC_ADAPTER_H
19 #include <limits>
20 #include <list>
21 #include "codec_buffer_pool.h"
22 #include "codec_cmd_executor.h"
23 #include "codec_component_manager.h"
24 #include "codec_manager.h"
25 #include "codec_port.h"
26 #include "foundation/utils/blocking_queue.h"
27 #include "plugin/interface/codec_plugin.h"
28 
29 namespace OHOS {
30 namespace Media {
31 namespace Plugin {
32 namespace CodecAdapter {
33 class HdiCodecAdapter : public CodecPlugin {
34 public:
35     HdiCodecAdapter(std::string componentName, std::string pluginMime);
36     ~HdiCodecAdapter() override;
37     Status Init() override;
38     Status Deinit() override;
39     Status Prepare() override;
40     Status Reset() override;
41     Status Start() override;
42     Status Stop() override;
43     Status Flush() override;
44     Status GetParameter(Plugin::Tag tag, Plugin::ValueType& value) override;
45     Status SetParameter(Plugin::Tag tag, const Plugin::ValueType& value) override;
46     std::shared_ptr<Plugin::Allocator> GetAllocator() override;
47     Status QueueInputBuffer(const std::shared_ptr<Buffer>& inputBuffer, int32_t timeoutMs) override;
48     Status QueueOutputBuffer(const std::shared_ptr<Buffer>& outputBuffer, int32_t timeoutMs) override;
49     Status SetCallback(Callback* cb) override;
50     Status SetDataCallback(DataCallback* dataCallback) override;
51 
52 private:
53     Status InitVersion();
54     Status InitPortIndex();
55     Status FreeBuffers();
56     void HandleFrame();
57     bool isFirstCall_ = true;
58     bool FillAllTheOutBuffer();
59 
60 private:
61     void NotifyInputBufferDone(const std::shared_ptr<Buffer>& input);
62     void NotifyOutputBufferDone(const std::shared_ptr<Buffer>& output);
63 
64     // HDI callback
65     static int32_t EventHandler(CodecCallbackType* self, OMX_EVENTTYPE event, EventInfo* info);
66     static int32_t EmptyBufferDone(CodecCallbackType* self, int64_t appData, const OmxCodecBuffer* omxBuffer);
67     static int32_t FillBufferDone(CodecCallbackType* self, int64_t appData, const OmxCodecBuffer* omxBuffer);
68 
69     Status ConfigOmx();
70 
71     Status ChangeState(OMX_STATETYPE state);
72     Status WaitForState(OMX_STATETYPE state);
73 
74     CodecComponentType* codecComp_ {nullptr};
75     CodecCallbackType* codecCallback_ {nullptr};
76     std::string componentName_ {};
77     uint32_t componentId_{};
78     std::string pluginMime_{};
79     std::list<std::shared_ptr<Buffer>> inBufQue_ {};
80     std::shared_ptr<OHOS::Media::BlockingQueue<std::shared_ptr<Buffer>>> outBufQue_{};
81 
82     uint32_t inBufferSize_{};
83     uint32_t inBufferCnt_{};
84     uint32_t outBufferSize_{};
85     uint32_t outBufferCnt_{};
86     std::shared_ptr<CodecBufferPool> inBufPool_ {};
87     std::shared_ptr<CodecBufferPool> outBufPool_ {};
88     MemoryType inputMemoryType_ {MemoryType::VIRTUAL_ADDR};
89     MemoryType outputMemoryType_ {MemoryType::VIRTUAL_ADDR};
90 
91     bool portConfigured_ {false};
92     uint32_t width_{};
93     uint32_t height_{};
94     uint32_t frameRate_{};
95     VideoPixelFormat pixelFormat_ {};
96     int64_t bitRate_{};
97 
98     Callback* callback_ {nullptr};
99     DataCallback* dataCallback_ {nullptr};
100 
101     OSAL::Mutex lockInputBuffers_;
102 
103     std::shared_ptr<ShareAllocator> shaAlloc_ {nullptr};
104     std::atomic<bool> isFlushing_ {false};
105     uint32_t inPortIndex_{};
106     uint32_t outPortIndex_{};
107     CompVerInfo verInfo_ {};
108     OMX_PORT_PARAM_TYPE portParam_ = {};
109     std::shared_ptr<CodecPort> inCodecPort_{};
110     std::shared_ptr<CodecPort> outCodecPort_{};
111 
112     OSAL::Mutex fillAllTheOutBufferMutex_;
113     OSAL::Mutex bufferMetaMutex_;
114     std::map<int64_t, std::shared_ptr<BufferMeta>> bufferMetaMap_;
115 
116     std::shared_ptr<CodecCmdExecutor> codecCmdExecutor_{};
117     OMX_STATETYPE curState_ {OMX_StateInvalid};
118     OMX_STATETYPE targetState_ {OMX_StateInvalid};
119 };
120 } // namespace CodecAdapter
121 } // namespace Plugin
122 } // namespace Media
123 } // namespace OHOS
124 #endif // HISTREAMER_PLUGIN_HDI_CODEC_ADAPTER_H
125 #endif