diff --git a/.idea/misc.xml b/.idea/misc.xml index 9bbb4ab..9a056b0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ diff --git a/app/src/main/java/fr/mobdev/peertubelive/activity/StreamActivity.kt b/app/src/main/java/fr/mobdev/peertubelive/activity/StreamActivity.kt index ad52aa5..73d641e 100644 --- a/app/src/main/java/fr/mobdev/peertubelive/activity/StreamActivity.kt +++ b/app/src/main/java/fr/mobdev/peertubelive/activity/StreamActivity.kt @@ -53,7 +53,7 @@ class StreamActivity : AppCompatActivity() { private var screenOrientation: Int = 0 private var lastScreenOrientation: Int = 0 private var orientationCounter: Int = 0 - private var rotationIsLanternEnabled: Boolean = true + private var rotationIsEnabled: Boolean = true companion object { const val BACKGROUND :Int = 1 @@ -71,8 +71,9 @@ class StreamActivity : AppCompatActivity() { orientationEventListener = object: OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){ override fun onOrientationChanged(orientation: Int) { - if(orientation < 0 || !rotationIsLanternEnabled) + if(orientation < 0 || !rotationIsEnabled) { return + } var localOrientation: Int localOrientation = when (orientation) { in 45..135 -> { @@ -108,6 +109,7 @@ class StreamActivity : AppCompatActivity() { binding.flash.rotation = localOrientation.toFloat() binding.muteMicro.rotation = localOrientation.toFloat() binding.switchCamera.rotation = localOrientation.toFloat() + binding.rotation.rotation = localOrientation.toFloat() } } } @@ -144,12 +146,12 @@ class StreamActivity : AppCompatActivity() { binding.flash.visibility = View.GONE binding.rotation.setOnClickListener { - if (rotationIsLanternEnabled) { - rotationIsLanternEnabled = !rotationIsLanternEnabled + if (rotationIsEnabled) { + rotationIsEnabled = !rotationIsEnabled binding.rotation.setImageResource(R.drawable.baseline_screen_lock_rotation_24) } else { - rotationIsLanternEnabled = !rotationIsLanternEnabled + rotationIsEnabled = !rotationIsEnabled binding.rotation.setImageResource(R.drawable.baseline_screen_rotation_24) } } @@ -351,7 +353,8 @@ class StreamActivity : AppCompatActivity() { //start stream - if (rtmpCamera2.prepareAudio() && rtmpCamera2.prepareVideo(width,height,30, (width*height*30*0.076).toInt(),CameraHelper.getCameraOrientation(this))) { + if (rtmpCamera2.prepareAudio() && rtmpCamera2.prepareVideo(width,height,30, (width*height*30*0.076).toInt(),CameraHelper.getCameraOrientation(this),true)) { + println("peertubeurl "+streamData.url+"/"+streamData.key) rtmpCamera2.startStream(streamData.url+"/"+streamData.key) } else { /**This device cant init encoders, this could be for 2 reasons: The encoder selected doesnt support any configuration setted or your device hasnt a H264 or AAC encoder (in this case you can see log error valid encoder not found) */ diff --git a/encoder/src/main/java/com/pedro/encoder/input/gl/render/ScreenRender.java b/encoder/src/main/java/com/pedro/encoder/input/gl/render/ScreenRender.java index a2ede8d..0210ad7 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/gl/render/ScreenRender.java +++ b/encoder/src/main/java/com/pedro/encoder/input/gl/render/ScreenRender.java @@ -95,7 +95,7 @@ public class ScreenRender { boolean flipStreamVertical, boolean flipStreamHorizontal) { GlUtil.checkGlError("drawScreen start"); - SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix); + SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical,width,height,true,keepAspectRatio, MVPMatrix); SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth, streamHeight); @@ -106,7 +106,7 @@ public class ScreenRender { boolean flipStreamVertical, boolean flipStreamHorizontal) { GlUtil.checkGlError("drawScreen start"); - SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix); + SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical,width, height, isPortrait,false, MVPMatrix); SizeCalculator.calculateViewPortEncoder(width, height, isPortrait); draw(width, height); @@ -116,7 +116,7 @@ public class ScreenRender { int mode, int rotation, boolean flipStreamVertical, boolean flipStreamHorizontal) { GlUtil.checkGlError("drawScreen start"); - SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix); + SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, width, height, isPortrait,true, MVPMatrix); float factor = (float) streamWidth / (float) streamHeight; int w; int h; diff --git a/encoder/src/main/java/com/pedro/encoder/input/gl/render/SimpleCameraRender.java b/encoder/src/main/java/com/pedro/encoder/input/gl/render/SimpleCameraRender.java index 574a16f..e4435ab 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/gl/render/SimpleCameraRender.java +++ b/encoder/src/main/java/com/pedro/encoder/input/gl/render/SimpleCameraRender.java @@ -105,7 +105,7 @@ public class SimpleCameraRender { GlUtil.checkGlError("drawFrame start"); surfaceTexture.getTransformMatrix(STMatrix); - SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix); + SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, width, height, true, false, MVPMatrix); SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth, streamHeight); diff --git a/encoder/src/main/java/com/pedro/encoder/utils/gl/SizeCalculator.java b/encoder/src/main/java/com/pedro/encoder/utils/gl/SizeCalculator.java index 064f09f..4fa62b1 100644 --- a/encoder/src/main/java/com/pedro/encoder/utils/gl/SizeCalculator.java +++ b/encoder/src/main/java/com/pedro/encoder/utils/gl/SizeCalculator.java @@ -96,13 +96,14 @@ public class SizeCalculator { } return new Pair<>(new Point(xo, yo), new Point(xf, yf)); } else { - return new Pair<>(new Point(0, 0), new Point(previewWidth, previewHeight)); + //return new Pair<>(new Point(0, 0), new Point(previewWidth,previewHeight)); + return new Pair<>(new Point(0, 0), new Point(previewHeight, previewWidth)); } } public static void processMatrix(int rotation, boolean flipStreamHorizontal, - boolean flipStreamVertical, float[] MVPMatrix) { - PointF scale = new PointF(1f, 1f); + boolean flipStreamVertical, int width, int height, boolean isPortrait, boolean isPreview, float[] MVPMatrix) { + PointF scale = getScale(rotation,width,height, isPortrait, isPreview); float xFlip = flipStreamHorizontal ? -1f : 1f; float yFlip = flipStreamVertical ? -1f : 1f; @@ -117,18 +118,17 @@ public class SizeCalculator { Matrix.rotateM(MVPMatrix, 0, rotation, 0f, 0f, -1f); } - private static PointF getScale(int rotation, int width, int height, boolean isPortrait, - boolean isPreview) { + private static PointF getScale(int rotation, int width, int height, boolean isPortrait, boolean isPreview) { float scaleX = 1f; float scaleY = 1f; if (!isPreview) { - if (isPortrait && rotation != 0 && rotation != 180) { //portrait + if (isPortrait && rotation != 90 && rotation != 270) { //portrait final float adjustedWidth = width * (width / (float) height); - scaleY = adjustedWidth / height; - } else if (!isPortrait && rotation != 90 && rotation != 270) { //landscape + scaleX = adjustedWidth / height; + } /*else if (isPortrait && rotation != 0 && rotation != 180) { //landscape final float adjustedWidth = height * (height / (float) width); - scaleX = adjustedWidth / width; - } + scaleY = adjustedWidth / width; + }*/ } return new PointF(scaleX, scaleY); } diff --git a/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java b/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java index 56b5911..4d4cdfe 100644 --- a/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java +++ b/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java @@ -77,7 +77,20 @@ public class VideoEncoder extends BaseEncoder implements GetCameraData { public boolean prepareVideoEncoder(int width, int height, int fps, int bitRate, int rotation, int iFrameInterval, FormatVideoEncoder formatVideoEncoder) { return prepareVideoEncoder(width, height, fps, bitRate, rotation, iFrameInterval, - formatVideoEncoder, -1, -1); + formatVideoEncoder, -1, -1, false); + } + + public boolean prepareVideoEncoder(int width, int height, int fps, int bitRate, int rotation, + int iFrameInterval, FormatVideoEncoder formatVideoEncoder, boolean landscape) { + return prepareVideoEncoder(width, height, fps, bitRate, rotation, iFrameInterval, + formatVideoEncoder, -1, -1, landscape); + } + + public boolean prepareVideoEncoder(int width, int height, int fps, int bitRate, int rotation, + int iFrameInterval, FormatVideoEncoder formatVideoEncoder, int avcProfile, + int avcProfileLevel) { + return prepareVideoEncoder(width, height, fps, bitRate, rotation, iFrameInterval, + formatVideoEncoder, avcProfile, avcProfileLevel, false); } /** @@ -85,7 +98,7 @@ public class VideoEncoder extends BaseEncoder implements GetCameraData { */ public boolean prepareVideoEncoder(int width, int height, int fps, int bitRate, int rotation, int iFrameInterval, FormatVideoEncoder formatVideoEncoder, int avcProfile, - int avcProfileLevel) { + int avcProfileLevel, boolean landscape) { this.width = width; this.height = height; this.fps = fps; @@ -116,10 +129,11 @@ public class VideoEncoder extends BaseEncoder implements GetCameraData { //if you dont use mediacodec rotation you need swap width and height in rotation 90 or 270 // for correct encoding resolution String resolution; - if ((rotation == 90 || rotation == 270)) { + if (!landscape && (rotation == 90 || rotation == 270)) { resolution = height + "x" + width; videoFormat = MediaFormat.createVideoFormat(type, height, width); } else { + System.out.println("resolution "+width+"x"+height); resolution = width + "x" + height; videoFormat = MediaFormat.createVideoFormat(type, width, height); } diff --git a/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java b/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java index e100706..4010c44 100644 --- a/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java +++ b/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java @@ -318,6 +318,7 @@ public abstract class Camera2Base implements GetAacData, GetVideoData, GetMicrop stopPreview(); onPreview = true; } + boolean result = videoEncoder.prepareVideoEncoder(width, height, fps, bitrate, rotation, iFrameInterval, FormatVideoEncoder.SURFACE, avcProfile, avcProfileLevel); prepareCameraManager(); @@ -325,20 +326,34 @@ public abstract class Camera2Base implements GetAacData, GetVideoData, GetMicrop } public boolean prepareVideo(int width, int height, int fps, int bitrate, int iFrameInterval, - int rotation) { - return prepareVideo(width, height, fps, bitrate, iFrameInterval, rotation, -1, -1); + int rotation, int avcProfile, int avcProfileLevel, boolean landscape) { + if (onPreview && glInterface != null && (width != previewWidth || height != previewHeight + || fps != videoEncoder.getFps() || rotation != videoEncoder.getRotation())) { + stopPreview(); + onPreview = true; + } + + boolean result = videoEncoder.prepareVideoEncoder(width, height, fps, bitrate, rotation, + iFrameInterval, FormatVideoEncoder.SURFACE, avcProfile, avcProfileLevel,landscape); + prepareCameraManager(); + return result; + } + + public boolean prepareVideo(int width, int height, int fps, int bitrate, int iFrameInterval, + int rotation, boolean landscape) { + return prepareVideo(width, height, fps, bitrate, iFrameInterval, rotation, -1, -1,landscape); } /** * backward compatibility reason */ - public boolean prepareVideo(int width, int height, int fps, int bitrate, int rotation) { - return prepareVideo(width, height, fps, bitrate, 2, rotation); + public boolean prepareVideo(int width, int height, int fps, int bitrate, int rotation, boolean landscape) { + return prepareVideo(width, height, fps, bitrate, 2, rotation,landscape); } public boolean prepareVideo(int width, int height, int bitrate) { int rotation = CameraHelper.getCameraOrientation(context); - return prepareVideo(width, height, 30, bitrate, 2, rotation); + return prepareVideo(width, height, 30, bitrate, 2, rotation, false); } protected abstract void prepareAudioRtp(boolean isStereo, int sampleRate); @@ -385,7 +400,7 @@ public abstract class Camera2Base implements GetAacData, GetVideoData, GetMicrop */ public boolean prepareVideo() { int rotation = CameraHelper.getCameraOrientation(context); - return prepareVideo(640, 480, 30, 1200 * 1024, rotation); + return prepareVideo(640, 480, 30, 1200 * 1024, rotation,false); } /**