blob: 68cbdf51cdc699389225347f65fb165a78a41bf7 [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
19#define LOG_TAG "MediaPlayer"
20#include <utils/Log.h>
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <fcntl.h>
26
Mathias Agopian75624082009-05-19 19:08:10 -070027#include <binder/IServiceManager.h>
28#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080030#include <gui/Surface.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <media/mediaplayer.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080033#include <media/AudioSystem.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Andreas Huber2db84552010-01-28 11:19:57 -080037#include <utils/KeyedVector.h>
38#include <utils/String8.h>
39
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070041#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043namespace android {
44
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045MediaPlayer::MediaPlayer()
46{
Steve Block3856b092011-10-20 11:56:00 +010047 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048 mListener = NULL;
49 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070050 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051 mCurrentPosition = -1;
52 mSeekPosition = -1;
53 mCurrentState = MEDIA_PLAYER_IDLE;
54 mPrepareSync = false;
55 mPrepareStatus = NO_ERROR;
56 mLoop = false;
57 mLeftVolume = mRightVolume = 1.0;
58 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070059 mLockThreadId = 0;
Eric Laurenta514bdb2010-06-21 09:27:30 -070060 mAudioSessionId = AudioSystem::newAudioSessionId();
Marco Nelissen3a34bef2011-08-02 13:33:41 -070061 AudioSystem::acquireAudioSessionId(mAudioSessionId);
Eric Laurent8c563ed2010-10-07 18:23:03 -070062 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080063 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064}
65
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066MediaPlayer::~MediaPlayer()
67{
Steve Block3856b092011-10-20 11:56:00 +010068 ALOGV("destructor");
Marco Nelissen3a34bef2011-08-02 13:33:41 -070069 AudioSystem::releaseAudioSessionId(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070 disconnect();
71 IPCThreadState::self()->flushCommands();
72}
73
74void MediaPlayer::disconnect()
75{
Steve Block3856b092011-10-20 11:56:00 +010076 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 sp<IMediaPlayer> p;
78 {
79 Mutex::Autolock _l(mLock);
80 p = mPlayer;
81 mPlayer.clear();
82 }
83
84 if (p != 0) {
85 p->disconnect();
86 }
87}
88
89// always call with lock held
90void MediaPlayer::clear_l()
91{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 mCurrentPosition = -1;
93 mSeekPosition = -1;
94 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -080095 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096}
97
98status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
99{
Steve Block3856b092011-10-20 11:56:00 +0100100 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101 Mutex::Autolock _l(mLock);
102 mListener = listener;
103 return NO_ERROR;
104}
105
106
Dave Burked681bbb2011-08-30 14:39:17 +0100107status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108{
109 status_t err = UNKNOWN_ERROR;
110 sp<IMediaPlayer> p;
111 { // scope for the lock
112 Mutex::Autolock _l(mLock);
113
Marco Nelissen83ff1432010-03-10 10:53:16 -0800114 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
115 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000116 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 return INVALID_OPERATION;
118 }
119
120 clear_l();
121 p = mPlayer;
122 mPlayer = player;
123 if (player != 0) {
124 mCurrentState = MEDIA_PLAYER_INITIALIZED;
125 err = NO_ERROR;
126 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Unable to to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128 }
129 }
130
131 if (p != 0) {
132 p->disconnect();
133 }
134
135 return err;
136}
137
Andreas Huber2db84552010-01-28 11:19:57 -0800138status_t MediaPlayer::setDataSource(
139 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140{
Steve Block3856b092011-10-20 11:56:00 +0100141 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 status_t err = BAD_VALUE;
143 if (url != NULL) {
144 const sp<IMediaPlayerService>& service(getMediaPlayerService());
145 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800146 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800147 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
148 (NO_ERROR != player->setDataSource(url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100149 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100150 }
Dave Burke06620672011-09-06 20:39:47 +0100151 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 }
153 }
154 return err;
155}
156
157status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
158{
Steve Block3856b092011-10-20 11:56:00 +0100159 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 status_t err = UNKNOWN_ERROR;
161 const sp<IMediaPlayerService>& service(getMediaPlayerService());
162 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800163 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800164 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
165 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100166 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100167 }
Dave Burke06620672011-09-06 20:39:47 +0100168 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100169 }
170 return err;
171}
172
173status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
174{
Steve Block3856b092011-10-20 11:56:00 +0100175 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100176 status_t err = UNKNOWN_ERROR;
177 const sp<IMediaPlayerService>& service(getMediaPlayerService());
178 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800179 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800180 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
181 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100182 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100183 }
Dave Burke06620672011-09-06 20:39:47 +0100184 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 }
186 return err;
187}
188
Nicolas Catania1d187f12009-05-12 23:25:55 -0700189status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
190{
191 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800192 const bool hasBeenInitialized =
193 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
194 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
195 if ((mPlayer != NULL) && hasBeenInitialized) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700196 ALOGV("invoke %d", request.dataSize());
197 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700198 }
Steve Block29357bc2012-01-06 19:20:56 +0000199 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700200 return INVALID_OPERATION;
201}
202
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700203status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
204{
Steve Blockb8a80522011-12-20 16:23:08 +0000205 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700206 Mutex::Autolock lock(mLock);
207 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700208 return NO_INIT;
209 }
210 return mPlayer->setMetadataFilter(filter);
211}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700212
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700213status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
214{
Steve Blockb8a80522011-12-20 16:23:08 +0000215 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700216 Mutex::Autolock lock(mLock);
217 if (mPlayer == NULL) {
218 return NO_INIT;
219 }
220 return mPlayer->getMetadata(update_only, apply_filter, metadata);
221}
222
Glenn Kasten11731182011-02-08 17:26:17 -0800223status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800224 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800225{
Steve Block3856b092011-10-20 11:56:00 +0100226 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800227 Mutex::Autolock _l(mLock);
228 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800229 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800230}
231
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232// must call with lock held
233status_t MediaPlayer::prepareAsync_l()
234{
235 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
236 mPlayer->setAudioStreamType(mStreamType);
237 mCurrentState = MEDIA_PLAYER_PREPARING;
238 return mPlayer->prepareAsync();
239 }
Steve Block29357bc2012-01-06 19:20:56 +0000240 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 return INVALID_OPERATION;
242}
243
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700244// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
245// one defined in the Android framework and one provided by the implementation
246// that generated the error. The sync version of prepare returns only 1 error
247// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248status_t MediaPlayer::prepare()
249{
Steve Block3856b092011-10-20 11:56:00 +0100250 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700252 mLockThreadId = getThreadId();
253 if (mPrepareSync) {
254 mLockThreadId = 0;
255 return -EALREADY;
256 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 mPrepareSync = true;
258 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700259 if (ret != NO_ERROR) {
260 mLockThreadId = 0;
261 return ret;
262 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263
264 if (mPrepareSync) {
265 mSignal.wait(mLock); // wait for prepare done
266 mPrepareSync = false;
267 }
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700269 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 return mPrepareStatus;
271}
272
273status_t MediaPlayer::prepareAsync()
274{
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 Mutex::Autolock _l(mLock);
277 return prepareAsync_l();
278}
279
280status_t MediaPlayer::start()
281{
Steve Block3856b092011-10-20 11:56:00 +0100282 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 Mutex::Autolock _l(mLock);
284 if (mCurrentState & MEDIA_PLAYER_STARTED)
285 return NO_ERROR;
286 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
287 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
288 mPlayer->setLooping(mLoop);
289 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700290 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 mCurrentState = MEDIA_PLAYER_STARTED;
292 status_t ret = mPlayer->start();
293 if (ret != NO_ERROR) {
294 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
295 } else {
296 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 }
299 }
300 return ret;
301 }
Steve Block29357bc2012-01-06 19:20:56 +0000302 ALOGE("start called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 return INVALID_OPERATION;
304}
305
306status_t MediaPlayer::stop()
307{
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 Mutex::Autolock _l(mLock);
310 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
311 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
312 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
313 status_t ret = mPlayer->stop();
314 if (ret != NO_ERROR) {
315 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
316 } else {
317 mCurrentState = MEDIA_PLAYER_STOPPED;
318 }
319 return ret;
320 }
Steve Block29357bc2012-01-06 19:20:56 +0000321 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 return INVALID_OPERATION;
323}
324
325status_t MediaPlayer::pause()
326{
Steve Block3856b092011-10-20 11:56:00 +0100327 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800329 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return NO_ERROR;
331 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
332 status_t ret = mPlayer->pause();
333 if (ret != NO_ERROR) {
334 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
335 } else {
336 mCurrentState = MEDIA_PLAYER_PAUSED;
337 }
338 return ret;
339 }
Steve Block29357bc2012-01-06 19:20:56 +0000340 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341 return INVALID_OPERATION;
342}
343
344bool MediaPlayer::isPlaying()
345{
346 Mutex::Autolock _l(mLock);
347 if (mPlayer != 0) {
348 bool temp = false;
349 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100350 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000352 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 mCurrentState = MEDIA_PLAYER_PAUSED;
354 }
355 return temp;
356 }
Steve Block3856b092011-10-20 11:56:00 +0100357 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 return false;
359}
360
361status_t MediaPlayer::getVideoWidth(int *w)
362{
Steve Block3856b092011-10-20 11:56:00 +0100363 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 Mutex::Autolock _l(mLock);
365 if (mPlayer == 0) return INVALID_OPERATION;
366 *w = mVideoWidth;
367 return NO_ERROR;
368}
369
370status_t MediaPlayer::getVideoHeight(int *h)
371{
Steve Block3856b092011-10-20 11:56:00 +0100372 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 Mutex::Autolock _l(mLock);
374 if (mPlayer == 0) return INVALID_OPERATION;
375 *h = mVideoHeight;
376 return NO_ERROR;
377}
378
379status_t MediaPlayer::getCurrentPosition(int *msec)
380{
Steve Block3856b092011-10-20 11:56:00 +0100381 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 Mutex::Autolock _l(mLock);
383 if (mPlayer != 0) {
384 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 *msec = mCurrentPosition;
387 return NO_ERROR;
388 }
389 return mPlayer->getCurrentPosition(msec);
390 }
391 return INVALID_OPERATION;
392}
393
394status_t MediaPlayer::getDuration_l(int *msec)
395{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800396 ALOGV("getDuration_l");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
398 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800399 int durationMs;
400 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700401
402 if (ret != OK) {
403 // Do not enter error state just because no duration was available.
404 durationMs = -1;
405 ret = OK;
406 }
407
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800408 if (msec) {
409 *msec = durationMs;
410 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 return ret;
412 }
Steve Block29357bc2012-01-06 19:20:56 +0000413 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 return INVALID_OPERATION;
415}
416
417status_t MediaPlayer::getDuration(int *msec)
418{
419 Mutex::Autolock _l(mLock);
420 return getDuration_l(msec);
421}
422
423status_t MediaPlayer::seekTo_l(int msec)
424{
Steve Block3856b092011-10-20 11:56:00 +0100425 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
427 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000428 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800431
432 int durationMs;
433 status_t err = mPlayer->getDuration(&durationMs);
434
435 if (err != OK) {
436 ALOGW("Stream has no duration and is therefore not seekable.");
437 return err;
438 }
439
440 if (msec > durationMs) {
441 ALOGW("Attempt to seek to past end of file: request = %d, "
442 "durationMs = %d",
443 msec,
444 durationMs);
445
446 msec = durationMs;
447 }
448
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 // cache duration
450 mCurrentPosition = msec;
451 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 mSeekPosition = msec;
453 return mPlayer->seekTo(msec);
454 }
455 else {
Steve Block3856b092011-10-20 11:56:00 +0100456 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 return NO_ERROR;
458 }
459 }
Steve Block29357bc2012-01-06 19:20:56 +0000460 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 return INVALID_OPERATION;
462}
463
464status_t MediaPlayer::seekTo(int msec)
465{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700466 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700468 status_t result = seekTo_l(msec);
469 mLockThreadId = 0;
470
471 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472}
473
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700474status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 mLoop = false;
477 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
478 mPrepareSync = false;
479 if (mPlayer != 0) {
480 status_t ret = mPlayer->reset();
481 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000482 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
484 } else {
485 mCurrentState = MEDIA_PLAYER_IDLE;
486 }
James Donga1680bc2010-11-18 12:23:58 -0800487 // setDataSource has to be called again to create a
488 // new mediaplayer.
489 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 return ret;
491 }
492 clear_l();
493 return NO_ERROR;
494}
495
John Grossmanc795b642012-02-22 15:38:35 -0800496status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
497 Mutex::Autolock _l(mLock);
498
499 if (player == NULL) {
500 return UNKNOWN_ERROR;
501 }
502
503 if (mRetransmitEndpointValid) {
504 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
505 }
506
507 return OK;
508}
509
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700510status_t MediaPlayer::reset()
511{
Steve Block3856b092011-10-20 11:56:00 +0100512 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700513 Mutex::Autolock _l(mLock);
514 return reset_l();
515}
516
Glenn Kastenfff6d712012-01-12 16:38:12 -0800517status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518{
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 Mutex::Autolock _l(mLock);
521 if (mStreamType == type) return NO_ERROR;
522 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
523 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
524 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000525 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 return INVALID_OPERATION;
527 }
528 // cache
529 mStreamType = type;
530 return OK;
531}
532
533status_t MediaPlayer::setLooping(int loop)
534{
Steve Block3856b092011-10-20 11:56:00 +0100535 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 Mutex::Autolock _l(mLock);
537 mLoop = (loop != 0);
538 if (mPlayer != 0) {
539 return mPlayer->setLooping(loop);
540 }
541 return OK;
542}
543
544bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100545 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 Mutex::Autolock _l(mLock);
547 if (mPlayer != 0) {
548 return mLoop;
549 }
Steve Block3856b092011-10-20 11:56:00 +0100550 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 return false;
552}
553
554status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
555{
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 Mutex::Autolock _l(mLock);
558 mLeftVolume = leftVolume;
559 mRightVolume = rightVolume;
560 if (mPlayer != 0) {
561 return mPlayer->setVolume(leftVolume, rightVolume);
562 }
563 return OK;
564}
565
Eric Laurenta514bdb2010-06-21 09:27:30 -0700566status_t MediaPlayer::setAudioSessionId(int sessionId)
567{
Steve Block3856b092011-10-20 11:56:00 +0100568 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700569 Mutex::Autolock _l(mLock);
570 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000571 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700572 return INVALID_OPERATION;
573 }
574 if (sessionId < 0) {
575 return BAD_VALUE;
576 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700577 if (sessionId != mAudioSessionId) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700578 AudioSystem::acquireAudioSessionId(sessionId);
Marco Nelissen8bdb01c2013-05-10 10:10:42 -0700579 AudioSystem::releaseAudioSessionId(mAudioSessionId);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700580 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700581 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700582 return NO_ERROR;
583}
584
585int MediaPlayer::getAudioSessionId()
586{
587 Mutex::Autolock _l(mLock);
588 return mAudioSessionId;
589}
590
Eric Laurent2beeb502010-07-16 07:43:46 -0700591status_t MediaPlayer::setAuxEffectSendLevel(float level)
592{
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700594 Mutex::Autolock _l(mLock);
595 mSendLevel = level;
596 if (mPlayer != 0) {
597 return mPlayer->setAuxEffectSendLevel(level);
598 }
599 return OK;
600}
601
602status_t MediaPlayer::attachAuxEffect(int effectId)
603{
Steve Block3856b092011-10-20 11:56:00 +0100604 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700605 Mutex::Autolock _l(mLock);
606 if (mPlayer == 0 ||
607 (mCurrentState & MEDIA_PLAYER_IDLE) ||
608 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000609 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700610 return INVALID_OPERATION;
611 }
612
613 return mPlayer->attachAuxEffect(effectId);
614}
615
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700616status_t MediaPlayer::setParameter(int key, const Parcel& request)
617{
Steve Block3856b092011-10-20 11:56:00 +0100618 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700619 Mutex::Autolock _l(mLock);
620 if (mPlayer != NULL) {
621 return mPlayer->setParameter(key, request);
622 }
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("setParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700624 return INVALID_OPERATION;
625}
626
627status_t MediaPlayer::getParameter(int key, Parcel *reply)
628{
Steve Block3856b092011-10-20 11:56:00 +0100629 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700630 Mutex::Autolock _l(mLock);
631 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700632 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700633 }
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700635 return INVALID_OPERATION;
636}
637
John Grossmanc795b642012-02-22 15:38:35 -0800638status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
639 uint16_t port) {
640 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
641 addrString ? addrString : "(null)", port);
642
643 Mutex::Autolock _l(mLock);
644 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
645 return INVALID_OPERATION;
646
647 if (NULL == addrString) {
648 mRetransmitEndpointValid = false;
649 return OK;
650 }
651
652 struct in_addr saddr;
653 if(!inet_aton(addrString, &saddr)) {
654 return BAD_VALUE;
655 }
656
657 memset(&mRetransmitEndpoint, 0, sizeof(&mRetransmitEndpoint));
658 mRetransmitEndpoint.sin_family = AF_INET;
659 mRetransmitEndpoint.sin_addr = saddr;
660 mRetransmitEndpoint.sin_port = htons(port);
661 mRetransmitEndpointValid = true;
662
663 return OK;
664}
665
Gloria Wangb483c472011-04-11 17:23:27 -0700666void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667{
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700670 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671
672 // TODO: In the future, we might be on the same thread if the app is
673 // running in the same process as the media server. In that case,
674 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700675 //
Jason Sams1af452f2009-03-24 18:45:22 -0700676 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700677 // and seekTo within the same process.
678 // FIXME: Remember, this is a hack, it's not even a hack that is applied
679 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700680 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700681 mLock.lock();
682 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700683 }
Jason Sams1af452f2009-03-24 18:45:22 -0700684
Eric Laurent3b268442010-08-03 07:49:49 -0700685 // Allows calls from JNI in idle state to notify errors
686 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700688 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 return;
690 }
691
692 switch (msg) {
693 case MEDIA_NOP: // interface test message
694 break;
695 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 mCurrentState = MEDIA_PLAYER_PREPARED;
698 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 mPrepareSync = false;
701 mPrepareStatus = NO_ERROR;
702 mSignal.signal();
703 }
704 break;
705 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100706 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700707 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000708 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700709 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710 if (!mLoop) {
711 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
712 }
713 break;
714 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700715 // Always log errors.
716 // ext1: Media framework error code.
717 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000718 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
720 if (mPrepareSync)
721 {
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723 mPrepareSync = false;
724 mPrepareStatus = ext1;
725 mSignal.signal();
726 send = false;
727 }
728 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700729 case MEDIA_INFO:
730 // ext1: Media framework error code.
731 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800732 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000733 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800734 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700735 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740 mSeekPosition = -1;
741 seekTo_l(mCurrentPosition);
742 }
743 else {
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745 mCurrentPosition = mSeekPosition = -1;
746 }
747 break;
748 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 break;
751 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100752 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753 mVideoWidth = ext1;
754 mVideoHeight = ext2;
755 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700756 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700758 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 default:
Steve Block3856b092011-10-20 11:56:00 +0100760 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761 break;
762 }
763
764 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700765 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766
767 // this prevents re-entrant calls into client code
768 if ((listener != 0) && send) {
769 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700771 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100772 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 }
774}
775
Glenn Kastene1c39622012-01-04 09:36:37 -0800776/*static*/ sp<IMemory> MediaPlayer::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777{
Steve Block3856b092011-10-20 11:56:00 +0100778 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 sp<IMemory> p;
780 const sp<IMediaPlayerService>& service = getMediaPlayerService();
781 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800782 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800783 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000784 ALOGE("Unable to locate media service");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785 }
786 return p;
787
788}
789
James Dongdd172fc2010-01-15 18:13:58 -0800790void MediaPlayer::died()
791{
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800793 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
794}
795
Glenn Kastene1c39622012-01-04 09:36:37 -0800796/*static*/ sp<IMemory> MediaPlayer::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800797{
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800799 sp<IMemory> p;
800 const sp<IMediaPlayerService>& service = getMediaPlayerService();
801 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800802 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800803 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000804 ALOGE("Unable to locate media service");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805 }
806 return p;
807
808}
809
Marco Nelissen6b74d672012-02-28 16:07:44 -0800810status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
811 if (mPlayer == NULL) {
812 return NO_INIT;
813 }
814 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
815}
816
Andreas Huberb7319a72013-05-29 14:20:52 -0700817status_t MediaPlayer::updateProxyConfig(
818 const char *host, int32_t port, const char *exclusionList) {
819 const sp<IMediaPlayerService>& service = getMediaPlayerService();
820
821 if (service != NULL) {
822 return service->updateProxyConfig(host, port, exclusionList);
823 }
824
825 return INVALID_OPERATION;
826}
827
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800828}; // namespace android