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 "native_avcodec_videoencoder.h"
21 #include "output/camera_output_capability.h"
22 #include "sample_info.h"
23 #include "camera_util.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 using namespace std;
28 class VideoEncoder {
29 public:
30     VideoEncoder() = default;
31     explicit VideoEncoder(VideoCodecType type);
32     ~VideoEncoder();
33 
34     int32_t Create(const std::string &codecMime);
35     int32_t Config();
36     int32_t Start();
37     int32_t PushInputData(sptr<CodecAVBufferInfo> info);
38     int32_t NotifyEndOfStream();
39     int32_t FreeOutputData(uint32_t bufferIndex);
40     bool EncodeSurfaceBuffer(sptr<FrameRecord> frameRecord);
41     int32_t Stop();
42     int32_t Release();
43     int32_t GetSurface();
44     int32_t ReleaseSurfaceBuffer(sptr<FrameRecord> frameRecord);
45 
46 private:
47     int32_t SetCallback(CodecUserData *codecUserData);
48     int32_t Configure();
49     void RestartVideoCodec(shared_ptr<Size> size, int32_t rotation);
50     bool EnqueueBuffer(sptr<FrameRecord> frameRecord, int32_t keyFrameInterval);
51     std::atomic<bool> isStarted_ { false };
52     std::mutex encoderMutex_;
53     OH_AVCodec *encoder_ = nullptr;
54     std::mutex contextMutex_;
55     CodecUserData *context_ = nullptr;
56     shared_ptr<Size> size_;
57     int32_t rotation_;
58     std::mutex surfaceMutex_; // guard codecSurface_
59     sptr<Surface> codecSurface_;
60     VideoCodecType videoCodecType_ = VIDEO_ENCODE_TYPE_AVC;
61     int32_t keyFrameInterval_ = KEY_FRAME_INTERVAL;
62     int32_t bitrate_ = 0;
63     bool successFrame_ = false;
64     int64_t preFrameTimestamp_ = 0;
65 };
66 } // CameraStandard
67 } // OHOS
68 #endif // AVCODEC_SAMPLE_VIDEO_ENCODER_H