blob: 889bd7fca709ffcd702a1cdaef01a8d13b601137 [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"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080021#include <fcntl.h>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070022#include <inttypes.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
Mathias Agopian75624082009-05-19 19:08:10 -070029#include <binder/IServiceManager.h>
30#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080032#include <gui/Surface.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034#include <media/mediaplayer.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080035#include <media/AudioSystem.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080038
Andreas Huber2db84552010-01-28 11:19:57 -080039#include <utils/KeyedVector.h>
40#include <utils/String8.h>
41
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070043#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045namespace android {
46
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047MediaPlayer::MediaPlayer()
48{
Steve Block3856b092011-10-20 11:56:00 +010049 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050 mListener = NULL;
51 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070052 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mCurrentPosition = -1;
54 mSeekPosition = -1;
55 mCurrentState = MEDIA_PLAYER_IDLE;
56 mPrepareSync = false;
57 mPrepareStatus = NO_ERROR;
58 mLoop = false;
59 mLeftVolume = mRightVolume = 1.0;
60 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070061 mLockThreadId = 0;
Eric Laurenta514bdb2010-06-21 09:27:30 -070062 mAudioSessionId = AudioSystem::newAudioSessionId();
Marco Nelissend457c972014-02-11 08:47:07 -080063 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070064 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080065 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066}
67
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068MediaPlayer::~MediaPlayer()
69{
Steve Block3856b092011-10-20 11:56:00 +010070 ALOGV("destructor");
Marco Nelissend457c972014-02-11 08:47:07 -080071 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072 disconnect();
73 IPCThreadState::self()->flushCommands();
74}
75
76void MediaPlayer::disconnect()
77{
Steve Block3856b092011-10-20 11:56:00 +010078 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080079 sp<IMediaPlayer> p;
80 {
81 Mutex::Autolock _l(mLock);
82 p = mPlayer;
83 mPlayer.clear();
84 }
85
86 if (p != 0) {
87 p->disconnect();
88 }
89}
90
91// always call with lock held
92void MediaPlayer::clear_l()
93{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 mCurrentPosition = -1;
95 mSeekPosition = -1;
96 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -080097 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098}
99
100status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
101{
Steve Block3856b092011-10-20 11:56:00 +0100102 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103 Mutex::Autolock _l(mLock);
104 mListener = listener;
105 return NO_ERROR;
106}
107
108
Dave Burked681bbb2011-08-30 14:39:17 +0100109status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110{
111 status_t err = UNKNOWN_ERROR;
112 sp<IMediaPlayer> p;
113 { // scope for the lock
114 Mutex::Autolock _l(mLock);
115
Marco Nelissen83ff1432010-03-10 10:53:16 -0800116 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
117 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000118 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119 return INVALID_OPERATION;
120 }
121
122 clear_l();
123 p = mPlayer;
124 mPlayer = player;
125 if (player != 0) {
126 mCurrentState = MEDIA_PLAYER_INITIALIZED;
127 err = NO_ERROR;
128 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000129 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 }
131 }
132
133 if (p != 0) {
134 p->disconnect();
135 }
136
137 return err;
138}
139
Andreas Huber2db84552010-01-28 11:19:57 -0800140status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800141 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800142 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Steve Block3856b092011-10-20 11:56:00 +0100144 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 status_t err = BAD_VALUE;
146 if (url != NULL) {
147 const sp<IMediaPlayerService>& service(getMediaPlayerService());
148 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800149 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800150 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800151 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100152 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100153 }
Dave Burke06620672011-09-06 20:39:47 +0100154 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 }
156 }
157 return err;
158}
159
160status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
161{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700162 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 status_t err = UNKNOWN_ERROR;
164 const sp<IMediaPlayerService>& service(getMediaPlayerService());
165 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800166 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800167 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
168 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100169 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100170 }
Dave Burke06620672011-09-06 20:39:47 +0100171 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100172 }
173 return err;
174}
175
176status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
177{
Steve Block3856b092011-10-20 11:56:00 +0100178 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100179 status_t err = UNKNOWN_ERROR;
180 const sp<IMediaPlayerService>& service(getMediaPlayerService());
181 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800182 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800183 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
184 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100185 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100186 }
Dave Burke06620672011-09-06 20:39:47 +0100187 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 }
189 return err;
190}
191
Nicolas Catania1d187f12009-05-12 23:25:55 -0700192status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
193{
194 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800195 const bool hasBeenInitialized =
196 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
197 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
198 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700199 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700200 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700201 }
Steve Block29357bc2012-01-06 19:20:56 +0000202 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700203 return INVALID_OPERATION;
204}
205
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700206status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
207{
Steve Blockb8a80522011-12-20 16:23:08 +0000208 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700209 Mutex::Autolock lock(mLock);
210 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700211 return NO_INIT;
212 }
213 return mPlayer->setMetadataFilter(filter);
214}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700215
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700216status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
217{
Steve Blockb8a80522011-12-20 16:23:08 +0000218 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700219 Mutex::Autolock lock(mLock);
220 if (mPlayer == NULL) {
221 return NO_INIT;
222 }
223 return mPlayer->getMetadata(update_only, apply_filter, metadata);
224}
225
Glenn Kasten11731182011-02-08 17:26:17 -0800226status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800227 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800228{
Steve Block3856b092011-10-20 11:56:00 +0100229 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800230 Mutex::Autolock _l(mLock);
231 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800232 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800233}
234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235// must call with lock held
236status_t MediaPlayer::prepareAsync_l()
237{
238 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
239 mPlayer->setAudioStreamType(mStreamType);
240 mCurrentState = MEDIA_PLAYER_PREPARING;
241 return mPlayer->prepareAsync();
242 }
Steve Block29357bc2012-01-06 19:20:56 +0000243 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 return INVALID_OPERATION;
245}
246
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700247// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
248// one defined in the Android framework and one provided by the implementation
249// that generated the error. The sync version of prepare returns only 1 error
250// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251status_t MediaPlayer::prepare()
252{
Steve Block3856b092011-10-20 11:56:00 +0100253 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700255 mLockThreadId = getThreadId();
256 if (mPrepareSync) {
257 mLockThreadId = 0;
258 return -EALREADY;
259 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260 mPrepareSync = true;
261 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700262 if (ret != NO_ERROR) {
263 mLockThreadId = 0;
264 return ret;
265 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266
267 if (mPrepareSync) {
268 mSignal.wait(mLock); // wait for prepare done
269 mPrepareSync = false;
270 }
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700272 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 return mPrepareStatus;
274}
275
276status_t MediaPlayer::prepareAsync()
277{
Steve Block3856b092011-10-20 11:56:00 +0100278 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 Mutex::Autolock _l(mLock);
280 return prepareAsync_l();
281}
282
283status_t MediaPlayer::start()
284{
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286 Mutex::Autolock _l(mLock);
287 if (mCurrentState & MEDIA_PLAYER_STARTED)
288 return NO_ERROR;
289 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
290 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
291 mPlayer->setLooping(mLoop);
292 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700293 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294 mCurrentState = MEDIA_PLAYER_STARTED;
295 status_t ret = mPlayer->start();
296 if (ret != NO_ERROR) {
297 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
298 } else {
299 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100300 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 }
302 }
303 return ret;
304 }
Steve Block29357bc2012-01-06 19:20:56 +0000305 ALOGE("start called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 return INVALID_OPERATION;
307}
308
309status_t MediaPlayer::stop()
310{
Steve Block3856b092011-10-20 11:56:00 +0100311 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 Mutex::Autolock _l(mLock);
313 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
314 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
315 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
316 status_t ret = mPlayer->stop();
317 if (ret != NO_ERROR) {
318 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
319 } else {
320 mCurrentState = MEDIA_PLAYER_STOPPED;
321 }
322 return ret;
323 }
Steve Block29357bc2012-01-06 19:20:56 +0000324 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 return INVALID_OPERATION;
326}
327
328status_t MediaPlayer::pause()
329{
Steve Block3856b092011-10-20 11:56:00 +0100330 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800332 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 return NO_ERROR;
334 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
335 status_t ret = mPlayer->pause();
336 if (ret != NO_ERROR) {
337 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
338 } else {
339 mCurrentState = MEDIA_PLAYER_PAUSED;
340 }
341 return ret;
342 }
Steve Block29357bc2012-01-06 19:20:56 +0000343 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 return INVALID_OPERATION;
345}
346
347bool MediaPlayer::isPlaying()
348{
349 Mutex::Autolock _l(mLock);
350 if (mPlayer != 0) {
351 bool temp = false;
352 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100353 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000355 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 mCurrentState = MEDIA_PLAYER_PAUSED;
357 }
358 return temp;
359 }
Steve Block3856b092011-10-20 11:56:00 +0100360 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 return false;
362}
363
364status_t MediaPlayer::getVideoWidth(int *w)
365{
Steve Block3856b092011-10-20 11:56:00 +0100366 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367 Mutex::Autolock _l(mLock);
368 if (mPlayer == 0) return INVALID_OPERATION;
369 *w = mVideoWidth;
370 return NO_ERROR;
371}
372
373status_t MediaPlayer::getVideoHeight(int *h)
374{
Steve Block3856b092011-10-20 11:56:00 +0100375 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 Mutex::Autolock _l(mLock);
377 if (mPlayer == 0) return INVALID_OPERATION;
378 *h = mVideoHeight;
379 return NO_ERROR;
380}
381
382status_t MediaPlayer::getCurrentPosition(int *msec)
383{
Steve Block3856b092011-10-20 11:56:00 +0100384 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 Mutex::Autolock _l(mLock);
386 if (mPlayer != 0) {
387 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 *msec = mCurrentPosition;
390 return NO_ERROR;
391 }
392 return mPlayer->getCurrentPosition(msec);
393 }
394 return INVALID_OPERATION;
395}
396
397status_t MediaPlayer::getDuration_l(int *msec)
398{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800399 ALOGV("getDuration_l");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
401 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800402 int durationMs;
403 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700404
405 if (ret != OK) {
406 // Do not enter error state just because no duration was available.
407 durationMs = -1;
408 ret = OK;
409 }
410
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800411 if (msec) {
412 *msec = durationMs;
413 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 return ret;
415 }
Steve Block29357bc2012-01-06 19:20:56 +0000416 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 return INVALID_OPERATION;
418}
419
420status_t MediaPlayer::getDuration(int *msec)
421{
422 Mutex::Autolock _l(mLock);
423 return getDuration_l(msec);
424}
425
426status_t MediaPlayer::seekTo_l(int msec)
427{
Steve Block3856b092011-10-20 11:56:00 +0100428 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
430 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000431 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800434
435 int durationMs;
436 status_t err = mPlayer->getDuration(&durationMs);
437
438 if (err != OK) {
439 ALOGW("Stream has no duration and is therefore not seekable.");
440 return err;
441 }
442
443 if (msec > durationMs) {
444 ALOGW("Attempt to seek to past end of file: request = %d, "
445 "durationMs = %d",
446 msec,
447 durationMs);
448
449 msec = durationMs;
450 }
451
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 // cache duration
453 mCurrentPosition = msec;
454 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 mSeekPosition = msec;
456 return mPlayer->seekTo(msec);
457 }
458 else {
Steve Block3856b092011-10-20 11:56:00 +0100459 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 return NO_ERROR;
461 }
462 }
Steve Block29357bc2012-01-06 19:20:56 +0000463 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 -0800464 return INVALID_OPERATION;
465}
466
467status_t MediaPlayer::seekTo(int msec)
468{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700469 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700471 status_t result = seekTo_l(msec);
472 mLockThreadId = 0;
473
474 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475}
476
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700477status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 mLoop = false;
480 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
481 mPrepareSync = false;
482 if (mPlayer != 0) {
483 status_t ret = mPlayer->reset();
484 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000485 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
487 } else {
488 mCurrentState = MEDIA_PLAYER_IDLE;
489 }
James Donga1680bc2010-11-18 12:23:58 -0800490 // setDataSource has to be called again to create a
491 // new mediaplayer.
492 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 return ret;
494 }
495 clear_l();
496 return NO_ERROR;
497}
498
John Grossmanc795b642012-02-22 15:38:35 -0800499status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
500 Mutex::Autolock _l(mLock);
501
502 if (player == NULL) {
503 return UNKNOWN_ERROR;
504 }
505
506 if (mRetransmitEndpointValid) {
507 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
508 }
509
510 return OK;
511}
512
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700513status_t MediaPlayer::reset()
514{
Steve Block3856b092011-10-20 11:56:00 +0100515 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700516 Mutex::Autolock _l(mLock);
517 return reset_l();
518}
519
Glenn Kastenfff6d712012-01-12 16:38:12 -0800520status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521{
Steve Block3856b092011-10-20 11:56:00 +0100522 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 Mutex::Autolock _l(mLock);
524 if (mStreamType == type) return NO_ERROR;
525 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
526 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
527 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000528 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 return INVALID_OPERATION;
530 }
531 // cache
532 mStreamType = type;
533 return OK;
534}
535
John Spurlockde9453f2014-03-19 13:05:45 -0400536status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
537{
538 ALOGV("getAudioStreamType");
539 Mutex::Autolock _l(mLock);
540 *type = mStreamType;
541 return OK;
542}
543
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544status_t MediaPlayer::setLooping(int loop)
545{
Steve Block3856b092011-10-20 11:56:00 +0100546 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 Mutex::Autolock _l(mLock);
548 mLoop = (loop != 0);
549 if (mPlayer != 0) {
550 return mPlayer->setLooping(loop);
551 }
552 return OK;
553}
554
555bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100556 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 Mutex::Autolock _l(mLock);
558 if (mPlayer != 0) {
559 return mLoop;
560 }
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 return false;
563}
564
565status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
566{
Steve Block3856b092011-10-20 11:56:00 +0100567 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 Mutex::Autolock _l(mLock);
569 mLeftVolume = leftVolume;
570 mRightVolume = rightVolume;
571 if (mPlayer != 0) {
572 return mPlayer->setVolume(leftVolume, rightVolume);
573 }
574 return OK;
575}
576
Eric Laurenta514bdb2010-06-21 09:27:30 -0700577status_t MediaPlayer::setAudioSessionId(int sessionId)
578{
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700580 Mutex::Autolock _l(mLock);
581 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000582 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700583 return INVALID_OPERATION;
584 }
585 if (sessionId < 0) {
586 return BAD_VALUE;
587 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700588 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800589 AudioSystem::acquireAudioSessionId(sessionId, -1);
590 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700591 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700592 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700593 return NO_ERROR;
594}
595
596int MediaPlayer::getAudioSessionId()
597{
598 Mutex::Autolock _l(mLock);
599 return mAudioSessionId;
600}
601
Eric Laurent2beeb502010-07-16 07:43:46 -0700602status_t MediaPlayer::setAuxEffectSendLevel(float level)
603{
Steve Block3856b092011-10-20 11:56:00 +0100604 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700605 Mutex::Autolock _l(mLock);
606 mSendLevel = level;
607 if (mPlayer != 0) {
608 return mPlayer->setAuxEffectSendLevel(level);
609 }
610 return OK;
611}
612
613status_t MediaPlayer::attachAuxEffect(int effectId)
614{
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700616 Mutex::Autolock _l(mLock);
617 if (mPlayer == 0 ||
618 (mCurrentState & MEDIA_PLAYER_IDLE) ||
619 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000620 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700621 return INVALID_OPERATION;
622 }
623
624 return mPlayer->attachAuxEffect(effectId);
625}
626
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700627// always call with lock held
628status_t MediaPlayer::checkStateForKeySet_l(int key)
629{
630 switch(key) {
631 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
632 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
633 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
634 // Can't change the audio attributes after prepare
635 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
636 return INVALID_OPERATION;
637 }
638 break;
639 default:
640 // parameter doesn't require player state check
641 break;
642 }
643 return OK;
644}
645
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700646status_t MediaPlayer::setParameter(int key, const Parcel& request)
647{
Steve Block3856b092011-10-20 11:56:00 +0100648 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700649 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700650 if (checkStateForKeySet_l(key) != OK) {
651 return INVALID_OPERATION;
652 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700653 if (mPlayer != NULL) {
654 return mPlayer->setParameter(key, request);
655 }
Steve Block3856b092011-10-20 11:56:00 +0100656 ALOGV("setParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700657 return INVALID_OPERATION;
658}
659
660status_t MediaPlayer::getParameter(int key, Parcel *reply)
661{
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700663 Mutex::Autolock _l(mLock);
664 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700665 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700666 }
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700668 return INVALID_OPERATION;
669}
670
John Grossmanc795b642012-02-22 15:38:35 -0800671status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
672 uint16_t port) {
673 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
674 addrString ? addrString : "(null)", port);
675
676 Mutex::Autolock _l(mLock);
677 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
678 return INVALID_OPERATION;
679
680 if (NULL == addrString) {
681 mRetransmitEndpointValid = false;
682 return OK;
683 }
684
685 struct in_addr saddr;
686 if(!inet_aton(addrString, &saddr)) {
687 return BAD_VALUE;
688 }
689
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800690 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800691 mRetransmitEndpoint.sin_family = AF_INET;
692 mRetransmitEndpoint.sin_addr = saddr;
693 mRetransmitEndpoint.sin_port = htons(port);
694 mRetransmitEndpointValid = true;
695
696 return OK;
697}
698
Gloria Wangb483c472011-04-11 17:23:27 -0700699void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700{
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700703 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704
705 // TODO: In the future, we might be on the same thread if the app is
706 // running in the same process as the media server. In that case,
707 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700708 //
Jason Sams1af452f2009-03-24 18:45:22 -0700709 // The threadId hack below works around this for the care of prepare
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700710 // and seekTo within the same process.
711 // FIXME: Remember, this is a hack, it's not even a hack that is applied
712 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700713 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700714 mLock.lock();
715 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700716 }
Jason Sams1af452f2009-03-24 18:45:22 -0700717
Eric Laurent3b268442010-08-03 07:49:49 -0700718 // Allows calls from JNI in idle state to notify errors
719 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100720 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700721 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722 return;
723 }
724
725 switch (msg) {
726 case MEDIA_NOP: // interface test message
727 break;
728 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100729 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 mCurrentState = MEDIA_PLAYER_PREPARED;
731 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733 mPrepareSync = false;
734 mPrepareStatus = NO_ERROR;
735 mSignal.signal();
736 }
737 break;
738 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700740 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000741 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700742 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743 if (!mLoop) {
744 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
745 }
746 break;
747 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700748 // Always log errors.
749 // ext1: Media framework error code.
750 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000751 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
753 if (mPrepareSync)
754 {
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 mPrepareSync = false;
757 mPrepareStatus = ext1;
758 mSignal.signal();
759 send = false;
760 }
761 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700762 case MEDIA_INFO:
763 // ext1: Media framework error code.
764 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800765 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000766 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800767 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700768 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100772 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 mSeekPosition = -1;
774 seekTo_l(mCurrentPosition);
775 }
776 else {
Steve Block3856b092011-10-20 11:56:00 +0100777 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 mCurrentPosition = mSeekPosition = -1;
779 }
780 break;
781 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100782 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800783 break;
784 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800786 mVideoWidth = ext1;
787 mVideoHeight = ext2;
788 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700789 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100790 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700791 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700792 case MEDIA_SUBTITLE_DATA:
793 ALOGV("Received subtitle data message");
794 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 default:
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800797 break;
798 }
799
800 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700801 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800802
803 // this prevents re-entrant calls into client code
804 if ((listener != 0) && send) {
805 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700807 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800809 }
810}
811
Andreas Huber1b86fe02014-01-29 11:13:26 -0800812/*static*/ status_t MediaPlayer::decode(
813 const sp<IMediaHTTPService> &httpService,
814 const char* url,
815 uint32_t *pSampleRate,
816 int* pNumChannels,
817 audio_format_t* pFormat,
818 const sp<IMemoryHeap>& heap,
819 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800820{
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("decode(%s)", url);
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) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800825 status = service->decode(httpService, url, pSampleRate, pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800826 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000827 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700828 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800829 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700830 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831
832}
833
James Dongdd172fc2010-01-15 18:13:58 -0800834void MediaPlayer::died()
835{
Steve Block3856b092011-10-20 11:56:00 +0100836 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800837 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
838}
839
Eric Laurent3d00aa62013-09-24 09:53:27 -0700840/*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length,
841 uint32_t *pSampleRate, int* pNumChannels,
842 audio_format_t* pFormat,
843 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700845 ALOGV("decode(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700846 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800847 const sp<IMediaPlayerService>& service = getMediaPlayerService();
848 if (service != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700849 status = service->decode(fd, offset, length, pSampleRate,
850 pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000852 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700853 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800854 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700855 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856
857}
858
Marco Nelissen6b74d672012-02-28 16:07:44 -0800859status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
860 if (mPlayer == NULL) {
861 return NO_INIT;
862 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700863
864 if (next != NULL && !(next->mCurrentState &
865 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
866 ALOGE("next player is not prepared");
867 return INVALID_OPERATION;
868 }
869
Marco Nelissen6b74d672012-02-28 16:07:44 -0800870 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
871}
872
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800873}; // namespace android