OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 15 matching lines...) Expand all Loading... |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/app/webrtc/peerconnection.h" | 28 #include "talk/app/webrtc/peerconnection.h" |
29 | 29 |
30 #include <vector> | 30 #include <vector> |
31 | 31 |
32 #include "talk/app/webrtc/dtmfsender.h" | 32 #include "talk/app/webrtc/dtmfsender.h" |
33 #include "talk/app/webrtc/jsepicecandidate.h" | 33 #include "talk/app/webrtc/jsepicecandidate.h" |
34 #include "talk/app/webrtc/jsepsessiondescription.h" | 34 #include "talk/app/webrtc/jsepsessiondescription.h" |
35 #include "talk/app/webrtc/mediaconstraintsinterface.h" | 35 #include "talk/app/webrtc/mediaconstraintsinterface.h" |
36 #include "talk/app/webrtc/mediastreamhandler.h" | 36 #include "talk/app/webrtc/rtpreceiver.h" |
| 37 #include "talk/app/webrtc/rtpsender.h" |
37 #include "talk/app/webrtc/streamcollection.h" | 38 #include "talk/app/webrtc/streamcollection.h" |
38 #include "webrtc/p2p/client/basicportallocator.h" | 39 #include "webrtc/p2p/client/basicportallocator.h" |
39 #include "talk/session/media/channelmanager.h" | 40 #include "talk/session/media/channelmanager.h" |
40 #include "webrtc/base/logging.h" | 41 #include "webrtc/base/logging.h" |
41 #include "webrtc/base/stringencode.h" | 42 #include "webrtc/base/stringencode.h" |
42 #include "webrtc/system_wrappers/interface/field_trial.h" | 43 #include "webrtc/system_wrappers/interface/field_trial.h" |
43 | 44 |
44 namespace { | 45 namespace { |
45 | 46 |
46 using webrtc::PeerConnectionInterface; | 47 using webrtc::PeerConnectionInterface; |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 observer_(NULL), | 333 observer_(NULL), |
333 uma_observer_(NULL), | 334 uma_observer_(NULL), |
334 signaling_state_(kStable), | 335 signaling_state_(kStable), |
335 ice_state_(kIceNew), | 336 ice_state_(kIceNew), |
336 ice_connection_state_(kIceConnectionNew), | 337 ice_connection_state_(kIceConnectionNew), |
337 ice_gathering_state_(kIceGatheringNew) { | 338 ice_gathering_state_(kIceGatheringNew) { |
338 } | 339 } |
339 | 340 |
340 PeerConnection::~PeerConnection() { | 341 PeerConnection::~PeerConnection() { |
341 ASSERT(signaling_thread()->IsCurrent()); | 342 ASSERT(signaling_thread()->IsCurrent()); |
342 if (mediastream_signaling_) | 343 if (mediastream_signaling_) { |
343 mediastream_signaling_->TearDown(); | 344 mediastream_signaling_->TearDown(); |
344 if (stream_handler_container_) | 345 } |
345 stream_handler_container_->TearDown(); | 346 // Need to detach RTP senders/receivers from WebRtcSession, |
| 347 // since it's about to be destroyed. |
| 348 for (const auto& sender : senders_) { |
| 349 sender->Stop(); |
| 350 } |
| 351 for (const auto& receiver : receivers_) { |
| 352 receiver->Stop(); |
| 353 } |
346 } | 354 } |
347 | 355 |
348 bool PeerConnection::Initialize( | 356 bool PeerConnection::Initialize( |
349 const PeerConnectionInterface::RTCConfiguration& configuration, | 357 const PeerConnectionInterface::RTCConfiguration& configuration, |
350 const MediaConstraintsInterface* constraints, | 358 const MediaConstraintsInterface* constraints, |
351 PortAllocatorFactoryInterface* allocator_factory, | 359 PortAllocatorFactoryInterface* allocator_factory, |
352 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, | 360 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
353 PeerConnectionObserver* observer) { | 361 PeerConnectionObserver* observer) { |
354 ASSERT(observer != NULL); | 362 ASSERT(observer != NULL); |
355 if (!observer) | 363 if (!observer) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 port_allocator_->set_step_delay(cricket::kMinimumStepDelay); | 400 port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
393 | 401 |
394 mediastream_signaling_.reset(new MediaStreamSignaling( | 402 mediastream_signaling_.reset(new MediaStreamSignaling( |
395 factory_->signaling_thread(), this, factory_->channel_manager())); | 403 factory_->signaling_thread(), this, factory_->channel_manager())); |
396 | 404 |
397 session_.reset(new WebRtcSession(factory_->channel_manager(), | 405 session_.reset(new WebRtcSession(factory_->channel_manager(), |
398 factory_->signaling_thread(), | 406 factory_->signaling_thread(), |
399 factory_->worker_thread(), | 407 factory_->worker_thread(), |
400 port_allocator_.get(), | 408 port_allocator_.get(), |
401 mediastream_signaling_.get())); | 409 mediastream_signaling_.get())); |
402 stream_handler_container_.reset(new MediaStreamHandlerContainer( | |
403 session_.get(), session_.get())); | |
404 stats_.reset(new StatsCollector(session_.get())); | 410 stats_.reset(new StatsCollector(session_.get())); |
405 | 411 |
406 // Initialize the WebRtcSession. It creates transport channels etc. | 412 // Initialize the WebRtcSession. It creates transport channels etc. |
407 if (!session_->Initialize(factory_->options(), constraints, | 413 if (!session_->Initialize(factory_->options(), constraints, |
408 dtls_identity_store.Pass(), configuration)) | 414 dtls_identity_store.Pass(), configuration)) |
409 return false; | 415 return false; |
410 | 416 |
411 // Register PeerConnection as receiver of local ice candidates. | 417 // Register PeerConnection as receiver of local ice candidates. |
412 // All the callbacks will be posted to the application from PeerConnection. | 418 // All the callbacks will be posted to the application from PeerConnection. |
413 session_->RegisterIceObserver(this); | 419 session_->RegisterIceObserver(this); |
414 session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); | 420 session_->SignalState.connect(this, &PeerConnection::OnSessionStateChange); |
415 return true; | 421 return true; |
416 } | 422 } |
417 | 423 |
418 rtc::scoped_refptr<StreamCollectionInterface> | 424 rtc::scoped_refptr<StreamCollectionInterface> |
419 PeerConnection::local_streams() { | 425 PeerConnection::local_streams() { |
420 return mediastream_signaling_->local_streams(); | 426 return mediastream_signaling_->local_streams(); |
421 } | 427 } |
422 | 428 |
423 rtc::scoped_refptr<StreamCollectionInterface> | 429 rtc::scoped_refptr<StreamCollectionInterface> |
424 PeerConnection::remote_streams() { | 430 PeerConnection::remote_streams() { |
425 return mediastream_signaling_->remote_streams(); | 431 return mediastream_signaling_->remote_streams(); |
426 } | 432 } |
427 | 433 |
| 434 // TODO(deadbeef): Create RtpSenders immediately here, even if local |
| 435 // description hasn't yet been set. |
428 bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { | 436 bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
429 if (IsClosed()) { | 437 if (IsClosed()) { |
430 return false; | 438 return false; |
431 } | 439 } |
432 if (!CanAddLocalMediaStream(mediastream_signaling_->local_streams(), | 440 if (!CanAddLocalMediaStream(mediastream_signaling_->local_streams(), |
433 local_stream)) | 441 local_stream)) |
434 return false; | 442 return false; |
435 | 443 |
436 if (!mediastream_signaling_->AddLocalStream(local_stream)) { | 444 if (!mediastream_signaling_->AddLocalStream(local_stream)) { |
437 return false; | 445 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
462 | 470 |
463 rtc::scoped_refptr<DtmfSenderInterface> sender( | 471 rtc::scoped_refptr<DtmfSenderInterface> sender( |
464 DtmfSender::Create(track, signaling_thread(), session_.get())); | 472 DtmfSender::Create(track, signaling_thread(), session_.get())); |
465 if (!sender.get()) { | 473 if (!sender.get()) { |
466 LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; | 474 LOG(LS_ERROR) << "CreateDtmfSender failed on DtmfSender::Create."; |
467 return NULL; | 475 return NULL; |
468 } | 476 } |
469 return DtmfSenderProxy::Create(signaling_thread(), sender.get()); | 477 return DtmfSenderProxy::Create(signaling_thread(), sender.get()); |
470 } | 478 } |
471 | 479 |
| 480 std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 481 const { |
| 482 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders; |
| 483 for (const auto& sender : senders_) { |
| 484 senders.push_back(RtpSenderProxy::Create(signaling_thread(), sender.get())); |
| 485 } |
| 486 return senders; |
| 487 } |
| 488 |
| 489 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 490 PeerConnection::GetReceivers() const { |
| 491 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers; |
| 492 for (const auto& receiver : receivers_) { |
| 493 receivers.push_back( |
| 494 RtpReceiverProxy::Create(signaling_thread(), receiver.get())); |
| 495 } |
| 496 return receivers; |
| 497 } |
| 498 |
472 bool PeerConnection::GetStats(StatsObserver* observer, | 499 bool PeerConnection::GetStats(StatsObserver* observer, |
473 MediaStreamTrackInterface* track, | 500 MediaStreamTrackInterface* track, |
474 StatsOutputLevel level) { | 501 StatsOutputLevel level) { |
475 ASSERT(signaling_thread()->IsCurrent()); | 502 ASSERT(signaling_thread()->IsCurrent()); |
476 if (!VERIFY(observer != NULL)) { | 503 if (!VERIFY(observer != NULL)) { |
477 LOG(LS_ERROR) << "GetStats - observer is NULL."; | 504 LOG(LS_ERROR) << "GetStats - observer is NULL."; |
478 return false; | 505 return false; |
479 } | 506 } |
480 | 507 |
481 stats_->UpdateStats(level); | 508 stats_->UpdateStats(level); |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 break; | 826 break; |
800 } | 827 } |
801 } | 828 } |
802 | 829 |
803 void PeerConnection::OnAddRemoteStream(MediaStreamInterface* stream) { | 830 void PeerConnection::OnAddRemoteStream(MediaStreamInterface* stream) { |
804 stats_->AddStream(stream); | 831 stats_->AddStream(stream); |
805 observer_->OnAddStream(stream); | 832 observer_->OnAddStream(stream); |
806 } | 833 } |
807 | 834 |
808 void PeerConnection::OnRemoveRemoteStream(MediaStreamInterface* stream) { | 835 void PeerConnection::OnRemoveRemoteStream(MediaStreamInterface* stream) { |
809 stream_handler_container_->RemoveRemoteStream(stream); | |
810 observer_->OnRemoveStream(stream); | 836 observer_->OnRemoveStream(stream); |
811 } | 837 } |
812 | 838 |
813 void PeerConnection::OnAddDataChannel(DataChannelInterface* data_channel) { | 839 void PeerConnection::OnAddDataChannel(DataChannelInterface* data_channel) { |
814 observer_->OnDataChannel(DataChannelProxy::Create(signaling_thread(), | 840 observer_->OnDataChannel(DataChannelProxy::Create(signaling_thread(), |
815 data_channel)); | 841 data_channel)); |
816 } | 842 } |
817 | 843 |
818 void PeerConnection::OnAddRemoteAudioTrack(MediaStreamInterface* stream, | 844 void PeerConnection::OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
819 AudioTrackInterface* audio_track, | 845 AudioTrackInterface* audio_track, |
820 uint32 ssrc) { | 846 uint32 ssrc) { |
821 stream_handler_container_->AddRemoteAudioTrack(stream, audio_track, ssrc); | 847 receivers_.push_back(new AudioRtpReceiver(audio_track, ssrc, session_.get())); |
822 } | 848 } |
823 | 849 |
824 void PeerConnection::OnAddRemoteVideoTrack(MediaStreamInterface* stream, | 850 void PeerConnection::OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
825 VideoTrackInterface* video_track, | 851 VideoTrackInterface* video_track, |
826 uint32 ssrc) { | 852 uint32 ssrc) { |
827 stream_handler_container_->AddRemoteVideoTrack(stream, video_track, ssrc); | 853 receivers_.push_back(new VideoRtpReceiver(video_track, ssrc, session_.get())); |
828 } | 854 } |
829 | 855 |
| 856 // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 857 // description. |
830 void PeerConnection::OnRemoveRemoteAudioTrack( | 858 void PeerConnection::OnRemoveRemoteAudioTrack( |
831 MediaStreamInterface* stream, | 859 MediaStreamInterface* stream, |
832 AudioTrackInterface* audio_track) { | 860 AudioTrackInterface* audio_track) { |
833 stream_handler_container_->RemoveRemoteTrack(stream, audio_track); | 861 auto it = FindReceiverForTrack(audio_track); |
| 862 if (it == receivers_.end()) { |
| 863 LOG(LS_WARNING) << "RtpReceiver for track with id " << audio_track->id() |
| 864 << " doesn't exist."; |
| 865 } else { |
| 866 (*it)->Stop(); |
| 867 receivers_.erase(it); |
| 868 } |
834 } | 869 } |
835 | 870 |
836 void PeerConnection::OnRemoveRemoteVideoTrack( | 871 void PeerConnection::OnRemoveRemoteVideoTrack( |
837 MediaStreamInterface* stream, | 872 MediaStreamInterface* stream, |
838 VideoTrackInterface* video_track) { | 873 VideoTrackInterface* video_track) { |
839 stream_handler_container_->RemoveRemoteTrack(stream, video_track); | 874 auto it = FindReceiverForTrack(video_track); |
| 875 if (it == receivers_.end()) { |
| 876 LOG(LS_WARNING) << "RtpReceiver for track with id " << video_track->id() |
| 877 << " doesn't exist."; |
| 878 } else { |
| 879 (*it)->Stop(); |
| 880 receivers_.erase(it); |
| 881 } |
840 } | 882 } |
| 883 |
841 void PeerConnection::OnAddLocalAudioTrack(MediaStreamInterface* stream, | 884 void PeerConnection::OnAddLocalAudioTrack(MediaStreamInterface* stream, |
842 AudioTrackInterface* audio_track, | 885 AudioTrackInterface* audio_track, |
843 uint32 ssrc) { | 886 uint32 ssrc) { |
844 stream_handler_container_->AddLocalAudioTrack(stream, audio_track, ssrc); | 887 senders_.push_back(new AudioRtpSender(audio_track, ssrc, session_.get())); |
845 stats_->AddLocalAudioTrack(audio_track, ssrc); | 888 stats_->AddLocalAudioTrack(audio_track, ssrc); |
846 } | 889 } |
| 890 |
847 void PeerConnection::OnAddLocalVideoTrack(MediaStreamInterface* stream, | 891 void PeerConnection::OnAddLocalVideoTrack(MediaStreamInterface* stream, |
848 VideoTrackInterface* video_track, | 892 VideoTrackInterface* video_track, |
849 uint32 ssrc) { | 893 uint32 ssrc) { |
850 stream_handler_container_->AddLocalVideoTrack(stream, video_track, ssrc); | 894 senders_.push_back(new VideoRtpSender(video_track, ssrc, session_.get())); |
851 } | 895 } |
852 | 896 |
| 897 // TODO(deadbeef): Keep RtpSenders around even if track goes away in local |
| 898 // description. |
853 void PeerConnection::OnRemoveLocalAudioTrack(MediaStreamInterface* stream, | 899 void PeerConnection::OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
854 AudioTrackInterface* audio_track, | 900 AudioTrackInterface* audio_track, |
855 uint32 ssrc) { | 901 uint32 ssrc) { |
856 stream_handler_container_->RemoveLocalTrack(stream, audio_track); | 902 auto it = FindSenderForTrack(audio_track); |
| 903 if (it == senders_.end()) { |
| 904 LOG(LS_WARNING) << "RtpSender for track with id " << audio_track->id() |
| 905 << " doesn't exist."; |
| 906 return; |
| 907 } else { |
| 908 (*it)->Stop(); |
| 909 senders_.erase(it); |
| 910 } |
857 stats_->RemoveLocalAudioTrack(audio_track, ssrc); | 911 stats_->RemoveLocalAudioTrack(audio_track, ssrc); |
858 } | 912 } |
859 | 913 |
860 void PeerConnection::OnRemoveLocalVideoTrack(MediaStreamInterface* stream, | 914 void PeerConnection::OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
861 VideoTrackInterface* video_track) { | 915 VideoTrackInterface* video_track) { |
862 stream_handler_container_->RemoveLocalTrack(stream, video_track); | 916 auto it = FindSenderForTrack(video_track); |
| 917 if (it == senders_.end()) { |
| 918 LOG(LS_WARNING) << "RtpSender for track with id " << video_track->id() |
| 919 << " doesn't exist."; |
| 920 return; |
| 921 } else { |
| 922 (*it)->Stop(); |
| 923 senders_.erase(it); |
| 924 } |
863 } | 925 } |
864 | 926 |
865 void PeerConnection::OnRemoveLocalStream(MediaStreamInterface* stream) { | 927 void PeerConnection::OnRemoveLocalStream(MediaStreamInterface* stream) { |
866 stream_handler_container_->RemoveLocalStream(stream); | |
867 } | 928 } |
868 | 929 |
869 void PeerConnection::OnIceConnectionChange( | 930 void PeerConnection::OnIceConnectionChange( |
870 PeerConnectionInterface::IceConnectionState new_state) { | 931 PeerConnectionInterface::IceConnectionState new_state) { |
871 ASSERT(signaling_thread()->IsCurrent()); | 932 ASSERT(signaling_thread()->IsCurrent()); |
872 ice_connection_state_ = new_state; | 933 ice_connection_state_ = new_state; |
873 observer_->OnIceConnectionChange(ice_connection_state_); | 934 observer_->OnIceConnectionChange(ice_connection_state_); |
874 } | 935 } |
875 | 936 |
876 void PeerConnection::OnIceGatheringChange( | 937 void PeerConnection::OnIceGatheringChange( |
(...skipping 29 matching lines...) Expand all Loading... |
906 observer_->OnIceConnectionChange(ice_connection_state_); | 967 observer_->OnIceConnectionChange(ice_connection_state_); |
907 if (ice_gathering_state_ != kIceGatheringComplete) { | 968 if (ice_gathering_state_ != kIceGatheringComplete) { |
908 ice_gathering_state_ = kIceGatheringComplete; | 969 ice_gathering_state_ = kIceGatheringComplete; |
909 observer_->OnIceGatheringChange(ice_gathering_state_); | 970 observer_->OnIceGatheringChange(ice_gathering_state_); |
910 } | 971 } |
911 } | 972 } |
912 observer_->OnSignalingChange(signaling_state_); | 973 observer_->OnSignalingChange(signaling_state_); |
913 observer_->OnStateChange(PeerConnectionObserver::kSignalingState); | 974 observer_->OnStateChange(PeerConnectionObserver::kSignalingState); |
914 } | 975 } |
915 | 976 |
| 977 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 978 PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) { |
| 979 return std::find_if( |
| 980 senders_.begin(), senders_.end(), |
| 981 [track](const rtc::scoped_refptr<RtpSenderInterface>& sender) { |
| 982 return sender->track() == track; |
| 983 }); |
| 984 } |
| 985 |
| 986 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 987 PeerConnection::FindReceiverForTrack(MediaStreamTrackInterface* track) { |
| 988 return std::find_if( |
| 989 receivers_.begin(), receivers_.end(), |
| 990 [track](const rtc::scoped_refptr<RtpReceiverInterface>& receiver) { |
| 991 return receiver->track() == track; |
| 992 }); |
| 993 } |
| 994 |
916 } // namespace webrtc | 995 } // namespace webrtc |
OLD | NEW |