The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* mediaplayer.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, 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 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaPlayer" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | #include <media/mediaplayer.h> |
| 31 | #include <media/AudioTrack.h> |
| 32 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 33 | #include <surfaceflinger/Surface.h> |
| 34 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/MemoryBase.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 37 | #include <utils/KeyedVector.h> |
| 38 | #include <utils/String8.h> |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | MediaPlayer::MediaPlayer() |
| 43 | { |
| 44 | LOGV("constructor"); |
| 45 | mListener = NULL; |
| 46 | mCookie = NULL; |
| 47 | mDuration = -1; |
| 48 | mStreamType = AudioSystem::MUSIC; |
| 49 | mCurrentPosition = -1; |
| 50 | mSeekPosition = -1; |
| 51 | mCurrentState = MEDIA_PLAYER_IDLE; |
| 52 | mPrepareSync = false; |
| 53 | mPrepareStatus = NO_ERROR; |
| 54 | mLoop = false; |
| 55 | mLeftVolume = mRightVolume = 1.0; |
| 56 | mVideoWidth = mVideoHeight = 0; |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 57 | mLockThreadId = 0; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 58 | mAudioSessionId = AudioSystem::newAudioSessionId(); |
Eric Laurent | b3bdf3f | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 59 | mSendLevel = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | MediaPlayer::~MediaPlayer() |
| 63 | { |
| 64 | LOGV("destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | disconnect(); |
| 66 | IPCThreadState::self()->flushCommands(); |
| 67 | } |
| 68 | |
| 69 | void MediaPlayer::disconnect() |
| 70 | { |
| 71 | LOGV("disconnect"); |
| 72 | sp<IMediaPlayer> p; |
| 73 | { |
| 74 | Mutex::Autolock _l(mLock); |
| 75 | p = mPlayer; |
| 76 | mPlayer.clear(); |
| 77 | } |
| 78 | |
| 79 | if (p != 0) { |
| 80 | p->disconnect(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // always call with lock held |
| 85 | void MediaPlayer::clear_l() |
| 86 | { |
| 87 | mDuration = -1; |
| 88 | mCurrentPosition = -1; |
| 89 | mSeekPosition = -1; |
| 90 | mVideoWidth = mVideoHeight = 0; |
| 91 | } |
| 92 | |
| 93 | status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener) |
| 94 | { |
| 95 | LOGV("setListener"); |
| 96 | Mutex::Autolock _l(mLock); |
| 97 | mListener = listener; |
| 98 | return NO_ERROR; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | status_t MediaPlayer::setDataSource(const sp<IMediaPlayer>& player) |
| 103 | { |
| 104 | status_t err = UNKNOWN_ERROR; |
| 105 | sp<IMediaPlayer> p; |
| 106 | { // scope for the lock |
| 107 | Mutex::Autolock _l(mLock); |
| 108 | |
Marco Nelissen | e44b41b | 2010-03-10 10:53:16 -0800 | [diff] [blame] | 109 | if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 110 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | LOGE("setDataSource called in state %d", mCurrentState); |
| 112 | return INVALID_OPERATION; |
| 113 | } |
| 114 | |
| 115 | clear_l(); |
| 116 | p = mPlayer; |
| 117 | mPlayer = player; |
| 118 | if (player != 0) { |
| 119 | mCurrentState = MEDIA_PLAYER_INITIALIZED; |
| 120 | err = NO_ERROR; |
| 121 | } else { |
| 122 | LOGE("Unable to to create media player"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (p != 0) { |
| 127 | p->disconnect(); |
| 128 | } |
| 129 | |
| 130 | return err; |
| 131 | } |
| 132 | |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 133 | status_t MediaPlayer::setDataSource( |
| 134 | const char *url, const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | { |
| 136 | LOGV("setDataSource(%s)", url); |
| 137 | status_t err = BAD_VALUE; |
| 138 | if (url != NULL) { |
| 139 | const sp<IMediaPlayerService>& service(getMediaPlayerService()); |
| 140 | if (service != 0) { |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 141 | sp<IMediaPlayer> player( |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 142 | service->create(getpid(), this, url, headers, mAudioSessionId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | err = setDataSource(player); |
| 144 | } |
| 145 | } |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length) |
| 150 | { |
| 151 | LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length); |
| 152 | status_t err = UNKNOWN_ERROR; |
| 153 | const sp<IMediaPlayerService>& service(getMediaPlayerService()); |
| 154 | if (service != 0) { |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 155 | sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | err = setDataSource(player); |
| 157 | } |
| 158 | return err; |
| 159 | } |
| 160 | |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 161 | status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply) |
| 162 | { |
| 163 | Mutex::Autolock _l(mLock); |
Nicolas Catania | 7bd3d7d | 2010-03-10 10:41:04 -0800 | [diff] [blame] | 164 | const bool hasBeenInitialized = |
| 165 | (mCurrentState != MEDIA_PLAYER_STATE_ERROR) && |
| 166 | ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE); |
| 167 | if ((mPlayer != NULL) && hasBeenInitialized) { |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 168 | LOGV("invoke %d", request.dataSize()); |
| 169 | return mPlayer->invoke(request, reply); |
| 170 | } |
| 171 | LOGE("invoke failed: wrong state %X", mCurrentState); |
| 172 | return INVALID_OPERATION; |
| 173 | } |
| 174 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 175 | status_t MediaPlayer::setMetadataFilter(const Parcel& filter) |
| 176 | { |
| 177 | LOGD("setMetadataFilter"); |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 178 | Mutex::Autolock lock(mLock); |
| 179 | if (mPlayer == NULL) { |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 180 | return NO_INIT; |
| 181 | } |
| 182 | return mPlayer->setMetadataFilter(filter); |
| 183 | } |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 184 | |
Nicolas Catania | 5d55c71 | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 185 | status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata) |
| 186 | { |
| 187 | LOGD("getMetadata"); |
| 188 | Mutex::Autolock lock(mLock); |
| 189 | if (mPlayer == NULL) { |
| 190 | return NO_INIT; |
| 191 | } |
| 192 | return mPlayer->getMetadata(update_only, apply_filter, metadata); |
| 193 | } |
| 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | status_t MediaPlayer::setVideoSurface(const sp<Surface>& surface) |
| 196 | { |
| 197 | LOGV("setVideoSurface"); |
| 198 | Mutex::Autolock _l(mLock); |
| 199 | if (mPlayer == 0) return NO_INIT; |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 200 | |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 201 | return mPlayer->setVideoSurface(surface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // must call with lock held |
| 205 | status_t MediaPlayer::prepareAsync_l() |
| 206 | { |
| 207 | if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) { |
| 208 | mPlayer->setAudioStreamType(mStreamType); |
| 209 | mCurrentState = MEDIA_PLAYER_PREPARING; |
| 210 | return mPlayer->prepareAsync(); |
| 211 | } |
| 212 | LOGE("prepareAsync called in state %d", mCurrentState); |
| 213 | return INVALID_OPERATION; |
| 214 | } |
| 215 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 216 | // TODO: In case of error, prepareAsync provides the caller with 2 error codes, |
| 217 | // one defined in the Android framework and one provided by the implementation |
| 218 | // that generated the error. The sync version of prepare returns only 1 error |
| 219 | // code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | status_t MediaPlayer::prepare() |
| 221 | { |
| 222 | LOGV("prepare"); |
| 223 | Mutex::Autolock _l(mLock); |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 224 | mLockThreadId = getThreadId(); |
| 225 | if (mPrepareSync) { |
| 226 | mLockThreadId = 0; |
| 227 | return -EALREADY; |
| 228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | mPrepareSync = true; |
| 230 | status_t ret = prepareAsync_l(); |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 231 | if (ret != NO_ERROR) { |
| 232 | mLockThreadId = 0; |
| 233 | return ret; |
| 234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | |
| 236 | if (mPrepareSync) { |
| 237 | mSignal.wait(mLock); // wait for prepare done |
| 238 | mPrepareSync = false; |
| 239 | } |
| 240 | LOGV("prepare complete - status=%d", mPrepareStatus); |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 241 | mLockThreadId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | return mPrepareStatus; |
| 243 | } |
| 244 | |
| 245 | status_t MediaPlayer::prepareAsync() |
| 246 | { |
| 247 | LOGV("prepareAsync"); |
| 248 | Mutex::Autolock _l(mLock); |
| 249 | return prepareAsync_l(); |
| 250 | } |
| 251 | |
| 252 | status_t MediaPlayer::start() |
| 253 | { |
| 254 | LOGV("start"); |
| 255 | Mutex::Autolock _l(mLock); |
| 256 | if (mCurrentState & MEDIA_PLAYER_STARTED) |
| 257 | return NO_ERROR; |
| 258 | if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED | |
| 259 | MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) { |
| 260 | mPlayer->setLooping(mLoop); |
| 261 | mPlayer->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 262 | mPlayer->setAuxEffectSendLevel(mSendLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | mCurrentState = MEDIA_PLAYER_STARTED; |
| 264 | status_t ret = mPlayer->start(); |
| 265 | if (ret != NO_ERROR) { |
| 266 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 267 | } else { |
| 268 | if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) { |
| 269 | LOGV("playback completed immediately following start()"); |
| 270 | } |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | LOGE("start called in state %d", mCurrentState); |
| 275 | return INVALID_OPERATION; |
| 276 | } |
| 277 | |
| 278 | status_t MediaPlayer::stop() |
| 279 | { |
| 280 | LOGV("stop"); |
| 281 | Mutex::Autolock _l(mLock); |
| 282 | if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR; |
| 283 | if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | |
| 284 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) { |
| 285 | status_t ret = mPlayer->stop(); |
| 286 | if (ret != NO_ERROR) { |
| 287 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 288 | } else { |
| 289 | mCurrentState = MEDIA_PLAYER_STOPPED; |
| 290 | } |
| 291 | return ret; |
| 292 | } |
| 293 | LOGE("stop called in state %d", mCurrentState); |
| 294 | return INVALID_OPERATION; |
| 295 | } |
| 296 | |
| 297 | status_t MediaPlayer::pause() |
| 298 | { |
| 299 | LOGV("pause"); |
| 300 | Mutex::Autolock _l(mLock); |
Marco Nelissen | 465faa9 | 2010-02-26 13:16:23 -0800 | [diff] [blame] | 301 | if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE)) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | return NO_ERROR; |
| 303 | if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) { |
| 304 | status_t ret = mPlayer->pause(); |
| 305 | if (ret != NO_ERROR) { |
| 306 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 307 | } else { |
| 308 | mCurrentState = MEDIA_PLAYER_PAUSED; |
| 309 | } |
| 310 | return ret; |
| 311 | } |
| 312 | LOGE("pause called in state %d", mCurrentState); |
| 313 | return INVALID_OPERATION; |
| 314 | } |
| 315 | |
| 316 | bool MediaPlayer::isPlaying() |
| 317 | { |
| 318 | Mutex::Autolock _l(mLock); |
| 319 | if (mPlayer != 0) { |
| 320 | bool temp = false; |
| 321 | mPlayer->isPlaying(&temp); |
| 322 | LOGV("isPlaying: %d", temp); |
| 323 | if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) { |
| 324 | LOGE("internal/external state mismatch corrected"); |
| 325 | mCurrentState = MEDIA_PLAYER_PAUSED; |
| 326 | } |
| 327 | return temp; |
| 328 | } |
| 329 | LOGV("isPlaying: no active player"); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | status_t MediaPlayer::getVideoWidth(int *w) |
| 334 | { |
| 335 | LOGV("getVideoWidth"); |
| 336 | Mutex::Autolock _l(mLock); |
| 337 | if (mPlayer == 0) return INVALID_OPERATION; |
| 338 | *w = mVideoWidth; |
| 339 | return NO_ERROR; |
| 340 | } |
| 341 | |
| 342 | status_t MediaPlayer::getVideoHeight(int *h) |
| 343 | { |
| 344 | LOGV("getVideoHeight"); |
| 345 | Mutex::Autolock _l(mLock); |
| 346 | if (mPlayer == 0) return INVALID_OPERATION; |
| 347 | *h = mVideoHeight; |
| 348 | return NO_ERROR; |
| 349 | } |
| 350 | |
| 351 | status_t MediaPlayer::getCurrentPosition(int *msec) |
| 352 | { |
| 353 | LOGV("getCurrentPosition"); |
| 354 | Mutex::Autolock _l(mLock); |
| 355 | if (mPlayer != 0) { |
| 356 | if (mCurrentPosition >= 0) { |
| 357 | LOGV("Using cached seek position: %d", mCurrentPosition); |
| 358 | *msec = mCurrentPosition; |
| 359 | return NO_ERROR; |
| 360 | } |
| 361 | return mPlayer->getCurrentPosition(msec); |
| 362 | } |
| 363 | return INVALID_OPERATION; |
| 364 | } |
| 365 | |
| 366 | status_t MediaPlayer::getDuration_l(int *msec) |
| 367 | { |
| 368 | LOGV("getDuration"); |
| 369 | bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE)); |
| 370 | if (mPlayer != 0 && isValidState) { |
| 371 | status_t ret = NO_ERROR; |
| 372 | if (mDuration <= 0) |
| 373 | ret = mPlayer->getDuration(&mDuration); |
| 374 | if (msec) |
| 375 | *msec = mDuration; |
| 376 | return ret; |
| 377 | } |
| 378 | LOGE("Attempt to call getDuration without a valid mediaplayer"); |
| 379 | return INVALID_OPERATION; |
| 380 | } |
| 381 | |
| 382 | status_t MediaPlayer::getDuration(int *msec) |
| 383 | { |
| 384 | Mutex::Autolock _l(mLock); |
| 385 | return getDuration_l(msec); |
| 386 | } |
| 387 | |
| 388 | status_t MediaPlayer::seekTo_l(int msec) |
| 389 | { |
| 390 | LOGV("seekTo %d", msec); |
| 391 | if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) { |
| 392 | if ( msec < 0 ) { |
| 393 | LOGW("Attempt to seek to invalid position: %d", msec); |
| 394 | msec = 0; |
| 395 | } else if ((mDuration > 0) && (msec > mDuration)) { |
| 396 | LOGW("Attempt to seek to past end of file: request = %d, EOF = %d", msec, mDuration); |
| 397 | msec = mDuration; |
| 398 | } |
| 399 | // cache duration |
| 400 | mCurrentPosition = msec; |
| 401 | if (mSeekPosition < 0) { |
| 402 | getDuration_l(NULL); |
| 403 | mSeekPosition = msec; |
| 404 | return mPlayer->seekTo(msec); |
| 405 | } |
| 406 | else { |
| 407 | LOGV("Seek in progress - queue up seekTo[%d]", msec); |
| 408 | return NO_ERROR; |
| 409 | } |
| 410 | } |
| 411 | LOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState); |
| 412 | return INVALID_OPERATION; |
| 413 | } |
| 414 | |
| 415 | status_t MediaPlayer::seekTo(int msec) |
| 416 | { |
Andreas Huber | effd8d5 | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 417 | mLockThreadId = getThreadId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | Mutex::Autolock _l(mLock); |
Andreas Huber | effd8d5 | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 419 | status_t result = seekTo_l(msec); |
| 420 | mLockThreadId = 0; |
| 421 | |
| 422 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | status_t MediaPlayer::reset() |
| 426 | { |
| 427 | LOGV("reset"); |
| 428 | Mutex::Autolock _l(mLock); |
| 429 | mLoop = false; |
| 430 | if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR; |
| 431 | mPrepareSync = false; |
| 432 | if (mPlayer != 0) { |
| 433 | status_t ret = mPlayer->reset(); |
| 434 | if (ret != NO_ERROR) { |
| 435 | LOGE("reset() failed with return code (%d)", ret); |
| 436 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 437 | } else { |
| 438 | mCurrentState = MEDIA_PLAYER_IDLE; |
| 439 | } |
James Dong | c5bba6c | 2010-11-18 12:23:58 -0800 | [diff] [blame] | 440 | // setDataSource has to be called again to create a |
| 441 | // new mediaplayer. |
| 442 | mPlayer = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | clear_l(); |
| 446 | return NO_ERROR; |
| 447 | } |
| 448 | |
| 449 | status_t MediaPlayer::setAudioStreamType(int type) |
| 450 | { |
| 451 | LOGV("MediaPlayer::setAudioStreamType"); |
| 452 | Mutex::Autolock _l(mLock); |
| 453 | if (mStreamType == type) return NO_ERROR; |
| 454 | if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | |
| 455 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) { |
| 456 | // Can't change the stream type after prepare |
| 457 | LOGE("setAudioStream called in state %d", mCurrentState); |
| 458 | return INVALID_OPERATION; |
| 459 | } |
| 460 | // cache |
| 461 | mStreamType = type; |
| 462 | return OK; |
| 463 | } |
| 464 | |
| 465 | status_t MediaPlayer::setLooping(int loop) |
| 466 | { |
| 467 | LOGV("MediaPlayer::setLooping"); |
| 468 | Mutex::Autolock _l(mLock); |
| 469 | mLoop = (loop != 0); |
| 470 | if (mPlayer != 0) { |
| 471 | return mPlayer->setLooping(loop); |
| 472 | } |
| 473 | return OK; |
| 474 | } |
| 475 | |
| 476 | bool MediaPlayer::isLooping() { |
| 477 | LOGV("isLooping"); |
| 478 | Mutex::Autolock _l(mLock); |
| 479 | if (mPlayer != 0) { |
| 480 | return mLoop; |
| 481 | } |
| 482 | LOGV("isLooping: no active player"); |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | status_t MediaPlayer::setVolume(float leftVolume, float rightVolume) |
| 487 | { |
| 488 | LOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume); |
| 489 | Mutex::Autolock _l(mLock); |
| 490 | mLeftVolume = leftVolume; |
| 491 | mRightVolume = rightVolume; |
| 492 | if (mPlayer != 0) { |
| 493 | return mPlayer->setVolume(leftVolume, rightVolume); |
| 494 | } |
| 495 | return OK; |
| 496 | } |
| 497 | |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 498 | status_t MediaPlayer::setAudioSessionId(int sessionId) |
| 499 | { |
| 500 | LOGV("MediaPlayer::setAudioSessionId(%d)", sessionId); |
| 501 | Mutex::Autolock _l(mLock); |
| 502 | if (!(mCurrentState & MEDIA_PLAYER_IDLE)) { |
| 503 | LOGE("setAudioSessionId called in state %d", mCurrentState); |
| 504 | return INVALID_OPERATION; |
| 505 | } |
| 506 | if (sessionId < 0) { |
| 507 | return BAD_VALUE; |
| 508 | } |
| 509 | mAudioSessionId = sessionId; |
| 510 | return NO_ERROR; |
| 511 | } |
| 512 | |
| 513 | int MediaPlayer::getAudioSessionId() |
| 514 | { |
| 515 | Mutex::Autolock _l(mLock); |
| 516 | return mAudioSessionId; |
| 517 | } |
| 518 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 519 | status_t MediaPlayer::setAuxEffectSendLevel(float level) |
| 520 | { |
| 521 | LOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level); |
| 522 | Mutex::Autolock _l(mLock); |
| 523 | mSendLevel = level; |
| 524 | if (mPlayer != 0) { |
| 525 | return mPlayer->setAuxEffectSendLevel(level); |
| 526 | } |
| 527 | return OK; |
| 528 | } |
| 529 | |
| 530 | status_t MediaPlayer::attachAuxEffect(int effectId) |
| 531 | { |
| 532 | LOGV("MediaPlayer::attachAuxEffect(%d)", effectId); |
| 533 | Mutex::Autolock _l(mLock); |
| 534 | if (mPlayer == 0 || |
| 535 | (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 536 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) { |
| 537 | LOGE("attachAuxEffect called in state %d", mCurrentState); |
| 538 | return INVALID_OPERATION; |
| 539 | } |
| 540 | |
| 541 | return mPlayer->attachAuxEffect(effectId); |
| 542 | } |
| 543 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | void MediaPlayer::notify(int msg, int ext1, int ext2) |
| 545 | { |
| 546 | LOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2); |
| 547 | bool send = true; |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 548 | bool locked = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | |
| 550 | // TODO: In the future, we might be on the same thread if the app is |
| 551 | // running in the same process as the media server. In that case, |
| 552 | // this will deadlock. |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 553 | // |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 554 | // The threadId hack below works around this for the care of prepare |
Andreas Huber | effd8d5 | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 555 | // and seekTo within the same process. |
| 556 | // FIXME: Remember, this is a hack, it's not even a hack that is applied |
| 557 | // consistently for all use-cases, this needs to be revisited. |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 558 | if (mLockThreadId != getThreadId()) { |
| 559 | mLock.lock(); |
| 560 | locked = true; |
Nicolas Catania | 32f8277 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 561 | } |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 562 | |
Eric Laurent | 1d53b2a | 2010-08-03 07:49:49 -0700 | [diff] [blame] | 563 | // Allows calls from JNI in idle state to notify errors |
| 564 | if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | LOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2); |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 566 | if (locked) mLock.unlock(); // release the lock when done. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
| 570 | switch (msg) { |
| 571 | case MEDIA_NOP: // interface test message |
| 572 | break; |
| 573 | case MEDIA_PREPARED: |
| 574 | LOGV("prepared"); |
| 575 | mCurrentState = MEDIA_PLAYER_PREPARED; |
| 576 | if (mPrepareSync) { |
| 577 | LOGV("signal application thread"); |
| 578 | mPrepareSync = false; |
| 579 | mPrepareStatus = NO_ERROR; |
| 580 | mSignal.signal(); |
| 581 | } |
| 582 | break; |
| 583 | case MEDIA_PLAYBACK_COMPLETE: |
| 584 | LOGV("playback complete"); |
Marco Nelissen | 3c473ea | 2010-09-17 15:04:01 -0700 | [diff] [blame] | 585 | if (mCurrentState == MEDIA_PLAYER_IDLE) { |
| 586 | LOGE("playback complete in idle state"); |
| 587 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | if (!mLoop) { |
| 589 | mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE; |
| 590 | } |
| 591 | break; |
| 592 | case MEDIA_ERROR: |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 593 | // Always log errors. |
| 594 | // ext1: Media framework error code. |
| 595 | // ext2: Implementation dependant error code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | LOGE("error (%d, %d)", ext1, ext2); |
| 597 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 598 | if (mPrepareSync) |
| 599 | { |
| 600 | LOGV("signal application thread"); |
| 601 | mPrepareSync = false; |
| 602 | mPrepareStatus = ext1; |
| 603 | mSignal.signal(); |
| 604 | send = false; |
| 605 | } |
| 606 | break; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 607 | case MEDIA_INFO: |
| 608 | // ext1: Media framework error code. |
| 609 | // ext2: Implementation dependant error code. |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 610 | if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) { |
| 611 | LOGW("info/warning (%d, %d)", ext1, ext2); |
| 612 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 613 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | case MEDIA_SEEK_COMPLETE: |
| 615 | LOGV("Received seek complete"); |
| 616 | if (mSeekPosition != mCurrentPosition) { |
| 617 | LOGV("Executing queued seekTo(%d)", mSeekPosition); |
| 618 | mSeekPosition = -1; |
| 619 | seekTo_l(mCurrentPosition); |
| 620 | } |
| 621 | else { |
| 622 | LOGV("All seeks complete - return to regularly scheduled program"); |
| 623 | mCurrentPosition = mSeekPosition = -1; |
| 624 | } |
| 625 | break; |
| 626 | case MEDIA_BUFFERING_UPDATE: |
| 627 | LOGV("buffering %d", ext1); |
| 628 | break; |
| 629 | case MEDIA_SET_VIDEO_SIZE: |
| 630 | LOGV("New video size %d x %d", ext1, ext2); |
| 631 | mVideoWidth = ext1; |
| 632 | mVideoHeight = ext2; |
| 633 | break; |
| 634 | default: |
| 635 | LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | sp<MediaPlayerListener> listener = mListener; |
Jason Sams | ebb020a | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 640 | if (locked) mLock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | |
| 642 | // this prevents re-entrant calls into client code |
| 643 | if ((listener != 0) && send) { |
| 644 | Mutex::Autolock _l(mNotifyLock); |
| 645 | LOGV("callback application"); |
| 646 | listener->notify(msg, ext1, ext2); |
| 647 | LOGV("back from callback"); |
| 648 | } |
| 649 | } |
| 650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | /*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 652 | { |
| 653 | LOGV("decode(%s)", url); |
| 654 | sp<IMemory> p; |
| 655 | const sp<IMediaPlayerService>& service = getMediaPlayerService(); |
| 656 | if (service != 0) { |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 657 | p = service->decode(url, pSampleRate, pNumChannels, pFormat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | } else { |
| 659 | LOGE("Unable to locate media service"); |
| 660 | } |
| 661 | return p; |
| 662 | |
| 663 | } |
| 664 | |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 665 | void MediaPlayer::died() |
| 666 | { |
| 667 | LOGV("died"); |
| 668 | notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0); |
| 669 | } |
| 670 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | /*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat) |
| 672 | { |
| 673 | LOGV("decode(%d, %lld, %lld)", fd, offset, length); |
| 674 | sp<IMemory> p; |
| 675 | const sp<IMediaPlayerService>& service = getMediaPlayerService(); |
| 676 | if (service != 0) { |
James Dong | 34bbc22 | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 677 | p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } else { |
| 679 | LOGE("Unable to locate media service"); |
| 680 | } |
| 681 | return p; |
| 682 | |
| 683 | } |
| 684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | }; // namespace android |