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