win: wstring->string16 in chrome/installer/util/html_dialog
[chromium-blink-merge.git] / remoting / protocol / session_config.cc
blobf6163f4fa8df622d07e92dfa5ce85620ec484416
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/session_config.h"
7 #include <algorithm>
9 namespace remoting {
10 namespace protocol {
12 namespace {
14 bool IsChannelConfigSupported(const std::list<ChannelConfig>& list,
15 const ChannelConfig& value) {
16 return std::find(list.begin(), list.end(), value) != list.end();
19 bool SelectCommonChannelConfig(const std::list<ChannelConfig>& host_configs,
20 const std::list<ChannelConfig>& client_configs,
21 ChannelConfig* config) {
22 // Usually each of these lists will contain just a few elements, so iterating
23 // over all of them is not a problem.
24 std::list<ChannelConfig>::const_iterator it;
25 for (it = client_configs.begin(); it != client_configs.end(); ++it) {
26 if (IsChannelConfigSupported(host_configs, *it)) {
27 *config = *it;
28 return true;
31 return false;
34 } // namespace
36 const int kDefaultStreamVersion = 2;
37 const int kControlStreamVersion = 3;
39 ChannelConfig ChannelConfig::None() {
40 return ChannelConfig();
43 ChannelConfig::ChannelConfig(TransportType transport, int version, Codec codec)
44 : transport(transport),
45 version(version),
46 codec(codec) {
49 bool ChannelConfig::operator==(const ChannelConfig& b) const {
50 // If the transport field is set to NONE then all other fields are irrelevant.
51 if (transport == ChannelConfig::TRANSPORT_NONE)
52 return transport == b.transport;
53 return transport == b.transport && version == b.version && codec == b.codec;
56 // static
57 scoped_ptr<SessionConfig> SessionConfig::SelectCommon(
58 const CandidateSessionConfig* client_config,
59 const CandidateSessionConfig* host_config) {
60 scoped_ptr<SessionConfig> result(new SessionConfig());
61 ChannelConfig control_config;
62 ChannelConfig event_config;
63 ChannelConfig video_config;
64 ChannelConfig audio_config;
66 result->standard_ice_ =
67 host_config->standard_ice() && client_config->standard_ice();
69 if (!SelectCommonChannelConfig(host_config->control_configs(),
70 client_config->control_configs(),
71 &result->control_config_) ||
72 !SelectCommonChannelConfig(host_config->event_configs(),
73 client_config->event_configs(),
74 &result->event_config_) ||
75 !SelectCommonChannelConfig(host_config->video_configs(),
76 client_config->video_configs(),
77 &result->video_config_) ||
78 !SelectCommonChannelConfig(host_config->audio_configs(),
79 client_config->audio_configs(),
80 &result->audio_config_)) {
81 return nullptr;
84 return result;
87 // static
88 scoped_ptr<SessionConfig> SessionConfig::GetFinalConfig(
89 const CandidateSessionConfig* candidate_config) {
90 if (candidate_config->control_configs().size() != 1 ||
91 candidate_config->event_configs().size() != 1 ||
92 candidate_config->video_configs().size() != 1 ||
93 candidate_config->audio_configs().size() != 1) {
94 return nullptr;
97 scoped_ptr<SessionConfig> result(new SessionConfig());
98 result->standard_ice_ = candidate_config->standard_ice();
99 result->control_config_ = candidate_config->control_configs().front();
100 result->event_config_ = candidate_config->event_configs().front();
101 result->video_config_ = candidate_config->video_configs().front();
102 result->audio_config_ = candidate_config->audio_configs().front();
104 return result.Pass();
107 // static
108 scoped_ptr<SessionConfig> SessionConfig::ForTest() {
109 scoped_ptr<SessionConfig> result(new SessionConfig());
110 result->standard_ice_ = true;
111 result->control_config_ = ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
112 kControlStreamVersion,
113 ChannelConfig::CODEC_UNDEFINED);
114 result->event_config_ = ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
115 kDefaultStreamVersion,
116 ChannelConfig::CODEC_UNDEFINED);
117 result->video_config_ = ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
118 kDefaultStreamVersion,
119 ChannelConfig::CODEC_VP8);
120 result->audio_config_ = ChannelConfig(ChannelConfig::TRANSPORT_NONE,
121 kDefaultStreamVersion,
122 ChannelConfig::CODEC_UNDEFINED);
123 return result.Pass();
126 // static
127 scoped_ptr<SessionConfig> SessionConfig::WithLegacyIceForTest() {
128 scoped_ptr<SessionConfig> result = ForTest();
129 result->standard_ice_ = false;
130 return result.Pass();
133 SessionConfig::SessionConfig() {
137 CandidateSessionConfig::CandidateSessionConfig() { }
139 CandidateSessionConfig::CandidateSessionConfig(
140 const CandidateSessionConfig& config)
141 : standard_ice_(true),
142 control_configs_(config.control_configs_),
143 event_configs_(config.event_configs_),
144 video_configs_(config.video_configs_),
145 audio_configs_(config.audio_configs_) {
148 CandidateSessionConfig::~CandidateSessionConfig() { }
150 bool CandidateSessionConfig::IsSupported(
151 const SessionConfig& config) const {
152 return
153 IsChannelConfigSupported(control_configs_, config.control_config()) &&
154 IsChannelConfigSupported(event_configs_, config.event_config()) &&
155 IsChannelConfigSupported(video_configs_, config.video_config()) &&
156 IsChannelConfigSupported(audio_configs_, config.audio_config());
159 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::Clone() const {
160 return make_scoped_ptr(new CandidateSessionConfig(*this));
163 // static
164 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() {
165 return make_scoped_ptr(new CandidateSessionConfig());
168 // static
169 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateFrom(
170 const SessionConfig& config) {
171 scoped_ptr<CandidateSessionConfig> result = CreateEmpty();
172 result->set_standard_ice(config.standard_ice());
173 result->mutable_control_configs()->push_back(config.control_config());
174 result->mutable_event_configs()->push_back(config.event_config());
175 result->mutable_video_configs()->push_back(config.video_config());
176 result->mutable_audio_configs()->push_back(config.audio_config());
177 return result.Pass();
180 // static
181 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateDefault() {
182 scoped_ptr<CandidateSessionConfig> result = CreateEmpty();
184 result->set_standard_ice(true);
186 // Control channel.
187 result->mutable_control_configs()->push_back(
188 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
189 kControlStreamVersion,
190 ChannelConfig::CODEC_UNDEFINED));
192 // Event channel.
193 result->mutable_event_configs()->push_back(
194 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
195 kDefaultStreamVersion,
196 ChannelConfig::CODEC_UNDEFINED));
198 // Video channel.
199 result->mutable_video_configs()->push_back(
200 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
201 kDefaultStreamVersion,
202 ChannelConfig::CODEC_VP8));
204 // Audio channel.
205 result->mutable_audio_configs()->push_back(
206 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
207 kDefaultStreamVersion,
208 ChannelConfig::CODEC_OPUS));
209 result->mutable_audio_configs()->push_back(ChannelConfig::None());
211 return result.Pass();
214 void CandidateSessionConfig::DisableAudioChannel() {
215 mutable_audio_configs()->clear();
216 mutable_audio_configs()->push_back(ChannelConfig());
219 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec) {
220 mutable_video_configs()->push_front(
221 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
222 kDefaultStreamVersion,
223 codec));
226 } // namespace protocol
227 } // namespace remoting