• 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_WIFIDISPLAY_H
17 #define OHOS_SHARING_WIFIDISPLAY_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include "impl/scene/wfd/wfd_def.h"
23 #include "wfd_sink_impl.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 
28 enum WfdMode : int32_t {
29     SINK,
30     SOURCE,
31 };
32 
33 enum class CodecType : int32_t {
34     CODEC_NONE = -1,
35     CODEC_H264 = 1,
36     CODEC_H265,
37     CODEC_AAC,
38 };
39 
40 enum class CastDeviceState : int32_t {
41     CONNECTED,
42     DISCONNECTED,
43     INIT,
44     READY,
45     PLAYING,
46     PAUSED,
47     STOPED,
48 };
49 
50 struct CastCodecAttr {
51     int32_t format;
52     CodecType codecType;
53 };
54 
55 struct CastDeviceInfo {
56     std::string deviceId;
57     std::string ipAddr;
58     std::string deviceName;
59     std::string primaryDeviceType;
60     std::string secondaryDeviceType;
61     CastDeviceState state;
62 };
63 
64 struct CastErrorInfo {
65     std::string deviceId;
66     int32_t errorCode;
67 };
68 
69 class IWfdListener {
70 public:
71     virtual ~IWfdListener() = default;
72 
73     virtual void OnError(const CastErrorInfo &errorInfo) = 0;
74     virtual void OnDeviceState(const CastDeviceInfo &deviceInfo) = 0;
75     virtual void OnDeviceFound(const std::vector<CastDeviceInfo> &deviceInfos) = 0;
76 };
77 
78 class WifiDisplay : public IWfdEventListener,
79                     public std::enable_shared_from_this<WifiDisplay> {
80 public:
81     ~WifiDisplay();
82     WifiDisplay(WfdMode mode);
83 
84     void Init();
85     int32_t Stop();
86     int32_t Start();
87     int32_t Play(std::string devId);
88     int32_t Pause(std::string devId);
89     int32_t Close(std::string devId);
90     int32_t AppendSurface(std::string devId, uint64_t surfaceId);
91     int32_t SetMediaFormat(std::string deviceId, CastCodecAttr videoAttr, CastCodecAttr audioAttr);
92 
93     int32_t StopDiscover();
94     int32_t StartDiscover();
95     int32_t RemoveDevice(std::string deviceId);
96     int32_t AddDevice(const CastDeviceInfo &deviceInfo);
97 
98     int32_t SetListener(const std::shared_ptr<IWfdListener> &listener);
99 
100     void OnInfo(std::shared_ptr<BaseMsg> &info) override;
101 
102 private:
103     bool IsVideoCodecAttr(CastCodecAttr videoAttr);
104     bool IsAudioCodecAttr(CastCodecAttr audioAttr);
105 
106 private:
107     WfdMode mode_ = WfdMode::SINK;
108     std::weak_ptr<IWfdListener> listener_;
109     std::shared_ptr<WfdSink> wfdSinkImpl_ = nullptr;
110 };
111 
112 class __attribute__((visibility("default"))) MiracastFactory {
113 public:
114     static std::shared_ptr<WifiDisplay> GetInstance(WfdMode mode);
115 
116 private:
117     MiracastFactory() = default;
118     ~MiracastFactory() = default;
119 };
120 
121 } // namespace Sharing
122 } // namespace OHOS
123 
124 #endif // OHOS_SHARING_WFD_H