admin管理员组

文章数量:1122832

I want to build an android application, share screen between android device and android tv, I need local wifi network only, I already created two clients, one for phone another for TV, the TV client show a QR code with ip and port info, phone client scan the QR code first then capture screen, send data to TV client by UDP, but the effect is very bad, the view is not clear, and it is not smooth
I use follow class

  1. MediaCodec and MediaProjection to capture screen
  2. DatagramSocket to send ByteArray data

I have not much knowledge about the UDP
I want to ask some questions

  1. How can I make the effect smooth and clear
  2. If I have other choices to achieve the function I want, for example, are there some open source library I can use or any other solutions

Thank you.

I already wrote two clients
send data

fun sendCaptureData(data: ByteArray) {
    if (tvInfo == null) {
        Logger.e("TV info is null")
        return
    }
    job.launch {
        try {
            val chunks = splitCaptureData(data)
            for (chunk in chunks) {
                val datagramPacket = datagramPacketPool.borrow()
                datagramPacket.data = chunk
                datagramPacket.length = chunk.size
                datagramPacket.address = InetAddress.getByName(tvInfo!!.ip)
                datagramPacket.port = tvInfo!!.port
                datagramSocket.send(datagramPacket)
                datagramPacketPool.release(datagramPacket)
            }
        } catch (e: Exception) {
            Logger.e(e, "Error sending data")
        }
    }
}

receive data

fun startReceiving(dataDecoder: DataDecoder) {
    isReceiving = true
    // 启动接收协程
    job.launch {
        while (isReceiving) {
            try {
                datagramSocket.receive(packet)
                val receivedData = ByteArray(packet.length)
                System.arraycopy(packet.data, packet.offset, receivedData, 0, packet.length)
                dataDecoder.decode(receivedData)
            } catch (e: Exception) {
                Logger.e(e, "Error receiving data")
            }
        }
    }
}

decode data

fun decode(receivedData: ByteArray) {
    if (mediaCodec == null) return
    val index = mediaCodec!!.dequeueInputBuffer(10000)
    if (index >= 0) {
        val inputBuffer = mediaCodec!!.getInputBuffer(index)
        inputBuffer?.clear()
        inputBuffer?.put(receivedData, 0, receivedData.size)
        mediaCodec!!.queueInputBuffer(index, 0, receivedData.size, 0, 0)
        val bufferInfo = MediaCodec.BufferInfo()
        var outputBufferIndex = mediaCodec!!.dequeueOutputBuffer(bufferInfo, 0)
        while (outputBufferIndex >= 0) {
            mediaCodec!!.releaseOutputBuffer(outputBufferIndex, true)
            outputBufferIndex = mediaCodec!!.dequeueOutputBuffer(bufferInfo, 0)
        }
        Logger.i("MediaCodec decoded")
    }
}

本文标签: screenshotAndroid screen mirroring application local wifi onlyStack Overflow