1 /*
2  * Copyright (c) 2022-2023 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 "screen_decision_center.h"
27 #include "iimage_source_processor.h"
28 #include "iscreen_source_trans.h"
29 #include "iscreen_source_trans_callback.h"
30 #include "iscreen_channel.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 class ScreenSourceTrans : public IScreenSourceTrans,
35     public IScreenChannelListener,
36     public IImageSourceProcessorListener,
37     public std::enable_shared_from_this<ScreenSourceTrans> {
38 public:
39     ScreenSourceTrans() = default;
40     ~ScreenSourceTrans() override = default;
41 
42     int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId) override;
43     int32_t Release() override;
44     int32_t Start() override;
45     int32_t Stop() override;
46     int32_t RegisterStateCallback(const std::shared_ptr<IScreenSourceTransCallback> &callback) override;
47     sptr<Surface> GetImageSurface() override;
48 
49     void OnSessionOpened() override;
50     void OnSessionClosed() override;
51     void OnDataReceived(const std::shared_ptr<DataBuffer> &data) override;
52     void OnImageProcessDone(const std::shared_ptr<DataBuffer> &data) override;
53     void OnProcessorStateNotify(int32_t state) override;
54     void OnDamageProcessDone(sptr<SurfaceBuffer> &surfaceBuffer, const std::vector<OHOS::Rect> &damages) override;
55     int32_t SetConsumerSurface() override;
56     void SetScreenVersion(const std::string &version) override;
57 
58 private:
59     int32_t CheckVideoParam(const VideoParam &param);
60     int32_t CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId);
61     int32_t InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId);
62     int32_t RegisterChannelListener();
63     int32_t RegisterProcessorListener(const VideoParam &localParam, const VideoParam &remoteParam);
64     void FeedChannelData();
65 
66 private:
67     static const constexpr char *DSCREEN_LOG_TAG = "ScreenSourceTrans";
68 
69     std::mutex sessionMtx_;
70     std::mutex dataMtx_;
71     std::condition_variable sessionCond_;
72     std::condition_variable dataCond_;
73     std::mutex dataQueueMtx_;
74 
75     bool isChannelReady_ = false;
76     sptr<Surface> consumerSurface_;
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     std::shared_ptr<ScreenDecisionCenter> screenDecisionCenter_;
83     std::string version_ = "1.0";
84 };
85 } // namespace DistributedHardware
86 } // namespace OHOS
87 #endif