diff options
author | Eric Laurent <elaurent@google.com> | 2011-03-07 14:52:59 -0800 |
---|---|---|
committer | Eric Laurent <elaurent@google.com> | 2011-03-08 16:33:15 -0800 |
commit | 421ddc014b31478ba0ef115e098d8780a35af331 (patch) | |
tree | 43575a2309d53bb4712db81ff5898721c7578b8c /core/jni/android_media_AudioRecord.cpp | |
parent | 949d0c8c384437d92fc1432b750da6da59df1fa7 (diff) | |
download | frameworks_base-421ddc014b31478ba0ef115e098d8780a35af331.zip frameworks_base-421ddc014b31478ba0ef115e098d8780a35af331.tar.gz frameworks_base-421ddc014b31478ba0ef115e098d8780a35af331.tar.bz2 |
Fix issue 3439872: video chat and bluetooth SCO
This change fixes the stability problems experienced when using
a bluetooth headset supporting both A2DP and SCO. Problems occur
when starting the video chat at which time the A2DP output is being
stopped to start SCO. At that time, active AudioTracks are invalidated
by AudioFlinger so that a new AudioTrack binder interface can be
recreated by the client process on the new mixer thread with correct parameters.
The problem was that the process to restore the binder interface was not
protected against concurrent requests which caused 2 binder interfaces
to be created sometimes. This could lead to permanent client deadlock
if one of the client threads was waiting for a condition of the first
created binder interface while the second one was created (as the AudioFlinger
would only signal conditions on the last one created).
This concurrent request situation is more likely to happen when a client
uses the JAVA AudioTrack as the JNI implementation uses simultaneously the
native AudioTrack callback and write push mechanisms. By doing so, the code
that checks if the binder interface should be restored (in obtainBuffer()) is
much more likely to be called concurrently from two different threads.
The fix consists in protecting the critical binder interface restore phase
with a flag in the AudioTrack control block. The first thread acting upon the binder
interface restore request will raise the flag and the second thread will just wait for
a condition to be signaled when the restore process is complete.
Also protected all accesses to the AudioTrack control block by a mutex to prevent
access while the track is being destroyed and restored. If a mutex cannot be held
(e.g because we call a callback function), acquire a strong reference on the IAudioTrack
to prevent its destruction while the cblk is being accessed.
Modified AudioTrack JNI to use GetByteArrayElements() instead of
GetPrimitiveArrayCritical() when writing audio buffers. Entering a critical section would
cause the JNI to abort if a mediaserver crash occurs during a write due to the AudioSystem
callback being called during the critical section when media server process restarts.
Anyway with current JNI implementation, either versions do not copy data most of the times
and the criticial version does not guaranty no data copy.
The same modifications have been made to AudioRecord.
Change-Id: Idc5aa711a04c3eee180cdd03f44fe17f3c4dcb52
Diffstat (limited to 'core/jni/android_media_AudioRecord.cpp')
-rw-r--r-- | core/jni/android_media_AudioRecord.cpp | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/core/jni/android_media_AudioRecord.cpp b/core/jni/android_media_AudioRecord.cpp index f78f83c..b6619ab 100644 --- a/core/jni/android_media_AudioRecord.cpp +++ b/core/jni/android_media_AudioRecord.cpp @@ -315,7 +315,11 @@ static jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz } // get the pointer to where we'll record the audio - recordBuff = (jbyte *)env->GetPrimitiveArrayCritical(javaAudioData, NULL); + // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such + // a way that it becomes much more efficient. When doing so, we will have to prevent the + // AudioSystem callback to be called while in critical section (in case of media server + // process crash for instance) + recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); if (recordBuff == NULL) { LOGE("Error retrieving destination for recorded audio data, can't record"); @@ -327,7 +331,7 @@ static jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, sizeInBytes > (jint)recorderBuffSize ? (jint)recorderBuffSize : sizeInBytes ); - env->ReleasePrimitiveArrayCritical(javaAudioData, recordBuff, 0); + env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); return (jint) readSize; } |