blob: 0be01a9669a71207fd78bd7525a49b3896f909ba [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 Nelissend457c972014-02-11 08:47:07 -080061 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
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 Nelissend457c972014-02-11 08:47:07 -080069 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
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 {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000127 ALOGE("Unable 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(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800139 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800140 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141{
Steve Block3856b092011-10-20 11:56:00 +0100142 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 status_t err = BAD_VALUE;
144 if (url != NULL) {
145 const sp<IMediaPlayerService>& service(getMediaPlayerService());
146 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800147 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800148 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800149 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100150 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100151 }
Dave Burke06620672011-09-06 20:39:47 +0100152 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153 }
154 }
155 return err;
156}
157
158status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
159{
Steve Block3856b092011-10-20 11:56:00 +0100160 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 status_t err = UNKNOWN_ERROR;
162 const sp<IMediaPlayerService>& service(getMediaPlayerService());
163 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)) ||
166 (NO_ERROR != player->setDataSource(fd, offset, length))) {
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);
Dave Burked681bbb2011-08-30 14:39:17 +0100170 }
171 return err;
172}
173
174status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
175{
Steve Block3856b092011-10-20 11:56:00 +0100176 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100177 status_t err = UNKNOWN_ERROR;
178 const sp<IMediaPlayerService>& service(getMediaPlayerService());
179 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800180 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800181 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
182 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100183 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100184 }
Dave Burke06620672011-09-06 20:39:47 +0100185 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 }
187 return err;
188}
189
Nicolas Catania1d187f12009-05-12 23:25:55 -0700190status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
191{
192 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800193 const bool hasBeenInitialized =
194 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
195 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
196 if ((mPlayer != NULL) && hasBeenInitialized) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700197 ALOGV("invoke %d", request.dataSize());
198 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700199 }
Steve Block29357bc2012-01-06 19:20:56 +0000200 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700201 return INVALID_OPERATION;
202}
203
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700204status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
205{
Steve Blockb8a80522011-12-20 16:23:08 +0000206 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700207 Mutex::Autolock lock(mLock);
208 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700209 return NO_INIT;
210 }
211 return mPlayer->setMetadataFilter(filter);
212}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700213
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700214status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
215{
Steve Blockb8a80522011-12-20 16:23:08 +0000216 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700217 Mutex::Autolock lock(mLock);
218 if (mPlayer == NULL) {
219 return NO_INIT;
220 }
221 return mPlayer->getMetadata(update_only, apply_filter, metadata);
222}
223
Glenn Kasten11731182011-02-08 17:26:17 -0800224status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800225 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800226{
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800228 Mutex::Autolock _l(mLock);
229 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800230 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800231}
232
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233// must call with lock held
234status_t MediaPlayer::prepareAsync_l()
235{
236 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
237 mPlayer->setAudioStreamType(mStreamType);
238 mCurrentState = MEDIA_PLAYER_PREPARING;
239 return mPlayer->prepareAsync();
240 }
Steve Block29357bc2012-01-06 19:20:56 +0000241 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800242 return INVALID_OPERATION;
243}
244
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700245// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
246// one defined in the Android framework and one provided by the implementation
247// that generated the error. The sync version of prepare returns only 1 error
248// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249status_t MediaPlayer::prepare()
250{
Steve Block3856b092011-10-20 11:56:00 +0100251 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700253 mLockThreadId = getThreadId();
254 if (mPrepareSync) {
255 mLockThreadId = 0;
256 return -EALREADY;
257 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 mPrepareSync = true;
259 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700260 if (ret != NO_ERROR) {
261 mLockThreadId = 0;
262 return ret;
263 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264
265 if (mPrepareSync) {
266 mSignal.wait(mLock); // wait for prepare done
267 mPrepareSync = false;
268 }
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700270 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 return mPrepareStatus;
272}
273
274status_t MediaPlayer::prepareAsync()
275{
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 Mutex::Autolock _l(mLock);
278 return prepareAsync_l();
279}
280
281status_t MediaPlayer::start()
282{
Steve Block3856b092011-10-20 11:56:00 +0100283 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 Mutex::Autolock _l(mLock);
285 if (mCurrentState & MEDIA_PLAYER_STARTED)
286 return NO_ERROR;
287 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
288 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
289 mPlayer->setLooping(mLoop);
290 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700291 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mCurrentState = MEDIA_PLAYER_STARTED;
293 status_t ret = mPlayer->start();
294 if (ret != NO_ERROR) {
295 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
296 } else {
297 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100298 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 }
300 }
301 return ret;
302 }
Steve Block29357bc2012-01-06 19:20:56 +0000303 ALOGE("start called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 return INVALID_OPERATION;
305}
306
307status_t MediaPlayer::stop()
308{
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 Mutex::Autolock _l(mLock);
311 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
312 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
313 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
314 status_t ret = mPlayer->stop();
315 if (ret != NO_ERROR) {
316 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
317 } else {
318 mCurrentState = MEDIA_PLAYER_STOPPED;
319 }
320 return ret;
321 }
Steve Block29357bc2012-01-06 19:20:56 +0000322 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 return INVALID_OPERATION;
324}
325
326status_t MediaPlayer::pause()
327{
Steve Block3856b092011-10-20 11:56:00 +0100328 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800330 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 return NO_ERROR;
332 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
333 status_t ret = mPlayer->pause();
334 if (ret != NO_ERROR) {
335 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
336 } else {
337 mCurrentState = MEDIA_PLAYER_PAUSED;
338 }
339 return ret;
340 }
Steve Block29357bc2012-01-06 19:20:56 +0000341 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return INVALID_OPERATION;
343}
344
345bool MediaPlayer::isPlaying()
346{
347 Mutex::Autolock _l(mLock);
348 if (mPlayer != 0) {
349 bool temp = false;
350 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100351 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000353 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 mCurrentState = MEDIA_PLAYER_PAUSED;
355 }
356 return temp;
357 }
Steve Block3856b092011-10-20 11:56:00 +0100358 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 return false;
360}
361
362status_t MediaPlayer::getVideoWidth(int *w)
363{
Steve Block3856b092011-10-20 11:56:00 +0100364 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 Mutex::Autolock _l(mLock);
366 if (mPlayer == 0) return INVALID_OPERATION;
367 *w = mVideoWidth;
368 return NO_ERROR;
369}
370
371status_t MediaPlayer::getVideoHeight(int *h)
372{
Steve Block3856b092011-10-20 11:56:00 +0100373 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 Mutex::Autolock _l(mLock);
375 if (mPlayer == 0) return INVALID_OPERATION;
376 *h = mVideoHeight;
377 return NO_ERROR;
378}
379
380status_t MediaPlayer::getCurrentPosition(int *msec)
381{
Steve Block3856b092011-10-20 11:56:00 +0100382 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383 Mutex::Autolock _l(mLock);
384 if (mPlayer != 0) {
385 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100386 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 *msec = mCurrentPosition;
388 return NO_ERROR;
389 }
390 return mPlayer->getCurrentPosition(msec);
391 }
392 return INVALID_OPERATION;
393}
394
395status_t MediaPlayer::getDuration_l(int *msec)
396{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800397 ALOGV("getDuration_l");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
399 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800400 int durationMs;
401 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700402
403 if (ret != OK) {
404 // Do not enter error state just because no duration was available.
405 durationMs = -1;
406 ret = OK;
407 }
408
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800409 if (msec) {
410 *msec = durationMs;
411 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 return ret;
413 }
Steve Block29357bc2012-01-06 19:20:56 +0000414 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 return INVALID_OPERATION;
416}
417
418status_t MediaPlayer::getDuration(int *msec)
419{
420 Mutex::Autolock _l(mLock);
421 return getDuration_l(msec);
422}
423
424status_t MediaPlayer::seekTo_l(int msec)
425{
Steve Block3856b092011-10-20 11:56:00 +0100426 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
428 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000429 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800432
433 int durationMs;
434 status_t err = mPlayer->getDuration(&durationMs);
435
436 if (err != OK) {
437 ALOGW("Stream has no duration and is therefore not seekable.");
438 return err;
439 }
440
441 if (msec > durationMs) {
442 ALOGW("Attempt to seek to past end of file: request = %d, "
443 "durationMs = %d",
444 msec,
445 durationMs);
446
447 msec = durationMs;
448 }
449
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 // cache duration
451 mCurrentPosition = msec;
452 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 mSeekPosition = msec;
454 return mPlayer->seekTo(msec);
455 }
456 else {
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458 return NO_ERROR;
459 }
460 }
Steve Block29357bc2012-01-06 19:20:56 +0000461 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 -0800462 return INVALID_OPERATION;
463}
464
465status_t MediaPlayer::seekTo(int msec)
466{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700467 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700469 status_t result = seekTo_l(msec);
470 mLockThreadId = 0;
471
472 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473}
474
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700475status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 mLoop = false;
478 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
479 mPrepareSync = false;
480 if (mPlayer != 0) {
481 status_t ret = mPlayer->reset();
482 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000483 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
485 } else {
486 mCurrentState = MEDIA_PLAYER_IDLE;
487 }
James Donga1680bc2010-11-18 12:23:58 -0800488 // setDataSource has to be called again to create a
489 // new mediaplayer.
490 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 return ret;
492 }
493 clear_l();
494 return NO_ERROR;
495}
496
John Grossmanc795b642012-02-22 15:38:35 -0800497status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
498 Mutex::Autolock _l(mLock);
499
500 if (player == NULL) {
501 return UNKNOWN_ERROR;
502 }
503
504 if (mRetransmitEndpointValid) {
505 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
506 }
507
508 return OK;
509}
510
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700511status_t MediaPlayer::reset()
512{
Steve Block3856b092011-10-20 11:56:00 +0100513 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700514 Mutex::Autolock _l(mLock);
515 return reset_l();
516}
517
Glenn Kastenfff6d712012-01-12 16:38:12 -0800518status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519{
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 Mutex::Autolock _l(mLock);
522 if (mStreamType == type) return NO_ERROR;
523 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
524 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
525 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000526 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 return INVALID_OPERATION;
528 }
529 // cache
530 mStreamType = type;
531 return OK;
532}
533
John Spurlockde9453f2014-03-19 13:05:45 -0400534status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
535{
536 ALOGV("getAudioStreamType");
537 Mutex::Autolock _l(mLock);
538 *type = mStreamType;
539 return OK;
540}
541
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542status_t MediaPlayer::setLooping(int loop)
543{
Steve Block3856b092011-10-20 11:56:00 +0100544 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 Mutex::Autolock _l(mLock);
546 mLoop = (loop != 0);
547 if (mPlayer != 0) {
548 return mPlayer->setLooping(loop);
549 }
550 return OK;
551}
552
553bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100554 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 Mutex::Autolock _l(mLock);
556 if (mPlayer != 0) {
557 return mLoop;
558 }
Steve Block3856b092011-10-20 11:56:00 +0100559 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 return false;
561}
562
563status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
564{
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 Mutex::Autolock _l(mLock);
567 mLeftVolume = leftVolume;
568 mRightVolume = rightVolume;
569 if (mPlayer != 0) {
570 return mPlayer->setVolume(leftVolume, rightVolume);
571 }
572 return OK;
573}
574
Eric Laurenta514bdb2010-06-21 09:27:30 -0700575status_t MediaPlayer::setAudioSessionId(int sessionId)
576{
Steve Block3856b092011-10-20 11:56:00 +0100577 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700578 Mutex::Autolock _l(mLock);
579 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000580 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700581 return INVALID_OPERATION;
582 }
583 if (sessionId < 0) {
584 return BAD_VALUE;
585 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700586 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800587 AudioSystem::acquireAudioSessionId(sessionId, -1);
588 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700589 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700590 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700591 return NO_ERROR;
592}
593
594int MediaPlayer::getAudioSessionId()
595{
596 Mutex::Autolock _l(mLock);
597 return mAudioSessionId;
598}
599
Eric Laurent2beeb502010-07-16 07:43:46 -0700600status_t MediaPlayer::setAuxEffectSendLevel(float level)
601{
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700603 Mutex::Autolock _l(mLock);
604 mSendLevel = level;
605 if (mPlayer != 0) {
606 return mPlayer->setAuxEffectSendLevel(level);
607 }
608 return OK;
609}
610
611status_t MediaPlayer::attachAuxEffect(int effectId)
612{
Steve Block3856b092011-10-20 11:56:00 +0100613 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700614 Mutex::Autolock _l(mLock);
615 if (mPlayer == 0 ||
616 (mCurrentState & MEDIA_PLAYER_IDLE) ||
617 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000618 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700619 return INVALID_OPERATION;
620 }
621
622 return mPlayer->attachAuxEffect(effectId);
623}
624
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700625status_t MediaPlayer::setParameter(int key, const Parcel& request)
626{
Steve Block3856b092011-10-20 11:56:00 +0100627 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700628 Mutex::Autolock _l(mLock);
629 if (mPlayer != NULL) {
630 return mPlayer->setParameter(key, request);
631 }
Steve Block3856b092011-10-20 11:56:00 +0100632 ALOGV("setParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700633 return INVALID_OPERATION;
634}
635
636status_t MediaPlayer::getParameter(int key, Parcel *reply)
637{
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700639 Mutex::Autolock _l(mLock);
640 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700641 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700642 }
Steve Block3856b092011-10-20 11:56:00 +0100643 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700644 return INVALID_OPERATION;
645}
646
John Grossmanc795b642012-02-22 15:38:35 -0800647status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
648 uint16_t port) {
649 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
650 addrString ? addrString : "(null)", port);
651
652 Mutex::Autolock _l(mLock);
653 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
654 return INVALID_OPERATION;
655
656 if (NULL == addrString) {
657 mRetransmitEndpointValid = false;
658 return OK;
659 }
660
661 struct in_addr saddr;
662 if(!inet_aton(addrString, &saddr)) {
663 return BAD_VALUE;
664 }
665
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800666 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800667 mRetransmitEndpoint.sin_family = AF_INET;
668 mRetransmitEndpoint.sin_addr = saddr;
669 mRetransmitEndpoint.sin_port = htons(port);
670 mRetransmitEndpointValid = true;
671
672 return OK;
673}
674
Gloria Wangb483c472011-04-11 17:23:27 -0700675void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676{
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700679 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680
681 // TODO: In the future, we might be on the same thread if the app is
682 // running in the same process as the media server. In that case,
683 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700684 //
Jason Sams1af452f2009-03-24 18:45:22 -0700685 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700686 // and seekTo within the same process.
687 // FIXME: Remember, this is a hack, it's not even a hack that is applied
688 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700689 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700690 mLock.lock();
691 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700692 }
Jason Sams1af452f2009-03-24 18:45:22 -0700693
Eric Laurent3b268442010-08-03 07:49:49 -0700694 // Allows calls from JNI in idle state to notify errors
695 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700697 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 return;
699 }
700
701 switch (msg) {
702 case MEDIA_NOP: // interface test message
703 break;
704 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 mCurrentState = MEDIA_PLAYER_PREPARED;
707 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709 mPrepareSync = false;
710 mPrepareStatus = NO_ERROR;
711 mSignal.signal();
712 }
713 break;
714 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700716 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000717 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700718 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 if (!mLoop) {
720 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
721 }
722 break;
723 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700724 // Always log errors.
725 // ext1: Media framework error code.
726 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000727 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
729 if (mPrepareSync)
730 {
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732 mPrepareSync = false;
733 mPrepareStatus = ext1;
734 mSignal.signal();
735 send = false;
736 }
737 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700738 case MEDIA_INFO:
739 // ext1: Media framework error code.
740 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800741 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000742 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800743 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700744 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749 mSeekPosition = -1;
750 seekTo_l(mCurrentPosition);
751 }
752 else {
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754 mCurrentPosition = mSeekPosition = -1;
755 }
756 break;
757 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100758 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 break;
760 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762 mVideoWidth = ext1;
763 mVideoHeight = ext2;
764 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700765 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700767 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700768 case MEDIA_SUBTITLE_DATA:
769 ALOGV("Received subtitle data message");
770 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 default:
Steve Block3856b092011-10-20 11:56:00 +0100772 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 break;
774 }
775
776 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700777 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778
779 // this prevents re-entrant calls into client code
780 if ((listener != 0) && send) {
781 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100782 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700783 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100784 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785 }
786}
787
Andreas Huber1b86fe02014-01-29 11:13:26 -0800788/*static*/ status_t MediaPlayer::decode(
789 const sp<IMediaHTTPService> &httpService,
790 const char* url,
791 uint32_t *pSampleRate,
792 int* pNumChannels,
793 audio_format_t* pFormat,
794 const sp<IMemoryHeap>& heap,
795 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796{
Steve Block3856b092011-10-20 11:56:00 +0100797 ALOGV("decode(%s)", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700798 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800799 const sp<IMediaPlayerService>& service = getMediaPlayerService();
800 if (service != 0) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800801 status = service->decode(httpService, url, pSampleRate, pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800802 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000803 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700804 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700806 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807
808}
809
James Dongdd172fc2010-01-15 18:13:58 -0800810void MediaPlayer::died()
811{
Steve Block3856b092011-10-20 11:56:00 +0100812 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800813 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
814}
815
Eric Laurent3d00aa62013-09-24 09:53:27 -0700816/*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length,
817 uint32_t *pSampleRate, int* pNumChannels,
818 audio_format_t* pFormat,
819 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800820{
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700822 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823 const sp<IMediaPlayerService>& service = getMediaPlayerService();
824 if (service != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700825 status = service->decode(fd, offset, length, pSampleRate,
826 pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800827 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000828 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700829 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800830 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700831 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800832
833}
834
Marco Nelissen6b74d672012-02-28 16:07:44 -0800835status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
836 if (mPlayer == NULL) {
837 return NO_INIT;
838 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700839
840 if (next != NULL && !(next->mCurrentState &
841 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
842 ALOGE("next player is not prepared");
843 return INVALID_OPERATION;
844 }
845
Marco Nelissen6b74d672012-02-28 16:07:44 -0800846 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
847}
848
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849}; // namespace android