1 /* 2 * Copyright (c) 2024-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 AVCODEC_SAMPLE_VIDEO_ENCODER_H 17 #define AVCODEC_SAMPLE_VIDEO_ENCODER_H 18 19 #include "frame_record.h" 20 #include "avcodec_video_encoder.h" 21 #include "output/camera_output_capability.h" 22 #include "sample_info.h" 23 #include "camera_util.h" 24 #include "surface_buffer.h" 25 #include "icapture_session.h" 26 27 namespace OHOS { 28 namespace CameraStandard { 29 using namespace std; 30 using namespace OHOS::MediaAVCodec; 31 class VideoEncoder : public std::enable_shared_from_this<VideoEncoder> { 32 public: 33 VideoEncoder() = default; 34 explicit VideoEncoder(VideoCodecType type, ColorSpace colorSpace); 35 ~VideoEncoder(); 36 37 int32_t Create(const std::string &codecMime); 38 int32_t Config(); 39 int32_t Start(); 40 int32_t PushInputData(sptr<CodecAVBufferInfo> info); 41 int32_t NotifyEndOfStream(); 42 int32_t FreeOutputData(uint32_t bufferIndex); 43 bool EncodeSurfaceBuffer(sptr<FrameRecord> frameRecord); 44 int32_t Stop(); 45 int32_t Release(); 46 int32_t GetSurface(); 47 int32_t ReleaseSurfaceBuffer(sptr<FrameRecord> frameRecord); 48 int32_t DetachCodecBuffer(sptr<SurfaceBuffer> &surfaceBuffer, sptr<FrameRecord> frameRecord); 49 struct CallBack : public MediaCodecCallback { CallBackCallBack50 explicit CallBack(std::weak_ptr<VideoEncoder> encoder) : videoEncoder_(encoder) {} 51 ~CallBack() override = default; 52 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 53 void OnOutputFormatChanged(const Format &format) override; 54 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 55 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 56 private: 57 std::weak_ptr<VideoEncoder> videoEncoder_; 58 }; 59 bool IsHdr(ColorSpace colorSpace); 60 61 private: 62 int32_t SetCallback(); 63 int32_t Configure(); 64 void RestartVideoCodec(shared_ptr<Size> size, int32_t rotation); 65 bool EnqueueBuffer(sptr<FrameRecord> frameRecord, int32_t keyFrameInterval); 66 std::atomic<bool> isStarted_ { false }; 67 std::mutex encoderMutex_; 68 shared_ptr<AVCodecVideoEncoder> encoder_ = nullptr; 69 std::mutex contextMutex_; 70 sptr<VideoCodecUserData> context_ = nullptr; 71 shared_ptr<Size> size_; 72 int32_t rotation_ = 0; 73 std::mutex surfaceMutex_; // guard codecSurface_ 74 sptr<Surface> codecSurface_; 75 int32_t keyFrameInterval_ = KEY_FRAME_INTERVAL; 76 VideoCodecType videoCodecType_ = VIDEO_ENCODE_TYPE_AVC; 77 int32_t bitrate_ = 0; 78 bool successFrame_ = false; 79 int64_t preFrameTimestamp_ = 0; 80 bool isHdr_ = false; 81 }; 82 } // CameraStandard 83 } // OHOS 84 #endif // AVCODEC_SAMPLE_VIDEO_ENCODER_H