Chromium Code Reviews| Index: media/base/android/java/src/org/chromium/media/WebAudioMediaCodecBridge.java |
| diff --git a/media/base/android/java/src/org/chromium/media/WebAudioMediaCodecBridge.java b/media/base/android/java/src/org/chromium/media/WebAudioMediaCodecBridge.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e3e2228121801826f6cab098b13febcad64c140f |
| --- /dev/null |
| +++ b/media/base/android/java/src/org/chromium/media/WebAudioMediaCodecBridge.java |
| @@ -0,0 +1,171 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.media; |
| + |
| +import android.content.Context; |
| +import android.media.AudioFormat; |
| +import android.media.MediaCodec; |
| +import android.media.MediaCodec.BufferInfo; |
| +import android.media.MediaExtractor; |
| +import android.media.MediaFormat; |
| +import android.util.Log; |
| + |
| +import java.nio.ByteBuffer; |
| +import android.os.ParcelFileDescriptor; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| + |
| +@JNINamespace("media") |
| +class WebAudioMediaCodecBridge { |
| + static final String LOG_TAG = "WebAudioMediaCodec"; |
| + @CalledByNative |
| + private static boolean decodeAudioFile(Context ctx, int nativeMediaCodecBridge, int inputFD) { |
| + MediaCodec codec; |
| + ByteBuffer[] codecInputBuffers; |
| + ByteBuffer[] codecOutputBuffers; |
|
bulach
2013/04/02 11:05:11
nit: probably best to declare this way below close
|
| + |
| + // TODO(rtoy): What is the correct timeout value for reading |
| + // from a file in memory? |
| + long TIMEOUT_US = 500; |
|
bulach
2013/04/02 11:05:11
nit: final
palmer
2013/04/02 18:01:08
NIT: "U" only sort of looks like the Greek letter
palmer
2013/04/02 18:01:08
Also static, and declared in class scope instead o
|
| + MediaExtractor extractor; |
|
bulach
2013/04/02 11:05:11
nit: declare and create in one line: MediaExtracto
|
| + extractor = new MediaExtractor(); |
| + |
| + ParcelFileDescriptor encodedFD; |
| + try { |
| + encodedFD = ParcelFileDescriptor.adoptFd(inputFD); |
| + extractor.setDataSource(encodedFD.getFileDescriptor()); |
| + } catch (Exception e) { |
| + e.printStackTrace(); |
| + return false; |
| + } |
| + |
| + Log.d(LOG_TAG, String.format("TRACKS #: %d", extractor.getTrackCount())); |
|
bulach
2013/04/02 11:05:11
nit: I still think there's a lot of logging going
palmer
2013/04/02 18:01:08
Agreed.
|
| + |
| + if (extractor.getTrackCount() <= 0) { |
| + encodedFD.detachFd(); |
| + return false; |
| + } |
| + |
| + MediaFormat format = extractor.getTrackFormat(0); |
| + String mime = format.getString(MediaFormat.KEY_MIME); |
|
palmer
2013/04/02 18:01:08
At the least, this is a lot of debug logging code
|
| + Log.d(LOG_TAG, String.format("MIME TYPE: %s", mime)); |
| + |
| + int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE); |
| + Log.d(LOG_TAG, String.format("Sample rate: %d", sampleRate)); |
| + |
| + int channelCount = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); |
| + Log.d(LOG_TAG, String.format("Channel count: %d", channelCount)); |
| + |
| + long duration_us = 0; |
| + if (format.containsKey(MediaFormat.KEY_DURATION)) { |
| + try { |
| + duration_us = format.getLong(MediaFormat.KEY_DURATION); |
| + Log.d(LOG_TAG, "Duration : " + duration_us + " us"); |
| + } catch (Exception e) { |
| + Log.d(LOG_TAG, "Cannot get duration"); |
| + } |
| + } |
| + |
| + int channelConfig = (channelCount == 2) ? |
| + AudioFormat.CHANNEL_OUT_STEREO : AudioFormat.CHANNEL_OUT_MONO; |
|
palmer
2013/04/02 18:01:08
So we treat 5.1 (hypothetically) as mono?
Raymond Toy (Google)
2013/04/02 21:52:21
This isn't used anywhere, so I'm deleting this.
|
| + |
| + // For audio/vorbis files, MediaFormat returns a really huge |
| + // (multi-year) duration value even for short files. Tell |
| + // nativeInitializeDestination that this is a vorbis file so |
| + // it can handle it properly. |
| + // |
| + // TODO(rtoy): File a bug and find out why a huge value is |
|
palmer
2013/04/02 18:01:08
Just file the bug now instead of putting a TODO in
|
| + // returned for the duration. |
| + nativeInitializeDestination(nativeMediaCodecBridge, |
| + channelCount, |
| + sampleRate, |
| + duration_us, |
| + mime.equals("audio/vorbis")); |
| + |
| + // Create decoder |
| + codec = MediaCodec.createDecoderByType(mime); |
|
bulach
2013/04/02 11:05:11
nit: as above: MediaCodec codec = MediaCodec.creat
|
| + codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */); |
| + codec.start(); |
| + |
| + codecInputBuffers = codec.getInputBuffers(); |
|
bulach
2013/04/02 11:05:11
ditto, ByteBuffer[] codecInputBuffers = codec.getI
|
| + codecOutputBuffers = codec.getOutputBuffers(); |
| + |
| + // A track must be selected and will be used to read samples. |
| + extractor.selectTrack(0); |
| + |
| + boolean sawInputEOS = false; |
| + boolean sawOutputEOS = false; |
| + |
| + // Keep processing until the output is done. |
| + while (!sawOutputEOS) { |
| + int sampleSize = 0; |
| + if (!sawInputEOS) { |
| + // Input side |
| + int inputBufIndex = codec.dequeueInputBuffer(TIMEOUT_US); |
| + |
| + if (inputBufIndex >= 0) { |
| + ByteBuffer dstBuf = codecInputBuffers[inputBufIndex]; |
| + sampleSize = extractor.readSampleData(dstBuf, 0); |
| + long presentationTimeUs = 0; |
|
palmer
2013/04/02 18:01:08
Again, maybe presentationTimeMicroS ?
|
| + |
| + if (sampleSize < 0) { |
| + sawInputEOS = true; |
| + sampleSize = 0; |
| + } else { |
| + presentationTimeUs = extractor.getSampleTime(); |
| + } |
| + |
| + codec.queueInputBuffer(inputBufIndex, |
| + 0, /* offset */ |
| + sampleSize, |
| + presentationTimeUs, |
| + sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0); |
| + |
| + if (!sawInputEOS) { |
| + extractor.advance(); |
| + } |
| + } |
| + } |
| + // Output side |
|
palmer
2013/04/02 18:01:08
NIT: Swap lines 132 and 133, so that the blank lin
|
| + |
| + MediaCodec.BufferInfo info = new BufferInfo(); |
| + final int res = codec.dequeueOutputBuffer(info, TIMEOUT_US); |
| + |
| + if (res >= 0) { |
| + int outputBufIndex = res; |
|
palmer
2013/04/02 18:01:08
NIT: Maybe you can get rid of outputBufIndex (and
|
| + ByteBuffer buf = codecOutputBuffers[outputBufIndex]; |
| + |
| + if (info.size > 0) { |
| + nativeOnChunkDecoded(nativeMediaCodecBridge, buf, info.size); |
| + } |
| + |
| + buf.clear(); |
| + codec.releaseOutputBuffer(outputBufIndex, false /* render */); |
| + |
| + if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { |
| + sawOutputEOS = true; |
| + } |
| + } |
| + } |
| + |
| + encodedFD.detachFd(); |
| + |
| + codec.stop(); |
| + codec.release(); |
| + codec = null; |
| + |
| + return true; |
| + } |
| + private static native void nativeOnChunkDecoded( |
|
palmer
2013/04/02 18:01:08
NIT: Blank line above this new method to separate
|
| + int nativeWebAudioMediaCodecBridge, ByteBuffer buf, int size); |
| + |
| + private static native void nativeInitializeDestination( |
| + int nativeWebAudioMediaCodecBridge, |
| + int channelCount, |
| + int sampleRate, |
| + long duration_us, |
| + boolean is_vorbis); |
| +} |