blob: 9611ac7f2b7325fe01492360363918c7714d6d99 [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;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070053 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 mCurrentPosition = -1;
55 mSeekPosition = -1;
56 mCurrentState = MEDIA_PLAYER_IDLE;
57 mPrepareSync = false;
58 mPrepareStatus = NO_ERROR;
59 mLoop = false;
60 mLeftVolume = mRightVolume = 1.0;
61 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070062 mLockThreadId = 0;
Eric Laurentde3f8392014-07-27 18:38:22 -070063 mAudioSessionId = AudioSystem::newAudioUniqueId();
Marco Nelissend457c972014-02-11 08:47:07 -080064 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070065 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080066 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067}
68
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069MediaPlayer::~MediaPlayer()
70{
Steve Block3856b092011-10-20 11:56:00 +010071 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070072 if (mAudioAttributesParcel != NULL) {
73 delete mAudioAttributesParcel;
74 mAudioAttributesParcel = NULL;
75 }
Marco Nelissend457c972014-02-11 08:47:07 -080076 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 disconnect();
78 IPCThreadState::self()->flushCommands();
79}
80
81void MediaPlayer::disconnect()
82{
Steve Block3856b092011-10-20 11:56:00 +010083 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084 sp<IMediaPlayer> p;
85 {
86 Mutex::Autolock _l(mLock);
87 p = mPlayer;
88 mPlayer.clear();
89 }
90
91 if (p != 0) {
92 p->disconnect();
93 }
94}
95
96// always call with lock held
97void MediaPlayer::clear_l()
98{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 mCurrentPosition = -1;
100 mSeekPosition = -1;
101 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800102 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103}
104
105status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
106{
Steve Block3856b092011-10-20 11:56:00 +0100107 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 Mutex::Autolock _l(mLock);
109 mListener = listener;
110 return NO_ERROR;
111}
112
113
Dave Burked681bbb2011-08-30 14:39:17 +0100114status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115{
116 status_t err = UNKNOWN_ERROR;
117 sp<IMediaPlayer> p;
118 { // scope for the lock
119 Mutex::Autolock _l(mLock);
120
Marco Nelissen83ff1432010-03-10 10:53:16 -0800121 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
122 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000123 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 return INVALID_OPERATION;
125 }
126
127 clear_l();
128 p = mPlayer;
129 mPlayer = player;
130 if (player != 0) {
131 mCurrentState = MEDIA_PLAYER_INITIALIZED;
132 err = NO_ERROR;
133 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000134 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 }
136 }
137
138 if (p != 0) {
139 p->disconnect();
140 }
141
142 return err;
143}
144
Andreas Huber2db84552010-01-28 11:19:57 -0800145status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800146 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800147 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148{
Steve Block3856b092011-10-20 11:56:00 +0100149 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 status_t err = BAD_VALUE;
151 if (url != NULL) {
152 const sp<IMediaPlayerService>& service(getMediaPlayerService());
153 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800154 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800155 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800156 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100157 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100158 }
Dave Burke06620672011-09-06 20:39:47 +0100159 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 }
161 }
162 return err;
163}
164
165status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
166{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700167 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 status_t err = UNKNOWN_ERROR;
169 const sp<IMediaPlayerService>& service(getMediaPlayerService());
170 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800171 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800172 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
173 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100174 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100175 }
Dave Burke06620672011-09-06 20:39:47 +0100176 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100177 }
178 return err;
179}
180
181status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
182{
Steve Block3856b092011-10-20 11:56:00 +0100183 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100184 status_t err = UNKNOWN_ERROR;
185 const sp<IMediaPlayerService>& service(getMediaPlayerService());
186 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800187 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800188 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
189 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100190 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100191 }
Dave Burke06620672011-09-06 20:39:47 +0100192 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 }
194 return err;
195}
196
Nicolas Catania1d187f12009-05-12 23:25:55 -0700197status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
198{
199 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800200 const bool hasBeenInitialized =
201 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
202 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
203 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700204 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700205 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700206 }
Steve Block29357bc2012-01-06 19:20:56 +0000207 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700208 return INVALID_OPERATION;
209}
210
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700211status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
212{
Steve Blockb8a80522011-12-20 16:23:08 +0000213 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700214 Mutex::Autolock lock(mLock);
215 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700216 return NO_INIT;
217 }
218 return mPlayer->setMetadataFilter(filter);
219}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700220
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700221status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
222{
Steve Blockb8a80522011-12-20 16:23:08 +0000223 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700224 Mutex::Autolock lock(mLock);
225 if (mPlayer == NULL) {
226 return NO_INIT;
227 }
228 return mPlayer->getMetadata(update_only, apply_filter, metadata);
229}
230
Glenn Kasten11731182011-02-08 17:26:17 -0800231status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800232 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800233{
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800235 Mutex::Autolock _l(mLock);
236 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800237 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800238}
239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240// must call with lock held
241status_t MediaPlayer::prepareAsync_l()
242{
243 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
244 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700245 if (mAudioAttributesParcel != NULL) {
246 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
247 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 mCurrentState = MEDIA_PLAYER_PREPARING;
249 return mPlayer->prepareAsync();
250 }
Steve Block29357bc2012-01-06 19:20:56 +0000251 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 return INVALID_OPERATION;
253}
254
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700255// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
256// one defined in the Android framework and one provided by the implementation
257// that generated the error. The sync version of prepare returns only 1 error
258// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259status_t MediaPlayer::prepare()
260{
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700263 mLockThreadId = getThreadId();
264 if (mPrepareSync) {
265 mLockThreadId = 0;
266 return -EALREADY;
267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 mPrepareSync = true;
269 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700270 if (ret != NO_ERROR) {
271 mLockThreadId = 0;
272 return ret;
273 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274
275 if (mPrepareSync) {
276 mSignal.wait(mLock); // wait for prepare done
277 mPrepareSync = false;
278 }
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700280 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 return mPrepareStatus;
282}
283
284status_t MediaPlayer::prepareAsync()
285{
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 Mutex::Autolock _l(mLock);
288 return prepareAsync_l();
289}
290
291status_t MediaPlayer::start()
292{
Steve Block3856b092011-10-20 11:56:00 +0100293 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700294
295 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700297
298 mLockThreadId = getThreadId();
299
300 if (mCurrentState & MEDIA_PLAYER_STARTED) {
301 ret = NO_ERROR;
302 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
304 mPlayer->setLooping(mLoop);
305 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700306 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700308 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 if (ret != NO_ERROR) {
310 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
311 } else {
312 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 }
315 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700316 } else {
317 ALOGE("start called in state %d", mCurrentState);
318 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700320
321 mLockThreadId = 0;
322
323 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324}
325
326status_t MediaPlayer::stop()
327{
Steve Block3856b092011-10-20 11:56:00 +0100328 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 Mutex::Autolock _l(mLock);
330 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
331 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
332 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
333 status_t ret = mPlayer->stop();
334 if (ret != NO_ERROR) {
335 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
336 } else {
337 mCurrentState = MEDIA_PLAYER_STOPPED;
338 }
339 return ret;
340 }
Steve Block29357bc2012-01-06 19:20:56 +0000341 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return INVALID_OPERATION;
343}
344
345status_t MediaPlayer::pause()
346{
Steve Block3856b092011-10-20 11:56:00 +0100347 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800349 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350 return NO_ERROR;
351 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
352 status_t ret = mPlayer->pause();
353 if (ret != NO_ERROR) {
354 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
355 } else {
356 mCurrentState = MEDIA_PLAYER_PAUSED;
357 }
358 return ret;
359 }
Steve Block29357bc2012-01-06 19:20:56 +0000360 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 return INVALID_OPERATION;
362}
363
364bool MediaPlayer::isPlaying()
365{
366 Mutex::Autolock _l(mLock);
367 if (mPlayer != 0) {
368 bool temp = false;
369 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000372 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 mCurrentState = MEDIA_PLAYER_PAUSED;
374 }
375 return temp;
376 }
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 return false;
379}
380
381status_t MediaPlayer::getVideoWidth(int *w)
382{
Steve Block3856b092011-10-20 11:56:00 +0100383 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 Mutex::Autolock _l(mLock);
385 if (mPlayer == 0) return INVALID_OPERATION;
386 *w = mVideoWidth;
387 return NO_ERROR;
388}
389
390status_t MediaPlayer::getVideoHeight(int *h)
391{
Steve Block3856b092011-10-20 11:56:00 +0100392 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 Mutex::Autolock _l(mLock);
394 if (mPlayer == 0) return INVALID_OPERATION;
395 *h = mVideoHeight;
396 return NO_ERROR;
397}
398
399status_t MediaPlayer::getCurrentPosition(int *msec)
400{
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 Mutex::Autolock _l(mLock);
403 if (mPlayer != 0) {
404 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 *msec = mCurrentPosition;
407 return NO_ERROR;
408 }
409 return mPlayer->getCurrentPosition(msec);
410 }
411 return INVALID_OPERATION;
412}
413
414status_t MediaPlayer::getDuration_l(int *msec)
415{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800416 ALOGV("getDuration_l");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
418 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800419 int durationMs;
420 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700421
422 if (ret != OK) {
423 // Do not enter error state just because no duration was available.
424 durationMs = -1;
425 ret = OK;
426 }
427
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800428 if (msec) {
429 *msec = durationMs;
430 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 return ret;
432 }
Steve Block29357bc2012-01-06 19:20:56 +0000433 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 return INVALID_OPERATION;
435}
436
437status_t MediaPlayer::getDuration(int *msec)
438{
439 Mutex::Autolock _l(mLock);
440 return getDuration_l(msec);
441}
442
443status_t MediaPlayer::seekTo_l(int msec)
444{
Steve Block3856b092011-10-20 11:56:00 +0100445 ALOGV("seekTo %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
447 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000448 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800451
452 int durationMs;
453 status_t err = mPlayer->getDuration(&durationMs);
454
455 if (err != OK) {
456 ALOGW("Stream has no duration and is therefore not seekable.");
457 return err;
458 }
459
460 if (msec > durationMs) {
461 ALOGW("Attempt to seek to past end of file: request = %d, "
462 "durationMs = %d",
463 msec,
464 durationMs);
465
466 msec = durationMs;
467 }
468
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 // cache duration
470 mCurrentPosition = msec;
471 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 mSeekPosition = msec;
473 return mPlayer->seekTo(msec);
474 }
475 else {
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 return NO_ERROR;
478 }
479 }
Steve Block29357bc2012-01-06 19:20:56 +0000480 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 -0800481 return INVALID_OPERATION;
482}
483
484status_t MediaPlayer::seekTo(int msec)
485{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700486 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700488 status_t result = seekTo_l(msec);
489 mLockThreadId = 0;
490
491 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492}
493
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700494status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 mLoop = false;
497 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
498 mPrepareSync = false;
499 if (mPlayer != 0) {
500 status_t ret = mPlayer->reset();
501 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000502 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
504 } else {
505 mCurrentState = MEDIA_PLAYER_IDLE;
506 }
James Donga1680bc2010-11-18 12:23:58 -0800507 // setDataSource has to be called again to create a
508 // new mediaplayer.
509 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 return ret;
511 }
512 clear_l();
513 return NO_ERROR;
514}
515
John Grossmanc795b642012-02-22 15:38:35 -0800516status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
517 Mutex::Autolock _l(mLock);
518
519 if (player == NULL) {
520 return UNKNOWN_ERROR;
521 }
522
523 if (mRetransmitEndpointValid) {
524 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
525 }
526
527 return OK;
528}
529
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700530status_t MediaPlayer::reset()
531{
Steve Block3856b092011-10-20 11:56:00 +0100532 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700533 Mutex::Autolock _l(mLock);
534 return reset_l();
535}
536
Glenn Kastenfff6d712012-01-12 16:38:12 -0800537status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538{
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 Mutex::Autolock _l(mLock);
541 if (mStreamType == type) return NO_ERROR;
542 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
543 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
544 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000545 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 return INVALID_OPERATION;
547 }
548 // cache
549 mStreamType = type;
550 return OK;
551}
552
John Spurlockde9453f2014-03-19 13:05:45 -0400553status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
554{
555 ALOGV("getAudioStreamType");
556 Mutex::Autolock _l(mLock);
557 *type = mStreamType;
558 return OK;
559}
560
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561status_t MediaPlayer::setLooping(int loop)
562{
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 Mutex::Autolock _l(mLock);
565 mLoop = (loop != 0);
566 if (mPlayer != 0) {
567 return mPlayer->setLooping(loop);
568 }
569 return OK;
570}
571
572bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100573 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 Mutex::Autolock _l(mLock);
575 if (mPlayer != 0) {
576 return mLoop;
577 }
Steve Block3856b092011-10-20 11:56:00 +0100578 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579 return false;
580}
581
582status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
583{
Steve Block3856b092011-10-20 11:56:00 +0100584 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 Mutex::Autolock _l(mLock);
586 mLeftVolume = leftVolume;
587 mRightVolume = rightVolume;
588 if (mPlayer != 0) {
589 return mPlayer->setVolume(leftVolume, rightVolume);
590 }
591 return OK;
592}
593
Eric Laurenta514bdb2010-06-21 09:27:30 -0700594status_t MediaPlayer::setAudioSessionId(int sessionId)
595{
Steve Block3856b092011-10-20 11:56:00 +0100596 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700597 Mutex::Autolock _l(mLock);
598 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000599 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700600 return INVALID_OPERATION;
601 }
602 if (sessionId < 0) {
603 return BAD_VALUE;
604 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700605 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800606 AudioSystem::acquireAudioSessionId(sessionId, -1);
607 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700608 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700609 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700610 return NO_ERROR;
611}
612
613int MediaPlayer::getAudioSessionId()
614{
615 Mutex::Autolock _l(mLock);
616 return mAudioSessionId;
617}
618
Eric Laurent2beeb502010-07-16 07:43:46 -0700619status_t MediaPlayer::setAuxEffectSendLevel(float level)
620{
Steve Block3856b092011-10-20 11:56:00 +0100621 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700622 Mutex::Autolock _l(mLock);
623 mSendLevel = level;
624 if (mPlayer != 0) {
625 return mPlayer->setAuxEffectSendLevel(level);
626 }
627 return OK;
628}
629
630status_t MediaPlayer::attachAuxEffect(int effectId)
631{
Steve Block3856b092011-10-20 11:56:00 +0100632 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700633 Mutex::Autolock _l(mLock);
634 if (mPlayer == 0 ||
635 (mCurrentState & MEDIA_PLAYER_IDLE) ||
636 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000637 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700638 return INVALID_OPERATION;
639 }
640
641 return mPlayer->attachAuxEffect(effectId);
642}
643
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700644// always call with lock held
645status_t MediaPlayer::checkStateForKeySet_l(int key)
646{
647 switch(key) {
648 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
649 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
650 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
651 // Can't change the audio attributes after prepare
652 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
653 return INVALID_OPERATION;
654 }
655 break;
656 default:
657 // parameter doesn't require player state check
658 break;
659 }
660 return OK;
661}
662
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700663status_t MediaPlayer::setParameter(int key, const Parcel& request)
664{
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("MediaPlayer::setParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700666 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700667 if (checkStateForKeySet_l(key) != OK) {
668 return INVALID_OPERATION;
669 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700670 if (mPlayer != NULL) {
671 return mPlayer->setParameter(key, request);
672 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700673 switch (key) {
674 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
675 // no player, save the marshalled audio attributes
676 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
677 mAudioAttributesParcel = new Parcel();
678 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
679 return OK;
680 default:
681 ALOGV("setParameter: no active player");
682 return INVALID_OPERATION;
683 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700684}
685
686status_t MediaPlayer::getParameter(int key, Parcel *reply)
687{
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700689 Mutex::Autolock _l(mLock);
690 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700691 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700692 }
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700694 return INVALID_OPERATION;
695}
696
John Grossmanc795b642012-02-22 15:38:35 -0800697status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
698 uint16_t port) {
699 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
700 addrString ? addrString : "(null)", port);
701
702 Mutex::Autolock _l(mLock);
703 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
704 return INVALID_OPERATION;
705
706 if (NULL == addrString) {
707 mRetransmitEndpointValid = false;
708 return OK;
709 }
710
711 struct in_addr saddr;
712 if(!inet_aton(addrString, &saddr)) {
713 return BAD_VALUE;
714 }
715
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800716 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800717 mRetransmitEndpoint.sin_family = AF_INET;
718 mRetransmitEndpoint.sin_addr = saddr;
719 mRetransmitEndpoint.sin_port = htons(port);
720 mRetransmitEndpointValid = true;
721
722 return OK;
723}
724
Gloria Wangb483c472011-04-11 17:23:27 -0700725void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726{
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700729 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730
731 // TODO: In the future, we might be on the same thread if the app is
732 // running in the same process as the media server. In that case,
733 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700734 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700735 // The threadId hack below works around this for the care of prepare,
736 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700737 // FIXME: Remember, this is a hack, it's not even a hack that is applied
738 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700739 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700740 mLock.lock();
741 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700742 }
Jason Sams1af452f2009-03-24 18:45:22 -0700743
Eric Laurent3b268442010-08-03 07:49:49 -0700744 // Allows calls from JNI in idle state to notify errors
745 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700747 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748 return;
749 }
750
751 switch (msg) {
752 case MEDIA_NOP: // interface test message
753 break;
754 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 mCurrentState = MEDIA_PLAYER_PREPARED;
757 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100758 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759 mPrepareSync = false;
760 mPrepareStatus = NO_ERROR;
761 mSignal.signal();
762 }
763 break;
764 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700766 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000767 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700768 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 if (!mLoop) {
770 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
771 }
772 break;
773 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700774 // Always log errors.
775 // ext1: Media framework error code.
776 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000777 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
779 if (mPrepareSync)
780 {
Steve Block3856b092011-10-20 11:56:00 +0100781 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782 mPrepareSync = false;
783 mPrepareStatus = ext1;
784 mSignal.signal();
785 send = false;
786 }
787 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700788 case MEDIA_INFO:
789 // ext1: Media framework error code.
790 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800791 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000792 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800793 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700794 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800797 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100798 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800799 mSeekPosition = -1;
800 seekTo_l(mCurrentPosition);
801 }
802 else {
Steve Block3856b092011-10-20 11:56:00 +0100803 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 mCurrentPosition = mSeekPosition = -1;
805 }
806 break;
807 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800809 break;
810 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 mVideoWidth = ext1;
813 mVideoHeight = ext2;
814 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700815 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700817 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700818 case MEDIA_SUBTITLE_DATA:
819 ALOGV("Received subtitle data message");
820 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800821 default:
Steve Block3856b092011-10-20 11:56:00 +0100822 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823 break;
824 }
825
826 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700827 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800828
829 // this prevents re-entrant calls into client code
830 if ((listener != 0) && send) {
831 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100832 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700833 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100834 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835 }
836}
837
Andreas Huber1b86fe02014-01-29 11:13:26 -0800838/*static*/ status_t MediaPlayer::decode(
839 const sp<IMediaHTTPService> &httpService,
840 const char* url,
841 uint32_t *pSampleRate,
842 int* pNumChannels,
843 audio_format_t* pFormat,
844 const sp<IMemoryHeap>& heap,
845 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800846{
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("decode(%s)", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700848 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849 const sp<IMediaPlayerService>& service = getMediaPlayerService();
850 if (service != 0) {
Andreas Huber1b86fe02014-01-29 11:13:26 -0800851 status = service->decode(httpService, url, pSampleRate, pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800852 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000853 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700854 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700856 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857
858}
859
James Dongdd172fc2010-01-15 18:13:58 -0800860void MediaPlayer::died()
861{
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800863 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
864}
865
Eric Laurent3d00aa62013-09-24 09:53:27 -0700866/*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length,
867 uint32_t *pSampleRate, int* pNumChannels,
868 audio_format_t* pFormat,
869 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700871 ALOGV("decode(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Eric Laurent3d00aa62013-09-24 09:53:27 -0700872 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800873 const sp<IMediaPlayerService>& service = getMediaPlayerService();
874 if (service != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700875 status = service->decode(fd, offset, length, pSampleRate,
876 pNumChannels, pFormat, heap, pSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800877 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000878 ALOGE("Unable to locate media service");
Eric Laurent3d00aa62013-09-24 09:53:27 -0700879 status = DEAD_OBJECT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800880 }
Eric Laurent3d00aa62013-09-24 09:53:27 -0700881 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800882
883}
884
Marco Nelissen6b74d672012-02-28 16:07:44 -0800885status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
886 if (mPlayer == NULL) {
887 return NO_INIT;
888 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700889
890 if (next != NULL && !(next->mCurrentState &
891 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
892 ALOGE("next player is not prepared");
893 return INVALID_OPERATION;
894 }
895
Marco Nelissen6b74d672012-02-28 16:07:44 -0800896 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
897}
898
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899}; // namespace android