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