RootEncoder icon indicating copy to clipboard operation
RootEncoder copied to clipboard

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

Open penmatsa opened this issue 1 year ago • 10 comments

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 avatar Dec 19 '24 12:12 penmatsa

Tried with customizing StreamBase

penmatsa avatar Dec 19 '24 18:12 penmatsa

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

pedroSG94 avatar Dec 20 '24 10:12 pedroSG94

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 avatar Dec 20 '24 11:12 penmatsa

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;
    }

penmatsa avatar Dec 20 '24 13:12 penmatsa

Hello,

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

pedroSG94 avatar Dec 20 '24 13:12 pedroSG94

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

penmatsa avatar Dec 20 '24 17:12 penmatsa

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

pedroSG94 avatar Dec 20 '24 18:12 pedroSG94

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

SharkFourSix avatar Dec 23 '24 10:12 SharkFourSix

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.

penmatsa avatar Dec 24 '24 09:12 penmatsa

Hello,

Sorry for late response. I'm on holidays

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

Yes, you have to add microphone permission to the service and make sure that the MicrophoneSource instance is created inside the Service. For example, the Screen example in my app is using the microphone in background. You can check it

pedroSG94 avatar Dec 30 '24 08:12 pedroSG94