1 /* 2 * Copyright (c) 2024 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 #ifndef HDI_CODEC_H 16 #define HDI_CODEC_H 17 #include <condition_variable> 18 #include <mutex> 19 #include "buffer/avbuffer.h" 20 #include "v3_0/codec_ext_types.h" 21 #include "v3_0/icodec_component_manager.h" 22 23 namespace OHOS { 24 namespace Media { 25 namespace Plugins { 26 namespace Hdi { 27 using namespace OHOS::HDI::Codec::V3_0; 28 29 struct AudioCodecOmxParam { 30 uint32_t size; 31 union { 32 struct { 33 uint8_t nVersionMajor; 34 uint8_t nVersionMinor; 35 uint8_t nRevision; 36 uint8_t nStep; 37 } s; 38 uint32_t nVersion; 39 } version; 40 uint32_t sampleRate; 41 uint32_t sampleFormat; 42 uint32_t channels; 43 uint32_t bitRate; 44 uint32_t reserved; 45 }; 46 47 class HdiCodec : public std::enable_shared_from_this<HdiCodec> { 48 public: 49 enum class PortIndex : uint32_t { 50 INPUT_PORT = 0, 51 OUTPUT_PORT = 1 52 }; 53 54 HdiCodec(); 55 56 Status InitComponent(const std::string &name); 57 58 bool IsSupportCodecType(const std::string &name); 59 60 void InitParameter(AudioCodecOmxParam ¶m); 61 62 Status GetParameter(uint32_t index, AudioCodecOmxParam ¶m); 63 64 Status SetParameter(uint32_t index, const std::vector<int8_t> ¶mVec); 65 66 Status InitBuffers(uint32_t bufferSize); 67 68 Status SendCommand(CodecCommandType cmd, uint32_t param); 69 70 Status EmptyThisBuffer(const std::shared_ptr<AVBuffer> &buffer); 71 72 Status FillThisBuffer(std::shared_ptr<AVBuffer> &buffer); 73 74 Status OnEventHandler(CodecEventType event, const EventInfo &info); 75 76 Status OnEmptyBufferDone(const OmxCodecBuffer &buffer); 77 78 Status OnFillBufferDone(const OmxCodecBuffer &buffer); 79 80 Status Reset(); 81 82 void Release(); 83 84 class HdiCallback : public ICodecCallback { 85 public: 86 explicit HdiCallback(std::shared_ptr<HdiCodec> hdiCodec); 87 virtual ~HdiCallback() = default; 88 int32_t EventHandler(CodecEventType event, const EventInfo &info) override; 89 int32_t EmptyBufferDone(int64_t appData, const OmxCodecBuffer &buffer) override; 90 int32_t FillBufferDone(int64_t appData, const OmxCodecBuffer &buffer) override; 91 private: 92 std::shared_ptr<HdiCodec> hdiCodec_ = nullptr; 93 }; 94 95 private: 96 sptr<ICodecComponentManager> GetComponentManager(); 97 std::vector<CodecCompCapability> GetCapabilityList(); 98 Status InitBuffersByPort(PortIndex portIndex, uint32_t bufferSize); 99 Status FreeBuffer(PortIndex portIndex, const std::shared_ptr<OmxCodecBuffer> &omxBuffer); 100 101 private: 102 struct OmxBufferInfo { 103 std::shared_ptr<OmxCodecBuffer> omxBuffer = nullptr; 104 std::shared_ptr<AVBuffer> avBuffer = nullptr; 105 ~OmxBufferInfoOmxBufferInfo106 ~OmxBufferInfo() 107 { 108 omxBuffer = nullptr; 109 avBuffer = nullptr; 110 } 111 ResetOmxBufferInfo112 void Reset() 113 { 114 omxBuffer = nullptr; 115 avBuffer = nullptr; 116 } 117 }; 118 119 uint32_t componentId_; 120 std::string componentName_; 121 sptr<ICodecCallback> compCb_; 122 sptr<ICodecComponent> compNode_; 123 sptr<ICodecComponentManager> compMgr_; 124 std::shared_ptr<OmxCodecBuffer> outputOmxBuffer_; 125 std::shared_ptr<OmxBufferInfo> omxInBufferInfo_; 126 std::shared_ptr<OmxBufferInfo> omxOutBufferInfo_; 127 CodecEventType event_; 128 std::mutex inMutex_; 129 std::mutex outMutex_; 130 std::condition_variable condition_; 131 }; 132 } // namespace Hdi 133 } // namespace Plugins 134 } // namespace Media 135 } // namespace OHOS 136 #endif