michael@0: /* michael@0: * Copyright (C) 2007 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_AUDIOTRACK_H michael@0: #define ANDROID_AUDIOTRACK_H michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "IAudioFlinger.h" michael@0: #include "IAudioTrack.h" michael@0: #include "AudioSystem.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: michael@0: namespace android { michael@0: michael@0: // ---------------------------------------------------------------------------- michael@0: michael@0: class audio_track_cblk_t; michael@0: michael@0: // ---------------------------------------------------------------------------- michael@0: michael@0: class AudioTrack michael@0: { michael@0: public: michael@0: enum channel_index { michael@0: MONO = 0, michael@0: LEFT = 0, michael@0: RIGHT = 1 michael@0: }; michael@0: michael@0: /* Events used by AudioTrack callback function (audio_track_cblk_t). michael@0: */ michael@0: enum event_type { michael@0: EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer. michael@0: EVENT_UNDERRUN = 1, // PCM buffer underrun occured. michael@0: EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from loop start if loop count was not 0. michael@0: EVENT_MARKER = 3, // Playback head is at the specified marker position (See setMarkerPosition()). michael@0: EVENT_NEW_POS = 4, // Playback head is at a new position (See setPositionUpdatePeriod()). michael@0: EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer. michael@0: }; michael@0: michael@0: /* Create Buffer on the stack and pass it to obtainBuffer() michael@0: * and releaseBuffer(). michael@0: */ michael@0: michael@0: class Buffer michael@0: { michael@0: public: michael@0: enum { michael@0: MUTE = 0x00000001 michael@0: }; michael@0: uint32_t flags; michael@0: int channelCount; michael@0: int format; michael@0: size_t frameCount; michael@0: size_t size; michael@0: union { michael@0: void* raw; michael@0: short* i16; michael@0: int8_t* i8; michael@0: }; michael@0: }; michael@0: michael@0: michael@0: /* As a convenience, if a callback is supplied, a handler thread michael@0: * is automatically created with the appropriate priority. This thread michael@0: * invokes the callback when a new buffer becomes availlable or an underrun condition occurs. michael@0: * Parameters: michael@0: * michael@0: * event: type of event notified (see enum AudioTrack::event_type). michael@0: * user: Pointer to context for use by the callback receiver. michael@0: * info: Pointer to optional parameter according to event type: michael@0: * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write michael@0: * more bytes than indicated by 'size' field and update 'size' if less bytes are michael@0: * written. michael@0: * - EVENT_UNDERRUN: unused. michael@0: * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining. michael@0: * - EVENT_MARKER: pointer to an uin32_t containing the marker position in frames. michael@0: * - EVENT_NEW_POS: pointer to an uin32_t containing the new position in frames. michael@0: * - EVENT_BUFFER_END: unused. michael@0: */ michael@0: michael@0: typedef void (*callback_t)(int event, void* user, void *info); michael@0: michael@0: /* Returns the minimum frame count required for the successful creation of michael@0: * an AudioTrack object. michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - NO_INIT: audio server or audio hardware not initialized michael@0: */ michael@0: michael@0: static status_t getMinFrameCount(int* frameCount, michael@0: int streamType =-1, michael@0: uint32_t sampleRate = 0); michael@0: michael@0: /* Constructs an uninitialized AudioTrack. No connection with michael@0: * AudioFlinger takes place. michael@0: */ michael@0: AudioTrack(); michael@0: michael@0: /* Creates an audio track and registers it with AudioFlinger. michael@0: * Once created, the track needs to be started before it can be used. michael@0: * Unspecified values are set to the audio hardware's current michael@0: * values. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * streamType: Select the type of audio stream this track is attached to michael@0: * (e.g. AudioSystem::MUSIC). michael@0: * sampleRate: Track sampling rate in Hz. michael@0: * format: Audio format (e.g AudioSystem::PCM_16_BIT for signed michael@0: * 16 bits per sample). michael@0: * channels: Channel mask: see AudioSystem::audio_channels. michael@0: * frameCount: Total size of track PCM buffer in frames. This defines the michael@0: * latency of the track. michael@0: * flags: Reserved for future use. michael@0: * cbf: Callback function. If not null, this function is called periodically michael@0: * to request new PCM data. michael@0: * notificationFrames: The callback function is called each time notificationFrames PCM michael@0: * frames have been comsumed from track input buffer. michael@0: * user Context for use by the callback receiver. michael@0: */ michael@0: michael@0: AudioTrack( int streamType, michael@0: uint32_t sampleRate = 0, michael@0: int format = 0, michael@0: int channels = 0, michael@0: int frameCount = 0, michael@0: uint32_t flags = 0, michael@0: callback_t cbf = 0, michael@0: void* user = 0, michael@0: int notificationFrames = 0, michael@0: int sessionId = 0); michael@0: michael@0: /* Creates an audio track and registers it with AudioFlinger. With this constructor, michael@0: * The PCM data to be rendered by AudioTrack is passed in a shared memory buffer michael@0: * identified by the argument sharedBuffer. This prototype is for static buffer playback. michael@0: * PCM data must be present into memory before the AudioTrack is started. michael@0: * The Write() and Flush() methods are not supported in this case. michael@0: * It is recommented to pass a callback function to be notified of playback end by an michael@0: * EVENT_UNDERRUN event. michael@0: */ michael@0: michael@0: AudioTrack( int streamType, michael@0: uint32_t sampleRate = 0, michael@0: int format = 0, michael@0: int channels = 0, michael@0: const sp& sharedBuffer = 0, michael@0: uint32_t flags = 0, michael@0: callback_t cbf = 0, michael@0: void* user = 0, michael@0: int notificationFrames = 0, michael@0: int sessionId = 0); michael@0: michael@0: /* Terminates the AudioTrack and unregisters it from AudioFlinger. michael@0: * Also destroys all resources assotiated with the AudioTrack. michael@0: */ michael@0: ~AudioTrack(); michael@0: michael@0: michael@0: /* Initialize an uninitialized AudioTrack. michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful intialization michael@0: * - INVALID_OPERATION: AudioTrack is already intitialized michael@0: * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) michael@0: * - NO_INIT: audio server or audio hardware not initialized michael@0: * */ michael@0: status_t set(int streamType =-1, michael@0: uint32_t sampleRate = 0, michael@0: int format = 0, michael@0: int channels = 0, michael@0: int frameCount = 0, michael@0: uint32_t flags = 0, michael@0: callback_t cbf = 0, michael@0: void* user = 0, michael@0: int notificationFrames = 0, michael@0: const sp& sharedBuffer = 0, michael@0: bool threadCanCallJava = false, michael@0: int sessionId = 0); michael@0: michael@0: michael@0: /* Result of constructing the AudioTrack. This must be checked michael@0: * before using any AudioTrack API (except for set()), using michael@0: * an uninitialized AudioTrack produces undefined results. michael@0: * See set() method above for possible return codes. michael@0: */ michael@0: status_t initCheck() const; michael@0: michael@0: /* Returns this track's latency in milliseconds. michael@0: * This includes the latency due to AudioTrack buffer size, AudioMixer (if any) michael@0: * and audio hardware driver. michael@0: */ michael@0: uint32_t latency() const; michael@0: michael@0: /* getters, see constructor */ michael@0: michael@0: int streamType() const; michael@0: int format() const; michael@0: int channelCount() const; michael@0: uint32_t frameCount() const; michael@0: int frameSize() const; michael@0: sp& sharedBuffer(); michael@0: michael@0: michael@0: /* After it's created the track is not active. Call start() to michael@0: * make it active. If set, the callback will start being called. michael@0: */ michael@0: void start(); michael@0: michael@0: /* Stop a track. If set, the callback will cease being called and michael@0: * obtainBuffer returns STOPPED. Note that obtainBuffer() still works michael@0: * and will fill up buffers until the pool is exhausted. michael@0: */ michael@0: void stop(); michael@0: bool stopped() const; michael@0: michael@0: /* flush a stopped track. All pending buffers are discarded. michael@0: * This function has no effect if the track is not stoped. michael@0: */ michael@0: void flush(); michael@0: michael@0: /* Pause a track. If set, the callback will cease being called and michael@0: * obtainBuffer returns STOPPED. Note that obtainBuffer() still works michael@0: * and will fill up buffers until the pool is exhausted. michael@0: */ michael@0: void pause(); michael@0: michael@0: /* mute or unmutes this track. michael@0: * While mutted, the callback, if set, is still called. michael@0: */ michael@0: void mute(bool); michael@0: bool muted() const; michael@0: michael@0: michael@0: /* set volume for this track, mostly used for games' sound effects michael@0: * left and right volumes. Levels must be <= 1.0. michael@0: */ michael@0: status_t setVolume(float left, float right); michael@0: void getVolume(float* left, float* right); michael@0: michael@0: /* set the send level for this track. An auxiliary effect should be attached michael@0: * to the track with attachEffect(). Level must be <= 1.0. michael@0: */ michael@0: status_t setAuxEffectSendLevel(float level); michael@0: void getAuxEffectSendLevel(float* level); michael@0: michael@0: /* set sample rate for this track, mostly used for games' sound effects michael@0: */ michael@0: status_t setSampleRate(int sampleRate); michael@0: uint32_t getSampleRate(); michael@0: michael@0: /* Enables looping and sets the start and end points of looping. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start. michael@0: * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start. michael@0: * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any pending or michael@0: * active loop. loopCount = -1 means infinite looping. michael@0: * michael@0: * For proper operation the following condition must be respected: michael@0: * (loopEnd-loopStart) <= framecount() michael@0: */ michael@0: status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount); michael@0: status_t getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount); michael@0: michael@0: michael@0: /* Sets marker position. When playback reaches the number of frames specified, a callback with event michael@0: * type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker notification michael@0: * callback. michael@0: * If the AudioTrack has been opened with no callback function associated, the operation will fail. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * marker: marker position expressed in frames. michael@0: * michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - INVALID_OPERATION: the AudioTrack has no callback installed. michael@0: */ michael@0: status_t setMarkerPosition(uint32_t marker); michael@0: status_t getMarkerPosition(uint32_t *marker); michael@0: michael@0: michael@0: /* Sets position update period. Every time the number of frames specified has been played, michael@0: * a callback with event type EVENT_NEW_POS is called. michael@0: * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification michael@0: * callback. michael@0: * If the AudioTrack has been opened with no callback function associated, the operation will fail. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * updatePeriod: position update notification period expressed in frames. michael@0: * michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - INVALID_OPERATION: the AudioTrack has no callback installed. michael@0: */ michael@0: status_t setPositionUpdatePeriod(uint32_t updatePeriod); michael@0: status_t getPositionUpdatePeriod(uint32_t *updatePeriod); michael@0: michael@0: michael@0: /* Sets playback head position within AudioTrack buffer. The new position is specified michael@0: * in number of frames. michael@0: * This method must be called with the AudioTrack in paused or stopped state. michael@0: * Note that the actual position set is modulo the AudioTrack buffer size in frames. michael@0: * Therefore using this method makes sense only when playing a "static" audio buffer michael@0: * as opposed to streaming. michael@0: * The getPosition() method on the other hand returns the total number of frames played since michael@0: * playback start. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * position: New playback head position within AudioTrack buffer. michael@0: * michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - INVALID_OPERATION: the AudioTrack is not stopped. michael@0: * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack buffer michael@0: */ michael@0: status_t setPosition(uint32_t position); michael@0: status_t getPosition(uint32_t *position); michael@0: michael@0: /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids michael@0: * rewriting the buffer before restarting playback after a stop. michael@0: * This method must be called with the AudioTrack in paused or stopped state. michael@0: * michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - INVALID_OPERATION: the AudioTrack is not stopped. michael@0: */ michael@0: status_t reload(); michael@0: michael@0: /* returns a handle on the audio output used by this AudioTrack. michael@0: * michael@0: * Parameters: michael@0: * none. michael@0: * michael@0: * Returned value: michael@0: * handle on audio hardware output michael@0: */ michael@0: audio_io_handle_t getOutput(); michael@0: michael@0: /* returns the unique ID associated to this track. michael@0: * michael@0: * Parameters: michael@0: * none. michael@0: * michael@0: * Returned value: michael@0: * AudioTrack ID. michael@0: */ michael@0: int getSessionId(); michael@0: michael@0: michael@0: /* Attach track auxiliary output to specified effect. Used effectId = 0 michael@0: * to detach track from effect. michael@0: * michael@0: * Parameters: michael@0: * michael@0: * effectId: effectId obtained from AudioEffect::id(). michael@0: * michael@0: * Returned status (from utils/Errors.h) can be: michael@0: * - NO_ERROR: successful operation michael@0: * - INVALID_OPERATION: the effect is not an auxiliary effect. michael@0: * - BAD_VALUE: The specified effect ID is invalid michael@0: */ michael@0: status_t attachAuxEffect(int effectId); michael@0: michael@0: /* obtains a buffer of "frameCount" frames. The buffer must be michael@0: * filled entirely. If the track is stopped, obtainBuffer() returns michael@0: * STOPPED instead of NO_ERROR as long as there are buffers availlable, michael@0: * at which point NO_MORE_BUFFERS is returned. michael@0: * Buffers will be returned until the pool (buffercount()) michael@0: * is exhausted, at which point obtainBuffer() will either block michael@0: * or return WOULD_BLOCK depending on the value of the "blocking" michael@0: * parameter. michael@0: */ michael@0: michael@0: enum { michael@0: NO_MORE_BUFFERS = 0x80000001, michael@0: STOPPED = 1 michael@0: }; michael@0: michael@0: status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); michael@0: void releaseBuffer(Buffer* audioBuffer); michael@0: michael@0: michael@0: /* As a convenience we provide a write() interface to the audio buffer. michael@0: * This is implemented on top of lockBuffer/unlockBuffer. For best michael@0: * performance michael@0: * michael@0: */ michael@0: ssize_t write(const void* buffer, size_t size); michael@0: michael@0: /* michael@0: * Dumps the state of an audio track. michael@0: */ michael@0: status_t dump(int fd, const Vector& args) const; michael@0: michael@0: private: michael@0: /* copying audio tracks is not allowed */ michael@0: AudioTrack(const AudioTrack& other); michael@0: AudioTrack& operator = (const AudioTrack& other); michael@0: michael@0: /* a small internal class to handle the callback */ michael@0: class AudioTrackThread : public Thread michael@0: { michael@0: public: michael@0: AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false); michael@0: private: michael@0: friend class AudioTrack; michael@0: virtual bool threadLoop(); michael@0: virtual status_t readyToRun(); michael@0: virtual void onFirstRef(); michael@0: AudioTrack& mReceiver; michael@0: Mutex mLock; michael@0: }; michael@0: michael@0: bool processAudioBuffer(const sp& thread); michael@0: status_t createTrack(int streamType, michael@0: uint32_t sampleRate, michael@0: int format, michael@0: int channelCount, michael@0: int frameCount, michael@0: uint32_t flags, michael@0: const sp& sharedBuffer, michael@0: audio_io_handle_t output, michael@0: bool enforceFrameCount); michael@0: michael@0: sp mAudioTrack; michael@0: sp mCblkMemory; michael@0: sp mAudioTrackThread; michael@0: michael@0: float mVolume[2]; michael@0: float mSendLevel; michael@0: uint32_t mFrameCount; michael@0: michael@0: audio_track_cblk_t* mCblk; michael@0: uint8_t mStreamType; michael@0: uint8_t mFormat; michael@0: uint8_t mChannelCount; michael@0: uint8_t mMuted; michael@0: uint32_t mChannels; michael@0: status_t mStatus; michael@0: uint32_t mLatency; michael@0: michael@0: volatile int32_t mActive; michael@0: michael@0: callback_t mCbf; michael@0: void* mUserData; michael@0: uint32_t mNotificationFramesReq; // requested number of frames between each notification callback michael@0: uint32_t mNotificationFramesAct; // actual number of frames between each notification callback michael@0: sp mSharedBuffer; michael@0: int mLoopCount; michael@0: uint32_t mRemainingFrames; michael@0: uint32_t mMarkerPosition; michael@0: bool mMarkerReached; michael@0: uint32_t mNewPosition; michael@0: uint32_t mUpdatePeriod; michael@0: uint32_t mFlags; michael@0: int mSessionId; michael@0: int mAuxEffectId; michael@0: uint32_t mPadding[8]; michael@0: }; michael@0: michael@0: michael@0: }; // namespace android michael@0: michael@0: #endif // ANDROID_AUDIOTRACK_H