Skip to content

Commit 645b023

Browse files
committed
Updated releaseResource() and usage
1 parent cf5dc0c commit 645b023

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,56 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
208208
private final Map<RemoteDataTrack, RemoteParticipant> dataTrackRemoteParticipantMap =
209209
new HashMap<>();
210210

211+
public void releaseResource() {
212+
thumbnailVideoView = null;
213+
roomName = null;
214+
accessToken = null;
215+
216+
/*
217+
* Remove stream voice control
218+
*/
219+
if (themedReactContext != null && themedReactContext.getCurrentActivity() != null) {
220+
themedReactContext.getCurrentActivity().setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
221+
themedReactContext.removeLifecycleEventListener(this);
222+
}
223+
/*
224+
* Always disconnect from the room before leaving the Activity to
225+
* ensure any memory allocated to the Room resource is freed.
226+
*/
227+
if (room != null && room.getState() != Room.State.DISCONNECTED) {
228+
room.disconnect();
229+
disconnectedFromOnDestroy = true;
230+
}
231+
room = null;
232+
233+
234+
if (localParticipant != null) {
235+
localParticipant.unpublishTrack(localVideoTrack);
236+
localParticipant = null;
237+
}
238+
239+
if (localVideoTrack != null) {
240+
localVideoTrack.release();
241+
localVideoTrack = null;
242+
}
243+
244+
if (localAudioTrack != null) {
245+
localAudioTrack.release();
246+
localAudioTrack = null;
247+
}
248+
249+
if (cameraCapturer != null) {
250+
cameraCapturer.stopCapture();
251+
cameraCapturer = null;
252+
}
253+
254+
// Quit the data track message thread
255+
dataTrackMessageThread.quit();
256+
}
257+
211258
public CustomTwilioVideoView(ThemedReactContext context) {
212259
super(context);
260+
releaseResource();
213261
this.themedReactContext = context;
214262
this.eventEmitter = themedReactContext.getJSModule(RCTEventEmitter.class);
215263

@@ -382,46 +430,7 @@ public void onHostPause() {
382430

383431
@Override
384432
public void onHostDestroy() {
385-
/*
386-
* Remove stream voice control
387-
*/
388-
if (themedReactContext.getCurrentActivity() != null) {
389-
themedReactContext.getCurrentActivity().setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
390-
}
391-
/*
392-
* Always disconnect from the room before leaving the Activity to
393-
* ensure any memory allocated to the Room resource is freed.
394-
*/
395-
if (room != null && room.getState() != Room.State.DISCONNECTED) {
396-
room.disconnect();
397-
disconnectedFromOnDestroy = true;
398-
}
399-
400-
/*
401-
* Release the local media ensuring any memory allocated to audio or video is freed.
402-
*/
403-
if (localVideoTrack != null) {
404-
localVideoTrack.release();
405-
localVideoTrack = null;
406-
}
407-
408-
if (localAudioTrack != null) {
409-
localAudioTrack.release();
410-
localAudioTrack = null;
411-
}
412-
413-
// Quit the data track message thread
414-
dataTrackMessageThread.quit();
415-
416-
417-
}
418-
419-
public void releaseResource() {
420-
themedReactContext.removeLifecycleEventListener(this);
421-
room = null;
422-
localVideoTrack = null;
423-
thumbnailVideoView = null;
424-
cameraCapturer = null;
433+
releaseResource();
425434
}
426435

427436
// ====== CONNECTING ===========================================================================

0 commit comments

Comments
 (0)