@@ -751,6 +751,8 @@ void PeerConnection::remoteCloseDataChannels() {
751
751
}
752
752
753
753
shared_ptr<Track> PeerConnection::emplaceTrack (Description::Media description) {
754
+ std::unique_lock lock (mTracksMutex ); // we are going to emplace
755
+
754
756
#if !RTC_ENABLE_MEDIA
755
757
// No media support, mark as removed
756
758
PLOG_WARNING << " Tracks are disabled (not compiled with media support)" ;
@@ -759,10 +761,12 @@ shared_ptr<Track> PeerConnection::emplaceTrack(Description::Media description) {
759
761
760
762
shared_ptr<Track> track;
761
763
if (auto it = mTracks .find (description.mid ()); it != mTracks .end ())
762
- if (track = it->second .lock (); track )
763
- track-> setDescription ( std::move (description) );
764
+ if (auto t = it->second .lock (); t && !t-> isClosed () )
765
+ track = std::move (t );
764
766
765
- if (!track) {
767
+ if (track) {
768
+ track->setDescription (std::move (description));
769
+ } else {
766
770
track = std::make_shared<Track>(weak_from_this (), std::move (description));
767
771
mTracks .emplace (std::make_pair (track->mid (), track));
768
772
mTrackLines .emplace_back (track);
@@ -896,7 +900,7 @@ void PeerConnection::processLocalDescription(Description description) {
896
900
description.addMedia (std::move (reciprocated));
897
901
},
898
902
[&](Description::Media *remoteMedia) {
899
- std::shared_lock lock (mTracksMutex );
903
+ std::unique_lock lock (mTracksMutex ); // we may emplace a track
900
904
if (auto it = mTracks .find (remoteMedia->mid ()); it != mTracks .end ()) {
901
905
// Prefer local description
902
906
if (auto track = it->second .lock ()) {
0 commit comments