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
- MediaCodec and MediaProjection to capture screen
- DatagramSocket to send ByteArray data
I have not much knowledge about the UDP
I want to ask some questions
- How can I make the effect smooth and clear
- 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
版权声明:本文标题:screenshot - Android screen mirroring application local wifi only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305884a1932750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论