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