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