Skip to content

Commit 8a3fe7a

Browse files
authored
Upgrade to Android 6 (#452)
* Upgrade to android v6 * Fix a bunch of issues * Last set of fixes * Switch the events around
1 parent 9cd03dd commit 8a3fe7a

File tree

7 files changed

+68
-51
lines changed

7 files changed

+68
-51
lines changed

Example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:4.0.0'
9+
classpath 'com.android.tools.build:gradle:4.0.1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencies {
4949

5050
implementation fileTree(dir: 'libs', include: ['*.jar'])
5151
implementation "com.android.support:appcompat-v7:$androidSupportVersion"
52-
implementation "com.twilio:video-android:5.10.1"
52+
implementation "com.twilio:video-android:6.2.1"
5353
implementation 'org.webrtc:google-webrtc:1.0.30039'
5454
implementation "com.facebook.react:react-native:+" // From node_modules
5555
}

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

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@
6969
import com.twilio.video.TrackPublication;
7070
import com.twilio.video.TwilioException;
7171
import com.twilio.video.Video;
72-
import com.twilio.video.VideoConstraints;
7372
import com.twilio.video.VideoDimensions;
73+
import com.twilio.video.VideoFormat;
7474

7575
import org.webrtc.voiceengine.WebRtcAudioManager;
7676

77+
import tvi.webrtc.Camera1Enumerator;
78+
7779
import java.lang.annotation.Retention;
7880
import java.lang.annotation.RetentionPolicy;
7981
import java.util.Collections;
@@ -109,6 +111,8 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
109111
private boolean enableNetworkQualityReporting = false;
110112
private boolean isVideoEnabled = false;
111113
private boolean dominantSpeakerEnabled = false;
114+
private static String frontFacingDevice;
115+
private static String backFacingDevice;
112116
private boolean maintainVideoTrackInBackground = false;
113117

114118
@Retention(RetentionPolicy.SOURCE)
@@ -234,29 +238,27 @@ public CustomTwilioVideoView(ThemedReactContext context) {
234238

235239
// ===== SETUP =================================================================================
236240

237-
private VideoConstraints buildVideoConstraints() {
238-
return new VideoConstraints.Builder()
239-
.minVideoDimensions(VideoDimensions.CIF_VIDEO_DIMENSIONS)
240-
.maxVideoDimensions(VideoDimensions.CIF_VIDEO_DIMENSIONS)
241-
.minFps(5)
242-
.maxFps(15)
243-
.build();
241+
private VideoFormat buildVideoFormat() {
242+
return new VideoFormat(VideoDimensions.CIF_VIDEO_DIMENSIONS, 15);
244243
}
245244

246-
private CameraCapturer createCameraCaputer(Context context, CameraCapturer.CameraSource cameraSource) {
245+
private CameraCapturer createCameraCaputer(Context context, String cameraId) {
247246
CameraCapturer newCameraCapturer = null;
248247
try {
249248
newCameraCapturer = new CameraCapturer(
250249
context,
251-
cameraSource,
250+
cameraId,
252251
new CameraCapturer.Listener() {
253252
@Override
254253
public void onFirstFrameAvailable() {
255254
}
256255

257256
@Override
258-
public void onCameraSwitched() {
257+
public void onCameraSwitched(String newCameraId) {
259258
setThumbnailMirror();
259+
WritableMap event = new WritableNativeMap();
260+
event.putBoolean("isBackCamera", isCurrentCameraSourceBackFacing());
261+
pushEvent(CustomTwilioVideoView.this, ON_CAMERA_SWITCHED, event);
260262
}
261263

262264
@Override
@@ -271,27 +273,40 @@ public void onError(int i) {
271273
}
272274
}
273275

276+
private void buildDeviceInfo() {
277+
Camera1Enumerator enumerator = new Camera1Enumerator();
278+
String[] deviceNames = enumerator.getDeviceNames();
279+
backFacingDevice = null;
280+
frontFacingDevice = null;
281+
for (String deviceName : deviceNames) {
282+
if (enumerator.isBackFacing(deviceName) && enumerator.getSupportedFormats(deviceName).size() > 0) {
283+
backFacingDevice = deviceName;
284+
} else if (enumerator.isFrontFacing(deviceName) && enumerator.getSupportedFormats(deviceName).size() > 0) {
285+
frontFacingDevice = deviceName;
286+
}
287+
}
288+
}
289+
274290
private boolean createLocalVideo(boolean enableVideo) {
275-
isVideoEnabled = enableVideo;
291+
isVideoEnabled = enableVideo;
276292
// Share your camera
277-
cameraCapturer = this.createCameraCaputer(getContext(), CameraCapturer.CameraSource.FRONT_CAMERA);
278-
if (cameraCapturer == null){
279-
cameraCapturer = this.createCameraCaputer(getContext(), CameraCapturer.CameraSource.BACK_CAMERA);
280-
}
281-
if (cameraCapturer == null){
293+
buildDeviceInfo();
294+
if (this.frontFacingDevice != null) {
295+
cameraCapturer = this.createCameraCaputer(getContext(), frontFacingDevice);
296+
} else if (this.backFacingDevice != null) {
297+
cameraCapturer = this.createCameraCaputer(getContext(), backFacingDevice);
298+
} else {
282299
WritableMap event = new WritableNativeMap();
283300
event.putString("error", "No camera is supported on this device");
284301
pushEvent(CustomTwilioVideoView.this, ON_CONNECT_FAILURE, event);
285302
return false;
286303
}
287304

288-
if (cameraCapturer.getSupportedFormats().size() > 0) {
289-
localVideoTrack = LocalVideoTrack.create(getContext(), enableVideo, cameraCapturer, buildVideoConstraints());
290-
if (thumbnailVideoView != null && localVideoTrack != null) {
291-
localVideoTrack.addRenderer(thumbnailVideoView);
292-
}
293-
setThumbnailMirror();
305+
localVideoTrack = LocalVideoTrack.create(getContext(), enableVideo, cameraCapturer, buildVideoFormat());
306+
if (thumbnailVideoView != null && localVideoTrack != null) {
307+
localVideoTrack.addSink(thumbnailVideoView);
294308
}
309+
setThumbnailMirror();
295310
return true;
296311
}
297312

@@ -308,12 +323,12 @@ public void onHostResume() {
308323
* If the local video track was released when the app was put in the background, recreate.
309324
*/
310325
if (cameraCapturer != null && localVideoTrack == null) {
311-
localVideoTrack = LocalVideoTrack.create(getContext(), isVideoEnabled, cameraCapturer, buildVideoConstraints());
326+
localVideoTrack = LocalVideoTrack.create(getContext(), isVideoEnabled, cameraCapturer, buildVideoFormat());
312327
}
313328

314329
if (localVideoTrack != null) {
315330
if (thumbnailVideoView != null) {
316-
localVideoTrack.addRenderer(thumbnailVideoView);
331+
localVideoTrack.addSink(thumbnailVideoView);
317332
}
318333

319334
/*
@@ -545,11 +560,14 @@ public void sendString(String message) {
545560
}
546561
}
547562

563+
private static boolean isCurrentCameraSourceBackFacing() {
564+
return cameraCapturer != null && cameraCapturer.getCameraId() == backFacingDevice;
565+
}
566+
548567
// ===== BUTTON LISTENERS ======================================================================
549568
private static void setThumbnailMirror() {
550569
if (cameraCapturer != null) {
551-
CameraCapturer.CameraSource cameraSource = cameraCapturer.getCameraSource();
552-
final boolean isBackCamera = (cameraSource == CameraCapturer.CameraSource.BACK_CAMERA);
570+
final boolean isBackCamera = isCurrentCameraSourceBackFacing();
553571
if (thumbnailVideoView != null && thumbnailVideoView.getVisibility() == View.VISIBLE) {
554572
thumbnailVideoView.setMirror(!isBackCamera);
555573
}
@@ -558,12 +576,12 @@ private static void setThumbnailMirror() {
558576

559577
public void switchCamera() {
560578
if (cameraCapturer != null) {
561-
cameraCapturer.switchCamera();
562-
CameraCapturer.CameraSource cameraSource = cameraCapturer.getCameraSource();
563-
final boolean isBackCamera = cameraSource == CameraCapturer.CameraSource.BACK_CAMERA;
564-
WritableMap event = new WritableNativeMap();
565-
event.putBoolean("isBackCamera", isBackCamera);
566-
pushEvent(CustomTwilioVideoView.this, ON_CAMERA_SWITCHED, event);
579+
final boolean isBackCamera = isCurrentCameraSourceBackFacing();
580+
if (frontFacingDevice != null && (isBackCamera || backFacingDevice == null)) {
581+
cameraCapturer.switchCamera(frontFacingDevice);
582+
} else {
583+
cameraCapturer.switchCamera(backFacingDevice);
584+
}
567585
}
568586
}
569587

@@ -1145,9 +1163,9 @@ public static void registerPrimaryVideoView(PatchedVideoView v, String trackSid)
11451163
continue;
11461164
}
11471165
if (publication.getTrackSid().equals(trackSid)) {
1148-
track.addRenderer(v);
1166+
track.addSink(v);
11491167
} else {
1150-
track.removeRenderer(v);
1168+
track.removeSink(v);
11511169
}
11521170
}
11531171
}
@@ -1157,7 +1175,7 @@ public static void registerPrimaryVideoView(PatchedVideoView v, String trackSid)
11571175
public static void registerThumbnailVideoView(PatchedVideoView v) {
11581176
thumbnailVideoView = v;
11591177
if (localVideoTrack != null) {
1160-
localVideoTrack.addRenderer(v);
1178+
localVideoTrack.addSink(v);
11611179
}
11621180
setThumbnailMirror();
11631181
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import android.os.Looper;
1212
import android.util.AttributeSet;
1313

14-
import com.twilio.video.I420Frame;
1514
import com.twilio.video.VideoView;
1615

16+
import tvi.webrtc.VideoFrame;
17+
1718
/*
1819
* VideoView that notifies Listener of the first frame rendered and the first frame after a reset
1920
* request.
@@ -33,7 +34,7 @@ public PatchedVideoView(Context context, AttributeSet attrs) {
3334
}
3435

3536
@Override
36-
public void renderFrame(I420Frame frame) {
37+
public void onFrame(VideoFrame frame) {
3738
if (notifyFrameRendered) {
3839
notifyFrameRendered = false;
3940
mainThreadHandler.post(new Runnable() {
@@ -43,7 +44,7 @@ public void run() {
4344
}
4445
});
4546
}
46-
super.renderFrame(frame);
47+
super.onFrame(frame);
4748
}
4849

4950
/*

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import com.facebook.react.bridge.WritableNativeMap;
1717
import com.facebook.react.uimanager.ThemedReactContext;
1818
import com.facebook.react.uimanager.events.RCTEventEmitter;
19-
import com.twilio.video.VideoFrame;
20-
import com.twilio.video.VideoRenderer;
2119
import com.twilio.video.VideoScaleType;
2220

23-
import org.webrtc.RendererCommon;
21+
import tvi.webrtc.RendererCommon;
22+
import tvi.webrtc.VideoFrame;
2423

2524
import java.lang.annotation.Retention;
2625
import java.lang.annotation.RetentionPolicy;
@@ -52,17 +51,16 @@ public RNVideoViewGroup(ThemedReactContext themedReactContext) {
5251
surfaceViewRenderer.setVideoScaleType(VideoScaleType.ASPECT_FILL);
5352
addView(surfaceViewRenderer);
5453
surfaceViewRenderer.setListener(
55-
new VideoRenderer.Listener() {
54+
new RendererCommon.RendererEvents() {
5655
@Override
57-
public void onFirstFrame() {
56+
public void onFirstFrameRendered() {
5857

5958
}
6059

6160
@Override
62-
public void onFrameDimensionsChanged(int vw, int vh, int rotation) {
61+
public void onFrameResolutionChanged(int vw, int vh, int rotation) {
6362
synchronized (layoutSync) {
64-
if (rotation == VideoFrame.RotationAngle.ROTATION_90.getValue() ||
65-
rotation == VideoFrame.RotationAngle.ROTATION_270.getValue()) {
63+
if (rotation == 90 || rotation == 270) {
6664
videoHeight = vw;
6765
videoWidth = vh;
6866
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import com.facebook.react.uimanager.ThemedReactContext;
1616
import com.facebook.react.uimanager.annotations.ReactProp;
1717

18-
import org.webrtc.RendererCommon;
18+
import tvi.webrtc.RendererCommon;
1919

2020
import java.util.Map;
2121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.util.Map;
1919

20-
import org.webrtc.RendererCommon;
20+
import tvi.webrtc.RendererCommon;
2121

2222
import static com.twiliorn.library.RNVideoViewGroup.Events.ON_FRAME_DIMENSIONS_CHANGED;
2323

0 commit comments

Comments
 (0)