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 <memory>
21 #include <vector>
22 
23 #include <binder/IMemory.h>
24 #include <cutils/sched_policy.h>
25 #include <media/AudioSystem.h>
26 #include <media/AudioTimestamp.h>
27 #include <media/MediaMetricsItem.h>
28 #include <media/Modulo.h>
29 #include <media/RecordingActivityTracker.h>
30 #include <utils/RefBase.h>
31 #include <utils/threads.h>
32 
33 #include "android/media/IAudioRecord.h"
34 #include <android/content/AttributionSourceState.h>
35 
36 namespace android {
37 
38 // ----------------------------------------------------------------------------
39 
40 struct audio_track_cblk_t;
41 class AudioRecordClientProxy;
42 // ----------------------------------------------------------------------------
43 
44 class AudioRecord : public AudioSystem::AudioDeviceCallback
45 {
46 public:
47 
48     class Buffer
49     {
50       friend AudioRecord;
51     public:
size()52         size_t size() const { return mSize; }
getFrameCount()53         size_t getFrameCount() const { return frameCount; }
data()54         uint8_t* data() const { return ui8; }
55         // Leaving public for now to assist refactoring. This class will
56         // be replaced.
57         size_t      frameCount;     // number of sample frames corresponding to size;
58                                     // on input to obtainBuffer() it is the number of frames desired
59                                     // on output from obtainBuffer() it is the number of available
60                                     //    frames to be read
61                                     // on input to releaseBuffer() it is currently ignored
62 
63     private:
64         size_t      mSize;          // input/output in bytes == frameCount * frameSize
65                                     // on input to obtainBuffer() it is ignored
66                                     // on output from obtainBuffer() it is the number of available
67                                     //    bytes to be read, which is frameCount * frameSize
68                                     // on input to releaseBuffer() it is the number of bytes to
69                                     //    release
70                                     // FIXME This is redundant with respect to frameCount.  Consider
71                                     //    removing size and making frameCount the primary field.
72 
73         union {
74             void*       raw;
75             int16_t*    i16;        // signed 16-bit
76             uint8_t*    ui8;        // unsigned 8-bit, offset by 0x80
77                                     // input to obtainBuffer(): unused, output: pointer to buffer
78         };
79 
80         uint32_t    sequence;       // IAudioRecord instance sequence number, as of obtainBuffer().
81                                     // It is set by obtainBuffer() and confirmed by releaseBuffer().
82                                     // Not "user-serviceable".
83                                     // TODO Consider sp<IMemory> instead, or in addition to this.
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 
104     class IAudioRecordCallback : public virtual RefBase {
105         friend AudioRecord;
106      protected:
107         // Request for client to read newly available data.
108         // Used for TRANSFER_CALLBACK mode.
109         // Parameters:
110         //  - buffer : Buffer to read from
111         // Returns:
112         //  - Number of bytes actually consumed.
onMoreData(const AudioRecord::Buffer & buffer)113         virtual size_t onMoreData([[maybe_unused]] const AudioRecord::Buffer& buffer) { return 0; }
114         // A buffer overrun occurred.
onOverrun()115         virtual void onOverrun() {}
116         // Record head is at the specified marker (see setMarkerPosition()).
onMarker(uint32_t markerPosition)117         virtual void onMarker([[maybe_unused]] uint32_t markerPosition) {}
118         // Record head is at a new position (see setPositionUpdatePeriod()).
onNewPos(uint32_t newPos)119         virtual void onNewPos([[maybe_unused]] uint32_t newPos) {}
120         // IAudioRecord was recreated due to re-routing, server invalidation or
121         // server crash.
onNewIAudioRecord()122         virtual void onNewIAudioRecord() {}
123     };
124 
125     /* Returns the minimum frame count required for the successful creation of
126      * an AudioRecord object.
127      * Returned status (from utils/Errors.h) can be:
128      *  - NO_ERROR: successful operation
129      *  - NO_INIT: audio server or audio hardware not initialized
130      *  - BAD_VALUE: unsupported configuration
131      * frameCount is guaranteed to be non-zero if status is NO_ERROR,
132      * and is undefined otherwise.
133      * FIXME This API assumes a route, and so should be deprecated.
134      */
135 
136      static status_t getMinFrameCount(size_t* frameCount,
137                                       uint32_t sampleRate,
138                                       audio_format_t format,
139                                       audio_channel_mask_t channelMask);
140 
141     /* How data is transferred from AudioRecord
142      */
143     enum transfer_type {
144         TRANSFER_DEFAULT,   // not specified explicitly; determine from the other parameters
145         TRANSFER_CALLBACK,  // callback EVENT_MORE_DATA
146         TRANSFER_OBTAIN,    // call obtainBuffer() and releaseBuffer()
147         TRANSFER_SYNC,      // synchronous read()
148     };
149 
150     /* Constructs an uninitialized AudioRecord. No connection with
151      * AudioFlinger takes place.  Use set() after this.
152      *
153      * Parameters:
154      *
155      * client:          The attribution source of the owner of the record
156      */
157                         AudioRecord(const android::content::AttributionSourceState& client);
158 
159     /* Creates an AudioRecord object and registers it with AudioFlinger.
160      * Once created, the track needs to be started before it can be used.
161      * Unspecified values are set to appropriate default values.
162      *
163      * Parameters:
164      *
165      * inputSource:        Select the audio input to record from (e.g. AUDIO_SOURCE_DEFAULT).
166      * sampleRate:         Data sink sampling rate in Hz.  Zero means to use the source sample rate.
167      * format:             Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
168      *                     16 bits per sample).
169      * channelMask:        Channel mask, such that audio_is_input_channel(channelMask) is true.
170      * client:             The attribution source of the owner of the record
171      * frameCount:         Minimum size of track PCM buffer in frames. This defines the
172      *                     application's contribution to the
173      *                     latency of the track.  The actual size selected by the AudioRecord could
174      *                     be larger if the requested size is not compatible with current audio HAL
175      *                     latency.  Zero means to use a default value.
176      * cbf:                Callback function. If not null, this function is called periodically
177      *                     to consume new data in TRANSFER_CALLBACK mode
178      *                     and inform of marker, position updates, etc.
179      * user:               Context for use by the callback receiver.
180      * notificationFrames: The callback function is called each time notificationFrames PCM
181      *                     frames are ready in record track output buffer.
182      * sessionId:          Not yet supported.
183      * transferType:       How data is transferred from AudioRecord.
184      * flags:              See comments on audio_input_flags_t in <system/audio.h>
185      * pAttributes:        If not NULL, supersedes inputSource for use case selection.
186      * threadCanCallJava:  Not present in parameter list, and so is fixed at false.
187      */
188                         AudioRecord(audio_source_t inputSource,
189                                     uint32_t sampleRate,
190                                     audio_format_t format,
191                                     audio_channel_mask_t channelMask,
192                                     const android::content::AttributionSourceState& client,
193                                     size_t frameCount = 0,
194                                     const wp<IAudioRecordCallback> &callback = nullptr,
195                                     uint32_t notificationFrames = 0,
196                                     audio_session_t sessionId = AUDIO_SESSION_ALLOCATE,
197                                     transfer_type transferType = TRANSFER_DEFAULT,
198                                     audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
199                                     const audio_attributes_t* pAttributes = nullptr,
200                                     audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
201                                     audio_microphone_direction_t
202                                         selectedMicDirection = MIC_DIRECTION_UNSPECIFIED,
203                                     float selectedMicFieldDimension = MIC_FIELD_DIMENSION_DEFAULT);
204 
205 
206     /* Terminates the AudioRecord and unregisters it from AudioFlinger.
207      * Also destroys all resources associated with the AudioRecord.
208      */
209 protected:
210                         virtual ~AudioRecord();
211 public:
212 
213     /* Initialize an AudioRecord that was created using the AudioRecord() constructor.
214      * Don't call set() more than once, or after an AudioRecord() constructor that takes parameters.
215      * set() is not multi-thread safe.
216      * Returned status (from utils/Errors.h) can be:
217      *  - NO_ERROR: successful intialization
218      *  - INVALID_OPERATION: AudioRecord is already initialized or record device is already in use
219      *  - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
220      *  - NO_INIT: audio server or audio hardware not initialized
221      *  - PERMISSION_DENIED: recording is not allowed for the requesting process
222      * If status is not equal to NO_ERROR, don't call any other APIs on this AudioRecord.
223      *
224      * Parameters not listed in the AudioRecord constructors above:
225      *
226      * threadCanCallJava:  Whether callbacks are made from an attached thread and thus can call JNI.
227      */
228            status_t    set(audio_source_t inputSource,
229                             uint32_t sampleRate,
230                             audio_format_t format,
231                             audio_channel_mask_t channelMask,
232                             size_t frameCount = 0,
233                             const wp<IAudioRecordCallback> &callback = nullptr,
234                             uint32_t notificationFrames = 0,
235                             bool threadCanCallJava = false,
236                             audio_session_t sessionId = AUDIO_SESSION_ALLOCATE,
237                             transfer_type transferType = TRANSFER_DEFAULT,
238                             audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE,
239                             uid_t uid = AUDIO_UID_INVALID,
240                             pid_t pid = -1,
241                             const audio_attributes_t* pAttributes = nullptr,
242                             audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
243                             audio_microphone_direction_t
244                                 selectedMicDirection = MIC_DIRECTION_UNSPECIFIED,
245                             float selectedMicFieldDimension = MIC_FIELD_DIMENSION_DEFAULT,
246                             int32_t maxSharedAudioHistoryMs = 0);
247 
248     /* Result of constructing the AudioRecord. This must be checked for successful initialization
249      * before using any AudioRecord API (except for set()), because using
250      * an uninitialized AudioRecord produces undefined results.
251      * See set() method above for possible return codes.
252      */
initCheck()253             status_t    initCheck() const   { return mStatus; }
254 
255     /* Returns this track's estimated latency in milliseconds.
256      * This includes the latency due to AudioRecord buffer size, resampling if applicable,
257      * and audio hardware driver.
258      */
latency()259             uint32_t    latency() const     { return mLatency; }
260 
261    /* getters, see constructor and set() */
262 
format()263             audio_format_t format() const   { return mFormat; }
channelCount()264             uint32_t    channelCount() const    { return mChannelCount; }
frameCount()265             size_t      frameCount() const  { return mFrameCount; }
frameSize()266             size_t      frameSize() const   { return mFrameSize; }
inputSource()267             audio_source_t inputSource() const  { return mAttributes.source; }
channelMask()268             audio_channel_mask_t channelMask() const { return mChannelMask; }
269 
270     /*
271      * Return the period of the notification callback in frames.
272      * This value is set when the AudioRecord is constructed.
273      * It can be modified if the AudioRecord is rerouted.
274      */
getNotificationPeriodInFrames()275             uint32_t    getNotificationPeriodInFrames() const { return mNotificationFramesAct; }
276 
277     /*
278      * return metrics information for the current instance.
279      */
280             status_t getMetrics(mediametrics::Item * &item);
281 
282     /*
283      * Set name of API that is using this object.
284      * For example "aaudio" or "opensles".
285      * This may be logged or reported as part of MediaMetrics.
286      */
setCallerName(const std::string & name)287             void setCallerName(const std::string &name) {
288                 mCallerName = name;
289             }
290 
getCallerName()291             std::string getCallerName() const {
292                 return mCallerName;
293             };
294 
295     /* After it's created the track is not active. Call start() to
296      * make it active. If set, the callback will start being called.
297      * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until
298      * the specified event occurs on the specified trigger session.
299      */
300             status_t    start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE,
301                               audio_session_t triggerSession = AUDIO_SESSION_NONE);
302 
303     /* Stop a track.  The callback will cease being called.  Note that obtainBuffer() still
304      * works and will drain buffers until the pool is exhausted, and then will return WOULD_BLOCK.
305      */
306             void        stop();
307             bool        stopped() const;
308 
309     /* Calls stop() and then wait for all of the callbacks to return.
310      * It is safe to call this if stop() or pause() has already been called.
311      *
312      * This function is called from the destructor. But since AudioRecord
313      * is ref counted, the destructor may be called later than desired.
314      * This can be called explicitly as part of closing an AudioRecord
315      * if you want to be certain that callbacks have completely finished.
316      *
317      * This is not thread safe and should only be called from one thread,
318      * ideally as the AudioRecord is being closed.
319      */
320             void        stopAndJoinCallbacks();
321 
322     /* Return the sink sample rate for this record track in Hz.
323      * If specified as zero in constructor or set(), this will be the source sample rate.
324      * Unlike AudioTrack, the sample rate is const after initialization, so doesn't need a lock.
325      */
getSampleRate()326             uint32_t    getSampleRate() const   { return mSampleRate; }
327 
328     /* Return the sample rate from the AudioFlinger input thread. */
329             uint32_t    getHalSampleRate() const;
330 
331     /* Return the channel count from the AudioFlinger input thread. */
332             uint32_t    getHalChannelCount() const;
333 
334     /* Return the HAL format from the AudioFlinger input thread. */
335             audio_format_t    getHalFormat() const;
336 
337     /* Sets marker position. When record reaches the number of frames specified,
338      * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition
339      * with marker == 0 cancels marker notification callback.
340      * To set a marker at a position which would compute as 0,
341      * a workaround is to set the marker at a nearby position such as ~0 or 1.
342      * If the AudioRecord has been opened with no callback function associated,
343      * the operation will fail.
344      *
345      * Parameters:
346      *
347      * marker:   marker position expressed in wrapping (overflow) frame units,
348      *           like the return value of getPosition().
349      *
350      * Returned status (from utils/Errors.h) can be:
351      *  - NO_ERROR: successful operation
352      *  - INVALID_OPERATION: the AudioRecord has no callback installed.
353      */
354             status_t    setMarkerPosition(uint32_t marker);
355             status_t    getMarkerPosition(uint32_t *marker) const;
356 
357     /* Sets position update period. Every time the number of frames specified has been recorded,
358      * a callback with event type EVENT_NEW_POS is called.
359      * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
360      * callback.
361      * If the AudioRecord has been opened with no callback function associated,
362      * the operation will fail.
363      * Extremely small values may be rounded up to a value the implementation can support.
364      *
365      * Parameters:
366      *
367      * updatePeriod:  position update notification period expressed in frames.
368      *
369      * Returned status (from utils/Errors.h) can be:
370      *  - NO_ERROR: successful operation
371      *  - INVALID_OPERATION: the AudioRecord has no callback installed.
372      */
373             status_t    setPositionUpdatePeriod(uint32_t updatePeriod);
374             status_t    getPositionUpdatePeriod(uint32_t *updatePeriod) const;
375 
376     /* Return the total number of frames recorded since recording started.
377      * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
378      * It is reset to zero by stop().
379      *
380      * Parameters:
381      *
382      *  position:  Address where to return record head position.
383      *
384      * Returned status (from utils/Errors.h) can be:
385      *  - NO_ERROR: successful operation
386      *  - BAD_VALUE:  position is NULL
387      */
388             status_t    getPosition(uint32_t *position) const;
389 
390     /* Return the record timestamp.
391      *
392      * Parameters:
393      *  timestamp: A pointer to the timestamp to be filled.
394      *
395      * Returned status (from utils/Errors.h) can be:
396      *  - NO_ERROR: successful operation
397      *  - BAD_VALUE: timestamp is NULL
398      */
399             status_t getTimestamp(ExtendedTimestamp *timestamp);
400 
401     /**
402      * @param transferType
403      * @return text string that matches the enum name
404      */
405     static const char * convertTransferToText(transfer_type transferType);
406 
407     /* Returns a handle on the audio input used by this AudioRecord.
408      *
409      * Parameters:
410      *  none.
411      *
412      * Returned value:
413      *  handle on audio hardware input
414      */
415 // FIXME The only known public caller is frameworks/opt/net/voip/src/jni/rtp/AudioGroup.cpp
getInput()416             audio_io_handle_t    getInput() const __attribute__((__deprecated__))
417                                                 { return getInputPrivate(); }
418 private:
419             audio_io_handle_t    getInputPrivate() const;
420 public:
421 
422     /* Returns the audio session ID associated with this AudioRecord.
423      *
424      * Parameters:
425      *  none.
426      *
427      * Returned value:
428      *  AudioRecord session ID.
429      *
430      * No lock needed because session ID doesn't change after first set().
431      */
getSessionId()432             audio_session_t getSessionId() const { return mSessionId; }
433 
434     /* Public API for TRANSFER_OBTAIN mode.
435      * Obtains a buffer of up to "audioBuffer->frameCount" full frames.
436      * After draining these frames of data, the caller should release them with releaseBuffer().
437      * If the track buffer is not empty, obtainBuffer() returns as many contiguous
438      * full frames as are available immediately.
439      *
440      * If nonContig is non-NULL, it is an output parameter that will be set to the number of
441      * additional non-contiguous frames that are predicted to be available immediately,
442      * if the client were to release the first frames and then call obtainBuffer() again.
443      * This value is only a prediction, and needs to be confirmed.
444      * It will be set to zero for an error return.
445      *
446      * If the track buffer is empty and track is stopped, obtainBuffer() returns WOULD_BLOCK
447      * regardless of the value of waitCount.
448      * If the track buffer is empty and track is not stopped, obtainBuffer() blocks with a
449      * maximum timeout based on waitCount; see chart below.
450      * Buffers will be returned until the pool
451      * is exhausted, at which point obtainBuffer() will either block
452      * or return WOULD_BLOCK depending on the value of the "waitCount"
453      * parameter.
454      *
455      * Interpretation of waitCount:
456      *  +n  limits wait time to n * WAIT_PERIOD_MS,
457      *  -1  causes an (almost) infinite wait time,
458      *   0  non-blocking.
459      *
460      * Buffer fields
461      * On entry:
462      *  frameCount  number of frames requested
463      *  size        ignored
464      *  raw         ignored
465      *  sequence    ignored
466      * After error return:
467      *  frameCount  0
468      *  size        0
469      *  raw         undefined
470      *  sequence    undefined
471      * After successful return:
472      *  frameCount  actual number of frames available, <= number requested
473      *  size        actual number of bytes available
474      *  raw         pointer to the buffer
475      *  sequence    IAudioRecord instance sequence number, as of obtainBuffer()
476      */
477 
478             status_t    obtainBuffer(Buffer* audioBuffer, int32_t waitCount,
479                                 size_t *nonContig = NULL);
480 
481             // Explicit Routing
482     /**
483      * TODO Document this method.
484      */
485             status_t setInputDevice(audio_port_handle_t deviceId);
486 
487     /**
488      * TODO Document this method.
489      */
490             audio_port_handle_t getInputDevice();
491 
492      /* Returns the ID of the audio device actually used by the input to which this AudioRecord
493       * is attached.
494       * The device ID is relevant only if the AudioRecord is active.
495       * When the AudioRecord is inactive, the device ID returned can be either:
496       * - AUDIO_PORT_HANDLE_NONE if the AudioRecord is not attached to any output.
497       * - The device ID used before paused or stopped.
498       * - The device ID selected by audio policy manager of setOutputDevice() if the AudioRecord
499       * has not been started yet.
500       *
501       * Parameters:
502       *  none.
503       */
504      audio_port_handle_t getRoutedDeviceId();
505 
506     /* Add an AudioDeviceCallback. The caller will be notified when the audio device
507      * to which this AudioRecord is routed is updated.
508      * Replaces any previously installed callback.
509      * Parameters:
510      *  callback:  The callback interface
511      * Returns NO_ERROR if successful.
512      *         INVALID_OPERATION if the same callback is already installed.
513      *         NO_INIT or PREMISSION_DENIED if AudioFlinger service is not reachable
514      *         BAD_VALUE if the callback is NULL
515      */
516             status_t addAudioDeviceCallback(
517                     const sp<AudioSystem::AudioDeviceCallback>& callback);
518 
519     /* remove an AudioDeviceCallback.
520      * Parameters:
521      *  callback:  The callback interface
522      * Returns NO_ERROR if successful.
523      *         INVALID_OPERATION if the callback is not installed
524      *         BAD_VALUE if the callback is NULL
525      */
526             status_t removeAudioDeviceCallback(
527                     const sp<AudioSystem::AudioDeviceCallback>& callback);
528 
529             // AudioSystem::AudioDeviceCallback> virtuals
530             virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo,
531                                              audio_port_handle_t deviceId);
532 
533 private:
534     /* If nonContig is non-NULL, it is an output parameter that will be set to the number of
535      * additional non-contiguous frames that are predicted to be available immediately,
536      * if the client were to release the first frames and then call obtainBuffer() again.
537      * This value is only a prediction, and needs to be confirmed.
538      * It will be set to zero for an error return.
539      * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(),
540      * in case the requested amount of frames is in two or more non-contiguous regions.
541      * FIXME requested and elapsed are both relative times.  Consider changing to absolute time.
542      */
543             status_t    obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
544                                      struct timespec *elapsed = NULL, size_t *nonContig = NULL);
545 public:
546 
547     /* Public API for TRANSFER_OBTAIN mode.
548      * Release an emptied buffer of "audioBuffer->frameCount" frames for AudioFlinger to re-fill.
549      *
550      * Buffer fields:
551      *  frameCount  currently ignored but recommend to set to actual number of frames consumed
552      *  size        actual number of bytes consumed, must be multiple of frameSize
553      *  raw         ignored
554      */
555             void        releaseBuffer(const Buffer* audioBuffer);
556 
557     /* As a convenience we provide a read() interface to the audio buffer.
558      * Input parameter 'size' is in byte units.
559      * This is implemented on top of obtainBuffer/releaseBuffer. For best
560      * performance use callbacks. Returns actual number of bytes read >= 0,
561      * or one of the following negative status codes:
562      *      INVALID_OPERATION   AudioRecord is configured for streaming mode
563      *      BAD_VALUE           size is invalid
564      *      WOULD_BLOCK         when obtainBuffer() returns same, or
565      *                          AudioRecord was stopped during the read
566      *      or any other error code returned by IAudioRecord::start() or restoreRecord_l().
567      * Default behavior is to only return when all data has been transferred. Set 'blocking' to
568      * false for the method to return immediately without waiting to try multiple times to read
569      * the full content of the buffer.
570      */
571             ssize_t     read(void* buffer, size_t size, bool blocking = true);
572 
573     /* Return the number of input frames lost in the audio driver since the last call of this
574      * function.  Audio driver is expected to reset the value to 0 and restart counting upon
575      * returning the current value by this function call.  Such loss typically occurs when the
576      * user space process is blocked longer than the capacity of audio driver buffers.
577      * Units: the number of input audio frames.
578      * FIXME The side-effect of resetting the counter may be incompatible with multi-client.
579      * Consider making it more like AudioTrack::getUnderrunFrames which doesn't have side effects.
580      */
581             uint32_t    getInputFramesLost() const;
582 
583     /* Get the flags */
getFlags()584             audio_input_flags_t getFlags() const { AutoMutex _l(mLock); return mFlags; }
585 
586     /* Get active microphones. A empty vector of MicrophoneInfoFw will be passed as a parameter,
587      * the data will be filled when querying the hal.
588      */
589             status_t    getActiveMicrophones(
590                     std::vector<media::MicrophoneInfoFw>* activeMicrophones);
591 
592     /* Set the Microphone direction (for processing purposes).
593      */
594             status_t    setPreferredMicrophoneDirection(audio_microphone_direction_t direction);
595 
596     /* Set the Microphone zoom factor (for processing purposes).
597      */
598             status_t    setPreferredMicrophoneFieldDimension(float zoom);
599 
600      /* Get the unique port ID assigned to this AudioRecord instance by audio policy manager.
601       * The ID is unique across all audioserver clients and can change during the life cycle
602       * of a given AudioRecord instance if the connection to audioserver is restored.
603       */
getPortId()604             audio_port_handle_t getPortId() const { return mPortId; };
605 
606     /* Sets the LogSessionId field which is used for metrics association of
607      * this object with other objects. A nullptr or empty string clears
608      * the logSessionId.
609      */
610             void setLogSessionId(const char *logSessionId);
611 
612 
613             status_t shareAudioHistory(const std::string& sharedPackageName,
614                                        int64_t sharedStartMs);
615 
616      /*
617       * Dumps the state of an audio record.
618       */
619             status_t    dump(int fd, const Vector<String16>& args) const;
620 
621 private:
622     /* copying audio record objects is not allowed */
623                         AudioRecord(const AudioRecord& other);
624             AudioRecord& operator = (const AudioRecord& other);
625 
626     /* a small internal class to handle the callback */
627     class AudioRecordThread : public Thread
628     {
629     public:
630         AudioRecordThread(AudioRecord& receiver);
631 
632         // Do not call Thread::requestExitAndWait() without first calling requestExit().
633         // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
634         virtual void        requestExit();
635 
636                 void        pause();    // suspend thread from execution at next loop boundary
637                 void        resume();   // allow thread to execute, if not requested to exit
638                 void        wake();     // wake to handle changed notification conditions.
639 
640     private:
641                 void        pauseInternal(nsecs_t ns = 0LL);
642                                         // like pause(), but only used internally within thread
643 
644         friend class AudioRecord;
645         virtual bool        threadLoop();
646         AudioRecord&        mReceiver;
647         virtual ~AudioRecordThread();
648         Mutex               mMyLock;    // Thread::mLock is private
649         Condition           mMyCond;    // Thread::mThreadExitedCondition is private
650         bool                mPaused;    // whether thread is requested to pause at next loop entry
651         bool                mPausedInt; // whether thread internally requests pause
652         nsecs_t             mPausedNs;  // if mPausedInt then associated timeout, otherwise ignored
653         bool                mIgnoreNextPausedInt;   // skip any internal pause and go immediately
654                                         // to processAudioBuffer() as state may have changed
655                                         // since pause time calculated.
656     };
657 
658             // body of AudioRecordThread::threadLoop()
659             // returns the maximum amount of time before we would like to run again, where:
660             //      0           immediately
661             //      > 0         no later than this many nanoseconds from now
662             //      NS_WHENEVER still active but no particular deadline
663             //      NS_INACTIVE inactive so don't run again until re-started
664             //      NS_NEVER    never again
665             static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3;
666             nsecs_t processAudioBuffer();
667 
668             // caller must hold lock on mLock for all _l methods
669 
670             status_t createRecord_l(const Modulo<uint32_t> &epoch);
671 
672             // FIXME enum is faster than strcmp() for parameter 'from'
673             status_t restoreRecord_l(const char *from);
674 
675             void     updateRoutedDeviceId_l();
676 
677     sp<AudioRecordThread>   mAudioRecordThread;
678     mutable Mutex           mLock;
679 
680     std::unique_ptr<RecordingActivityTracker> mTracker;
681 
682     // Current client state:  false = stopped, true = active.  Protected by mLock.  If more states
683     // are added, consider changing this to enum State { ... } mState as in AudioTrack.
684     bool mActive = false;
685 
686     // for client callback handler
687 
688     wp<IAudioRecordCallback> mCallback;
689     sp<IAudioRecordCallback> mLegacyCallbackWrapper;
690 
691     bool                    mInitialized = false;   // Protect against double set
692     // for notification APIs
693     uint32_t                mNotificationFramesReq; // requested number of frames between each
694                                                     // notification callback
695                                                     // as specified in constructor or set()
696     uint32_t                mNotificationFramesAct; // actual number of frames between each
697                                                     // notification callback
698     bool                    mRefreshRemaining;      // processAudioBuffer() should refresh
699                                                     // mRemainingFrames and mRetryOnPartialBuffer
700 
701     // These are private to processAudioBuffer(), and are not protected by a lock
702     uint32_t                mRemainingFrames;       // number of frames to request in obtainBuffer()
703     bool                    mRetryOnPartialBuffer;  // sleep and retry after partial obtainBuffer()
704     uint32_t                mObservedSequence;      // last observed value of mSequence
705 
706     Modulo<uint32_t>        mMarkerPosition;        // in wrapping (overflow) frame units
707     bool                    mMarkerReached;
708     Modulo<uint32_t>        mNewPosition;           // in frames
709     uint32_t                mUpdatePeriod;          // in frames, zero means no EVENT_NEW_POS
710 
711     status_t mStatus = NO_INIT;
712 
713     android::content::AttributionSourceState mClientAttributionSource; // Owner's attribution source
714 
715     size_t                  mFrameCount;            // corresponds to current IAudioRecord, value is
716                                                     // reported back by AudioFlinger to the client
717     size_t                  mReqFrameCount;         // frame count to request the first or next time
718                                                     // a new IAudioRecord is needed, non-decreasing
719 
720     int64_t                 mFramesRead;            // total frames read. reset to zero after
721                                                     // the start() following stop(). It is not
722                                                     // changed after restoring the track.
723     int64_t                 mFramesReadServerOffset; // An offset to server frames read due to
724                                                     // restoring AudioRecord, or stop/start.
725     // constant after constructor or set()
726     uint32_t                mSampleRate;
727     audio_format_t          mFormat;
728     uint32_t                mChannelCount;
729     size_t                  mFrameSize;         // app-level frame size == AudioFlinger frame size
730     uint32_t                mLatency;           // in ms
731     audio_channel_mask_t    mChannelMask;
732 
733     audio_input_flags_t     mFlags;                 // same as mOrigFlags, except for bits that may
734                                                     // be denied by client or server, such as
735                                                     // AUDIO_INPUT_FLAG_FAST.  mLock must be
736                                                     // held to read or write those bits reliably.
737     audio_input_flags_t     mOrigFlags;             // as specified in constructor or set(), const
738 
739     audio_session_t mSessionId = AUDIO_SESSION_ALLOCATE;
740     audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE;
741 
742     /**
743      * mLogSessionId is a string identifying this AudioRecord for the metrics service.
744      * It may be unique or shared with other objects.  An empty string means the
745      * logSessionId is not set.
746      */
747     std::string             mLogSessionId{};
748 
749     transfer_type           mTransfer;
750 
751     // Next 5 fields may be changed if IAudioRecord is re-created, but always != 0
752     // provided the initial set() was successful
753     sp<media::IAudioRecord> mAudioRecord;
754     sp<IMemory>             mCblkMemory;
755     audio_track_cblk_t*     mCblk;              // re-load after mLock.unlock()
756     sp<IMemory>             mBufferMemory;
757     audio_io_handle_t       mInput = AUDIO_IO_HANDLE_NONE; // from AudioSystem::getInputforAttr()
758 
759     int mPreviousPriority = ANDROID_PRIORITY_NORMAL;  // before start()
760     SchedPolicy mPreviousSchedulingGroup = SP_DEFAULT;
761     bool mAwaitBoost = false;  // thread should wait for priority boost before running
762 
763     // The proxy should only be referenced while a lock is held because the proxy isn't
764     // multi-thread safe.
765     // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock,
766     // provided that the caller also holds an extra reference to the proxy and shared memory to keep
767     // them around in case they are replaced during the obtainBuffer().
768     sp<AudioRecordClientProxy> mProxy;
769 
770     bool                    mInOverrun;         // whether recorder is currently in overrun state
771 
772     ExtendedTimestamp       mPreviousTimestamp{}; // used to detect retrograde motion
773     bool                    mTimestampRetrogradePositionReported = false; // reduce log spam
774     bool                    mTimestampRetrogradeTimeReported = false;     // reduce log spam
775 
776     // Format conversion. Maybe needed for adding fast tracks whose format is different from server.
777     audio_config_base_t     mServerConfig;
778     size_t                  mServerFrameSize;
779     size_t                  mServerSampleSize;
780     std::unique_ptr<uint8_t[]> mFormatConversionBufRaw;
781     Buffer                  mFormatConversionBuffer;
782     uint32_t                mHalSampleRate;          // AudioFlinger thread sample rate
783     uint32_t                mHalChannelCount;        // AudioFlinger thread channel count
784     audio_format_t          mHalFormat;              // AudioFlinger thread format
785 
786 private:
787     class DeathNotifier : public IBinder::DeathRecipient {
788     public:
DeathNotifier(AudioRecord * audioRecord)789         DeathNotifier(AudioRecord* audioRecord) : mAudioRecord(audioRecord) { }
790     protected:
791         virtual void        binderDied(const wp<IBinder>& who);
792     private:
793         const wp<AudioRecord> mAudioRecord;
794     };
795 
796     sp<DeathNotifier>       mDeathNotifier;
797     uint32_t                mSequence;              // incremented for each new IAudioRecord attempt
798     audio_attributes_t      mAttributes;
799 
800     // For Device Selection API
801     //  a value of AUDIO_PORT_HANDLE_NONE indicated default (AudioPolicyManager) routing.
802 
803     // Device requested by the application.
804     audio_port_handle_t     mSelectedDeviceId = AUDIO_PORT_HANDLE_NONE;
805     // Device actually selected by AudioPolicyManager: This may not match the app
806     // selection depending on other activity and connected devices
807     audio_port_handle_t     mRoutedDeviceId = AUDIO_PORT_HANDLE_NONE;
808 
809     wp<AudioSystem::AudioDeviceCallback> mDeviceCallback;
810 
811     audio_microphone_direction_t mSelectedMicDirection = MIC_DIRECTION_UNSPECIFIED;
812     float mSelectedMicFieldDimension = MIC_FIELD_DIMENSION_DEFAULT;
813 
814     int32_t                    mMaxSharedAudioHistoryMs = 0;
815     std::string                mSharedAudioPackageName = {};
816     int64_t                    mSharedAudioStartMs = 0;
817 
818 private:
819     class MediaMetrics {
820       public:
MediaMetrics()821         MediaMetrics() : mMetricsItem(mediametrics::Item::create("audiorecord")),
822                          mCreatedNs(systemTime(SYSTEM_TIME_REALTIME)),
823                          mStartedNs(0), mDurationNs(0), mCount(0),
824                          mLastError(NO_ERROR) {
825         }
~MediaMetrics()826         ~MediaMetrics() {
827             // mMetricsItem alloc failure will be flagged in the constructor
828             // don't log empty records
829             if (mMetricsItem->count() > 0) {
830                 mMetricsItem->selfrecord();
831             }
832         }
833         void gather(const AudioRecord *record);
dup()834         mediametrics::Item *dup() { return mMetricsItem->dup(); }
835 
logStart(nsecs_t when)836         void logStart(nsecs_t when) { mStartedNs = when; mCount++; }
logStop(nsecs_t when)837         void logStop(nsecs_t when) { mDurationNs += (when-mStartedNs); mStartedNs = 0;}
markError(status_t errcode,const char * func)838         void markError(status_t errcode, const char *func)
839                  { mLastError = errcode; mLastErrorFunc = func;}
840       private:
841         std::unique_ptr<mediametrics::Item> mMetricsItem;
842         nsecs_t mCreatedNs;     // XXX: perhaps not worth it in production
843         nsecs_t mStartedNs;
844         nsecs_t mDurationNs;
845         int32_t mCount;
846 
847         status_t mLastError;
848         std::string mLastErrorFunc;
849     };
850     MediaMetrics mMediaMetrics;
851     std::string mMetricsId;  // GUARDED_BY(mLock), could change in createRecord_l().
852     std::string mCallerName; // for example "aaudio"
853 
854     void reportError(status_t status, const char *event, const char *message) const;
855 };
856 
857 }; // namespace android
858 
859 #endif // ANDROID_AUDIORECORD_H
860