dom/system/gonk/android_audio/AudioTrack.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2007 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef ANDROID_AUDIOTRACK_H
michael@0 18 #define ANDROID_AUDIOTRACK_H
michael@0 19
michael@0 20 #include <stdint.h>
michael@0 21 #include <sys/types.h>
michael@0 22
michael@0 23 #include "IAudioFlinger.h"
michael@0 24 #include "IAudioTrack.h"
michael@0 25 #include "AudioSystem.h"
michael@0 26
michael@0 27 #include <utils/RefBase.h>
michael@0 28 #include <utils/Errors.h>
michael@0 29 #include <binder/IInterface.h>
michael@0 30 #include <binder/IMemory.h>
michael@0 31 #include <utils/threads.h>
michael@0 32
michael@0 33
michael@0 34 namespace android {
michael@0 35
michael@0 36 // ----------------------------------------------------------------------------
michael@0 37
michael@0 38 class audio_track_cblk_t;
michael@0 39
michael@0 40 // ----------------------------------------------------------------------------
michael@0 41
michael@0 42 class AudioTrack
michael@0 43 {
michael@0 44 public:
michael@0 45 enum channel_index {
michael@0 46 MONO = 0,
michael@0 47 LEFT = 0,
michael@0 48 RIGHT = 1
michael@0 49 };
michael@0 50
michael@0 51 /* Events used by AudioTrack callback function (audio_track_cblk_t).
michael@0 52 */
michael@0 53 enum event_type {
michael@0 54 EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer.
michael@0 55 EVENT_UNDERRUN = 1, // PCM buffer underrun occured.
michael@0 56 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from loop start if loop count was not 0.
michael@0 57 EVENT_MARKER = 3, // Playback head is at the specified marker position (See setMarkerPosition()).
michael@0 58 EVENT_NEW_POS = 4, // Playback head is at a new position (See setPositionUpdatePeriod()).
michael@0 59 EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer.
michael@0 60 };
michael@0 61
michael@0 62 /* Create Buffer on the stack and pass it to obtainBuffer()
michael@0 63 * and releaseBuffer().
michael@0 64 */
michael@0 65
michael@0 66 class Buffer
michael@0 67 {
michael@0 68 public:
michael@0 69 enum {
michael@0 70 MUTE = 0x00000001
michael@0 71 };
michael@0 72 uint32_t flags;
michael@0 73 int channelCount;
michael@0 74 int format;
michael@0 75 size_t frameCount;
michael@0 76 size_t size;
michael@0 77 union {
michael@0 78 void* raw;
michael@0 79 short* i16;
michael@0 80 int8_t* i8;
michael@0 81 };
michael@0 82 };
michael@0 83
michael@0 84
michael@0 85 /* As a convenience, if a callback is supplied, a handler thread
michael@0 86 * is automatically created with the appropriate priority. This thread
michael@0 87 * invokes the callback when a new buffer becomes availlable or an underrun condition occurs.
michael@0 88 * Parameters:
michael@0 89 *
michael@0 90 * event: type of event notified (see enum AudioTrack::event_type).
michael@0 91 * user: Pointer to context for use by the callback receiver.
michael@0 92 * info: Pointer to optional parameter according to event type:
michael@0 93 * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write
michael@0 94 * more bytes than indicated by 'size' field and update 'size' if less bytes are
michael@0 95 * written.
michael@0 96 * - EVENT_UNDERRUN: unused.
michael@0 97 * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining.
michael@0 98 * - EVENT_MARKER: pointer to an uin32_t containing the marker position in frames.
michael@0 99 * - EVENT_NEW_POS: pointer to an uin32_t containing the new position in frames.
michael@0 100 * - EVENT_BUFFER_END: unused.
michael@0 101 */
michael@0 102
michael@0 103 typedef void (*callback_t)(int event, void* user, void *info);
michael@0 104
michael@0 105 /* Returns the minimum frame count required for the successful creation of
michael@0 106 * an AudioTrack object.
michael@0 107 * Returned status (from utils/Errors.h) can be:
michael@0 108 * - NO_ERROR: successful operation
michael@0 109 * - NO_INIT: audio server or audio hardware not initialized
michael@0 110 */
michael@0 111
michael@0 112 static status_t getMinFrameCount(int* frameCount,
michael@0 113 int streamType =-1,
michael@0 114 uint32_t sampleRate = 0);
michael@0 115
michael@0 116 /* Constructs an uninitialized AudioTrack. No connection with
michael@0 117 * AudioFlinger takes place.
michael@0 118 */
michael@0 119 AudioTrack();
michael@0 120
michael@0 121 /* Creates an audio track and registers it with AudioFlinger.
michael@0 122 * Once created, the track needs to be started before it can be used.
michael@0 123 * Unspecified values are set to the audio hardware's current
michael@0 124 * values.
michael@0 125 *
michael@0 126 * Parameters:
michael@0 127 *
michael@0 128 * streamType: Select the type of audio stream this track is attached to
michael@0 129 * (e.g. AudioSystem::MUSIC).
michael@0 130 * sampleRate: Track sampling rate in Hz.
michael@0 131 * format: Audio format (e.g AudioSystem::PCM_16_BIT for signed
michael@0 132 * 16 bits per sample).
michael@0 133 * channels: Channel mask: see AudioSystem::audio_channels.
michael@0 134 * frameCount: Total size of track PCM buffer in frames. This defines the
michael@0 135 * latency of the track.
michael@0 136 * flags: Reserved for future use.
michael@0 137 * cbf: Callback function. If not null, this function is called periodically
michael@0 138 * to request new PCM data.
michael@0 139 * notificationFrames: The callback function is called each time notificationFrames PCM
michael@0 140 * frames have been comsumed from track input buffer.
michael@0 141 * user Context for use by the callback receiver.
michael@0 142 */
michael@0 143
michael@0 144 AudioTrack( int streamType,
michael@0 145 uint32_t sampleRate = 0,
michael@0 146 int format = 0,
michael@0 147 int channels = 0,
michael@0 148 int frameCount = 0,
michael@0 149 uint32_t flags = 0,
michael@0 150 callback_t cbf = 0,
michael@0 151 void* user = 0,
michael@0 152 int notificationFrames = 0,
michael@0 153 int sessionId = 0);
michael@0 154
michael@0 155 /* Creates an audio track and registers it with AudioFlinger. With this constructor,
michael@0 156 * The PCM data to be rendered by AudioTrack is passed in a shared memory buffer
michael@0 157 * identified by the argument sharedBuffer. This prototype is for static buffer playback.
michael@0 158 * PCM data must be present into memory before the AudioTrack is started.
michael@0 159 * The Write() and Flush() methods are not supported in this case.
michael@0 160 * It is recommented to pass a callback function to be notified of playback end by an
michael@0 161 * EVENT_UNDERRUN event.
michael@0 162 */
michael@0 163
michael@0 164 AudioTrack( int streamType,
michael@0 165 uint32_t sampleRate = 0,
michael@0 166 int format = 0,
michael@0 167 int channels = 0,
michael@0 168 const sp<IMemory>& sharedBuffer = 0,
michael@0 169 uint32_t flags = 0,
michael@0 170 callback_t cbf = 0,
michael@0 171 void* user = 0,
michael@0 172 int notificationFrames = 0,
michael@0 173 int sessionId = 0);
michael@0 174
michael@0 175 /* Terminates the AudioTrack and unregisters it from AudioFlinger.
michael@0 176 * Also destroys all resources assotiated with the AudioTrack.
michael@0 177 */
michael@0 178 ~AudioTrack();
michael@0 179
michael@0 180
michael@0 181 /* Initialize an uninitialized AudioTrack.
michael@0 182 * Returned status (from utils/Errors.h) can be:
michael@0 183 * - NO_ERROR: successful intialization
michael@0 184 * - INVALID_OPERATION: AudioTrack is already intitialized
michael@0 185 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...)
michael@0 186 * - NO_INIT: audio server or audio hardware not initialized
michael@0 187 * */
michael@0 188 status_t set(int streamType =-1,
michael@0 189 uint32_t sampleRate = 0,
michael@0 190 int format = 0,
michael@0 191 int channels = 0,
michael@0 192 int frameCount = 0,
michael@0 193 uint32_t flags = 0,
michael@0 194 callback_t cbf = 0,
michael@0 195 void* user = 0,
michael@0 196 int notificationFrames = 0,
michael@0 197 const sp<IMemory>& sharedBuffer = 0,
michael@0 198 bool threadCanCallJava = false,
michael@0 199 int sessionId = 0);
michael@0 200
michael@0 201
michael@0 202 /* Result of constructing the AudioTrack. This must be checked
michael@0 203 * before using any AudioTrack API (except for set()), using
michael@0 204 * an uninitialized AudioTrack produces undefined results.
michael@0 205 * See set() method above for possible return codes.
michael@0 206 */
michael@0 207 status_t initCheck() const;
michael@0 208
michael@0 209 /* Returns this track's latency in milliseconds.
michael@0 210 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
michael@0 211 * and audio hardware driver.
michael@0 212 */
michael@0 213 uint32_t latency() const;
michael@0 214
michael@0 215 /* getters, see constructor */
michael@0 216
michael@0 217 int streamType() const;
michael@0 218 int format() const;
michael@0 219 int channelCount() const;
michael@0 220 uint32_t frameCount() const;
michael@0 221 int frameSize() const;
michael@0 222 sp<IMemory>& sharedBuffer();
michael@0 223
michael@0 224
michael@0 225 /* After it's created the track is not active. Call start() to
michael@0 226 * make it active. If set, the callback will start being called.
michael@0 227 */
michael@0 228 void start();
michael@0 229
michael@0 230 /* Stop a track. If set, the callback will cease being called and
michael@0 231 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
michael@0 232 * and will fill up buffers until the pool is exhausted.
michael@0 233 */
michael@0 234 void stop();
michael@0 235 bool stopped() const;
michael@0 236
michael@0 237 /* flush a stopped track. All pending buffers are discarded.
michael@0 238 * This function has no effect if the track is not stoped.
michael@0 239 */
michael@0 240 void flush();
michael@0 241
michael@0 242 /* Pause a track. If set, the callback will cease being called and
michael@0 243 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
michael@0 244 * and will fill up buffers until the pool is exhausted.
michael@0 245 */
michael@0 246 void pause();
michael@0 247
michael@0 248 /* mute or unmutes this track.
michael@0 249 * While mutted, the callback, if set, is still called.
michael@0 250 */
michael@0 251 void mute(bool);
michael@0 252 bool muted() const;
michael@0 253
michael@0 254
michael@0 255 /* set volume for this track, mostly used for games' sound effects
michael@0 256 * left and right volumes. Levels must be <= 1.0.
michael@0 257 */
michael@0 258 status_t setVolume(float left, float right);
michael@0 259 void getVolume(float* left, float* right);
michael@0 260
michael@0 261 /* set the send level for this track. An auxiliary effect should be attached
michael@0 262 * to the track with attachEffect(). Level must be <= 1.0.
michael@0 263 */
michael@0 264 status_t setAuxEffectSendLevel(float level);
michael@0 265 void getAuxEffectSendLevel(float* level);
michael@0 266
michael@0 267 /* set sample rate for this track, mostly used for games' sound effects
michael@0 268 */
michael@0 269 status_t setSampleRate(int sampleRate);
michael@0 270 uint32_t getSampleRate();
michael@0 271
michael@0 272 /* Enables looping and sets the start and end points of looping.
michael@0 273 *
michael@0 274 * Parameters:
michael@0 275 *
michael@0 276 * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start.
michael@0 277 * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start.
michael@0 278 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any pending or
michael@0 279 * active loop. loopCount = -1 means infinite looping.
michael@0 280 *
michael@0 281 * For proper operation the following condition must be respected:
michael@0 282 * (loopEnd-loopStart) <= framecount()
michael@0 283 */
michael@0 284 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
michael@0 285 status_t getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount);
michael@0 286
michael@0 287
michael@0 288 /* Sets marker position. When playback reaches the number of frames specified, a callback with event
michael@0 289 * type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker notification
michael@0 290 * callback.
michael@0 291 * If the AudioTrack has been opened with no callback function associated, the operation will fail.
michael@0 292 *
michael@0 293 * Parameters:
michael@0 294 *
michael@0 295 * marker: marker position expressed in frames.
michael@0 296 *
michael@0 297 * Returned status (from utils/Errors.h) can be:
michael@0 298 * - NO_ERROR: successful operation
michael@0 299 * - INVALID_OPERATION: the AudioTrack has no callback installed.
michael@0 300 */
michael@0 301 status_t setMarkerPosition(uint32_t marker);
michael@0 302 status_t getMarkerPosition(uint32_t *marker);
michael@0 303
michael@0 304
michael@0 305 /* Sets position update period. Every time the number of frames specified has been played,
michael@0 306 * a callback with event type EVENT_NEW_POS is called.
michael@0 307 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
michael@0 308 * callback.
michael@0 309 * If the AudioTrack has been opened with no callback function associated, the operation will fail.
michael@0 310 *
michael@0 311 * Parameters:
michael@0 312 *
michael@0 313 * updatePeriod: position update notification period expressed in frames.
michael@0 314 *
michael@0 315 * Returned status (from utils/Errors.h) can be:
michael@0 316 * - NO_ERROR: successful operation
michael@0 317 * - INVALID_OPERATION: the AudioTrack has no callback installed.
michael@0 318 */
michael@0 319 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
michael@0 320 status_t getPositionUpdatePeriod(uint32_t *updatePeriod);
michael@0 321
michael@0 322
michael@0 323 /* Sets playback head position within AudioTrack buffer. The new position is specified
michael@0 324 * in number of frames.
michael@0 325 * This method must be called with the AudioTrack in paused or stopped state.
michael@0 326 * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames.
michael@0 327 * Therefore using this method makes sense only when playing a "static" audio buffer
michael@0 328 * as opposed to streaming.
michael@0 329 * The getPosition() method on the other hand returns the total number of frames played since
michael@0 330 * playback start.
michael@0 331 *
michael@0 332 * Parameters:
michael@0 333 *
michael@0 334 * position: New playback head position within AudioTrack buffer.
michael@0 335 *
michael@0 336 * Returned status (from utils/Errors.h) can be:
michael@0 337 * - NO_ERROR: successful operation
michael@0 338 * - INVALID_OPERATION: the AudioTrack is not stopped.
michael@0 339 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack buffer
michael@0 340 */
michael@0 341 status_t setPosition(uint32_t position);
michael@0 342 status_t getPosition(uint32_t *position);
michael@0 343
michael@0 344 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
michael@0 345 * rewriting the buffer before restarting playback after a stop.
michael@0 346 * This method must be called with the AudioTrack in paused or stopped state.
michael@0 347 *
michael@0 348 * Returned status (from utils/Errors.h) can be:
michael@0 349 * - NO_ERROR: successful operation
michael@0 350 * - INVALID_OPERATION: the AudioTrack is not stopped.
michael@0 351 */
michael@0 352 status_t reload();
michael@0 353
michael@0 354 /* returns a handle on the audio output used by this AudioTrack.
michael@0 355 *
michael@0 356 * Parameters:
michael@0 357 * none.
michael@0 358 *
michael@0 359 * Returned value:
michael@0 360 * handle on audio hardware output
michael@0 361 */
michael@0 362 audio_io_handle_t getOutput();
michael@0 363
michael@0 364 /* returns the unique ID associated to this track.
michael@0 365 *
michael@0 366 * Parameters:
michael@0 367 * none.
michael@0 368 *
michael@0 369 * Returned value:
michael@0 370 * AudioTrack ID.
michael@0 371 */
michael@0 372 int getSessionId();
michael@0 373
michael@0 374
michael@0 375 /* Attach track auxiliary output to specified effect. Used effectId = 0
michael@0 376 * to detach track from effect.
michael@0 377 *
michael@0 378 * Parameters:
michael@0 379 *
michael@0 380 * effectId: effectId obtained from AudioEffect::id().
michael@0 381 *
michael@0 382 * Returned status (from utils/Errors.h) can be:
michael@0 383 * - NO_ERROR: successful operation
michael@0 384 * - INVALID_OPERATION: the effect is not an auxiliary effect.
michael@0 385 * - BAD_VALUE: The specified effect ID is invalid
michael@0 386 */
michael@0 387 status_t attachAuxEffect(int effectId);
michael@0 388
michael@0 389 /* obtains a buffer of "frameCount" frames. The buffer must be
michael@0 390 * filled entirely. If the track is stopped, obtainBuffer() returns
michael@0 391 * STOPPED instead of NO_ERROR as long as there are buffers availlable,
michael@0 392 * at which point NO_MORE_BUFFERS is returned.
michael@0 393 * Buffers will be returned until the pool (buffercount())
michael@0 394 * is exhausted, at which point obtainBuffer() will either block
michael@0 395 * or return WOULD_BLOCK depending on the value of the "blocking"
michael@0 396 * parameter.
michael@0 397 */
michael@0 398
michael@0 399 enum {
michael@0 400 NO_MORE_BUFFERS = 0x80000001,
michael@0 401 STOPPED = 1
michael@0 402 };
michael@0 403
michael@0 404 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
michael@0 405 void releaseBuffer(Buffer* audioBuffer);
michael@0 406
michael@0 407
michael@0 408 /* As a convenience we provide a write() interface to the audio buffer.
michael@0 409 * This is implemented on top of lockBuffer/unlockBuffer. For best
michael@0 410 * performance
michael@0 411 *
michael@0 412 */
michael@0 413 ssize_t write(const void* buffer, size_t size);
michael@0 414
michael@0 415 /*
michael@0 416 * Dumps the state of an audio track.
michael@0 417 */
michael@0 418 status_t dump(int fd, const Vector<String16>& args) const;
michael@0 419
michael@0 420 private:
michael@0 421 /* copying audio tracks is not allowed */
michael@0 422 AudioTrack(const AudioTrack& other);
michael@0 423 AudioTrack& operator = (const AudioTrack& other);
michael@0 424
michael@0 425 /* a small internal class to handle the callback */
michael@0 426 class AudioTrackThread : public Thread
michael@0 427 {
michael@0 428 public:
michael@0 429 AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
michael@0 430 private:
michael@0 431 friend class AudioTrack;
michael@0 432 virtual bool threadLoop();
michael@0 433 virtual status_t readyToRun();
michael@0 434 virtual void onFirstRef();
michael@0 435 AudioTrack& mReceiver;
michael@0 436 Mutex mLock;
michael@0 437 };
michael@0 438
michael@0 439 bool processAudioBuffer(const sp<AudioTrackThread>& thread);
michael@0 440 status_t createTrack(int streamType,
michael@0 441 uint32_t sampleRate,
michael@0 442 int format,
michael@0 443 int channelCount,
michael@0 444 int frameCount,
michael@0 445 uint32_t flags,
michael@0 446 const sp<IMemory>& sharedBuffer,
michael@0 447 audio_io_handle_t output,
michael@0 448 bool enforceFrameCount);
michael@0 449
michael@0 450 sp<IAudioTrack> mAudioTrack;
michael@0 451 sp<IMemory> mCblkMemory;
michael@0 452 sp<AudioTrackThread> mAudioTrackThread;
michael@0 453
michael@0 454 float mVolume[2];
michael@0 455 float mSendLevel;
michael@0 456 uint32_t mFrameCount;
michael@0 457
michael@0 458 audio_track_cblk_t* mCblk;
michael@0 459 uint8_t mStreamType;
michael@0 460 uint8_t mFormat;
michael@0 461 uint8_t mChannelCount;
michael@0 462 uint8_t mMuted;
michael@0 463 uint32_t mChannels;
michael@0 464 status_t mStatus;
michael@0 465 uint32_t mLatency;
michael@0 466
michael@0 467 volatile int32_t mActive;
michael@0 468
michael@0 469 callback_t mCbf;
michael@0 470 void* mUserData;
michael@0 471 uint32_t mNotificationFramesReq; // requested number of frames between each notification callback
michael@0 472 uint32_t mNotificationFramesAct; // actual number of frames between each notification callback
michael@0 473 sp<IMemory> mSharedBuffer;
michael@0 474 int mLoopCount;
michael@0 475 uint32_t mRemainingFrames;
michael@0 476 uint32_t mMarkerPosition;
michael@0 477 bool mMarkerReached;
michael@0 478 uint32_t mNewPosition;
michael@0 479 uint32_t mUpdatePeriod;
michael@0 480 uint32_t mFlags;
michael@0 481 int mSessionId;
michael@0 482 int mAuxEffectId;
michael@0 483 uint32_t mPadding[8];
michael@0 484 };
michael@0 485
michael@0 486
michael@0 487 }; // namespace android
michael@0 488
michael@0 489 #endif // ANDROID_AUDIOTRACK_H

mercurial