From 03f61bbb2e7293592ad4a304b0f0b8e6ee8e6aac Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Sun, 17 Jan 2016 21:49:58 +0000 Subject: Revert "Revert "AudioTrack: Add getUnderrunCount()"" This reverts commit 0ac70888d3f08b694e3e31939f2cfb90ce5e0f58. The first revert was not needed. It was made to fix a broken build. But the break was from a different CL. SO I am reverting the revert. Change-Id: Iad30209a38f9a0af18d684e44f033a49f32af778 --- core/jni/android_media_AudioTrack.cpp | 12 ++++++++++++ media/java/android/media/AudioTrack.java | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/jni/android_media_AudioTrack.cpp b/core/jni/android_media_AudioTrack.cpp index 42f3fb0d4465..bc83b7fa4178 100644 --- a/core/jni/android_media_AudioTrack.cpp +++ b/core/jni/android_media_AudioTrack.cpp @@ -857,6 +857,17 @@ static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) { return (jint)lpTrack->latency(); } +// ---------------------------------------------------------------------------- +static jint android_media_AudioTrack_get_underrun_count(JNIEnv *env, jobject thiz) { + sp lpTrack = getAudioTrack(env, thiz); + + if (lpTrack == NULL) { + jniThrowException(env, "java/lang/IllegalStateException", + "Unable to retrieve AudioTrack pointer for getUnderrunCount()"); + return (jint)AUDIO_JAVA_ERROR; + } + return (jint)lpTrack->getUnderrunCount(); +} // ---------------------------------------------------------------------------- static jint android_media_AudioTrack_get_timestamp(JNIEnv *env, jobject thiz, jlongArray jTimestamp) { @@ -1094,6 +1105,7 @@ static const JNINativeMethod gMethods[] = { {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position}, {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position}, {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency}, + {"native_get_underrun_count", "()I", (void *)android_media_AudioTrack_get_underrun_count}, {"native_get_timestamp", "([J)I", (void *)android_media_AudioTrack_get_timestamp}, {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop}, {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload}, diff --git a/media/java/android/media/AudioTrack.java b/media/java/android/media/AudioTrack.java index c110ce8de863..775d05b646b5 100644 --- a/media/java/android/media/AudioTrack.java +++ b/media/java/android/media/AudioTrack.java @@ -42,7 +42,6 @@ import android.util.Log; import com.android.internal.app.IAppOpsService; - /** * The AudioTrack class manages and plays a single audio resource for Java applications. * It allows streaming of PCM audio buffers to the audio sink for playback. This is @@ -1131,6 +1130,25 @@ public class AudioTrack implements AudioRouting return native_get_latency(); } + /** + * Returns the number of underrun occurrences in the application-level write buffer + * since the AudioTrack was created. + * An underrun occurs if the application does not write audio + * data quickly enough, causing the buffer to underflow + * and a potential audio glitch or pop. + * Underruns are less likely when buffer sizes are large. + *

Though the "int" type is signed 32-bits, the value should be reinterpreted + * as if it is unsigned 32-bits. + * That is, the next position after 0x7FFFFFFF is (int) 0x80000000. + * This is a continuously advancing counter. It can wrap around to zero + * if there are too many underruns. If there were, for example, 68 underruns per + * second then the counter would wrap in 2 years. + * @hide + */ + public int getUnderrunCount() { + return native_get_underrun_count(); + } + /** * Returns the output sample rate in Hz for the specified stream type. */ @@ -2722,6 +2740,8 @@ public class AudioTrack implements AudioRouting private native final int native_get_latency(); + private native final int native_get_underrun_count(); + // longArray must be a non-null array of length >= 2 // [0] is assigned the frame position // [1] is assigned the time in CLOCK_MONOTONIC nanoseconds -- cgit v1.2.3-59-g8ed1b