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