AudioRecord.java revision cfde15d709a3466b9d503f51819b7d28e718b482
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.media;
18
19import java.lang.ref.WeakReference;
20import java.io.OutputStream;
21import java.io.IOException;
22import java.lang.IllegalArgumentException;
23import java.lang.IllegalStateException;
24import java.lang.Thread;
25import java.nio.ByteBuffer;
26
27import android.os.Handler;
28import android.os.Looper;
29import android.os.Message;
30import android.util.Log;
31
32/**
33 * The AudioRecord class manages the audio resources for Java applications
34 * to record audio from the audio input hardware of the platform. This is
35 * achieved by "pulling" (reading) the data from the AudioRecord object. The
36 * application is responsible for polling the AudioRecord object in time using one of
37 * the following three methods:  {@link #read(byte[],int, int)}, {@link #read(short[], int, int)}
38 * or {@link #read(ByteBuffer, int)}. The choice of which method to use will be based
39 * on the audio data storage format that is the most convenient for the user of AudioRecord.
40 * <p>Upon creation, an AudioRecord object initializes its associated audio buffer that it will
41 * fill with the new audio data. The size of this buffer, specified during the construction,
42 * determines how long an AudioRecord can record before "over-running" data that has not
43 * been read yet. Data should be read from the audio hardware in chunks of sizes inferior to
44 * the total recording buffer size.
45 */
46public class AudioRecord
47{
48    //---------------------------------------------------------
49    // Constants
50    //--------------------
51    /**
52     *  indicates AudioRecord state is not successfully initialized.
53     */
54    public static final int STATE_UNINITIALIZED = 0;
55    /**
56     *  indicates AudioRecord state is ready to be used
57     */
58    public static final int STATE_INITIALIZED   = 1;
59
60    /**
61     * indicates AudioRecord recording state is not recording
62     */
63    public static final int RECORDSTATE_STOPPED = 1;  // matches SL_RECORDSTATE_STOPPED
64    /**
65     * indicates AudioRecord recording state is recording
66     */
67    public static final int RECORDSTATE_RECORDING = 3;// matches SL_RECORDSTATE_RECORDING
68
69    // Error codes:
70    // to keep in sync with frameworks/base/core/jni/android_media_AudioRecord.cpp
71    /**
72     * Denotes a successful operation.
73     */
74    public static final int SUCCESS                 = 0;
75    /**
76     * Denotes a generic operation failure.
77     */
78    public static final int ERROR                   = -1;
79    /**
80     * Denotes a failure due to the use of an invalid value.
81     */
82    public static final int ERROR_BAD_VALUE         = -2;
83    /**
84     * Denotes a failure due to the improper use of a method.
85     */
86    public static final int ERROR_INVALID_OPERATION = -3;
87
88    private static final int AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT      = -16;
89    private static final int AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK  = -17;
90    private static final int AUDIORECORD_ERROR_SETUP_INVALIDFORMAT       = -18;
91    private static final int AUDIORECORD_ERROR_SETUP_INVALIDSOURCE       = -19;
92    private static final int AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED    = -20;
93
94    // Events:
95    // to keep in sync with frameworks/base/include/media/AudioRecord.h
96    /**
97     * Event id denotes when record head has reached a previously set marker.
98     */
99    private static final int NATIVE_EVENT_MARKER  = 2;
100    /**
101     * Event id denotes when previously set update period has elapsed during recording.
102     */
103    private static final int NATIVE_EVENT_NEW_POS = 3;
104
105    private final static String TAG = "AudioRecord-Java";
106
107
108    //---------------------------------------------------------
109    // Used exclusively by native code
110    //--------------------
111    /**
112     * Accessed by native methods: provides access to C++ AudioRecord object
113     */
114    @SuppressWarnings("unused")
115    private int mNativeRecorderInJavaObj;
116
117    /**
118     * Accessed by native methods: provides access to the callback data.
119     */
120    @SuppressWarnings("unused")
121    private int mNativeCallbackCookie;
122
123
124    //---------------------------------------------------------
125    // Member variables
126    //--------------------
127    /**
128     * The audio data sampling rate in Hz.
129     */
130    private int mSampleRate = 22050;
131    /**
132     * The number of input audio channels (1 is mono, 2 is stereo)
133     */
134    private int mChannelCount = 1;
135    /**
136     * The audio channel mask
137     */
138    private int mChannels = AudioFormat.CHANNEL_IN_MONO;
139    /**
140     * The current audio channel configuration
141     */
142    private int mChannelConfiguration = AudioFormat.CHANNEL_IN_MONO;
143    /**
144     * The encoding of the audio samples.
145     * @see AudioFormat#ENCODING_PCM_8BIT
146     * @see AudioFormat#ENCODING_PCM_16BIT
147     */
148    private int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
149    /**
150     * Where the audio data is recorded from.
151     */
152    private int mRecordSource = MediaRecorder.AudioSource.DEFAULT;
153    /**
154     * Indicates the state of the AudioRecord instance.
155     */
156    private int mState = STATE_UNINITIALIZED;
157    /**
158     * Indicates the recording state of the AudioRecord instance.
159     */
160    private int mRecordingState = RECORDSTATE_STOPPED;
161    /**
162     * Lock to make sure mRecordingState updates are reflecting the actual state of the object.
163     */
164    private final Object mRecordingStateLock = new Object();
165    /**
166     * The listener the AudioRecord notifies when the record position reaches a marker
167     * or for periodic updates during the progression of the record head.
168     *  @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)
169     *  @see #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)
170     */
171    private OnRecordPositionUpdateListener mPositionListener = null;
172    /**
173     * Lock to protect position listener updates against event notifications
174     */
175    private final Object mPositionListenerLock = new Object();
176    /**
177     * Handler for marker events coming from the native code
178     */
179    private NativeEventHandler mEventHandler = null;
180    /**
181     * Looper associated with the thread that creates the AudioRecord instance
182     */
183    private Looper mInitializationLooper = null;
184    /**
185     * Size of the native audio buffer.
186     */
187    private int mNativeBufferSizeInBytes = 0;
188    /**
189     * Audio session ID
190     */
191    private int mSessionId = 0;
192
193    //---------------------------------------------------------
194    // Constructor, Finalize
195    //--------------------
196    /**
197     * Class constructor.
198     * @param audioSource the recording source. See {@link MediaRecorder.AudioSource} for
199     *    recording source definitions.
200     * @param sampleRateInHz the sample rate expressed in Hertz. 44100Hz is currently the only
201     *   rate that is guaranteed to work on all devices, but other rates such as 22050,
202     *   16000, and 11025 may work on some devices.
203     * @param channelConfig describes the configuration of the audio channels.
204     *   See {@link AudioFormat#CHANNEL_IN_MONO} and
205     *   {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is guaranteed
206     *   to work on all devices.
207     * @param audioFormat the format in which the audio data is represented.
208     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
209     *   {@link AudioFormat#ENCODING_PCM_8BIT}
210     * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is written
211     *   to during the recording. New audio data can be read from this buffer in smaller chunks
212     *   than this size. See {@link #getMinBufferSize(int, int, int)} to determine the minimum
213     *   required buffer size for the successful creation of an AudioRecord instance. Using values
214     *   smaller than getMinBufferSize() will result in an initialization failure.
215     * @throws java.lang.IllegalArgumentException
216     */
217    public AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
218            int bufferSizeInBytes)
219    throws IllegalArgumentException {
220        mState = STATE_UNINITIALIZED;
221        mRecordingState = RECORDSTATE_STOPPED;
222
223        // remember which looper is associated with the AudioRecord instanciation
224        if ((mInitializationLooper = Looper.myLooper()) == null) {
225            mInitializationLooper = Looper.getMainLooper();
226        }
227
228        audioParamCheck(audioSource, sampleRateInHz, channelConfig, audioFormat);
229
230        audioBuffSizeCheck(bufferSizeInBytes);
231
232        // native initialization
233        int[] session = new int[1];
234        session[0] = 0;
235        //TODO: update native initialization when information about hardware init failure
236        //      due to capture device already open is available.
237        int initResult = native_setup( new WeakReference<AudioRecord>(this),
238                mRecordSource, mSampleRate, mChannels, mAudioFormat, mNativeBufferSizeInBytes,
239                session);
240        if (initResult != SUCCESS) {
241            loge("Error code "+initResult+" when initializing native AudioRecord object.");
242            return; // with mState == STATE_UNINITIALIZED
243        }
244
245        mSessionId = session[0];
246
247        mState = STATE_INITIALIZED;
248    }
249
250
251    // Convenience method for the constructor's parameter checks.
252    // This is where constructor IllegalArgumentException-s are thrown
253    // postconditions:
254    //    mRecordSource is valid
255    //    mChannelCount is valid
256    //    mChannels is valid
257    //    mAudioFormat is valid
258    //    mSampleRate is valid
259    private void audioParamCheck(int audioSource, int sampleRateInHz,
260                                 int channelConfig, int audioFormat) {
261
262        //--------------
263        // audio source
264        if ( (audioSource < MediaRecorder.AudioSource.DEFAULT) ||
265             (audioSource > MediaRecorder.getAudioSourceMax()) )  {
266            throw (new IllegalArgumentException("Invalid audio source."));
267        } else {
268            mRecordSource = audioSource;
269        }
270
271        //--------------
272        // sample rate
273        if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) {
274            throw (new IllegalArgumentException(sampleRateInHz
275                    + "Hz is not a supported sample rate."));
276        } else {
277            mSampleRate = sampleRateInHz;
278        }
279
280        //--------------
281        // channel config
282        mChannelConfiguration = channelConfig;
283
284        switch (channelConfig) {
285        case AudioFormat.CHANNEL_IN_DEFAULT: // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
286        case AudioFormat.CHANNEL_IN_MONO:
287        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
288            mChannelCount = 1;
289            mChannels = AudioFormat.CHANNEL_IN_MONO;
290            break;
291        case AudioFormat.CHANNEL_IN_STEREO:
292        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
293            mChannelCount = 2;
294            mChannels = AudioFormat.CHANNEL_IN_STEREO;
295            break;
296        default:
297            mChannelCount = 0;
298            mChannels = AudioFormat.CHANNEL_INVALID;
299            mChannelConfiguration = AudioFormat.CHANNEL_INVALID;
300            throw (new IllegalArgumentException("Unsupported channel configuration."));
301        }
302
303        //--------------
304        // audio format
305        switch (audioFormat) {
306        case AudioFormat.ENCODING_DEFAULT:
307            mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
308            break;
309        case AudioFormat.ENCODING_PCM_16BIT:
310        case AudioFormat.ENCODING_PCM_8BIT:
311            mAudioFormat = audioFormat;
312            break;
313        default:
314            mAudioFormat = AudioFormat.ENCODING_INVALID;
315        throw (new IllegalArgumentException("Unsupported sample encoding."
316                + " Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT."));
317        }
318    }
319
320
321    // Convenience method for the contructor's audio buffer size check.
322    // preconditions:
323    //    mChannelCount is valid
324    //    mAudioFormat is AudioFormat.ENCODING_PCM_8BIT OR AudioFormat.ENCODING_PCM_16BIT
325    // postcondition:
326    //    mNativeBufferSizeInBytes is valid (multiple of frame size, positive)
327    private void audioBuffSizeCheck(int audioBufferSize) {
328        // NB: this section is only valid with PCM data.
329        // To update when supporting compressed formats
330        int frameSizeInBytes = mChannelCount
331            * (mAudioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
332        if ((audioBufferSize % frameSizeInBytes != 0) || (audioBufferSize < 1)) {
333            throw (new IllegalArgumentException("Invalid audio buffer size."));
334        }
335
336        mNativeBufferSizeInBytes = audioBufferSize;
337    }
338
339
340
341    /**
342     * Releases the native AudioRecord resources.
343     * The object can no longer be used and the reference should be set to null
344     * after a call to release()
345     */
346    public void release() {
347        try {
348            stop();
349        } catch(IllegalStateException ise) {
350            // don't raise an exception, we're releasing the resources.
351        }
352        native_release();
353        mState = STATE_UNINITIALIZED;
354    }
355
356
357    @Override
358    protected void finalize() {
359        native_finalize();
360    }
361
362
363    //--------------------------------------------------------------------------
364    // Getters
365    //--------------------
366    /**
367     * Returns the configured audio data sample rate in Hz
368     */
369    public int getSampleRate() {
370        return mSampleRate;
371    }
372
373    /**
374     * Returns the audio recording source.
375     * @see MediaRecorder.AudioSource
376     */
377    public int getAudioSource() {
378        return mRecordSource;
379    }
380
381    /**
382     * Returns the configured audio data format. See {@link AudioFormat#ENCODING_PCM_16BIT}
383     * and {@link AudioFormat#ENCODING_PCM_8BIT}.
384     */
385    public int getAudioFormat() {
386        return mAudioFormat;
387    }
388
389    /**
390     * Returns the configured channel configuration.
391     * See {@link AudioFormat#CHANNEL_IN_MONO}
392     * and {@link AudioFormat#CHANNEL_IN_STEREO}.
393     */
394    public int getChannelConfiguration() {
395        return mChannelConfiguration;
396    }
397
398    /**
399     * Returns the configured number of channels.
400     */
401    public int getChannelCount() {
402        return mChannelCount;
403    }
404
405    /**
406     * Returns the state of the AudioRecord instance. This is useful after the
407     * AudioRecord instance has been created to check if it was initialized
408     * properly. This ensures that the appropriate hardware resources have been
409     * acquired.
410     * @see AudioRecord#STATE_INITIALIZED
411     * @see AudioRecord#STATE_UNINITIALIZED
412     */
413    public int getState() {
414        return mState;
415    }
416
417    /**
418     * Returns the recording state of the AudioRecord instance.
419     * @see AudioRecord#RECORDSTATE_STOPPED
420     * @see AudioRecord#RECORDSTATE_RECORDING
421     */
422    public int getRecordingState() {
423        return mRecordingState;
424    }
425
426    /**
427     * Returns the notification marker position expressed in frames.
428     */
429    public int getNotificationMarkerPosition() {
430        return native_get_marker_pos();
431    }
432
433    /**
434     * Returns the notification update period expressed in frames.
435     */
436    public int getPositionNotificationPeriod() {
437        return native_get_pos_update_period();
438    }
439
440    /**
441     * Returns the minimum buffer size required for the successful creation of an AudioRecord
442     * object.
443     * Note that this size doesn't guarantee a smooth recording under load, and higher values
444     * should be chosen according to the expected frequency at which the AudioRecord instance
445     * will be polled for new data.
446     * @param sampleRateInHz the sample rate expressed in Hertz.
447     * @param channelConfig describes the configuration of the audio channels.
448     *   See {@link AudioFormat#CHANNEL_IN_MONO} and
449     *   {@link AudioFormat#CHANNEL_IN_STEREO}
450     * @param audioFormat the format in which the audio data is represented.
451     *   See {@link AudioFormat#ENCODING_PCM_16BIT}.
452     * @return {@link #ERROR_BAD_VALUE} if the recording parameters are not supported by the
453     *  hardware, or an invalid parameter was passed,
454     *  or {@link #ERROR} if the implementation was unable to query the hardware for its
455     *  output properties,
456     *   or the minimum buffer size expressed in bytes.
457     * @see #AudioRecord(int, int, int, int, int) for more information on valid
458     *   configuration values.
459     */
460    static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
461        int channelCount = 0;
462        switch(channelConfig) {
463        case AudioFormat.CHANNEL_IN_DEFAULT: // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
464        case AudioFormat.CHANNEL_IN_MONO:
465        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
466            channelCount = 1;
467            break;
468        case AudioFormat.CHANNEL_IN_STEREO:
469        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
470            channelCount = 2;
471            break;
472        case AudioFormat.CHANNEL_INVALID:
473        default:
474            loge("getMinBufferSize(): Invalid channel configuration.");
475            return AudioRecord.ERROR_BAD_VALUE;
476        }
477
478        // PCM_8BIT is not supported at the moment
479        if (audioFormat != AudioFormat.ENCODING_PCM_16BIT) {
480            loge("getMinBufferSize(): Invalid audio format.");
481            return AudioRecord.ERROR_BAD_VALUE;
482        }
483
484        int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);
485        if (size == 0) {
486            return AudioRecord.ERROR_BAD_VALUE;
487        }
488        else if (size == -1) {
489            return AudioRecord.ERROR;
490        }
491        else {
492            return size;
493        }
494    }
495
496    /**
497     * Returns the audio session ID.
498     *
499     * @return the ID of the audio session this AudioRecord belongs to.
500     */
501    public int getAudioSessionId() {
502        return mSessionId;
503    }
504
505    //---------------------------------------------------------
506    // Transport control methods
507    //--------------------
508    /**
509     * Starts recording from the AudioRecord instance.
510     * @throws IllegalStateException
511     */
512    public void startRecording()
513    throws IllegalStateException {
514        if (mState != STATE_INITIALIZED) {
515            throw(new IllegalStateException("startRecording() called on an "
516                    +"uninitialized AudioRecord."));
517        }
518
519        // start recording
520        synchronized(mRecordingStateLock) {
521            if (native_start(MediaSyncEvent.SYNC_EVENT_NONE, 0) == SUCCESS) {
522                mRecordingState = RECORDSTATE_RECORDING;
523            }
524        }
525    }
526
527    /**
528     * Starts recording from the AudioRecord instance when the specified synchronization event
529     * occurs on the specified audio session.
530     * @throws IllegalStateException
531     * @param syncEvent event that triggers the capture.
532     * @see MediaSyncEvent
533     */
534    public void startRecording(MediaSyncEvent syncEvent)
535    throws IllegalStateException {
536        if (mState != STATE_INITIALIZED) {
537            throw(new IllegalStateException("startRecording() called on an "
538                    +"uninitialized AudioRecord."));
539        }
540
541        // start recording
542        synchronized(mRecordingStateLock) {
543            if (native_start(syncEvent.getType(), syncEvent.getAudioSessionId()) == SUCCESS) {
544                mRecordingState = RECORDSTATE_RECORDING;
545            }
546        }
547    }
548
549    /**
550     * Stops recording.
551     * @throws IllegalStateException
552     */
553    public void stop()
554    throws IllegalStateException {
555        if (mState != STATE_INITIALIZED) {
556            throw(new IllegalStateException("stop() called on an uninitialized AudioRecord."));
557        }
558
559        // stop recording
560        synchronized(mRecordingStateLock) {
561            native_stop();
562            mRecordingState = RECORDSTATE_STOPPED;
563        }
564    }
565
566
567    //---------------------------------------------------------
568    // Audio data supply
569    //--------------------
570    /**
571     * Reads audio data from the audio hardware for recording into a buffer.
572     * @param audioData the array to which the recorded audio data is written.
573     * @param offsetInBytes index in audioData from which the data is written expressed in bytes.
574     * @param sizeInBytes the number of requested bytes.
575     * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION}
576     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
577     *    the parameters don't resolve to valid data and indexes.
578     *    The number of bytes will not exceed sizeInBytes.
579     */
580    public int read(byte[] audioData, int offsetInBytes, int sizeInBytes) {
581        if (mState != STATE_INITIALIZED) {
582            return ERROR_INVALID_OPERATION;
583        }
584
585        if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
586                || (offsetInBytes + sizeInBytes > audioData.length)) {
587            return ERROR_BAD_VALUE;
588        }
589
590        return native_read_in_byte_array(audioData, offsetInBytes, sizeInBytes);
591    }
592
593
594    /**
595     * Reads audio data from the audio hardware for recording into a buffer.
596     * @param audioData the array to which the recorded audio data is written.
597     * @param offsetInShorts index in audioData from which the data is written expressed in shorts.
598     * @param sizeInShorts the number of requested shorts.
599     * @return the number of shorts that were read or or {@link #ERROR_INVALID_OPERATION}
600     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
601     *    the parameters don't resolve to valid data and indexes.
602     *    The number of shorts will not exceed sizeInShorts.
603     */
604    public int read(short[] audioData, int offsetInShorts, int sizeInShorts) {
605        if (mState != STATE_INITIALIZED) {
606            return ERROR_INVALID_OPERATION;
607        }
608
609        if ( (audioData == null) || (offsetInShorts < 0 ) || (sizeInShorts < 0)
610                || (offsetInShorts + sizeInShorts > audioData.length)) {
611            return ERROR_BAD_VALUE;
612        }
613
614        return native_read_in_short_array(audioData, offsetInShorts, sizeInShorts);
615    }
616
617
618    /**
619     * Reads audio data from the audio hardware for recording into a direct buffer. If this buffer
620     * is not a direct buffer, this method will always return 0.
621     * Note that the value returned by {@link java.nio.Buffer#position()} on this buffer is
622     * unchanged after a call to this method.
623     * @param audioBuffer the direct buffer to which the recorded audio data is written.
624     * @param sizeInBytes the number of requested bytes.
625     * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION}
626     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
627     *    the parameters don't resolve to valid data and indexes.
628     *    The number of bytes will not exceed sizeInBytes.
629     */
630    public int read(ByteBuffer audioBuffer, int sizeInBytes) {
631        if (mState != STATE_INITIALIZED) {
632            return ERROR_INVALID_OPERATION;
633        }
634
635        if ( (audioBuffer == null) || (sizeInBytes < 0) ) {
636            return ERROR_BAD_VALUE;
637        }
638
639        return native_read_in_direct_buffer(audioBuffer, sizeInBytes);
640    }
641
642
643    //--------------------------------------------------------------------------
644    // Initialization / configuration
645    //--------------------
646    /**
647     * Sets the listener the AudioRecord notifies when a previously set marker is reached or
648     * for each periodic record head position update.
649     * @param listener
650     */
651    public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener) {
652        setRecordPositionUpdateListener(listener, null);
653    }
654
655    /**
656     * Sets the listener the AudioRecord notifies when a previously set marker is reached or
657     * for each periodic record head position update.
658     * Use this method to receive AudioRecord events in the Handler associated with another
659     * thread than the one in which you created the AudioTrack instance.
660     * @param listener
661     * @param handler the Handler that will receive the event notification messages.
662     */
663    public void setRecordPositionUpdateListener(OnRecordPositionUpdateListener listener,
664                                                    Handler handler) {
665        synchronized (mPositionListenerLock) {
666
667            mPositionListener = listener;
668
669            if (listener != null) {
670                if (handler != null) {
671                    mEventHandler = new NativeEventHandler(this, handler.getLooper());
672                } else {
673                    // no given handler, use the looper the AudioRecord was created in
674                    mEventHandler = new NativeEventHandler(this, mInitializationLooper);
675                }
676            } else {
677                mEventHandler = null;
678            }
679        }
680
681    }
682
683
684    /**
685     * Sets the marker position at which the listener is called, if set with
686     * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or
687     * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}.
688     * @param markerInFrames marker position expressed in frames
689     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
690     *  {@link #ERROR_INVALID_OPERATION}
691     */
692    public int setNotificationMarkerPosition(int markerInFrames) {
693        return native_set_marker_pos(markerInFrames);
694    }
695
696
697    /**
698     * Sets the period at which the listener is called, if set with
699     * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener)} or
700     * {@link #setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)}.
701     * @param periodInFrames update period expressed in frames
702     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_INVALID_OPERATION}
703     */
704    public int setPositionNotificationPeriod(int periodInFrames) {
705        return native_set_pos_update_period(periodInFrames);
706    }
707
708
709    //---------------------------------------------------------
710    // Interface definitions
711    //--------------------
712    /**
713     * Interface definition for a callback to be invoked when an AudioRecord has
714     * reached a notification marker set by {@link AudioRecord#setNotificationMarkerPosition(int)}
715     * or for periodic updates on the progress of the record head, as set by
716     * {@link AudioRecord#setPositionNotificationPeriod(int)}.
717     */
718    public interface OnRecordPositionUpdateListener  {
719        /**
720         * Called on the listener to notify it that the previously set marker has been reached
721         * by the recording head.
722         */
723        void onMarkerReached(AudioRecord recorder);
724
725        /**
726         * Called on the listener to periodically notify it that the record head has reached
727         * a multiple of the notification period.
728         */
729        void onPeriodicNotification(AudioRecord recorder);
730    }
731
732
733
734    //---------------------------------------------------------
735    // Inner classes
736    //--------------------
737
738    /**
739     * Helper class to handle the forwarding of native events to the appropriate listener
740     * (potentially) handled in a different thread
741     */
742    private class NativeEventHandler extends Handler {
743
744        private final AudioRecord mAudioRecord;
745
746        NativeEventHandler(AudioRecord recorder, Looper looper) {
747            super(looper);
748            mAudioRecord = recorder;
749        }
750
751        @Override
752        public void handleMessage(Message msg) {
753            OnRecordPositionUpdateListener listener = null;
754            synchronized (mPositionListenerLock) {
755                listener = mAudioRecord.mPositionListener;
756            }
757
758            switch(msg.what) {
759            case NATIVE_EVENT_MARKER:
760                if (listener != null) {
761                    listener.onMarkerReached(mAudioRecord);
762                }
763                break;
764            case NATIVE_EVENT_NEW_POS:
765                if (listener != null) {
766                    listener.onPeriodicNotification(mAudioRecord);
767                }
768                break;
769            default:
770                Log.e(TAG, "[ android.media.AudioRecord.NativeEventHandler ] " +
771                        "Unknown event type: " + msg.what);
772            break;
773            }
774        }
775    };
776
777
778    //---------------------------------------------------------
779    // Java methods called from the native side
780    //--------------------
781    @SuppressWarnings("unused")
782    private static void postEventFromNative(Object audiorecord_ref,
783            int what, int arg1, int arg2, Object obj) {
784        //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2);
785        AudioRecord recorder = (AudioRecord)((WeakReference)audiorecord_ref).get();
786        if (recorder == null) {
787            return;
788        }
789
790        if (recorder.mEventHandler != null) {
791            Message m =
792                recorder.mEventHandler.obtainMessage(what, arg1, arg2, obj);
793            recorder.mEventHandler.sendMessage(m);
794        }
795
796    }
797
798
799    //---------------------------------------------------------
800    // Native methods called from the Java side
801    //--------------------
802
803    private native final int native_setup(Object audiorecord_this,
804            int recordSource, int sampleRate, int nbChannels, int audioFormat,
805            int buffSizeInBytes, int[] sessionId);
806
807    private native final void native_finalize();
808
809    private native final void native_release();
810
811    private native final int native_start(int syncEvent, int sessionId);
812
813    private native final void native_stop();
814
815    private native final int native_read_in_byte_array(byte[] audioData,
816            int offsetInBytes, int sizeInBytes);
817
818    private native final int native_read_in_short_array(short[] audioData,
819            int offsetInShorts, int sizeInShorts);
820
821    private native final int native_read_in_direct_buffer(Object jBuffer, int sizeInBytes);
822
823    private native final int native_set_marker_pos(int marker);
824    private native final int native_get_marker_pos();
825
826    private native final int native_set_pos_update_period(int updatePeriod);
827    private native final int native_get_pos_update_period();
828
829    static private native final int native_get_min_buff_size(
830            int sampleRateInHz, int channelCount, int audioFormat);
831
832
833    //---------------------------------------------------------
834    // Utility methods
835    //------------------
836
837    private static void logd(String msg) {
838        Log.d(TAG, "[ android.media.AudioRecord ] " + msg);
839    }
840
841    private static void loge(String msg) {
842        Log.e(TAG, "[ android.media.AudioRecord ] " + msg);
843    }
844
845}
846
847