• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 OHOS_SCREEN_SOURCE_TRANS_H
17 #define OHOS_SCREEN_SOURCE_TRANS_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <memory>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 
26 #include "iimage_source_processor.h"
27 #include "iscreen_source_trans.h"
28 #include "iscreen_source_trans_callback.h"
29 #include "iscreen_channel.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 class ScreenSourceTrans : public IScreenSourceTrans,
34     public IScreenChannelListener,
35     public IImageSourceProcessorListener,
36     public std::enable_shared_from_this<ScreenSourceTrans> {
37 public:
38     ScreenSourceTrans() = default;
39     ~ScreenSourceTrans() = default;
40 
41     int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId) override;
42     int32_t Release() override;
43     int32_t Start() override;
44     int32_t Stop() override;
45     int32_t RegisterStateCallback(const std::shared_ptr<IScreenSourceTransCallback> &callBack) override;
46     sptr<Surface> &GetImageSurface() override;
47 
48     void OnSessionOpened() override;
49     void OnSessionClosed() override;
50     void OnDataReceived(const std::shared_ptr<DataBuffer> &data) override;
51     void OnImageProcessDone(const std::shared_ptr<DataBuffer> &data) override;
52     void OnProcessorStateNotify(int32_t state) override;
53 
54 private:
55     int32_t CheckVideoParam(const VideoParam &param);
56     int32_t CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId);
57     int32_t InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId);
58     int32_t RegisterChannelListener();
59     int32_t RegisterProcessorListener(const VideoParam &localParam, const VideoParam &remoteParam);
60     void FeedChannelData();
61 
62 private:
63     static const constexpr char *LOG_TAG = "ScreenSourceTrans";
64     static constexpr uint8_t SESSION_WAIT_SECONDS = 5;
65     static constexpr uint8_t DATA_WAIT_SECONDS = 1;
66     static constexpr size_t DATA_QUEUE_MAX_SIZE = 1000;
67 
68     std::mutex sessionMtx_;
69     std::mutex dataMtx_;
70     std::condition_variable sessionCond_;
71     std::condition_variable dataCond_;
72     std::mutex dataQueueMtx_;
73     std::thread sendDataThread_;
74 
75     bool isChannelReady_ = false;
76     sptr<Surface> encoderSurface_;
77     std::queue<std::shared_ptr<DataBuffer>> dataQueue_;
78 
79     std::shared_ptr<IImageSourceProcessor> imageProcessor_;
80     std::shared_ptr<IScreenChannel> screenChannel_;
81     std::weak_ptr<IScreenSourceTransCallback> transCallback_;
82 };
83 } // namespace DistributedHardware
84 } // namespace OHOS
85 #endif