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 16 #ifndef OH_VEF_VIDEO_ENCODER_ENGINE_IMPL_H 17 #define OH_VEF_VIDEO_ENCODER_ENGINE_IMPL_H 18 19 #include "codec/audio/encoder/audio_encoder.h" 20 #include "codec/video_encoder_engine.h" 21 #include "ffrt.h" 22 #include "native_avsource.h" 23 #include "video_encoder.h" 24 #include "video_muxer.h" 25 #include "video_editor.h" 26 #include <list> 27 #include <native_avdemuxer.h> 28 #include <native_avsource.h> 29 namespace OHOS { 30 namespace Media { 31 32 enum class DecodeEngineState { 33 INIT = 0, 34 RUNNING, 35 FINISH_SUCCESS, 36 FINISH_FAILED, 37 CANCEL 38 }; 39 40 class VideoEncoderEngineImpl : public IVideoEncoderEngine { 41 public: 42 VideoEncoderEngineImpl(uint64_t id, VideoEncodeCallback* cb); 43 virtual ~VideoEncoderEngineImpl(); 44 45 uint64_t GetId() const override; 46 VEFError StartEncode() override; 47 VEFError StopEncode() override; 48 VEFError SendEos() override; 49 VEFError Flush() override; 50 OHNativeWindow* GetVideoInputWindow() override; 51 VEFError Init(const VideoEncodeParam& encodeParam) override; 52 std::shared_ptr<PcmBufferQueue> GetAudioInputBufferQueue() const override; 53 54 private: 55 VEFError InitVideoMuxer(const VideoMuxerParam& muxerParam); 56 VEFError InitAudioStreamEncoder(OH_AVFormat* audioFormat); 57 VEFError InitVideoStreamEncoder(OH_AVFormat* videoFormat); 58 void UnInit(); 59 void OnVideoNewOutputDataCallBack(OH_AVMemory* data, OH_AVCodecBufferAttr* attr); 60 void OnEncodeResult(CodecResult result); 61 VEFError OnAudioEncodeOutput(OH_AVCodec *codec, uint32_t index, OH_AVMemory* data, OH_AVCodecBufferAttr* attr); 62 63 private: 64 uint64_t id_{ 0 }; 65 std::string logTag_; 66 VideoEncodeCallback* cb_; 67 std::shared_ptr<VideoEncoder> encoder_{ nullptr }; 68 std::shared_ptr<AudioEncoder> audioEncoder_{ nullptr }; 69 std::shared_ptr<VideoMuxer> muxer_{ nullptr }; 70 std::mutex streamFinishMutex_; 71 72 // audio renderer 73 std::list<PcmData> pcmDataList_; 74 75 CodecState audioEncoderState_ { CodecState::INIT }; 76 CodecState videoEncoderState_ { CodecState::INIT }; 77 }; 78 79 } // namespace Media 80 } // namespace OHOS 81 82 #endif // OH_VEF_VIDEO_ENCODER_ENGINE_IMPL_H