Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

#68 - Camera does not get reclaimed #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
97 changes: 91 additions & 6 deletions src/android/OpenTokAndroidPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.hardware.camera2.CameraManager;
import android.util.Log;
import android.util.DisplayMetrics;
import android.view.View;
Expand All @@ -45,6 +46,7 @@
import com.opentok.android.Subscriber;
import com.opentok.android.SubscriberKit;
import com.opentok.android.BaseVideoRenderer;
import com.opentok.android.BaseVideoCapturer;

public class OpenTokAndroidPlugin extends CordovaPlugin
implements Session.SessionListener,
Expand Down Expand Up @@ -78,6 +80,8 @@ public class OpenTokAndroidPlugin extends CordovaPlugin
public static final String[] perms = {Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
public CallbackContext permissionsCallback;

public boolean inBackground = false;
public boolean claimedInBackground = false;

public class RunnableUpdateViews implements Runnable {
public JSONArray mProperty;
Expand Down Expand Up @@ -124,6 +128,16 @@ public int getZIndex() {
}
}

public void removeView() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ViewGroup parent = (ViewGroup) webView.getView().getParent();
parent.removeView(mView);
}
});
}

@SuppressLint("NewApi")
@Override
public void run() {
Expand Down Expand Up @@ -169,6 +183,50 @@ public class RunnablePublisher extends RunnableUpdateViews implements
audioFallbackEnabled, audioBitrate, audioSource, videoSource, frameRate, cameraResolution]
*/
public Publisher mPublisher;
public boolean publishing = false;
public boolean oldPublishVideoState = false;

private final CameraManager.AvailabilityCallback cameraAvailableCallback = new CameraManager.AvailabilityCallback() {
@Override
public void onCameraAvailable(String cameraId) {
super.onCameraAvailable(cameraId);
if(mPublisher != null && publishing) {
BaseVideoCapturer bvc = mPublisher.getCapturer();
// If not yet capturing.. Initialize and start capturing..
if (bvc != null && !bvc.isCaptureStarted()) {
Log.i(TAG, "Camera available");
try {
// Camera claimed in background.
if(inBackground) {
claimedInBackground = true;
}
bvc.init();
bvc.startCapture();
mPublisher.setPublishVideo(oldPublishVideoState);
} catch(Exception e) {

}
}
}
}

@Override
public void onCameraUnavailable(String cameraId) {
super.onCameraUnavailable(cameraId);
// If app is in background, and not camera yet claimed by us in background.. release camera.
if(mPublisher != null && inBackground && !claimedInBackground) {
BaseVideoCapturer bvc = mPublisher.getCapturer();
// If still capturing, stop it and release.
if(bvc != null && bvc.isCaptureStarted() ){
Log.i(TAG, "Camera unavailable");
oldPublishVideoState = mPublisher.getPublishVideo();
bvc.stopCapture();
bvc.destroy();
mPublisher.setPublishVideo(false);
}
}
}
};

public RunnablePublisher(JSONArray args) {
this.mProperty = args;
Expand Down Expand Up @@ -228,23 +286,32 @@ public RunnablePublisher(JSONArray args) {
mPublisher.setPublishVideo(publishVideo);
mPublisher.setPublishAudio(publishAudio);

oldPublishVideoState = publishVideo;

if (cameraName.equals("back")) {
mPublisher.cycleCamera();
}

CameraManager manager = (CameraManager) cordova.getActivity().getSystemService(Context.CAMERA_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.registerAvailabilityCallback(cameraAvailableCallback, null);
}
}


public void setPropertyFromArray(JSONArray args) {
this.mProperty = args;
}

public void startPublishing() {
publishing = true;
mSession.publish(mPublisher);
cordova.getActivity().runOnUiThread(this);
}

public void stopPublishing() {
ViewGroup parent = (ViewGroup) webView.getView().getParent();
parent.removeView(this.mView);
publishing = false;
this.removeView();
if(this.mPublisher != null){
try {
mSession.unpublish(this.mPublisher);
Expand All @@ -255,8 +322,12 @@ public void stopPublishing() {
}

public void destroyPublisher() {
ViewGroup parent = (ViewGroup) webView.getView().getParent();
parent.removeView(this.mView);
CameraManager manager = (CameraManager) cordova.getActivity().getSystemService(Context.CAMERA_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.unregisterAvailabilityCallback(cameraAvailableCallback);
}

this.removeView();
if (this.mPublisher != null) {
this.mPublisher.destroy();
this.mPublisher = null;
Expand Down Expand Up @@ -361,8 +432,7 @@ public void setPropertyFromArray(JSONArray args) {
}

public void removeStreamView() {
ViewGroup parent = (ViewGroup) webView.getView().getParent();
parent.removeView(this.mView);
this.removeView();
if(mSubscriber != null) {
try {
mSession.unsubscribe(mSubscriber);
Expand Down Expand Up @@ -678,6 +748,21 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
}
}

@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
inBackground = true;
Log.i(TAG, "Pause");
}

@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
inBackground = false;
Log.i(TAG, "Resume");
claimedInBackground = false; // We are on foreground, so its now claimed in foreground.
}

public void alertUser(String message) {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(cordova.getActivity());
Expand Down