Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions app/src/main/java/com/mobilenext/devicekit/AvcServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
"--fps" -> {
if (i + 1 < args.size) {
val parsedFps = args[i + 1].toIntOrNull()
if (parsedFps == null) {
throw IllegalArgumentException("Invalid fps value: ${args[i + 1]}. Must be an integer between $MIN_FPS and $MAX_FPS")
}
if (parsedFps < MIN_FPS || parsedFps > MAX_FPS) {
throw IllegalArgumentException("fps value out of range: $parsedFps. Must be between $MIN_FPS and $MAX_FPS")
fps = if (parsedFps != null && parsedFps in MIN_FPS..MAX_FPS) {
parsedFps
} else {
Log.w(TAG, "Invalid fps value: ${args[i + 1]}. Using default: $DEFAULT_FPS")
DEFAULT_FPS
}
fps = parsedFps
i++
}
}
Expand Down Expand Up @@ -103,6 +102,33 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
shutdownLatch.countDown()
}

private fun cleanupResources(
stdoutChannel: java.nio.channels.FileChannel?,
codec: MediaCodec?,
virtualDisplay: VirtualDisplay?
) {
try {
stdoutChannel?.close()
} catch (e: Exception) {
Log.e(TAG, "Error closing stdout channel", e)
}
try {
codec?.stop()
} catch (e: Exception) {
Log.e(TAG, "Error stopping codec", e)
}
try {
codec?.release()
} catch (e: Exception) {
Log.e(TAG, "Error releasing codec", e)
}
try {
virtualDisplay?.release()
} catch (e: Exception) {
Log.e(TAG, "Error releasing virtual display", e)
}
}

private fun streamAvcFrames() {
val displayInfo = DisplayUtils.getDisplayInfo()
val scaledWidth = (displayInfo.width * scale).toInt()
Expand Down Expand Up @@ -163,6 +189,8 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
// Low latency settings
setInteger(MediaFormat.KEY_LATENCY, 0) // Request lowest latency
setInteger(MediaFormat.KEY_PRIORITY, 0) // Realtime priority
// Repeat previous frame after 100ms to keep stream alive when screen is static
setLong(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 100_000L) // 100ms in microseconds
}

Log.d(TAG, "MediaFormat created: $format")
Expand Down Expand Up @@ -206,7 +234,7 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
Log.d(TAG, "AVC encoder started")

val bufferInfo = MediaCodec.BufferInfo()
val timeout = 10000L // 10ms timeout for lower latency
val timeout = 100_000L // 100ms timeout for responsive shutdown (matches REPEAT_FRAME_DELAY)
Copy link

@cubic-dev-ai cubic-dev-ai bot Jan 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Comment references non-existent constant REPEAT_FRAME_DELAY. Consider updating to reference KEY_REPEAT_PREVIOUS_FRAME_AFTER which is the actual setting being matched.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/src/main/java/com/mobilenext/devicekit/AvcServer.kt, line 237:

<comment>Comment references non-existent constant `REPEAT_FRAME_DELAY`. Consider updating to reference `KEY_REPEAT_PREVIOUS_FRAME_AFTER` which is the actual setting being matched.</comment>

<file context>
@@ -206,7 +234,7 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
 
         val bufferInfo = MediaCodec.BufferInfo()
-        val timeout = 10000L  // 10ms timeout for lower latency
+        val timeout = 100_000L  // 100ms timeout for responsive shutdown (matches REPEAT_FRAME_DELAY)
 
         // Get FileChannel for stdout to write directly from ByteBuffer (zero-copy)
</file context>
Suggested change
val timeout = 100_000L // 100ms timeout for responsive shutdown (matches REPEAT_FRAME_DELAY)
val timeout = 100_000L // 100ms timeout for responsive shutdown (matches KEY_REPEAT_PREVIOUS_FRAME_AFTER)
Fix with Cubic


// Get FileChannel for stdout to write directly from ByteBuffer (zero-copy)
val stdoutChannel = FileOutputStream(FileDescriptor.out).channel
Expand Down Expand Up @@ -243,9 +271,9 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
}
} catch (e: IOException) {
// Pipe broken - client disconnected
Log.d(TAG, "Output pipe broken, shutting down")
shutdown()
break
Log.d(TAG, "Output pipe broken, cleaning up and exiting")
cleanupResources(stdoutChannel, codec, virtualDisplay)
exitProcess(0)
}

// Log frame info
Expand Down Expand Up @@ -297,10 +325,7 @@ class AvcServer(private val bitrate: Int, private val scale: Float, private val
}
} finally {
Log.d(TAG, "Stopping AVC encoder")
stdoutChannel.close()
codec.stop()
codec.release()
virtualDisplay.release()
cleanupResources(stdoutChannel, codec, virtualDisplay)
}
}
}