[Smart Lock] Update some more UI strings.
[chromium-blink-merge.git] / remoting / protocol / session_config.cc
blob460da2807bfa7bd13b1cbdbca1eac2b837b08e66
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 const int kDefaultStreamVersion = 2;
13 const int kControlStreamVersion = 3;
15 ChannelConfig ChannelConfig::None() {
16 return ChannelConfig();
19 ChannelConfig::ChannelConfig()
20 : transport(TRANSPORT_NONE),
21 version(0),
22 codec(CODEC_UNDEFINED) {
25 ChannelConfig::ChannelConfig(TransportType transport, int version, Codec codec)
26 : transport(transport),
27 version(version),
28 codec(codec) {
31 bool ChannelConfig::operator==(const ChannelConfig& b) const {
32 // If the transport field is set to NONE then all other fields are irrelevant.
33 if (transport == ChannelConfig::TRANSPORT_NONE)
34 return transport == b.transport;
35 return transport == b.transport && version == b.version && codec == b.codec;
38 SessionConfig::SessionConfig() {
41 // static
42 SessionConfig SessionConfig::ForTest() {
43 SessionConfig result;
44 result.set_control_config(ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
45 kControlStreamVersion,
46 ChannelConfig::CODEC_UNDEFINED));
47 result.set_event_config(ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
48 kDefaultStreamVersion,
49 ChannelConfig::CODEC_UNDEFINED));
50 result.set_video_config(ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
51 kDefaultStreamVersion,
52 ChannelConfig::CODEC_VP8));
53 result.set_audio_config(ChannelConfig(ChannelConfig::TRANSPORT_NONE,
54 kDefaultStreamVersion,
55 ChannelConfig::CODEC_UNDEFINED));
56 return result;
59 CandidateSessionConfig::CandidateSessionConfig() { }
61 CandidateSessionConfig::CandidateSessionConfig(
62 const CandidateSessionConfig& config)
63 : control_configs_(config.control_configs_),
64 event_configs_(config.event_configs_),
65 video_configs_(config.video_configs_),
66 audio_configs_(config.audio_configs_) {
69 CandidateSessionConfig::~CandidateSessionConfig() { }
71 bool CandidateSessionConfig::Select(
72 const CandidateSessionConfig* client_config,
73 SessionConfig* result) {
74 ChannelConfig control_config;
75 ChannelConfig event_config;
76 ChannelConfig video_config;
77 ChannelConfig audio_config;
79 if (!SelectCommonChannelConfig(
80 control_configs_, client_config->control_configs_, &control_config) ||
81 !SelectCommonChannelConfig(
82 event_configs_, client_config->event_configs_, &event_config) ||
83 !SelectCommonChannelConfig(
84 video_configs_, client_config->video_configs_, &video_config) ||
85 !SelectCommonChannelConfig(
86 audio_configs_, client_config->audio_configs_, &audio_config)) {
87 return false;
90 result->set_control_config(control_config);
91 result->set_event_config(event_config);
92 result->set_video_config(video_config);
93 result->set_audio_config(audio_config);
95 return true;
98 bool CandidateSessionConfig::IsSupported(
99 const SessionConfig& config) const {
100 return
101 IsChannelConfigSupported(control_configs_, config.control_config()) &&
102 IsChannelConfigSupported(event_configs_, config.event_config()) &&
103 IsChannelConfigSupported(video_configs_, config.video_config()) &&
104 IsChannelConfigSupported(audio_configs_, config.audio_config());
107 bool CandidateSessionConfig::GetFinalConfig(SessionConfig* result) const {
108 if (control_configs_.size() != 1 ||
109 event_configs_.size() != 1 ||
110 video_configs_.size() != 1 ||
111 audio_configs_.size() != 1) {
112 return false;
115 result->set_control_config(control_configs_.front());
116 result->set_event_config(event_configs_.front());
117 result->set_video_config(video_configs_.front());
118 result->set_audio_config(audio_configs_.front());
120 return true;
123 // static
124 bool CandidateSessionConfig::SelectCommonChannelConfig(
125 const std::list<ChannelConfig>& host_configs,
126 const std::list<ChannelConfig>& client_configs,
127 ChannelConfig* config) {
128 // Usually each of these vectors will contain just several elements,
129 // so iterating over all of them is not a problem.
130 std::list<ChannelConfig>::const_iterator it;
131 for (it = client_configs.begin(); it != client_configs.end(); ++it) {
132 if (IsChannelConfigSupported(host_configs, *it)) {
133 *config = *it;
134 return true;
137 return false;
140 // static
141 bool CandidateSessionConfig::IsChannelConfigSupported(
142 const std::list<ChannelConfig>& vector,
143 const ChannelConfig& value) {
144 return std::find(vector.begin(), vector.end(), value) != vector.end();
147 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::Clone() const {
148 return make_scoped_ptr(new CandidateSessionConfig(*this));
151 // static
152 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() {
153 return make_scoped_ptr(new CandidateSessionConfig());
156 // static
157 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateFrom(
158 const SessionConfig& config) {
159 scoped_ptr<CandidateSessionConfig> result = CreateEmpty();
160 result->mutable_control_configs()->push_back(config.control_config());
161 result->mutable_event_configs()->push_back(config.event_config());
162 result->mutable_video_configs()->push_back(config.video_config());
163 result->mutable_audio_configs()->push_back(config.audio_config());
164 return result.Pass();
167 // static
168 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateDefault() {
169 scoped_ptr<CandidateSessionConfig> result = CreateEmpty();
171 // Control channel.
172 result->mutable_control_configs()->push_back(
173 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
174 kControlStreamVersion,
175 ChannelConfig::CODEC_UNDEFINED));
177 // Event channel.
178 result->mutable_event_configs()->push_back(
179 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
180 kDefaultStreamVersion,
181 ChannelConfig::CODEC_UNDEFINED));
183 // Video channel.
184 result->mutable_video_configs()->push_back(
185 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
186 kDefaultStreamVersion,
187 ChannelConfig::CODEC_VP8));
189 // Audio channel.
190 result->mutable_audio_configs()->push_back(
191 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
192 kDefaultStreamVersion,
193 ChannelConfig::CODEC_OPUS));
194 result->mutable_audio_configs()->push_back(ChannelConfig::None());
196 return result.Pass();
199 void CandidateSessionConfig::DisableAudioChannel() {
200 mutable_audio_configs()->clear();
201 mutable_audio_configs()->push_back(ChannelConfig());
204 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec) {
205 mutable_video_configs()->push_front(
206 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
207 kDefaultStreamVersion,
208 codec));
211 } // namespace protocol
212 } // namespace remoting