Skip to content

Commit

Permalink
fix stream orientation using GlStreaInterface on Camera1Base, Camera2…
Browse files Browse the repository at this point in the history
…Base, DisplayBase and FromFileBase
  • Loading branch information
pedroSG94 committed Mar 13, 2024
1 parent 3cc8251 commit 9f2178d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
61 changes: 24 additions & 37 deletions library/src/main/java/com/pedro/library/base/Camera1Base.java
Expand Up @@ -441,14 +441,15 @@ private void replaceGlInterface(GlInterface glInterface) {
this.glInterface.removeMediaCodecSurface();
this.glInterface.stop();
this.glInterface = glInterface;
this.glInterface.setEncoderSize(size.x, size.y);
this.glInterface.setRotation(0);
this.glInterface.start();
if (isStreaming() || isRecording()) {
this.glInterface.addMediaCodecSurface(videoEncoder.getInputSurface());
int w = size.x;
int h = size.y;
int rotation = videoEncoder.getRotation();
if (rotation == 90 || rotation == 270) {
h = size.x;
w = size.y;
}
cameraManager.setSurfaceTexture(glInterface.getSurfaceTexture());
cameraManager.setRotation(videoEncoder.getRotation());
prepareGlView(w, h, rotation);
cameraManager.setRotation(rotation);
cameraManager.start(videoEncoder.getWidth(), videoEncoder.getHeight(),
videoEncoder.getFps());
} else {
Expand Down Expand Up @@ -479,16 +480,7 @@ public void startPreview(CameraHelper.Facing cameraFacing, int width, int height
previewHeight = height;
videoEncoder.setFps(fps);
videoEncoder.setRotation(rotation);
if (glInterface != null && Build.VERSION.SDK_INT >= 18) {
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(height, width);
} else {
glInterface.setEncoderSize(width, height);
}
glInterface.setRotation(0);
glInterface.start();
cameraManager.setSurfaceTexture(glInterface.getSurfaceTexture());
}
prepareGlView(width, height, rotation);
cameraManager.setRotation(rotation);
cameraManager.start(cameraFacing, width, height, videoEncoder.getFps());
onPreview = true;
Expand Down Expand Up @@ -519,16 +511,7 @@ public void startPreview(int cameraId, int width, int height, int fps, int rotat
previewHeight = height;
videoEncoder.setFps(fps);
videoEncoder.setRotation(rotation);
if (glInterface != null && Build.VERSION.SDK_INT >= 18) {
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(height, width);
} else {
glInterface.setEncoderSize(width, height);
}
glInterface.setRotation(0);
glInterface.start();
cameraManager.setSurfaceTexture(glInterface.getSurfaceTexture());
}
prepareGlView(width, height, rotation);
cameraManager.setRotation(rotation);
cameraManager.start(cameraId, width, height, videoEncoder.getFps());
onPreview = true;
Expand Down Expand Up @@ -688,7 +671,7 @@ public void startStream(String url) {
private void startEncoders() {
videoEncoder.start();
if (audioInitialized) audioEncoder.start();
prepareGlView();
prepareGlView(videoEncoder.getWidth(), videoEncoder.getHeight(), videoEncoder.getRotation());
if (audioInitialized) microphoneManager.start();
cameraManager.setRotation(videoEncoder.getRotation());
if (!cameraManager.isRunning() && videoEncoder.getWidth() != previewWidth
Expand All @@ -714,18 +697,22 @@ public void requestKeyFrame() {
}
}

private void prepareGlView() {
private void prepareGlView(int width, int height, int rotation) {
if (glInterface != null && Build.VERSION.SDK_INT >= 18) {
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(videoEncoder.getHeight(), videoEncoder.getWidth());
} else {
glInterface.setEncoderSize(videoEncoder.getWidth(), videoEncoder.getHeight());
int w = width;
int h = height;
boolean isPortrait = false;
if (rotation == 90 || rotation == 270) {
h = width;
w = height;
isPortrait = true;
}
glInterface.setRotation(0);
if (!cameraManager.isRunning() && videoEncoder.getWidth() != previewWidth
|| videoEncoder.getHeight() != previewHeight) {
glInterface.start();
glInterface.setEncoderSize(w, h);
if (glInterface instanceof GlStreamInterface) {
((GlStreamInterface) glInterface).setPreviewResolution(w, h, isPortrait);
}
glInterface.setRotation(0);
if (!glInterface.isRunning()) glInterface.start();
if (videoEncoder.getInputSurface() != null) {
glInterface.addMediaCodecSurface(videoEncoder.getInputSurface());
}
Expand Down
49 changes: 22 additions & 27 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Expand Up @@ -477,14 +477,14 @@ private void replaceGlInterface(GlInterface glInterface) {
this.glInterface.removeMediaCodecSurface();
this.glInterface.stop();
this.glInterface = glInterface;
this.glInterface.setEncoderSize(size.x, size.y);
this.glInterface.setRotation(videoEncoder.getRotation() == 0 ? 270 : videoEncoder.getRotation() - 90);
this.glInterface.start();
if (isStreaming() || isRecording()) {
this.glInterface.addMediaCodecSurface(videoEncoder.getInputSurface());
int w = size.x;
int h = size.y;
int rotation = videoEncoder.getRotation();
if (rotation == 90 || rotation == 270) {
h = size.x;
w = size.y;
}
cameraManager.prepareCamera(this.glInterface.getSurfaceTexture(), videoEncoder.getWidth(),
videoEncoder.getHeight(), videoEncoder.getFps());
prepareGlView(w, h, rotation);
cameraManager.openLastCamera();
} else {
this.glInterface = glInterface;
Expand Down Expand Up @@ -524,15 +524,7 @@ public void startPreview(String cameraId, int width, int height, int fps, int ro
cameraManager.prepareCamera(new Surface(textureView.getSurfaceTexture()),
videoEncoder.getFps());
} else if (glInterface != null) {
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(height, width);
} else {
glInterface.setEncoderSize(width, height);
}
glInterface.setRotation(rotation == 0 ? 270 : rotation - 90);
glInterface.start();
cameraManager.prepareCamera(glInterface.getSurfaceTexture(), width, height,
videoEncoder.getFps(), cameraId);
prepareGlView(width, height, rotation);
}
cameraManager.openCameraId(cameraId);
onPreview = true;
Expand Down Expand Up @@ -634,7 +626,7 @@ public void startStream(String url) {
private void startEncoders() {
videoEncoder.start();
if (audioInitialized) audioEncoder.start();
prepareGlView();
prepareGlView(videoEncoder.getWidth(), videoEncoder.getHeight(), videoEncoder.getRotation());
if (audioInitialized) microphoneManager.start();
if (glInterface == null && !cameraManager.isRunning() && videoEncoder.getWidth() != previewWidth
|| videoEncoder.getHeight() != previewHeight) {
Expand All @@ -649,19 +641,22 @@ public void requestKeyFrame() {
}
}

private void prepareGlView() {
private void prepareGlView(int width, int height, int rotation) {
if (glInterface != null) {
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(videoEncoder.getHeight(), videoEncoder.getWidth());
} else {
glInterface.setEncoderSize(videoEncoder.getWidth(), videoEncoder.getHeight());
int w = width;
int h = height;
boolean isPortrait = false;
if (rotation == 90 || rotation == 270) {
h = width;
w = height;
isPortrait = true;
}
int rotation = videoEncoder.getRotation();
glInterface.setRotation(rotation == 0 ? 270 : rotation - 90);
if (!cameraManager.isRunning() && videoEncoder.getWidth() != previewWidth
|| videoEncoder.getHeight() != previewHeight) {
glInterface.start();
glInterface.setEncoderSize(w, h);
if (glInterface instanceof GlStreamInterface) {
((GlStreamInterface) glInterface).setPreviewResolution(w, h, isPortrait);
}
glInterface.setRotation(rotation == 0 ? 270 : rotation - 90);
if (!glInterface.isRunning()) glInterface.start();
if (videoEncoder.getInputSurface() != null) {
glInterface.addMediaCodecSurface(videoEncoder.getInputSurface());
}
Expand Down
13 changes: 10 additions & 3 deletions library/src/main/java/com/pedro/library/base/DisplayBase.java
Expand Up @@ -174,10 +174,17 @@ public boolean prepareVideo(int width, int height, int fps, int bitrate, int rot
videoEncoder.prepareVideoEncoder(width, height, fps, bitrate, rotation, iFrameInterval,
FormatVideoEncoder.SURFACE, profile, level);
if (glInterface != null) {
int w = width;
int h = height;
boolean isPortrait = false;
if (rotation == 90 || rotation == 270) {
glInterface.setEncoderSize(videoEncoder.getHeight(), videoEncoder.getWidth());
} else {
glInterface.setEncoderSize(videoEncoder.getWidth(), videoEncoder.getHeight());
h = width;
w = height;
isPortrait = true;
}
glInterface.setEncoderSize(w, h);
if (glInterface instanceof GlStreamInterface) {
((GlStreamInterface) glInterface).setPreviewResolution(w, h, isPortrait);
}
}
return videoInitialized;
Expand Down
13 changes: 10 additions & 3 deletions library/src/main/java/com/pedro/library/base/FromFileBase.java
Expand Up @@ -409,10 +409,17 @@ private void replaceGlInterface(GlInterface glInterface) {

private void prepareGlView() {
if (glInterface != null) {
int w = videoEncoder.getWidth();
int h = videoEncoder.getHeight();
boolean isPortrait = false;
if (videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) {
glInterface.setEncoderSize(videoEncoder.getHeight(), videoEncoder.getWidth());
} else {
glInterface.setEncoderSize(videoEncoder.getWidth(), videoEncoder.getHeight());
h = videoEncoder.getWidth();
w = videoEncoder.getHeight();
isPortrait = true;
}
glInterface.setEncoderSize(w, h);
if (glInterface instanceof GlStreamInterface) {
((GlStreamInterface) glInterface).setPreviewResolution(w, h, isPortrait);
}
glInterface.setRotation(0);
glInterface.start();
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/pedro/library/view/GlInterface.java
Expand Up @@ -195,4 +195,6 @@ public interface GlInterface {
* Not recommendable in others modes.
*/
void setForceRender(boolean enabled);

boolean isRunning();
}
Expand Up @@ -106,6 +106,8 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
setForceRender(enabled, 5)
}

override fun isRunning(): Boolean = running

override fun getSurfaceTexture(): SurfaceTexture {
return mainRender.getSurfaceTexture()
}
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/pedro/library/view/OpenGlView.java
Expand Up @@ -211,6 +211,11 @@ public void setForceRender(boolean enabled) {
setForceRender(enabled, 5);
}

@Override
public boolean isRunning() {
return running;
}

@Override
public void setEncoderSize(int width, int height) {
this.encoderWidth = width;
Expand Down

0 comments on commit 9f2178d

Please sign in to comment.