mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-21 03:52:48 +00:00
Remove object allocations from audio decoding path
This commit is contained in:
parent
b63c6223b0
commit
c2401e7a75
@ -1,29 +1,37 @@
|
|||||||
package com.limelight.nvstream.av;
|
package com.limelight.nvstream.av;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
public class RtpPacket {
|
public class RtpPacket {
|
||||||
|
|
||||||
private byte packetType;
|
private byte packetType;
|
||||||
private short seqNum;
|
private short seqNum;
|
||||||
|
|
||||||
private ByteBufferDescriptor buffer;
|
private ByteBufferDescriptor buffer;
|
||||||
|
private ByteBuffer bb;
|
||||||
|
|
||||||
public static final int HEADER_SIZE = 12;
|
public static final int HEADER_SIZE = 12;
|
||||||
|
|
||||||
public RtpPacket(ByteBufferDescriptor buffer)
|
public RtpPacket(byte[] buffer)
|
||||||
{
|
{
|
||||||
this.buffer = new ByteBufferDescriptor(buffer);
|
this.buffer = new ByteBufferDescriptor(buffer, 0, buffer.length);
|
||||||
|
this.bb = ByteBuffer.wrap(buffer).order(ByteOrder.BIG_ENDIAN);
|
||||||
ByteBuffer bb = ByteBuffer.wrap(buffer.data, buffer.offset, buffer.length);
|
}
|
||||||
|
|
||||||
|
public void initializeWithLength(int length)
|
||||||
|
{
|
||||||
// Discard the first byte
|
// Discard the first byte
|
||||||
bb.position(bb.position()+1);
|
bb.position(1);
|
||||||
|
|
||||||
// Get the packet type
|
// Get the packet type
|
||||||
packetType = bb.get();
|
packetType = bb.get();
|
||||||
|
|
||||||
// Get the sequence number
|
// Get the sequence number
|
||||||
seqNum = bb.getShort();
|
seqNum = bb.getShort();
|
||||||
|
|
||||||
|
// Update descriptor length
|
||||||
|
buffer.length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte getPacketType()
|
public byte getPacketType()
|
||||||
@ -36,13 +44,13 @@ public class RtpPacket {
|
|||||||
return seqNum;
|
return seqNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getBackingBuffer()
|
public byte[] getBuffer()
|
||||||
{
|
{
|
||||||
return buffer.data;
|
return buffer.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteBufferDescriptor getNewPayloadDescriptor()
|
public void initializePayloadDescriptor(ByteBufferDescriptor bb)
|
||||||
{
|
{
|
||||||
return new ByteBufferDescriptor(buffer.data, buffer.offset+HEADER_SIZE, buffer.length-HEADER_SIZE);
|
bb.reinitialize(buffer.data, buffer.offset+HEADER_SIZE, buffer.length-HEADER_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,9 @@ public class AudioDepacketizer {
|
|||||||
private byte[][] pcmRing;
|
private byte[][] pcmRing;
|
||||||
private int ringIndex;
|
private int ringIndex;
|
||||||
|
|
||||||
|
// Cached objects
|
||||||
|
private ByteBufferDescriptor cachedDesc = new ByteBufferDescriptor(null, 0, 0);
|
||||||
|
|
||||||
// Sequencing state
|
// Sequencing state
|
||||||
private short lastSequenceNumber;
|
private short lastSequenceNumber;
|
||||||
|
|
||||||
@ -88,8 +91,8 @@ public class AudioDepacketizer {
|
|||||||
lastSequenceNumber = seq;
|
lastSequenceNumber = seq;
|
||||||
|
|
||||||
// This is all the depacketizing we need to do
|
// This is all the depacketizing we need to do
|
||||||
ByteBufferDescriptor rtpPayload = packet.getNewPayloadDescriptor();
|
packet.initializePayloadDescriptor(cachedDesc);
|
||||||
decodeData(rtpPayload.data, rtpPayload.offset, rtpPayload.length);
|
decodeData(cachedDesc.data, cachedDesc.offset, cachedDesc.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteBufferDescriptor getNextDecodedData() throws InterruptedException
|
public ByteBufferDescriptor getNextDecodedData() throws InterruptedException
|
||||||
|
@ -152,17 +152,18 @@ public class AudioStream {
|
|||||||
Thread t = new Thread() {
|
Thread t = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ByteBufferDescriptor desc = new ByteBufferDescriptor(new byte[MAX_PACKET_SIZE], 0, MAX_PACKET_SIZE);
|
byte[] buffer = new byte[MAX_PACKET_SIZE];
|
||||||
DatagramPacket packet = new DatagramPacket(desc.data, desc.length);
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||||
|
RtpPacket rtpPacket = new RtpPacket(buffer);
|
||||||
|
|
||||||
while (!isInterrupted())
|
while (!isInterrupted())
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
rtp.receive(packet);
|
rtp.receive(packet);
|
||||||
desc.length = packet.getLength();
|
|
||||||
|
|
||||||
// DecodeInputData() doesn't hold onto the buffer so we are free to reuse it
|
// DecodeInputData() doesn't hold onto the buffer so we are free to reuse it
|
||||||
depacketizer.decodeInputData(new RtpPacket(desc));
|
rtpPacket.initializeWithLength(packet.getLength());
|
||||||
|
depacketizer.decodeInputData(rtpPacket);
|
||||||
|
|
||||||
packet.setLength(MAX_PACKET_SIZE);
|
packet.setLength(MAX_PACKET_SIZE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user