blob: 6cd377acc8e8d89ebd6940b77ef4f3b92bdf6274 [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 Laurentde3f8392014-07-27 18:38:22 -070062 mAudioSessionId = AudioSystem::newAudioUniqueId();
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");
Chong Zhangd88adb92014-07-23 11:43:46 -0700286
287 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700289
290 mLockThreadId = getThreadId();
291
292 if (mCurrentState & MEDIA_PLAYER_STARTED) {
293 ret = NO_ERROR;
294 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
296 mPlayer->setLooping(mLoop);
297 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700298 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700300 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 if (ret != NO_ERROR) {
302 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
303 } else {
304 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100305 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 }
307 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700308 } else {
309 ALOGE("start called in state %d", mCurrentState);
310 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700312
313 mLockThreadId = 0;
314
315 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316}
317
318status_t MediaPlayer::stop()
319{
Steve Block3856b092011-10-20 11:56:00 +0100320 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 Mutex::Autolock _l(mLock);
322 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
323 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
324 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
325 status_t ret = mPlayer->stop();
326 if (ret != NO_ERROR) {
327 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
328 } else {
329 mCurrentState = MEDIA_PLAYER_STOPPED;
330 }
331 return ret;
332 }
Steve Block29357bc2012-01-06 19:20:56 +0000333 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 return INVALID_OPERATION;
335}
336
337status_t MediaPlayer::pause()
338{
Steve Block3856b092011-10-20 11:56:00 +0100339 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800341 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return NO_ERROR;
343 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
344 status_t ret = mPlayer->pause();
345 if (ret != NO_ERROR) {
346 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
347 } else {
348 mCurrentState = MEDIA_PLAYER_PAUSED;
349 }
350 return ret;
351 }
Steve Block29357bc2012-01-06 19:20:56 +0000352 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 return INVALID_OPERATION;
354}
355
356bool MediaPlayer::isPlaying()
357{
358 Mutex::Autolock _l(mLock);
359 if (mPlayer != 0) {
360 bool temp = false;
361 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100362 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000364 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 mCurrentState = MEDIA_PLAYER_PAUSED;
366 }
367 return temp;
368 }
Steve Block3856b092011-10-20 11:56:00 +0100369 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 return false;
371}
372
373status_t MediaPlayer::getVideoWidth(int *w)
374{
Steve Block3856b092011-10-20 11:56:00 +0100375 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 Mutex::Autolock _l(mLock);
377 if (mPlayer == 0) return INVALID_OPERATION;
378 *w = mVideoWidth;
379 return NO_ERROR;
380}
381
382status_t MediaPlayer::getVideoHeight(int *h)
383{
Steve Block3856b092011-10-20 11:56:00 +0100384 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 Mutex::Autolock _l(mLock);
386 if (mPlayer == 0) return INVALID_OPERATION;
387 *h = mVideoHeight;
388 return NO_ERROR;
389}
390
391status_t MediaPlayer::getCurrentPosition(int *msec)
392{
Steve Block3856b092011-10-20 11:56:00 +0100393 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 Mutex::Autolock _l(mLock);
395 if (mPlayer != 0) {
396 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100397 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 *msec = mCurrentPosition;
399 return NO_ERROR;
400 }
401 return mPlayer->getCurrentPosition(msec);
402 }
403 return INVALID_OPERATION;
404}
405
406status_t MediaPlayer::getDuration_l(int *msec)
407{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800408 ALOGV("getDuration_l");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
410 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800411 int durationMs;
412 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700413
414 if (ret != OK) {
415 // Do not enter error state just because no duration was available.
416 durationMs = -1;
417 ret = OK;
418 }
419
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800420 if (msec) {
421 *msec = durationMs;
422 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 return ret;
424 }
Steve Block29357bc2012-01-06 19:20:56 +0000425 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 return INVALID_OPERATION;
427}
428
429status_t MediaPlayer::getDuration(int *msec)
430{
431 Mutex::Autolock _l(mLock);
432 return getDuration_l(msec);
433}
434
435status_t MediaPlayer::seekTo_l(int msec)
436{
Steve Block3856b092011-10-20 11:56:00 +0100437 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
439 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000440 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800443
444 int durationMs;
445 status_t err = mPlayer->getDuration(&durationMs);
446
447 if (err != OK) {
448 ALOGW("Stream has no duration and is therefore not seekable.");
449 return err;
450 }
451
452 if (msec > durationMs) {
453 ALOGW("Attempt to seek to past end of file: request = %d, "
454 "durationMs = %d",
455 msec,
456 durationMs);
457
458 msec = durationMs;
459 }
460
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 // cache duration
462 mCurrentPosition = msec;
463 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 mSeekPosition = msec;
465 return mPlayer->seekTo(msec);
466 }
467 else {
Steve Block3856b092011-10-20 11:56:00 +0100468 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 return NO_ERROR;
470 }
471 }
Steve Block29357bc2012-01-06 19:20:56 +0000472 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 -0800473 return INVALID_OPERATION;
474}
475
476status_t MediaPlayer::seekTo(int msec)
477{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700478 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700480 status_t result = seekTo_l(msec);
481 mLockThreadId = 0;
482
483 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484}
485
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700486status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 mLoop = false;
489 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
490 mPrepareSync = false;
491 if (mPlayer != 0) {
492 status_t ret = mPlayer->reset();
493 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000494 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
496 } else {
497 mCurrentState = MEDIA_PLAYER_IDLE;
498 }
James Donga1680bc2010-11-18 12:23:58 -0800499 // setDataSource has to be called again to create a
500 // new mediaplayer.
501 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 return ret;
503 }
504 clear_l();
505 return NO_ERROR;
506}
507
John Grossmanc795b642012-02-22 15:38:35 -0800508status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
509 Mutex::Autolock _l(mLock);
510
511 if (player == NULL) {
512 return UNKNOWN_ERROR;
513 }
514
515 if (mRetransmitEndpointValid) {
516 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
517 }
518
519 return OK;
520}
521
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700522status_t MediaPlayer::reset()
523{
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700525 Mutex::Autolock _l(mLock);
526 return reset_l();
527}
528
Glenn Kastenfff6d712012-01-12 16:38:12 -0800529status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530{
Steve Block3856b092011-10-20 11:56:00 +0100531 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 Mutex::Autolock _l(mLock);
533 if (mStreamType == type) return NO_ERROR;
534 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
535 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
536 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000537 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 return INVALID_OPERATION;
539 }
540 // cache
541 mStreamType = type;
542 return OK;
543}
544
John Spurlockde9453f2014-03-19 13:05:45 -0400545status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
546{
547 ALOGV("getAudioStreamType");
548 Mutex::Autolock _l(mLock);
549 *type = mStreamType;
550 return OK;
551}
552
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553status_t MediaPlayer::setLooping(int loop)
554{
Steve Block3856b092011-10-20 11:56:00 +0100555 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 Mutex::Autolock _l(mLock);
557 mLoop = (loop != 0);
558 if (mPlayer != 0) {
559 return mPlayer->setLooping(loop);
560 }
561 return OK;
562}
563
564bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 Mutex::Autolock _l(mLock);
567 if (mPlayer != 0) {
568 return mLoop;
569 }
Steve Block3856b092011-10-20 11:56:00 +0100570 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 return false;
572}
573
574status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
575{
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 Mutex::Autolock _l(mLock);
578 mLeftVolume = leftVolume;
579 mRightVolume = rightVolume;
580 if (mPlayer != 0) {
581 return mPlayer->setVolume(leftVolume, rightVolume);
582 }
583 return OK;
584}
585
Eric Laurenta514bdb2010-06-21 09:27:30 -0700586status_t MediaPlayer::setAudioSessionId(int sessionId)
587{
Steve Block3856b092011-10-20 11:56:00 +0100588 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700589 Mutex::Autolock _l(mLock);
590 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000591 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700592 return INVALID_OPERATION;
593 }
594 if (sessionId < 0) {
595 return BAD_VALUE;
596 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700597 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800598 AudioSystem::acquireAudioSessionId(sessionId, -1);
599 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700600 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700601 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700602 return NO_ERROR;
603}
604
605int MediaPlayer::getAudioSessionId()
606{
607 Mutex::Autolock _l(mLock);
608 return mAudioSessionId;
609}
610
Eric Laurent2beeb502010-07-16 07:43:46 -0700611status_t MediaPlayer::setAuxEffectSendLevel(float level)
612{
Steve Block3856b092011-10-20 11:56:00 +0100613 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700614 Mutex::Autolock _l(mLock);
615 mSendLevel = level;
616 if (mPlayer != 0) {
617 return mPlayer->setAuxEffectSendLevel(level);
618 }
619 return OK;
620}
621
622status_t MediaPlayer::attachAuxEffect(int effectId)
623{
Steve Block3856b092011-10-20 11:56:00 +0100624 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700625 Mutex::Autolock _l(mLock);
626 if (mPlayer == 0 ||
627 (mCurrentState & MEDIA_PLAYER_IDLE) ||
628 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000629 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700630 return INVALID_OPERATION;
631 }
632
633 return mPlayer->attachAuxEffect(effectId);
634}
635
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700636// always call with lock held
637status_t MediaPlayer::checkStateForKeySet_l(int key)
638{
639 switch(key) {
640 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
641 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
642 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
643 // Can't change the audio attributes after prepare
644 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
645 return INVALID_OPERATION;
646 }
647 break;
648 default:
649 // parameter doesn't require player state check
650 break;
651 }
652 return OK;
653}
654
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700655status_t MediaPlayer::setParameter(int key, const Parcel& request)
656{
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700658 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700659 if (checkStateForKeySet_l(key) != OK) {
660 return INVALID_OPERATION;
661 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700662 if (mPlayer != NULL) {
663 return mPlayer->setParameter(key, request);
664 }
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("setParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700666 return INVALID_OPERATION;
667}
668
669status_t MediaPlayer::getParameter(int key, Parcel *reply)
670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700672 Mutex::Autolock _l(mLock);
673 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700674 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700675 }
Steve Block3856b092011-10-20 11:56:00 +0100676 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700677 return INVALID_OPERATION;
678}
679
John Grossmanc795b642012-02-22 15:38:35 -0800680status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
681 uint16_t port) {
682 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
683 addrString ? addrString : "(null)", port);
684
685 Mutex::Autolock _l(mLock);
686 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
687 return INVALID_OPERATION;
688
689 if (NULL == addrString) {
690 mRetransmitEndpointValid = false;
691 return OK;
692 }
693
694 struct in_addr saddr;
695 if(!inet_aton(addrString, &saddr)) {
696 return BAD_VALUE;
697 }
698
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800699 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800700 mRetransmitEndpoint.sin_family = AF_INET;
701 mRetransmitEndpoint.sin_addr = saddr;
702 mRetransmitEndpoint.sin_port = htons(port);
703 mRetransmitEndpointValid = true;
704
705 return OK;
706}
707
Gloria Wangb483c472011-04-11 17:23:27 -0700708void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709{
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700712 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713
714 // TODO: In the future, we might be on the same thread if the app is
715 // running in the same process as the media server. In that case,
716 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700717 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700718 // The threadId hack below works around this for the care of prepare,
719 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700720 // FIXME: Remember, this is a hack, it's not even a hack that is applied
721 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700722 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700723 mLock.lock();
724 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700725 }
Jason Sams1af452f2009-03-24 18:45:22 -0700726
Eric Laurent3b268442010-08-03 07:49:49 -0700727 // Allows calls from JNI in idle state to notify errors
728 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100729 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700730 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731 return;
732 }
733
734 switch (msg) {
735 case MEDIA_NOP: // interface test message
736 break;
737 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100738 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739 mCurrentState = MEDIA_PLAYER_PREPARED;
740 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 mPrepareSync = false;
743 mPrepareStatus = NO_ERROR;
744 mSignal.signal();
745 }
746 break;
747 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700749 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000750 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700751 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752 if (!mLoop) {
753 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
754 }
755 break;
756 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700757 // Always log errors.
758 // ext1: Media framework error code.
759 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000760 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
762 if (mPrepareSync)
763 {
Steve Block3856b092011-10-20 11:56:00 +0100764 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 mPrepareSync = false;
766 mPrepareStatus = ext1;
767 mSignal.signal();
768 send = false;
769 }
770 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700771 case MEDIA_INFO:
772 // ext1: Media framework error code.
773 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800774 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000775 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800776 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700777 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100779 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100781 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782 mSeekPosition = -1;
783 seekTo_l(mCurrentPosition);
784 }
785 else {
Steve Block3856b092011-10-20 11:56:00 +0100786 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800787 mCurrentPosition = mSeekPosition = -1;
788 }
789 break;
790 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800792 break;
793 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 mVideoWidth = ext1;
796 mVideoHeight = ext2;
797 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700798 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100799 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700800 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700801 case MEDIA_SUBTITLE_DATA:
802 ALOGV("Received subtitle data message");
803 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 default:
Steve Block3856b092011-10-20 11:56:00 +0100805 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800806 break;
807 }
808
809 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700810 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800811
812 // this prevents re-entrant calls into client code
813 if ((listener != 0) && send) {
814 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700816 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800818 }
819}
820
Andreas Huber1b86fe02014-01-29 11:13:26 -0800821/*static*/ status_t MediaPlayer::decode(
822 const sp<IMediaHTTPService> &httpService,
823 const char* url,
824 uint32_t *pSampleRate,
825 int* pNumChannels,
826 audio_format_t* pFormat,
827 const sp<IMemoryHeap>& heap,
828 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800829{
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("decode(%s)", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700831 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800832 const sp<IMediaPlayerService>& service = getMediaPlayerService();
833 if (service != 0) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800834 status = service->decode(httpService, url, pSampleRate, pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000836 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700837 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800838 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700839 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800840
841}
842
James Dongdd172fc2010-01-15 18:13:58 -0800843void MediaPlayer::died()
844{
Steve Block3856b092011-10-20 11:56:00 +0100845 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800846 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
847}
848
Eric Laurent3d00aa62013-09-24 09:53:27 -0700849/*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length,
850 uint32_t *pSampleRate, int* pNumChannels,
851 audio_format_t* pFormat,
852 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700854 ALOGV("decode(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700855 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856 const sp<IMediaPlayerService>& service = getMediaPlayerService();
857 if (service != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700858 status = service->decode(fd, offset, length, pSampleRate,
859 pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800860 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000861 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700862 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700864 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800865
866}
867
Marco Nelissen6b74d672012-02-28 16:07:44 -0800868status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
869 if (mPlayer == NULL) {
870 return NO_INIT;
871 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700872
873 if (next != NULL && !(next->mCurrentState &
874 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
875 ALOGE("next player is not prepared");
876 return INVALID_OPERATION;
877 }
878
Marco Nelissen6b74d672012-02-28 16:07:44 -0800879 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
880}
881
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800882}; // namespace android