1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/session_config.h"
#include <vector>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
namespace remoting::protocol {
namespace {
bool SelectCommonChannelConfig(const std::list<ChannelConfig>& host_configs,
const std::list<ChannelConfig>& client_configs,
ChannelConfig* config) {
// Usually each of these lists will contain just a few elements, so iterating
// over all of them is not a problem.
std::list<ChannelConfig>::const_iterator it;
for (it = client_configs.begin(); it != client_configs.end(); ++it) {
if (base::Contains(host_configs, *it)) {
*config = *it;
return true;
}
}
return false;
}
void UpdateConfigListToPreferTransport(std::list<ChannelConfig>* configs,
ChannelConfig::TransportType transport) {
std::vector<ChannelConfig> sorted(configs->begin(), configs->end());
std::stable_sort(sorted.begin(), sorted.end(),
[transport](const ChannelConfig& a, const ChannelConfig& b) {
// |a| must precede |b| if |a| uses preferred transport and
// |b| doesn't.
return a.transport == transport &&
b.transport != transport;
});
configs->assign(sorted.begin(), sorted.end());
}
} // namespace
const int kDefaultStreamVersion = 2;
const int kControlStreamVersion = 3;
ChannelConfig ChannelConfig::None() {
return ChannelConfig();
}
ChannelConfig::ChannelConfig(TransportType transport, int version, Codec codec)
: transport(transport), version(version), codec(codec) {}
bool ChannelConfig::operator==(const ChannelConfig& b) const {
// If the transport field is set to NONE then all other fields are irrelevant.
if (transport == ChannelConfig::TRANSPORT_NONE) {
return transport == b.transport;
}
return transport == b.transport && version == b.version && codec == b.codec;
}
// static
std::unique_ptr<SessionConfig> SessionConfig::SelectCommon(
const CandidateSessionConfig* client_config,
const CandidateSessionConfig* host_config) {
// Use WebRTC if both host and client support it.
if (client_config->webrtc_supported() && host_config->webrtc_supported()) {
return base::WrapUnique(new SessionConfig(Protocol::WEBRTC));
}
// Reject connection if ICE is not supported by either of the peers.
if (!host_config->ice_supported() || !client_config->ice_supported()) {
return nullptr;
}
std::unique_ptr<SessionConfig> result(new SessionConfig(Protocol::ICE));
std::list<ChannelConfig> host_video_configs = host_config->video_configs();
host_video_configs.remove_if([](const ChannelConfig& config) {
// Older ICE-based clients do not support VP9, H.264, or AV1 so remove them.
return config.codec == ChannelConfig::CODEC_H264 ||
config.codec == ChannelConfig::CODEC_VP9 ||
config.codec == ChannelConfig::CODEC_AV1;
});
if (!SelectCommonChannelConfig(host_config->control_configs(),
client_config->control_configs(),
&result->control_config_) ||
!SelectCommonChannelConfig(host_config->event_configs(),
client_config->event_configs(),
&result->event_config_) ||
!SelectCommonChannelConfig(host_video_configs,
client_config->video_configs(),
&result->video_config_) ||
!SelectCommonChannelConfig(host_config->audio_configs(),
client_config->audio_configs(),
&result->audio_config_)) {
return nullptr;
}
return result;
}
// static
std::unique_ptr<SessionConfig> SessionConfig::GetFinalConfig(
const CandidateSessionConfig* candidate_config) {
if (candidate_config->webrtc_supported()) {
if (candidate_config->ice_supported()) {
LOG(ERROR) << "Received candidate config is ambiguous.";
return nullptr;
}
return base::WrapUnique(new SessionConfig(Protocol::WEBRTC));
}
if (!candidate_config->ice_supported()) {
return nullptr;
}
if (candidate_config->control_configs().size() != 1 ||
candidate_config->event_configs().size() != 1 ||
candidate_config->video_configs().size() != 1 ||
candidate_config->audio_configs().size() != 1) {
return nullptr;
}
std::unique_ptr<SessionConfig> result(new SessionConfig(Protocol::ICE));
result->control_config_ = candidate_config->control_configs().front();
result->event_config_ = candidate_config->event_configs().front();
result->video_config_ = candidate_config->video_configs().front();
result->audio_config_ = candidate_config->audio_configs().front();
return result;
}
// static
std::unique_ptr<SessionConfig> SessionConfig::ForTest() {
std::unique_ptr<SessionConfig> result(new SessionConfig(Protocol::ICE));
result->control_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kControlStreamVersion,
ChannelConfig::CODEC_UNDEFINED);
result->event_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_UNDEFINED);
result->video_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_VP8);
result->audio_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_OPUS);
return result;
}
std::unique_ptr<SessionConfig> SessionConfig::ForTestWithAudio() {
std::unique_ptr<SessionConfig> result(ForTest());
result->audio_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_OPUS);
return result;
}
std::unique_ptr<SessionConfig> SessionConfig::ForTestWithVerbatimVideo() {
std::unique_ptr<SessionConfig> result = ForTest();
result->video_config_ =
ChannelConfig(ChannelConfig::TRANSPORT_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_VERBATIM);
return result;
}
std::unique_ptr<SessionConfig> SessionConfig::ForTestWithWebrtc() {
return base::WrapUnique(new SessionConfig(Protocol::WEBRTC));
}
const ChannelConfig& SessionConfig::control_config() const {
DCHECK(protocol_ == Protocol::ICE);
return control_config_;
}
const ChannelConfig& SessionConfig::event_config() const {
DCHECK(protocol_ == Protocol::ICE);
return event_config_;
}
const ChannelConfig& SessionConfig::video_config() const {
DCHECK(protocol_ == Protocol::ICE);
return video_config_;
}
const ChannelConfig& SessionConfig::audio_config() const {
DCHECK(protocol_ == Protocol::ICE);
return audio_config_;
}
SessionConfig::SessionConfig(Protocol protocol) : protocol_(protocol) {}
CandidateSessionConfig::CandidateSessionConfig() = default;
CandidateSessionConfig::CandidateSessionConfig(
const CandidateSessionConfig& config) = default;
CandidateSessionConfig::~CandidateSessionConfig() = default;
bool CandidateSessionConfig::IsSupported(const SessionConfig& config) const {
switch (config.protocol()) {
case SessionConfig::Protocol::ICE:
return ice_supported() &&
base::Contains(control_configs_, config.control_config()) &&
base::Contains(event_configs_, config.event_config()) &&
base::Contains(video_configs_, config.video_config()) &&
base::Contains(audio_configs_, config.audio_config());
case SessionConfig::Protocol::WEBRTC:
return webrtc_supported();
}
NOTREACHED();
}
std::unique_ptr<CandidateSessionConfig> CandidateSessionConfig::Clone() const {
return base::WrapUnique(new CandidateSessionConfig(*this));
}
// static
std::unique_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() {
return base::WrapUnique(new CandidateSessionConfig());
}
// static
std::unique_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateFrom(
const SessionConfig& config) {
std::unique_ptr<CandidateSessionConfig> result = CreateEmpty();
switch (config.protocol()) {
case SessionConfig::Protocol::WEBRTC:
result->set_webrtc_supported(true);
result->set_ice_supported(false);
break;
case SessionConfig::Protocol::ICE:
result->set_webrtc_supported(false);
result->set_ice_supported(true);
result->mutable_control_configs()->push_back(config.control_config());
result->mutable_event_configs()->push_back(config.event_config());
result->mutable_video_configs()->push_back(config.video_config());
result->mutable_audio_configs()->push_back(config.audio_config());
break;
}
return result;
}
// static
std::unique_ptr<CandidateSessionConfig>
CandidateSessionConfig::CreateDefault() {
std::unique_ptr<CandidateSessionConfig> result = CreateEmpty();
result->set_ice_supported(true);
// Control channel.
result->mutable_control_configs()->push_back(
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kControlStreamVersion,
ChannelConfig::CODEC_UNDEFINED));
// Event channel.
result->mutable_event_configs()->push_back(
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_UNDEFINED));
// Video channel.
result->mutable_video_configs()->push_back(
ChannelConfig(ChannelConfig::TRANSPORT_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_VP9));
result->mutable_video_configs()->push_back(
ChannelConfig(ChannelConfig::TRANSPORT_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_VP8));
// Audio channel.
result->mutable_audio_configs()->push_back(
ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, kDefaultStreamVersion,
ChannelConfig::CODEC_OPUS));
result->mutable_audio_configs()->push_back(ChannelConfig::None());
return result;
}
void CandidateSessionConfig::DisableAudioChannel() {
mutable_audio_configs()->clear();
mutable_audio_configs()->push_back(ChannelConfig());
}
void CandidateSessionConfig::PreferTransport(
ChannelConfig::TransportType transport) {
UpdateConfigListToPreferTransport(&control_configs_, transport);
UpdateConfigListToPreferTransport(&event_configs_, transport);
UpdateConfigListToPreferTransport(&video_configs_, transport);
UpdateConfigListToPreferTransport(&audio_configs_, transport);
}
} // namespace remoting::protocol
|