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

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

Issue 2977493002: When a track is added/removed directly to MediaStream notify observer->OnRenegotionNeeded (Closed)
Patch Set: When removing stream, do not remove tracks if pc is closed Created 3 years, 4 months 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/pc/peerconnection.h ('k') | webrtc/pc/peerconnectioninterface_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 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 &PeerConnection::OnAudioTrackAdded); 539 &PeerConnection::OnAudioTrackAdded);
540 observer->SignalAudioTrackRemoved.connect( 540 observer->SignalAudioTrackRemoved.connect(
541 this, &PeerConnection::OnAudioTrackRemoved); 541 this, &PeerConnection::OnAudioTrackRemoved);
542 observer->SignalVideoTrackAdded.connect(this, 542 observer->SignalVideoTrackAdded.connect(this,
543 &PeerConnection::OnVideoTrackAdded); 543 &PeerConnection::OnVideoTrackAdded);
544 observer->SignalVideoTrackRemoved.connect( 544 observer->SignalVideoTrackRemoved.connect(
545 this, &PeerConnection::OnVideoTrackRemoved); 545 this, &PeerConnection::OnVideoTrackRemoved);
546 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); 546 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
547 547
548 for (const auto& track : local_stream->GetAudioTracks()) { 548 for (const auto& track : local_stream->GetAudioTracks()) {
549 OnAudioTrackAdded(track.get(), local_stream); 549 AddAudioTrack(track.get(), local_stream);
550 } 550 }
551 for (const auto& track : local_stream->GetVideoTracks()) { 551 for (const auto& track : local_stream->GetVideoTracks()) {
552 OnVideoTrackAdded(track.get(), local_stream); 552 AddVideoTrack(track.get(), local_stream);
553 } 553 }
554 554
555 stats_->AddStream(local_stream); 555 stats_->AddStream(local_stream);
556 observer_->OnRenegotiationNeeded(); 556 observer_->OnRenegotiationNeeded();
557 return true; 557 return true;
558 } 558 }
559 559
560 void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { 560 void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
561 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); 561 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
562 for (const auto& track : local_stream->GetAudioTracks()) { 562 if (!IsClosed()) {
563 OnAudioTrackRemoved(track.get(), local_stream); 563 for (const auto& track : local_stream->GetAudioTracks()) {
564 RemoveAudioTrack(track.get(), local_stream);
565 }
566 for (const auto& track : local_stream->GetVideoTracks()) {
567 RemoveVideoTrack(track.get(), local_stream);
568 }
564 } 569 }
565 for (const auto& track : local_stream->GetVideoTracks()) {
566 OnVideoTrackRemoved(track.get(), local_stream);
567 }
568
569 local_streams_->RemoveStream(local_stream); 570 local_streams_->RemoveStream(local_stream);
570 stream_observers_.erase( 571 stream_observers_.erase(
571 std::remove_if( 572 std::remove_if(
572 stream_observers_.begin(), stream_observers_.end(), 573 stream_observers_.begin(), stream_observers_.end(),
573 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { 574 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
574 return observer->stream()->label().compare(local_stream->label()) == 575 return observer->stream()->label().compare(local_stream->label()) ==
575 0; 576 0;
576 }), 577 }),
577 stream_observers_.end()); 578 stream_observers_.end());
578 579
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 auto it = FindReceiverForTrack(track_id); 1456 auto it = FindReceiverForTrack(track_id);
1456 if (it == receivers_.end()) { 1457 if (it == receivers_.end()) {
1457 LOG(LS_WARNING) << "RtpReceiver for track with id " << track_id 1458 LOG(LS_WARNING) << "RtpReceiver for track with id " << track_id
1458 << " doesn't exist."; 1459 << " doesn't exist.";
1459 } else { 1460 } else {
1460 (*it)->internal()->Stop(); 1461 (*it)->internal()->Stop();
1461 receivers_.erase(it); 1462 receivers_.erase(it);
1462 } 1463 }
1463 } 1464 }
1464 1465
1466 void PeerConnection::AddAudioTrack(AudioTrackInterface* track,
1467 MediaStreamInterface* stream) {
1468 RTC_DCHECK(!IsClosed());
1469 auto sender = FindSenderForTrack(track);
1470 if (sender != senders_.end()) {
1471 // We already have a sender for this track, so just change the stream_id
1472 // so that it's correct in the next call to CreateOffer.
1473 (*sender)->internal()->set_stream_id(stream->label());
1474 return;
1475 }
1476
1477 // Normal case; we've never seen this track before.
1478 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1479 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1480 signaling_thread(),
1481 new AudioRtpSender(track, stream->label(), session_->voice_channel(),
1482 stats_.get()));
1483 senders_.push_back(new_sender);
1484 // If the sender has already been configured in SDP, we call SetSsrc,
1485 // which will connect the sender to the underlying transport. This can
1486 // occur if a local session description that contains the ID of the sender
1487 // is set before AddStream is called. It can also occur if the local
1488 // session description is not changed and RemoveStream is called, and
1489 // later AddStream is called again with the same stream.
1490 const TrackInfo* track_info =
1491 FindTrackInfo(local_audio_tracks_, stream->label(), track->id());
1492 if (track_info) {
1493 new_sender->internal()->SetSsrc(track_info->ssrc);
1494 }
1495 }
1496
1497 // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
1498 // indefinitely, when we have unified plan SDP.
1499 void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track,
1500 MediaStreamInterface* stream) {
1501 RTC_DCHECK(!IsClosed());
1502 auto sender = FindSenderForTrack(track);
1503 if (sender == senders_.end()) {
1504 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1505 << " doesn't exist.";
1506 return;
1507 }
1508 (*sender)->internal()->Stop();
1509 senders_.erase(sender);
1510 }
1511
1512 void PeerConnection::AddVideoTrack(VideoTrackInterface* track,
1513 MediaStreamInterface* stream) {
1514 RTC_DCHECK(!IsClosed());
1515 auto sender = FindSenderForTrack(track);
1516 if (sender != senders_.end()) {
1517 // We already have a sender for this track, so just change the stream_id
1518 // so that it's correct in the next call to CreateOffer.
1519 (*sender)->internal()->set_stream_id(stream->label());
1520 return;
1521 }
1522
1523 // Normal case; we've never seen this track before.
1524 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1525 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1526 signaling_thread(), new VideoRtpSender(track, stream->label(),
1527 session_->video_channel()));
1528 senders_.push_back(new_sender);
1529 const TrackInfo* track_info =
1530 FindTrackInfo(local_video_tracks_, stream->label(), track->id());
1531 if (track_info) {
1532 new_sender->internal()->SetSsrc(track_info->ssrc);
1533 }
1534 }
1535
1536 void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track,
1537 MediaStreamInterface* stream) {
1538 RTC_DCHECK(!IsClosed());
1539 auto sender = FindSenderForTrack(track);
1540 if (sender == senders_.end()) {
1541 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1542 << " doesn't exist.";
1543 return;
1544 }
1545 (*sender)->internal()->Stop();
1546 senders_.erase(sender);
1547 }
1548
1465 void PeerConnection::OnIceConnectionStateChange( 1549 void PeerConnection::OnIceConnectionStateChange(
1466 PeerConnectionInterface::IceConnectionState new_state) { 1550 PeerConnectionInterface::IceConnectionState new_state) {
1467 RTC_DCHECK(signaling_thread()->IsCurrent()); 1551 RTC_DCHECK(signaling_thread()->IsCurrent());
1468 // After transitioning to "closed", ignore any additional states from 1552 // After transitioning to "closed", ignore any additional states from
1469 // WebRtcSession (such as "disconnected"). 1553 // WebRtcSession (such as "disconnected").
1470 if (IsClosed()) { 1554 if (IsClosed()) {
1471 return; 1555 return;
1472 } 1556 }
1473 ice_connection_state_ = new_state; 1557 ice_connection_state_ = new_state;
1474 observer_->OnIceConnectionChange(ice_connection_state_); 1558 observer_->OnIceConnectionChange(ice_connection_state_);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1522 } 1606 }
1523 } 1607 }
1524 observer_->OnSignalingChange(signaling_state_); 1608 observer_->OnSignalingChange(signaling_state_);
1525 } 1609 }
1526 1610
1527 void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, 1611 void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track,
1528 MediaStreamInterface* stream) { 1612 MediaStreamInterface* stream) {
1529 if (IsClosed()) { 1613 if (IsClosed()) {
1530 return; 1614 return;
1531 } 1615 }
1532 auto sender = FindSenderForTrack(track); 1616 AddAudioTrack(track, stream);
1533 if (sender != senders_.end()) { 1617 observer_->OnRenegotiationNeeded();
1534 // We already have a sender for this track, so just change the stream_id
1535 // so that it's correct in the next call to CreateOffer.
1536 (*sender)->internal()->set_stream_id(stream->label());
1537 return;
1538 }
1539
1540 // Normal case; we've never seen this track before.
1541 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1542 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1543 signaling_thread(),
1544 new AudioRtpSender(track, stream->label(), session_->voice_channel(),
1545 stats_.get()));
1546 senders_.push_back(new_sender);
1547 // If the sender has already been configured in SDP, we call SetSsrc,
1548 // which will connect the sender to the underlying transport. This can
1549 // occur if a local session description that contains the ID of the sender
1550 // is set before AddStream is called. It can also occur if the local
1551 // session description is not changed and RemoveStream is called, and
1552 // later AddStream is called again with the same stream.
1553 const TrackInfo* track_info =
1554 FindTrackInfo(local_audio_tracks_, stream->label(), track->id());
1555 if (track_info) {
1556 new_sender->internal()->SetSsrc(track_info->ssrc);
1557 }
1558 } 1618 }
1559 1619
1560 // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around
1561 // indefinitely, when we have unified plan SDP.
1562 void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, 1620 void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track,
1563 MediaStreamInterface* stream) { 1621 MediaStreamInterface* stream) {
1564 if (IsClosed()) { 1622 if (IsClosed()) {
1565 return; 1623 return;
1566 } 1624 }
1567 auto sender = FindSenderForTrack(track); 1625 RemoveAudioTrack(track, stream);
1568 if (sender == senders_.end()) { 1626 observer_->OnRenegotiationNeeded();
1569 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1570 << " doesn't exist.";
1571 return;
1572 }
1573 (*sender)->internal()->Stop();
1574 senders_.erase(sender);
1575 } 1627 }
1576 1628
1577 void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, 1629 void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track,
1578 MediaStreamInterface* stream) { 1630 MediaStreamInterface* stream) {
1579 if (IsClosed()) { 1631 if (IsClosed()) {
1580 return; 1632 return;
1581 } 1633 }
1582 auto sender = FindSenderForTrack(track); 1634 AddVideoTrack(track, stream);
1583 if (sender != senders_.end()) { 1635 observer_->OnRenegotiationNeeded();
1584 // We already have a sender for this track, so just change the stream_id
1585 // so that it's correct in the next call to CreateOffer.
1586 (*sender)->internal()->set_stream_id(stream->label());
1587 return;
1588 }
1589
1590 // Normal case; we've never seen this track before.
1591 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender =
1592 RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
1593 signaling_thread(), new VideoRtpSender(track, stream->label(),
1594 session_->video_channel()));
1595 senders_.push_back(new_sender);
1596 const TrackInfo* track_info =
1597 FindTrackInfo(local_video_tracks_, stream->label(), track->id());
1598 if (track_info) {
1599 new_sender->internal()->SetSsrc(track_info->ssrc);
1600 }
1601 } 1636 }
1602 1637
1603 void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, 1638 void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track,
1604 MediaStreamInterface* stream) { 1639 MediaStreamInterface* stream) {
1605 if (IsClosed()) { 1640 if (IsClosed()) {
1606 return; 1641 return;
1607 } 1642 }
1608 auto sender = FindSenderForTrack(track); 1643 RemoveVideoTrack(track, stream);
1609 if (sender == senders_.end()) { 1644 observer_->OnRenegotiationNeeded();
1610 LOG(LS_WARNING) << "RtpSender for track with id " << track->id()
1611 << " doesn't exist.";
1612 return;
1613 }
1614 (*sender)->internal()->Stop();
1615 senders_.erase(sender);
1616 } 1645 }
1617 1646
1618 void PeerConnection::PostSetSessionDescriptionFailure( 1647 void PeerConnection::PostSetSessionDescriptionFailure(
1619 SetSessionDescriptionObserver* observer, 1648 SetSessionDescriptionObserver* observer,
1620 const std::string& error) { 1649 const std::string& error) {
1621 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); 1650 SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer);
1622 msg->error = error; 1651 msg->error = error;
1623 signaling_thread()->Post(RTC_FROM_HERE, this, 1652 signaling_thread()->Post(RTC_FROM_HERE, this,
1624 MSG_SET_SESSIONDESCRIPTION_FAILED, msg); 1653 MSG_SET_SESSIONDESCRIPTION_FAILED, msg);
1625 } 1654 }
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
2368 return event_log_->StartLogging(file, max_size_bytes); 2397 return event_log_->StartLogging(file, max_size_bytes);
2369 } 2398 }
2370 2399
2371 void PeerConnection::StopRtcEventLog_w() { 2400 void PeerConnection::StopRtcEventLog_w() {
2372 if (event_log_) { 2401 if (event_log_) {
2373 event_log_->StopLogging(); 2402 event_log_->StopLogging();
2374 } 2403 }
2375 } 2404 }
2376 2405
2377 } // namespace webrtc 2406 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/pc/peerconnection.h ('k') | webrtc/pc/peerconnectioninterface_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698