AudioRecord.h revision ad04d9201452001dbaac4349f084cc9316190b89
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: Reserved for future use. 129 * cbf: Callback function. If not null, this function is called periodically 130 * to provide new PCM data. 131 * notificationFrames: The callback function is called each time notificationFrames PCM 132 * frames are ready in record track output buffer. 133 * user Context for use by the callback receiver. 134 */ 135 136 AudioRecord(int streamType, 137 uint32_t sampleRate = 0, 138 int format = 0, 139 int channelCount = 0, 140 int frameCount = 0, 141 uint32_t flags = 0, 142 callback_t cbf = 0, 143 void* user = 0, 144 int notificationFrames = 0); 145 146 147 /* Terminates the AudioRecord and unregisters it from AudioFlinger. 148 * Also destroys all resources assotiated with the AudioRecord. 149 */ 150 ~AudioRecord(); 151 152 153 /* Initialize an uninitialized AudioRecord. 154 * Returned status (from utils/Errors.h) can be: 155 * - NO_ERROR: successful intialization 156 * - INVALID_OPERATION: AudioRecord is already intitialized or record device is already in use 157 * - BAD_VALUE: invalid parameter (channelCount, format, sampleRate...) 158 * - NO_INIT: audio server or audio hardware not initialized 159 * - PERMISSION_DENIED: recording is not allowed for the requesting process 160 * */ 161 status_t set(int streamType = 0, 162 uint32_t sampleRate = 0, 163 int format = 0, 164 int channelCount = 0, 165 int frameCount = 0, 166 uint32_t flags = 0, 167 callback_t cbf = 0, 168 void* user = 0, 169 int notificationFrames = 0, 170 bool threadCanCallJava = false); 171 172 173 /* Result of constructing the AudioRecord. This must be checked 174 * before using any AudioRecord API (except for set()), using 175 * an uninitialized AudioRecord produces undefined results. 176 * See set() method above for possible return codes. 177 */ 178 status_t initCheck() const; 179 180 /* Returns this track's latency in milliseconds. 181 * This includes the latency due to AudioRecord buffer size 182 * and audio hardware driver. 183 */ 184 uint32_t latency() const; 185 186 /* getters, see constructor */ 187 188 uint32_t sampleRate() const; 189 int format() const; 190 int channelCount() const; 191 uint32_t frameCount() const; 192 int frameSize() const; 193 194 195 /* After it's created the track is not active. Call start() to 196 * make it active. If set, the callback will start being called. 197 */ 198 status_t start(); 199 200 /* Stop a track. If set, the callback will cease being called and 201 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works 202 * and will fill up buffers until the pool is exhausted. 203 */ 204 status_t stop(); 205 bool stopped() const; 206 207 /* get sample rate for this track 208 */ 209 uint32_t getSampleRate(); 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 213 * with marker == 0 cancels marker notification callback. 214 * If the AudioRecord has been opened with no callback function associated, 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); 226 status_t getMarkerPosition(uint32_t *marker); 227 228 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. 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); 245 status_t getPositionUpdatePeriod(uint32_t *updatePeriod); 246 247 248 /* Gets record head position. The position is the total number of frames 249 * recorded since record start. 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 */ 259 status_t getPosition(uint32_t *position); 260 261 262 263 /* obtains a buffer of "frameCount" frames. The buffer must be 264 * filled entirely. If the track is stopped, obtainBuffer() returns 265 * STOPPED instead of NO_ERROR as long as there are buffers availlable, 266 * at which point NO_MORE_BUFFERS is returned. 267 * Buffers will be returned until the pool (buffercount()) 268 * is exhausted, at which point obtainBuffer() will either block 269 * or return WOULD_BLOCK depending on the value of the "blocking" 270 * parameter. 271 */ 272 273 enum { 274 NO_MORE_BUFFERS = 0x80000001, 275 STOPPED = 1 276 }; 277 278 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); 279 void releaseBuffer(Buffer* audioBuffer); 280 281 282 /* As a convenience we provide a read() interface to the audio buffer. 283 * This is implemented on top of lockBuffer/unlockBuffer. 284 */ 285 ssize_t read(void* buffer, size_t size); 286 287private: 288 /* copying audio tracks is not allowed */ 289 AudioRecord(const AudioRecord& other); 290 AudioRecord& operator = (const AudioRecord& other); 291 292 /* a small internal class to handle the callback */ 293 class ClientRecordThread : public Thread 294 { 295 public: 296 ClientRecordThread(AudioRecord& receiver, bool bCanCallJava = false); 297 private: 298 friend class AudioRecord; 299 virtual bool threadLoop(); 300 virtual status_t readyToRun() { return NO_ERROR; } 301 virtual void onFirstRef() {} 302 AudioRecord& mReceiver; 303 Mutex mLock; 304 }; 305 306 bool processAudioBuffer(const sp<ClientRecordThread>& thread); 307 308 sp<IAudioFlinger> mAudioFlinger; 309 sp<IAudioRecord> mAudioRecord; 310 sp<IMemory> mCblkMemory; 311 sp<ClientRecordThread> mClientRecordThread; 312 Mutex mRecordThreadLock; 313 314 uint32_t mSampleRate; 315 uint32_t mFrameCount; 316 317 audio_track_cblk_t* mCblk; 318 uint8_t mFormat; 319 uint8_t mChannelCount; 320 uint8_t mReserved[2]; 321 status_t mStatus; 322 uint32_t mLatency; 323 324 volatile int32_t mActive; 325 326 callback_t mCbf; 327 void* mUserData; 328 uint32_t mNotificationFrames; 329 uint32_t mRemainingFrames; 330 uint32_t mMarkerPosition; 331 uint32_t mNewPosition; 332 uint32_t mUpdatePeriod; 333}; 334 335}; // namespace android 336 337#endif /*AUDIORECORD_H_*/ 338