Release capture before service ownership and report startup failures
This commit is contained in:
1 parent
c9743ee85d
commit
e09ee919a5
3 files changed
+20
-7
No files matched your search
@@ -114,7 +114,9 @@ class StreamCaptureEngine(
|
|||||||
}
|
}
|
||||||
} catch (e: Exception) { failed("encoded frame: ${e.message}") }
|
} catch (e: Exception) { failed("encoded frame: ${e.message}") }
|
||||||
finally { if (!stopped) codec.releaseOutputBuffer(index, false) }
|
finally { if (!stopped) codec.releaseOutputBuffer(index, false) }
|
||||||
if (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) finishStop()
|
if (info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) {
|
||||||
|
if (stopping) finishStop() else failed("unexpected encoder EOS")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, handler)
|
}, handler)
|
||||||
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
||||||
|
|||||||
@@ -57,12 +57,13 @@ object StreamController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun failed(id: Long, detail: String) {
|
fun failed(id: Long, detail: String) {
|
||||||
main.post {
|
val update = Runnable {
|
||||||
val st = mutable.value ?: return@post
|
val st = mutable.value
|
||||||
if (st.id != id || !st.ongoing) return@post
|
if (st == null || st.id != id || !st.ongoing) return@Runnable
|
||||||
LogManager.addLog("ERROR", "stream", "publisher failed id=$id $detail")
|
LogManager.addLog("ERROR", "stream", "publisher failed id=$id $detail")
|
||||||
mutable.value = st.copy(error = R.string.stream_failed); stop()
|
mutable.value = st.copy(error = R.string.stream_failed); stop()
|
||||||
}
|
}
|
||||||
|
if (Looper.myLooper() == Looper.getMainLooper()) update.run() else main.post(update)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import android.content.Intent
|
|||||||
import android.content.pm.ServiceInfo
|
import android.content.pm.ServiceInfo
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.ServiceCompat
|
import androidx.core.app.ServiceCompat
|
||||||
@@ -95,6 +97,7 @@ class StreamService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun finish() {
|
private fun finish() {
|
||||||
|
if (capture != null) { shutdown(abort = true); return }
|
||||||
poll?.cancel(); poll = null
|
poll?.cancel(); poll = null
|
||||||
lock?.let { if (it.isHeld) it.release() }; lock = null
|
lock?.let { if (it.isHeld) it.release() }; lock = null
|
||||||
StreamController.ended(id, this)
|
StreamController.ended(id, this)
|
||||||
@@ -103,9 +106,16 @@ class StreamService : Service() {
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
poll?.cancel()
|
poll?.cancel()
|
||||||
capture?.stop { NativeLib.streamStop(id) }; capture = null
|
val engine = capture; capture = null
|
||||||
lock?.let { if (it.isHeld) it.release() }; lock = null
|
val ownedLock = lock; lock = null
|
||||||
StreamController.ended(id, this); scope.cancel()
|
val release = {
|
||||||
|
NativeLib.streamStop(id)
|
||||||
|
ownedLock?.let { if (it.isHeld) it.release() }
|
||||||
|
Handler(Looper.getMainLooper()).post { StreamController.ended(id, this) }
|
||||||
|
Unit
|
||||||
|
}
|
||||||
|
if (engine != null) engine.stop(release) else release()
|
||||||
|
scope.cancel()
|
||||||
LogManager.addLog("INFO", "stream", "foreground destroyed id=$id")
|
LogManager.addLog("INFO", "stream", "foreground destroyed id=$id")
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user