Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we stream to RTMP when activity is put in background. #1682

Open
penmatsa opened this issue Dec 19, 2024 · 9 comments
Open

How can we stream to RTMP when activity is put in background. #1682

penmatsa opened this issue Dec 19, 2024 · 9 comments

Comments

@penmatsa
Copy link

penmatsa commented Dec 19, 2024

Hello,

RTMP Streaming using GenericStream and TextureView working good when activity is in foreground, but when app is put in background RTMP streaming stops.

NOTE: Can't use Media Projection as we need to record HD content

//STEP: 1 For showing Camera Preview

genericStream.prepareVideo(1280, 720, bitrate, 30,2, 0, -1, -1, 1280, 720, 30);
genericStream.prepareAudio(sampleRate, isStereo, aBitrate);
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {

@Override public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {

    if (!genericStream.isOnPreview()) {
        genericStream.startPreview(surface, mTextureView.getWidth(), mTextureView.getHeight());
        AndroidViewFilterRender androidViewFilterRender = new AndroidViewFilterRender();
        androidViewFilterRender.setView(mWebView);
        androidViewFilterRender.setPosition(0f, 0f);
        genericStream.getGlInterface().setFilter(androidViewFilterRender);
        isCameraOpened = true;
    }
}

@Override public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
    genericStream.getGlInterface().setPreviewResolution(width, height);
}

@Override public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
    return true;
}

@Override public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

}

});

//STEP 2: For RTMP streaming and recording local video

genericStream.startStream(rtmpUrl);
genericStream.startRecord(mediaFile.getPath(), new RecordController.Listener() {
@OverRide
public void onStatusChange(RecordController.Status status) {

        }
    });

How can we stream camera content when app is in backgound

@penmatsa penmatsa reopened this Dec 19, 2024
@penmatsa
Copy link
Author

penmatsa commented Dec 19, 2024

Tried with customizing StreamBase

@pedroSG94
Copy link
Owner

Hello,

I don't fully understand you but you have a code example to stream in background using the app example. In this case that example use ScreenSource as videoSource but you can use any VideoSource.
Remember that if you want stream in background with Android you need create a service a declare in the service that you will use mediaprojection, camera or microphone like this:
https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/AndroidManifest.xml#L73
App code example:
https://github.com/pedroSG94/RootEncoder/tree/master/app/src/main/java/com/pedro/streamer/screen

@penmatsa
Copy link
Author

Hello,

We are using Camera2Source for streaming and recording . we are testing on Android 12 device.

Will try adding Service with Camera2Source.

@pedroSG94 Thanks.

@penmatsa
Copy link
Author

Hello,

Getting below error when using camera2Source with foreground Service and app is put in background

java.lang.RuntimeException: drawScreen end. GL error: 1285
com.pedro.encoder.utils.gl.GlUtil.checkGlError(GlUtil.java:132)
com.pedro.encoder.input.gl.render.ScreenRender.draw(ScreenRender.java:162)
com.pedro.encoder.input.gl.render.ScreenRender.drawEncoder(ScreenRender.java:113)
com.pedro.encoder.input.gl.render.MainRender.drawScreenEncoder(MainRender.kt:72)
com.pedro.library.view.GlStreamInterface.draw(GlStreamInterface.kt:233)
com.pedro.library.view.GlStreamInterface.onFrameAvailable$lambda$9(GlStreamInterface.kt:268)
com.pedro.library.view.GlStreamInterface.$r8$lambda$BAtYl6ZVCZbXX960JSr5g3IQUyw(Unknown Source:0)
com.pedro.library.view.GlStreamInterface$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)

Using below code inside Service class

Code:

    genericStream.prepareVideo(1280, 720, bitrate, 30, 2, 0, -1, -1, 1280, 720, 30);
    genericStream.prepareAudio(sampleRate, isStereo, aBitrate);
    if (!genericStream.isOnPreview()) {
        genericStream.startPreview(textureView);
        AndroidViewFilterRender androidViewFilterRender = new AndroidViewFilterRender();
        androidViewFilterRender.setView(mWebView);
        androidViewFilterRender.setPosition(0f, 0f);
        genericStream.getGlInterface().setFilter(androidViewFilterRender);
        isCameraOpened = true;
    }

@pedroSG94
Copy link
Owner

Hello,

For now, try to remove the Android view filter. Maybe the problem is that you can't render that filter properly.

@penmatsa
Copy link
Author

Hi @pedroSG94,

Camera2Source RTMP streaming in background service is working good only when Android view filter is removed.

please suggest steps to add Android view filter with backgound streaming.

Thanks

@pedroSG94
Copy link
Owner

Ok, in this case you should change the way you are creating that view used in the filter.

Can you share the way you are inflating the view?
I suggest you try to inflate a XML in background like in this code:
https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/java/com/pedro/streamer/utils/FilterMenu.kt#L100

@SharkFourSix
Copy link

Hi @pedroSG94,

Camera2Source RTMP streaming in background service is working good only when Android view filter is removed.

please suggest steps to add Android view filter with backgound streaming.

Thanks

You can try using picture-in-picture mode. At least that will keep your activity running but also visible, albeit taking up a 3rd of your screen. You'll be able to interact with your device at least

@penmatsa
Copy link
Author

Hi @pedroSG94,

Adding overlays as you suggested are working fine for RTMP streaming using foreground service.

When app is in background microphone audio is not working. I think its a restriction from Android 12.

Thanks for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants