AudioRecord.h revision 7d563247cdac0509009d579bbf849157d47c38a9
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 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <media/IAudioFlinger.h> 24#include <media/IAudioRecord.h> 25#include <media/AudioTrack.h> 26 27#include <utils/RefBase.h> 28#include <utils/Errors.h> 29#include <utils/IInterface.h> 30#include <utils/IMemory.h> 31#include <utils/threads.h> 32 33 34namespace android { 35 36// ---------------------------------------------------------------------------- 37 38class AudioRecord 39{ 40public: 41 42 enum stream_type { 43 DEFAULT_INPUT =-1, 44 MIC_INPUT = 0, 45 NUM_STREAM_TYPES 46 }; 47 48 static const int DEFAULT_SAMPLE_RATE = 8000; 49 50 /* Events used by AudioRecord callback function (callback_t). 51 * 52 * to keep in sync with frameworks/base/media/java/android/media/AudioRecord.java 53 */ 54 enum event_type { 55 EVENT_MORE_DATA = 0, // Request to reqd more data from PCM buffer. 56 EVENT_OVERRUN = 1, // PCM buffer overrun occured. 57 EVENT_MARKER = 2, // Record head is at the specified marker position 58 // (See setMarkerPosition()). 59 EVENT_NEW_POS = 3, // Record head is at a new position 60 // (See setPositionUpdatePeriod()). 61 }; 62 63 /* Create Buffer on the stack and pass it to obtainBuffer() 64 * and releaseBuffer(). 65 */ 66 67 class Buffer 68 { 69 public: 70 enum { 71 MUTE = 0x00000001 72 }; 73 uint32_t flags; 74 int channelCount; 75 int format; 76 size_t frameCount; 77 size_t size; 78 union { 79 void* raw; 80 short* i16; 81 int8_t* i8; 82 }; 83 }; 84 85 /* These are static methods to control the system-wide AudioFlinger 86 * only privileged processes can have access to them 87 */ 88 89// static status_t setMasterMute(bool mute); 90 91 /* As a convenience, if a callback is supplied, a handler thread 92 * is automatically created with the appropriate priority. This thread 93 * invokes the callback when a new buffer becomes ready or an overrun condition occurs. 94 * Parameters: 95 * 96 * event: type of event notified (see enum AudioRecord::event_type). 97 * user: Pointer to context for use by the callback receiver. 98 * info: Pointer to optional parameter according to event type: 99 * - EVENT_MORE_DATA: pointer to AudioRecord::Buffer struct. The callback must not read 100 * more bytes than indicated by 'size' field and update 'size' if less bytes are 101 * read. 102 * - EVENT_OVERRUN: unused. 103 * - EVENT_MARKER: pointer to an uin32_t containing the marker position in frames. 104 * - EVENT_NEW_POS: pointer to an uin32_t containing the new position in frames. 105 */ 106 107 typedef void (*callback_t)(int event, void* user, void *info); 108 109 /* Constructs an uninitialized AudioRecord. No connection with 110 * AudioFlinger takes place. 111 */ 112 AudioRecord(); 113 114 /* Creates an AudioRecord track and registers it with AudioFlinger. 115 * Once created, the track needs to be started before it can be used. 116 * Unspecified values are set to the audio hardware's current 117 * values. 118 * 119 * Parameters: 120 * 121 * streamType: Select the audio input to record to (e.g. AudioRecord::MIC_INPUT). 122 * sampleRate: Track sampling rate in Hz. 123 * format: PCM sample format (e.g AudioSystem::PCM_16_BIT for signed 124 * 16 bits per sample). 125 * channelCount: Number of PCM channels (e.g 2 for stereo). 126 * frameCount: Total size of track PCM buffer in frames. This defines the 127 * latency of the track. 128 * flags: A bitmask of acoustic values from enum record_flags. It enables 129 * AGC, NS, and IIR. 130 * cbf: Callback function. If not null, this function is called periodically 131 * to provide new PCM data. 132 * notificationFrames: The callback function is called each time notificationFrames PCM 133 * frames are ready in record track output buffer. 134 * user Context for use by the callback receiver. 135 */ 136 137 enum record_flags { 138 RECORD_AGC_ENABLE = AudioSystem::AGC_ENABLE, 139 RECORD_NS_ENABLE = AudioSystem::NS_ENABLE, 140 RECORD_IIR_ENABLE = AudioSystem::TX_IIR_ENABLE 141 }; 142 143 AudioRecord(int streamType, 144 uint32_t sampleRate = 0, 145 int format = 0, 146 int channelCount = 0, 147 int frameCount = 0, 148 uint32_t flags = 0, 149 callback_t cbf = 0, 150 void* user = 0, 151 int notificationFrames = 0); 152 153 154 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 155 * Also destroys all resources assotiated with the AudioRecord. 156 */ 157 ~AudioRecord(); 158 159 160 /* Initialize an uninitialized AudioRecord. 161 * Returned status (from utils/Errors.h) can be: 162 * - NO_ERROR: successful intialization 163 * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use 164 * - BAD_VALUE: invalid parameter (channelCount, format, sampleRate...) 165 * - NO_INIT: audio server or audio hardware not initialized 166 * - PERMISSION_DENIED: recording is not allowed for the requesting process 167 * */ 168 status_t set(int streamType = 0, 169 uint32_t sampleRate = 0, 170 int format = 0, 171 int channelCount = 0, 172 int frameCount = 0, 173 uint32_t flags = 0, 174 callback_t cbf = 0, 175 void* user = 0, 176 int notificationFrames = 0, 177 bool threadCanCallJava = false); 178 179 180 /* Result of constructing the AudioRecord. This must be checked 181 * before using any AudioRecord API (except for set()), using 182 * an uninitialized AudioRecord produces undefined results. 183 * See set() method above for possible return codes. 184 */ 185 status_t initCheck() const; 186 187 /* Returns this track's latency in milliseconds. 188 * This includes the latency due to AudioRecord buffer size 189 * and audio hardware driver. 190 */ 191 uint32_t latency() const; 192 193 /* getters, see constructor */ 194 195 uint32_t sampleRate() const; 196 int format() const; 197 int channelCount() const; 198 uint32_t frameCount() const; 199 int frameSize() const; 200 201 202 /* After it's created the track is not active. Call start() to 203 * make it active. If set, the callback will start being called. 204 */ 205 status_t start(); 206 207 /* Stop a track. If set, the callback will cease being called and 208 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works 209 * and will fill up buffers until the pool is exhausted. 210 */ 211 status_t stop(); 212 bool stopped() const; 213 214 /* get sample rate for this track 215 */ 216 uint32_t getSampleRate(); 217 218 /* Sets marker position. When record reaches the number of frames specified, 219 * a callback with event type EVENT_MARKER is called. Calling setMarkerPosition 220 * with marker == 0 cancels marker notification callback. 221 * If the AudioRecord has been opened with no callback function associated, 222 * the operation will fail. 223 * 224 * Parameters: 225 * 226 * marker: marker position expressed in frames. 227 * 228 * Returned status (from utils/Errors.h) can be: 229 * - NO_ERROR: successful operation 230 * - INVALID_OPERATION: the AudioRecord has no callback installed. 231 */ 232 status_t setMarkerPosition(uint32_t marker); 233 status_t getMarkerPosition(uint32_t *marker); 234 235 236 /* Sets position update period. Every time the number of frames specified has been recorded, 237 * a callback with event type EVENT_NEW_POS is called. 238 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 239 * callback. 240 * If the AudioRecord has been opened with no callback function associated, 241 * the operation will fail. 242 * 243 * Parameters: 244 * 245 * updatePeriod: position update notification period expressed in frames. 246 * 247 * Returned status (from utils/Errors.h) can be: 248 * - NO_ERROR: successful operation 249 * - INVALID_OPERATION: the AudioRecord has no callback installed. 250 */ 251 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 252 status_t getPositionUpdatePeriod(uint32_t *updatePeriod); 253 254 255 /* Gets record head position. The position is the total number of frames 256 * recorded since record start. 257 * 258 * Parameters: 259 * 260 * position: Address where to return record head position within AudioRecord buffer. 261 * 262 * Returned status (from utils/Errors.h) can be: 263 * - NO_ERROR: successful operation 264 * - BAD_VALUE: position is NULL 265 */ 266 status_t getPosition(uint32_t *position); 267 268 269 270 /* obtains a buffer of "frameCount" frames. The buffer must be 271 * filled entirely. If the track is stopped, obtainBuffer() returns 272 * STOPPED instead of NO_ERROR as long as there are buffers availlable, 273 * at which point NO_MORE_BUFFERS is returned. 274 * Buffers will be returned until the pool (buffercount()) 275 * is exhausted, at which point obtainBuffer() will either block 276 * or return WOULD_BLOCK depending on the value of the "blocking" 277 * parameter. 278 */ 279 280 enum { 281 NO_MORE_BUFFERS = 0x80000001, 282 STOPPED = 1 283 }; 284 285 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); 286 void releaseBuffer(Buffer* audioBuffer); 287 288 289 /* As a convenience we provide a read() interface to the audio buffer. 290 * This is implemented on top of lockBuffer/unlockBuffer. 291 */ 292 ssize_t read(void* buffer, size_t size); 293 294private: 295 /* copying audio tracks is not allowed */ 296 AudioRecord(const AudioRecord& other); 297 AudioRecord& operator = (const AudioRecord& other); 298 299 /* a small internal class to handle the callback */ 300 class ClientRecordThread : public Thread 301 { 302 public: 303 ClientRecordThread(AudioRecord& receiver, bool bCanCallJava = false); 304 private: 305 friend class AudioRecord; 306 virtual bool threadLoop(); 307 virtual status_t readyToRun() { return NO_ERROR; } 308 virtual void onFirstRef() {} 309 AudioRecord& mReceiver; 310 Mutex mLock; 311 }; 312 313 bool processAudioBuffer(const sp<ClientRecordThread>& thread); 314 315 sp<IAudioFlinger> mAudioFlinger; 316 sp<IAudioRecord> mAudioRecord; 317 sp<IMemory> mCblkMemory; 318 sp<ClientRecordThread> mClientRecordThread; 319 Mutex mRecordThreadLock; 320 321 uint32_t mSampleRate; 322 uint32_t mFrameCount; 323 324 audio_track_cblk_t* mCblk; 325 uint8_t mFormat; 326 uint8_t mChannelCount; 327 uint8_t mReserved[2]; 328 status_t mStatus; 329 uint32_t mLatency; 330 331 volatile int32_t mActive; 332 333 callback_t mCbf; 334 void* mUserData; 335 uint32_t mNotificationFrames; 336 uint32_t mRemainingFrames; 337 uint32_t mMarkerPosition; 338 bool mMarkerReached; 339 uint32_t mNewPosition; 340 uint32_t mUpdatePeriod; 341}; 342 343}; // namespace android 344 345#endif /*AUDIORECORD_H_*/ 346