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_WFD_MESSAGE_H
17 #define OHOS_SHARING_WFD_MESSAGE_H
18 
19 #include <sstream>
20 #include "protocol/rtsp/include/rtsp_request.h"
21 #include "protocol/rtsp/include/rtsp_response.h"
22 #include "wfd_session_def.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 
27 struct WfdVideoFormatsInfo {
28     uint16_t native = 0;
29     uint16_t preferredDisplayMode = 0;
30     uint16_t profile = 0;
31     uint16_t level = 0;
32     uint32_t ceaMask = 0;
33     uint32_t veaMask = 0;
34     uint32_t hhMask = 0;
35     uint16_t latency = 0;
36     uint16_t minSlice = 0;
37     uint16_t sliceEncParam = 0;
38     uint16_t frameRateCtlSupport = 0;
39 };
40 
41 struct WfdAudioCodec {
42     CodecId codecId;
43     AudioFormat format;
44 };
45 
46 // WfdRtspM1Request
47 class WfdRtspM1Request : public RtspRequestOptions {
48 public:
WfdRtspM1Request(int32_t cseq)49     explicit WfdRtspM1Request(int32_t cseq) : RtspRequestOptions(cseq, "*") { SetRequire(RTSP_METHOD_WFD); }
50 };
51 
52 class WfdRtspM1Response : public RtspResponseOptions {
53 public:
54     WfdRtspM1Response(int32_t cseq, int32_t status);
55 };
56 
57 class WfdRtspM2Request : public RtspRequestOptions {
58 public:
WfdRtspM2Request(int32_t cseq)59     explicit WfdRtspM2Request(int32_t cseq) : RtspRequestOptions(cseq, "*") { SetRequire(RTSP_METHOD_WFD); }
60 };
61 
62 // WfdRtspM2Response
63 class WfdRtspM2Response : public RtspResponseOptions {
64 public:
WfdRtspM2Response(int32_t cseq, int32_t status)65     WfdRtspM2Response(int32_t cseq, int32_t status) : RtspResponseOptions(cseq, status)
66     {
67         std::stringstream ss;
68         ss << RTSP_METHOD_WFD << "," << RTSP_SP << RTSP_METHOD_SETUP << "," << RTSP_SP << RTSP_METHOD_TEARDOWN << ","
69            << RTSP_SP << RTSP_METHOD_PLAY << "," << RTSP_SP << RTSP_METHOD_PAUSE << "," << RTSP_SP
70            << RTSP_METHOD_GET_PARAMETER << "," << RTSP_SP << RTSP_METHOD_SET_PARAMETER;
71         SetPublicItems(ss.str());
72     }
73 };
74 
75 // WfdRtspM3Request
76 class WfdRtspM3Request : public RtspRequestParameter {
77 public:
WfdRtspM3Request(int32_t cseq, const std::string &url)78     WfdRtspM3Request(int32_t cseq, const std::string &url) : RtspRequestParameter(RTSP_METHOD_GET_PARAMETER, cseq, url)
79     {
80         AddBodyItem(WFD_PARAM_CONTENT_PROTECTION);
81         AddBodyItem(WFD_PARAM_VIDEO_FORMATS);
82         AddBodyItem(WFD_PARAM_AUDIO_CODECS);
83         AddBodyItem(WFD_PARAM_RTP_PORTS);
84     }
85 };
86 
87 class WfdRtspM3Response : public RtspResponse {
88 public:
89     WfdRtspM3Response() = default;
90     ~WfdRtspM3Response() override = default;
WfdRtspM3Response(int32_t cseq, int32_t status)91     WfdRtspM3Response(int32_t cseq, int32_t status) : RtspResponse(cseq, status) {}
92 
93     void SetClientRtpPorts(int32_t port);
94     void SetUibcCapability(const std::string &value = "none");
95     void SetAudioCodecs(AudioFormat format = AUDIO_48000_16_2);
96     void SetCoupledSink(const std::string &value = "00  none");
97     void SetContentProtection(const std::string &value = "none");
98     void SetVideoFormats(VideoFormat format = VIDEO_1920X1080_30);
99     void SetStandbyResumeCapability(const std::string &value = "none");
100     void SetCustomParam(const std::string &key, const std::string &value = "none");
101 
102     int32_t GetClientRtpPorts();
103     std::string GetUibcCapability();
104     AudioFormat GetAudioCodecs(WfdAudioCodec &codec);
105     std::string GetCoupledSink();
106     std::string GetContentProtection();
107     VideoFormat GetVideoFormats();
108     std::string GetStandbyResumeCapability();
109     std::string GetCustomParam(const std::string &key);
110     VideoFormat GetVideoFormatsByCea(int index);
111     std::string Stringify() override;
112 
113     RtspError Parse(const std::string &response) override;
GetWfdVideoFormatsInfo()114     WfdVideoFormatsInfo GetWfdVideoFormatsInfo() { return vWfdVideoFormatsInfo_[0]; }
115 
116 private:
117     std::list<std::pair<std::string, std::string>> params_;
118     std::vector<WfdVideoFormatsInfo> vWfdVideoFormatsInfo_;
119 };
120 
121 class WfdRtspM4Request : public RtspRequestParameter {
122 public:
123     WfdRtspM4Request() = default;
124     ~WfdRtspM4Request() override = default;
WfdRtspM4Request(int32_t cseq, const std::string &url)125     WfdRtspM4Request(int32_t cseq, const std::string &url) : RtspRequestParameter(RTSP_METHOD_SET_PARAMETER, cseq, url)
126     {
127     }
128 
129     void SetClientRtpPorts(int32_t port);
130     void SetAudioCodecs(WfdAudioCodec &codec);
131     void SetPresentationUrl(const std::string &ip);
132     void SetVideoFormats(const WfdVideoFormatsInfo &wfdVideoFormatsInfo, VideoFormat format = VIDEO_1920X1080_30);
133 
134     int32_t GetRtpPort();
135 
136     std::string GetPresentationUrl();
137     std::string GetParameterValue(const std::string &param);
138 
139     RtspError Parse(const std::string &request) override;
140 
141 private:
142     std::list<std::pair<std::string, std::string>> params_;
143 };
144 
145 using WfdRtspM4Response = RtspResponse;
146 
147 // WfdRtspM5Request
148 class WfdRtspM5Request : public RtspRequestParameter {
149 public:
150     WfdRtspM5Request() = default;
WfdRtspM5Request(int32_t cseq)151     explicit WfdRtspM5Request(int32_t cseq) : RtspRequestParameter(RTSP_METHOD_SET_PARAMETER,
152         cseq, WFD_RTSP_URL_DEFAULT) {}
153 
154     void SetTriggerMethod(const std::string &method);
155     std::string GetTriggerMethod();
156 };
157 
158 using WfdRtspM5Response = RtspResponse;
159 using WfdRtspM6Request = RtspRequestSetup;
160 
161 // WfdRtspM6Response
162 class WfdRtspM6Response : public RtspResponse {
163 public:
164     WfdRtspM6Response() = default;
WfdRtspM6Response(int32_t cseq, int32_t status, std::string sessionID, int32_t timeOut)165     WfdRtspM6Response(int32_t cseq, int32_t status, std::string sessionID, int32_t timeOut) : RtspResponse(cseq, status)
166     {
167         SetSession(sessionID);
168         SetTimeout(timeOut);
169     }
170     std::string StringifyEx();
171     int32_t GetClientPort();
172     int32_t GetServerPort();
173     void SetClientPort(int port);
174     void SetServerPort(int port);
175 
176 private:
177     int32_t clientPort_ = 0;
178     int32_t serverPort_ = 0;
179 };
180 
181 using WfdRtspM7Request = RtspRequestPlay;
182 using WfdRtspM8Request = RtspRequestTeardown;
183 
184 class WfdRtspIDRRequest : public RtspRequestParameter {
185 public:
186     WfdRtspIDRRequest() = default;
WfdRtspIDRRequest(int32_t cseq, const std::string &url)187     explicit WfdRtspIDRRequest(int32_t cseq, const std::string &url)
188         : RtspRequestParameter(RTSP_METHOD_SET_PARAMETER, cseq, url)
189     {
190         AddBodyItem(WFD_PARAM_IDR_REQUEST);
191     }
192 };
193 
194 // WfdRtspM7Response
195 class WfdRtspM7Response : public RtspResponse {
196 public:
197     WfdRtspM7Response() = default;
WfdRtspM7Response(int32_t cseq, int32_t status, std::string sessionID)198     WfdRtspM7Response(int32_t cseq, int32_t status, std::string sessionID) : RtspResponse(cseq, status)
199     {
200         SetSession(sessionID);
201     }
202     std::string StringifyEx();
203 };
204 
205 using WfdRtspM8Response = RtspResponse;
206 
207 class WfdRtspM16Request : public RtspRequestParameter {
208 public:
WfdRtspM16Request(int32_t cseq, const std::string &url, std::string sessionID)209     WfdRtspM16Request(int32_t cseq, const std::string &url, std::string sessionID)
210         : RtspRequestParameter(RTSP_METHOD_GET_PARAMETER, cseq, url)
211     {
212         SetSession(sessionID);
213     }
214 };
215 
216 using WfdRtspM16Response = RtspResponse;
217 
218 } // namespace Sharing
219 } // namespace OHOS
220 #endif
221