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