@@ -715,13 +715,10 @@ void PeerConnection::iterateDataChannels(
715
715
{
716
716
std::shared_lock lock (mDataChannelsMutex ); // read-only
717
717
locked.reserve (mDataChannels .size ());
718
- auto it = mDataChannels .begin ();
719
- while (it != mDataChannels .end ()) {
718
+ for (auto it = mDataChannels .begin (); it != mDataChannels .end (); ++it) {
720
719
auto channel = it->second .lock ();
721
720
if (channel && !channel->isClosed ())
722
721
locked.push_back (std::move (channel));
723
-
724
- ++it;
725
722
}
726
723
}
727
724
@@ -751,6 +748,8 @@ void PeerConnection::remoteCloseDataChannels() {
751
748
}
752
749
753
750
shared_ptr<Track> PeerConnection::emplaceTrack (Description::Media description) {
751
+ std::unique_lock lock (mTracksMutex ); // we are going to emplace
752
+
754
753
#if !RTC_ENABLE_MEDIA
755
754
// No media support, mark as removed
756
755
PLOG_WARNING << " Tracks are disabled (not compiled with media support)" ;
@@ -759,10 +758,12 @@ shared_ptr<Track> PeerConnection::emplaceTrack(Description::Media description) {
759
758
760
759
shared_ptr<Track> track;
761
760
if (auto it = mTracks .find (description.mid ()); it != mTracks .end ())
762
- if (track = it->second .lock (); track )
763
- track-> setDescription ( std::move (description) );
761
+ if (auto t = it->second .lock (); t && !t-> isClosed () )
762
+ track = std::move (t );
764
763
765
- if (!track) {
764
+ if (track) {
765
+ track->setDescription (std::move (description));
766
+ } else {
766
767
track = std::make_shared<Track>(weak_from_this (), std::move (description));
767
768
mTracks .emplace (std::make_pair (track->mid (), track));
768
769
mTrackLines .emplace_back (track);
@@ -779,15 +780,22 @@ shared_ptr<Track> PeerConnection::emplaceTrack(Description::Media description) {
779
780
}
780
781
781
782
void PeerConnection::iterateTracks (std::function<void (shared_ptr<Track> track)> func) {
782
- std::shared_lock lock (mTracksMutex ); // read-only
783
- for (auto it = mTrackLines .begin (); it != mTrackLines .end (); ++it) {
784
- auto track = it->lock ();
785
- if (track && !track->isClosed ()) {
786
- try {
787
- func (std::move (track));
788
- } catch (const std::exception &e) {
789
- PLOG_WARNING << e.what ();
790
- }
783
+ std::vector<shared_ptr<Track>> locked;
784
+ {
785
+ std::shared_lock lock (mTracksMutex ); // read-only
786
+ locked.reserve (mTrackLines .size ());
787
+ for (auto it = mTrackLines .begin (); it != mTrackLines .end (); ++it) {
788
+ auto track = it->lock ();
789
+ if (track && !track->isClosed ())
790
+ locked.push_back (std::move (track));
791
+ }
792
+ }
793
+
794
+ for (auto &track : locked) {
795
+ try {
796
+ func (std::move (track));
797
+ } catch (const std::exception &e) {
798
+ PLOG_WARNING << e.what ();
791
799
}
792
800
}
793
801
}
@@ -896,7 +904,7 @@ void PeerConnection::processLocalDescription(Description description) {
896
904
description.addMedia (std::move (reciprocated));
897
905
},
898
906
[&](Description::Media *remoteMedia) {
899
- std::shared_lock lock (mTracksMutex );
907
+ std::unique_lock lock (mTracksMutex ); // we may emplace a track
900
908
if (auto it = mTracks .find (remoteMedia->mid ()); it != mTracks .end ()) {
901
909
// Prefer local description
902
910
if (auto track = it->second .lock ()) {
0 commit comments