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 |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 19 | #define LOG_TAG "MediaPlayerNative" |
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> |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 35 | #include <media/AudioResamplerPublic.h> |
Glenn Kasten | a3f1fa3 | 2012-01-18 14:54:46 -0800 | [diff] [blame] | 36 | #include <media/AudioSystem.h> |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 37 | #include <media/AVSyncSettings.h> |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 38 | #include <media/IDataSource.h> |
Ray Essick | db12214 | 2017-01-25 17:51:55 -0800 | [diff] [blame] | 39 | #include <media/MediaAnalyticsItem.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 41 | #include <binder/MemoryBase.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 43 | #include <utils/KeyedVector.h> |
| 44 | #include <utils/String8.h> |
| 45 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 47 | #include <system/window.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | MediaPlayer::MediaPlayer() |
| 52 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 53 | ALOGV("constructor"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | mListener = NULL; |
| 55 | mCookie = NULL; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 56 | mStreamType = AUDIO_STREAM_MUSIC; |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 57 | mAudioAttributesParcel = NULL; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | mCurrentPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 59 | mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | mSeekPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 61 | mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | mCurrentState = MEDIA_PLAYER_IDLE; |
| 63 | mPrepareSync = false; |
| 64 | mPrepareStatus = NO_ERROR; |
| 65 | mLoop = false; |
| 66 | mLeftVolume = mRightVolume = 1.0; |
| 67 | mVideoWidth = mVideoHeight = 0; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 68 | mLockThreadId = 0; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 69 | mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 70 | AudioSystem::acquireAudioSessionId(mAudioSessionId, -1); |
Eric Laurent | 8c563ed | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 71 | mSendLevel = 0; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 72 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | MediaPlayer::~MediaPlayer() |
| 76 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 77 | ALOGV("destructor"); |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 78 | if (mAudioAttributesParcel != NULL) { |
| 79 | delete mAudioAttributesParcel; |
| 80 | mAudioAttributesParcel = NULL; |
| 81 | } |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 82 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | disconnect(); |
| 84 | IPCThreadState::self()->flushCommands(); |
| 85 | } |
| 86 | |
| 87 | void MediaPlayer::disconnect() |
| 88 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 89 | ALOGV("disconnect"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | sp<IMediaPlayer> p; |
| 91 | { |
| 92 | Mutex::Autolock _l(mLock); |
| 93 | p = mPlayer; |
| 94 | mPlayer.clear(); |
| 95 | } |
| 96 | |
| 97 | if (p != 0) { |
| 98 | p->disconnect(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // always call with lock held |
| 103 | void MediaPlayer::clear_l() |
| 104 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | mCurrentPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 106 | mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | mSeekPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 108 | mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | mVideoWidth = mVideoHeight = 0; |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 110 | mRetransmitEndpointValid = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener) |
| 114 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 115 | ALOGV("setListener"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | Mutex::Autolock _l(mLock); |
| 117 | mListener = listener; |
| 118 | return NO_ERROR; |
| 119 | } |
| 120 | |
| 121 | |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 122 | status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | { |
| 124 | status_t err = UNKNOWN_ERROR; |
| 125 | sp<IMediaPlayer> p; |
| 126 | { // scope for the lock |
| 127 | Mutex::Autolock _l(mLock); |
| 128 | |
Marco Nelissen | 83ff143 | 2010-03-10 10:53:16 -0800 | [diff] [blame] | 129 | if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 130 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 131 | ALOGE("attachNewPlayer called in state %d", mCurrentState); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | return INVALID_OPERATION; |
| 133 | } |
| 134 | |
| 135 | clear_l(); |
| 136 | p = mPlayer; |
| 137 | mPlayer = player; |
| 138 | if (player != 0) { |
| 139 | mCurrentState = MEDIA_PLAYER_INITIALIZED; |
Wei Jia | ed32086 | 2017-01-18 14:03:40 -0800 | [diff] [blame] | 140 | player->getDefaultBufferingSettings(&mCurrentBufferingSettings); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | err = NO_ERROR; |
| 142 | } else { |
Wei Jia | ed32086 | 2017-01-18 14:03:40 -0800 | [diff] [blame] | 143 | mCurrentBufferingSettings = BufferingSettings(); |
Masaki Muranaka | f65fa17 | 2013-06-06 09:36:34 +0000 | [diff] [blame] | 144 | ALOGE("Unable to create media player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | if (p != 0) { |
| 149 | p->disconnect(); |
| 150 | } |
| 151 | |
| 152 | return err; |
| 153 | } |
| 154 | |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 155 | status_t MediaPlayer::setDataSource( |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 156 | const sp<IMediaHTTPService> &httpService, |
Andreas Huber | 2db8455 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 157 | const char *url, const KeyedVector<String8, String8> *headers) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 159 | ALOGV("setDataSource(%s)", url); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | status_t err = BAD_VALUE; |
| 161 | if (url != NULL) { |
Marco Nelissen | fc908d0 | 2016-06-07 12:26:43 -0700 | [diff] [blame] | 162 | const sp<IMediaPlayerService> service(getMediaPlayerService()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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)) || |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 166 | (NO_ERROR != player->setDataSource(httpService, url, headers))) { |
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); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length) |
| 176 | { |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 177 | ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | status_t err = UNKNOWN_ERROR; |
Marco Nelissen | fc908d0 | 2016-06-07 12:26:43 -0700 | [diff] [blame] | 179 | const sp<IMediaPlayerService> service(getMediaPlayerService()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | if (service != 0) { |
Glenn Kasten | f37971f | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 181 | sp<IMediaPlayer> player(service->create(this, mAudioSessionId)); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 182 | if ((NO_ERROR != doSetRetransmitEndpoint(player)) || |
| 183 | (NO_ERROR != player->setDataSource(fd, offset, length))) { |
Dave Burke | 0662067 | 2011-09-06 20:39:47 +0100 | [diff] [blame] | 184 | player.clear(); |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 185 | } |
Dave Burke | 0662067 | 2011-09-06 20:39:47 +0100 | [diff] [blame] | 186 | err = attachNewPlayer(player); |
Dave Burke | d681bbb | 2011-08-30 14:39:17 +0100 | [diff] [blame] | 187 | } |
| 188 | return err; |
| 189 | } |
| 190 | |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 191 | status_t MediaPlayer::setDataSource(const sp<IDataSource> &source) |
| 192 | { |
| 193 | ALOGV("setDataSource(IDataSource)"); |
| 194 | status_t err = UNKNOWN_ERROR; |
Marco Nelissen | fc908d0 | 2016-06-07 12:26:43 -0700 | [diff] [blame] | 195 | const sp<IMediaPlayerService> service(getMediaPlayerService()); |
Chris Watkins | 99f3160 | 2015-03-20 13:06:33 -0700 | [diff] [blame] | 196 | if (service != 0) { |
| 197 | sp<IMediaPlayer> player(service->create(this, mAudioSessionId)); |
| 198 | if ((NO_ERROR != doSetRetransmitEndpoint(player)) || |
| 199 | (NO_ERROR != player->setDataSource(source))) { |
| 200 | player.clear(); |
| 201 | } |
| 202 | err = attachNewPlayer(player); |
| 203 | } |
| 204 | return err; |
| 205 | } |
| 206 | |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 207 | status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply) |
| 208 | { |
| 209 | Mutex::Autolock _l(mLock); |
Nicolas Catania | 4023493 | 2010-03-10 10:41:04 -0800 | [diff] [blame] | 210 | const bool hasBeenInitialized = |
| 211 | (mCurrentState != MEDIA_PLAYER_STATE_ERROR) && |
| 212 | ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE); |
| 213 | if ((mPlayer != NULL) && hasBeenInitialized) { |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 214 | ALOGV("invoke %zu", request.dataSize()); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 215 | return mPlayer->invoke(request, reply); |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 216 | } |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 217 | ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 218 | return INVALID_OPERATION; |
| 219 | } |
| 220 | |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 221 | status_t MediaPlayer::setMetadataFilter(const Parcel& filter) |
| 222 | { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 223 | ALOGD("setMetadataFilter"); |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 224 | Mutex::Autolock lock(mLock); |
| 225 | if (mPlayer == NULL) { |
Nicolas Catania | a7e0e8b | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 226 | return NO_INIT; |
| 227 | } |
| 228 | return mPlayer->setMetadataFilter(filter); |
| 229 | } |
Nicolas Catania | 1d187f1 | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 230 | |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 231 | status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata) |
| 232 | { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 233 | ALOGD("getMetadata"); |
Nicolas Catania | 8e1b6cc | 2009-07-09 09:21:33 -0700 | [diff] [blame] | 234 | Mutex::Autolock lock(mLock); |
| 235 | if (mPlayer == NULL) { |
| 236 | return NO_INIT; |
| 237 | } |
| 238 | return mPlayer->getMetadata(update_only, apply_filter, metadata); |
| 239 | } |
| 240 | |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 241 | status_t MediaPlayer::setVideoSurfaceTexture( |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 242 | const sp<IGraphicBufferProducer>& bufferProducer) |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 243 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("setVideoSurfaceTexture"); |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 245 | Mutex::Autolock _l(mLock); |
| 246 | if (mPlayer == 0) return NO_INIT; |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 247 | return mPlayer->setVideoSurfaceTexture(bufferProducer); |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 250 | status_t MediaPlayer::getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */) |
| 251 | { |
| 252 | ALOGV("getDefaultBufferingSettings"); |
| 253 | |
| 254 | Mutex::Autolock _l(mLock); |
| 255 | if (mPlayer == 0) { |
| 256 | return NO_INIT; |
| 257 | } |
| 258 | return mPlayer->getDefaultBufferingSettings(buffering); |
| 259 | } |
| 260 | |
Wei Jia | ed32086 | 2017-01-18 14:03:40 -0800 | [diff] [blame] | 261 | status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */) |
| 262 | { |
| 263 | ALOGV("getBufferingSettings"); |
| 264 | |
| 265 | Mutex::Autolock _l(mLock); |
| 266 | if (mPlayer == 0) { |
| 267 | return NO_INIT; |
| 268 | } |
| 269 | *buffering = mCurrentBufferingSettings; |
| 270 | return NO_ERROR; |
| 271 | } |
| 272 | |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 273 | status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering) |
| 274 | { |
| 275 | ALOGV("setBufferingSettings"); |
| 276 | |
| 277 | Mutex::Autolock _l(mLock); |
| 278 | if (mPlayer == 0) { |
| 279 | return NO_INIT; |
| 280 | } |
Wei Jia | ed32086 | 2017-01-18 14:03:40 -0800 | [diff] [blame] | 281 | status_t err = mPlayer->setBufferingSettings(buffering); |
| 282 | if (err == NO_ERROR) { |
| 283 | mCurrentBufferingSettings = buffering; |
| 284 | } |
| 285 | return err; |
Wei Jia | dc6f340 | 2017-01-09 15:04:18 -0800 | [diff] [blame] | 286 | } |
| 287 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | // must call with lock held |
| 289 | status_t MediaPlayer::prepareAsync_l() |
| 290 | { |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 291 | if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) { |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 292 | if (mAudioAttributesParcel != NULL) { |
| 293 | mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 294 | } else { |
| 295 | mPlayer->setAudioStreamType(mStreamType); |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 296 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | mCurrentState = MEDIA_PLAYER_PREPARING; |
| 298 | return mPlayer->prepareAsync(); |
| 299 | } |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 300 | ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | return INVALID_OPERATION; |
| 302 | } |
| 303 | |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 304 | // TODO: In case of error, prepareAsync provides the caller with 2 error codes, |
| 305 | // one defined in the Android framework and one provided by the implementation |
| 306 | // that generated the error. The sync version of prepare returns only 1 error |
| 307 | // code. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | status_t MediaPlayer::prepare() |
| 309 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 310 | ALOGV("prepare"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | Mutex::Autolock _l(mLock); |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 312 | mLockThreadId = getThreadId(); |
| 313 | if (mPrepareSync) { |
| 314 | mLockThreadId = 0; |
| 315 | return -EALREADY; |
| 316 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | mPrepareSync = true; |
| 318 | status_t ret = prepareAsync_l(); |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 319 | if (ret != NO_ERROR) { |
| 320 | mLockThreadId = 0; |
| 321 | return ret; |
| 322 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | |
| 324 | if (mPrepareSync) { |
| 325 | mSignal.wait(mLock); // wait for prepare done |
| 326 | mPrepareSync = false; |
| 327 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 328 | ALOGV("prepare complete - status=%d", mPrepareStatus); |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 329 | mLockThreadId = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | return mPrepareStatus; |
| 331 | } |
| 332 | |
| 333 | status_t MediaPlayer::prepareAsync() |
| 334 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 335 | ALOGV("prepareAsync"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | Mutex::Autolock _l(mLock); |
| 337 | return prepareAsync_l(); |
| 338 | } |
| 339 | |
| 340 | status_t MediaPlayer::start() |
| 341 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 342 | ALOGV("start"); |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 343 | |
| 344 | status_t ret = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | Mutex::Autolock _l(mLock); |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 346 | |
| 347 | mLockThreadId = getThreadId(); |
| 348 | |
| 349 | if (mCurrentState & MEDIA_PLAYER_STARTED) { |
| 350 | ret = NO_ERROR; |
| 351 | } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) { |
| 353 | mPlayer->setLooping(mLoop); |
| 354 | mPlayer->setVolume(mLeftVolume, mRightVolume); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 355 | mPlayer->setAuxEffectSendLevel(mSendLevel); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | mCurrentState = MEDIA_PLAYER_STARTED; |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 357 | ret = mPlayer->start(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | if (ret != NO_ERROR) { |
| 359 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 360 | } else { |
| 361 | if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 362 | ALOGV("playback completed immediately following start()"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 365 | } else { |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 366 | ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 367 | ret = INVALID_OPERATION; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 369 | |
| 370 | mLockThreadId = 0; |
| 371 | |
| 372 | return ret; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | status_t MediaPlayer::stop() |
| 376 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 377 | ALOGV("stop"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | Mutex::Autolock _l(mLock); |
| 379 | if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR; |
| 380 | if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | |
| 381 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) { |
| 382 | status_t ret = mPlayer->stop(); |
| 383 | if (ret != NO_ERROR) { |
| 384 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 385 | } else { |
| 386 | mCurrentState = MEDIA_PLAYER_STOPPED; |
| 387 | } |
| 388 | return ret; |
| 389 | } |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 390 | ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | return INVALID_OPERATION; |
| 392 | } |
| 393 | |
| 394 | status_t MediaPlayer::pause() |
| 395 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 396 | ALOGV("pause"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | Mutex::Autolock _l(mLock); |
Marco Nelissen | 698f476 | 2010-02-26 13:16:23 -0800 | [diff] [blame] | 398 | if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE)) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | return NO_ERROR; |
| 400 | if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) { |
| 401 | status_t ret = mPlayer->pause(); |
| 402 | if (ret != NO_ERROR) { |
| 403 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 404 | } else { |
| 405 | mCurrentState = MEDIA_PLAYER_PAUSED; |
| 406 | } |
| 407 | return ret; |
| 408 | } |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 409 | ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | return INVALID_OPERATION; |
| 411 | } |
| 412 | |
| 413 | bool MediaPlayer::isPlaying() |
| 414 | { |
| 415 | Mutex::Autolock _l(mLock); |
| 416 | if (mPlayer != 0) { |
| 417 | bool temp = false; |
| 418 | mPlayer->isPlaying(&temp); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 419 | ALOGV("isPlaying: %d", temp); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 421 | ALOGE("internal/external state mismatch corrected"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | mCurrentState = MEDIA_PLAYER_PAUSED; |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 423 | } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) { |
| 424 | ALOGE("internal/external state mismatch corrected"); |
| 425 | mCurrentState = MEDIA_PLAYER_STARTED; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | } |
| 427 | return temp; |
| 428 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 429 | ALOGV("isPlaying: no active player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 433 | status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate) |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 434 | { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 435 | ALOGV("setPlaybackSettings: %f %f %d %d", |
| 436 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
| 437 | // Negative speed and pitch does not make sense. Further validation will |
| 438 | // be done by the respective mediaplayers. |
| 439 | if (rate.mSpeed < 0.f || rate.mPitch < 0.f) { |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 440 | return BAD_VALUE; |
| 441 | } |
| 442 | Mutex::Autolock _l(mLock); |
Wei Jia | 5af6a9b | 2016-06-16 12:07:26 -0700 | [diff] [blame] | 443 | if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) { |
| 444 | return INVALID_OPERATION; |
| 445 | } |
Wei Jia | 5be109f | 2016-06-10 16:15:07 -0700 | [diff] [blame] | 446 | |
| 447 | if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED) |
| 448 | && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED |
| 449 | | MEDIA_PLAYER_PLAYBACK_COMPLETE))) { |
| 450 | mPlayer->setLooping(mLoop); |
| 451 | mPlayer->setVolume(mLeftVolume, mRightVolume); |
| 452 | mPlayer->setAuxEffectSendLevel(mSendLevel); |
| 453 | } |
| 454 | |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 455 | status_t err = mPlayer->setPlaybackSettings(rate); |
| 456 | if (err == OK) { |
| 457 | if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) { |
| 458 | mCurrentState = MEDIA_PLAYER_PAUSED; |
Wei Jia | 5be109f | 2016-06-10 16:15:07 -0700 | [diff] [blame] | 459 | } else if (rate.mSpeed != 0.f |
| 460 | && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED |
| 461 | | MEDIA_PLAYER_PLAYBACK_COMPLETE))) { |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 462 | mCurrentState = MEDIA_PLAYER_STARTED; |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 463 | } |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 464 | } |
Lajos Molnar | 3a474aa | 2015-04-24 17:10:07 -0700 | [diff] [blame] | 465 | return err; |
| 466 | } |
| 467 | |
| 468 | status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) |
| 469 | { |
| 470 | Mutex::Autolock _l(mLock); |
| 471 | if (mPlayer == 0) return INVALID_OPERATION; |
| 472 | return mPlayer->getPlaybackSettings(rate); |
| 473 | } |
| 474 | |
| 475 | status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint) |
| 476 | { |
| 477 | ALOGV("setSyncSettings: %u %u %f %f", |
| 478 | sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 479 | Mutex::Autolock _l(mLock); |
| 480 | if (mPlayer == 0) return INVALID_OPERATION; |
| 481 | return mPlayer->setSyncSettings(sync, videoFpsHint); |
| 482 | } |
| 483 | |
| 484 | status_t MediaPlayer::getSyncSettings( |
| 485 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) |
| 486 | { |
| 487 | Mutex::Autolock _l(mLock); |
| 488 | if (mPlayer == 0) return INVALID_OPERATION; |
| 489 | return mPlayer->getSyncSettings(sync, videoFps); |
Wei Jia | 9816016 | 2015-02-04 17:01:11 -0800 | [diff] [blame] | 490 | } |
| 491 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | status_t MediaPlayer::getVideoWidth(int *w) |
| 493 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 494 | ALOGV("getVideoWidth"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | Mutex::Autolock _l(mLock); |
| 496 | if (mPlayer == 0) return INVALID_OPERATION; |
| 497 | *w = mVideoWidth; |
| 498 | return NO_ERROR; |
| 499 | } |
| 500 | |
| 501 | status_t MediaPlayer::getVideoHeight(int *h) |
| 502 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 503 | ALOGV("getVideoHeight"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | Mutex::Autolock _l(mLock); |
| 505 | if (mPlayer == 0) return INVALID_OPERATION; |
| 506 | *h = mVideoHeight; |
| 507 | return NO_ERROR; |
| 508 | } |
| 509 | |
| 510 | status_t MediaPlayer::getCurrentPosition(int *msec) |
| 511 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 512 | ALOGV("getCurrentPosition"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | Mutex::Autolock _l(mLock); |
| 514 | if (mPlayer != 0) { |
| 515 | if (mCurrentPosition >= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 516 | ALOGV("Using cached seek position: %d", mCurrentPosition); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | *msec = mCurrentPosition; |
| 518 | return NO_ERROR; |
| 519 | } |
| 520 | return mPlayer->getCurrentPosition(msec); |
| 521 | } |
| 522 | return INVALID_OPERATION; |
| 523 | } |
| 524 | |
| 525 | status_t MediaPlayer::getDuration_l(int *msec) |
| 526 | { |
Andreas Huber | a4c5bc0 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 527 | ALOGV("getDuration_l"); |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 528 | bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | |
| 529 | 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] | 530 | if (mPlayer != 0 && isValidState) { |
Andreas Huber | a4c5bc0 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 531 | int durationMs; |
| 532 | status_t ret = mPlayer->getDuration(&durationMs); |
Andreas Huber | 2070254 | 2013-04-11 11:07:55 -0700 | [diff] [blame] | 533 | |
| 534 | if (ret != OK) { |
| 535 | // Do not enter error state just because no duration was available. |
| 536 | durationMs = -1; |
| 537 | ret = OK; |
| 538 | } |
| 539 | |
Andreas Huber | a4c5bc0 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 540 | if (msec) { |
| 541 | *msec = durationMs; |
| 542 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | return ret; |
| 544 | } |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 545 | ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u", |
| 546 | mPlayer.get(), mCurrentState); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | return INVALID_OPERATION; |
| 548 | } |
| 549 | |
| 550 | status_t MediaPlayer::getDuration(int *msec) |
| 551 | { |
| 552 | Mutex::Autolock _l(mLock); |
| 553 | return getDuration_l(msec); |
| 554 | } |
| 555 | |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 556 | status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | { |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 558 | ALOGV("seekTo (%d, %d)", msec, mode); |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 559 | if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | |
| 560 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | if ( msec < 0 ) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 562 | ALOGW("Attempt to seek to invalid position: %d", msec); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | msec = 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | } |
Andreas Huber | a4c5bc0 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 565 | |
| 566 | int durationMs; |
| 567 | status_t err = mPlayer->getDuration(&durationMs); |
| 568 | |
| 569 | if (err != OK) { |
| 570 | ALOGW("Stream has no duration and is therefore not seekable."); |
| 571 | return err; |
| 572 | } |
| 573 | |
| 574 | if (msec > durationMs) { |
| 575 | ALOGW("Attempt to seek to past end of file: request = %d, " |
| 576 | "durationMs = %d", |
| 577 | msec, |
| 578 | durationMs); |
| 579 | |
| 580 | msec = durationMs; |
| 581 | } |
| 582 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | // cache duration |
| 584 | mCurrentPosition = msec; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 585 | mCurrentSeekMode = mode; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | if (mSeekPosition < 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | mSeekPosition = msec; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 588 | mSeekMode = mode; |
| 589 | return mPlayer->seekTo(msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | } |
| 591 | else { |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 592 | ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | return NO_ERROR; |
| 594 | } |
| 595 | } |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 596 | ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), |
| 597 | mCurrentState); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | return INVALID_OPERATION; |
| 599 | } |
| 600 | |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 601 | status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | { |
Andreas Huber | 5cb07aa | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 603 | mLockThreadId = getThreadId(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | Mutex::Autolock _l(mLock); |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 605 | status_t result = seekTo_l(msec, mode); |
Andreas Huber | 5cb07aa | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 606 | mLockThreadId = 0; |
| 607 | |
| 608 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 611 | status_t MediaPlayer::reset_l() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | mLoop = false; |
| 614 | if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR; |
| 615 | mPrepareSync = false; |
| 616 | if (mPlayer != 0) { |
| 617 | status_t ret = mPlayer->reset(); |
| 618 | if (ret != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 619 | ALOGE("reset() failed with return code (%d)", ret); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 621 | } else { |
Robert Shih | 2b95bda | 2015-07-22 10:11:51 -0700 | [diff] [blame] | 622 | mPlayer->disconnect(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | mCurrentState = MEDIA_PLAYER_IDLE; |
| 624 | } |
James Dong | a1680bc | 2010-11-18 12:23:58 -0800 | [diff] [blame] | 625 | // setDataSource has to be called again to create a |
| 626 | // new mediaplayer. |
| 627 | mPlayer = 0; |
Wei Jia | ed32086 | 2017-01-18 14:03:40 -0800 | [diff] [blame] | 628 | mCurrentBufferingSettings = BufferingSettings(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | return ret; |
| 630 | } |
| 631 | clear_l(); |
| 632 | return NO_ERROR; |
| 633 | } |
| 634 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 635 | status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) { |
| 636 | Mutex::Autolock _l(mLock); |
| 637 | |
| 638 | if (player == NULL) { |
| 639 | return UNKNOWN_ERROR; |
| 640 | } |
| 641 | |
| 642 | if (mRetransmitEndpointValid) { |
| 643 | return player->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 644 | } |
| 645 | |
| 646 | return OK; |
| 647 | } |
| 648 | |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 649 | status_t MediaPlayer::reset() |
| 650 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 651 | ALOGV("reset"); |
Jamie Gennis | 61c7ef5 | 2011-07-13 12:59:34 -0700 | [diff] [blame] | 652 | Mutex::Autolock _l(mLock); |
| 653 | return reset_l(); |
| 654 | } |
| 655 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 656 | status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 658 | ALOGV("MediaPlayer::setAudioStreamType"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | Mutex::Autolock _l(mLock); |
| 660 | if (mStreamType == type) return NO_ERROR; |
| 661 | if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | |
| 662 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) { |
| 663 | // Can't change the stream type after prepare |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 664 | ALOGE("setAudioStream called in state %d", mCurrentState); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | return INVALID_OPERATION; |
| 666 | } |
| 667 | // cache |
| 668 | mStreamType = type; |
| 669 | return OK; |
| 670 | } |
| 671 | |
John Spurlock | de9453f | 2014-03-19 13:05:45 -0400 | [diff] [blame] | 672 | status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type) |
| 673 | { |
| 674 | ALOGV("getAudioStreamType"); |
| 675 | Mutex::Autolock _l(mLock); |
| 676 | *type = mStreamType; |
| 677 | return OK; |
| 678 | } |
| 679 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | status_t MediaPlayer::setLooping(int loop) |
| 681 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 682 | ALOGV("MediaPlayer::setLooping"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | Mutex::Autolock _l(mLock); |
| 684 | mLoop = (loop != 0); |
| 685 | if (mPlayer != 0) { |
| 686 | return mPlayer->setLooping(loop); |
| 687 | } |
| 688 | return OK; |
| 689 | } |
| 690 | |
| 691 | bool MediaPlayer::isLooping() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 692 | ALOGV("isLooping"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | Mutex::Autolock _l(mLock); |
| 694 | if (mPlayer != 0) { |
| 695 | return mLoop; |
| 696 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 697 | ALOGV("isLooping: no active player"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | return false; |
| 699 | } |
| 700 | |
| 701 | status_t MediaPlayer::setVolume(float leftVolume, float rightVolume) |
| 702 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 703 | ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | Mutex::Autolock _l(mLock); |
| 705 | mLeftVolume = leftVolume; |
| 706 | mRightVolume = rightVolume; |
| 707 | if (mPlayer != 0) { |
| 708 | return mPlayer->setVolume(leftVolume, rightVolume); |
| 709 | } |
| 710 | return OK; |
| 711 | } |
| 712 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 713 | status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId) |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 714 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 715 | ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 716 | Mutex::Autolock _l(mLock); |
| 717 | if (!(mCurrentState & MEDIA_PLAYER_IDLE)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 718 | ALOGE("setAudioSessionId called in state %d", mCurrentState); |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 719 | return INVALID_OPERATION; |
| 720 | } |
| 721 | if (sessionId < 0) { |
| 722 | return BAD_VALUE; |
| 723 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 724 | if (sessionId != mAudioSessionId) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 725 | AudioSystem::acquireAudioSessionId(sessionId, -1); |
| 726 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 727 | mAudioSessionId = sessionId; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 728 | } |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 729 | return NO_ERROR; |
| 730 | } |
| 731 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 732 | audio_session_t MediaPlayer::getAudioSessionId() |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 733 | { |
| 734 | Mutex::Autolock _l(mLock); |
| 735 | return mAudioSessionId; |
| 736 | } |
| 737 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 738 | status_t MediaPlayer::setAuxEffectSendLevel(float level) |
| 739 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 740 | ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 741 | Mutex::Autolock _l(mLock); |
| 742 | mSendLevel = level; |
| 743 | if (mPlayer != 0) { |
| 744 | return mPlayer->setAuxEffectSendLevel(level); |
| 745 | } |
| 746 | return OK; |
| 747 | } |
| 748 | |
| 749 | status_t MediaPlayer::attachAuxEffect(int effectId) |
| 750 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 751 | ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 752 | Mutex::Autolock _l(mLock); |
| 753 | if (mPlayer == 0 || |
| 754 | (mCurrentState & MEDIA_PLAYER_IDLE) || |
| 755 | (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) { |
Wei Jia | 848ebc6 | 2016-03-23 14:06:46 -0700 | [diff] [blame] | 756 | ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 757 | return INVALID_OPERATION; |
| 758 | } |
| 759 | |
| 760 | return mPlayer->attachAuxEffect(effectId); |
| 761 | } |
| 762 | |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 763 | // always call with lock held |
| 764 | status_t MediaPlayer::checkStateForKeySet_l(int key) |
| 765 | { |
| 766 | switch(key) { |
| 767 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 768 | if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | |
| 769 | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) { |
| 770 | // Can't change the audio attributes after prepare |
| 771 | ALOGE("trying to set audio attributes called in state %d", mCurrentState); |
| 772 | return INVALID_OPERATION; |
| 773 | } |
| 774 | break; |
| 775 | default: |
| 776 | // parameter doesn't require player state check |
| 777 | break; |
| 778 | } |
| 779 | return OK; |
| 780 | } |
| 781 | |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 782 | status_t MediaPlayer::setParameter(int key, const Parcel& request) |
| 783 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 784 | ALOGV("MediaPlayer::setParameter(%d)", key); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 785 | status_t status = INVALID_OPERATION; |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 786 | Mutex::Autolock _l(mLock); |
Jean-Michel Trivi | d9d7fa0 | 2014-06-24 08:01:46 -0700 | [diff] [blame] | 787 | if (checkStateForKeySet_l(key) != OK) { |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 788 | return status; |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 789 | } |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 790 | switch (key) { |
| 791 | case KEY_PARAMETER_AUDIO_ATTRIBUTES: |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 792 | // save the marshalled audio attributes |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 793 | if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; }; |
| 794 | mAudioAttributesParcel = new Parcel(); |
| 795 | mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize()); |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 796 | status = OK; |
| 797 | break; |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 798 | default: |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 799 | ALOGV_IF(mPlayer == NULL, "setParameter: no active player"); |
| 800 | break; |
Jean-Michel Trivi | 640adb3 | 2014-09-05 11:20:11 -0700 | [diff] [blame] | 801 | } |
Eric Laurent | 4356269 | 2015-07-15 16:49:07 -0700 | [diff] [blame] | 802 | |
| 803 | if (mPlayer != NULL) { |
| 804 | status = mPlayer->setParameter(key, request); |
| 805 | } |
| 806 | return status; |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | status_t MediaPlayer::getParameter(int key, Parcel *reply) |
| 810 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 811 | ALOGV("MediaPlayer::getParameter(%d)", key); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 812 | Mutex::Autolock _l(mLock); |
| 813 | if (mPlayer != NULL) { |
Ray Essick | db12214 | 2017-01-25 17:51:55 -0800 | [diff] [blame] | 814 | status_t status = mPlayer->getParameter(key, reply); |
| 815 | if (status != OK) { |
| 816 | ALOGD("getParameter returns %d", status); |
| 817 | } |
| 818 | return status; |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 819 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 820 | ALOGV("getParameter: no active player"); |
Gloria Wang | 4f9e47f | 2011-04-25 17:28:22 -0700 | [diff] [blame] | 821 | return INVALID_OPERATION; |
| 822 | } |
| 823 | |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 824 | status_t MediaPlayer::setRetransmitEndpoint(const char* addrString, |
| 825 | uint16_t port) { |
| 826 | ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)", |
| 827 | addrString ? addrString : "(null)", port); |
| 828 | |
| 829 | Mutex::Autolock _l(mLock); |
| 830 | if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE)) |
| 831 | return INVALID_OPERATION; |
| 832 | |
| 833 | if (NULL == addrString) { |
| 834 | mRetransmitEndpointValid = false; |
| 835 | return OK; |
| 836 | } |
| 837 | |
| 838 | struct in_addr saddr; |
| 839 | if(!inet_aton(addrString, &saddr)) { |
| 840 | return BAD_VALUE; |
| 841 | } |
| 842 | |
Glenn Kasten | be08f6a | 2013-12-19 09:09:33 -0800 | [diff] [blame] | 843 | memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint)); |
John Grossman | c795b64 | 2012-02-22 15:38:35 -0800 | [diff] [blame] | 844 | mRetransmitEndpoint.sin_family = AF_INET; |
| 845 | mRetransmitEndpoint.sin_addr = saddr; |
| 846 | mRetransmitEndpoint.sin_port = htons(port); |
| 847 | mRetransmitEndpointValid = true; |
| 848 | |
| 849 | return OK; |
| 850 | } |
| 851 | |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 852 | 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] | 853 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 854 | 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] | 855 | bool send = true; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 856 | bool locked = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | |
| 858 | // TODO: In the future, we might be on the same thread if the app is |
| 859 | // running in the same process as the media server. In that case, |
| 860 | // this will deadlock. |
Nicolas Catania | 6609518 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 861 | // |
Chong Zhang | d88adb9 | 2014-07-23 11:43:46 -0700 | [diff] [blame] | 862 | // The threadId hack below works around this for the care of prepare, |
| 863 | // seekTo and start within the same process. |
Andreas Huber | 5cb07aa | 2009-03-24 20:48:51 -0700 | [diff] [blame] | 864 | // FIXME: Remember, this is a hack, it's not even a hack that is applied |
| 865 | // consistently for all use-cases, this needs to be revisited. |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 866 | if (mLockThreadId != getThreadId()) { |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 867 | mLock.lock(); |
| 868 | locked = true; |
Nicolas Catania | 6609518 | 2009-06-11 16:33:49 -0700 | [diff] [blame] | 869 | } |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 870 | |
Eric Laurent | 3b26844 | 2010-08-03 07:49:49 -0700 | [diff] [blame] | 871 | // Allows calls from JNI in idle state to notify errors |
| 872 | if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 873 | ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2); |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 874 | if (locked) mLock.unlock(); // release the lock when done. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | return; |
| 876 | } |
| 877 | |
| 878 | switch (msg) { |
| 879 | case MEDIA_NOP: // interface test message |
| 880 | break; |
| 881 | case MEDIA_PREPARED: |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 882 | ALOGV("MediaPlayer::notify() prepared"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | mCurrentState = MEDIA_PLAYER_PREPARED; |
| 884 | if (mPrepareSync) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 885 | ALOGV("signal application thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | mPrepareSync = false; |
| 887 | mPrepareStatus = NO_ERROR; |
| 888 | mSignal.signal(); |
| 889 | } |
| 890 | break; |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 891 | case MEDIA_DRM_INFO: |
| 892 | ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj); |
| 893 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | case MEDIA_PLAYBACK_COMPLETE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 895 | ALOGV("playback complete"); |
Marco Nelissen | 1c1503c | 2010-09-17 15:04:01 -0700 | [diff] [blame] | 896 | if (mCurrentState == MEDIA_PLAYER_IDLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 897 | ALOGE("playback complete in idle state"); |
Marco Nelissen | 1c1503c | 2010-09-17 15:04:01 -0700 | [diff] [blame] | 898 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | if (!mLoop) { |
| 900 | mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE; |
| 901 | } |
| 902 | break; |
| 903 | case MEDIA_ERROR: |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 904 | // Always log errors. |
| 905 | // ext1: Media framework error code. |
| 906 | // ext2: Implementation dependant error code. |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 907 | ALOGE("error (%d, %d)", ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | mCurrentState = MEDIA_PLAYER_STATE_ERROR; |
| 909 | if (mPrepareSync) |
| 910 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 911 | ALOGV("signal application thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | mPrepareSync = false; |
| 913 | mPrepareStatus = ext1; |
| 914 | mSignal.signal(); |
| 915 | send = false; |
| 916 | } |
| 917 | break; |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 918 | case MEDIA_INFO: |
| 919 | // ext1: Media framework error code. |
| 920 | // ext2: Implementation dependant error code. |
Andreas Huber | 145e68f | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 921 | if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 922 | ALOGW("info/warning (%d, %d)", ext1, ext2); |
Andreas Huber | 145e68f | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 923 | } |
The Android Open Source Project | 65e731f | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 924 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | case MEDIA_SEEK_COMPLETE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 926 | ALOGV("Received seek complete"); |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 927 | if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) { |
| 928 | ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | mSeekPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 930 | mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
| 931 | seekTo_l(mCurrentPosition, mCurrentSeekMode); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 934 | ALOGV("All seeks complete - return to regularly scheduled program"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | mCurrentPosition = mSeekPosition = -1; |
Wei Jia | c5de091 | 2016-11-18 10:22:14 -0800 | [diff] [blame] | 936 | mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | } |
| 938 | break; |
| 939 | case MEDIA_BUFFERING_UPDATE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 940 | ALOGV("buffering %d", ext1); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | break; |
| 942 | case MEDIA_SET_VIDEO_SIZE: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 943 | ALOGV("New video size %d x %d", ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | mVideoWidth = ext1; |
| 945 | mVideoHeight = ext2; |
| 946 | break; |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 947 | case MEDIA_TIMED_TEXT: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 948 | ALOGV("Received timed text message"); |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 949 | break; |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame] | 950 | case MEDIA_SUBTITLE_DATA: |
| 951 | ALOGV("Received subtitle data message"); |
| 952 | break; |
Robert Shih | 0852843 | 2015-04-08 09:06:54 -0700 | [diff] [blame] | 953 | case MEDIA_META_DATA: |
| 954 | ALOGV("Received timed metadata message"); |
| 955 | break; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | default: |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 957 | ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | break; |
| 959 | } |
| 960 | |
| 961 | sp<MediaPlayerListener> listener = mListener; |
Jason Sams | 1af452f | 2009-03-24 18:45:22 -0700 | [diff] [blame] | 962 | if (locked) mLock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | |
| 964 | // this prevents re-entrant calls into client code |
| 965 | if ((listener != 0) && send) { |
| 966 | Mutex::Autolock _l(mNotifyLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 967 | ALOGV("callback application"); |
Gloria Wang | b483c47 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 968 | listener->notify(msg, ext1, ext2, obj); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 969 | ALOGV("back from callback"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
James Dong | dd172fc | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 973 | void MediaPlayer::died() |
| 974 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 975 | ALOGV("died"); |
James Dong | dd172fc | 2010-01-15 18:13:58 -0800 | [diff] [blame] | 976 | notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0); |
| 977 | } |
| 978 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 979 | status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) { |
Wei Jia | 1243869 | 2016-03-28 14:12:57 -0700 | [diff] [blame] | 980 | Mutex::Autolock _l(mLock); |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 981 | if (mPlayer == NULL) { |
| 982 | return NO_INIT; |
| 983 | } |
Marco Nelissen | b13820f | 2013-08-05 12:22:43 -0700 | [diff] [blame] | 984 | |
| 985 | if (next != NULL && !(next->mCurrentState & |
| 986 | (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) { |
| 987 | ALOGE("next player is not prepared"); |
| 988 | return INVALID_OPERATION; |
| 989 | } |
| 990 | |
Marco Nelissen | 6b74d67 | 2012-02-28 16:07:44 -0800 | [diff] [blame] | 991 | return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer); |
| 992 | } |
| 993 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 994 | VolumeShaper::Status MediaPlayer::applyVolumeShaper( |
| 995 | const sp<VolumeShaper::Configuration>& configuration, |
| 996 | const sp<VolumeShaper::Operation>& operation) |
| 997 | { |
| 998 | Mutex::Autolock _l(mLock); |
| 999 | if (mPlayer == nullptr) { |
| 1000 | return VolumeShaper::Status(NO_INIT); |
| 1001 | } |
| 1002 | VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation); |
| 1003 | return status; |
| 1004 | } |
| 1005 | |
| 1006 | sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id) |
| 1007 | { |
| 1008 | Mutex::Autolock _l(mLock); |
| 1009 | if (mPlayer == nullptr) { |
| 1010 | return nullptr; |
| 1011 | } |
| 1012 | return mPlayer->getVolumeShaperState(id); |
| 1013 | } |
| 1014 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1015 | // Modular DRM |
| 1016 | status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId) |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1017 | { |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1018 | // TODO change to ALOGV |
| 1019 | ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid, |
| 1020 | drmSessionId.array(), drmSessionId.size()); |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1021 | Mutex::Autolock _l(mLock); |
| 1022 | if (mPlayer == NULL) { |
| 1023 | return NO_INIT; |
| 1024 | } |
| 1025 | |
Hassan Shojania | 838be39 | 2017-05-23 14:14:24 -0700 | [diff] [blame^] | 1026 | // Only allowed it in player's preparing/prepared state. |
| 1027 | // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or |
| 1028 | // completed) so the state change to "prepared" might not have happened yet (e.g., buffering). |
| 1029 | // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener. |
| 1030 | if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) { |
| 1031 | ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState); |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1032 | return INVALID_OPERATION; |
| 1033 | } |
| 1034 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1035 | if (drmSessionId.isEmpty()) { |
| 1036 | ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId."); |
| 1037 | return INVALID_OPERATION; |
| 1038 | } |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1039 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1040 | // Passing down to mediaserver mainly for creating the crypto |
| 1041 | status_t status = mPlayer->prepareDrm(uuid, drmSessionId); |
| 1042 | ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status); |
| 1043 | |
| 1044 | // TODO change to ALOGV |
| 1045 | ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status); |
| 1046 | |
| 1047 | return status; |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | status_t MediaPlayer::releaseDrm() |
| 1051 | { |
| 1052 | Mutex::Autolock _l(mLock); |
| 1053 | if (mPlayer == NULL) { |
| 1054 | return NO_INIT; |
| 1055 | } |
| 1056 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1057 | // Not allowing releaseDrm in an active/resumable state |
| 1058 | if (mCurrentState & (MEDIA_PLAYER_STARTED | |
| 1059 | MEDIA_PLAYER_PAUSED | |
| 1060 | MEDIA_PLAYER_PLAYBACK_COMPLETE | |
| 1061 | MEDIA_PLAYER_STATE_ERROR)) { |
| 1062 | ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState); |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1063 | return INVALID_OPERATION; |
| 1064 | } |
| 1065 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1066 | status_t status = mPlayer->releaseDrm(); |
| 1067 | // TODO change to ALOGV |
| 1068 | ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status); |
| 1069 | if (status != OK) { |
| 1070 | ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status); |
| 1071 | // Overriding to OK so the client proceed with its own cleanup |
| 1072 | // Client can't do more cleanup. mediaserver release its crypto at end of session anyway. |
| 1073 | status = OK; |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 1076 | return status; |
Hassan Shojania | 071437a | 2017-01-23 09:19:40 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1079 | } // namespace android |