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