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