summaryrefslogtreecommitdiffstats
path: root/media/java
diff options
context:
space:
mode:
authorJean-Michel Trivi <>2009-03-24 17:55:14 -0700
committerThe Android Open Source Project <initial-contribution@android.com>2009-03-24 17:55:14 -0700
commit470f71f4a4d6d3a504b62473bd435bf80493414f (patch)
tree826fde73356264b2f803b7ad41ebcb2e0f9ea303 /media/java
parentde7c071e7391faac28e3de8a4891f0f564c37e04 (diff)
downloadframeworks_base-470f71f4a4d6d3a504b62473bd435bf80493414f.zip
frameworks_base-470f71f4a4d6d3a504b62473bd435bf80493414f.tar.gz
frameworks_base-470f71f4a4d6d3a504b62473bd435bf80493414f.tar.bz2
Automated import from //branches/donutburger/...@140566,140566
Diffstat (limited to 'media/java')
-rw-r--r--media/java/android/media/AudioRecord.java161
1 files changed, 74 insertions, 87 deletions
diff --git a/media/java/android/media/AudioRecord.java b/media/java/android/media/AudioRecord.java
index 0ef7760..a49bd67 100644
--- a/media/java/android/media/AudioRecord.java
+++ b/media/java/android/media/AudioRecord.java
@@ -165,27 +165,24 @@ public class AudioRecord
*/
private Object mRecordingStateLock = new Object();
/**
- * The listener the AudioRecord notifies when a previously set marker is reached.
- * @see #setMarkerReachedListener(OnMarkerReachedListener)
+ * The listener the AudioRecord notifies when the record position reaches a marker
+ * or for periodic updates during the progression of the record head.
+ * @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)
+ * @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)
*/
- private OnMarkerReachedListener mMarkerListener = null;
+ private OnRecordPositionUpdateListener mPositionListener = null;
/**
- * Lock to protect marker listener updates against event notifications
+ * Lock to protect position listener updates against event notifications
*/
- private final Object mMarkerListenerLock = new Object();
+ private final Object mPositionListenerLock = new Object();
/**
- * The listener the AudioRecord notifies periodically during recording.
- * @see #setPeriodicNotificationListener(OnPeriodicNotificationListener)
+ * Handler for marker events coming from the native code
*/
- private OnPeriodicNotificationListener mPeriodicListener = null;
+ private NativeEventHandler mEventHandler = null;
/**
- * Lock to protect periodic listener updates against event notifications
+ * Looper associated with the thread that creates the AudioRecord instance
*/
- private final Object mPeriodicListenerLock = new Object();
- /**
- * Handler for events coming from the native code
- */
- private NativeEventHandler mNativeEventHandler = null;
+ private Looper mInitializationLooper = null;
/**
* Size of the native audio buffer.
*/
@@ -217,6 +214,11 @@ public class AudioRecord
throws IllegalArgumentException {
mState = STATE_UNINITIALIZED;
mRecordingState = RECORDSTATE_STOPPED;
+
+ // remember which looper is associated with the AudioRecord instanciation
+ if ((mInitializationLooper = Looper.myLooper()) == null) {
+ mInitializationLooper = Looper.getMainLooper();
+ }
audioParamCheck(audioSource, sampleRateInHz, channelConfig, audioFormat);
@@ -319,21 +321,6 @@ public class AudioRecord
}
- // Convenience method for the creation of the native event handler
- // It is called only when a non-null event listener is set.
- // precondition:
- // mNativeEventHandler is null
- private void createNativeEventHandler() {
- Looper looper;
- if ((looper = Looper.myLooper()) != null) {
- mNativeEventHandler = new NativeEventHandler(this, looper);
- } else if ((looper = Looper.getMainLooper()) != null) {
- mNativeEventHandler = new NativeEventHandler(this, looper);
- } else {
- mNativeEventHandler = null;
- }
- }
-
/**
* Releases the native AudioRecord resources.
@@ -433,7 +420,6 @@ public class AudioRecord
}
/**
- * {@hide}
* Returns the minimum buffer size required for the successful creation of an AudioRecord
* object.
* @param sampleRateInHz the sample rate expressed in Hertz.
@@ -602,36 +588,40 @@ public class AudioRecord
// Initialization / configuration
//--------------------
/**
- * Sets the listener the AudioRecord notifies when a previously set marker is reached.
+ * Sets the listener the AudioRecord notifies when a previously set marker is reached or
+ * for each periodic record head position update.
* @param listener
*/
- public void setMarkerReachedListener(OnMarkerReachedListener listener) {
- synchronized (mMarkerListenerLock) {
- mMarkerListener = listener;
- }
- if ((listener != null) && (mNativeEventHandler == null)) {
- createNativeEventHandler();
- }
+ public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener) {
+ setRecordPositionUpdateListener(listener, null);
}
-
- /**
- * Sets the listener the AudioRecord notifies periodically during recording.
- * @param listener
- */
- public void setPeriodicNotificationListener(OnPeriodicNotificationListener listener) {
- synchronized (mPeriodicListenerLock) {
- mPeriodicListener = listener;
- }
- if ((listener != null) && (mNativeEventHandler == null)) {
- createNativeEventHandler();
+
+ public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener,
+ Handler handler) {
+ synchronized (mPositionListenerLock) {
+
+ mPositionListener = listener;
+
+ if (listener != null) {
+ if (handler != null) {
+ mEventHandler = new NativeEventHandler(this, handler.getLooper());
+ } else {
+ // no given handler, use the looper the AudioRecord was created in
+ mEventHandler = new NativeEventHandler(this, mInitializationLooper);
+ }
+ } else {
+ mEventHandler = null;
+ }
}
+
}
/**
- * Sets the marker position at which the listener, if set with
- * {@link #setMarkerReachedListener(OnMarkerReachedListener)}, is called.
+ * Sets the marker position at which the listener is called, if set with
+ * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or
+ * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}.
* @param markerInFrames marker position expressed in frames
* @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
* {@link #ERROR_INVALID_OPERATION}
@@ -642,8 +632,9 @@ public class AudioRecord
/**
- * Sets the period at which the listener, if set with
- * {@link #setPositionNotificationPeriod(int)}, is called.
+ * Sets the period at which the listener is called, if set with
+ * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or
+ * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}.
* @param periodInFrames update period expressed in frames
* @return error code or success, see {@link #SUCCESS}, {@link #ERROR_INVALID_OPERATION}
*/
@@ -659,70 +650,65 @@ public class AudioRecord
* Interface definition for a callback to be invoked when an AudioRecord has
* reached a notification marker set by setNotificationMarkerPosition().
*/
- public interface OnMarkerReachedListener {
+ public interface OnRecordPositionUpdateListener {
/**
* Called on the listener to notify it that the previously set marker has been reached
* by the recording head.
*/
void onMarkerReached(AudioRecord recorder);
- }
-
-
- /**
- * Interface definition for a callback to be invoked for each periodic AudioRecord
- * update during recording. The update interval is set by setPositionNotificationPeriod().
- */
- public interface OnPeriodicNotificationListener {
+
/**
- * Called on the listener to periodically notify it that the recording head has reached
+ * Called on the listener to periodically notify it that the record head has reached
* a multiple of the notification period.
*/
void onPeriodicNotification(AudioRecord recorder);
}
+
+
//---------------------------------------------------------
// Inner classes
//--------------------
+
/**
- * Helper class to handle the forwarding of native events to the appropriate listeners
- */
- private class NativeEventHandler extends Handler
- {
- private AudioRecord mAudioRecord;
-
- public NativeEventHandler(AudioRecord ar, Looper looper) {
+ * Helper class to handle the forwarding of native events to the appropriate listener
+ * (potentially) handled in a different thread
+ */
+ private class NativeEventHandler extends Handler {
+
+ private final AudioRecord mAudioRecord;
+
+ NativeEventHandler(AudioRecord recorder, Looper looper) {
super(looper);
- mAudioRecord = ar;
+ mAudioRecord = recorder;
}
-
+
@Override
public void handleMessage(Message msg) {
- if (mAudioRecord == null) {
- return;
+ OnRecordPositionUpdateListener listener = null;
+ synchronized (mPositionListenerLock) {
+ listener = mAudioRecord.mPositionListener;
}
+
switch(msg.what) {
case NATIVE_EVENT_MARKER:
- synchronized (mMarkerListenerLock) {
- if (mAudioRecord.mMarkerListener != null) {
- mAudioRecord.mMarkerListener.onMarkerReached(mAudioRecord);
- }
+ if (listener != null) {
+ listener.onMarkerReached(mAudioRecord);
}
break;
case NATIVE_EVENT_NEW_POS:
- synchronized (mPeriodicListenerLock) {
- if (mAudioRecord.mPeriodicListener != null) {
- mAudioRecord.mPeriodicListener.onPeriodicNotification(mAudioRecord);
- }
+ if (listener != null) {
+ listener.onPeriodicNotification(mAudioRecord);
}
break;
default:
Log.e(TAG, "[ android.media.AudioRecord.NativeEventHandler ] " +
"Unknown event type: " + msg.what);
- break;
+ break;
}
}
- }
+ };
//---------------------------------------------------------
@@ -737,9 +723,10 @@ public class AudioRecord
return;
}
- if (recorder.mNativeEventHandler != null) {
- Message m = recorder.mNativeEventHandler.obtainMessage(what, arg1, arg2, obj);
- recorder.mNativeEventHandler.sendMessage(m);
+ if (recorder.mEventHandler != null) {
+ Message m =
+ recorder.mEventHandler.obtainMessage(what, arg1, arg2, obj);
+ recorder.mEventHandler.sendMessage(m);
}
}