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 40 int32_t codecType_ = CodecId::CODEC_H264; 41 int32_t pixleFormat_ = static_cast<int32_t>(OHOS::MediaAVCodec::VideoPixelFormat::RGBA); 42 43 uint64_t srcScreenId_ = 0; 44 }; 45 46 class VideoSourceEncoderListener { 47 public: 48 virtual ~VideoSourceEncoderListener() = default; 49 50 virtual void OnFrameBufferUsed() = 0; 51 virtual void OnFrame(const Frame::Ptr &frame, FRAME_TYPE frameType, bool keyFrame) = 0; 52 }; 53 54 class VideoSourceEncoder : public std::enable_shared_from_this<VideoSourceEncoder> { 55 public: 56 class VideoEncodeCallback : public MediaAVCodec::AVCodecCallback { 57 public: VideoEncodeCallback(std::shared_ptr<VideoSourceEncoder> parent)58 VideoEncodeCallback(std::shared_ptr<VideoSourceEncoder> parent) : parent_(parent) {} 59 ~VideoEncodeCallback() = default; 60 61 void OnOutputFormatChanged(const MediaAVCodec::Format &format); 62 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode); 63 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer); 64 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 65 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer); 66 private: 67 std::weak_ptr<VideoSourceEncoder> parent_; 68 }; 69 VideoSourceEncoder(std::shared_ptr<VideoSourceEncoderListener> listener)70 VideoSourceEncoder(std::shared_ptr<VideoSourceEncoderListener> listener) : listener_(listener){}; 71 ~VideoSourceEncoder(); 72 73 bool StartEncoder(); 74 bool StopEncoder(); 75 bool ReleaseEncoder(); 76 bool InitEncoder(const VideoSourceConfigure &configure); 77 78 sptr<Surface> &GetEncoderSurface(); 79 80 protected: 81 void OnOutputFormatChanged(const MediaAVCodec::Format &format); 82 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode); 83 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer); 84 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 85 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer); 86 87 private: 88 bool CreateEncoder(const VideoSourceConfigure &configure); 89 bool ConfigEncoder(const VideoSourceConfigure &configure); 90 91 private: 92 sptr<OHOS::Surface> videoEncoderSurface_; 93 std::shared_ptr<VideoEncodeCallback> encoderCb_; 94 std::weak_ptr<VideoSourceEncoderListener> listener_; 95 std::shared_ptr<OHOS::MediaAVCodec::AVCodecVideoEncoder> videoEncoder_; 96 }; 97 } // namespace Sharing 98 } // namespace OHOS 99 #endif 100