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