1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_VIDEO_SOURCE_ENCODER_H 17 #define OHOS_SHARING_VIDEO_SOURCE_ENCODER_H 18 19 #include "avcodec_video_encoder.h" 20 #include "common/const_def.h" 21 #include "protocol/frame/frame.h" 22 #include "utils/data_buffer.h" 23 24 namespace OHOS { 25 namespace Sharing { 26 enum FRAME_TYPE { 27 SPS_FRAME, 28 PPS_FRAME, 29 IDR_FRAME 30 }; 31 32 class VideoSourceConfigure { 33 public: 34 uint32_t screenWidth_ = DEFAULT_VIDEO_WIDTH; 35 uint32_t screenHeight_ = DEFAULT_VIDEO_HEIGHT; 36 uint32_t videoWidth_ = DEFAULT_VIDEO_WIDTH; 37 uint32_t videoHeight_ = DEFAULT_VIDEO_HEIGHT; 38 uint32_t frameRate_ = DEFAULT_FRAME_RATE; 39 int32_t codecType_ = CodecId::CODEC_H264; 40 int32_t pixleFormat_ = Media::VideoPixelFormat::RGBA; 41 uint64_t srcScreenId_ = 0; 42 }; 43 44 class VideoSourceEncoderListener { 45 public: 46 virtual ~VideoSourceEncoderListener() = default; 47 48 virtual void OnFrameBufferUsed() = 0; 49 virtual void OnFrame(const Frame::Ptr &frame, FRAME_TYPE frameType, bool keyFrame) = 0; 50 }; 51 52 class VideoSourceEncoder : public std::enable_shared_from_this<VideoSourceEncoder> { 53 public: 54 class VideoEncodeCallback : public Media::AVCodecCallback { 55 public: VideoEncodeCallback(std::shared_ptr<VideoSourceEncoder> parent)56 VideoEncodeCallback(std::shared_ptr<VideoSourceEncoder> parent) : parent_(parent) {} 57 ~VideoEncodeCallback() = default; 58 59 void OnInputBufferAvailable(uint32_t index); 60 void OnOutputFormatChanged(const Media::Format &format); 61 void OnError(Media::AVCodecErrorType errorType, int32_t errorCode); 62 void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); 63 64 private: 65 std::weak_ptr<VideoSourceEncoder> parent_; 66 }; 67 VideoSourceEncoder(std::shared_ptr<VideoSourceEncoderListener> listener)68 VideoSourceEncoder(std::shared_ptr<VideoSourceEncoderListener> listener) : listener_(listener){}; 69 ~VideoSourceEncoder(); 70 71 bool StartEncoder(); 72 bool StopEncoder(); 73 bool ReleaseEncoder(); 74 bool InitEncoder(const VideoSourceConfigure &configure); 75 76 sptr<Surface> &GetEncoderSurface(); 77 78 protected: 79 void OnInputBufferAvailable(uint32_t index); 80 void OnOutputFormatChanged(const Media::Format &format); 81 void OnError(Media::AVCodecErrorType errorType, int32_t errorCode); 82 void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); 83 84 private: 85 bool CreateEncoder(const VideoSourceConfigure &configure); 86 bool ConfigEncoder(const VideoSourceConfigure &configure); 87 88 private: 89 sptr<OHOS::Surface> videoEncoderSurface_; 90 std::shared_ptr<VideoEncodeCallback> encoderCb_; 91 std::weak_ptr<VideoSourceEncoderListener> listener_; 92 std::shared_ptr<OHOS::Media::AVCodecVideoEncoder> videoEncoder_; 93 }; 94 } // namespace Sharing 95 } // namespace OHOS 96 #endif 97