blob: 26908e5570236ae28fe19905aa0e39182316c532 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
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 Shojaniacefac142017-02-06 21:02:02 -080019#define LOG_TAG "MediaPlayerNative"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070022#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 Project89fa4ad2009-03-03 19:31:44 -080028
Mathias Agopian75624082009-05-19 19:08:10 -070029#include <binder/IServiceManager.h>
30#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080032#include <gui/Surface.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034#include <media/mediaplayer.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070035#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080036#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070037#include <media/AVSyncSettings.h>
Chris Watkins99f31602015-03-20 13:06:33 -070038#include <media/IDataSource.h>
Ray Essickdb122142017-01-25 17:51:55 -080039#include <media/MediaAnalyticsItem.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040
Mathias Agopian75624082009-05-19 19:08:10 -070041#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042
Andreas Huber2db84552010-01-28 11:19:57 -080043#include <utils/KeyedVector.h>
44#include <utils/String8.h>
45
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070047#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070048
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049namespace android {
50
Ivan Lozano8cf3a072017-08-09 09:01:33 -070051using media::VolumeShaper;
52
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053MediaPlayer::MediaPlayer()
54{
Steve Block3856b092011-10-20 11:56:00 +010055 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056 mListener = NULL;
57 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070058 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070059 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080061 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080063 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 mCurrentState = MEDIA_PLAYER_IDLE;
65 mPrepareSync = false;
66 mPrepareStatus = NO_ERROR;
67 mLoop = false;
68 mLeftVolume = mRightVolume = 1.0;
69 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070070 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080071 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Marco Nelissend457c972014-02-11 08:47:07 -080072 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070073 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080074 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075}
76
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077MediaPlayer::~MediaPlayer()
78{
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070080 if (mAudioAttributesParcel != NULL) {
81 delete mAudioAttributesParcel;
82 mAudioAttributesParcel = NULL;
83 }
Marco Nelissend457c972014-02-11 08:47:07 -080084 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 disconnect();
86 IPCThreadState::self()->flushCommands();
87}
88
89void MediaPlayer::disconnect()
90{
Steve Block3856b092011-10-20 11:56:00 +010091 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 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
105void MediaPlayer::clear_l()
106{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800108 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800110 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800112 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113}
114
115status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
116{
Steve Block3856b092011-10-20 11:56:00 +0100117 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 Mutex::Autolock _l(mLock);
119 mListener = listener;
120 return NO_ERROR;
121}
122
123
Dave Burked681bbb2011-08-30 14:39:17 +0100124status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125{
126 status_t err = UNKNOWN_ERROR;
127 sp<IMediaPlayer> p;
128 { // scope for the lock
129 Mutex::Autolock _l(mLock);
130
Marco Nelissen83ff1432010-03-10 10:53:16 -0800131 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
132 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000133 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 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 Muranakaf65fa172013-06-06 09:36:34 +0000144 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 }
146 }
147
148 if (p != 0) {
149 p->disconnect();
150 }
151
152 return err;
153}
154
Andreas Huber2db84552010-01-28 11:19:57 -0800155status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800156 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800157 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158{
Steve Block3856b092011-10-20 11:56:00 +0100159 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 status_t err = BAD_VALUE;
161 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700162 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800164 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800165 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800166 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100167 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100168 }
Dave Burke06620672011-09-06 20:39:47 +0100169 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 }
171 }
172 return err;
173}
174
175status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
176{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700177 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700179 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800181 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800182 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
183 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100184 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100185 }
Dave Burke06620672011-09-06 20:39:47 +0100186 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100187 }
188 return err;
189}
190
Chris Watkins99f31602015-03-20 13:06:33 -0700191status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
192{
193 ALOGV("setDataSource(IDataSource)");
194 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700195 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700196 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 Catania1d187f12009-05-12 23:25:55 -0700207status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
208{
209 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800210 const bool hasBeenInitialized =
211 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
212 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
213 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700214 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700215 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700216 }
Wei Jia848ebc62016-03-23 14:06:46 -0700217 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700218 return INVALID_OPERATION;
219}
220
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700221status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
222{
Steve Blockb8a80522011-12-20 16:23:08 +0000223 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700224 Mutex::Autolock lock(mLock);
225 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700226 return NO_INIT;
227 }
228 return mPlayer->setMetadataFilter(filter);
229}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700230
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700231status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
232{
Steve Blockb8a80522011-12-20 16:23:08 +0000233 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700234 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 Kasten11731182011-02-08 17:26:17 -0800241status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800242 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800243{
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800245 Mutex::Autolock _l(mLock);
246 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800247 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800248}
249
Wei Jiaed320862017-01-18 14:03:40 -0800250status_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 Jia9bb38032017-03-23 18:00:38 -0700258 return mPlayer->getBufferingSettings(buffering);
Wei Jiaed320862017-01-18 14:03:40 -0800259}
260
Wei Jiadc6f3402017-01-09 15:04:18 -0800261status_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 Jia9bb38032017-03-23 18:00:38 -0700269 return mPlayer->setBufferingSettings(buffering);
Wei Jiadc6f3402017-01-09 15:04:18 -0800270}
271
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272// must call with lock held
273status_t MediaPlayer::prepareAsync_l()
274{
Glenn Kastenb187de12014-12-30 08:18:15 -0800275 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700276 if (mAudioAttributesParcel != NULL) {
277 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700278 } else {
279 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700280 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 mCurrentState = MEDIA_PLAYER_PREPARING;
282 return mPlayer->prepareAsync();
283 }
Wei Jia848ebc62016-03-23 14:06:46 -0700284 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 return INVALID_OPERATION;
286}
287
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700288// 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 Project89fa4ad2009-03-03 19:31:44 -0800292status_t MediaPlayer::prepare()
293{
Steve Block3856b092011-10-20 11:56:00 +0100294 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700296 mLockThreadId = getThreadId();
297 if (mPrepareSync) {
298 mLockThreadId = 0;
299 return -EALREADY;
300 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 mPrepareSync = true;
302 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700303 if (ret != NO_ERROR) {
304 mLockThreadId = 0;
305 return ret;
306 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307
308 if (mPrepareSync) {
309 mSignal.wait(mLock); // wait for prepare done
310 mPrepareSync = false;
311 }
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700313 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 return mPrepareStatus;
315}
316
317status_t MediaPlayer::prepareAsync()
318{
Steve Block3856b092011-10-20 11:56:00 +0100319 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 Mutex::Autolock _l(mLock);
321 return prepareAsync_l();
322}
323
324status_t MediaPlayer::start()
325{
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700327
328 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700330
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 Project89fa4ad2009-03-03 19:31:44 -0800336 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
337 mPlayer->setLooping(mLoop);
338 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700339 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700341 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 if (ret != NO_ERROR) {
343 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
344 } else {
345 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100346 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 }
348 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700349 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700350 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700351 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700353
354 mLockThreadId = 0;
355
356 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357}
358
359status_t MediaPlayer::stop()
360{
Steve Block3856b092011-10-20 11:56:00 +0100361 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 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 Jia848ebc62016-03-23 14:06:46 -0700374 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375 return INVALID_OPERATION;
376}
377
378status_t MediaPlayer::pause()
379{
Steve Block3856b092011-10-20 11:56:00 +0100380 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800382 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383 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 Jia848ebc62016-03-23 14:06:46 -0700393 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 return INVALID_OPERATION;
395}
396
397bool MediaPlayer::isPlaying()
398{
399 Mutex::Autolock _l(mLock);
400 if (mPlayer != 0) {
401 bool temp = false;
402 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100403 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000405 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700407 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
408 ALOGE("internal/external state mismatch corrected");
409 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 }
411 return temp;
412 }
Steve Block3856b092011-10-20 11:56:00 +0100413 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 return false;
415}
416
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700417status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800418{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700419 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 Jia98160162015-02-04 17:01:11 -0800424 return BAD_VALUE;
425 }
426 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700427 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
428 return INVALID_OPERATION;
429 }
Wei Jia5be109f2016-06-10 16:15:07 -0700430
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 Molnar3a474aa2015-04-24 17:10:07 -0700439 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 Jia5be109f2016-06-10 16:15:07 -0700443 } else if (rate.mSpeed != 0.f
444 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
445 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700446 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800447 }
Wei Jia98160162015-02-04 17:01:11 -0800448 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700449 return err;
450}
451
452status_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
459status_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
468status_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 Jia98160162015-02-04 17:01:11 -0800474}
475
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476status_t MediaPlayer::getVideoWidth(int *w)
477{
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 Mutex::Autolock _l(mLock);
480 if (mPlayer == 0) return INVALID_OPERATION;
481 *w = mVideoWidth;
482 return NO_ERROR;
483}
484
485status_t MediaPlayer::getVideoHeight(int *h)
486{
Steve Block3856b092011-10-20 11:56:00 +0100487 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 Mutex::Autolock _l(mLock);
489 if (mPlayer == 0) return INVALID_OPERATION;
490 *h = mVideoHeight;
491 return NO_ERROR;
492}
493
494status_t MediaPlayer::getCurrentPosition(int *msec)
495{
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497 Mutex::Autolock _l(mLock);
498 if (mPlayer != 0) {
499 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100500 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501 *msec = mCurrentPosition;
502 return NO_ERROR;
503 }
504 return mPlayer->getCurrentPosition(msec);
505 }
506 return INVALID_OPERATION;
507}
508
509status_t MediaPlayer::getDuration_l(int *msec)
510{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800511 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800512 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
513 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800515 int durationMs;
516 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700517
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 Hubera4c5bc02012-11-27 15:02:53 -0800524 if (msec) {
525 *msec = durationMs;
526 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 return ret;
528 }
Wei Jia848ebc62016-03-23 14:06:46 -0700529 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
530 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531 return INVALID_OPERATION;
532}
533
534status_t MediaPlayer::getDuration(int *msec)
535{
536 Mutex::Autolock _l(mLock);
537 return getDuration_l(msec);
538}
539
Wei Jiac5de0912016-11-18 10:22:14 -0800540status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541{
Wei Jiac5de0912016-11-18 10:22:14 -0800542 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800543 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
544 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000546 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800549
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 Project89fa4ad2009-03-03 19:31:44 -0800567 // cache duration
568 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800569 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800572 mSeekMode = mode;
573 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 }
575 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800576 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 return NO_ERROR;
578 }
579 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800580 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
581 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 return INVALID_OPERATION;
583}
584
Wei Jiac5de0912016-11-18 10:22:14 -0800585status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700587 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800589 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700590 mLockThreadId = 0;
591
592 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593}
594
Wei Jia52c28512017-09-13 18:17:51 -0700595status_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 Gennis61c7ef52011-07-13 12:59:34 -0700604status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 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 Block29357bc2012-01-06 19:20:56 +0000612 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
614 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700615 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616 mCurrentState = MEDIA_PLAYER_IDLE;
617 }
James Donga1680bc2010-11-18 12:23:58 -0800618 // setDataSource has to be called again to create a
619 // new mediaplayer.
620 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 return ret;
622 }
623 clear_l();
624 return NO_ERROR;
625}
626
John Grossmanc795b642012-02-22 15:38:35 -0800627status_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 Gennis61c7ef52011-07-13 12:59:34 -0700641status_t MediaPlayer::reset()
642{
Steve Block3856b092011-10-20 11:56:00 +0100643 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700644 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700645 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700646 status_t result = reset_l();
647 mLockThreadId = 0;
648
649 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700650}
651
Glenn Kastenfff6d712012-01-12 16:38:12 -0800652status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653{
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 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 Block29357bc2012-01-06 19:20:56 +0000660 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 return INVALID_OPERATION;
662 }
663 // cache
664 mStreamType = type;
665 return OK;
666}
667
John Spurlockde9453f2014-03-19 13:05:45 -0400668status_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 Project89fa4ad2009-03-03 19:31:44 -0800676status_t MediaPlayer::setLooping(int loop)
677{
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 Mutex::Autolock _l(mLock);
680 mLoop = (loop != 0);
681 if (mPlayer != 0) {
682 return mPlayer->setLooping(loop);
683 }
684 return OK;
685}
686
687bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 Mutex::Autolock _l(mLock);
690 if (mPlayer != 0) {
691 return mLoop;
692 }
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694 return false;
695}
696
697status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
698{
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 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 Kastend848eb42016-03-08 13:42:11 -0800709status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700710{
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700712 Mutex::Autolock _l(mLock);
713 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000714 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700715 return INVALID_OPERATION;
716 }
717 if (sessionId < 0) {
718 return BAD_VALUE;
719 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700720 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800721 AudioSystem::acquireAudioSessionId(sessionId, -1);
722 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700723 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700724 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700725 return NO_ERROR;
726}
727
Glenn Kastend848eb42016-03-08 13:42:11 -0800728audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700729{
730 Mutex::Autolock _l(mLock);
731 return mAudioSessionId;
732}
733
Eric Laurent2beeb502010-07-16 07:43:46 -0700734status_t MediaPlayer::setAuxEffectSendLevel(float level)
735{
Steve Block3856b092011-10-20 11:56:00 +0100736 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700737 Mutex::Autolock _l(mLock);
738 mSendLevel = level;
739 if (mPlayer != 0) {
740 return mPlayer->setAuxEffectSendLevel(level);
741 }
742 return OK;
743}
744
745status_t MediaPlayer::attachAuxEffect(int effectId)
746{
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700748 Mutex::Autolock _l(mLock);
749 if (mPlayer == 0 ||
750 (mCurrentState & MEDIA_PLAYER_IDLE) ||
751 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700752 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700753 return INVALID_OPERATION;
754 }
755
756 return mPlayer->attachAuxEffect(effectId);
757}
758
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700759// always call with lock held
760status_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 Wang4f9e47f2011-04-25 17:28:22 -0700778status_t MediaPlayer::setParameter(int key, const Parcel& request)
779{
Steve Block3856b092011-10-20 11:56:00 +0100780 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700781 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700782 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700783 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700784 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700785 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700786 switch (key) {
787 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700788 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700789 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
790 mAudioAttributesParcel = new Parcel();
791 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700792 status = OK;
793 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700794 default:
Eric Laurent43562692015-07-15 16:49:07 -0700795 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
796 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700797 }
Eric Laurent43562692015-07-15 16:49:07 -0700798
799 if (mPlayer != NULL) {
800 status = mPlayer->setParameter(key, request);
801 }
802 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700803}
804
805status_t MediaPlayer::getParameter(int key, Parcel *reply)
806{
Steve Block3856b092011-10-20 11:56:00 +0100807 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700808 Mutex::Autolock _l(mLock);
809 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800810 status_t status = mPlayer->getParameter(key, reply);
811 if (status != OK) {
812 ALOGD("getParameter returns %d", status);
813 }
814 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700815 }
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700817 return INVALID_OPERATION;
818}
819
John Grossmanc795b642012-02-22 15:38:35 -0800820status_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 Kastenbe08f6a2013-12-19 09:09:33 -0800839 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800840 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 Wangb483c472011-04-11 17:23:27 -0700848void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849{
Steve Block3856b092011-10-20 11:56:00 +0100850 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700852 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853
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 Catania66095182009-06-11 16:33:49 -0700857 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700858 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700859 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700860 // 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 Kastene53b9ea2012-03-12 16:29:55 -0700862 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700863 mLock.lock();
864 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700865 }
Jason Sams1af452f2009-03-24 18:45:22 -0700866
Eric Laurent3b268442010-08-03 07:49:49 -0700867 // Allows calls from JNI in idle state to notify errors
868 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100869 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700870 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800871 return;
872 }
873
874 switch (msg) {
875 case MEDIA_NOP: // interface test message
876 break;
877 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800878 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800879 mCurrentState = MEDIA_PLAYER_PREPARED;
880 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800882 mPrepareSync = false;
883 mPrepareStatus = NO_ERROR;
884 mSignal.signal();
885 }
886 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800887 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 Project89fa4ad2009-03-03 19:31:44 -0800890 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700892 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000893 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700894 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800895 if (!mLoop) {
896 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
897 }
898 break;
899 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700900 // Always log errors.
901 // ext1: Media framework error code.
902 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
905 if (mPrepareSync)
906 {
Steve Block3856b092011-10-20 11:56:00 +0100907 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 mPrepareSync = false;
909 mPrepareStatus = ext1;
910 mSignal.signal();
911 send = false;
912 }
913 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700914 case MEDIA_INFO:
915 // ext1: Media framework error code.
916 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800917 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000918 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800919 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700920 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800921 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800923 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
924 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800926 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
927 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928 }
929 else {
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800932 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933 }
934 break;
935 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 break;
938 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100939 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940 mVideoWidth = ext1;
941 mVideoHeight = ext2;
942 break;
Wei Jia52c28512017-09-13 18:17:51 -0700943 case MEDIA_NOTIFY_TIME:
944 ALOGV("Received notify time message");
945 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700946 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700948 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700949 case MEDIA_SUBTITLE_DATA:
950 ALOGV("Received subtitle data message");
951 break;
Robert Shih08528432015-04-08 09:06:54 -0700952 case MEDIA_META_DATA:
953 ALOGV("Received timed metadata message");
954 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800955 default:
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 break;
958 }
959
960 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700961 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962
963 // this prevents re-entrant calls into client code
964 if ((listener != 0) && send) {
965 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700967 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 }
970}
971
James Dongdd172fc2010-01-15 18:13:58 -0800972void MediaPlayer::died()
973{
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800975 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
976}
977
Marco Nelissen6b74d672012-02-28 16:07:44 -0800978status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700979 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800980 if (mPlayer == NULL) {
981 return NO_INIT;
982 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700983
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 Nelissen6b74d672012-02-28 16:07:44 -0800990 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
991}
992
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800993VolumeShaper::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
1005sp<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 Shojaniacefac142017-02-06 21:02:02 -08001014// Modular DRM
1015status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001016{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001017 // TODO change to ALOGV
1018 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1019 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001020 Mutex::Autolock _l(mLock);
1021 if (mPlayer == NULL) {
1022 return NO_INIT;
1023 }
1024
Hassan Shojania838be392017-05-23 14:14:24 -07001025 // 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 Shojania071437a2017-01-23 09:19:40 -08001031 return INVALID_OPERATION;
1032 }
1033
Hassan Shojaniacefac142017-02-06 21:02:02 -08001034 if (drmSessionId.isEmpty()) {
1035 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1036 return INVALID_OPERATION;
1037 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001038
Hassan Shojaniacefac142017-02-06 21:02:02 -08001039 // 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 Shojania071437a2017-01-23 09:19:40 -08001047}
1048
1049status_t MediaPlayer::releaseDrm()
1050{
1051 Mutex::Autolock _l(mLock);
1052 if (mPlayer == NULL) {
1053 return NO_INIT;
1054 }
1055
Hassan Shojaniacefac142017-02-06 21:02:02 -08001056 // 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 Shojania071437a2017-01-23 09:19:40 -08001062 return INVALID_OPERATION;
1063 }
1064
Hassan Shojaniacefac142017-02-06 21:02:02 -08001065 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 Shojania071437a2017-01-23 09:19:40 -08001073 }
1074
Hassan Shojaniacefac142017-02-06 21:02:02 -08001075 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001076}
1077
jiabin156c6872017-10-06 09:47:15 -07001078status_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
1088audio_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
1103status_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 Kasten40bc9062015-03-20 09:09:33 -07001113} // namespace android