Roll src/third_party/WebKit 2dc4bc6:34469ea (svn 182261:182277)
[chromium-blink-merge.git] / remoting / protocol / session_config.cc
blob06936276d0b015e7102bd69b2fbb826e841a85b3
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 scoped_ptr<CandidateSessionConfig>(new CandidateSessionConfig(*this));
151 // static
152 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() {
153 return scoped_ptr<CandidateSessionConfig>(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 #if !defined(MEDIA_DISABLE_LIBVPX)
185 result->mutable_video_configs()->push_back(
186 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
187 kDefaultStreamVersion,
188 ChannelConfig::CODEC_VP8));
189 #endif // !defined(MEDIA_DISABLE_LIBVPX)
191 // Audio channel.
192 result->mutable_audio_configs()->push_back(
193 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
194 kDefaultStreamVersion,
195 ChannelConfig::CODEC_OPUS));
196 result->mutable_audio_configs()->push_back(ChannelConfig::None());
198 return result.Pass();
201 void CandidateSessionConfig::DisableAudioChannel() {
202 mutable_audio_configs()->clear();
203 mutable_audio_configs()->push_back(ChannelConfig());
206 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec) {
207 mutable_video_configs()->push_front(
208 ChannelConfig(ChannelConfig::TRANSPORT_STREAM,
209 kDefaultStreamVersion,
210 codec));
213 } // namespace protocol
214 } // namespace remoting