summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorAndroid (Google) Code Review <android-gerrit@google.com>2009-05-12 11:53:05 -0700
committerAndroid (Google) Code Review <android-gerrit@google.com>2009-05-12 11:53:05 -0700
commite51f3a012b146d57a01b9f7cc7c043b9da3c600d (patch)
tree6f1b434a7260948fbe02037ed0c01c109fae9d50 /core
parentda33d0d139531ff184a1a339b734de00f1cc0743 (diff)
parent21dc037e7e7c51e33b5808320f47339ee3a2b103 (diff)
downloadframeworks_base-e51f3a012b146d57a01b9f7cc7c043b9da3c600d.zip
frameworks_base-e51f3a012b146d57a01b9f7cc7c043b9da3c600d.tar.gz
frameworks_base-e51f3a012b146d57a01b9f7cc7c043b9da3c600d.tar.bz2
Merge change 1297 into donut
* changes: Check the size of the data to write for STATIC AudioTrack objects. Extracted actual writing from write jni function so it can be reused in an upcoming change.
Diffstat (limited to 'core')
-rw-r--r--core/jni/android_media_AudioTrack.cpp67
1 files changed, 42 insertions, 25 deletions
diff --git a/core/jni/android_media_AudioTrack.cpp b/core/jni/android_media_AudioTrack.cpp
index b8d6586..42ada54 100644
--- a/core/jni/android_media_AudioTrack.cpp
+++ b/core/jni/android_media_AudioTrack.cpp
@@ -335,7 +335,7 @@ android_media_AudioTrack_start(JNIEnv *env, jobject thiz)
jniThrowException(env, "java/lang/IllegalStateException",
"Unable to retrieve AudioTrack pointer for start()");
}
-
+
lpTrack->start();
}
@@ -433,6 +433,45 @@ static void android_media_AudioTrack_native_release(JNIEnv *env, jobject thiz)
// ----------------------------------------------------------------------------
+jint writeToTrack(AudioTrack* pTrack, jint audioFormat, jbyte* data,
+ jint offsetInBytes, jint sizeInBytes) {
+ // give the data to the native AudioTrack object (the data starts at the offset)
+ ssize_t written = 0;
+ // regular write() or copy the data to the AudioTrack's shared memory?
+ if (pTrack->sharedBuffer() == 0) {
+ written = pTrack->write(data + offsetInBytes, sizeInBytes);
+ } else {
+ if (audioFormat == javaAudioTrackFields.PCM16) {
+ // writing to shared memory, check for capacity
+ if ((size_t)sizeInBytes > pTrack->sharedBuffer()->size()) {
+ sizeInBytes = pTrack->sharedBuffer()->size();
+ }
+ memcpy(pTrack->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
+ written = sizeInBytes;
+ } else if (audioFormat == javaAudioTrackFields.PCM8) {
+ // data contains 8bit data we need to expand to 16bit before copying
+ // to the shared memory
+ // writing to shared memory, check for capacity,
+ // note that input data will occupy 2X the input space due to 8 to 16bit conversion
+ if (((size_t)sizeInBytes)*2 > pTrack->sharedBuffer()->size()) {
+ sizeInBytes = pTrack->sharedBuffer()->size() / 2;
+ }
+ int count = sizeInBytes;
+ int16_t *dst = (int16_t *)pTrack->sharedBuffer()->pointer();
+ const int8_t *src = (const int8_t *)(data + offsetInBytes);
+ while(count--) {
+ *dst++ = (int16_t)(*src++^0x80) << 8;
+ }
+ // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
+ // the 8bit mixer restriction from the user of this function
+ written = sizeInBytes;
+ }
+ }
+ return written;
+
+}
+
+// ----------------------------------------------------------------------------
static jint android_media_AudioTrack_native_write(JNIEnv *env, jobject thiz,
jbyteArray javaAudioData,
jint offsetInBytes, jint sizeInBytes,
@@ -461,35 +500,13 @@ static jint android_media_AudioTrack_native_write(JNIEnv *env, jobject thiz,
return 0;
}
- // give the data to the native AudioTrack object (the data starts at the offset)
- ssize_t written = 0;
- // regular write() or copy the data to the AudioTrack's shared memory?
- if (lpTrack->sharedBuffer() == 0) {
- written = lpTrack->write(cAudioData + offsetInBytes, sizeInBytes);
- } else {
- if (javaAudioFormat == javaAudioTrackFields.PCM16) {
- memcpy(lpTrack->sharedBuffer()->pointer(), cAudioData + offsetInBytes, sizeInBytes);
- written = sizeInBytes;
- } else if (javaAudioFormat == javaAudioTrackFields.PCM8) {
- // cAudioData contains 8bit data we need to expand to 16bit before copying
- // to the shared memory
- int count = sizeInBytes;
- int16_t *dst = (int16_t *)lpTrack->sharedBuffer()->pointer();
- const int8_t *src = (const int8_t *)(cAudioData + offsetInBytes);
- while(count--) {
- *dst++ = (int16_t)(*src++^0x80) << 8;
- }
- // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
- // the 8bit mixer restriction from the user of this function
- written = sizeInBytes;
- }
- }
+ jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
env->ReleasePrimitiveArrayCritical(javaAudioData, cAudioData, 0);
//LOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d",
// (int)written, (int)(sizeInBytes), (int)offsetInBytes);
- return (int)written;
+ return written;
}