The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* //device/include/server/AudioFlinger/AudioFlinger.h |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_AUDIO_FLINGER_H |
| 19 | #define ANDROID_AUDIO_FLINGER_H |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <media/IAudioFlinger.h> |
| 25 | #include <media/IAudioTrack.h> |
| 26 | #include <media/IAudioRecord.h> |
| 27 | #include <media/AudioTrack.h> |
| 28 | |
| 29 | #include <utils/Atomic.h> |
| 30 | #include <utils/Errors.h> |
| 31 | #include <utils/threads.h> |
| 32 | #include <utils/MemoryDealer.h> |
| 33 | #include <utils/KeyedVector.h> |
| 34 | #include <utils/SortedVector.h> |
| 35 | |
| 36 | #include <hardware/AudioHardwareInterface.h> |
| 37 | |
| 38 | #include "AudioBufferProvider.h" |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | class audio_track_cblk_t; |
| 43 | class AudioMixer; |
| 44 | class AudioBuffer; |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 49 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 50 | |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
| 53 | class AudioFlinger : public BnAudioFlinger, protected Thread |
| 54 | { |
| 55 | public: |
| 56 | static void instantiate(); |
| 57 | |
| 58 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 59 | |
| 60 | // Thread virtuals |
| 61 | virtual bool threadLoop(); |
| 62 | virtual status_t readyToRun(); |
| 63 | virtual void onFirstRef(); |
| 64 | |
| 65 | // IAudioFlinger interface |
| 66 | virtual sp<IAudioTrack> createTrack( |
| 67 | pid_t pid, |
| 68 | int streamType, |
| 69 | uint32_t sampleRate, |
| 70 | int format, |
| 71 | int channelCount, |
| 72 | int bufferCount, |
| 73 | uint32_t flags); |
| 74 | |
| 75 | virtual uint32_t sampleRate() const; |
| 76 | virtual int channelCount() const; |
| 77 | virtual int format() const; |
| 78 | virtual size_t frameCount() const; |
| 79 | |
| 80 | virtual status_t setMasterVolume(float value); |
| 81 | virtual status_t setMasterMute(bool muted); |
| 82 | |
| 83 | virtual float masterVolume() const; |
| 84 | virtual bool masterMute() const; |
| 85 | |
| 86 | virtual status_t setStreamVolume(int stream, float value); |
| 87 | virtual status_t setStreamMute(int stream, bool muted); |
| 88 | |
| 89 | virtual float streamVolume(int stream) const; |
| 90 | virtual bool streamMute(int stream) const; |
| 91 | |
| 92 | virtual status_t setRouting(int mode, uint32_t routes, uint32_t mask); |
| 93 | virtual uint32_t getRouting(int mode) const; |
| 94 | |
| 95 | virtual status_t setMode(int mode); |
| 96 | virtual int getMode() const; |
| 97 | |
| 98 | virtual status_t setMicMute(bool state); |
| 99 | virtual bool getMicMute() const; |
| 100 | |
| 101 | virtual bool isMusicActive() const; |
| 102 | |
| 103 | virtual status_t setParameter(const char* key, const char* value); |
| 104 | |
| 105 | enum hardware_call_state { |
| 106 | AUDIO_HW_IDLE = 0, |
| 107 | AUDIO_HW_INIT, |
| 108 | AUDIO_HW_OUTPUT_OPEN, |
| 109 | AUDIO_HW_OUTPUT_CLOSE, |
| 110 | AUDIO_HW_INPUT_OPEN, |
| 111 | AUDIO_HW_INPUT_CLOSE, |
| 112 | AUDIO_HW_STANDBY, |
| 113 | AUDIO_HW_SET_MASTER_VOLUME, |
| 114 | AUDIO_HW_GET_ROUTING, |
| 115 | AUDIO_HW_SET_ROUTING, |
| 116 | AUDIO_HW_GET_MODE, |
| 117 | AUDIO_HW_SET_MODE, |
| 118 | AUDIO_HW_GET_MIC_MUTE, |
| 119 | AUDIO_HW_SET_MIC_MUTE, |
| 120 | AUDIO_SET_VOICE_VOLUME, |
| 121 | AUDIO_SET_PARAMETER, |
| 122 | }; |
| 123 | |
| 124 | // record interface |
| 125 | virtual sp<IAudioRecord> openRecord( |
| 126 | pid_t pid, |
| 127 | int streamType, |
| 128 | uint32_t sampleRate, |
| 129 | int format, |
| 130 | int channelCount, |
| 131 | int bufferCount, |
| 132 | uint32_t flags); |
| 133 | |
| 134 | virtual status_t onTransact( |
| 135 | uint32_t code, |
| 136 | const Parcel& data, |
| 137 | Parcel* reply, |
| 138 | uint32_t flags); |
| 139 | |
| 140 | private: |
| 141 | AudioFlinger(); |
| 142 | virtual ~AudioFlinger(); |
| 143 | |
| 144 | // Internal dump utilites. |
| 145 | status_t dumpPermissionDenial(int fd, const Vector<String16>& args); |
| 146 | status_t dumpClients(int fd, const Vector<String16>& args); |
| 147 | status_t dumpTracks(int fd, const Vector<String16>& args); |
| 148 | status_t dumpInternals(int fd, const Vector<String16>& args); |
| 149 | |
| 150 | // --- Client --- |
| 151 | class Client : public RefBase { |
| 152 | public: |
| 153 | Client(const sp<AudioFlinger>& audioFlinger, pid_t pid); |
| 154 | virtual ~Client(); |
| 155 | const sp<MemoryDealer>& heap() const; |
| 156 | pid_t pid() const { return mPid; } |
| 157 | private: |
| 158 | Client(const Client&); |
| 159 | Client& operator = (const Client&); |
| 160 | sp<AudioFlinger> mAudioFlinger; |
| 161 | sp<MemoryDealer> mMemoryDealer; |
| 162 | pid_t mPid; |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | // --- Track --- |
| 167 | class TrackHandle; |
| 168 | class RecordHandle; |
| 169 | class AudioRecordThread; |
| 170 | |
| 171 | // base for record and playback |
| 172 | class TrackBase : public AudioBufferProvider, public RefBase { |
| 173 | |
| 174 | public: |
| 175 | enum track_state { |
| 176 | IDLE, |
| 177 | TERMINATED, |
| 178 | STOPPED, |
| 179 | RESUMING, |
| 180 | ACTIVE, |
| 181 | PAUSING, |
| 182 | PAUSED |
| 183 | }; |
| 184 | |
| 185 | enum track_flags { |
| 186 | STEPSERVER_FAILED = 0x01 // StepServer could not acquire cblk->lock mutex |
| 187 | }; |
| 188 | |
| 189 | TrackBase( const sp<AudioFlinger>& audioFlinger, |
| 190 | const sp<Client>& client, |
| 191 | int streamType, |
| 192 | uint32_t sampleRate, |
| 193 | int format, |
| 194 | int channelCount, |
| 195 | int bufferCount, |
| 196 | int bufferSize); |
| 197 | ~TrackBase(); |
| 198 | |
| 199 | virtual status_t start() = 0; |
| 200 | virtual void stop() = 0; |
| 201 | sp<IMemory> getCblk() const; |
| 202 | |
| 203 | protected: |
| 204 | friend class AudioFlinger; |
| 205 | friend class RecordHandle; |
| 206 | |
| 207 | TrackBase(const TrackBase&); |
| 208 | TrackBase& operator = (const TrackBase&); |
| 209 | |
| 210 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) = 0; |
| 211 | virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); |
| 212 | |
| 213 | audio_track_cblk_t* cblk() const { |
| 214 | return mCblk; |
| 215 | } |
| 216 | |
| 217 | int type() const { |
| 218 | return mStreamType; |
| 219 | } |
| 220 | |
| 221 | int format() const { |
| 222 | return mFormat; |
| 223 | } |
| 224 | |
| 225 | int channelCount() const { |
| 226 | return mChannelCount; |
| 227 | } |
| 228 | |
| 229 | int bufferCount() const { |
| 230 | return mBufferCount; |
| 231 | } |
| 232 | |
| 233 | int sampleRate() const; |
| 234 | |
| 235 | void* getBuffer(int n) const { |
| 236 | return (char*)mBuffers + n * mBufferSize; |
| 237 | } |
| 238 | |
| 239 | int name() const { |
| 240 | return mName; |
| 241 | } |
| 242 | |
| 243 | bool isStopped() const { |
| 244 | return mState == STOPPED; |
| 245 | } |
| 246 | |
| 247 | bool isTerminated() const { |
| 248 | return mState == TERMINATED; |
| 249 | } |
| 250 | |
| 251 | bool step(); |
| 252 | void reset(); |
| 253 | |
| 254 | sp<AudioFlinger> mAudioFlinger; |
| 255 | sp<Client> mClient; |
| 256 | sp<IMemory> mCblkMemory; |
| 257 | audio_track_cblk_t* mCblk; |
| 258 | int mStreamType; |
| 259 | uint8_t mFormat; |
| 260 | uint8_t mChannelCount; |
| 261 | uint8_t mBufferCount; |
| 262 | uint8_t mFlags; |
| 263 | void* mBuffers; |
| 264 | size_t mBufferSize; |
| 265 | int mName; |
| 266 | // we don't really need a lock for these |
| 267 | int mState; |
| 268 | int mClientTid; |
| 269 | }; |
| 270 | |
| 271 | // playback track |
| 272 | class Track : public TrackBase { |
| 273 | public: |
| 274 | Track( const sp<AudioFlinger>& audioFlinger, |
| 275 | const sp<Client>& client, |
| 276 | int streamType, |
| 277 | uint32_t sampleRate, |
| 278 | int format, |
| 279 | int channelCount, |
| 280 | int bufferCount, |
| 281 | int bufferSize); |
| 282 | ~Track(); |
| 283 | |
| 284 | void dump(char* buffer, size_t size); |
| 285 | virtual status_t start(); |
| 286 | virtual void stop(); |
| 287 | void pause(); |
| 288 | |
| 289 | void flush(); |
| 290 | void destroy(); |
| 291 | void mute(bool); |
| 292 | void setVolume(float left, float right); |
| 293 | |
| 294 | private: |
| 295 | friend class AudioFlinger; |
| 296 | friend class TrackHandle; |
| 297 | |
| 298 | Track(const Track&); |
| 299 | Track& operator = (const Track&); |
| 300 | |
| 301 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); |
| 302 | |
| 303 | bool isMuted() const { |
| 304 | return mMute; |
| 305 | } |
| 306 | |
| 307 | bool isPausing() const { |
| 308 | return mState == PAUSING; |
| 309 | } |
| 310 | |
| 311 | bool isPaused() const { |
| 312 | return mState == PAUSED; |
| 313 | } |
| 314 | |
| 315 | bool isReady(uint32_t u, int32_t s) const; |
| 316 | |
| 317 | void setPaused() { mState = PAUSED; } |
| 318 | void reset(); |
| 319 | |
| 320 | // we don't really need a lock for these |
| 321 | float mVolume[2]; |
| 322 | volatile bool mMute; |
| 323 | // FILLED state is used for suppressing volume ramp at begin of playing |
| 324 | enum {FS_FILLING, FS_FILLED, FS_ACTIVE}; |
| 325 | mutable uint8_t mFillingUpStatus; |
| 326 | int8_t mRetryCount; |
| 327 | }; // end of Track |
| 328 | |
| 329 | friend class AudioBuffer; |
| 330 | |
| 331 | class TrackHandle : public android::BnAudioTrack { |
| 332 | public: |
| 333 | TrackHandle(const sp<Track>& track); |
| 334 | virtual ~TrackHandle(); |
| 335 | virtual status_t start(); |
| 336 | virtual void stop(); |
| 337 | virtual void flush(); |
| 338 | virtual void mute(bool); |
| 339 | virtual void pause(); |
| 340 | virtual void setVolume(float left, float right); |
| 341 | virtual sp<IMemory> getCblk() const; |
| 342 | virtual status_t onTransact( |
| 343 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 344 | private: |
| 345 | sp<Track> mTrack; |
| 346 | }; |
| 347 | |
| 348 | struct stream_type_t { |
| 349 | stream_type_t() |
| 350 | : volume(1.0f), |
| 351 | mute(false) |
| 352 | { |
| 353 | } |
| 354 | float volume; |
| 355 | bool mute; |
| 356 | }; |
| 357 | |
| 358 | friend class Client; |
| 359 | friend class Track; |
| 360 | |
| 361 | |
| 362 | void removeClient(pid_t pid); |
| 363 | |
| 364 | status_t addTrack(const sp<Track>& track); |
| 365 | void removeTrack(wp<Track> track, int name); |
| 366 | void remove_track_l(wp<Track> track, int name); |
| 367 | void destroyTrack(const sp<Track>& track); |
| 368 | |
| 369 | AudioMixer& audioMixer() { |
| 370 | return *mAudioMixer; |
| 371 | } |
| 372 | |
| 373 | // record track |
| 374 | class RecordTrack : public TrackBase { |
| 375 | public: |
| 376 | RecordTrack( const sp<AudioFlinger>& audioFlinger, |
| 377 | const sp<Client>& client, |
| 378 | int streamType, |
| 379 | uint32_t sampleRate, |
| 380 | int format, |
| 381 | int channelCount, |
| 382 | int bufferCount, |
| 383 | int bufferSize); |
| 384 | ~RecordTrack(); |
| 385 | |
| 386 | virtual status_t start(); |
| 387 | virtual void stop(); |
| 388 | |
| 389 | bool overflow() { bool tmp = mOverflow; mOverflow = false; return tmp; } |
| 390 | bool setOverflow() { bool tmp = mOverflow; mOverflow = true; return tmp; } |
| 391 | |
| 392 | private: |
| 393 | friend class AudioFlinger; |
| 394 | friend class RecordHandle; |
| 395 | friend class AudioRecordThread; |
| 396 | |
| 397 | RecordTrack(const Track&); |
| 398 | RecordTrack& operator = (const Track&); |
| 399 | |
| 400 | virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); |
| 401 | |
| 402 | bool mOverflow; |
| 403 | }; |
| 404 | |
| 405 | class RecordHandle : public android::BnAudioRecord { |
| 406 | public: |
| 407 | RecordHandle(const sp<RecordTrack>& recordTrack); |
| 408 | virtual ~RecordHandle(); |
| 409 | virtual status_t start(); |
| 410 | virtual void stop(); |
| 411 | virtual sp<IMemory> getCblk() const; |
| 412 | virtual status_t onTransact( |
| 413 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); |
| 414 | private: |
| 415 | sp<RecordTrack> mRecordTrack; |
| 416 | }; |
| 417 | |
| 418 | // record thread |
| 419 | class AudioRecordThread : public Thread |
| 420 | { |
| 421 | public: |
| 422 | AudioRecordThread(const sp<AudioFlinger>& audioFlinger); |
| 423 | virtual ~AudioRecordThread(); |
| 424 | virtual bool threadLoop(); |
| 425 | virtual status_t readyToRun() { return NO_ERROR; } |
| 426 | virtual void onFirstRef() {} |
| 427 | |
| 428 | status_t open(const sp<RecordTrack>& recordTrack, AudioStreamIn *input); |
| 429 | status_t start(); |
| 430 | void stop(); |
| 431 | status_t close(); |
| 432 | void exit(); |
| 433 | |
| 434 | bool isOpen() { return bool(mRecordTrack != NULL); } |
| 435 | |
| 436 | private: |
| 437 | AudioRecordThread(); |
| 438 | sp<AudioFlinger> mAudioFlinger; |
| 439 | wp<RecordTrack> mRecordTrack; |
| 440 | AudioStreamIn* mInput; |
| 441 | Mutex mLock; |
| 442 | Condition mWaitWorkCV; |
| 443 | AudioBufferProvider::Buffer mBuffer; |
| 444 | volatile bool mActive; |
| 445 | }; |
| 446 | |
| 447 | friend class AudioRecordThread; |
| 448 | |
| 449 | sp<AudioRecordThread> audioRecordThread(); |
| 450 | void endRecord(); |
| 451 | status_t startRecord(); |
| 452 | void stopRecord(); |
| 453 | void exitRecord(); |
| 454 | |
| 455 | AudioHardwareInterface* audioHardware() { return mAudioHardware; } |
| 456 | |
| 457 | mutable Mutex mHardwareLock; |
| 458 | mutable Mutex mLock; |
| 459 | mutable Condition mWaitWorkCV; |
| 460 | DefaultKeyedVector< pid_t, wp<Client> > mClients; |
| 461 | SortedVector< wp<Track> > mActiveTracks; |
| 462 | SortedVector< sp<Track> > mTracks; |
| 463 | float mMasterVolume; |
| 464 | uint32_t mMasterRouting; |
| 465 | bool mMasterMute; |
| 466 | stream_type_t mStreamTypes[AudioTrack::NUM_STREAM_TYPES]; |
| 467 | |
| 468 | AudioMixer* mAudioMixer; |
| 469 | AudioHardwareInterface* mAudioHardware; |
| 470 | AudioStreamOut* mOutput; |
| 471 | sp<AudioRecordThread> mAudioRecordThread; |
| 472 | uint32_t mSampleRate; |
| 473 | size_t mFrameCount; |
| 474 | int mChannelCount; |
| 475 | int mFormat; |
| 476 | int mMixBufferSize; |
| 477 | int16_t* mMixBuffer; |
| 478 | mutable int mHardwareStatus; |
| 479 | nsecs_t mLastWriteTime; |
| 480 | int mNumWrites; |
| 481 | int mNumDelayedWrites; |
| 482 | bool mStandby; |
| 483 | bool mInWrite; |
| 484 | }; |
| 485 | |
| 486 | // ---------------------------------------------------------------------------- |
| 487 | |
| 488 | }; // namespace android |
| 489 | |
| 490 | #endif // ANDROID_AUDIO_FLINGER_H |