• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 HDI_CODEC_H
17 #define HDI_CODEC_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <condition_variable>
22 #include <gst/gst.h>
23 #include <OMX_Core.h>
24 #include <OMX_Component.h>
25 #include <shared_mutex>
26 #include "nocopyable.h"
27 #include "i_gst_codec.h"
28 #include "hdi_buffer_mgr.h"
29 #include "hdi_params_mgr.h"
30 #include "task_queue.h"
31 
32 namespace OHOS {
33 namespace Media {
34 class HdiCodec : public IGstCodec, public NoCopyable, public std::enable_shared_from_this<HdiCodec> {
35 public:
36     explicit HdiCodec(const std::string& component);
37     virtual ~HdiCodec();
38     int32_t Init() override;
39     void SetHdiInBufferMgr(std::shared_ptr<HdiBufferMgr> bufferMgr);
40     void SetHdiOutBufferMgr(std::shared_ptr<HdiBufferMgr> bufferMgr);
41     void SetHdiParamsMgr(std::shared_ptr<HdiParamsMgr> paramsMgr);
42     int32_t SetParameter(GstCodecParamKey key, GstElement *element) override;
43     int32_t GetParameter(GstCodecParamKey key, GstElement *element) override;
44     int32_t Start() override;
45     int32_t Stop() override;
46     int32_t UseInputBuffers(std::vector<GstBuffer*> buffers) override;
47     int32_t PushInputBuffer(GstBuffer *buffer) override;
48     int32_t FreeInputBuffers() override;
49     int32_t UseOutputBuffers(std::vector<GstBuffer*> buffers) override;
50     int32_t PushOutputBuffer(GstBuffer *buffer) override;
51     int32_t PullOutputBuffer(GstBuffer **buffer) override;
52     int32_t FreeOutputBuffers() override;
53     int32_t Flush(GstCodecDirect direct) override;
54     int32_t ActiveBufferMgr(GstCodecDirect direct, bool active) override;
55     void Deinit() override;
56     void OnCodecDied() override;
57     void SetOutputPool(GstBufferPool *pool) override;
58     bool IsFormatChanged() override;
59 
60 private:
61     struct AppData {
62         std::weak_ptr<HdiCodec> instance;
63     };
64     enum PortState : int32_t {
65         ACTIVATED,
66         ACTIVING,
67         DEACTIVATED,
68         DEACTIVING,
69     };
70     AppData *appData_ = nullptr;
71     CodecComponentType *handle_ = nullptr;
72     CodecCallbackType *callback_ = nullptr;
73     static int32_t Event(CodecCallbackType *self, OMX_EVENTTYPE event, EventInfo *info);
74     static int32_t EmptyBufferDone(CodecCallbackType *self, int64_t appData, const OmxCodecBuffer *buffer);
75     static int32_t FillBufferDone(CodecCallbackType *self, int64_t appData, const OmxCodecBuffer *buffer);
76     int32_t OnEvent(OMX_EVENTTYPE event, EventInfo *info);
77     int32_t OnEmptyBufferDone(const OmxCodecBuffer *buffer);
78     int32_t OnFillBufferDone(const OmxCodecBuffer *buffer);
79     void HandelEventFlush(OMX_U32 data);
80     void HandelEventCmdComplete(OMX_U32 data1, OMX_U32 data2);
81     void HandelEventStateSet(OMX_U32 data);
82     void HandleEventPortSettingsChanged(OMX_U32 data1, OMX_U32 data2);
83     void HandleEventBufferFlag(OMX_U32 data1, OMX_U32 data2);
84     void HandleEventError(OMX_U32 data);
85     void WaitForEvent(OMX_U32 cmd);
86     int32_t WaitForState(OMX_STATETYPE state);
87     void HandelEventPortDisable(OMX_U32 data);
88     void HandelEventPortEnable(OMX_U32 data);
89     int32_t ChangeState(OMX_STATETYPE state);
90     void InitVersion();
91     void DeinitInner();
92     std::shared_ptr<HdiBufferMgr> inBufferMgr_;
93     std::shared_ptr<HdiBufferMgr> outBufferMgr_;
94     std::shared_ptr<HdiParamsMgr> paramsMgr_;
95     std::mutex mutex_;
96     std::condition_variable cond_;
97     std::string componentName_ = "";
98     OMX_PORT_PARAM_TYPE portParam_ = {};
99     GstCodecRet ret_ = GST_CODEC_OK;
100     bool eventDone_ = false;
101     int32_t lastCmd_ = -2; // -1 for error cmd and -2 for invaild
102     OMX_STATETYPE curState_ = OMX_StateInvalid;
103     OMX_STATETYPE targetState_ = OMX_StateInvalid;
104     uint32_t inPortIndex_ = 0;
105     uint32_t outPortIndex_ = 0;
106     PortState inState_ = ACTIVATED;
107     PortState outState_ = ACTIVATED;
108     bool start_ = false;
109     uint32_t id_ = 0;
110     CompVerInfo verInfo_ = {};
111     TaskQueue taskQue_;
112     bool isError_ = false;
113     std::atomic<bool> startFormatChange_ = false;
114     std::shared_mutex bufferMgrMutex_;
115 };
116 } // namespace Media
117 } // namespace OHOS
118 #endif // OMX_CODEC_H
119