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