Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaPlayer" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Mathias Agopian | b1e7cd1 | 2013-02-14 17:11:27 -0800 | [diff] [blame] | 30 | #include <gui/Surface.h> |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <media/mediaplayer.h> |
Glenn Kasten | a3f1fa3 | 2012-01-18 14:54:46 -0800 | [diff] [blame] | 33 | #include <media/AudioSystem.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/MemoryBase.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 37 | #include <utils/KeyedVector.h> |
| 38 | #include <utils/String8.h> |
| 39 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 40 | #include <system/audio.h> |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 41 | #include <system/window.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 42 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | MediaPlayer::MediaPlayer() |
| 46 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 47 | ALOGV("constructor"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | mListener = NULL; |
| 49 | mCookie = NULL; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 50 | mStreamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | mCurrentPosition = -1; |
| 52 | mSeekPosition = -1; |
| 53 | mCurrentState = MEDIA_PLAYER_IDLE; |
| 54 | mPrepareSync = false; |
| 55 | mPrepareStatus = NO_ERROR; |
| 56 | mLoop = false; |
| 57 | mLeftVolume = mRightVolume = 1.0; |
| 58 | mVideoWidth = mVideoHeight = 0; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 59 | mLockThreadId = 0; |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 60 | mAudioSessionId = AudioSystem::newAudioSessionId(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 61 | AudioSystem::acquireAudioSessionId(mAudioSessionId); |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 62 | mSendLevel = 0; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 63 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | MediaPlayer::~MediaPlayer() |
| 67 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 68 | ALOGV("destructor"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 69 | AudioSystem::releaseAudioSessionId(mAudioSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | disconnect(); |
| 71 | IPCThreadState::self()->flushCommands(); |
| 72 | } |
| 73 | |
| 74 | void MediaPlayer::disconnect() |
| 75 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 76 | ALOGV("disconnect"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | sp<IMediaPlayer> p; |
| 78 | { |
| 79 | Mutex::Autolock _l(mLock); |
| 80 | p = mPlayer; |
| 81 | mPlayer.clear(); |
| 82 | } |
| 83 | |
| 84 | if (p != 0) { |
| 85 | p->disconnect(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // always call with lock held |
| 90 | void MediaPlayer::clear_l() |
| 91 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | mCurrentPosition = -1; |
| 93 | mSeekPosition = -1; |
| 94 | mVideoWidth = mVideoHeight = 0; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 95 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener) |
| 99 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 100 | ALOGV("setListener"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | Mutex::Autolock _l(mLock); |
| 102 | mListener = listener; |
| 103 | return NO_ERROR; |
| 104 | } |
| 105 | |
| 106 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 107 | status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | { |
| 109 | status_t err = UNKNOWN_ERROR; |
| 110 | sp<IMediaPlayer> p; |
| 111 | { // scope for the lock |
| 112 | Mutex::Autolock _l(mLock); |
| 113 | |
Marco Nelissen | 83ff143 | 2010-03-10 10:53:16 -0800 | [diff] [blame] | 114 | if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 115 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 116 | ALOGE("attachNewPlayer called in state %d", mCurrentState); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | return INVALID_OPERATION; |
| 118 | } |
| 119 | |
| 120 | clear_l(); |
| 121 | p = mPlayer; |
| 122 | mPlayer = player; |
| 123 | if (player != 0) { |
| 124 | mCurrentState = MEDIA_PLAYER_INITIALIZED; |
| 125 | err = NO_ERROR; |
| 126 | } else { |
Masaki Muranaka | f65fa17 | 2013-06-06 09:36:34 +0000 | [diff] [blame] | 127 | ALOGE("Unable to create media player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | if (p != 0) { |
| 132 | p->disconnect(); |
| 133 | } |
| 134 | |
| 135 | return err; |
| 136 | } |
| 137 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 138 | status_t MediaPlayer::setDataSource( |
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 | |
| 534 | status_t MediaPlayer::setLooping(int loop) |
| 535 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 536 | ALOGV("MediaPlayer::setLooping"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | Mutex::Autolock _l(mLock); |
| 538 | mLoop = (loop != 0); |
| 539 | if (mPlayer != 0) { |
| 540 | return mPlayer->setLooping(loop); |
| 541 | } |
| 542 | return OK; |
| 543 | } |
| 544 | |
| 545 | bool MediaPlayer::isLooping() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 546 | ALOGV("isLooping"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | Mutex::Autolock _l(mLock); |
| 548 | if (mPlayer != 0) { |
| 549 | return mLoop; |
| 550 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 551 | ALOGV("isLooping: no active player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | |
| 555 | status_t MediaPlayer::setVolume(float leftVolume, float rightVolume) |
| 556 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 557 | ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | Mutex::Autolock _l(mLock); |
| 559 | mLeftVolume = leftVolume; |
| 560 | mRightVolume = rightVolume; |
| 561 | if (mPlayer != 0) { |
| 562 | return mPlayer->setVolume(leftVolume, rightVolume); |
| 563 | } |
| 564 | return OK; |
| 565 | } |
| 566 | |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 567 | status_t MediaPlayer::setAudioSessionId(int sessionId) |
| 568 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 569 | ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 570 | Mutex::Autolock _l(mLock); |
| 571 | if (!(mCurrentState & MEDIA_PLAYER_IDLE)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 572 | ALOGE("setAudioSessionId called in state %d", mCurrentState); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 573 | return INVALID_OPERATION; |
| 574 | } |
| 575 | if (sessionId < 0) { |
| 576 | return BAD_VALUE; |
| 577 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 578 | if (sessionId != mAudioSessionId) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 579 | AudioSystem::acquireAudioSessionId(sessionId); |
Marco Nelissen | 8bdb01c | 2013-05-10 10:10:42 -0700 | [diff] [blame] | 580 | AudioSystem::releaseAudioSessionId(mAudioSessionId); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 581 | mAudioSessionId = sessionId; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 582 | } |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 583 | return NO_ERROR; |
| 584 | } |
| 585 | |
| 586 | int MediaPlayer::getAudioSessionId() |
| 587 | { |
| 588 | Mutex::Autolock _l(mLock); |
| 589 | return mAudioSessionId; |
| 590 | } |
| 591 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 592 | status_t MediaPlayer::setAuxEffectSendLevel(float level) |
| 593 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 594 | ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 595 | Mutex::Autolock _l(mLock); |
| 596 | mSendLevel = level; |
| 597 | if (mPlayer != 0) { |
| 598 | return mPlayer->setAuxEffectSendLevel(level); |
| 599 | } |
| 600 | return OK; |
| 601 | } |
| 602 | |
| 603 | status_t MediaPlayer::attachAuxEffect(int effectId) |
| 604 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 605 | ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 606 | Mutex::Autolock _l(mLock); |
| 607 | if (mPlayer == 0 || |
| 608 | (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 609 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 610 | ALOGE("attachAuxEffect called in state %d", mCurrentState); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 611 | return INVALID_OPERATION; |
| 612 | } |
| 613 | |
| 614 | return mPlayer->attachAuxEffect(effectId); |
| 615 | } |
| 616 | |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 617 | status_t MediaPlayer::setParameter(int key, const Parcel& request) |
| 618 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 619 | ALOGV("MediaPlayer::setParameter(%d)", key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 620 | Mutex::Autolock _l(mLock); |
| 621 | if (mPlayer != NULL) { |
| 622 | return mPlayer->setParameter(key, request); |
| 623 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 624 | ALOGV("setParameter: no active player"); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 625 | return INVALID_OPERATION; |
| 626 | } |
| 627 | |
| 628 | status_t MediaPlayer::getParameter(int key, Parcel *reply) |
| 629 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 630 | ALOGV("MediaPlayer::getParameter(%d)", key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 631 | Mutex::Autolock _l(mLock); |
| 632 | if (mPlayer != NULL) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 633 | return mPlayer->getParameter(key, reply); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 634 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 635 | ALOGV("getParameter: no active player"); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 636 | return INVALID_OPERATION; |
| 637 | } |
| 638 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 639 | status_t MediaPlayer::setRetransmitEndpoint(const char* addrString, |
| 640 | uint16_t port) { |
| 641 | ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)", |
| 642 | addrString ? addrString : "(null)", port); |
| 643 | |
| 644 | Mutex::Autolock _l(mLock); |
| 645 | if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE)) |
| 646 | return INVALID_OPERATION; |
| 647 | |
| 648 | if (NULL == addrString) { |
| 649 | mRetransmitEndpointValid = false; |
| 650 | return OK; |
| 651 | } |
| 652 | |
| 653 | struct in_addr saddr; |
| 654 | if(!inet_aton(addrString, &saddr)) { |
| 655 | return BAD_VALUE; |
| 656 | } |
| 657 | |
Glenn Kasten | be08f6a | 2013-12-19 09:09:33 -0800 | [diff] [blame] | 658 | memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint)); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 659 | mRetransmitEndpoint.sin_family = AF_INET; |
| 660 | mRetransmitEndpoint.sin_addr = saddr; |
| 661 | mRetransmitEndpoint.sin_port = htons(port); |
| 662 | mRetransmitEndpointValid = true; |
| 663 | |
| 664 | return OK; |
| 665 | } |
| 666 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 667 | 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] | 668 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 669 | 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] | 670 | bool send = true; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 671 | bool locked = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | |
| 673 | // TODO: In the future, we might be on the same thread if the app is |
| 674 | // running in the same process as the media server. In that case, |
| 675 | // this will deadlock. |
Nicolas Catania | 6609518 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 676 | // |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 677 | // The threadId hack below works around this for the care of prepare |
Andreas Huber | 5cb07aa | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 678 | // and seekTo within the same process. |
| 679 | // FIXME: Remember, this is a hack, it's not even a hack that is applied |
| 680 | // consistently for all use-cases, this needs to be revisited. |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 681 | if (mLockThreadId != getThreadId()) { |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 682 | mLock.lock(); |
| 683 | locked = true; |
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 | |
Eric Laurent | 3b26844 | 2010-08-03 07:49:49 -0700 | [diff] [blame] | 686 | // Allows calls from JNI in idle state to notify errors |
| 687 | if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 688 | ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2); |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 689 | if (locked) mLock.unlock(); // release the lock when done. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | |
| 693 | switch (msg) { |
| 694 | case MEDIA_NOP: // interface test message |
| 695 | break; |
| 696 | case MEDIA_PREPARED: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 697 | ALOGV("prepared"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | mCurrentState = MEDIA_PLAYER_PREPARED; |
| 699 | if (mPrepareSync) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 700 | ALOGV("signal application thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | mPrepareSync = false; |
| 702 | mPrepareStatus = NO_ERROR; |
| 703 | mSignal.signal(); |
| 704 | } |
| 705 | break; |
| 706 | case MEDIA_PLAYBACK_COMPLETE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 707 | ALOGV("playback complete"); |
Marco Nelissen | 1c1503c | 2010-09-17 15:04:01 -0700 | [diff] [blame] | 708 | if (mCurrentState == MEDIA_PLAYER_IDLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 709 | ALOGE("playback complete in idle state"); |
Marco Nelissen | 1c1503c | 2010-09-17 15:04:01 -0700 | [diff] [blame] | 710 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | if (!mLoop) { |
| 712 | mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE; |
| 713 | } |
| 714 | break; |
| 715 | case MEDIA_ERROR: |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 716 | // Always log errors. |
| 717 | // ext1: Media framework error code. |
| 718 | // ext2: Implementation dependant error code. |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 719 | ALOGE("error (%d, %d)", ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 721 | if (mPrepareSync) |
| 722 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 723 | ALOGV("signal application thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | mPrepareSync = false; |
| 725 | mPrepareStatus = ext1; |
| 726 | mSignal.signal(); |
| 727 | send = false; |
| 728 | } |
| 729 | break; |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 730 | case MEDIA_INFO: |
| 731 | // ext1: Media framework error code. |
| 732 | // ext2: Implementation dependant error code. |
Andreas Huber | 145e68f | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 733 | if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 734 | ALOGW("info/warning (%d, %d)", ext1, ext2); |
Andreas Huber | 145e68f | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 735 | } |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 736 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | case MEDIA_SEEK_COMPLETE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 738 | ALOGV("Received seek complete"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | if (mSeekPosition != mCurrentPosition) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 740 | ALOGV("Executing queued seekTo(%d)", mSeekPosition); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | mSeekPosition = -1; |
| 742 | seekTo_l(mCurrentPosition); |
| 743 | } |
| 744 | else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 745 | ALOGV("All seeks complete - return to regularly scheduled program"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | mCurrentPosition = mSeekPosition = -1; |
| 747 | } |
| 748 | break; |
| 749 | case MEDIA_BUFFERING_UPDATE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 750 | ALOGV("buffering %d", ext1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | break; |
| 752 | case MEDIA_SET_VIDEO_SIZE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 753 | ALOGV("New video size %d x %d", ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | mVideoWidth = ext1; |
| 755 | mVideoHeight = ext2; |
| 756 | break; |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 757 | case MEDIA_TIMED_TEXT: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 758 | ALOGV("Received timed text message"); |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 759 | break; |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 760 | case MEDIA_SUBTITLE_DATA: |
| 761 | ALOGV("Received subtitle data message"); |
| 762 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | default: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 764 | ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | |
| 768 | sp<MediaPlayerListener> listener = mListener; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 769 | if (locked) mLock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | |
| 771 | // this prevents re-entrant calls into client code |
| 772 | if ((listener != 0) && send) { |
| 773 | Mutex::Autolock _l(mNotifyLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 774 | ALOGV("callback application"); |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 775 | listener->notify(msg, ext1, ext2, obj); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 776 | ALOGV("back from callback"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame^] | 780 | /*static*/ status_t MediaPlayer::decode( |
| 781 | const sp<IMediaHTTPService> &httpService, |
| 782 | const char* url, |
| 783 | uint32_t *pSampleRate, |
| 784 | int* pNumChannels, |
| 785 | audio_format_t* pFormat, |
| 786 | const sp<IMemoryHeap>& heap, |
| 787 | size_t *pSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 789 | ALOGV("decode(%s)", url); |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 790 | status_t status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | const sp<IMediaPlayerService>& service = getMediaPlayerService(); |
| 792 | if (service != 0) { |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame^] | 793 | 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] | 794 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 795 | ALOGE("Unable to locate media service"); |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 796 | status = DEAD_OBJECT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | } |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 798 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | |
| 800 | } |
| 801 | |
James Dong | dd172fc | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 802 | void MediaPlayer::died() |
| 803 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 804 | ALOGV("died"); |
James Dong | dd172fc | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 805 | notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0); |
| 806 | } |
| 807 | |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 808 | /*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length, |
| 809 | uint32_t *pSampleRate, int* pNumChannels, |
| 810 | audio_format_t* pFormat, |
| 811 | const sp<IMemoryHeap>& heap, size_t *pSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 813 | ALOGV("decode(%d, %lld, %lld)", fd, offset, length); |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 814 | status_t status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | const sp<IMediaPlayerService>& service = getMediaPlayerService(); |
| 816 | if (service != 0) { |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 817 | status = service->decode(fd, offset, length, pSampleRate, |
| 818 | pNumChannels, pFormat, heap, pSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 820 | ALOGE("Unable to locate media service"); |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 821 | status = DEAD_OBJECT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |
Eric Laurent | 3d00aa6 | 2013-09-24 09:53:27 -0700 | [diff] [blame] | 823 | return status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | |
| 825 | } |
| 826 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 827 | status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) { |
| 828 | if (mPlayer == NULL) { |
| 829 | return NO_INIT; |
| 830 | } |
Marco Nelissen | b13820f | 2013-08-05 12:22:43 -0700 | [diff] [blame] | 831 | |
| 832 | if (next != NULL && !(next->mCurrentState & |
| 833 | (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) { |
| 834 | ALOGE("next player is not prepared"); |
| 835 | return INVALID_OPERATION; |
| 836 | } |
| 837 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 838 | return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer); |
| 839 | } |
| 840 | |
Andreas Huber | b7319a7 | 2013-05-29 14:20:52 -0700 | [diff] [blame] | 841 | status_t MediaPlayer::updateProxyConfig( |
| 842 | const char *host, int32_t port, const char *exclusionList) { |
| 843 | const sp<IMediaPlayerService>& service = getMediaPlayerService(); |
| 844 | |
| 845 | if (service != NULL) { |
| 846 | return service->updateProxyConfig(host, port, exclusionList); |
| 847 | } |
| 848 | |
| 849 | return INVALID_OPERATION; |
| 850 | } |
| 851 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | }; // namespace android |