OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.media; | 5 package org.chromium.media; |
6 | 6 |
7 import android.media.AudioFormat; | 7 import android.media.AudioFormat; |
8 import android.media.AudioManager; | 8 import android.media.AudioManager; |
9 import android.media.AudioTrack; | 9 import android.media.AudioTrack; |
10 import android.media.MediaCodec; | 10 import android.media.MediaCodec; |
11 import android.media.MediaCrypto; | 11 import android.media.MediaCrypto; |
12 import android.media.MediaFormat; | 12 import android.media.MediaFormat; |
13 import android.view.Surface; | 13 import android.view.Surface; |
14 import android.util.Log; | 14 import android.util.Log; |
15 | 15 |
16 import java.nio.ByteBuffer; | 16 import java.nio.ByteBuffer; |
17 | 17 |
18 import org.chromium.base.CalledByNative; | 18 import org.chromium.base.CalledByNative; |
19 import org.chromium.base.JNINamespace; | 19 import org.chromium.base.JNINamespace; |
20 | 20 |
21 /** | 21 /** |
22 * A wrapper of the MediaCodec class to facilitate exception capturing and | 22 * A wrapper of the MediaCodec class to facilitate exception capturing and |
23 * audio rendering. | 23 * audio rendering. |
24 */ | 24 */ |
25 @JNINamespace("media") | 25 @JNINamespace("media") |
26 class MediaCodecBridge { | 26 class MediaCodecBridge { |
27 | 27 |
28 private static final String TAG = "MediaCodecBridge"; | 28 private static final String TAG = "MediaCodecBridge"; |
29 | 29 |
| 30 // Error code for MediaCodecBridge. Keep this value in sync with |
| 31 // INFO_MEDIA_CODEC_ERROR in media_codec_bridge.h. |
| 32 private static final int MEDIA_CODEC_ERROR = -1000; |
| 33 |
30 private ByteBuffer[] mInputBuffers; | 34 private ByteBuffer[] mInputBuffers; |
31 private ByteBuffer[] mOutputBuffers; | 35 private ByteBuffer[] mOutputBuffers; |
32 | 36 |
33 private MediaCodec mMediaCodec; | 37 private MediaCodec mMediaCodec; |
34 | 38 |
35 private AudioTrack mAudioTrack; | 39 private AudioTrack mAudioTrack; |
36 | 40 |
37 private static class DequeueOutputResult { | 41 private static class DequeueOutputResult { |
38 private final int mIndex; | 42 private final int mIndex; |
39 private final int mFlags; | 43 private final int mFlags; |
40 private final int mOffset; | 44 private final int mOffset; |
41 private final long mPresentationTimeMicroseconds; | 45 private final long mPresentationTimeMicroseconds; |
42 private final int mNumBytes; | 46 private final int mNumBytes; |
43 | 47 |
44 private DequeueOutputResult( | 48 private DequeueOutputResult(int index, int flags, int offset, |
45 int index, int flags, int offset, long presentationTimeMicroseconds,
int numBytes) { | 49 long presentationTimeMicroseconds, int numBytes) { |
46 mIndex = index; | 50 mIndex = index; |
47 mFlags = flags; | 51 mFlags = flags; |
48 mOffset = offset; | 52 mOffset = offset; |
49 mPresentationTimeMicroseconds = presentationTimeMicroseconds; | 53 mPresentationTimeMicroseconds = presentationTimeMicroseconds; |
50 mNumBytes = numBytes; | 54 mNumBytes = numBytes; |
51 } | 55 } |
52 | 56 |
53 @CalledByNative("DequeueOutputResult") | 57 @CalledByNative("DequeueOutputResult") |
54 private int index() { return mIndex; } | 58 private int index() { return mIndex; } |
55 | 59 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 146 } |
143 | 147 |
144 @CalledByNative | 148 @CalledByNative |
145 private void getOutputBuffers() { | 149 private void getOutputBuffers() { |
146 mOutputBuffers = mMediaCodec.getOutputBuffers(); | 150 mOutputBuffers = mMediaCodec.getOutputBuffers(); |
147 } | 151 } |
148 | 152 |
149 @CalledByNative | 153 @CalledByNative |
150 private DequeueOutputResult dequeueOutputBuffer(long timeoutUs) { | 154 private DequeueOutputResult dequeueOutputBuffer(long timeoutUs) { |
151 MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); | 155 MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); |
152 int index = mMediaCodec.dequeueOutputBuffer(info, timeoutUs); | 156 int index = MEDIA_CODEC_ERROR; |
| 157 try { |
| 158 index = mMediaCodec.dequeueOutputBuffer(info, timeoutUs); |
| 159 } catch(IllegalStateException e) { |
| 160 Log.e(TAG, "Cannot dequeue output buffer " + e.toString()); |
| 161 } |
153 return new DequeueOutputResult( | 162 return new DequeueOutputResult( |
154 index, info.flags, info.offset, info.presentationTimeUs, info.si
ze); | 163 index, info.flags, info.offset, info.presentationTimeUs, info.si
ze); |
155 } | 164 } |
156 | 165 |
157 @CalledByNative | 166 @CalledByNative |
158 private void configureVideo(MediaFormat format, Surface surface, MediaCrypto
crypto, | 167 private void configureVideo(MediaFormat format, Surface surface, MediaCrypto
crypto, |
159 int flags) { | 168 int flags) { |
160 mMediaCodec.configure(format, surface, crypto, flags); | 169 mMediaCodec.configure(format, surface, crypto, flags); |
161 } | 170 } |
162 | 171 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 mAudioTrack.play(); | 215 mAudioTrack.play(); |
207 } | 216 } |
208 int size = mAudioTrack.write(buf, 0, buf.length); | 217 int size = mAudioTrack.write(buf, 0, buf.length); |
209 if (buf.length != size) { | 218 if (buf.length != size) { |
210 Log.i(TAG, "Failed to send all data to audio output, expected si
ze: " + | 219 Log.i(TAG, "Failed to send all data to audio output, expected si
ze: " + |
211 buf.length + ", actual size: " + size); | 220 buf.length + ", actual size: " + size); |
212 } | 221 } |
213 } | 222 } |
214 } | 223 } |
215 } | 224 } |
OLD | NEW |