From d8f31d5873a74a917d29b13e13a0d771f587f6f8 Mon Sep 17 00:00:00 2001 From: Derek Edwards Date: Fri, 25 Oct 2019 16:49:14 -0400 Subject: [PATCH] Patch underlying Android RTMP library to send more consistent key frames --- android/build.gradle | 2 +- android/src/main/AndroidManifest.xml | 1 + .../com/pedrolibrary/RNBroadcastView.java | 47 +++++++++++++------ ios/RNBroadcast/LFLivePreview.m | 4 +- package.json | 2 +- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index e1033e1..6afed90 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -33,5 +33,5 @@ repositories { dependencies { compile 'com.facebook.react:react-native:+' - implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:1.6.7' + implementation 'com.github.behaviorcloud.rtmp-rtsp-stream-client-java:rtplibrary:1.7.3' } diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 6a5e789..f8dc703 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -6,5 +6,6 @@ + \ No newline at end of file diff --git a/android/src/main/java/com/pedrolibrary/RNBroadcastView.java b/android/src/main/java/com/pedrolibrary/RNBroadcastView.java index a54a897..bf949d6 100644 --- a/android/src/main/java/com/pedrolibrary/RNBroadcastView.java +++ b/android/src/main/java/com/pedrolibrary/RNBroadcastView.java @@ -6,13 +6,15 @@ import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; +import com.pedro.rtplibrary.view.OpenGlView; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.facebook.react.uimanager.ThemedReactContext; import com.pedro.encoder.input.video.CameraOpenException; -import com.pedro.rtplibrary.rtmp.RtmpCamera2; +import com.pedro.rtplibrary.rtmp.RtmpCamera1; +import com.pedro.encoder.input.video.CameraHelper; import net.ossrs.rtmp.ConnectCheckerRtmp; import static com.facebook.react.common.ReactConstants.TAG; @@ -20,9 +22,10 @@ public class RNBroadcastView extends FrameLayout implements ConnectCheckerRtmp, SurfaceHolder.Callback { - private SurfaceView mCameraView; + // private SurfaceView mCameraView; + private OpenGlView mCameraView; private ThemedReactContext mContext = null; - private RtmpCamera2 rtmpCamera2; + private RtmpCamera1 rtmpCamera1; private Boolean isLive = false; private Boolean surfaceExists = false; private String streamUrl = ""; @@ -32,20 +35,22 @@ public RNBroadcastView(@NonNull ThemedReactContext context) { // context.addLifecycleEventListener(this); mContext = context; - mCameraView = new SurfaceView(context); + // mCameraView = new SurfaceView(context); + mCameraView = new OpenGlView(context); + mCameraView.setKeepAspectRatio(true); mCameraView.getHolder().addCallback(this); addView(mCameraView); - rtmpCamera2 = new RtmpCamera2(mCameraView, this); - rtmpCamera2.setReTries(10); + rtmpCamera1 = new RtmpCamera1(mCameraView, this); + rtmpCamera1.setReTries(10); Log.d(TAG, "About to return from RNBroadcastView"); } private void handleException(Exception e) { try { - rtmpCamera2.stopStream(); + rtmpCamera1.stopStream(); } catch (Exception e1) { // } @@ -76,9 +81,21 @@ public void handleStopPublish() { public void startStreaming() { if (surfaceExists && !streamUrl.isEmpty()) { - if (rtmpCamera2.isRecording() - || rtmpCamera2.prepareAudio() && rtmpCamera2.prepareVideo()) { - rtmpCamera2.startStream(streamUrl); + boolean hardwareRotation = true; + try { + rtmpCamera1.getGlInterface(); + } + catch (Exception e) { + hardwareRotation = false; + } + + int cameraOrientation = CameraHelper.getCameraOrientation(getContext()), + captureWidth = 640, + captureHeight = 480; + + if (rtmpCamera1.isRecording() + || rtmpCamera1.prepareAudio() && rtmpCamera1.prepareVideo(captureWidth, captureHeight, 30, 600*1024, hardwareRotation, 1, cameraOrientation)) { + rtmpCamera1.startStream(streamUrl); isLive = true; } else { // Toast.makeText(this, "Error preparing stream, This device cant do it", @@ -89,7 +106,7 @@ public void startStreaming() { } public void stopStreaming() { - rtmpCamera2.stopStream(); + rtmpCamera1.stopStream(); isLive = false; } @@ -185,15 +202,17 @@ public void surfaceCreated(SurfaceHolder surfaceHolder) { @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { Log.d(TAG, "SurfaceChanged"); - rtmpCamera2.startPreview(); + rtmpCamera1.startPreview(); + int cameraOrientation = CameraHelper.getCameraOrientation(getContext()); + rtmpCamera1.setPreviewOrientation(cameraOrientation); } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { surfaceExists = false; - if (rtmpCamera2.isStreaming()) { + if (rtmpCamera1.isStreaming()) { stopStreaming(); } - rtmpCamera2.stopPreview(); + rtmpCamera1.stopPreview(); } } diff --git a/ios/RNBroadcast/LFLivePreview.m b/ios/RNBroadcast/LFLivePreview.m index 0d3e4a9..01a23b2 100755 --- a/ios/RNBroadcast/LFLivePreview.m +++ b/ios/RNBroadcast/LFLivePreview.m @@ -40,8 +40,8 @@ - (LFLiveSession*)session { videoConfiguration.videoBitRate = 800*1024; videoConfiguration.videoMaxBitRate = 1000*1024; videoConfiguration.videoMinBitRate = 500*1024; - videoConfiguration.videoFrameRate = 24; - videoConfiguration.videoMaxKeyframeInterval = 48; + videoConfiguration.videoFrameRate = 30; + videoConfiguration.videoMaxKeyframeInterval = 30; videoConfiguration.outputImageOrientation = UIInterfaceOrientationLandscapeRight; videoConfiguration.autorotate = YES; videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280; diff --git a/package.json b/package.json index d71fd4f..0ed57b3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-broadcast", - "version": "1.0.9", + "version": "1.0.10", "description": "RTMP Broadcaster library for React Native", "main": "index.js", "scripts": {