Home | History | Annotate | Download | only in media
      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 
     17 #ifndef ANDROID_AUDIORECORD_H
     18 #define ANDROID_AUDIORECORD_H
     19 
     20 #include <cutils/sched_policy.h>
     21 #include <media/AudioSystem.h>
     22 #include <media/IAudioRecord.h>
     23 #include <utils/threads.h>
     24 
     25 namespace android {
     26 
     27 // ----------------------------------------------------------------------------
     28 
     29 struct audio_track_cblk_t;
     30 class AudioRecordClientProxy;
     31 
     32 // ----------------------------------------------------------------------------
     33 
     34 class AudioRecord : public RefBase
     35 {
     36 public:
     37 
     38     /* Events used by AudioRecord callback function (callback_t).
     39      * Keep in sync with frameworks/base/media/java/android/media/AudioRecord.java NATIVE_EVENT_*.
     40      */
     41     enum event_type {
     42         EVENT_MORE_DATA = 0,        // Request to read available data from buffer.
     43                                     // If this event is delivered but the callback handler
     44                                     // does not want to read the available data, the handler must
     45                                     // explicitly ignore the event by setting frameCount to zero.
     46         EVENT_OVERRUN = 1,          // Buffer overrun occurred.
     47         EVENT_MARKER = 2,           // Record head is at the specified marker position
     48                                     // (See setMarkerPosition()).
     49         EVENT_NEW_POS = 3,          // Record head is at a new position
     50                                     // (See setPositionUpdatePeriod()).
     51         EVENT_NEW_IAUDIORECORD = 4, // IAudioRecord was re-created, either due to re-routing and
     52                                     // voluntary invalidation by mediaserver, or mediaserver crash.
     53     };
     54 
     55     /* Client should declare a Buffer and pass address to obtainBuffer()
     56      * and releaseBuffer().  See also callback_t for EVENT_MORE_DATA.
     57      */
     58 
     59     class Buffer
     60     {
     61     public:
     62         // FIXME use m prefix
     63         size_t      frameCount;     // number of sample frames corresponding to size;
     64                                     // on input to obtainBuffer() it is the number of frames desired
     65                                     // on output from obtainBuffer() it is the number of available
     66                                     //    frames to be read
     67                                     // on input to releaseBuffer() it is currently ignored
     68 
     69         size_t      size;           // input/output in bytes == frameCount * frameSize
     70                                     // on input to obtainBuffer() it is ignored
     71                                     // on output from obtainBuffer() it is the number of available
     72                                     //    bytes to be read, which is frameCount * frameSize
     73                                     // on input to releaseBuffer() it is the number of bytes to
     74                                     //    release
     75                                     // FIXME This is redundant with respect to frameCount.  Consider
     76                                     //    removing size and making frameCount the primary field.
     77 
     78         union {
     79             void*       raw;
     80             short*      i16;        // signed 16-bit
     81             int8_t*     i8;         // unsigned 8-bit, offset by 0x80
     82                                     // input to obtainBuffer(): unused, output: pointer to buffer
     83         };
     84     };
     85 
     86     /* As a convenience, if a callback is supplied, a handler thread
     87      * is automatically created with the appropriate priority. This thread
     88      * invokes the callback when a new buffer becomes available or various conditions occur.
     89      * Parameters:
     90      *
     91      * event:   type of event notified (see enum AudioRecord::event_type).
     92      * user:    Pointer to context for use by the callback receiver.
     93      * info:    Pointer to optional parameter according to event type:
     94      *          - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read
     95      *                             more bytes than indicated by 'size' field and update 'size' if
     96      *                             fewer bytes are consumed.
     97      *          - EVENT_OVERRUN: unused.
     98      *          - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames.
     99      *          - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames.
    100      *          - EVENT_NEW_IAUDIORECORD: unused.
    101      */
    102 
    103     typedef void (*callback_t)(int event, void* user, void *info);
    104 
    105     /* Returns the minimum frame count required for the successful creation of
    106      * an AudioRecord object.
    107      * Returned status (from utils/Errors.h) can be:
    108      *  - NO_ERROR: successful operation
    109      *  - NO_INIT: audio server or audio hardware not initialized
    110      *  - BAD_VALUE: unsupported configuration
    111      * frameCount is guaranteed to be non-zero if status is NO_ERROR,
    112      * and is undefined otherwise.
    113      * FIXME This API assumes a route, and so should be deprecated.
    114      */
    115 
    116      static status_t getMinFrameCount(size_t* frameCount,
    117                                       uint32_t sampleRate,
    118                                       audio_format_t format,
    119                                       audio_channel_mask_t channelMask);
    120 
    121     /* How data is transferred from AudioRecord
    122      */
    123     enum transfer_type {
    124         TRANSFER_DEFAULT,   // not specified explicitly; determine from the other parameters
    125         TRANSFER_CALLBACK,  // callback EVENT_MORE_DATA
    126         TRANSFER_OBTAIN,    // call obtainBuffer() and releaseBuffer()
    127         TRANSFER_SYNC,      // synchronous read()
    128     };
    129 
    130     /* Constructs an uninitialized AudioRecord. No connection with
    131      * AudioFlinger takes place.  Use set() after this.
    132      *
    133      * Parameters:
    134      *
    135      * opPackageName:      The package name used for app ops.
    136      */
    137                         AudioRecord(const String16& opPackageName);
    138 
    139     /* Creates an AudioRecord object and registers it with AudioFlinger.
    140      * Once created, the track needs to be started before it can be used.
    141      * Unspecified values are set to appropriate default values.
    142      *
    143      * Parameters:
    144      *
    145      * inputSource:        Select the audio input to record from (e.g. AUDIO_SOURCE_DEFAULT).
    146      * sampleRate:         Data sink sampling rate in Hz.
    147      * format:             Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
    148      *                     16 bits per sample).
    149      * channelMask:        Channel mask, such that audio_is_input_channel(channelMask) is true.
    150      * opPackageName:      The package name used for app ops.
    151      * frameCount:         Minimum size of track PCM buffer in frames. This defines the
    152      *                     application's contribution to the
    153      *                     latency of the track.  The actual size selected by the AudioRecord could
    154      *                     be larger if the requested size is not compatible with current audio HAL
    155      *                     latency.  Zero means to use a default value.
    156      * cbf:                Callback function. If not null, this function is called periodically
    157      *                     to consume new data in TRANSFER_CALLBACK mode
    158      *                     and inform of marker, position updates, etc.
    159      * user:               Context for use by the callback receiver.
    160      * notificationFrames: The callback function is called each time notificationFrames PCM
    161      *                     frames are ready in record track output buffer.
    162      * sessionId:          Not yet supported.
    163      * transferType:       How data is transferred from AudioRecord.
    164      * flags:              See comments on audio_input_flags_t in <system/audio.h>
    165      * pAttributes:        If not NULL, supersedes inputSource for use case selection.
    166      * threadCanCallJava:  Not present in parameter list, and so is fixed at false.
    167      */
    168 
    169                         AudioRecord(audio_source_t inputSource,
    170                                     uint32_t sampleRate,
    171                                     audio_format_t format,
    172                                     audio_channel_mask_t channelMask,
    173                                     const String16& opPackageName,
    174                                     size_t frameCount = 0,
    175                                     callback_t cbf = NULL,
    176                                     void* user = NULL,
    177                                     uint32_t notificationFrames = 0,
    178                                     int sessionId = AUDIO_SESSION_ALLOCATE,
    179                                     transfer_type transferType = TRANSFER_DEFAULT,
    180                                     audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
    181                                     int uid = -1,
    182                                     pid_t pid = -1,
    183                                     const audio_attributes_t* pAttributes = NULL);
    184 
    185     /* Terminates the AudioRecord and unregisters it from AudioFlinger.
    186      * Also destroys all resources associated with the AudioRecord.
    187      */
    188 protected:
    189                         virtual ~AudioRecord();
    190 public:
    191 
    192     /* Initialize an AudioRecord that was created using the AudioRecord() constructor.
    193      * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters.
    194      * set() is not multi-thread safe.
    195      * Returned status (from utils/Errors.h) can be:
    196      *  - NO_ERROR: successful intialization
    197      *  - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use
    198      *  - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
    199      *  - NO_INIT: audio server or audio hardware not initialized
    200      *  - PERMISSION_DENIED: recording is not allowed for the requesting process
    201      * If status is not equal to NO_ERROR, don't call any other APIs on this AudioRecord.
    202      *
    203      * Parameters not listed in the AudioRecord constructors above:
    204      *
    205      * threadCanCallJava:  Whether callbacks are made from an attached thread and thus can call JNI.
    206      */
    207             status_t    set(audio_source_t inputSource,
    208                             uint32_t sampleRate,
    209                             audio_format_t format,
    210                             audio_channel_mask_t channelMask,
    211                             size_t frameCount = 0,
    212                             callback_t cbf = NULL,
    213                             void* user = NULL,
    214                             uint32_t notificationFrames = 0,
    215                             bool threadCanCallJava = false,
    216                             int sessionId = AUDIO_SESSION_ALLOCATE,
    217                             transfer_type transferType = TRANSFER_DEFAULT,
    218                             audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
    219                             int uid = -1,
    220                             pid_t pid = -1,
    221                             const audio_attributes_t* pAttributes = NULL);
    222 
    223     /* Result of constructing the AudioRecord. This must be checked for successful initialization
    224      * before using any AudioRecord API (except for set()), because using
    225      * an uninitialized AudioRecord produces undefined results.
    226      * See set() method above for possible return codes.
    227      */
    228             status_t    initCheck() const   { return mStatus; }
    229 
    230     /* Returns this track's estimated latency in milliseconds.
    231      * This includes the latency due to AudioRecord buffer size, resampling if applicable,
    232      * and audio hardware driver.
    233      */
    234             uint32_t    latency() const     { return mLatency; }
    235 
    236    /* getters, see constructor and set() */
    237 
    238             audio_format_t format() const   { return mFormat; }
    239             uint32_t    channelCount() const    { return mChannelCount; }
    240             size_t      frameCount() const  { return mFrameCount; }
    241             size_t      frameSize() const   { return mFrameSize; }
    242             audio_source_t inputSource() const  { return mAttributes.source; }
    243 
    244     /* After it's created the track is not active. Call start() to
    245      * make it active. If set, the callback will start being called.
    246      * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until
    247      * the specified event occurs on the specified trigger session.
    248      */
    249             status_t    start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
    250                               int triggerSession = 0);
    251 
    252     /* Stop a track.  The callback will cease being called.  Note that obtainBuffer() still
    253      * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK.
    254      */
    255             void        stop();
    256             bool        stopped() const;
    257 
    258     /* Return the sink sample rate for this record track in Hz.
    259      * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock.
    260      */
    261             uint32_t    getSampleRate() const   { return mSampleRate; }
    262 
    263     /* Sets marker position. When record reaches the number of frames specified,
    264      * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition
    265      * with marker == 0 cancels marker notification callback.
    266      * To set a marker at a position which would compute as 0,
    267      * a workaround is to set the marker at a nearby position such as ~0 or 1.
    268      * If the AudioRecord has been opened with no callback function associated,
    269      * the operation will fail.
    270      *
    271      * Parameters:
    272      *
    273      * marker:   marker position expressed in wrapping (overflow) frame units,
    274      *           like the return value of getPosition().
    275      *
    276      * Returned status (from utils/Errors.h) can be:
    277      *  - NO_ERROR: successful operation
    278      *  - INVALID_OPERATION: the AudioRecord has no callback installed.
    279      */
    280             status_t    setMarkerPosition(uint32_t marker);
    281             status_t    getMarkerPosition(uint32_t *marker) const;
    282 
    283     /* Sets position update period. Every time the number of frames specified has been recorded,
    284      * a callback with event type EVENT_NEW_POS is called.
    285      * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
    286      * callback.
    287      * If the AudioRecord has been opened with no callback function associated,
    288      * the operation will fail.
    289      * Extremely small values may be rounded up to a value the implementation can support.
    290      *
    291      * Parameters:
    292      *
    293      * updatePeriod:  position update notification period expressed in frames.
    294      *
    295      * Returned status (from utils/Errors.h) can be:
    296      *  - NO_ERROR: successful operation
    297      *  - INVALID_OPERATION: the AudioRecord has no callback installed.
    298      */
    299             status_t    setPositionUpdatePeriod(uint32_t updatePeriod);
    300             status_t    getPositionUpdatePeriod(uint32_t *updatePeriod) const;
    301 
    302     /* Return the total number of frames recorded since recording started.
    303      * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
    304      * It is reset to zero by stop().
    305      *
    306      * Parameters:
    307      *
    308      *  position:  Address where to return record head position.
    309      *
    310      * Returned status (from utils/Errors.h) can be:
    311      *  - NO_ERROR: successful operation
    312      *  - BAD_VALUE:  position is NULL
    313      */
    314             status_t    getPosition(uint32_t *position) const;
    315 
    316     /* Returns a handle on the audio input used by this AudioRecord.
    317      *
    318      * Parameters:
    319      *  none.
    320      *
    321      * Returned value:
    322      *  handle on audio hardware input
    323      */
    324 // FIXME The only known public caller is frameworks/opt/net/voip/src/jni/rtp/AudioGroup.cpp
    325             audio_io_handle_t    getInput() const __attribute__((__deprecated__))
    326                                                 { return getInputPrivate(); }
    327 private:
    328             audio_io_handle_t    getInputPrivate() const;
    329 public:
    330 
    331     /* Returns the audio session ID associated with this AudioRecord.
    332      *
    333      * Parameters:
    334      *  none.
    335      *
    336      * Returned value:
    337      *  AudioRecord session ID.
    338      *
    339      * No lock needed because session ID doesn't change after first set().
    340      */
    341             int    getSessionId() const { return mSessionId; }
    342 
    343     /* Public API for TRANSFER_OBTAIN mode.
    344      * Obtains a buffer of up to "audioBuffer->frameCount" full frames.
    345      * After draining these frames of data, the caller should release them with releaseBuffer().
    346      * If the track buffer is not empty, obtainBuffer() returns as many contiguous
    347      * full frames as are available immediately.
    348      *
    349      * If nonContig is non-NULL, it is an output parameter that will be set to the number of
    350      * additional non-contiguous frames that are predicted to be available immediately,
    351      * if the client were to release the first frames and then call obtainBuffer() again.
    352      * This value is only a prediction, and needs to be confirmed.
    353      * It will be set to zero for an error return.
    354      *
    355      * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK
    356      * regardless of the value of waitCount.
    357      * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a
    358      * maximum timeout based on waitCount; see chart below.
    359      * Buffers will be returned until the pool
    360      * is exhausted, at which point obtainBuffer() will either block
    361      * or return WOULD_BLOCK depending on the value of the "waitCount"
    362      * parameter.
    363      *
    364      * Interpretation of waitCount:
    365      *  +n  limits wait time to n * WAIT_PERIOD_MS,
    366      *  -1  causes an (almost) infinite wait time,
    367      *   0  non-blocking.
    368      *
    369      * Buffer fields
    370      * On entry:
    371      *  frameCount  number of frames requested
    372      *  size        ignored
    373      *  raw         ignored
    374      * After error return:
    375      *  frameCount  0
    376      *  size        0
    377      *  raw         undefined
    378      * After successful return:
    379      *  frameCount  actual number of frames available, <= number requested
    380      *  size        actual number of bytes available
    381      *  raw         pointer to the buffer
    382      */
    383 
    384             status_t    obtainBuffer(Buffer* audioBuffer, int32_t waitCount,
    385                                 size_t *nonContig = NULL);
    386 
    387             // Explicit Routing
    388     /**
    389      * TODO Document this method.
    390      */
    391             status_t setInputDevice(audio_port_handle_t deviceId);
    392 
    393     /**
    394      * TODO Document this method.
    395      */
    396             audio_port_handle_t getInputDevice();
    397 
    398      /* Returns the ID of the audio device actually used by the input to which this AudioRecord
    399       * is attached.
    400       * A value of AUDIO_PORT_HANDLE_NONE indicates the AudioRecord is not attached to any input.
    401       *
    402       * Parameters:
    403       *  none.
    404       */
    405      audio_port_handle_t getRoutedDeviceId();
    406 
    407     /* Add an AudioDeviceCallback. The caller will be notified when the audio device
    408      * to which this AudioRecord is routed is updated.
    409      * Replaces any previously installed callback.
    410      * Parameters:
    411      *  callback:  The callback interface
    412      * Returns NO_ERROR if successful.
    413      *         INVALID_OPERATION if the same callback is already installed.
    414      *         NO_INIT or PREMISSION_DENIED if AudioFlinger service is not reachable
    415      *         BAD_VALUE if the callback is NULL
    416      */
    417             status_t addAudioDeviceCallback(
    418                     const sp<AudioSystem::AudioDeviceCallback>& callback);
    419 
    420     /* remove an AudioDeviceCallback.
    421      * Parameters:
    422      *  callback:  The callback interface
    423      * Returns NO_ERROR if successful.
    424      *         INVALID_OPERATION if the callback is not installed
    425      *         BAD_VALUE if the callback is NULL
    426      */
    427             status_t removeAudioDeviceCallback(
    428                     const sp<AudioSystem::AudioDeviceCallback>& callback);
    429 
    430 private:
    431     /* If nonContig is non-NULL, it is an output parameter that will be set to the number of
    432      * additional non-contiguous frames that are predicted to be available immediately,
    433      * if the client were to release the first frames and then call obtainBuffer() again.
    434      * This value is only a prediction, and needs to be confirmed.
    435      * It will be set to zero for an error return.
    436      * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(),
    437      * in case the requested amount of frames is in two or more non-contiguous regions.
    438      * FIXME requested and elapsed are both relative times.  Consider changing to absolute time.
    439      */
    440             status_t    obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
    441                                      struct timespec *elapsed = NULL, size_t *nonContig = NULL);
    442 public:
    443 
    444     /* Public API for TRANSFER_OBTAIN mode.
    445      * Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill.
    446      *
    447      * Buffer fields:
    448      *  frameCount  currently ignored but recommend to set to actual number of frames consumed
    449      *  size        actual number of bytes consumed, must be multiple of frameSize
    450      *  raw         ignored
    451      */
    452             void        releaseBuffer(const Buffer* audioBuffer);
    453 
    454     /* As a convenience we provide a read() interface to the audio buffer.
    455      * Input parameter 'size' is in byte units.
    456      * This is implemented on top of obtainBuffer/releaseBuffer. For best
    457      * performance use callbacks. Returns actual number of bytes read >= 0,
    458      * or one of the following negative status codes:
    459      *      INVALID_OPERATION   AudioRecord is configured for streaming mode
    460      *      BAD_VALUE           size is invalid
    461      *      WOULD_BLOCK         when obtainBuffer() returns same, or
    462      *                          AudioRecord was stopped during the read
    463      *      or any other error code returned by IAudioRecord::start() or restoreRecord_l().
    464      * Default behavior is to only return when all data has been transferred. Set 'blocking' to
    465      * false for the method to return immediately without waiting to try multiple times to read
    466      * the full content of the buffer.
    467      */
    468             ssize_t     read(void* buffer, size_t size, bool blocking = true);
    469 
    470     /* Return the number of input frames lost in the audio driver since the last call of this
    471      * function.  Audio driver is expected to reset the value to 0 and restart counting upon
    472      * returning the current value by this function call.  Such loss typically occurs when the
    473      * user space process is blocked longer than the capacity of audio driver buffers.
    474      * Units: the number of input audio frames.
    475      * FIXME The side-effect of resetting the counter may be incompatible with multi-client.
    476      * Consider making it more like AudioTrack::getUnderrunFrames which doesn't have side effects.
    477      */
    478             uint32_t    getInputFramesLost() const;
    479 
    480 private:
    481     /* copying audio record objects is not allowed */
    482                         AudioRecord(const AudioRecord& other);
    483             AudioRecord& operator = (const AudioRecord& other);
    484 
    485     /* a small internal class to handle the callback */
    486     class AudioRecordThread : public Thread
    487     {
    488     public:
    489         AudioRecordThread(AudioRecord& receiver, bool bCanCallJava = false);
    490 
    491         // Do not call Thread::requestExitAndWait() without first calling requestExit().
    492         // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
    493         virtual void        requestExit();
    494 
    495                 void        pause();    // suspend thread from execution at next loop boundary
    496                 void        resume();   // allow thread to execute, if not requested to exit
    497                 void        wake();     // wake to handle changed notification conditions.
    498 
    499     private:
    500                 void        pauseInternal(nsecs_t ns = 0LL);
    501                                         // like pause(), but only used internally within thread
    502 
    503         friend class AudioRecord;
    504         virtual bool        threadLoop();
    505         AudioRecord&        mReceiver;
    506         virtual ~AudioRecordThread();
    507         Mutex               mMyLock;    // Thread::mLock is private
    508         Condition           mMyCond;    // Thread::mThreadExitedCondition is private
    509         bool                mPaused;    // whether thread is requested to pause at next loop entry
    510         bool                mPausedInt; // whether thread internally requests pause
    511         nsecs_t             mPausedNs;  // if mPausedInt then associated timeout, otherwise ignored
    512         bool                mIgnoreNextPausedInt;   // skip any internal pause and go immediately
    513                                         // to processAudioBuffer() as state may have changed
    514                                         // since pause time calculated.
    515     };
    516 
    517             // body of AudioRecordThread::threadLoop()
    518             // returns the maximum amount of time before we would like to run again, where:
    519             //      0           immediately
    520             //      > 0         no later than this many nanoseconds from now
    521             //      NS_WHENEVER still active but no particular deadline
    522             //      NS_INACTIVE inactive so don't run again until re-started
    523             //      NS_NEVER    never again
    524             static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3;
    525             nsecs_t processAudioBuffer();
    526 
    527             // caller must hold lock on mLock for all _l methods
    528 
    529             status_t openRecord_l(size_t epoch, const String16& opPackageName);
    530 
    531             // FIXME enum is faster than strcmp() for parameter 'from'
    532             status_t restoreRecord_l(const char *from);
    533 
    534     sp<AudioRecordThread>   mAudioRecordThread;
    535     mutable Mutex           mLock;
    536 
    537     // Current client state:  false = stopped, true = active.  Protected by mLock.  If more states
    538     // are added, consider changing this to enum State { ... } mState as in AudioTrack.
    539     bool                    mActive;
    540 
    541     // for client callback handler
    542     callback_t              mCbf;                   // callback handler for events, or NULL
    543     void*                   mUserData;
    544 
    545     // for notification APIs
    546     uint32_t                mNotificationFramesReq; // requested number of frames between each
    547                                                     // notification callback
    548                                                     // as specified in constructor or set()
    549     uint32_t                mNotificationFramesAct; // actual number of frames between each
    550                                                     // notification callback
    551     bool                    mRefreshRemaining;      // processAudioBuffer() should refresh
    552                                                     // mRemainingFrames and mRetryOnPartialBuffer
    553 
    554     // These are private to processAudioBuffer(), and are not protected by a lock
    555     uint32_t                mRemainingFrames;       // number of frames to request in obtainBuffer()
    556     bool                    mRetryOnPartialBuffer;  // sleep and retry after partial obtainBuffer()
    557     uint32_t                mObservedSequence;      // last observed value of mSequence
    558 
    559     uint32_t                mMarkerPosition;        // in wrapping (overflow) frame units
    560     bool                    mMarkerReached;
    561     uint32_t                mNewPosition;           // in frames
    562     uint32_t                mUpdatePeriod;          // in frames, zero means no EVENT_NEW_POS
    563 
    564     status_t                mStatus;
    565 
    566     String16                mOpPackageName;         // The package name used for app ops.
    567 
    568     size_t                  mFrameCount;            // corresponds to current IAudioRecord, value is
    569                                                     // reported back by AudioFlinger to the client
    570     size_t                  mReqFrameCount;         // frame count to request the first or next time
    571                                                     // a new IAudioRecord is needed, non-decreasing
    572 
    573     // constant after constructor or set()
    574     uint32_t                mSampleRate;
    575     audio_format_t          mFormat;
    576     uint32_t                mChannelCount;
    577     size_t                  mFrameSize;         // app-level frame size == AudioFlinger frame size
    578     uint32_t                mLatency;           // in ms
    579     audio_channel_mask_t    mChannelMask;
    580     audio_input_flags_t     mFlags;
    581     int                     mSessionId;
    582     transfer_type           mTransfer;
    583 
    584     // Next 5 fields may be changed if IAudioRecord is re-created, but always != 0
    585     // provided the initial set() was successful
    586     sp<IAudioRecord>        mAudioRecord;
    587     sp<IMemory>             mCblkMemory;
    588     audio_track_cblk_t*     mCblk;              // re-load after mLock.unlock()
    589     sp<IMemory>             mBufferMemory;
    590     audio_io_handle_t       mInput;             // returned by AudioSystem::getInput()
    591 
    592     int                     mPreviousPriority;  // before start()
    593     SchedPolicy             mPreviousSchedulingGroup;
    594     bool                    mAwaitBoost;    // thread should wait for priority boost before running
    595 
    596     // The proxy should only be referenced while a lock is held because the proxy isn't
    597     // multi-thread safe.
    598     // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock,
    599     // provided that the caller also holds an extra reference to the proxy and shared memory to keep
    600     // them around in case they are replaced during the obtainBuffer().
    601     sp<AudioRecordClientProxy> mProxy;
    602 
    603     bool                    mInOverrun;         // whether recorder is currently in overrun state
    604 
    605 private:
    606     class DeathNotifier : public IBinder::DeathRecipient {
    607     public:
    608         DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { }
    609     protected:
    610         virtual void        binderDied(const wp<IBinder>& who);
    611     private:
    612         const wp<AudioRecord> mAudioRecord;
    613     };
    614 
    615     sp<DeathNotifier>       mDeathNotifier;
    616     uint32_t                mSequence;              // incremented for each new IAudioRecord attempt
    617     int                     mClientUid;
    618     pid_t                   mClientPid;
    619     audio_attributes_t      mAttributes;
    620 
    621     // For Device Selection API
    622     //  a value of AUDIO_PORT_HANDLE_NONE indicated default (AudioPolicyManager) routing.
    623     audio_port_handle_t    mSelectedDeviceId;
    624     sp<AudioSystem::AudioDeviceCallback> mDeviceCallback;
    625 };
    626 
    627 }; // namespace android
    628 
    629 #endif // ANDROID_AUDIORECORD_H
    630