blob: dbff8dca57507e18f94106598e57ef5959bbae4c [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
Jamie Gennis61c7ef52011-07-13 12:59:34 -070030#include <gui/SurfaceTextureClient.h>
31
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(
224 const sp<ISurfaceTexture>& surfaceTexture)
225{
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;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700229 return mPlayer->setVideoSurfaceTexture(surfaceTexture);
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);
401 if (msec) {
402 *msec = durationMs;
403 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 return ret;
405 }
Steve Block29357bc2012-01-06 19:20:56 +0000406 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 return INVALID_OPERATION;
408}
409
410status_t MediaPlayer::getDuration(int *msec)
411{
412 Mutex::Autolock _l(mLock);
413 return getDuration_l(msec);
414}
415
416status_t MediaPlayer::seekTo_l(int msec)
417{
Steve Block3856b092011-10-20 11:56:00 +0100418 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
420 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000421 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800424
425 int durationMs;
426 status_t err = mPlayer->getDuration(&durationMs);
427
428 if (err != OK) {
429 ALOGW("Stream has no duration and is therefore not seekable.");
430 return err;
431 }
432
433 if (msec > durationMs) {
434 ALOGW("Attempt to seek to past end of file: request = %d, "
435 "durationMs = %d",
436 msec,
437 durationMs);
438
439 msec = durationMs;
440 }
441
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 // cache duration
443 mCurrentPosition = msec;
444 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 mSeekPosition = msec;
446 return mPlayer->seekTo(msec);
447 }
448 else {
Steve Block3856b092011-10-20 11:56:00 +0100449 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 return NO_ERROR;
451 }
452 }
Steve Block29357bc2012-01-06 19:20:56 +0000453 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 -0800454 return INVALID_OPERATION;
455}
456
457status_t MediaPlayer::seekTo(int msec)
458{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700459 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700461 status_t result = seekTo_l(msec);
462 mLockThreadId = 0;
463
464 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465}
466
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700467status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 mLoop = false;
470 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
471 mPrepareSync = false;
472 if (mPlayer != 0) {
473 status_t ret = mPlayer->reset();
474 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000475 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
477 } else {
478 mCurrentState = MEDIA_PLAYER_IDLE;
479 }
James Donga1680bc2010-11-18 12:23:58 -0800480 // setDataSource has to be called again to create a
481 // new mediaplayer.
482 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483 return ret;
484 }
485 clear_l();
486 return NO_ERROR;
487}
488
John Grossmanc795b642012-02-22 15:38:35 -0800489status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
490 Mutex::Autolock _l(mLock);
491
492 if (player == NULL) {
493 return UNKNOWN_ERROR;
494 }
495
496 if (mRetransmitEndpointValid) {
497 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
498 }
499
500 return OK;
501}
502
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700503status_t MediaPlayer::reset()
504{
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700506 Mutex::Autolock _l(mLock);
507 return reset_l();
508}
509
Glenn Kastenfff6d712012-01-12 16:38:12 -0800510status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511{
Steve Block3856b092011-10-20 11:56:00 +0100512 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 Mutex::Autolock _l(mLock);
514 if (mStreamType == type) return NO_ERROR;
515 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
516 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
517 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000518 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 return INVALID_OPERATION;
520 }
521 // cache
522 mStreamType = type;
523 return OK;
524}
525
526status_t MediaPlayer::setLooping(int loop)
527{
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 Mutex::Autolock _l(mLock);
530 mLoop = (loop != 0);
531 if (mPlayer != 0) {
532 return mPlayer->setLooping(loop);
533 }
534 return OK;
535}
536
537bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100538 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 Mutex::Autolock _l(mLock);
540 if (mPlayer != 0) {
541 return mLoop;
542 }
Steve Block3856b092011-10-20 11:56:00 +0100543 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544 return false;
545}
546
547status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
548{
Steve Block3856b092011-10-20 11:56:00 +0100549 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 Mutex::Autolock _l(mLock);
551 mLeftVolume = leftVolume;
552 mRightVolume = rightVolume;
553 if (mPlayer != 0) {
554 return mPlayer->setVolume(leftVolume, rightVolume);
555 }
556 return OK;
557}
558
Eric Laurenta514bdb2010-06-21 09:27:30 -0700559status_t MediaPlayer::setAudioSessionId(int sessionId)
560{
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700562 Mutex::Autolock _l(mLock);
563 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000564 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700565 return INVALID_OPERATION;
566 }
567 if (sessionId < 0) {
568 return BAD_VALUE;
569 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700570 if (sessionId != mAudioSessionId) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700571 AudioSystem::releaseAudioSessionId(mAudioSessionId);
572 AudioSystem::acquireAudioSessionId(sessionId);
573 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700574 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700575 return NO_ERROR;
576}
577
578int MediaPlayer::getAudioSessionId()
579{
580 Mutex::Autolock _l(mLock);
581 return mAudioSessionId;
582}
583
Eric Laurent2beeb502010-07-16 07:43:46 -0700584status_t MediaPlayer::setAuxEffectSendLevel(float level)
585{
Steve Block3856b092011-10-20 11:56:00 +0100586 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700587 Mutex::Autolock _l(mLock);
588 mSendLevel = level;
589 if (mPlayer != 0) {
590 return mPlayer->setAuxEffectSendLevel(level);
591 }
592 return OK;
593}
594
595status_t MediaPlayer::attachAuxEffect(int effectId)
596{
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700598 Mutex::Autolock _l(mLock);
599 if (mPlayer == 0 ||
600 (mCurrentState & MEDIA_PLAYER_IDLE) ||
601 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000602 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700603 return INVALID_OPERATION;
604 }
605
606 return mPlayer->attachAuxEffect(effectId);
607}
608
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700609status_t MediaPlayer::setParameter(int key, const Parcel& request)
610{
Steve Block3856b092011-10-20 11:56:00 +0100611 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700612 Mutex::Autolock _l(mLock);
613 if (mPlayer != NULL) {
614 return mPlayer->setParameter(key, request);
615 }
Steve Block3856b092011-10-20 11:56:00 +0100616 ALOGV("setParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700617 return INVALID_OPERATION;
618}
619
620status_t MediaPlayer::getParameter(int key, Parcel *reply)
621{
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700623 Mutex::Autolock _l(mLock);
624 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700625 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700626 }
Steve Block3856b092011-10-20 11:56:00 +0100627 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700628 return INVALID_OPERATION;
629}
630
John Grossmanc795b642012-02-22 15:38:35 -0800631status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
632 uint16_t port) {
633 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
634 addrString ? addrString : "(null)", port);
635
636 Mutex::Autolock _l(mLock);
637 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
638 return INVALID_OPERATION;
639
640 if (NULL == addrString) {
641 mRetransmitEndpointValid = false;
642 return OK;
643 }
644
645 struct in_addr saddr;
646 if(!inet_aton(addrString, &saddr)) {
647 return BAD_VALUE;
648 }
649
650 memset(&mRetransmitEndpoint, 0, sizeof(&mRetransmitEndpoint));
651 mRetransmitEndpoint.sin_family = AF_INET;
652 mRetransmitEndpoint.sin_addr = saddr;
653 mRetransmitEndpoint.sin_port = htons(port);
654 mRetransmitEndpointValid = true;
655
656 return OK;
657}
658
Gloria Wangb483c472011-04-11 17:23:27 -0700659void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660{
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700663 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664
665 // TODO: In the future, we might be on the same thread if the app is
666 // running in the same process as the media server. In that case,
667 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700668 //
Jason Sams1af452f2009-03-24 18:45:22 -0700669 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700670 // and seekTo within the same process.
671 // FIXME: Remember, this is a hack, it's not even a hack that is applied
672 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700673 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700674 mLock.lock();
675 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700676 }
Jason Sams1af452f2009-03-24 18:45:22 -0700677
Eric Laurent3b268442010-08-03 07:49:49 -0700678 // Allows calls from JNI in idle state to notify errors
679 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700681 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682 return;
683 }
684
685 switch (msg) {
686 case MEDIA_NOP: // interface test message
687 break;
688 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690 mCurrentState = MEDIA_PLAYER_PREPARED;
691 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 mPrepareSync = false;
694 mPrepareStatus = NO_ERROR;
695 mSignal.signal();
696 }
697 break;
698 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700700 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000701 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700702 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703 if (!mLoop) {
704 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
705 }
706 break;
707 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700708 // Always log errors.
709 // ext1: Media framework error code.
710 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000711 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
713 if (mPrepareSync)
714 {
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 mPrepareSync = false;
717 mPrepareStatus = ext1;
718 mSignal.signal();
719 send = false;
720 }
721 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700722 case MEDIA_INFO:
723 // ext1: Media framework error code.
724 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800725 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000726 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800727 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700728 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100730 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733 mSeekPosition = -1;
734 seekTo_l(mCurrentPosition);
735 }
736 else {
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 mCurrentPosition = mSeekPosition = -1;
739 }
740 break;
741 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100742 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743 break;
744 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 mVideoWidth = ext1;
747 mVideoHeight = ext2;
748 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700749 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100750 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700751 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752 default:
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754 break;
755 }
756
757 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700758 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759
760 // this prevents re-entrant calls into client code
761 if ((listener != 0) && send) {
762 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700764 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766 }
767}
768
Glenn Kastene1c39622012-01-04 09:36:37 -0800769/*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 -0800770{
Steve Block3856b092011-10-20 11:56:00 +0100771 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772 sp<IMemory> p;
773 const sp<IMediaPlayerService>& service = getMediaPlayerService();
774 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800775 p = service->decode(url, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800776 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000777 ALOGE("Unable to locate media service");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 }
779 return p;
780
781}
782
James Dongdd172fc2010-01-15 18:13:58 -0800783void MediaPlayer::died()
784{
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800786 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
787}
788
Glenn Kastene1c39622012-01-04 09:36:37 -0800789/*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 -0800790{
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800792 sp<IMemory> p;
793 const sp<IMediaPlayerService>& service = getMediaPlayerService();
794 if (service != 0) {
James Dongdd172fc2010-01-15 18:13:58 -0800795 p = service->decode(fd, offset, length, pSampleRate, pNumChannels, pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000797 ALOGE("Unable to locate media service");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798 }
799 return p;
800
801}
802
Marco Nelissen6b74d672012-02-28 16:07:44 -0800803status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
804 if (mPlayer == NULL) {
805 return NO_INIT;
806 }
807 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
808}
809
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800810}; // namespace android