pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.45k stars 761 forks source link

How to keep streaming when rotating device #1480

Open punto338 opened 1 month ago

punto338 commented 1 month ago

Hello, I have a problem when streaming in landscape mode. I want to keep streaming when rotating screen so video output now should be in portrait mode. I have this code but it seems release() method cancels the streaming. I need to keep sending video without stopping streaming and starting again, any idea?

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (genericStream != null && genericStream.isOnPreview()) {
            genericStream.stopPreview();
            genericStream.release();
            mCameraView.getHolder().addCallback(this);
        }
    }
pedroSG94 commented 1 month ago

Hello,

You have a code example to support rotation here: https://github.com/pedroSG94/RootEncoder/tree/master/app/src/main/java/com/pedro/streamer/rotation

punto338 commented 1 month ago

Hello,

You have a code example to support rotation here: https://github.com/pedroSG94/RootEncoder/tree/master/app/src/main/java/com/pedro/streamer/rotation

I am trying to streaming portrait and landscape mode and both make output of 1920x1080, so in portrait mode I get black space on the sides, is any way to make the output same size as the video? I see OK when in the device which is streaming but in the "player" device its wrong, see next comment:

punto338 commented 1 month ago

See pictures, first one is landscape, which is OK, and the other one is portrait mode but it seems video is outputing in landscape mode:

WhatsApp Image 2024-05-17 at 12 15 03 (1) WhatsApp Image 2024-05-17 at 12 15 03

pedroSG94 commented 1 month ago

Hello,

This is the expected result.

You can't change the video size while you are streaming/recording so the video is adapted depend of the device orientation to avoid distortion. You can choose use 1080x1920 and you will have the same case but inverted (portrait is filled and landscape with black borders).

punto338 commented 1 month ago

I mean I am not trying to change video size while streaming. Use case:

-I have portrait camera, it show in my preview fullscreen and good, then I start recording and in the player I get an output of 1920x1080 with the streaming small in the middle, and black on the sides

This is my code. Its working in landscape but not in portrait. bestSize is 2400 width 1080 height

Size bestSize = getBestCameraResolution();
genericStream.getGlInterface().setAspectRatioMode(AspectRatioMode.Fill);
genericStream.getGlInterface().setAutoHandleOrientation(true);
genericStream.prepareVideo(bestSize.getWidth(), bestSize.getHeight(), 1200 * 1000);
genericStream.prepareAudio(32000, true, 128 * 1000);
punto338 commented 1 month ago

@pedroSG94 I was able to fix this with the rotation in prepareVideo() method with this code, similar to the example you gave:

genericStream.prepareVideo(width, height, vBitrate, rotation = rotation)

 genericStream.prepareVideo(
                    bestSize.getWidth(),
                    bestSize.getHeight(),
                    1200 * 1000,
                    30,
                    2,
                    getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? 90 : 0
            );

How can I set just the orientation like you using Java, without setting fps and iFrameInterval param? If not possible, which are the default values for fps and iFrameInterval ?