Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,25 @@ void setPeerConnection(PeerConnection peerConnection) {
}

void close() {
Log.d(TAG, "PeerConnection.close() for " + id);
final PeerConnection pc = peerConnection;
if (pc == null) {
Log.d(TAG, "PeerConnection.close() for " + id + " ignored; already disposed");
return;
}

peerConnection.close();
Log.d(TAG, "PeerConnection.close() for " + id);
pc.close();
}

void dispose() {
final PeerConnection pc = peerConnection;
peerConnection = null;

if (pc == null) {
Log.d(TAG, "PeerConnection.dispose() for " + id + " ignored; already disposed");
return;
}

Log.d(TAG, "PeerConnection.dispose() for " + id);

// Remove track adapters for remote tracks
Expand All @@ -85,7 +98,7 @@ void dispose() {
}

// Remove video track adapters for local tracks (from senders)
for (RtpSender sender : this.peerConnection.getSenders()) {
for (RtpSender sender : pc.getSenders()) {
MediaStreamTrack track = sender.track();
if (track instanceof VideoTrack) {
videoTrackAdapters.removeAdapter((VideoTrack) track);
Expand All @@ -102,7 +115,7 @@ void dispose() {
// At this point there should be no local MediaStreams in the associated
// PeerConnection. Call dispose() to free all remaining resources held
// by the PeerConnection instance (RtpReceivers, RtpSenders, etc.)
peerConnection.dispose();
pc.dispose();

videoTrackAdapters.dispose();

Expand Down Expand Up @@ -305,7 +318,13 @@ public void onIceCandidate(final IceCandidate candidate) {

params.putMap("candidate", candidateParams);

SessionDescription newSdp = peerConnection.getLocalDescription();
final PeerConnection pc = peerConnection;
if (pc == null) {
Log.d(TAG, "onIceCandidate for " + id + " skipped; peer connection already disposed");
return;
}

SessionDescription newSdp = pc.getLocalDescription();
WritableMap newSdpMap = Arguments.createMap();

// Can happen when doing a rollback.
Expand Down Expand Up @@ -356,7 +375,13 @@ public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringSt
params.putString("iceGatheringState", iceGatheringStateString(iceGatheringState));

if (iceGatheringState == PeerConnection.IceGatheringState.COMPLETE) {
SessionDescription newSdp = peerConnection.getLocalDescription();
final PeerConnection pc = peerConnection;
if (pc == null) {
Log.d(TAG,
"onIceGatheringChange for " + id + " skipped; peer connection already disposed");
return;
}
SessionDescription newSdp = pc.getLocalDescription();
WritableMap newSdpMap = Arguments.createMap();

// Can happen when doing a rollback.
Expand Down Expand Up @@ -429,8 +454,14 @@ public void onAddTrack(final RtpReceiver receiver, final MediaStream[] mediaStre
Log.d(TAG, "onAddTrack");

ThreadUtils.runOnExecutor(() -> {
final PeerConnection pc = peerConnection;
if (pc == null) {
Log.d(TAG, "onAddTrack for " + id + " skipped; peer connection already disposed");
return;
}

RtpTransceiver transceiver = null;
for (RtpTransceiver t : this.peerConnection.getTransceivers()) {
for (RtpTransceiver t : pc.getTransceivers()) {
if (Objects.equals(t.getReceiver().id(), receiver.id())) {
transceiver = t;
break;
Expand Down Expand Up @@ -502,6 +533,11 @@ public void onTrack(final RtpTransceiver transceiver) {}
@Override
public void onRemoveTrack(RtpReceiver receiver) {
ThreadUtils.runOnExecutor(() -> {
if (peerConnection == null) {
Log.d(TAG, "onRemoveTrack for " + id + " skipped; peer connection already disposed");
return;
}

// Tear down track adapters so a subsequent onAddTrack with the
// same trackId (SFU participant rejoin) creates a fresh adapter
// on the new MediaStreamTrack object. Without this, the old sink
Expand Down
Loading
Loading