Skip to content

fix: don't dispose the mediaSource when disposing a cloned track #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2025
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 @@ -86,7 +86,9 @@ public void startCapture() {

public boolean stopCapture() {
try {
videoCapturer.stopCapture();
if (videoCapturer != null) {
videoCapturer.stopCapture();
}
return true;
} catch (InterruptedException e) {
return false;
Expand Down
43 changes: 37 additions & 6 deletions android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ void mediaStreamTrackSetEnabled(String trackId, final boolean enabled) {
if (enabled) {
track.videoCaptureController.startCapture();
} else {
track.videoCaptureController.stopCapture();
if (!track.isClone()) {
track.videoCaptureController.stopCapture();
}
}
}
}
Expand Down Expand Up @@ -419,17 +421,28 @@ MediaStreamTrack cloneTrack(String trackId) {
if (track == null) {
throw new IllegalArgumentException("No track found for id: " + trackId);
}

PeerConnectionFactory pcFactory = webRTCModule.mFactory;

String id = UUID.randomUUID().toString();
MediaStreamTrack nativeTrack = track.track;
MediaStreamTrack clonedNativeTrack;
final MediaStreamTrack clonedNativeTrack;
if (nativeTrack instanceof VideoTrack) {
clonedNativeTrack = pcFactory.createVideoTrack(id, (VideoSource) track.mediaSource);
} else {
clonedNativeTrack = pcFactory.createAudioTrack(id, (AudioSource) track.mediaSource);
}
clonedNativeTrack.setEnabled(nativeTrack.enabled());
tracks.put(id, new TrackPrivate(clonedNativeTrack, track.mediaSource, track.videoCaptureController, track.surfaceTextureHelper));

final TrackPrivate clone = new TrackPrivate(
clonedNativeTrack,
track.mediaSource,
track.videoCaptureController,
track.surfaceTextureHelper
);
clone.setParent(track);
tracks.put(id, clone);

return clonedNativeTrack;
}

Expand Down Expand Up @@ -494,6 +507,11 @@ private static class TrackPrivate {
*/
private boolean disposed;

/**
* Whether this object is a clone of another object.
*/
private TrackPrivate parent = null;

/**
* Initializes a new {@code TrackPrivate} instance.
*
Expand All @@ -514,8 +532,9 @@ public TrackPrivate(MediaStreamTrack track, MediaSource mediaSource,
}

public void dispose() {
final boolean isClone = this.isClone();
if (!disposed) {
if (videoCaptureController != null) {
if (!isClone && videoCaptureController != null) {
if (videoCaptureController.stopCapture()) {
videoCaptureController.dispose();
}
Expand All @@ -527,16 +546,28 @@ public void dispose() {
* called. This also means that the caller can reuse the SurfaceTextureHelper to initialize a new
* VideoCapturer once the previous VideoCapturer has been disposed. */

if (surfaceTextureHelper != null) {
if (!isClone && surfaceTextureHelper != null) {
surfaceTextureHelper.stopListening();
surfaceTextureHelper.dispose();
}

mediaSource.dispose();
// clones should not dispose the mediaSource as that will affect the original track
// and other clones as well (since they share the same mediaSource).
if (!isClone) {
mediaSource.dispose();
}
track.dispose();
disposed = true;
}
}

public void setParent(TrackPrivate parent) {
this.parent = parent;
}

public boolean isClone() {
return this.parent != null;
}
}

public interface BiConsumer<T, U> { void accept(T t, U u); }
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stream-io/react-native-webrtc",
"version": "125.0.7",
"version": "125.0.8-rc.2",
"repository": {
"type": "git",
"url": "git+https://github.com/GetStream/react-native-webrtc.git"
Expand Down
Loading