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