Stage WIP audio support

This commit is contained in:
Cameron Gutman
2013-10-30 22:23:11 -04:00
parent 9e09ca2b7a
commit e067669044
3 changed files with 199 additions and 83 deletions
+74 -2
View File
@@ -30,8 +30,8 @@ public class NvVideoStream {
public static final int RTCP_PORT = 47999;
public static final int FIRST_FRAME_PORT = 47996;
private ByteBuffer[] videoDecoderInputBuffers = null;
private MediaCodec videoDecoder;
private ByteBuffer[] videoDecoderInputBuffers, audioDecoderInputBuffers;
private MediaCodec videoDecoder, audioDecoder;
private LinkedBlockingQueue<AvRtpPacket> packets = new LinkedBlockingQueue<AvRtpPacket>();
@@ -94,10 +94,19 @@ public class NvVideoStream {
videoDecoder = MediaCodec.createDecoderByType("video/avc");
MediaFormat videoFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720);
audioDecoder = MediaCodec.createDecoderByType("audio/mp4a-latm");
MediaFormat audioFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", 48000, 2);
videoDecoder.configure(videoFormat, surface, null, 0);
audioDecoder.configure(audioFormat, null, null, 0);
videoDecoder.setVideoScalingMode(MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
videoDecoder.start();
audioDecoder.start();
videoDecoderInputBuffers = videoDecoder.getInputBuffers();
audioDecoderInputBuffers = audioDecoder.getInputBuffers();
}
public void startVideoStream(final String host, final Surface surface)
@@ -130,6 +139,9 @@ public class NvVideoStream {
// Start decoding the data we're receiving
startDecoderThread();
// Start playing back audio data
startAudioPlaybackThread();
// Read the first frame to start the UDP video stream
try {
readFirstFrame(host);
@@ -188,6 +200,32 @@ public class NvVideoStream {
}
}
break;
case AvDecodeUnit.TYPE_AAC:
{
int inputIndex = audioDecoder.dequeueInputBuffer(0);
if (inputIndex == -4)
{
ByteBuffer buf = audioDecoderInputBuffers[inputIndex];
// Clear old input data
buf.clear();
// Copy data from our buffer list into the input buffer
for (AvBufferDescriptor desc : du.getBufferList())
{
buf.put(desc.data, desc.offset, desc.length);
// Release the buffer back to the buffer pool
pool.free(desc.data);
}
audioDecoder.queueInputBuffer(inputIndex,
0, du.getDataLength(),
0, du.getFlags());
}
}
break;
default:
{
@@ -284,6 +322,40 @@ public class NvVideoStream {
}).start();
}
private void startAudioPlaybackThread()
{
new Thread(new Runnable() {
@Override
public void run() {
for (;;)
{
BufferInfo info = new BufferInfo();
System.out.println("Waiting for audio");
int outIndex = audioDecoder.dequeueOutputBuffer(info, -1);
System.out.println("Got audio");
switch (outIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
System.out.println("Output buffers changed");
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
System.out.println("Output format changed");
System.out.println("New output Format: " + videoDecoder.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
System.out.println("Try again later");
break;
default:
break;
}
if (outIndex >= 0) {
audioDecoder.releaseOutputBuffer(outIndex, true);
}
}
}
}).start();
}
private void outputDisplayLoop()
{
for (;;)