1 /* 2 * Copyright (c) 2023-2024 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_SCREEN_CAPTURE_CONSUMER_H 17 #define OHOS_SHARING_SCREEN_CAPTURE_CONSUMER_H 18 19 #include <mutex> 20 #include "audio_aac_codec.h" 21 #include "audio_source_capturer.h" 22 #include "codec_factory.h" 23 #include "magic_enum.hpp" 24 #include "mediachannel/base_consumer.h" 25 #include "video_source_encoder.h" 26 #include "video_source_screen.h" 27 28 namespace OHOS { 29 namespace Sharing { 30 class ScreenCaptureConsumer : public BaseConsumer, 31 public VideoSourceEncoderListener, 32 public std::enable_shared_from_this<ScreenCaptureConsumer> { 33 public: 34 class AudioEncoderReceiver : public FrameDestination { 35 public: AudioEncoderReceiver(std::weak_ptr<ScreenCaptureConsumer> parent)36 explicit AudioEncoderReceiver(std::weak_ptr<ScreenCaptureConsumer> parent) : parent_(parent){}; 37 38 void OnFrame(const Frame::Ptr &frame) override; 39 40 private: 41 std::weak_ptr<ScreenCaptureConsumer> parent_; 42 }; 43 44 ScreenCaptureConsumer(); 45 ~ScreenCaptureConsumer(); 46 47 public: 48 int32_t Release() override; 49 int32_t ReleaseScreenBuffer(); 50 int32_t HandleEvent(SharingEvent &event) override; 51 52 void OnInitVideoCaptureError(); 53 void OnFrameBufferUsed() override; 54 void UpdateOperation(ProsumerStatusMsg::Ptr &statusMsg) override; 55 void OnFrame(const Frame::Ptr &frame, FRAME_TYPE frameType, bool keyFrame) override; 56 57 private: 58 bool IsPaused(); 59 bool StopCapture(); 60 bool StartCapture(); 61 bool StopAudioCapture(); 62 bool StopVideoCapture(); 63 bool StartAudioCapture(); 64 bool StartVideoCapture(); 65 66 bool InitAudioCapture(); 67 bool InitAudioEncoder(); 68 bool Init(uint64_t screenId); 69 bool InitVideoCapture(uint64_t screenId); 70 71 void HandleProsumerInitState(SharingEvent &event); 72 void HandleSpsFrame(BufferDispatcher::Ptr dispatcher, const Frame::Ptr &frame); 73 void HandlePpsFrame(BufferDispatcher::Ptr dispatcher, const Frame::Ptr &frame); 74 75 private: 76 bool paused_ = false; 77 78 std::mutex mutex_; 79 80 std::shared_ptr<VideoSourceScreen> videoSourceScreen_ = nullptr; 81 std::shared_ptr<VideoSourceEncoder> videoSourceEncoder_ = nullptr; 82 83 std::shared_ptr<AudioEncoder> audioEncoder_ = nullptr; 84 std::shared_ptr<AudioSourceCapturer> audioSourceCapturer_ = nullptr; 85 std::shared_ptr<AudioEncoderReceiver> audioEncoderReceiver_ = nullptr; 86 }; 87 88 } // namespace Sharing 89 } // namespace OHOS 90 #endif 91