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