1 /*
2  * Copyright (c) 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_AV_TRANSPORT_SENDER_ADAPTER_H
17 #define OHOS_AV_TRANSPORT_SENDER_ADAPTER_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <memory>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 
26 #include "dscreen_constants.h"
27 #include "i_av_engine_provider.h"
28 #include "video_param.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 class AVSenderAdapterCallback {
33 public:
AVSenderAdapterCallback()34     AVSenderAdapterCallback() {};
35     virtual ~AVSenderAdapterCallback() = default;
36     virtual void OnEngineEvent(DScreenEventType event, const std::string &content) = 0;
37     virtual void OnEngineMessage(const std::shared_ptr<AVTransMessage> &message) = 0;
38 };
39 
40 class AVTransSenderAdapter : public IAVSenderEngineCallback,
41                              public std::enable_shared_from_this<AVTransSenderAdapter> {
42 public:
AVTransSenderAdapter()43     AVTransSenderAdapter() {};
~AVTransSenderAdapter()44     ~AVTransSenderAdapter() override {};
45 
46     int32_t Initialize(IAVEngineProvider *providerPtr, const std::string &peerDevId);
47     int32_t Release();
48     int32_t Start();
49     int32_t Stop();
50     int32_t SetParameter(const AVTransTag &tag, const std::string &param);
51     int32_t PushData(const VideoData &video);
52     int32_t SendMessageToRemote(const std::shared_ptr<AVTransMessage> &message);
53     int32_t CreateControlChannel(const std::string &peerDevId);
54     int32_t RegisterAdapterCallback(const std::shared_ptr<AVSenderAdapterCallback> &callback);
55 
56     // interfaces from IAVSenderEngineCallback
57     int32_t OnSenderEvent(const AVTransEvent& event) override;
58     int32_t OnMessageReceived(const std::shared_ptr<AVTransMessage> &message) override;
59 
60 private:
61     int32_t WaitForChannelCreated();
62     int32_t WaitForAVTransStarted();
63 
64 private:
65     std::atomic<bool> initialized_ {false};
66     std::mutex chnCreatedMtx_;
67     std::mutex transStartedMtx_;
68     std::condition_variable chnCreatedCondVar_;
69     std::condition_variable transStartedCondVar_;
70     std::atomic<bool> chnCreateSuccess_ {false};
71     std::atomic<bool> transStartSuccess_ {false};
72 
73     std::shared_ptr<IAVSenderEngine> senderEngine_;
74     std::shared_ptr<AVSenderAdapterCallback> adapterCallback_;
75 };
76 } // namespace DistributedHardware
77 } // namespace OHOS
78 #endif