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