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