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"
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
)) {
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
),
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
;
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_
)) {
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) {
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();
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();
127 scoped_ptr
<SessionConfig
> SessionConfig::WithLegacyIceForTest() {
128 scoped_ptr
<SessionConfig
> result
= ForTest();
129 result
->standard_ice_
= false;
130 return result
.Pass();
133 SessionConfig::SessionConfig() {
136 CandidateSessionConfig::CandidateSessionConfig() { }
138 CandidateSessionConfig::CandidateSessionConfig(
139 const CandidateSessionConfig
& config
) = default;
141 CandidateSessionConfig::~CandidateSessionConfig() { }
143 bool CandidateSessionConfig::IsSupported(
144 const SessionConfig
& config
) const {
146 IsChannelConfigSupported(control_configs_
, config
.control_config()) &&
147 IsChannelConfigSupported(event_configs_
, config
.event_config()) &&
148 IsChannelConfigSupported(video_configs_
, config
.video_config()) &&
149 IsChannelConfigSupported(audio_configs_
, config
.audio_config());
152 scoped_ptr
<CandidateSessionConfig
> CandidateSessionConfig::Clone() const {
153 return make_scoped_ptr(new CandidateSessionConfig(*this));
157 scoped_ptr
<CandidateSessionConfig
> CandidateSessionConfig::CreateEmpty() {
158 return make_scoped_ptr(new CandidateSessionConfig());
162 scoped_ptr
<CandidateSessionConfig
> CandidateSessionConfig::CreateFrom(
163 const SessionConfig
& config
) {
164 scoped_ptr
<CandidateSessionConfig
> result
= CreateEmpty();
165 result
->set_standard_ice(config
.standard_ice());
166 result
->mutable_control_configs()->push_back(config
.control_config());
167 result
->mutable_event_configs()->push_back(config
.event_config());
168 result
->mutable_video_configs()->push_back(config
.video_config());
169 result
->mutable_audio_configs()->push_back(config
.audio_config());
170 return result
.Pass();
174 scoped_ptr
<CandidateSessionConfig
> CandidateSessionConfig::CreateDefault() {
175 scoped_ptr
<CandidateSessionConfig
> result
= CreateEmpty();
177 result
->set_standard_ice(true);
180 result
->mutable_control_configs()->push_back(
181 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM
,
182 kControlStreamVersion
,
183 ChannelConfig::CODEC_UNDEFINED
));
186 result
->mutable_event_configs()->push_back(
187 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM
,
188 kDefaultStreamVersion
,
189 ChannelConfig::CODEC_UNDEFINED
));
192 result
->mutable_video_configs()->push_back(
193 ChannelConfig(ChannelConfig::TRANSPORT_STREAM
,
194 kDefaultStreamVersion
,
195 ChannelConfig::CODEC_VP8
));
198 result
->mutable_audio_configs()->push_back(
199 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM
,
200 kDefaultStreamVersion
,
201 ChannelConfig::CODEC_OPUS
));
202 result
->mutable_audio_configs()->push_back(ChannelConfig::None());
204 return result
.Pass();
207 void CandidateSessionConfig::DisableAudioChannel() {
208 mutable_audio_configs()->clear();
209 mutable_audio_configs()->push_back(ChannelConfig());
212 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec
) {
213 mutable_video_configs()->push_front(
214 ChannelConfig(ChannelConfig::TRANSPORT_STREAM
,
215 kDefaultStreamVersion
,
219 } // namespace protocol
220 } // namespace remoting