Closed r-nov closed 4 months ago
Hello,
I need more info. I need know:
Hi,
Explain in detail the error experimented, like: when I re start, the stream never start and onConnectionStarted is not received, I always receive connection failed X, etc.
Roughly speaking it's service example from version 2.3.1. My steps are: start the stream with BackgroundActivity
, hide the app to the background, restart the stream when app is in the background. To restart the stream in the background I'm using following code:
stopStream()
if (prepare()) {
startStream(url)
With this code stream stops but doesn't start.
It doesn't move further than waiting for sps and pps
and there's always this error in logcat [SurfaceTexture-1-3428-1] setDefaultBufferSize: SurfaceTexture is abandoned!
(probably not related?).
As you recommended I don't need to stop/start preview in background (furthermore preview is stopped when app is in the background) but during my experiments I have figured out that after I call stopPreview like this:
stopStream()
stopPreview() // <-- here
if (prepare()) {
startStream(url)
while app is in the foreground, I can restart stream in background with same code. Again, only if I call this code at least once when app is in the foreground. Don't know if it help, just mention it just in case. Let me know it there's additional info you need to know.
The code used inside that methods and all code related to debug it and reproduce the case.
To reproduce my case with same code I'd like to ask you:
6f98f00a73e2f5604859bbc317ec9c8b349a541e
) since I'm using this version without changing these methodsRtmpCamera2
with RtspCamera2
in RtpService.kt
Since I need to restart stream in the background, add following code to surfaceDestroyed()
in BackgroundActivity.kt
to simulate my actions.
override fun surfaceDestroyed(holder: SurfaceHolder) {
service?.setView(this)
if (service?.isOnPreview() == true) service?.stopPreview()
CoroutineScope(Dispatchers.Default).launch {
delay(5000)
service?.stopStream()
delay(2000)
if (service?.prepare() == true) {
service?.startStream("url")
}
}
}
Make sure that you stop stream, preview and record before prepare and start stream again. If you are using background you don't need use startPreview and stopPreview.
Checked it again, stream, preview and record stopped when I'm calling prepare() and startStream().
Are you moving from foreground to background or are you always in background?
I'm moving from foreground to background with stream running.
Let me know if I missed something and you need additional info. Thank you.
Hello,
This is the mentioned class in version 2.3.1: https://github.com/pedroSG94/RootEncoder/blob/2.3.1/app/src/main/java/com/pedro/streamer/backgroundexample/BackgroundActivity.kt
I don't understand the reason to stop and start stream again when you go to background if you can continue the current stream like in the example. Using the example you can move to foreground and background without re start stream. Try this way if it is valid for you, if not explain me the reason to find a better way.
Also, can you check onConnectionStarted callback and onConnectionFailed callback and share me the string in the callback when you re start stream?
Sorry for misunderstanding :) Should mention it earlier, but the purpose is to control (including restart) the stream remotely through HTTP when the app is in the background. So I have added an example with coroutine and delay because it's an easy way to simulate my case with remote restart. I have no problem with moving to background and continuing the streaming, your example works just fine.
Also, can you check onConnectionStarted callback and onConnectionFailed callback and share me the string in the callback when you re start stream?
Sure.
onConnectionStarted callback contains correct url rtsp://192.168.1.10:8554/stream
and onConnectionFailed contains sps or pps is null
.
Sorry for misunderstanding :) Should mention it earlier, but the purpose is to control (including restart) the stream remotely through HTTP when the app is in the background. So I have added an example with coroutine and delay because it's an easy way to simulate my case with remote restart.
I see, but you have betters ways to do it because the current way produce a really bad experience for the user because the stream stop fews second before it start again. You can create an api rest in the server side to notify about it using a simple http request or use a socket with socket.io for example if you want a real time result.
onConnectionStarted callback contains correct url
rtsp://192.168.1.10:8554/stream
and onConnectionFailed containssps or pps is null
.
I will check it because this should works but, check that you have this error in library version 2.4.4 and then I will try to fix it in the last library version. Also, make sure that you only added the code mentioned in the onSurfaceDestroyed and notify me if you have more code to modify in order to reproduce the case.
You can create an api rest in the server side to notify about it using a simple http request
I have exactly this implementation with REST api on my side, sorry if my example was confusing, it's just for debug purposes :)
Also, make sure that you only added the code mentioned in the onSurfaceDestroyed and notify me if you have more code to modify in order to reproduce the case.
Checked it on more time, there's no other code, I've created a clean project and changed only parts that I mentioned (onSurfaceDestroyed
and changed RtmpCamera2
to RtspCamera2
).
check that you have this error in library version 2.4.4 and then I will try to fix it in the last library version.
Checked with last 2.4.4 version. Behaviour is the same, correct url but still sps or pps is null
in onConnectionFailed
.
Hello,
Error fixed here: https://github.com/pedroSG94/RootEncoder/commit/39bd2f1e89d664fedf865d0addb4e5ca96733054
You can compile the last commit using this gradle:
implementation 'com.github.pedroSG94.RootEncoder:library:39bd2f1e89'
Or wait for the release
Thank you very much for the quick fix, works like a charm!
Hi @pedroSG94!
I'd like to ask you about restarting stream while app in background (encoder running in service). I'm using RootEncoder 2.3.1, I know it's quite old but I tried newer versions and seems like behaviour is the same. I have no problems with restarting stream when app is in foreground (OpenGlView) with simple:
but can't restart stream this way when app is in background (OffScreenGlThread).
The only way I managed it to restart is add
stopPreview()
afterstopStream()
BUT it's working only if I restarted stream this way at least once when app is in foreground.I mean if I restart stream with this code at least once while app is open I can restart stream in background, but otherwise behaviour is the same as without this
stopPreview()
.This is logcat when I try to restart stream in background (without stopPreview, just stop -> prepare -> start):
My guess is that I don't handle surface correctly, but I couldn't figure out the approach to manage it.
Thank you.