| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/session_config.h" | 5 #include "remoting/protocol/session_config.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 namespace remoting { | 9 namespace remoting { |
| 10 namespace protocol { | 10 namespace protocol { |
| 11 | 11 |
| 12 namespace { | 12 const int kDefaultStreamVersion = 2; |
| 13 const int kControlStreamVersion = 3; |
| 13 | 14 |
| 14 bool IsChannelConfigSupported(const std::list<ChannelConfig>& list, | 15 ChannelConfig ChannelConfig::None() { |
| 15 const ChannelConfig& value) { | 16 return ChannelConfig(); |
| 16 return std::find(list.begin(), list.end(), value) != list.end(); | |
| 17 } | 17 } |
| 18 | 18 |
| 19 bool SelectCommonChannelConfig(const std::list<ChannelConfig>& host_configs, | 19 ChannelConfig::ChannelConfig() |
| 20 const std::list<ChannelConfig>& client_configs, | 20 : transport(TRANSPORT_NONE), |
| 21 ChannelConfig* config) { | 21 version(0), |
| 22 // Usually each of these lists will contain just a few elements, so iterating | 22 codec(CODEC_UNDEFINED) { |
| 23 // over all of them is not a problem. | 23 } |
| 24 |
| 25 ChannelConfig::ChannelConfig(TransportType transport, int version, Codec codec) |
| 26 : transport(transport), |
| 27 version(version), |
| 28 codec(codec) { |
| 29 } |
| 30 |
| 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; |
| 36 } |
| 37 |
| 38 SessionConfig::SessionConfig() { |
| 39 } |
| 40 |
| 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; |
| 57 } |
| 58 |
| 59 CandidateSessionConfig::CandidateSessionConfig() { } |
| 60 |
| 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_) { |
| 67 } |
| 68 |
| 69 CandidateSessionConfig::~CandidateSessionConfig() { } |
| 70 |
| 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; |
| 78 |
| 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; |
| 88 } |
| 89 |
| 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); |
| 94 |
| 95 return true; |
| 96 } |
| 97 |
| 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()); |
| 105 } |
| 106 |
| 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; |
| 113 } |
| 114 |
| 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()); |
| 119 |
| 120 return true; |
| 121 } |
| 122 |
| 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. |
| 24 std::list<ChannelConfig>::const_iterator it; | 130 std::list<ChannelConfig>::const_iterator it; |
| 25 for (it = client_configs.begin(); it != client_configs.end(); ++it) { | 131 for (it = client_configs.begin(); it != client_configs.end(); ++it) { |
| 26 if (IsChannelConfigSupported(host_configs, *it)) { | 132 if (IsChannelConfigSupported(host_configs, *it)) { |
| 27 *config = *it; | 133 *config = *it; |
| 28 return true; | 134 return true; |
| 29 } | 135 } |
| 30 } | 136 } |
| 31 return false; | 137 return false; |
| 32 } | 138 } |
| 33 | 139 |
| 34 } // namespace | |
| 35 | |
| 36 const int kDefaultStreamVersion = 2; | |
| 37 const int kControlStreamVersion = 3; | |
| 38 | |
| 39 ChannelConfig ChannelConfig::None() { | |
| 40 return ChannelConfig(); | |
| 41 } | |
| 42 | |
| 43 ChannelConfig::ChannelConfig(TransportType transport, int version, Codec codec) | |
| 44 : transport(transport), | |
| 45 version(version), | |
| 46 codec(codec) { | |
| 47 } | |
| 48 | |
| 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; | |
| 54 } | |
| 55 | |
| 56 // static | 140 // static |
| 57 scoped_ptr<SessionConfig> SessionConfig::SelectCommon( | 141 bool CandidateSessionConfig::IsChannelConfigSupported( |
| 58 const CandidateSessionConfig* client_config, | 142 const std::list<ChannelConfig>& vector, |
| 59 const CandidateSessionConfig* host_config) { | 143 const ChannelConfig& value) { |
| 60 scoped_ptr<SessionConfig> result(new SessionConfig()); | 144 return std::find(vector.begin(), vector.end(), value) != vector.end(); |
| 61 ChannelConfig control_config; | |
| 62 ChannelConfig event_config; | |
| 63 ChannelConfig video_config; | |
| 64 ChannelConfig audio_config; | |
| 65 | |
| 66 result->standard_ice_ = | |
| 67 host_config->standard_ice() && client_config->standard_ice(); | |
| 68 | |
| 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; | |
| 82 } | |
| 83 | |
| 84 return result; | |
| 85 } | |
| 86 | |
| 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; | |
| 95 } | |
| 96 | |
| 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(); | |
| 103 | |
| 104 return result.Pass(); | |
| 105 } | |
| 106 | |
| 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(); | |
| 124 } | |
| 125 | |
| 126 // static | |
| 127 scoped_ptr<SessionConfig> SessionConfig::WithLegacyIceForTest() { | |
| 128 scoped_ptr<SessionConfig> result = ForTest(); | |
| 129 result->standard_ice_ = false; | |
| 130 return result.Pass(); | |
| 131 } | |
| 132 | |
| 133 SessionConfig::SessionConfig() { | |
| 134 } | |
| 135 | |
| 136 | |
| 137 CandidateSessionConfig::CandidateSessionConfig() { } | |
| 138 | |
| 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_) { | |
| 146 } | |
| 147 | |
| 148 CandidateSessionConfig::~CandidateSessionConfig() { } | |
| 149 | |
| 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()); | |
| 157 } | 145 } |
| 158 | 146 |
| 159 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::Clone() const { | 147 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::Clone() const { |
| 160 return make_scoped_ptr(new CandidateSessionConfig(*this)); | 148 return make_scoped_ptr(new CandidateSessionConfig(*this)); |
| 161 } | 149 } |
| 162 | 150 |
| 163 // static | 151 // static |
| 164 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() { | 152 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateEmpty() { |
| 165 return make_scoped_ptr(new CandidateSessionConfig()); | 153 return make_scoped_ptr(new CandidateSessionConfig()); |
| 166 } | 154 } |
| 167 | 155 |
| 168 // static | 156 // static |
| 169 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateFrom( | 157 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateFrom( |
| 170 const SessionConfig& config) { | 158 const SessionConfig& config) { |
| 171 scoped_ptr<CandidateSessionConfig> result = CreateEmpty(); | 159 scoped_ptr<CandidateSessionConfig> result = CreateEmpty(); |
| 172 result->set_standard_ice(config.standard_ice()); | |
| 173 result->mutable_control_configs()->push_back(config.control_config()); | 160 result->mutable_control_configs()->push_back(config.control_config()); |
| 174 result->mutable_event_configs()->push_back(config.event_config()); | 161 result->mutable_event_configs()->push_back(config.event_config()); |
| 175 result->mutable_video_configs()->push_back(config.video_config()); | 162 result->mutable_video_configs()->push_back(config.video_config()); |
| 176 result->mutable_audio_configs()->push_back(config.audio_config()); | 163 result->mutable_audio_configs()->push_back(config.audio_config()); |
| 177 return result.Pass(); | 164 return result.Pass(); |
| 178 } | 165 } |
| 179 | 166 |
| 180 // static | 167 // static |
| 181 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateDefault() { | 168 scoped_ptr<CandidateSessionConfig> CandidateSessionConfig::CreateDefault() { |
| 182 scoped_ptr<CandidateSessionConfig> result = CreateEmpty(); | 169 scoped_ptr<CandidateSessionConfig> result = CreateEmpty(); |
| 183 | 170 |
| 184 result->set_standard_ice(true); | |
| 185 | |
| 186 // Control channel. | 171 // Control channel. |
| 187 result->mutable_control_configs()->push_back( | 172 result->mutable_control_configs()->push_back( |
| 188 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, | 173 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, |
| 189 kControlStreamVersion, | 174 kControlStreamVersion, |
| 190 ChannelConfig::CODEC_UNDEFINED)); | 175 ChannelConfig::CODEC_UNDEFINED)); |
| 191 | 176 |
| 192 // Event channel. | 177 // Event channel. |
| 193 result->mutable_event_configs()->push_back( | 178 result->mutable_event_configs()->push_back( |
| 194 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, | 179 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, |
| 195 kDefaultStreamVersion, | 180 kDefaultStreamVersion, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 218 | 203 |
| 219 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec) { | 204 void CandidateSessionConfig::EnableVideoCodec(ChannelConfig::Codec codec) { |
| 220 mutable_video_configs()->push_front( | 205 mutable_video_configs()->push_front( |
| 221 ChannelConfig(ChannelConfig::TRANSPORT_STREAM, | 206 ChannelConfig(ChannelConfig::TRANSPORT_STREAM, |
| 222 kDefaultStreamVersion, | 207 kDefaultStreamVersion, |
| 223 codec)); | 208 codec)); |
| 224 } | 209 } |
| 225 | 210 |
| 226 } // namespace protocol | 211 } // namespace protocol |
| 227 } // namespace remoting | 212 } // namespace remoting |
| OLD | NEW |