OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2010 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 /* | |
29 * Documentation is in mediamessages.h. | |
30 */ | |
31 | |
32 #include "webrtc/libjingle/session/media/mediamessages.h" | |
33 | |
34 #include "webrtc/p2p/base/constants.h" | |
35 #include "webrtc/libjingle/session/media/mediasessionclient.h" | |
36 #include "webrtc/libjingle/xmllite/xmlelement.h" | |
37 #include "webrtc/base/logging.h" | |
38 #include "webrtc/base/stringencode.h" | |
39 | |
40 namespace cricket { | |
41 | |
42 namespace { | |
43 | |
44 bool ParseSsrc(const std::string& string, uint32* ssrc) { | |
45 return rtc::FromString(string, ssrc); | |
46 } | |
47 | |
48 // Builds a <view> element according to the following spec: | |
49 // goto/jinglemuc | |
50 buzz::XmlElement* CreateViewElem(const std::string& name, | |
51 const std::string& type) { | |
52 buzz::XmlElement* view_elem = | |
53 new buzz::XmlElement(QN_JINGLE_DRAFT_VIEW, true); | |
54 view_elem->AddAttr(QN_NAME, name); | |
55 view_elem->SetAttr(QN_TYPE, type); | |
56 return view_elem; | |
57 } | |
58 | |
59 buzz::XmlElement* CreateVideoViewElem(const std::string& content_name, | |
60 const std::string& type) { | |
61 return CreateViewElem(content_name, type); | |
62 } | |
63 | |
64 buzz::XmlElement* CreateNoneVideoViewElem(const std::string& content_name) { | |
65 return CreateVideoViewElem(content_name, STR_JINGLE_DRAFT_VIEW_TYPE_NONE); | |
66 } | |
67 | |
68 buzz::XmlElement* CreateStaticVideoViewElem(const std::string& content_name, | |
69 const StaticVideoView& view) { | |
70 buzz::XmlElement* view_elem = | |
71 CreateVideoViewElem(content_name, STR_JINGLE_DRAFT_VIEW_TYPE_STATIC); | |
72 AddXmlAttr(view_elem, QN_SSRC, view.selector.ssrc); | |
73 | |
74 buzz::XmlElement* params_elem = new buzz::XmlElement(QN_JINGLE_DRAFT_PARAMS); | |
75 AddXmlAttr(params_elem, QN_WIDTH, view.width); | |
76 AddXmlAttr(params_elem, QN_HEIGHT, view.height); | |
77 AddXmlAttr(params_elem, QN_FRAMERATE, view.framerate); | |
78 AddXmlAttr(params_elem, QN_PREFERENCE, view.preference); | |
79 view_elem->AddElement(params_elem); | |
80 | |
81 return view_elem; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 bool IsJingleViewRequest(const buzz::XmlElement* action_elem) { | |
87 return action_elem->FirstNamed(QN_JINGLE_DRAFT_VIEW) != NULL; | |
88 } | |
89 | |
90 bool ParseStaticVideoView(const buzz::XmlElement* view_elem, | |
91 StaticVideoView* view, | |
92 ParseError* error) { | |
93 uint32 ssrc; | |
94 if (!ParseSsrc(view_elem->Attr(QN_SSRC), &ssrc)) { | |
95 return BadParse("Invalid or missing view ssrc.", error); | |
96 } | |
97 view->selector = StreamSelector(ssrc); | |
98 | |
99 const buzz::XmlElement* params_elem = | |
100 view_elem->FirstNamed(QN_JINGLE_DRAFT_PARAMS); | |
101 if (params_elem) { | |
102 view->width = GetXmlAttr(params_elem, QN_WIDTH, 0); | |
103 view->height = GetXmlAttr(params_elem, QN_HEIGHT, 0); | |
104 view->framerate = GetXmlAttr(params_elem, QN_FRAMERATE, 0); | |
105 view->preference = GetXmlAttr(params_elem, QN_PREFERENCE, 0); | |
106 } else { | |
107 return BadParse("Missing view params.", error); | |
108 } | |
109 | |
110 return true; | |
111 } | |
112 | |
113 bool ParseJingleViewRequest(const buzz::XmlElement* action_elem, | |
114 ViewRequest* view_request, | |
115 ParseError* error) { | |
116 for (const buzz::XmlElement* view_elem = | |
117 action_elem->FirstNamed(QN_JINGLE_DRAFT_VIEW); | |
118 view_elem != NULL; | |
119 view_elem = view_elem->NextNamed(QN_JINGLE_DRAFT_VIEW)) { | |
120 std::string type = view_elem->Attr(QN_TYPE); | |
121 if (STR_JINGLE_DRAFT_VIEW_TYPE_NONE == type) { | |
122 view_request->static_video_views.clear(); | |
123 return true; | |
124 } else if (STR_JINGLE_DRAFT_VIEW_TYPE_STATIC == type) { | |
125 StaticVideoView static_video_view(StreamSelector(0), 0, 0, 0); | |
126 if (!ParseStaticVideoView(view_elem, &static_video_view, error)) { | |
127 return false; | |
128 } | |
129 view_request->static_video_views.push_back(static_video_view); | |
130 } else { | |
131 LOG(LS_INFO) << "Ingnoring unknown view type: " << type; | |
132 } | |
133 } | |
134 return true; | |
135 } | |
136 | |
137 bool WriteJingleViewRequest(const std::string& content_name, | |
138 const ViewRequest& request, | |
139 XmlElements* elems, | |
140 WriteError* error) { | |
141 if (request.static_video_views.empty()) { | |
142 elems->push_back(CreateNoneVideoViewElem(content_name)); | |
143 } else { | |
144 for (StaticVideoViews::const_iterator view = | |
145 request.static_video_views.begin(); | |
146 view != request.static_video_views.end(); ++view) { | |
147 elems->push_back(CreateStaticVideoViewElem(content_name, *view)); | |
148 } | |
149 } | |
150 return true; | |
151 } | |
152 | |
153 bool ParseSsrcAsLegacyStream(const buzz::XmlElement* desc_elem, | |
154 std::vector<StreamParams>* streams, | |
155 ParseError* error) { | |
156 const std::string ssrc_str = desc_elem->Attr(QN_SSRC); | |
157 if (!ssrc_str.empty()) { | |
158 uint32 ssrc; | |
159 if (!ParseSsrc(ssrc_str, &ssrc)) { | |
160 return BadParse("Missing or invalid ssrc.", error); | |
161 } | |
162 | |
163 streams->push_back(StreamParams::CreateLegacy(ssrc)); | |
164 } | |
165 return true; | |
166 } | |
167 | |
168 bool ParseSsrcs(const buzz::XmlElement* parent_elem, | |
169 std::vector<uint32>* ssrcs, | |
170 ParseError* error) { | |
171 for (const buzz::XmlElement* ssrc_elem = | |
172 parent_elem->FirstNamed(QN_JINGLE_DRAFT_SSRC); | |
173 ssrc_elem != NULL; | |
174 ssrc_elem = ssrc_elem->NextNamed(QN_JINGLE_DRAFT_SSRC)) { | |
175 uint32 ssrc; | |
176 if (!ParseSsrc(ssrc_elem->BodyText(), &ssrc)) { | |
177 return BadParse("Missing or invalid ssrc.", error); | |
178 } | |
179 | |
180 ssrcs->push_back(ssrc); | |
181 } | |
182 return true; | |
183 } | |
184 | |
185 bool ParseSsrcGroups(const buzz::XmlElement* parent_elem, | |
186 std::vector<SsrcGroup>* ssrc_groups, | |
187 ParseError* error) { | |
188 for (const buzz::XmlElement* group_elem = | |
189 parent_elem->FirstNamed(QN_JINGLE_DRAFT_SSRC_GROUP); | |
190 group_elem != NULL; | |
191 group_elem = group_elem->NextNamed(QN_JINGLE_DRAFT_SSRC_GROUP)) { | |
192 std::string semantics = group_elem->Attr(QN_SEMANTICS); | |
193 std::vector<uint32> ssrcs; | |
194 if (!ParseSsrcs(group_elem, &ssrcs, error)) { | |
195 return false; | |
196 } | |
197 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs)); | |
198 } | |
199 return true; | |
200 } | |
201 | |
202 bool ParseJingleStream(const buzz::XmlElement* stream_elem, | |
203 std::vector<StreamParams>* streams, | |
204 ParseError* error) { | |
205 StreamParams stream; | |
206 // We treat the nick as a stream groupid. | |
207 stream.groupid = stream_elem->Attr(QN_NICK); | |
208 stream.id = stream_elem->Attr(QN_NAME); | |
209 stream.type = stream_elem->Attr(QN_TYPE); | |
210 stream.display = stream_elem->Attr(QN_DISPLAY); | |
211 stream.cname = stream_elem->Attr(QN_CNAME); | |
212 if (!ParseSsrcs(stream_elem, &(stream.ssrcs), error)) { | |
213 return false; | |
214 } | |
215 std::vector<SsrcGroup> ssrc_groups; | |
216 if (!ParseSsrcGroups(stream_elem, &(stream.ssrc_groups), error)) { | |
217 return false; | |
218 } | |
219 streams->push_back(stream); | |
220 return true; | |
221 } | |
222 | |
223 bool ParseJingleRtpHeaderExtensions(const buzz::XmlElement* parent_elem, | |
224 std::vector<RtpHeaderExtension>* hdrexts, | |
225 ParseError* error) { | |
226 for (const buzz::XmlElement* hdrext_elem = | |
227 parent_elem->FirstNamed(QN_JINGLE_RTP_HDREXT); | |
228 hdrext_elem != NULL; | |
229 hdrext_elem = hdrext_elem->NextNamed(QN_JINGLE_RTP_HDREXT)) { | |
230 std::string uri = hdrext_elem->Attr(QN_URI); | |
231 int id = GetXmlAttr(hdrext_elem, QN_ID, 0); | |
232 if (id <= 0) { | |
233 return BadParse("Invalid RTP header extension id.", error); | |
234 } | |
235 hdrexts->push_back(RtpHeaderExtension(uri, id)); | |
236 } | |
237 return true; | |
238 } | |
239 | |
240 bool HasJingleStreams(const buzz::XmlElement* desc_elem) { | |
241 const buzz::XmlElement* streams_elem = | |
242 desc_elem->FirstNamed(QN_JINGLE_DRAFT_STREAMS); | |
243 return (streams_elem != NULL); | |
244 } | |
245 | |
246 bool ParseJingleStreams(const buzz::XmlElement* desc_elem, | |
247 std::vector<StreamParams>* streams, | |
248 ParseError* error) { | |
249 const buzz::XmlElement* streams_elem = | |
250 desc_elem->FirstNamed(QN_JINGLE_DRAFT_STREAMS); | |
251 if (streams_elem == NULL) { | |
252 return BadParse("Missing streams element.", error); | |
253 } | |
254 for (const buzz::XmlElement* stream_elem = | |
255 streams_elem->FirstNamed(QN_JINGLE_DRAFT_STREAM); | |
256 stream_elem != NULL; | |
257 stream_elem = stream_elem->NextNamed(QN_JINGLE_DRAFT_STREAM)) { | |
258 if (!ParseJingleStream(stream_elem, streams, error)) { | |
259 return false; | |
260 } | |
261 } | |
262 return true; | |
263 } | |
264 | |
265 void WriteSsrcs(const std::vector<uint32>& ssrcs, | |
266 buzz::XmlElement* parent_elem) { | |
267 for (std::vector<uint32>::const_iterator ssrc = ssrcs.begin(); | |
268 ssrc != ssrcs.end(); ++ssrc) { | |
269 buzz::XmlElement* ssrc_elem = | |
270 new buzz::XmlElement(QN_JINGLE_DRAFT_SSRC, false); | |
271 SetXmlBody(ssrc_elem, *ssrc); | |
272 | |
273 parent_elem->AddElement(ssrc_elem); | |
274 } | |
275 } | |
276 | |
277 void WriteSsrcGroups(const std::vector<SsrcGroup>& groups, | |
278 buzz::XmlElement* parent_elem) { | |
279 for (std::vector<SsrcGroup>::const_iterator group = groups.begin(); | |
280 group != groups.end(); ++group) { | |
281 buzz::XmlElement* group_elem = | |
282 new buzz::XmlElement(QN_JINGLE_DRAFT_SSRC_GROUP, false); | |
283 AddXmlAttrIfNonEmpty(group_elem, QN_SEMANTICS, group->semantics); | |
284 WriteSsrcs(group->ssrcs, group_elem); | |
285 | |
286 parent_elem->AddElement(group_elem); | |
287 } | |
288 } | |
289 | |
290 void WriteJingleStream(const StreamParams& stream, | |
291 buzz::XmlElement* parent_elem) { | |
292 buzz::XmlElement* stream_elem = | |
293 new buzz::XmlElement(QN_JINGLE_DRAFT_STREAM, false); | |
294 // We treat the nick as a stream groupid. | |
295 AddXmlAttrIfNonEmpty(stream_elem, QN_NICK, stream.groupid); | |
296 AddXmlAttrIfNonEmpty(stream_elem, QN_NAME, stream.id); | |
297 AddXmlAttrIfNonEmpty(stream_elem, QN_TYPE, stream.type); | |
298 AddXmlAttrIfNonEmpty(stream_elem, QN_DISPLAY, stream.display); | |
299 AddXmlAttrIfNonEmpty(stream_elem, QN_CNAME, stream.cname); | |
300 WriteSsrcs(stream.ssrcs, stream_elem); | |
301 WriteSsrcGroups(stream.ssrc_groups, stream_elem); | |
302 | |
303 parent_elem->AddElement(stream_elem); | |
304 } | |
305 | |
306 void WriteJingleStreams(const std::vector<StreamParams>& streams, | |
307 buzz::XmlElement* parent_elem) { | |
308 buzz::XmlElement* streams_elem = | |
309 new buzz::XmlElement(QN_JINGLE_DRAFT_STREAMS, true); | |
310 for (std::vector<StreamParams>::const_iterator stream = streams.begin(); | |
311 stream != streams.end(); ++stream) { | |
312 WriteJingleStream(*stream, streams_elem); | |
313 } | |
314 | |
315 parent_elem->AddElement(streams_elem); | |
316 } | |
317 | |
318 void WriteJingleRtpHeaderExtensions( | |
319 const std::vector<RtpHeaderExtension>& hdrexts, | |
320 buzz::XmlElement* parent_elem) { | |
321 for (std::vector<RtpHeaderExtension>::const_iterator hdrext = hdrexts.begin(); | |
322 hdrext != hdrexts.end(); ++hdrext) { | |
323 buzz::XmlElement* hdrext_elem = | |
324 new buzz::XmlElement(QN_JINGLE_RTP_HDREXT, false); | |
325 AddXmlAttr(hdrext_elem, QN_URI, hdrext->uri); | |
326 AddXmlAttr(hdrext_elem, QN_ID, hdrext->id); | |
327 parent_elem->AddElement(hdrext_elem); | |
328 } | |
329 } | |
330 | |
331 | |
332 } // namespace cricket | |
OLD | NEW |