first commit

This commit is contained in:
srtk 2025-11-21 21:37:05 +05:30
commit 797c5c48d7
59 changed files with 3217 additions and 0 deletions

View file

@ -0,0 +1,550 @@
package com.srtk.airlink
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.camera2.*
import android.media.MediaCodec
import android.media.MediaCodecInfo
import android.media.MediaFormat
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.util.Range
import android.view.Surface
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
import kotlin.math.abs
import kotlin.math.min
data class VideoConfig(val width: Int, val height: Int, val fps: Int) {
override fun toString() = "${height}p"
fun toDetailedString() = "${height}p (${width}×${height} @ ${fps}fps)"
}
data class CameraCapabilities(
val supportedConfigs: List<VideoConfig>,
val maxZoom: Float,
val hasFlash: Boolean,
val hasFrontCamera: Boolean,
val supportedWhiteBalance: List<WhiteBalance>,
val focusRange: Range<Float>?,
val exposureRange: Range<Int>?
)
enum class WhiteBalance(val value: Int, val label: String) {
AUTO(CameraMetadata.CONTROL_AWB_MODE_AUTO, "Auto"),
WARM(CameraMetadata.CONTROL_AWB_MODE_INCANDESCENT, "Warm"),
COOL(CameraMetadata.CONTROL_AWB_MODE_FLUORESCENT, "Cool")
}
class CameraStreamer(private val context: Context) {
private val TAG = "CameraStreamer"
private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
private val cameraThread = HandlerThread("CamThread").apply { start() }
private val camHandler = Handler(cameraThread.looper)
private var cameraDevice: CameraDevice? = null
private var captureSession: CameraCaptureSession? = null
private var requestBuilder: CaptureRequest.Builder? = null
private var mediaCodec: MediaCodec? = null
private var socket: DatagramSocket? = null
@Volatile private var isStreaming = false
private val restartLock = Semaphore(1)
private var currentConfig = VideoConfig(960, 720, 30)
private var bitrate = 3000000
private var zoomRatio = 1f
private var flashMode = false
private var wbMode = CameraMetadata.CONTROL_AWB_MODE_AUTO
private var focusMode = CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_VIDEO
private var focusDistance = 0f
private var exposureCompensation = 0
private var onErrorCallback: ((String) -> Unit)? = null
private var onDisconnectedCallback: (() -> Unit)? = null
private var lastPacketTime = 0L
private val connectionTimeout = 5000L // 5 seconds
fun setErrorCallback(callback: (String) -> Unit) {
onErrorCallback = callback
}
fun setDisconnectedCallback(callback: () -> Unit) {
onDisconnectedCallback = callback
}
// Test if a resolution actually works with the encoder
private fun testEncoderResolution(width: Int, height: Int): Boolean {
var testCodec: MediaCodec? = null
try {
val format = MediaFormat.createVideoFormat(
MediaFormat.MIMETYPE_VIDEO_AVC,
width,
height
).apply {
setInteger(
MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface
)
setInteger(MediaFormat.KEY_BIT_RATE, 2000000)
setInteger(MediaFormat.KEY_FRAME_RATE, 30)
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
}
testCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC)
testCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
testCodec.release()
Log.d(TAG, "✅ Resolution ${width}x${height} WORKS")
return true
} catch (e: Exception) {
Log.d(TAG, "❌ Resolution ${width}x${height} FAILED: ${e.message}")
return false
} finally {
try {
testCodec?.release()
} catch (e: Exception) {}
}
}
private fun is16by9(width: Int, height: Int): Boolean {
val ratio = width.toDouble() / height.toDouble()
return abs(ratio - 16.0 / 9.0) < 0.1
}
private fun is4by3(width: Int, height: Int): Boolean {
val ratio = width.toDouble() / height.toDouble()
return abs(ratio - 4.0 / 3.0) < 0.1
}
fun getCameraCapabilities(useFront: Boolean): CameraCapabilities {
val camId = getCameraId(useFront)
val chars = manager.getCameraCharacteristics(camId)
val map = chars.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!!
val allSizes = map.getOutputSizes(MediaCodec::class.java)
.filter { is16by9(it.width, it.height) || is4by3(it.width, it.height) }
.sortedByDescending { it.width * it.height }
Log.d(TAG, "Testing encoder capabilities...")
val configs = mutableListOf<VideoConfig>()
val fpsRanges = chars.get(
CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES
) ?: emptyArray()
for (size in allSizes) {
try {
// TEST if encoder actually supports this resolution
if (!testEncoderResolution(size.width, size.height)) {
continue
}
val minFrameDuration = map.getOutputMinFrameDuration(
MediaCodec::class.java,
size
)
if (minFrameDuration <= 0) continue
val maxFpsFromDuration = (1_000_000_000.0 / minFrameDuration).toInt()
val validRanges = fpsRanges.filter {
it.upper <= maxFpsFromDuration && it.upper >= 15
}
val targetFps = min(validRanges.maxOfOrNull { it.upper } ?: 30, 30)
if (targetFps >= 15) {
configs.add(VideoConfig(size.width, size.height, targetFps))
}
} catch (e: Exception) {
Log.w(TAG, "Skipping ${size.width}x${size.height}: ${e.message}")
}
}
if (configs.isEmpty()) {
configs.add(VideoConfig(640, 480, 30))
Log.w(TAG, "No configs found, added 640x480 fallback")
}
val maxZoom = chars.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM) ?: 1f
val hasFlash = chars.get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true
val hasFront = manager.cameraIdList.any {
manager.getCameraCharacteristics(it).get(CameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_FRONT
}
val minFocus = chars.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE)
val exposureRange = chars.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_RANGE)
return CameraCapabilities(
configs.sortedByDescending { it.width * it.height },
maxZoom,
hasFlash,
hasFront,
listOf(WhiteBalance.AUTO, WhiteBalance.WARM, WhiteBalance.COOL),
if (minFocus != null && minFocus > 0) Range(0f, minFocus) else null,
exposureRange
)
}
fun start(
ip: InetAddress,
port: Int,
config: VideoConfig,
useFront: Boolean
) {
currentConfig = config
val camId = getCameraId(useFront)
camHandler.post {
try {
if (restartLock.tryAcquire(3, TimeUnit.SECONDS)) {
try {
stopInternal()
Thread.sleep(500)
startInternal(ip, port, camId)
} finally {
restartLock.release()
}
}
} catch (e: Exception) {
Log.e(TAG, "Start error", e)
onErrorCallback?.invoke("Failed to start: ${e.message}")
}
}
}
fun updateBitrate(mbps: Int) {
val newBitrate = mbps * 1_000_000
if (bitrate == newBitrate) return
bitrate = newBitrate
camHandler.post {
try {
mediaCodec?.let { codec ->
val bundle = Bundle().apply {
putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, bitrate)
}
codec.setParameters(bundle)
Log.d(TAG, "Bitrate updated to ${mbps} Mbps")
}
} catch (e: Exception) {
Log.e(TAG, "Bitrate update failed", e)
}
}
}
fun setZoom(z: Float) {
zoomRatio = z
updateSession()
}
fun setTorch(on: Boolean) {
flashMode = on
updateSession()
}
fun setWhiteBalance(wb: WhiteBalance) {
wbMode = wb.value
updateSession()
}
fun setFocusMode(auto: Boolean) {
focusMode = if (auto) CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_VIDEO
else CameraMetadata.CONTROL_AF_MODE_OFF
updateSession()
}
fun setFocusDistance(distance: Float) {
focusDistance = distance
focusMode = CameraMetadata.CONTROL_AF_MODE_OFF
updateSession()
}
fun setExposure(value: Int) {
exposureCompensation = value
updateSession()
}
@SuppressLint("MissingPermission")
private fun startInternal(
ip: InetAddress,
port: Int,
camId: String
) {
try {
socket = DatagramSocket()
isStreaming = true
lastPacketTime = System.currentTimeMillis()
val width = currentConfig.width
val height = currentConfig.height
val pixelCount = width * height
val calculatedBitrate = when {
pixelCount >= 1920 * 1080 -> 8_000_000
pixelCount >= 1280 * 720 -> 5_000_000
pixelCount >= 640 * 480 -> 2_500_000
else -> 1_500_000
}
Log.d(TAG, "Starting: ${width}x${height} @ ${currentConfig.fps}fps")
val format = MediaFormat.createVideoFormat(
MediaFormat.MIMETYPE_VIDEO_AVC,
width,
height
).apply {
setInteger(
MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface
)
setInteger(MediaFormat.KEY_BIT_RATE, calculatedBitrate)
setInteger(MediaFormat.KEY_FRAME_RATE, currentConfig.fps)
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
}
mediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC)
mediaCodec?.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
val surface = mediaCodec?.createInputSurface()
?: throw Exception("Failed to create input surface")
mediaCodec?.start()
Log.d(TAG, "✅ Encoder started with ${width}x${height}")
// Start connection monitor
Thread {
while (isStreaming) {
Thread.sleep(1000)
if (System.currentTimeMillis() - lastPacketTime > connectionTimeout) {
Log.w(TAG, "Server disconnected - no data sent")
stop()
onDisconnectedCallback?.invoke()
break
}
}
}.start()
Thread {
Thread.currentThread().priority = Thread.MAX_PRIORITY
streamLoop(ip, port)
}.start()
manager.openCamera(
camId,
object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
cameraDevice = camera
createSession(camera, surface)
}
override fun onDisconnected(camera: CameraDevice) {
Log.w(TAG, "Camera disconnected")
stopInternal()
}
override fun onError(camera: CameraDevice, error: Int) {
Log.e(TAG, "Camera error: $error")
stopInternal()
onErrorCallback?.invoke("Camera error: $error")
}
},
camHandler
)
} catch (e: Exception) {
Log.e(TAG, "Start failed: ${e.message}", e)
onErrorCallback?.invoke("Failed to start: ${e.message}")
stopInternal()
}
}
private fun createSession(camera: CameraDevice, surface: Surface) {
try {
camera.createCaptureSession(
listOf(surface),
object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {
if (!isStreaming) return
captureSession = session
requestBuilder = camera.createCaptureRequest(
CameraDevice.TEMPLATE_RECORD
).apply {
addTarget(surface)
set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO)
set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON)
set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
Range(currentConfig.fps, currentConfig.fps))
set(CaptureRequest.CONTROL_AF_MODE, focusMode)
set(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE,
CameraMetadata.CONTROL_VIDEO_STABILIZATION_MODE_ON)
if (focusMode == CameraMetadata.CONTROL_AF_MODE_OFF) {
set(CaptureRequest.LENS_FOCUS_DISTANCE, focusDistance)
}
set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, exposureCompensation)
}
updateSession()
Log.d(TAG, "Camera session started")
}
override fun onConfigureFailed(session: CameraCaptureSession) {
Log.e(TAG, "Session config failed")
onErrorCallback?.invoke("Camera session failed")
}
},
camHandler
)
} catch (e: Exception) {
Log.e(TAG, "Create session error", e)
}
}
private fun updateSession() {
val builder = requestBuilder ?: return
val session = captureSession ?: return
try {
if (zoomRatio > 1f) {
builder.set(CaptureRequest.CONTROL_ZOOM_RATIO, zoomRatio)
}
builder.set(
CaptureRequest.FLASH_MODE,
if (flashMode) CameraMetadata.FLASH_MODE_TORCH
else CameraMetadata.FLASH_MODE_OFF
)
builder.set(CaptureRequest.CONTROL_AWB_MODE, wbMode)
builder.set(CaptureRequest.CONTROL_AF_MODE, focusMode)
if (focusMode == CameraMetadata.CONTROL_AF_MODE_OFF) {
builder.set(CaptureRequest.LENS_FOCUS_DISTANCE, focusDistance)
}
builder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, exposureCompensation)
session.setRepeatingRequest(builder.build(), null, camHandler)
} catch (e: Exception) {
Log.e(TAG, "Update session error", e)
}
}
private fun streamLoop(ip: InetAddress, port: Int) {
val bufferInfo = MediaCodec.BufferInfo()
val maxChunkSize = 1400
Log.d(TAG, "Stream loop started")
try {
while (isStreaming) {
val codec = mediaCodec ?: break
val index = codec.dequeueOutputBuffer(bufferInfo, 10000)
when (index) {
MediaCodec.INFO_OUTPUT_FORMAT_CHANGED -> {
val format = codec.outputFormat
Log.d(TAG, "Output format: $format")
}
in 0..Int.MAX_VALUE -> {
try {
if (bufferInfo.size <= 0) {
codec.releaseOutputBuffer(index, false)
continue
}
val buffer = codec.getOutputBuffer(index) ?: continue
buffer.position(bufferInfo.offset)
buffer.limit(bufferInfo.offset + bufferInfo.size)
val frameData = ByteArray(bufferInfo.size)
buffer.get(frameData)
var offset = 0
while (offset < frameData.size && isStreaming) {
val chunkSize = min(maxChunkSize, frameData.size - offset)
socket?.send(
DatagramPacket(frameData, offset, chunkSize, ip, port)
)
offset += chunkSize
}
lastPacketTime = System.currentTimeMillis()
codec.releaseOutputBuffer(index, false)
} catch (e: Exception) {
Log.e(TAG, "Buffer processing error", e)
}
}
}
}
} catch (e: Exception) {
Log.e(TAG, "Stream loop error", e)
}
Log.d(TAG, "Stream loop ended")
}
fun stop() {
camHandler.post { stopInternal() }
}
fun cleanup() {
stop()
cameraThread.quitSafely()
}
private fun stopInternal() {
isStreaming = false
try {
captureSession?.stopRepeating()
captureSession?.close()
captureSession = null
} catch (e: Exception) {
Log.e(TAG, "Session stop error", e)
}
try {
cameraDevice?.close()
cameraDevice = null
} catch (e: Exception) {
Log.e(TAG, "Camera close error", e)
}
try {
mediaCodec?.stop()
mediaCodec?.release()
mediaCodec = null
} catch (e: Exception) {
Log.e(TAG, "Codec stop error", e)
}
try {
socket?.close()
socket = null
} catch (e: Exception) {
Log.e(TAG, "Socket close error", e)
}
requestBuilder = null
}
private fun getCameraId(front: Boolean): String {
val target = if (front)
CameraCharacteristics.LENS_FACING_FRONT
else
CameraCharacteristics.LENS_FACING_BACK
return manager.cameraIdList.firstOrNull {
manager.getCameraCharacteristics(it)
.get(CameraCharacteristics.LENS_FACING) == target
} ?: manager.cameraIdList[0]
}
}