| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 AUDIORECORD_H_ |
| 18 | #define AUDIORECORD_H_ |
| 19 | |
| Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 20 | #include <binder/IMemory.h> |
| Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 21 | #include <cutils/sched_policy.h> |
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 22 | #include <media/AudioSystem.h> |
| Glenn Kasten | 868a6a3 | 2012-06-21 16:22:09 -0700 | [diff] [blame] | 23 | #include <media/IAudioRecord.h> |
| 24 | #include <system/audio.h> |
| 25 | #include <utils/RefBase.h> |
| 26 | #include <utils/Errors.h> |
| 27 | #include <utils/threads.h> |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| Glenn Kasten | a3f1fa3 | 2012-01-18 14:54:46 -0800 | [diff] [blame] | 31 | class audio_track_cblk_t; |
| 32 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| Eric Laurent | a0a98ca | 2012-04-20 16:40:28 -0700 | [diff] [blame] | 35 | class AudioRecord : virtual public RefBase |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | static const int DEFAULT_SAMPLE_RATE = 8000; |
| 40 | |
| 41 | /* Events used by AudioRecord callback function (callback_t). |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 42 | * Keep in sync with frameworks/base/media/java/android/media/AudioRecord.java NATIVE_EVENT_*. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | */ |
| 44 | enum event_type { |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 45 | EVENT_MORE_DATA = 0, // Request to read more data from PCM buffer. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 46 | EVENT_OVERRUN = 1, // PCM buffer overrun occurred. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | EVENT_MARKER = 2, // Record head is at the specified marker position |
| 48 | // (See setMarkerPosition()). |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 49 | EVENT_NEW_POS = 3, // Record head is at a new position |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | // (See setPositionUpdatePeriod()). |
| 51 | }; |
| 52 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 53 | /* Client should declare Buffer on the stack and pass address to obtainBuffer() |
| 54 | * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | class Buffer |
| 58 | { |
| 59 | public: |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 60 | size_t frameCount; // number of sample frames corresponding to size; |
| 61 | // on input it is the number of frames available, |
| 62 | // on output is the number of frames actually drained |
| 63 | |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 64 | size_t size; // total size in bytes == frameCount * frameSize |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | union { |
| 66 | void* raw; |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 67 | short* i16; // signed 16-bit |
| 68 | int8_t* i8; // unsigned 8-bit, offset by 0x80 |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | }; |
| 70 | }; |
| 71 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | /* As a convenience, if a callback is supplied, a handler thread |
| 73 | * is automatically created with the appropriate priority. This thread |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 74 | * invokes the callback when a new buffer becomes ready or various conditions occur. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * Parameters: |
| 76 | * |
| 77 | * event: type of event notified (see enum AudioRecord::event_type). |
| 78 | * user: Pointer to context for use by the callback receiver. |
| 79 | * info: Pointer to optional parameter according to event type: |
| 80 | * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 81 | * more bytes than indicated by 'size' field and update 'size' if fewer bytes are |
| 82 | * consumed. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * - EVENT_OVERRUN: unused. |
| Glenn Kasten | 955e781 | 2012-02-21 10:32:45 -0800 | [diff] [blame] | 84 | * - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames. |
| 85 | * - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | */ |
| 87 | |
| 88 | typedef void (*callback_t)(int event, void* user, void *info); |
| 89 | |
| Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 90 | /* Returns the minimum frame count required for the successful creation of |
| 91 | * an AudioRecord object. |
| 92 | * Returned status (from utils/Errors.h) can be: |
| 93 | * - NO_ERROR: successful operation |
| 94 | * - NO_INIT: audio server or audio hardware not initialized |
| 95 | * - BAD_VALUE: unsupported configuration |
| 96 | */ |
| 97 | |
| 98 | static status_t getMinFrameCount(int* frameCount, |
| 99 | uint32_t sampleRate, |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 100 | audio_format_t format, |
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 101 | audio_channel_mask_t channelMask); |
| Chia-chi Yeh | 15304d6 | 2010-06-22 08:01:13 +0800 | [diff] [blame] | 102 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | /* Constructs an uninitialized AudioRecord. No connection with |
| 104 | * AudioFlinger takes place. |
| 105 | */ |
| 106 | AudioRecord(); |
| 107 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 108 | /* Creates an AudioRecord object and registers it with AudioFlinger. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * Once created, the track needs to be started before it can be used. |
| 110 | * Unspecified values are set to the audio hardware's current |
| 111 | * values. |
| 112 | * |
| 113 | * Parameters: |
| 114 | * |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 115 | * inputSource: Select the audio input to record to (e.g. AUDIO_SOURCE_DEFAULT). |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * sampleRate: Track sampling rate in Hz. |
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 117 | * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * 16 bits per sample). |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 119 | * channelMask: Channel mask. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 120 | * frameCount: Minimum size of track PCM buffer in frames. This defines the |
| 121 | * application's contribution to the |
| 122 | * latency of the track. The actual size selected by the AudioRecord could |
| 123 | * be larger if the requested size is not compatible with current audio HAL |
| 124 | * latency. Zero means to use a default value. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | * cbf: Callback function. If not null, this function is called periodically |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 126 | * to consume new PCM data. |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 127 | * user: Context for use by the callback receiver. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | * notificationFrames: The callback function is called each time notificationFrames PCM |
| 129 | * frames are ready in record track output buffer. |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 130 | * sessionId: Not yet supported. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | */ |
| 132 | |
| Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 133 | AudioRecord(audio_source_t inputSource, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | uint32_t sampleRate = 0, |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 135 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
| Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 136 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | int frameCount = 0, |
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 138 | callback_t cbf = NULL, |
| 139 | void* user = NULL, |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 140 | int notificationFrames = 0, |
| 141 | int sessionId = 0); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| 143 | |
| 144 | /* Terminates the AudioRecord and unregisters it from AudioFlinger. |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 145 | * Also destroys all resources associated with the AudioRecord. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | */ |
| 147 | ~AudioRecord(); |
| 148 | |
| 149 | |
| 150 | /* Initialize an uninitialized AudioRecord. |
| 151 | * Returned status (from utils/Errors.h) can be: |
| 152 | * - NO_ERROR: successful intialization |
| 153 | * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 154 | * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | * - NO_INIT: audio server or audio hardware not initialized |
| 156 | * - PERMISSION_DENIED: recording is not allowed for the requesting process |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 157 | */ |
| Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 158 | status_t set(audio_source_t inputSource = AUDIO_SOURCE_DEFAULT, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | uint32_t sampleRate = 0, |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 160 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
| Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 161 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | int frameCount = 0, |
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 163 | callback_t cbf = NULL, |
| 164 | void* user = NULL, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | int notificationFrames = 0, |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 166 | bool threadCanCallJava = false, |
| 167 | int sessionId = 0); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
| 169 | |
| 170 | /* Result of constructing the AudioRecord. This must be checked |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 171 | * before using any AudioRecord API (except for set()), because using |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | * an uninitialized AudioRecord produces undefined results. |
| 173 | * See set() method above for possible return codes. |
| 174 | */ |
| 175 | status_t initCheck() const; |
| 176 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 177 | /* Returns this track's estimated latency in milliseconds. |
| 178 | * This includes the latency due to AudioRecord buffer size, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | * and audio hardware driver. |
| 180 | */ |
| 181 | uint32_t latency() const; |
| 182 | |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 183 | /* getters, see constructor and set() */ |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 185 | audio_format_t format() const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | int channelCount() const; |
| 187 | uint32_t frameCount() const; |
| Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 188 | size_t frameSize() const; |
| Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 189 | audio_source_t inputSource() const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | |
| 192 | /* After it's created the track is not active. Call start() to |
| 193 | * make it active. If set, the callback will start being called. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 194 | * If event is not AudioSystem::SYNC_EVENT_NONE, the capture start will be delayed until |
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 195 | * the specified event occurs on the specified trigger session. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | */ |
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 197 | status_t start(AudioSystem::sync_event_t event = AudioSystem::SYNC_EVENT_NONE, |
| 198 | int triggerSession = 0); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | |
| 200 | /* Stop a track. If set, the callback will cease being called and |
| 201 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 202 | * and will drain buffers until the pool is exhausted. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | */ |
| Glenn Kasten | d64cd23 | 2012-02-21 10:21:23 -0800 | [diff] [blame] | 204 | void stop(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | bool stopped() const; |
| 206 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 207 | /* Get sample rate for this record track in Hz. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | */ |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 209 | uint32_t getSampleRate() const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
| 211 | /* Sets marker position. When record reaches the number of frames specified, |
| 212 | * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | * with marker == 0 cancels marker notification callback. |
| 214 | * If the AudioRecord has been opened with no callback function associated, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | * the operation will fail. |
| 216 | * |
| 217 | * Parameters: |
| 218 | * |
| 219 | * marker: marker position expressed in frames. |
| 220 | * |
| 221 | * Returned status (from utils/Errors.h) can be: |
| 222 | * - NO_ERROR: successful operation |
| 223 | * - INVALID_OPERATION: the AudioRecord has no callback installed. |
| 224 | */ |
| 225 | status_t setMarkerPosition(uint32_t marker); |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 226 | status_t getMarkerPosition(uint32_t *marker) const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
| 228 | |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 229 | /* Sets position update period. Every time the number of frames specified has been recorded, |
| 230 | * a callback with event type EVENT_NEW_POS is called. |
| 231 | * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification |
| 232 | * callback. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | * If the AudioRecord has been opened with no callback function associated, |
| 234 | * the operation will fail. |
| 235 | * |
| 236 | * Parameters: |
| 237 | * |
| 238 | * updatePeriod: position update notification period expressed in frames. |
| 239 | * |
| 240 | * Returned status (from utils/Errors.h) can be: |
| 241 | * - NO_ERROR: successful operation |
| 242 | * - INVALID_OPERATION: the AudioRecord has no callback installed. |
| 243 | */ |
| 244 | status_t setPositionUpdatePeriod(uint32_t updatePeriod); |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 245 | status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
| 247 | |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 248 | /* Gets record head position. The position is the total number of frames |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 249 | * recorded since record start. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * |
| 251 | * Parameters: |
| 252 | * |
| 253 | * position: Address where to return record head position within AudioRecord buffer. |
| 254 | * |
| 255 | * Returned status (from utils/Errors.h) can be: |
| 256 | * - NO_ERROR: successful operation |
| 257 | * - BAD_VALUE: position is NULL |
| 258 | */ |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 259 | status_t getPosition(uint32_t *position) const; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 261 | /* Returns a handle on the audio input used by this AudioRecord. |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 262 | * |
| 263 | * Parameters: |
| 264 | * none. |
| 265 | * |
| 266 | * Returned value: |
| 267 | * handle on audio hardware input |
| 268 | */ |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 269 | audio_io_handle_t getInput() const; |
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 270 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 271 | /* Returns the audio session ID associated with this AudioRecord. |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 272 | * |
| 273 | * Parameters: |
| 274 | * none. |
| 275 | * |
| 276 | * Returned value: |
| 277 | * AudioRecord session ID. |
| 278 | */ |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 279 | int getSessionId() const; |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 280 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 281 | /* Obtains a buffer of "frameCount" frames. The buffer must be |
| 282 | * drained entirely, and then released with releaseBuffer(). |
| 283 | * If the track is stopped, obtainBuffer() returns |
| Glenn Kasten | 417c273 | 2012-02-24 13:00:45 -0800 | [diff] [blame] | 284 | * STOPPED instead of NO_ERROR as long as there are buffers available, |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | * at which point NO_MORE_BUFFERS is returned. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 286 | * Buffers will be returned until the pool |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | * is exhausted, at which point obtainBuffer() will either block |
| 288 | * or return WOULD_BLOCK depending on the value of the "blocking" |
| 289 | * parameter. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 290 | * |
| 291 | * Interpretation of waitCount: |
| 292 | * +n limits wait time to n * WAIT_PERIOD_MS, |
| 293 | * -1 causes an (almost) infinite wait time, |
| 294 | * 0 non-blocking. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | */ |
| 296 | |
| 297 | enum { |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 298 | NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | STOPPED = 1 |
| 300 | }; |
| 301 | |
| 302 | status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 303 | |
| 304 | /* Release an emptied buffer of "frameCount" frames for AudioFlinger to re-fill. */ |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | void releaseBuffer(Buffer* audioBuffer); |
| 306 | |
| 307 | |
| 308 | /* As a convenience we provide a read() interface to the audio buffer. |
| Glenn Kasten | 417c273 | 2012-02-24 13:00:45 -0800 | [diff] [blame] | 309 | * This is implemented on top of obtainBuffer/releaseBuffer. |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | */ |
| 311 | ssize_t read(void* buffer, size_t size); |
| 312 | |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 313 | /* Return the number of input frames lost in the audio driver since the last call of this |
| Glenn Kasten | 417c273 | 2012-02-24 13:00:45 -0800 | [diff] [blame] | 314 | * function. Audio driver is expected to reset the value to 0 and restart counting upon |
| 315 | * returning the current value by this function call. Such loss typically occurs when the |
| 316 | * user space process is blocked longer than the capacity of audio driver buffers. |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 317 | * Units: the number of input audio frames. |
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 318 | */ |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 319 | unsigned int getInputFramesLost() const; |
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 320 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | private: |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 322 | /* copying audio record objects is not allowed */ |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | AudioRecord(const AudioRecord& other); |
| 324 | AudioRecord& operator = (const AudioRecord& other); |
| 325 | |
| 326 | /* a small internal class to handle the callback */ |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 327 | class AudioRecordThread : public Thread |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | { |
| 329 | public: |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 330 | AudioRecordThread(AudioRecord& receiver, bool bCanCallJava = false); |
| 331 | |
| 332 | // Do not call Thread::requestExitAndWait() without first calling requestExit(). |
| 333 | // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough. |
| 334 | virtual void requestExit(); |
| 335 | |
| 336 | void pause(); // suspend thread from execution at next loop boundary |
| 337 | void resume(); // allow thread to execute, if not requested to exit |
| 338 | |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | private: |
| 340 | friend class AudioRecord; |
| 341 | virtual bool threadLoop(); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | AudioRecord& mReceiver; |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 343 | virtual ~AudioRecordThread(); |
| 344 | Mutex mMyLock; // Thread::mLock is private |
| 345 | Condition mMyCond; // Thread::mThreadExitedCondition is private |
| 346 | bool mPaused; // whether thread is currently paused |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 349 | // body of AudioRecordThread::threadLoop() |
| 350 | bool processAudioBuffer(const sp<AudioRecordThread>& thread); |
| 351 | |
| Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 352 | status_t openRecord_l(uint32_t sampleRate, |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 353 | audio_format_t format, |
| Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 354 | audio_channel_mask_t channelMask, |
| Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 355 | int frameCount, |
| Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 356 | audio_io_handle_t input); |
| Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 357 | audio_io_handle_t getInput_l(); |
| 358 | status_t restoreRecord_l(audio_track_cblk_t*& cblk); |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 360 | sp<AudioRecordThread> mAudioRecordThread; |
| Glenn Kasten | 606ee61 | 2012-02-24 16:33:14 -0800 | [diff] [blame] | 361 | mutable Mutex mLock; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
| Glenn Kasten | 68337ed | 2012-07-12 09:05:58 -0700 | [diff] [blame] | 363 | bool mActive; // protected by mLock |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 365 | // for client callback handler |
| Glenn Kasten | c28c03b | 2012-11-01 15:41:48 -0700 | [diff] [blame] | 366 | callback_t mCbf; // callback handler for events, or NULL |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 367 | void* mUserData; |
| 368 | |
| 369 | // for notification APIs |
| 370 | uint32_t mNotificationFrames; |
| 371 | uint32_t mRemainingFrames; |
| 372 | uint32_t mMarkerPosition; // in frames |
| 373 | bool mMarkerReached; |
| 374 | uint32_t mNewPosition; // in frames |
| 375 | uint32_t mUpdatePeriod; // in ms |
| 376 | |
| 377 | // constant after constructor or set() |
| 378 | uint32_t mFrameCount; |
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 379 | audio_format_t mFormat; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | uint8_t mChannelCount; |
| Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 381 | audio_source_t mInputSource; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | status_t mStatus; |
| 383 | uint32_t mLatency; |
| Glenn Kasten | 624a7fc | 2012-06-21 16:24:00 -0700 | [diff] [blame] | 384 | audio_channel_mask_t mChannelMask; |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 385 | audio_io_handle_t mInput; // returned by AudioSystem::getInput() |
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 386 | int mSessionId; |
| Glenn Kasten | 02e84ea | 2012-06-25 13:45:12 -0700 | [diff] [blame] | 387 | |
| 388 | // may be changed if IAudioRecord object is re-created |
| 389 | sp<IAudioRecord> mAudioRecord; |
| 390 | sp<IMemory> mCblkMemory; |
| 391 | audio_track_cblk_t* mCblk; |
| 392 | |
| Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 393 | int mPreviousPriority; // before start() |
| Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 394 | SchedPolicy mPreviousSchedulingGroup; |
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | }; // namespace android |
| 398 | |
| 399 | #endif /*AUDIORECORD_H_*/ |