Skip to content

Commit d679240

Browse files
committed
Updated releaseResource() and usage
1 parent 45911f4 commit d679240

File tree

1 file changed

+49
-34
lines changed

1 file changed

+49
-34
lines changed

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

+49-34
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

@@ -385,40 +433,7 @@ public void onHostPause() {
385433

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

424439
// ====== CONNECTING ===========================================================================

0 commit comments

Comments
 (0)