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_H 17 #define OH_VEF_VIDEO_ENCODER_H 18 19 #include <mutex> 20 #include <string> 21 #include <functional> 22 #include <native_avcodec_base.h> 23 #include <native_avmuxer.h> 24 #include "ffrt.h" 25 #include "video_editor.h" 26 27 namespace OHOS { 28 namespace Media { 29 using OnNewOutputDataCallBack = std::function<void(OH_AVMemory* data, OH_AVCodecBufferAttr* attr)>; 30 31 class VideoEncoder { 32 public: 33 VideoEncoder(uint64_t id, const OnNewOutputDataCallBack& cb); 34 ~VideoEncoder(); 35 36 VEFError Init(OH_AVFormat* format); 37 VEFError Start(); 38 VEFError Stop(); 39 VEFError SendEos(); 40 VEFError Flush(); 41 OHNativeWindow* GetOHNativeWindow() const; 42 43 static void CodecOnStreamChanged(OH_AVCodec* codec, OH_AVFormat* format, void* userData); 44 static void CodecOnNeedInputData(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, void* userData); 45 static void CodecOnError(OH_AVCodec* codec, int32_t errorCode, void* userData); 46 static void CodecOnNewOutputData(OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, OH_AVCodecBufferAttr* attr, 47 void* userData); 48 49 private: 50 VEFError CreateEncoder(OH_AVFormat* format); 51 VEFError ConfigureEncoder(OH_AVFormat* format); 52 VEFError WriteFrame(OH_AVMemory* data, OH_AVCodecBufferAttr* attr); 53 54 private: 55 std::string logTag_ = ""; 56 ffrt::mutex encoderMutex_; 57 OH_AVCodec* encoder_ = nullptr; 58 std::atomic_bool codecState_ = false; 59 OHNativeWindow* nativeWindow_ = nullptr; 60 OnNewOutputDataCallBack onNewOutputDataCallBack_ = nullptr; 61 }; 62 } // namespace Media 63 } // namespace OHOS 64 65 #endif // OH_VEF_VIDEO_ENCODER_H 66