Skip to content

Commit

Permalink
fix: add track release for react-native whenever track stop is called
Browse files Browse the repository at this point in the history
  • Loading branch information
santhoshvai committed Oct 10, 2024
1 parent 3306793 commit 7ba0a9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
9 changes: 8 additions & 1 deletion packages/client/src/devices/InputMediaDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,14 @@ export abstract class InputMediaDeviceManager<

private stopTracks() {
this.getTracks().forEach((track) => {
if (track.readyState === 'live') track.stop();
if (track.readyState === 'live') {
track.stop();
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof track.release === 'function') {
// @ts-expect-error
track.release();
}
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/client/src/devices/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ export const disposeOfMediaStream = (stream: MediaStream) => {
stream.getTracks().forEach((track) => {
track.stop();
stream.removeTrack(track);
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof track.release === 'function') {
// @ts-expect-error
track.release();
}
});
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the stream
if (typeof stream.release === 'function') {
Expand Down
9 changes: 8 additions & 1 deletion packages/client/src/helpers/RNSpeechDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ export class RNSpeechDetector {
if (!this.audioStream) {
return;
}
this.audioStream.getTracks().forEach((track) => track.stop());
this.audioStream.getTracks().forEach((track) => {
track.stop();
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof track.release === 'function') {
// @ts-expect-error
track.release();
}
});
if (
// @ts-expect-error release() is present in react-native-webrtc
typeof this.audioStream.release === 'function'
Expand Down
35 changes: 24 additions & 11 deletions packages/client/src/rtc/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ export class Publisher {
if (previousTrack && previousTrack !== track) {
previousTrack.stop();
previousTrack.removeEventListener('ended', handleTrackEnded);
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof previousTrack.release === 'function') {
// @ts-expect-error
track.release();
}
track.addEventListener('ended', handleTrackEnded);
}
if (!track.enabled) {
Expand All @@ -337,16 +342,18 @@ export class Publisher {
const transceiver = this.pc
.getTransceivers()
.find((t) => t === this.transceiverRegistry[trackType] && t.sender.track);
if (
transceiver &&
transceiver.sender.track &&
(stopTrack
? transceiver.sender.track.readyState === 'live'
: transceiver.sender.track.enabled)
) {
stopTrack
? transceiver.sender.track.stop()
: (transceiver.sender.track.enabled = false);
const track = transceiver?.sender.track;
if (track && (stopTrack ? track.readyState === 'live' : track.enabled)) {
if (stopTrack) {
track.stop();
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof track.release === 'function') {
// @ts-expect-error
track.release();
}
} else {
track.enabled = false;
}
// We don't need to notify SFU if unpublishing in response to remote soft mute
if (this.state.localParticipant?.publishedTracks.includes(trackType)) {
await this.notifyTrackMuteStateChanged(undefined, trackType, true);
Expand Down Expand Up @@ -399,7 +406,13 @@ export class Publisher {
private stopPublishing = () => {
this.logger('debug', 'Stopping publishing all tracks');
this.pc.getSenders().forEach((s) => {
s.track?.stop();
const track = s.track;
track?.stop();
// @ts-expect-error release() is present in react-native-webrtc and must be called to dispose the track
if (typeof track?.release === 'function') {
// @ts-expect-error
track.release();
}
if (this.pc.signalingState !== 'closed') {
this.pc.removeTrack(s);
}
Expand Down

0 comments on commit 7ba0a9e

Please sign in to comment.