Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: webrtc/api/peerconnection.cc

Issue 2587133004: Reland of: Adding error output param to SetConfiguration, using new RTCError type. (Closed)
Patch Set: Comment formatting. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/api/peerconnection.h ('k') | webrtc/api/peerconnection_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "webrtc/pc/channelmanager.h" 42 #include "webrtc/pc/channelmanager.h"
43 #include "webrtc/system_wrappers/include/clock.h" 43 #include "webrtc/system_wrappers/include/clock.h"
44 #include "webrtc/system_wrappers/include/field_trial.h" 44 #include "webrtc/system_wrappers/include/field_trial.h"
45 45
46 namespace { 46 namespace {
47 47
48 using webrtc::DataChannel; 48 using webrtc::DataChannel;
49 using webrtc::MediaConstraintsInterface; 49 using webrtc::MediaConstraintsInterface;
50 using webrtc::MediaStreamInterface; 50 using webrtc::MediaStreamInterface;
51 using webrtc::PeerConnectionInterface; 51 using webrtc::PeerConnectionInterface;
52 using webrtc::RtcError;
52 using webrtc::RtpSenderInternal; 53 using webrtc::RtpSenderInternal;
53 using webrtc::RtpSenderInterface; 54 using webrtc::RtpSenderInterface;
54 using webrtc::RtpSenderProxy; 55 using webrtc::RtpSenderProxy;
55 using webrtc::RtpSenderProxyWithInternal; 56 using webrtc::RtpSenderProxyWithInternal;
56 using webrtc::StreamCollection; 57 using webrtc::StreamCollection;
57 58
58 static const char kDefaultStreamLabel[] = "default"; 59 static const char kDefaultStreamLabel[] = "default";
59 static const char kDefaultAudioTrackLabel[] = "defaulta0"; 60 static const char kDefaultAudioTrackLabel[] = "defaulta0";
60 static const char kDefaultVideoTrackLabel[] = "defaultv0"; 61 static const char kDefaultVideoTrackLabel[] = "defaultv0";
61 62
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 *host = in_str.substr(0, colonpos); 202 *host = in_str.substr(0, colonpos);
202 } else { 203 } else {
203 *host = in_str; 204 *host = in_str;
204 } 205 }
205 } 206 }
206 return !host->empty(); 207 return !host->empty();
207 } 208 }
208 209
209 // Adds a STUN or TURN server to the appropriate list, 210 // Adds a STUN or TURN server to the appropriate list,
210 // by parsing |url| and using the username/password in |server|. 211 // by parsing |url| and using the username/password in |server|.
211 bool ParseIceServerUrl(const PeerConnectionInterface::IceServer& server, 212 RtcError ParseIceServerUrl(
212 const std::string& url, 213 const PeerConnectionInterface::IceServer& server,
213 cricket::ServerAddresses* stun_servers, 214 const std::string& url,
214 std::vector<cricket::RelayServerConfig>* turn_servers) { 215 cricket::ServerAddresses* stun_servers,
216 std::vector<cricket::RelayServerConfig>* turn_servers) {
215 // draft-nandakumar-rtcweb-stun-uri-01 217 // draft-nandakumar-rtcweb-stun-uri-01
216 // stunURI = scheme ":" stun-host [ ":" stun-port ] 218 // stunURI = scheme ":" stun-host [ ":" stun-port ]
217 // scheme = "stun" / "stuns" 219 // scheme = "stun" / "stuns"
218 // stun-host = IP-literal / IPv4address / reg-name 220 // stun-host = IP-literal / IPv4address / reg-name
219 // stun-port = *DIGIT 221 // stun-port = *DIGIT
220 222
221 // draft-petithuguenin-behave-turn-uris-01 223 // draft-petithuguenin-behave-turn-uris-01
222 // turnURI = scheme ":" turn-host [ ":" turn-port ] 224 // turnURI = scheme ":" turn-host [ ":" turn-port ]
223 // [ "?transport=" transport ] 225 // [ "?transport=" transport ]
224 // scheme = "turn" / "turns" 226 // scheme = "turn" / "turns"
225 // transport = "udp" / "tcp" / transport-ext 227 // transport = "udp" / "tcp" / transport-ext
226 // transport-ext = 1*unreserved 228 // transport-ext = 1*unreserved
227 // turn-host = IP-literal / IPv4address / reg-name 229 // turn-host = IP-literal / IPv4address / reg-name
228 // turn-port = *DIGIT 230 // turn-port = *DIGIT
229 RTC_DCHECK(stun_servers != nullptr); 231 RTC_DCHECK(stun_servers != nullptr);
230 RTC_DCHECK(turn_servers != nullptr); 232 RTC_DCHECK(turn_servers != nullptr);
231 std::vector<std::string> tokens; 233 std::vector<std::string> tokens;
232 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP; 234 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP;
233 RTC_DCHECK(!url.empty()); 235 RTC_DCHECK(!url.empty());
234 rtc::tokenize_with_empty_tokens(url, '?', &tokens); 236 rtc::tokenize_with_empty_tokens(url, '?', &tokens);
235 std::string uri_without_transport = tokens[0]; 237 std::string uri_without_transport = tokens[0];
236 // Let's look into transport= param, if it exists. 238 // Let's look into transport= param, if it exists.
237 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. 239 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present.
238 std::string uri_transport_param = tokens[1]; 240 std::string uri_transport_param = tokens[1];
239 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens); 241 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens);
240 if (tokens[0] != kTransport) { 242 if (tokens[0] != kTransport) {
241 LOG(LS_WARNING) << "Invalid transport parameter key."; 243 LOG(LS_WARNING) << "Invalid transport parameter key.";
242 return false; 244 return RtcError::SYNTAX_ERROR;
243 } 245 }
244 if (tokens.size() < 2 || 246 if (tokens.size() < 2 ||
245 !cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) || 247 !cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
246 (turn_transport_type != cricket::PROTO_UDP && 248 (turn_transport_type != cricket::PROTO_UDP &&
247 turn_transport_type != cricket::PROTO_TCP)) { 249 turn_transport_type != cricket::PROTO_TCP)) {
248 LOG(LS_WARNING) << "Transport param should always be udp or tcp."; 250 LOG(LS_WARNING) << "Transport param should always be udp or tcp.";
249 return false; 251 return RtcError::SYNTAX_ERROR;
250 } 252 }
251 } 253 }
252 254
253 std::string hoststring; 255 std::string hoststring;
254 ServiceType service_type; 256 ServiceType service_type;
255 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, 257 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport,
256 &service_type, 258 &service_type,
257 &hoststring)) { 259 &hoststring)) {
258 LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url; 260 LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url;
259 return false; 261 return RtcError::SYNTAX_ERROR;
260 } 262 }
261 263
262 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring 264 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring
263 RTC_DCHECK(!hoststring.empty()); 265 RTC_DCHECK(!hoststring.empty());
264 266
265 // Let's break hostname. 267 // Let's break hostname.
266 tokens.clear(); 268 tokens.clear();
267 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens); 269 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens);
268 270
269 std::string username(server.username); 271 std::string username(server.username);
270 if (tokens.size() > kTurnHostTokensNum) { 272 if (tokens.size() > kTurnHostTokensNum) {
271 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; 273 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
272 return false; 274 return RtcError::SYNTAX_ERROR;
273 } 275 }
274 if (tokens.size() == kTurnHostTokensNum) { 276 if (tokens.size() == kTurnHostTokensNum) {
275 if (tokens[0].empty() || tokens[1].empty()) { 277 if (tokens[0].empty() || tokens[1].empty()) {
276 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; 278 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring;
277 return false; 279 return RtcError::SYNTAX_ERROR;
278 } 280 }
279 username.assign(rtc::s_url_decode(tokens[0])); 281 username.assign(rtc::s_url_decode(tokens[0]));
280 hoststring = tokens[1]; 282 hoststring = tokens[1];
281 } else { 283 } else {
282 hoststring = tokens[0]; 284 hoststring = tokens[0];
283 } 285 }
284 286
285 int port = kDefaultStunPort; 287 int port = kDefaultStunPort;
286 if (service_type == TURNS) { 288 if (service_type == TURNS) {
287 port = kDefaultStunTlsPort; 289 port = kDefaultStunTlsPort;
288 turn_transport_type = cricket::PROTO_TLS; 290 turn_transport_type = cricket::PROTO_TLS;
289 } 291 }
290 292
291 std::string address; 293 std::string address;
292 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) { 294 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) {
293 LOG(WARNING) << "Invalid hostname format: " << uri_without_transport; 295 LOG(WARNING) << "Invalid hostname format: " << uri_without_transport;
294 return false; 296 return RtcError::SYNTAX_ERROR;
295 } 297 }
296 298
297 if (port <= 0 || port > 0xffff) { 299 if (port <= 0 || port > 0xffff) {
298 LOG(WARNING) << "Invalid port: " << port; 300 LOG(WARNING) << "Invalid port: " << port;
299 return false; 301 return RtcError::SYNTAX_ERROR;
300 } 302 }
301 303
302 switch (service_type) { 304 switch (service_type) {
303 case STUN: 305 case STUN:
304 case STUNS: 306 case STUNS:
305 stun_servers->insert(rtc::SocketAddress(address, port)); 307 stun_servers->insert(rtc::SocketAddress(address, port));
306 break; 308 break;
307 case TURN: 309 case TURN:
308 case TURNS: { 310 case TURNS: {
311 if (username.empty() || server.password.empty()) {
312 // The WebRTC spec requires throwing an InvalidAccessError when username
313 // or credential are ommitted; this is the native equivalent.
314 return RtcError::INVALID_PARAMETER;
315 }
309 turn_servers->push_back(cricket::RelayServerConfig( 316 turn_servers->push_back(cricket::RelayServerConfig(
310 address, port, username, server.password, turn_transport_type)); 317 address, port, username, server.password, turn_transport_type));
311 break; 318 break;
312 } 319 }
313 case INVALID:
314 default: 320 default:
315 LOG(WARNING) << "Configuration not supported: " << url; 321 // We shouldn't get to this point with an invalid service_type, we should
316 return false; 322 // have returned an error already.
323 RTC_DCHECK(false) << "Unexpected service type";
324 return RtcError::INTERNAL_ERROR;
317 } 325 }
318 return true; 326 return RtcError::NONE;
319 } 327 }
320 328
321 // Check if we can send |new_stream| on a PeerConnection. 329 // Check if we can send |new_stream| on a PeerConnection.
322 bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, 330 bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
323 webrtc::MediaStreamInterface* new_stream) { 331 webrtc::MediaStreamInterface* new_stream) {
324 if (!new_stream || !current_streams) { 332 if (!new_stream || !current_streams) {
325 return false; 333 return false;
326 } 334 }
327 if (current_streams->find(new_stream->label()) != nullptr) { 335 if (current_streams->find(new_stream->label()) != nullptr) {
328 LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() 336 LOG(LS_ERROR) << "MediaStream with label " << new_stream->label()
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 for (auto& receiver : receivers) { 428 for (auto& receiver : receivers) {
421 if (receiver->media_type() == media_type) { 429 if (receiver->media_type() == media_type) {
422 if (!channel) { 430 if (!channel) {
423 receiver->internal()->Stop(); 431 receiver->internal()->Stop();
424 } 432 }
425 static_cast<RECEIVER*>(receiver->internal())->SetChannel(channel); 433 static_cast<RECEIVER*>(receiver->internal())->SetChannel(channel);
426 } 434 }
427 } 435 }
428 } 436 }
429 437
438 // Helper to set an error and return from a method.
439 bool SafeSetError(webrtc::RtcError error, webrtc::RtcError* error_output) {
440 if (error_output) {
441 *error_output = error;
442 }
443 return error == webrtc::RtcError::NONE;
444 }
445
430 } // namespace 446 } // namespace
431 447
432 namespace webrtc { 448 namespace webrtc {
433 449
434 static const char* const kRtcErrorNames[] = { 450 static const char* const kRtcErrorNames[] = {
435 "NONE", 451 "NONE",
436 "UNSUPPORTED_PARAMETER", 452 "UNSUPPORTED_PARAMETER",
437 "INVALID_PARAMETER", 453 "INVALID_PARAMETER",
438 "INVALID_RANGE", 454 "INVALID_RANGE",
439 "SYNTAX_ERROR", 455 "SYNTAX_ERROR",
440 "INVALID_STATE", 456 "INVALID_STATE",
441 "INVALID_MODIFICATION", 457 "INVALID_MODIFICATION",
442 "NETWORK_ERROR", 458 "NETWORK_ERROR",
443 "INTERNAL_ERROR", 459 "INTERNAL_ERROR",
444 }; 460 };
461 static_assert(static_cast<int>(RtcError::INTERNAL_ERROR) ==
462 (arraysize(kRtcErrorNames) - 1),
463 "kRtcErrorNames must have as many strings as RtcError "
464 "has values.");
445 465
446 std::ostream& operator<<(std::ostream& stream, RtcError error) { 466 std::ostream& operator<<(std::ostream& stream, RtcError error) {
447 int index = static_cast<int>(error); 467 int index = static_cast<int>(error);
448 RTC_CHECK(index < static_cast<int>(sizeof(kRtcErrorNames) /
449 sizeof(kRtcErrorNames[0])));
450 return stream << kRtcErrorNames[index]; 468 return stream << kRtcErrorNames[index];
451 } 469 }
452 470
471 bool PeerConnectionInterface::RTCConfiguration::operator==(
472 const PeerConnectionInterface::RTCConfiguration& o) const {
473 // This static_assert prevents us from accidentally breaking operator==.
474 struct stuff_being_tested_for_equality {
475 IceTransportsType type;
476 IceServers servers;
477 BundlePolicy bundle_policy;
478 RtcpMuxPolicy rtcp_mux_policy;
479 TcpCandidatePolicy tcp_candidate_policy;
480 CandidateNetworkPolicy candidate_network_policy;
481 int audio_jitter_buffer_max_packets;
482 bool audio_jitter_buffer_fast_accelerate;
483 int ice_connection_receiving_timeout;
484 int ice_backup_candidate_pair_ping_interval;
485 ContinualGatheringPolicy continual_gathering_policy;
486 std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
487 bool prioritize_most_likely_ice_candidate_pairs;
488 struct cricket::MediaConfig media_config;
489 bool disable_ipv6;
490 bool enable_rtp_data_channel;
491 bool enable_quic;
492 rtc::Optional<int> screencast_min_bitrate;
493 rtc::Optional<bool> combined_audio_video_bwe;
494 rtc::Optional<bool> enable_dtls_srtp;
495 int ice_candidate_pool_size;
496 bool prune_turn_ports;
497 bool presume_writable_when_fully_relayed;
498 bool enable_ice_renomination;
499 bool redetermine_role_on_ice_restart;
500 };
501 static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this),
502 "Did you add something to RTCConfiguration and forget to "
503 "update operator==?");
504 return type == o.type && servers == o.servers &&
505 bundle_policy == o.bundle_policy &&
506 rtcp_mux_policy == o.rtcp_mux_policy &&
507 tcp_candidate_policy == o.tcp_candidate_policy &&
508 candidate_network_policy == o.candidate_network_policy &&
509 audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
510 audio_jitter_buffer_fast_accelerate ==
511 o.audio_jitter_buffer_fast_accelerate &&
512 ice_connection_receiving_timeout ==
513 o.ice_connection_receiving_timeout &&
514 ice_backup_candidate_pair_ping_interval ==
515 o.ice_backup_candidate_pair_ping_interval &&
516 continual_gathering_policy == o.continual_gathering_policy &&
517 certificates == o.certificates &&
518 prioritize_most_likely_ice_candidate_pairs ==
519 o.prioritize_most_likely_ice_candidate_pairs &&
520 media_config == o.media_config && disable_ipv6 == o.disable_ipv6 &&
521 enable_rtp_data_channel == o.enable_rtp_data_channel &&
522 enable_quic == o.enable_quic &&
523 screencast_min_bitrate == o.screencast_min_bitrate &&
524 combined_audio_video_bwe == o.combined_audio_video_bwe &&
525 enable_dtls_srtp == o.enable_dtls_srtp &&
526 ice_candidate_pool_size == o.ice_candidate_pool_size &&
527 prune_turn_ports == o.prune_turn_ports &&
528 presume_writable_when_fully_relayed ==
529 o.presume_writable_when_fully_relayed &&
530 enable_ice_renomination == o.enable_ice_renomination &&
531 redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart;
532 }
533
534 bool PeerConnectionInterface::RTCConfiguration::operator!=(
535 const PeerConnectionInterface::RTCConfiguration& o) const {
536 return !(*this == o);
537 }
538
453 // Generate a RTCP CNAME when a PeerConnection is created. 539 // Generate a RTCP CNAME when a PeerConnection is created.
454 std::string GenerateRtcpCname() { 540 std::string GenerateRtcpCname() {
455 std::string cname; 541 std::string cname;
456 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { 542 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
457 LOG(LS_ERROR) << "Failed to generate CNAME."; 543 LOG(LS_ERROR) << "Failed to generate CNAME.";
458 RTC_DCHECK(false); 544 RTC_DCHECK(false);
459 } 545 }
460 return cname; 546 return cname;
461 } 547 }
462 548
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 for (auto& kv : session_options->transport_options) { 628 for (auto& kv : session_options->transport_options) {
543 kv.second.ice_restart = ice_restart; 629 kv.second.ice_restart = ice_restart;
544 } 630 }
545 631
546 if (!constraints) { 632 if (!constraints) {
547 return true; 633 return true;
548 } 634 }
549 return mandatory_constraints_satisfied == constraints->GetMandatory().size(); 635 return mandatory_constraints_satisfied == constraints->GetMandatory().size();
550 } 636 }
551 637
552 bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, 638 RtcError ParseIceServers(
553 cricket::ServerAddresses* stun_servers, 639 const PeerConnectionInterface::IceServers& servers,
554 std::vector<cricket::RelayServerConfig>* turn_servers) { 640 cricket::ServerAddresses* stun_servers,
641 std::vector<cricket::RelayServerConfig>* turn_servers) {
555 for (const webrtc::PeerConnectionInterface::IceServer& server : servers) { 642 for (const webrtc::PeerConnectionInterface::IceServer& server : servers) {
556 if (!server.urls.empty()) { 643 if (!server.urls.empty()) {
557 for (const std::string& url : server.urls) { 644 for (const std::string& url : server.urls) {
558 if (url.empty()) { 645 if (url.empty()) {
559 LOG(LS_ERROR) << "Empty uri."; 646 LOG(LS_ERROR) << "Empty uri.";
560 return false; 647 return RtcError::SYNTAX_ERROR;
561 } 648 }
562 if (!ParseIceServerUrl(server, url, stun_servers, turn_servers)) { 649 RtcError err =
563 return false; 650 ParseIceServerUrl(server, url, stun_servers, turn_servers);
651 if (err != RtcError::NONE) {
652 return err;
564 } 653 }
565 } 654 }
566 } else if (!server.uri.empty()) { 655 } else if (!server.uri.empty()) {
567 // Fallback to old .uri if new .urls isn't present. 656 // Fallback to old .uri if new .urls isn't present.
568 if (!ParseIceServerUrl(server, server.uri, stun_servers, turn_servers)) { 657 RtcError err =
569 return false; 658 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers);
659 if (err != RtcError::NONE) {
660 return err;
570 } 661 }
571 } else { 662 } else {
572 LOG(LS_ERROR) << "Empty uri."; 663 LOG(LS_ERROR) << "Empty uri.";
573 return false; 664 return RtcError::SYNTAX_ERROR;
574 } 665 }
575 } 666 }
576 // Candidates must have unique priorities, so that connectivity checks 667 // Candidates must have unique priorities, so that connectivity checks
577 // are performed in a well-defined order. 668 // are performed in a well-defined order.
578 int priority = static_cast<int>(turn_servers->size() - 1); 669 int priority = static_cast<int>(turn_servers->size() - 1);
579 for (cricket::RelayServerConfig& turn_server : *turn_servers) { 670 for (cricket::RelayServerConfig& turn_server : *turn_servers) {
580 // First in the list gets highest priority. 671 // First in the list gets highest priority.
581 turn_server.priority = priority--; 672 turn_server.priority = priority--;
582 } 673 }
583 return true; 674 return RtcError::NONE;
584 } 675 }
585 676
586 PeerConnection::PeerConnection(PeerConnectionFactory* factory) 677 PeerConnection::PeerConnection(PeerConnectionFactory* factory)
587 : factory_(factory), 678 : factory_(factory),
588 observer_(NULL), 679 observer_(NULL),
589 uma_observer_(NULL), 680 uma_observer_(NULL),
590 signaling_state_(kStable), 681 signaling_state_(kStable),
591 ice_connection_state_(kIceConnectionNew), 682 ice_connection_state_(kIceConnectionNew),
592 ice_gathering_state_(kIceGatheringNew), 683 ice_gathering_state_(kIceGatheringNew),
593 event_log_(RtcEventLog::Create(webrtc::Clock::GetRealTimeClock())), 684 event_log_(RtcEventLog::Create(webrtc::Clock::GetRealTimeClock())),
(...skipping 22 matching lines...) Expand all
616 network_thread()->Invoke<void>(RTC_FROM_HERE, 707 network_thread()->Invoke<void>(RTC_FROM_HERE,
617 [this] { port_allocator_.reset(nullptr); }); 708 [this] { port_allocator_.reset(nullptr); });
618 } 709 }
619 710
620 bool PeerConnection::Initialize( 711 bool PeerConnection::Initialize(
621 const PeerConnectionInterface::RTCConfiguration& configuration, 712 const PeerConnectionInterface::RTCConfiguration& configuration,
622 std::unique_ptr<cricket::PortAllocator> allocator, 713 std::unique_ptr<cricket::PortAllocator> allocator,
623 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, 714 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
624 PeerConnectionObserver* observer) { 715 PeerConnectionObserver* observer) {
625 TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); 716 TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
626 RTC_DCHECK(observer != nullptr); 717 if (!allocator) {
718 LOG(LS_ERROR) << "PeerConnection initialized without a PortAllocator? "
719 << "This shouldn't happen if using PeerConnectionFactory.";
720 return false;
721 }
627 if (!observer) { 722 if (!observer) {
723 // TODO(deadbeef): Why do we do this?
724 LOG(LS_ERROR) << "PeerConnection initialized without a "
725 << "PeerConnectionObserver";
628 return false; 726 return false;
629 } 727 }
630 observer_ = observer; 728 observer_ = observer;
631
632 port_allocator_ = std::move(allocator); 729 port_allocator_ = std::move(allocator);
633 730
634 // The port allocator lives on the network thread and should be initialized 731 // The port allocator lives on the network thread and should be initialized
635 // there. 732 // there.
636 if (!network_thread()->Invoke<bool>( 733 if (!network_thread()->Invoke<bool>(
637 RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, 734 RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n,
638 this, configuration))) { 735 this, configuration))) {
639 return false; 736 return false;
640 } 737 }
641 738
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
1277 1374
1278 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); 1375 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1279 signaling_thread()->Post(RTC_FROM_HERE, this, 1376 signaling_thread()->Post(RTC_FROM_HERE, this,
1280 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); 1377 MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
1281 } 1378 }
1282 1379
1283 PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { 1380 PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() {
1284 return configuration_; 1381 return configuration_;
1285 } 1382 }
1286 1383
1287 bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration) { 1384 bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration,
1385 RtcError* error) {
1288 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); 1386 TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
1289 1387
1290 if (session_->local_description() && 1388 if (session_->local_description() &&
1291 configuration.ice_candidate_pool_size != 1389 configuration.ice_candidate_pool_size !=
1292 configuration_.ice_candidate_pool_size) { 1390 configuration_.ice_candidate_pool_size) {
1293 LOG(LS_ERROR) << "Can't change candidate pool size after calling " 1391 LOG(LS_ERROR) << "Can't change candidate pool size after calling "
1294 "SetLocalDescription."; 1392 "SetLocalDescription.";
1295 return false; 1393 return SafeSetError(RtcError::INVALID_MODIFICATION, error);
1296 }
1297 // TODO(deadbeef): Return false and log an error if there are any unsupported
1298 // modifications.
1299 if (port_allocator_) {
1300 if (!network_thread()->Invoke<bool>(
1301 RTC_FROM_HERE,
1302 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
1303 configuration))) {
1304 LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator.";
1305 return false;
1306 }
1307 } 1394 }
1308 1395
1309 // TODO(deadbeef): Shouldn't have to hop to the network thread twice... 1396 // The simplest (and most future-compatible) way to tell if the config was
1310 session_->SetIceConfig(session_->ParseIceConfig(configuration)); 1397 // modified in an invalid way is to copy each property we do support
1398 // modifying, then use operator==. There are far more properties we don't
1399 // support modifying than those we do, and more could be added.
pthatcher1 2016/12/21 00:57:24 That's a good approach. I like it.
1400 RTCConfiguration modified_config = configuration_;
1401 modified_config.servers = configuration.servers;
1402 modified_config.type = configuration.type;
1403 modified_config.ice_candidate_pool_size =
1404 configuration.ice_candidate_pool_size;
1405 modified_config.prune_turn_ports = configuration.prune_turn_ports;
1406 if (configuration != modified_config) {
1407 LOG(LS_ERROR) << "Modifying the configuration in an unsupported way.";
1408 return SafeSetError(RtcError::INVALID_MODIFICATION, error);
1409 }
1410
1411 // Note that this isn't possible through chromium, since it's an unsigned
1412 // short in WebIDL.
1413 if (configuration.ice_candidate_pool_size < 0 ||
1414 configuration.ice_candidate_pool_size > UINT16_MAX) {
1415 return SafeSetError(RtcError::INVALID_RANGE, error);
1416 }
1417
1418 // Parse ICE servers before hopping to network thread.
1419 cricket::ServerAddresses stun_servers;
1420 std::vector<cricket::RelayServerConfig> turn_servers;
1421 RtcError parse_error =
1422 ParseIceServers(configuration.servers, &stun_servers, &turn_servers);
1423 if (parse_error != RtcError::NONE) {
1424 return SafeSetError(parse_error, error);
1425 }
1426
1427 // In theory this shouldn't fail.
1428 if (!network_thread()->Invoke<bool>(
1429 RTC_FROM_HERE,
1430 rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this,
1431 stun_servers, turn_servers, modified_config.type,
1432 modified_config.ice_candidate_pool_size,
1433 modified_config.prune_turn_ports))) {
1434 LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator.";
1435 return SafeSetError(RtcError::INTERNAL_ERROR, error);
1436 }
1311 1437
1312 // As described in JSEP, calling setConfiguration with new ICE servers or 1438 // As described in JSEP, calling setConfiguration with new ICE servers or
1313 // candidate policy must set a "needs-ice-restart" bit so that the next offer 1439 // candidate policy must set a "needs-ice-restart" bit so that the next offer
1314 // triggers an ICE restart which will pick up the changes. 1440 // triggers an ICE restart which will pick up the changes.
1315 if (configuration.servers != configuration_.servers || 1441 if (modified_config.servers != configuration_.servers ||
1316 configuration.type != configuration_.type) { 1442 modified_config.type != configuration_.type ||
1443 modified_config.prune_turn_ports != configuration_.prune_turn_ports) {
1317 session_->SetNeedsIceRestartFlag(); 1444 session_->SetNeedsIceRestartFlag();
1318 } 1445 }
1319 configuration_ = configuration; 1446 configuration_ = modified_config;
1320 return true; 1447 return SafeSetError(RtcError::NONE, error);
1321 } 1448 }
1322 1449
1323 bool PeerConnection::AddIceCandidate( 1450 bool PeerConnection::AddIceCandidate(
1324 const IceCandidateInterface* ice_candidate) { 1451 const IceCandidateInterface* ice_candidate) {
1325 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); 1452 TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
1326 if (IsClosed()) { 1453 if (IsClosed()) {
1327 return false; 1454 return false;
1328 } 1455 }
1329 return session_->ProcessIceMessage(ice_candidate); 1456 return session_->ProcessIceMessage(ice_candidate);
1330 } 1457 }
1331 1458
1332 bool PeerConnection::RemoveIceCandidates( 1459 bool PeerConnection::RemoveIceCandidates(
1333 const std::vector<cricket::Candidate>& candidates) { 1460 const std::vector<cricket::Candidate>& candidates) {
1334 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); 1461 TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates");
1335 return session_->RemoveRemoteIceCandidates(candidates); 1462 return session_->RemoveRemoteIceCandidates(candidates);
1336 } 1463 }
1337 1464
1338 void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { 1465 void PeerConnection::RegisterUMAObserver(UMAObserver* observer) {
1339 TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); 1466 TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver");
1340 uma_observer_ = observer; 1467 uma_observer_ = observer;
1341 1468
1342 if (session_) { 1469 if (session_) {
1343 session_->set_metrics_observer(uma_observer_); 1470 session_->set_metrics_observer(uma_observer_);
1344 } 1471 }
1345 1472
1346 // Send information about IPv4/IPv6 status. 1473 // Send information about IPv4/IPv6 status.
1347 if (uma_observer_ && port_allocator_) { 1474 if (uma_observer_) {
1348 port_allocator_->SetMetricsObserver(uma_observer_); 1475 port_allocator_->SetMetricsObserver(uma_observer_);
1349 if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { 1476 if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) {
1350 uma_observer_->IncrementEnumCounter( 1477 uma_observer_->IncrementEnumCounter(
1351 kEnumCounterAddressFamily, kPeerConnection_IPv6, 1478 kEnumCounterAddressFamily, kPeerConnection_IPv6,
1352 kPeerConnectionAddressFamilyCounter_Max); 1479 kPeerConnectionAddressFamilyCounter_Max);
1353 } else { 1480 } else {
1354 uma_observer_->IncrementEnumCounter( 1481 uma_observer_->IncrementEnumCounter(
1355 kEnumCounterAddressFamily, kPeerConnection_IPv4, 1482 kEnumCounterAddressFamily, kPeerConnection_IPv4,
1356 kPeerConnectionAddressFamilyCounter_Max); 1483 kPeerConnectionAddressFamilyCounter_Max);
1357 } 1484 }
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after
2330 return channel; 2457 return channel;
2331 } 2458 }
2332 } 2459 }
2333 return nullptr; 2460 return nullptr;
2334 } 2461 }
2335 2462
2336 bool PeerConnection::InitializePortAllocator_n( 2463 bool PeerConnection::InitializePortAllocator_n(
2337 const RTCConfiguration& configuration) { 2464 const RTCConfiguration& configuration) {
2338 cricket::ServerAddresses stun_servers; 2465 cricket::ServerAddresses stun_servers;
2339 std::vector<cricket::RelayServerConfig> turn_servers; 2466 std::vector<cricket::RelayServerConfig> turn_servers;
2340 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) { 2467 if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) !=
2468 RtcError::NONE) {
2341 return false; 2469 return false;
2342 } 2470 }
2343 2471
2344 port_allocator_->Initialize(); 2472 port_allocator_->Initialize();
2345 2473
2346 // To handle both internal and externally created port allocator, we will 2474 // To handle both internal and externally created port allocator, we will
2347 // enable BUNDLE here. 2475 // enable BUNDLE here.
2348 int portallocator_flags = port_allocator_->flags(); 2476 int portallocator_flags = port_allocator_->flags();
2349 portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 2477 portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2350 cricket::PORTALLOCATOR_ENABLE_IPV6; 2478 cricket::PORTALLOCATOR_ENABLE_IPV6;
(...skipping 25 matching lines...) Expand all
2376 2504
2377 // Call this last since it may create pooled allocator sessions using the 2505 // Call this last since it may create pooled allocator sessions using the
2378 // properties set above. 2506 // properties set above.
2379 port_allocator_->SetConfiguration(stun_servers, turn_servers, 2507 port_allocator_->SetConfiguration(stun_servers, turn_servers,
2380 configuration.ice_candidate_pool_size, 2508 configuration.ice_candidate_pool_size,
2381 configuration.prune_turn_ports); 2509 configuration.prune_turn_ports);
2382 return true; 2510 return true;
2383 } 2511 }
2384 2512
2385 bool PeerConnection::ReconfigurePortAllocator_n( 2513 bool PeerConnection::ReconfigurePortAllocator_n(
2386 const RTCConfiguration& configuration) { 2514 const cricket::ServerAddresses& stun_servers,
2387 cricket::ServerAddresses stun_servers; 2515 const std::vector<cricket::RelayServerConfig>& turn_servers,
2388 std::vector<cricket::RelayServerConfig> turn_servers; 2516 IceTransportsType type,
2389 if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) { 2517 int candidate_pool_size,
2390 return false; 2518 bool prune_turn_ports) {
2391 }
2392 port_allocator_->set_candidate_filter( 2519 port_allocator_->set_candidate_filter(
2393 ConvertIceTransportTypeToCandidateFilter(configuration.type)); 2520 ConvertIceTransportTypeToCandidateFilter(type));
2394 // Call this last since it may create pooled allocator sessions using the 2521 // Call this last since it may create pooled allocator sessions using the
2395 // candidate filter set above. 2522 // candidate filter set above.
2396 return port_allocator_->SetConfiguration( 2523 return port_allocator_->SetConfiguration(
2397 stun_servers, turn_servers, configuration.ice_candidate_pool_size, 2524 stun_servers, turn_servers, candidate_pool_size, prune_turn_ports);
2398 configuration.prune_turn_ports);
2399 } 2525 }
2400 2526
2401 bool PeerConnection::StartRtcEventLog_w(rtc::PlatformFile file, 2527 bool PeerConnection::StartRtcEventLog_w(rtc::PlatformFile file,
2402 int64_t max_size_bytes) { 2528 int64_t max_size_bytes) {
2403 return event_log_->StartLogging(file, max_size_bytes); 2529 return event_log_->StartLogging(file, max_size_bytes);
2404 } 2530 }
2405 2531
2406 void PeerConnection::StopRtcEventLog_w() { 2532 void PeerConnection::StopRtcEventLog_w() {
2407 event_log_->StopLogging(); 2533 event_log_->StopLogging();
2408 } 2534 }
2409 } // namespace webrtc 2535 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/api/peerconnection.h ('k') | webrtc/api/peerconnection_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698