Clean camera resources independently of RX and image release failures
This commit is contained in:
1 parent
c085ab8d5e
commit
3bac31efee
1 file changed
+28
-6
@@ -78,11 +78,13 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
private val sessions = mutableMapOf<Long, Pair<CameraDevice, () -> Unit>>()
|
||||
@Volatile private var stopped = false
|
||||
@Volatile private var cleanupComplete = false
|
||||
@Volatile private var rxReleased = false
|
||||
private var cleanupQueued = false // Под монитором engine: один callback, более ранний запрос заменяет таймер.
|
||||
private var cleanupDue = 0L
|
||||
private var cleanupDelay = 250L
|
||||
private var portOwned = false // TX: выставляется до native open, снимается только после close.
|
||||
private val ownedReaders = linkedSetOf<ImageReader>()
|
||||
private val ownedImages = linkedSetOf<android.media.Image>()
|
||||
private val closingCameras = linkedSetOf<CameraDevice>()
|
||||
private val closedCameras = linkedSetOf<CameraDevice>() // Отсекает повтор уже подтверждённого SDK callback.
|
||||
private val ownedSessions = linkedMapOf<CameraCaptureSession, CameraDevice>()
|
||||
@@ -459,9 +461,12 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
val nextReader = ImageReader.newInstance(settings.width, settings.height, ImageFormat.YUV_420_888, 3)
|
||||
ownedReaders.add(nextReader); reader = nextReader
|
||||
nextReader.setOnImageAvailableListener({ source ->
|
||||
val image = try { source.acquireLatestImage() } catch (e: IllegalStateException) {
|
||||
LogManager.addLog("WARN", "call", "closed camera reader token=$token/$cameraToken: ${e.message}"); null
|
||||
val image = try { source.acquireLatestImage() } catch (e: Exception) {
|
||||
if (stopped || token != cameraToken) LogManager.addLog("WARN", "call", "closed camera reader token=$token/$cameraToken: $e")
|
||||
else fail("Camera image acquisition: $e")
|
||||
null
|
||||
} ?: return@setOnImageAvailableListener
|
||||
ownedImages.add(image)
|
||||
try {
|
||||
if (stopped || token != cameraToken || !wanted.enabled) return@setOnImageAvailableListener
|
||||
val now = System.nanoTime()
|
||||
@@ -526,7 +531,13 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
transformMaxUs = maxOf(transformMaxUs, (System.nanoTime() - transformStarted) / 1000)
|
||||
drain()
|
||||
} catch (e: Exception) { fail("Camera encode: ${e.message}") }
|
||||
finally { image.close() }
|
||||
finally {
|
||||
try { image.close(); ownedImages.remove(image) } catch (e: Exception) {
|
||||
LogManager.addLog("ERROR", "call", "camera Image close failed id=$id token=$token; retained: $e")
|
||||
if (!stopped && token == cameraToken) fail("Camera image release: $e")
|
||||
queueCleanup(0)
|
||||
}
|
||||
}
|
||||
}, handler)
|
||||
opening = true; openingToken = token; openedAt = System.nanoTime(); lastCaptureAt = openedAt
|
||||
try { manager.openCamera(settings.camera, object : CameraDevice.StateCallback() {
|
||||
@@ -717,6 +728,9 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
for (owned in closingCameras.toList()) try { owned.close() } catch (e: Exception) {
|
||||
LogManager.addLog("ERROR", "call", "camera device close failed id=$id device=${owned.id}; retained: $e")
|
||||
}
|
||||
for (owned in ownedImages.toList()) try { owned.close(); ownedImages.remove(owned) } catch (e: Exception) {
|
||||
LogManager.addLog("ERROR", "call", "camera Image cleanup failed id=$id; retained: $e")
|
||||
}
|
||||
if (!opening && camera == null && closingCameras.isEmpty()) {
|
||||
for (owned in ownedReaders.toList()) try {
|
||||
owned.close(); ownedReaders.remove(owned)
|
||||
@@ -728,7 +742,8 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
val release = pendingEncoders.toMutableSet()
|
||||
if (!wanted.enabled) encoder?.let { release.add(it) }
|
||||
for (owned in release) releaseEncoder(owned)
|
||||
val pending = opening || closing || closingCameras.isNotEmpty() || closingSessions.isNotEmpty() || pendingEncoders.isNotEmpty() ||
|
||||
val pending = opening || closing || closingCameras.isNotEmpty() || closingSessions.isNotEmpty() || ownedImages.isNotEmpty() ||
|
||||
pendingEncoders.isNotEmpty() || (stopped && !rxReleased) ||
|
||||
(!wanted.enabled && (encoder != null || orientationEnabled))
|
||||
if (stopped && !pending) {
|
||||
if (portOwned) try { CallVideoNative.close(id); portOwned = false } catch (e: Exception) {
|
||||
@@ -745,7 +760,7 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
if (pending || (stopped && portOwned)) {
|
||||
LogManager.addLog("DEBUG", "call", "video cleanup pending id=$id opening=$opening closing=$closing " +
|
||||
"devices=${closingCameras.size} sessions=${ownedSessions.size}/${sessions.size} readers=${ownedReaders.size} " +
|
||||
"encoders=${pendingEncoders.size} sensor=$orientationEnabled port=$portOwned")
|
||||
"images=${ownedImages.size} encoders=${pendingEncoders.size} sensor=$orientationEnabled rxReleased=$rxReleased port=$portOwned")
|
||||
queueCleanup(cleanupDelay); cleanupDelay = (cleanupDelay * 2).coerceAtMost(30_000)
|
||||
} else {
|
||||
cleanupDelay = 250
|
||||
@@ -775,8 +790,14 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
"Video stop requires main"
|
||||
}
|
||||
if (cleanupComplete) return true
|
||||
retained.add(this); stopped = true; player.stop()
|
||||
retained.add(this); stopped = true
|
||||
var rxFailure: Exception? = null
|
||||
try { player.stop(); rxReleased = true } catch (e: Exception) {
|
||||
rxFailure = e
|
||||
LogManager.addLog("ERROR", "call", "video RX stop pending id=$id; continuing independent TX cleanup: $e")
|
||||
}
|
||||
if (!::handler.isInitialized) {
|
||||
rxFailure?.let { throw it }
|
||||
preview.close(); thread.quitSafely(); cleanupComplete = true; retained.remove(this)
|
||||
LogManager.addLog("INFO", "call", "video stop before TX setup id=$id")
|
||||
return true
|
||||
@@ -785,6 +806,7 @@ class CallVideoEngine(private val context: Context, private val id: Long) {
|
||||
LogManager.addLog("ERROR", "call", "video stop cleanup rejected id=$id; stop remains retryable")
|
||||
"Video cleanup worker rejected stop"
|
||||
}
|
||||
rxFailure?.let { throw it }
|
||||
return cleanupComplete
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user