@@ -925,7 +925,7 @@ void PeerConnection::validateRemoteDescription(const Description &description) {
925
925
PLOG_VERBOSE << " Remote description looks valid" ;
926
926
}
927
927
928
- void PeerConnection::processLocalDescription (Description description) {
928
+ void PeerConnection::populateLocalDescription (Description & description) const {
929
929
const uint16_t localSctpPort = DEFAULT_SCTP_PORT;
930
930
const size_t localMaxMessageSize =
931
931
config.maxMessageSize .value_or (DEFAULT_LOCAL_MAX_MESSAGE_SIZE);
@@ -935,7 +935,7 @@ void PeerConnection::processLocalDescription(Description description) {
935
935
936
936
if (auto remote = remoteDescription ()) {
937
937
// Reciprocate remote description
938
- for (int i = 0 ; i < remote->mediaCount (); ++i)
938
+ for (int i = 0 ; i < remote->mediaCount (); ++i) {
939
939
std::visit ( // reciprocate each media
940
940
rtc::overloaded{
941
941
[&](Description::Application *remoteApp) {
@@ -950,78 +950,46 @@ void PeerConnection::processLocalDescription(Description description) {
950
950
<< app.mid () << " \" " ;
951
951
952
952
description.addMedia (std::move (app));
953
- return ;
954
- }
955
953
956
- auto reciprocated = remoteApp->reciprocate ();
957
- reciprocated.hintSctpPort (localSctpPort);
958
- reciprocated.setMaxMessageSize (localMaxMessageSize);
954
+ } else {
955
+ auto reciprocated = remoteApp->reciprocate ();
956
+ reciprocated.hintSctpPort (localSctpPort);
957
+ reciprocated.setMaxMessageSize (localMaxMessageSize);
959
958
960
- PLOG_DEBUG << " Reciprocating application in local description, mid=\" "
961
- << reciprocated.mid () << " \" " ;
959
+ PLOG_DEBUG << " Reciprocating application in local description, mid=\" "
960
+ << reciprocated.mid () << " \" " ;
962
961
963
- description.addMedia (std::move (reciprocated));
962
+ description.addMedia (std::move (reciprocated));
963
+ }
964
964
},
965
965
[&](Description::Media *remoteMedia) {
966
- std::unique_lock lock (mTracksMutex ); // we may emplace a track
967
- if (auto it = mTracks .find (remoteMedia->mid ()); it != mTracks .end ()) {
968
- // Prefer local description
969
- if (auto track = it->second .lock ()) {
970
- auto media = track->description ();
971
-
972
- PLOG_DEBUG << " Adding media to local description, mid=\" "
973
- << media.mid () << " \" , removed=" << std::boolalpha
974
- << media.isRemoved ();
975
-
976
- description.addMedia (std::move (media));
977
-
978
- } else {
979
- auto reciprocated = remoteMedia->reciprocate ();
980
- reciprocated.markRemoved ();
966
+ std::shared_lock lock (mTracksMutex );
967
+ auto it = mTracks .find (remoteMedia->mid ());
968
+ auto track = it != mTracks .end () ? it->second .lock () : nullptr ;
969
+ if (track) {
970
+ // Prefer local description
971
+ auto media = track->description ();
981
972
982
- PLOG_DEBUG << " Adding media to local description, mid=\" "
983
- << reciprocated.mid ()
984
- << " \" , removed=true (track is destroyed)" ;
985
-
986
- description.addMedia (std::move (reciprocated));
987
- }
988
- return ;
989
- }
990
-
991
- auto reciprocated = remoteMedia->reciprocate ();
992
- #if !RTC_ENABLE_MEDIA
993
- if (!reciprocated.isRemoved ()) {
994
- // No media support, mark as removed
995
- PLOG_WARNING << " Rejecting track (not compiled with media support)" ;
996
- reciprocated.markRemoved ();
997
- }
998
- #endif
973
+ PLOG_DEBUG << " Adding media to local description, mid=\" "
974
+ << media.mid () << " \" , removed=" << std::boolalpha
975
+ << media.isRemoved ();
999
976
1000
- PLOG_DEBUG << " Reciprocating media in local description, mid=\" "
1001
- << reciprocated.mid () << " \" , removed=" << std::boolalpha
1002
- << reciprocated.isRemoved ();
977
+ description.addMedia (std::move (media));
1003
978
1004
- // Create incoming track
1005
- auto track =
1006
- std::make_shared<Track>(weak_from_this (), std::move (reciprocated));
1007
- mTracks .emplace (std::make_pair (track->mid (), track));
1008
- mTrackLines .emplace_back (track);
1009
- triggerTrack (track); // The user may modify the track description
979
+ } else {
980
+ auto reciprocated = remoteMedia->reciprocate ();
981
+ reciprocated.markRemoved ();
1010
982
1011
- auto handler = getMediaHandler ();
1012
- if (handler )
1013
- handler-> media (track-> description ()) ;
983
+ PLOG_DEBUG << " Adding media to local description, mid= \" "
984
+ << reciprocated. mid ( )
985
+ << " \" , removed=true (track is destroyed) " ;
1014
986
1015
- if (track->description ().isRemoved ())
1016
- track->close ();
1017
-
1018
- description.addMedia (track->description ());
987
+ description.addMedia (std::move (reciprocated));
988
+ }
1019
989
},
1020
990
},
1021
991
remote->media (i));
1022
-
1023
- // We need to update the SSRC cache for newly-created incoming tracks
1024
- updateTrackSsrcCache (*remote);
992
+ }
1025
993
}
1026
994
1027
995
if (description.type () == Description::Type::Offer) {
@@ -1061,22 +1029,18 @@ void PeerConnection::processLocalDescription(Description description) {
1061
1029
description.addMedia (std::move (app));
1062
1030
}
1063
1031
}
1064
-
1065
- // There might be no media at this point, for instance if the user deleted tracks
1066
- if (description.mediaCount () == 0 )
1067
- throw std::runtime_error (" No DataChannel or Track to negotiate" );
1068
1032
}
1069
1033
1070
1034
// Set local fingerprint (wait for certificate if necessary)
1071
1035
description.setFingerprint (mCertificate .get ()->fingerprint ());
1036
+ }
1072
1037
1038
+ void PeerConnection::processLocalDescription (Description description) {
1073
1039
PLOG_VERBOSE << " Issuing local description: " << description;
1074
1040
1075
1041
if (description.mediaCount () == 0 )
1076
1042
throw std::logic_error (" Local description has no media line" );
1077
1043
1078
- updateTrackSsrcCache (description);
1079
-
1080
1044
{
1081
1045
// Set as local description
1082
1046
std::lock_guard lock (mLocalDescriptionMutex );
@@ -1093,11 +1057,6 @@ void PeerConnection::processLocalDescription(Description description) {
1093
1057
1094
1058
mProcessor .enqueue (&PeerConnection::trigger<Description>, shared_from_this (),
1095
1059
&localDescriptionCallback, std::move (description));
1096
-
1097
- // Reciprocated tracks might need to be open
1098
- if (auto dtlsTransport = std::atomic_load (&mDtlsTransport );
1099
- dtlsTransport && dtlsTransport->state () == Transport::State::Connected)
1100
- mProcessor .enqueue (&PeerConnection::openTracks, shared_from_this ());
1101
1060
}
1102
1061
1103
1062
void PeerConnection::processLocalCandidate (Candidate candidate) {
@@ -1121,6 +1080,40 @@ void PeerConnection::processLocalCandidate(Candidate candidate) {
1121
1080
}
1122
1081
1123
1082
void PeerConnection::processRemoteDescription (Description description) {
1083
+ // Create tracks from remote description
1084
+ for (int i = 0 ; i < description.mediaCount (); ++i) {
1085
+ if (std::holds_alternative<Description::Media *>(description.media (i))) {
1086
+ auto remoteMedia = std::get<Description::Media *>(description.media (i));
1087
+ std::unique_lock lock (mTracksMutex ); // we may emplace a track
1088
+ if (auto it = mTracks .find (remoteMedia->mid ()); it != mTracks .end ())
1089
+ continue ;
1090
+
1091
+ PLOG_DEBUG << " New remote track, mid=\" " << remoteMedia->mid () << " \" " ;
1092
+
1093
+ auto reciprocated = remoteMedia->reciprocate ();
1094
+ #if !RTC_ENABLE_MEDIA
1095
+ if (!reciprocated.isRemoved ()) {
1096
+ // No media support, mark as removed
1097
+ PLOG_WARNING << " Rejecting track (not compiled with media support)" ;
1098
+ reciprocated.markRemoved ();
1099
+ }
1100
+ #endif
1101
+
1102
+ // Create incoming track
1103
+ auto track = std::make_shared<Track>(weak_from_this (), std::move (reciprocated));
1104
+ mTracks .emplace (std::make_pair (track->mid (), track));
1105
+ mTrackLines .emplace_back (track);
1106
+ triggerTrack (track); // The user may modify the track description
1107
+
1108
+ auto handler = getMediaHandler ();
1109
+ if (handler)
1110
+ handler->media (track->description ());
1111
+
1112
+ if (track->description ().isRemoved ())
1113
+ track->close ();
1114
+ }
1115
+ }
1116
+
1124
1117
// Update the SSRC cache for existing tracks
1125
1118
updateTrackSsrcCache (description);
1126
1119
@@ -1146,9 +1139,9 @@ void PeerConnection::processRemoteDescription(Description description) {
1146
1139
mProcessor .enqueue (&PeerConnection::remoteCloseDataChannels, shared_from_this ());
1147
1140
}
1148
1141
1142
+ // Reciprocated tracks might need to be open
1149
1143
if (dtlsTransport && dtlsTransport->state () == Transport::State::Connected)
1150
1144
mProcessor .enqueue (&PeerConnection::openTracks, shared_from_this ());
1151
-
1152
1145
}
1153
1146
1154
1147
void PeerConnection::processRemoteCandidate (Candidate candidate) {
@@ -1238,7 +1231,7 @@ void PeerConnection::setMediaHandler(shared_ptr<MediaHandler> handler) {
1238
1231
mMediaHandler = handler;
1239
1232
}
1240
1233
1241
- shared_ptr<MediaHandler> PeerConnection::getMediaHandler () {
1234
+ shared_ptr<MediaHandler> PeerConnection::getMediaHandler () const {
1242
1235
std::shared_lock lock (mMediaHandlerMutex );
1243
1236
return mMediaHandler ;
1244
1237
}
0 commit comments