• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SCREEN_CAPTURE_CONSUMER_H
17 #define OHOS_SHARING_SCREEN_CAPTURE_CONSUMER_H
18 
19 #include <mutex>
20 #include "magic_enum.hpp"
21 #include "mediachannel/base_consumer.h"
22 #include "video_source_encoder.h"
23 #include "video_source_screen.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 class ScreenCaptureConsumer : public BaseConsumer,
28                               public VideoSourceEncoderListener,
29                               public std::enable_shared_from_this<ScreenCaptureConsumer> {
30 public:
31     ScreenCaptureConsumer();
32     ~ScreenCaptureConsumer();
33 
34 public:
35     int32_t Release() override;
36     int32_t ReleaseScreenBuffer();
37     int32_t HandleEvent(SharingEvent &event) override;
38 
39     void OnInitVideoCaptureError();
40     void OnFrameBufferUsed() override;
41     void UpdateOperation(ProsumerStatusMsg::Ptr &statusMsg) override;
42     void OnFrame(const Frame::Ptr &frame, FRAME_TYPE frameType, bool keyFrame) override;
43 
44 private:
45     bool IsPaused();
46     bool StopCapture();
47     bool StartCapture();
48     bool StartVideoCapture();
49     bool Init(uint64_t screenId);
50     bool InitAudioCapture() const;
51     bool InitVideoCapture(uint64_t screenId);
52 
53     void HandleProsumerInitState(SharingEvent &event);
54     void HandleSpsFrame(BufferDispatcher::Ptr dispatcher, const Frame::Ptr &frame);
55     void HandlePpsFrame(BufferDispatcher::Ptr dispatcher, const Frame::Ptr &frame);
56 
57 private:
58     bool paused_ = false;
59 
60     std::mutex mutex_;
61     std::shared_ptr<VideoSourceScreen> videoSourceScreen_ = nullptr;
62     std::shared_ptr<VideoSourceEncoder> videoSourceEncoder_ = nullptr;
63 };
64 
65 } // namespace Sharing
66 } // namespace OHOS
67 #endif
68