1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/protocol/content_description.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "remoting/base/constants.h"
10 #include "remoting/protocol/authenticator.h"
11 #include "remoting/protocol/name_value_map.h"
12 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
15 using buzz::XmlElement
;
20 const char ContentDescription::kChromotingContentName
[] = "chromoting";
24 const char kDefaultNs
[] = "";
26 // Following constants are used to format session description in XML.
27 const char kDescriptionTag
[] = "description";
28 const char kStandardIceTag
[] = "standard-ice";
29 const char kControlTag
[] = "control";
30 const char kEventTag
[] = "event";
31 const char kVideoTag
[] = "video";
32 const char kAudioTag
[] = "audio";
33 const char kDeprecatedResolutionTag
[] = "initial-resolution";
35 const char kTransportAttr
[] = "transport";
36 const char kVersionAttr
[] = "version";
37 const char kCodecAttr
[] = "codec";
38 const char kDeprecatedWidthAttr
[] = "width";
39 const char kDeprecatedHeightAttr
[] = "height";
41 const NameMapElement
<ChannelConfig::TransportType
> kTransports
[] = {
42 { ChannelConfig::TRANSPORT_STREAM
, "stream" },
43 { ChannelConfig::TRANSPORT_MUX_STREAM
, "mux-stream" },
44 { ChannelConfig::TRANSPORT_DATAGRAM
, "datagram" },
45 { ChannelConfig::TRANSPORT_NONE
, "none" },
48 const NameMapElement
<ChannelConfig::Codec
> kCodecs
[] = {
49 { ChannelConfig::CODEC_VERBATIM
, "verbatim" },
50 { ChannelConfig::CODEC_VP8
, "vp8" },
51 { ChannelConfig::CODEC_VP9
, "vp9" },
52 { ChannelConfig::CODEC_ZIP
, "zip" },
53 { ChannelConfig::CODEC_OPUS
, "opus" },
54 { ChannelConfig::CODEC_SPEEX
, "speex" },
57 // Format a channel configuration tag for chromotocol session description,
58 // e.g. for video channel:
59 // <video transport="stream" version="1" codec="vp8" />
60 XmlElement
* FormatChannelConfig(const ChannelConfig
& config
,
61 const std::string
& tag_name
) {
62 XmlElement
* result
= new XmlElement(
63 QName(kChromotingXmlNamespace
, tag_name
));
65 result
->AddAttr(QName(kDefaultNs
, kTransportAttr
),
66 ValueToName(kTransports
, config
.transport
));
68 if (config
.transport
!= ChannelConfig::TRANSPORT_NONE
) {
69 result
->AddAttr(QName(kDefaultNs
, kVersionAttr
),
70 base::IntToString(config
.version
));
72 if (config
.codec
!= ChannelConfig::CODEC_UNDEFINED
) {
73 result
->AddAttr(QName(kDefaultNs
, kCodecAttr
),
74 ValueToName(kCodecs
, config
.codec
));
81 // Returns false if the element is invalid.
82 bool ParseChannelConfig(const XmlElement
* element
, bool codec_required
,
83 ChannelConfig
* config
) {
85 kTransports
, element
->Attr(QName(kDefaultNs
, kTransportAttr
)),
86 &config
->transport
)) {
90 // Version is not required when transport="none".
91 if (config
->transport
!= ChannelConfig::TRANSPORT_NONE
) {
92 if (!base::StringToInt(element
->Attr(QName(kDefaultNs
, kVersionAttr
)),
97 // Codec is not required when transport="none".
99 if (!NameToValue(kCodecs
, element
->Attr(QName(kDefaultNs
, kCodecAttr
)),
104 config
->codec
= ChannelConfig::CODEC_UNDEFINED
;
108 config
->codec
= ChannelConfig::CODEC_UNDEFINED
;
116 ContentDescription::ContentDescription(
117 scoped_ptr
<CandidateSessionConfig
> config
,
118 scoped_ptr
<buzz::XmlElement
> authenticator_message
)
119 : candidate_config_(config
.Pass()),
120 authenticator_message_(authenticator_message
.Pass()) {
123 ContentDescription::~ContentDescription() { }
125 // ToXml() creates content description for chromoting session. The
126 // description looks as follows:
127 // <description xmlns="google:remoting">
129 // <control transport="stream" version="1" />
130 // <event transport="datagram" version="1" />
131 // <video transport="stream" codec="vp8" version="1" />
132 // <audio transport="stream" codec="opus" version="1" />
134 // Message created by Authenticator implementation.
138 XmlElement
* ContentDescription::ToXml() const {
139 XmlElement
* root
= new XmlElement(
140 QName(kChromotingXmlNamespace
, kDescriptionTag
), true);
142 if (config()->standard_ice()) {
144 new buzz::XmlElement(QName(kChromotingXmlNamespace
, kStandardIceTag
)));
147 for (const ChannelConfig
& channel_config
: config()->control_configs()) {
148 root
->AddElement(FormatChannelConfig(channel_config
, kControlTag
));
151 for (const ChannelConfig
& channel_config
: config()->event_configs()) {
152 root
->AddElement(FormatChannelConfig(channel_config
, kEventTag
));
155 for (const ChannelConfig
& channel_config
: config()->video_configs()) {
156 root
->AddElement(FormatChannelConfig(channel_config
, kVideoTag
));
159 for (const ChannelConfig
& channel_config
: config()->audio_configs()) {
160 root
->AddElement(FormatChannelConfig(channel_config
, kAudioTag
));
163 // Older endpoints require an initial-resolution tag, but otherwise ignore it.
164 XmlElement
* resolution_tag
= new XmlElement(
165 QName(kChromotingXmlNamespace
, kDeprecatedResolutionTag
));
166 resolution_tag
->AddAttr(QName(kDefaultNs
, kDeprecatedWidthAttr
), "640");
167 resolution_tag
->AddAttr(QName(kDefaultNs
, kDeprecatedHeightAttr
), "480");
168 root
->AddElement(resolution_tag
);
170 if (authenticator_message_
.get()) {
171 DCHECK(Authenticator::IsAuthenticatorMessage(authenticator_message_
.get()));
172 root
->AddElement(new XmlElement(*authenticator_message_
));
179 // Adds the channel configs corresponding to |tag_name|,
180 // found in |element|, to |configs|.
181 bool ContentDescription::ParseChannelConfigs(
182 const XmlElement
* const element
,
183 const char tag_name
[],
186 std::list
<ChannelConfig
>* const configs
) {
187 QName
tag(kChromotingXmlNamespace
, tag_name
);
188 const XmlElement
* child
= element
->FirstNamed(tag
);
190 ChannelConfig channel_config
;
191 if (ParseChannelConfig(child
, codec_required
, &channel_config
)) {
192 configs
->push_back(channel_config
);
194 child
= child
->NextNamed(tag
);
196 if (optional
&& configs
->empty()) {
197 // If there's no mention of the tag, implicitly assume disabled channel.
198 configs
->push_back(ChannelConfig::None());
204 scoped_ptr
<ContentDescription
> ContentDescription::ParseXml(
205 const XmlElement
* element
) {
206 if (element
->Name() != QName(kChromotingXmlNamespace
, kDescriptionTag
)) {
207 LOG(ERROR
) << "Invalid description: " << element
->Str();
210 scoped_ptr
<CandidateSessionConfig
> config(
211 CandidateSessionConfig::CreateEmpty());
213 config
->set_standard_ice(
214 element
->FirstNamed(QName(kChromotingXmlNamespace
, kStandardIceTag
)) !=
217 if (!ParseChannelConfigs(element
, kControlTag
, false, false,
218 config
->mutable_control_configs()) ||
219 !ParseChannelConfigs(element
, kEventTag
, false, false,
220 config
->mutable_event_configs()) ||
221 !ParseChannelConfigs(element
, kVideoTag
, true, false,
222 config
->mutable_video_configs()) ||
223 !ParseChannelConfigs(element
, kAudioTag
, true, true,
224 config
->mutable_audio_configs())) {
228 scoped_ptr
<XmlElement
> authenticator_message
;
229 const XmlElement
* child
= Authenticator::FindAuthenticatorMessage(element
);
231 authenticator_message
.reset(new XmlElement(*child
));
233 return make_scoped_ptr(
234 new ContentDescription(config
.Pass(), authenticator_message
.Pass()));
237 } // namespace protocol
238 } // namespace remoting