Skip to content

Commit

Permalink
opentok#86 - Current implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochum van der Ploeg committed Apr 25, 2018
1 parent 15ec34c commit 8ff1b67
Showing 1 changed file with 94 additions and 15 deletions.
109 changes: 94 additions & 15 deletions src/android/OpenTokAndroidPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.lang.Math;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -21,11 +22,17 @@
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;
import android.util.DisplayMetrics;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
Expand Down Expand Up @@ -78,12 +85,80 @@ public class OpenTokAndroidPlugin extends CordovaPlugin
public static final String[] perms = {Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
public CallbackContext permissionsCallback;

public class CustomView extends ViewGroup {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
TextureView view;

public CustomView(Context context, TextureView textureView) {
super(context);
this.view = textureView;
this.setClipChildren(true);
this.addView(this.view);
}

public void setPosition(int xPos, int yPos, int width, int height) {
this.x = xPos;
this.y = yPos;
this.width = width;
this.height = height;

DisplayMetrics metrics = new DisplayMetrics();
cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

int videoWidth = metrics.widthPixels;
int videoHeight = metrics.heightPixels;
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
videoWidth = metrics.heightPixels;
videoHeight = metrics.widthPixels;
}

float videoRatio = (videoHeight / videoWidth);
float containerRatio = (height / width);

float scale = Math.max((float) width / videoWidth, (float) height / videoHeight);

Matrix matrix = new Matrix();
this.view.getTransform(matrix);
matrix.setScale(scale, scale);

float scaledWidth = width * scale;
float scaledHeight = height * scale;
matrix.postTranslate(xPos, yPos);
this.view.setTransform(matrix);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
for(int i=0; i < childCount;i++) {
View v = getChildAt(i);
v.layout(l, t, r, b);
}
}

@Override
protected void dispatchDraw(Canvas canvas) {
int save = canvas.save();
Path clipPath = new Path();
clipPath.addRect(new RectF(this.x, this.y, this.width + this.x, this.height + this.y), Path.Direction.CW);
canvas.clipPath(clipPath);
super.dispatchDraw(canvas);
canvas.restoreToCount(save);
}
}

public class RunnableUpdateViews implements Runnable {
public JSONArray mProperty;
public View mView;
public CustomView mView;
public ArrayList<RunnableUpdateViews> allStreamViews;

// Used for setting the camera views.
public float widthRatio;
public float heightRatio;

public class CustomComparator implements Comparator<RunnableUpdateViews> {
@Override
public int compare(RunnableUpdateViews object1, RunnableUpdateViews object2) {
Expand Down Expand Up @@ -124,15 +199,19 @@ public int getZIndex() {
}
}

public void setPosition() {
try {
this.mView.setPosition((int) (mProperty.getInt(2) * widthRatio), (int) (mProperty.getInt(1) * heightRatio), (int) (mProperty.getInt(3) * widthRatio), (int) (mProperty.getInt(4) * heightRatio));
} catch (Exception e) {}
}

@SuppressLint("NewApi")
@Override
public void run() {
try {
Log.i(TAG, "updating view in ui runnable" + mProperty.toString());
Log.i(TAG, "updating view in ui runnable " + mView.toString());

float widthRatio, heightRatio;

// Ratios are index 6 & 7 on TB.updateViews, 8 & 9 on subscribe event, and 9 & 10 on TB.initPublisher
int ratioIndex;
if (mProperty.get(6) instanceof Number) {
Expand All @@ -148,12 +227,7 @@ public void run() {

widthRatio = (float) mProperty.getDouble(ratioIndex) * metrics.density;
heightRatio = (float) mProperty.getDouble(ratioIndex + 1) * metrics.density;
mView.setY(mProperty.getInt(1) * heightRatio);
mView.setX(mProperty.getInt(2) * widthRatio);
ViewGroup.LayoutParams params = mView.getLayoutParams();
params.height = (int) (mProperty.getInt(4) * heightRatio);
params.width = (int) (mProperty.getInt(3) * widthRatio);
mView.setLayoutParams(params);
setPosition();
updateZIndices();
} catch (Exception e) {
Log.i(TAG, "error when trying to retrieve properties while resizing properties");
Expand Down Expand Up @@ -265,7 +339,7 @@ public void destroyPublisher() {

public void run() {
if(this.mView == null) {
this.mView = mPublisher.getView();
this.mView = new CustomView(cordova.getActivity().getApplicationContext(), (TextureView) mPublisher.getView());
((ViewGroup) webView.getView().getParent()).addView(this.mView);

// Set depth location of camera view based on CSS z-index.
Expand All @@ -286,7 +360,7 @@ public void onError(PublisherKit arg0, OpentokError arg1) {
public void onStreamCreated(PublisherKit arg0, Stream arg1) {
Log.i(TAG, "publisher stream received");
streamCollection.put(arg1.getStreamId(), arg1);

streamHasAudio.put(arg1.getStreamId(), arg1.hasAudio());
streamHasVideo.put(arg1.getStreamId(), arg1.hasVideo());
JSONObject videoDimensions = new JSONObject();
Expand Down Expand Up @@ -375,7 +449,7 @@ public void removeStreamView() {

public void run() {
if(this.mView == null) {
this.mView = mSubscriber.getView();
this.mView = new CustomView(cordova.getActivity().getApplicationContext(), (TextureView) mSubscriber.getView());
((ViewGroup) webView.getView().getParent()).addView(this.mView);

// Set depth location of camera view based on CSS z-index.
Expand Down Expand Up @@ -540,7 +614,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
apiKey = args.getString(0);
sessionId = args.getString(1);
Log.i(TAG, "created new session with data: " + args.toString());
mSession = new Session(this.cordova.getActivity().getApplicationContext(), apiKey, sessionId);
mSession = new Session.Builder(this.cordova.getActivity().getApplicationContext(), apiKey, sessionId).sessionOptions(new Session.SessionOptions() {
@Override
public boolean useTextureViews() {
return true;
}
}).build();
mSession.setSessionListener(this);
mSession.setConnectionListener(this);
mSession.setReconnectionListener(this);
Expand Down Expand Up @@ -643,7 +722,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
Log.i(TAG, "setting subscribeToVideo");
runsub.subscribeToVideo(subscribeVideo);
}
} else if (action.equals("updateView")) {
} else if (action.equals("uspdateView")) {
if (args.getString(0).equals("TBPublisher") && myPublisher != null && sessionConnected) {
Log.i(TAG, "updating view for publisher");
myPublisher.setPropertyFromArray(args);
Expand Down Expand Up @@ -873,7 +952,7 @@ public void onStreamHasVideoChanged(Session session, Stream stream, boolean newV
@Override
public void onStreamVideoDimensionsChanged(Session session, Stream stream, int width, int height) {
JSONObject oldValue = this.streamVideoDimensions.get(stream.getStreamId());

JSONObject newValue = new JSONObject();
try {
newValue.put("width", width);
Expand Down

0 comments on commit 8ff1b67

Please sign in to comment.