1 /* 2 * Copyright (C) 2021 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 AVCODEC_ENGINE_CTRL_H 17 #define AVCODEC_ENGINE_CTRL_H 18 19 #include <condition_variable> 20 #include <cstdint> 21 #include <mutex> 22 #include "avcodec_engine_factory.h" 23 #include "i_avcodec_engine.h" 24 #include "nocopyable.h" 25 26 namespace OHOS { 27 namespace Media { 28 class AVCodecEngineCtrl : public NoCopyable { 29 public: 30 AVCodecEngineCtrl(); 31 ~AVCodecEngineCtrl(); 32 33 int32_t Init(AVCodecType type, bool useSoftware, const std::string &name); 34 int32_t Prepare(std::shared_ptr<ProcessorConfig> inputConfig, std::shared_ptr<ProcessorConfig> outputConfig); 35 int32_t Start(); 36 int32_t Stop(); 37 int32_t Flush(); 38 int32_t Release(); 39 void SetObs(const std::weak_ptr<IAVCodecEngineObs> &obs); 40 sptr<Surface> CreateInputSurface(std::shared_ptr<ProcessorConfig> inputConfig); 41 int32_t SetOutputSurface(sptr<Surface> surface); 42 std::shared_ptr<AVSharedMemory> GetInputBuffer(uint32_t index); 43 int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag); 44 std::shared_ptr<AVSharedMemory> GetOutputBuffer(uint32_t index); 45 int32_t ReleaseOutputBuffer(uint32_t index, bool render); 46 int32_t SetParameter(const Format &format); 47 48 private: 49 static GstBusSyncReply BusSyncHandler(GstBus *bus, GstMessage *message, gpointer userData); 50 51 AVCodecType codecType_ = AVCODEC_TYPE_VIDEO_ENCODER; 52 GstPipeline *gstPipeline_ = nullptr; 53 GstBus *bus_ = nullptr; 54 GstElement *codecBin_ = nullptr; 55 bool useSurfaceInput_ = false; 56 bool useSurfaceRender_ = false; 57 std::condition_variable gstPipeCond_; 58 std::mutex gstPipeMutex_; 59 std::weak_ptr<IAVCodecEngineObs> obs_; 60 std::unique_ptr<SrcBase> src_; 61 std::unique_ptr<SinkBase> sink_; 62 bool isEncoder_ = false; 63 bool flushAtStart_ = false; 64 bool isStart_ = false; 65 }; 66 } // namespace Media 67 } // namespace OHOS 68 #endif // AVCODEC_ENGINE_CTRL_H 69