blob: 502ab2dcfca2fee8e1848803e27303e8842ce291 [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>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070035#include <media/AudioResamplerPublic.h>
Glenn Kastena3f1fa32012-01-18 14:54:46 -080036#include <media/AudioSystem.h>
Lajos Molnar3a474aa2015-04-24 17:10:07 -070037#include <media/AVSyncSettings.h>
Chris Watkins99f31602015-03-20 13:06:33 -070038#include <media/IDataSource.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Mathias Agopian75624082009-05-19 19:08:10 -070040#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041
Andreas Huber2db84552010-01-28 11:19:57 -080042#include <utils/KeyedVector.h>
43#include <utils/String8.h>
44
Dima Zavin64760242011-05-11 14:15:23 -070045#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070046#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070047
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
49
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050MediaPlayer::MediaPlayer()
51{
Steve Block3856b092011-10-20 11:56:00 +010052 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 mListener = NULL;
54 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070055 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070056 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057 mCurrentPosition = -1;
58 mSeekPosition = -1;
59 mCurrentState = MEDIA_PLAYER_IDLE;
60 mPrepareSync = false;
61 mPrepareStatus = NO_ERROR;
62 mLoop = false;
63 mLeftVolume = mRightVolume = 1.0;
64 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070065 mLockThreadId = 0;
Eric Laurentde3f8392014-07-27 18:38:22 -070066 mAudioSessionId = AudioSystem::newAudioUniqueId();
Marco Nelissend457c972014-02-11 08:47:07 -080067 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070068 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080069 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070}
71
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072MediaPlayer::~MediaPlayer()
73{
Steve Block3856b092011-10-20 11:56:00 +010074 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070075 if (mAudioAttributesParcel != NULL) {
76 delete mAudioAttributesParcel;
77 mAudioAttributesParcel = NULL;
78 }
Marco Nelissend457c972014-02-11 08:47:07 -080079 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 disconnect();
81 IPCThreadState::self()->flushCommands();
82}
83
84void MediaPlayer::disconnect()
85{
Steve Block3856b092011-10-20 11:56:00 +010086 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080087 sp<IMediaPlayer> p;
88 {
89 Mutex::Autolock _l(mLock);
90 p = mPlayer;
91 mPlayer.clear();
92 }
93
94 if (p != 0) {
95 p->disconnect();
96 }
97}
98
99// always call with lock held
100void MediaPlayer::clear_l()
101{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 mCurrentPosition = -1;
103 mSeekPosition = -1;
104 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800105 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106}
107
108status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
109{
Steve Block3856b092011-10-20 11:56:00 +0100110 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 Mutex::Autolock _l(mLock);
112 mListener = listener;
113 return NO_ERROR;
114}
115
116
Dave Burked681bbb2011-08-30 14:39:17 +0100117status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118{
119 status_t err = UNKNOWN_ERROR;
120 sp<IMediaPlayer> p;
121 { // scope for the lock
122 Mutex::Autolock _l(mLock);
123
Marco Nelissen83ff1432010-03-10 10:53:16 -0800124 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
125 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000126 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 return INVALID_OPERATION;
128 }
129
130 clear_l();
131 p = mPlayer;
132 mPlayer = player;
133 if (player != 0) {
134 mCurrentState = MEDIA_PLAYER_INITIALIZED;
135 err = NO_ERROR;
136 } else {
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000137 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 }
139 }
140
141 if (p != 0) {
142 p->disconnect();
143 }
144
145 return err;
146}
147
Andreas Huber2db84552010-01-28 11:19:57 -0800148status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800149 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800150 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151{
Steve Block3856b092011-10-20 11:56:00 +0100152 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153 status_t err = BAD_VALUE;
154 if (url != NULL) {
155 const sp<IMediaPlayerService>& service(getMediaPlayerService());
156 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800157 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800158 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800159 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100160 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100161 }
Dave Burke06620672011-09-06 20:39:47 +0100162 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 }
164 }
165 return err;
166}
167
168status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
169{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700170 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 status_t err = UNKNOWN_ERROR;
172 const sp<IMediaPlayerService>& service(getMediaPlayerService());
173 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800174 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800175 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
176 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100177 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100178 }
Dave Burke06620672011-09-06 20:39:47 +0100179 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100180 }
181 return err;
182}
183
184status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source)
185{
Steve Block3856b092011-10-20 11:56:00 +0100186 ALOGV("setDataSource");
Dave Burked681bbb2011-08-30 14:39:17 +0100187 status_t err = UNKNOWN_ERROR;
188 const sp<IMediaPlayerService>& service(getMediaPlayerService());
189 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800190 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800191 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
192 (NO_ERROR != player->setDataSource(source))) {
Dave Burke06620672011-09-06 20:39:47 +0100193 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100194 }
Dave Burke06620672011-09-06 20:39:47 +0100195 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 }
197 return err;
198}
199
Chris Watkins99f31602015-03-20 13:06:33 -0700200status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
201{
202 ALOGV("setDataSource(IDataSource)");
203 status_t err = UNKNOWN_ERROR;
204 const sp<IMediaPlayerService>& service(getMediaPlayerService());
205 if (service != 0) {
206 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
207 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
208 (NO_ERROR != player->setDataSource(source))) {
209 player.clear();
210 }
211 err = attachNewPlayer(player);
212 }
213 return err;
214}
215
Nicolas Catania1d187f12009-05-12 23:25:55 -0700216status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
217{
218 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800219 const bool hasBeenInitialized =
220 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
221 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
222 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700223 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700224 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700225 }
Steve Block29357bc2012-01-06 19:20:56 +0000226 ALOGE("invoke failed: wrong state %X", mCurrentState);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700227 return INVALID_OPERATION;
228}
229
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700230status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
231{
Steve Blockb8a80522011-12-20 16:23:08 +0000232 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700233 Mutex::Autolock lock(mLock);
234 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700235 return NO_INIT;
236 }
237 return mPlayer->setMetadataFilter(filter);
238}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700239
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700240status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
241{
Steve Blockb8a80522011-12-20 16:23:08 +0000242 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700243 Mutex::Autolock lock(mLock);
244 if (mPlayer == NULL) {
245 return NO_INIT;
246 }
247 return mPlayer->getMetadata(update_only, apply_filter, metadata);
248}
249
Glenn Kasten11731182011-02-08 17:26:17 -0800250status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800251 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800252{
Steve Block3856b092011-10-20 11:56:00 +0100253 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800254 Mutex::Autolock _l(mLock);
255 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800256 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800257}
258
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259// must call with lock held
260status_t MediaPlayer::prepareAsync_l()
261{
Glenn Kastenb187de12014-12-30 08:18:15 -0800262 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700263 if (mAudioAttributesParcel != NULL) {
264 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700265 } else {
266 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 mCurrentState = MEDIA_PLAYER_PREPARING;
269 return mPlayer->prepareAsync();
270 }
Steve Block29357bc2012-01-06 19:20:56 +0000271 ALOGE("prepareAsync called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 return INVALID_OPERATION;
273}
274
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700275// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
276// one defined in the Android framework and one provided by the implementation
277// that generated the error. The sync version of prepare returns only 1 error
278// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279status_t MediaPlayer::prepare()
280{
Steve Block3856b092011-10-20 11:56:00 +0100281 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700283 mLockThreadId = getThreadId();
284 if (mPrepareSync) {
285 mLockThreadId = 0;
286 return -EALREADY;
287 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 mPrepareSync = true;
289 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700290 if (ret != NO_ERROR) {
291 mLockThreadId = 0;
292 return ret;
293 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294
295 if (mPrepareSync) {
296 mSignal.wait(mLock); // wait for prepare done
297 mPrepareSync = false;
298 }
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700300 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 return mPrepareStatus;
302}
303
304status_t MediaPlayer::prepareAsync()
305{
Steve Block3856b092011-10-20 11:56:00 +0100306 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 Mutex::Autolock _l(mLock);
308 return prepareAsync_l();
309}
310
311status_t MediaPlayer::start()
312{
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700314
315 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700317
318 mLockThreadId = getThreadId();
319
320 if (mCurrentState & MEDIA_PLAYER_STARTED) {
321 ret = NO_ERROR;
322 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
324 mPlayer->setLooping(mLoop);
325 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700326 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700328 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 if (ret != NO_ERROR) {
330 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
331 } else {
332 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100333 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 }
335 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700336 } else {
337 ALOGE("start called in state %d", mCurrentState);
338 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700340
341 mLockThreadId = 0;
342
343 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344}
345
346status_t MediaPlayer::stop()
347{
Steve Block3856b092011-10-20 11:56:00 +0100348 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 Mutex::Autolock _l(mLock);
350 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
351 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
352 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
353 status_t ret = mPlayer->stop();
354 if (ret != NO_ERROR) {
355 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
356 } else {
357 mCurrentState = MEDIA_PLAYER_STOPPED;
358 }
359 return ret;
360 }
Steve Block29357bc2012-01-06 19:20:56 +0000361 ALOGE("stop called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 return INVALID_OPERATION;
363}
364
365status_t MediaPlayer::pause()
366{
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800369 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 return NO_ERROR;
371 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
372 status_t ret = mPlayer->pause();
373 if (ret != NO_ERROR) {
374 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
375 } else {
376 mCurrentState = MEDIA_PLAYER_PAUSED;
377 }
378 return ret;
379 }
Steve Block29357bc2012-01-06 19:20:56 +0000380 ALOGE("pause called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 return INVALID_OPERATION;
382}
383
384bool MediaPlayer::isPlaying()
385{
386 Mutex::Autolock _l(mLock);
387 if (mPlayer != 0) {
388 bool temp = false;
389 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000392 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700394 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
395 ALOGE("internal/external state mismatch corrected");
396 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 }
398 return temp;
399 }
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 return false;
402}
403
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700404status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800405{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700406 ALOGV("setPlaybackSettings: %f %f %d %d",
407 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
408 // Negative speed and pitch does not make sense. Further validation will
409 // be done by the respective mediaplayers.
410 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800411 return BAD_VALUE;
412 }
413 Mutex::Autolock _l(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700414 if (mPlayer == 0) return INVALID_OPERATION;
415 status_t err = mPlayer->setPlaybackSettings(rate);
416 if (err == OK) {
417 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
418 mCurrentState = MEDIA_PLAYER_PAUSED;
419 } else if (rate.mSpeed != 0.f && mCurrentState == MEDIA_PLAYER_PAUSED) {
420 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800421 }
Wei Jia98160162015-02-04 17:01:11 -0800422 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700423 return err;
424}
425
426status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
427{
428 Mutex::Autolock _l(mLock);
429 if (mPlayer == 0) return INVALID_OPERATION;
430 return mPlayer->getPlaybackSettings(rate);
431}
432
433status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
434{
435 ALOGV("setSyncSettings: %u %u %f %f",
436 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
437 Mutex::Autolock _l(mLock);
438 if (mPlayer == 0) return INVALID_OPERATION;
439 return mPlayer->setSyncSettings(sync, videoFpsHint);
440}
441
442status_t MediaPlayer::getSyncSettings(
443 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
444{
445 Mutex::Autolock _l(mLock);
446 if (mPlayer == 0) return INVALID_OPERATION;
447 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800448}
449
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450status_t MediaPlayer::getVideoWidth(int *w)
451{
Steve Block3856b092011-10-20 11:56:00 +0100452 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 Mutex::Autolock _l(mLock);
454 if (mPlayer == 0) return INVALID_OPERATION;
455 *w = mVideoWidth;
456 return NO_ERROR;
457}
458
459status_t MediaPlayer::getVideoHeight(int *h)
460{
Steve Block3856b092011-10-20 11:56:00 +0100461 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462 Mutex::Autolock _l(mLock);
463 if (mPlayer == 0) return INVALID_OPERATION;
464 *h = mVideoHeight;
465 return NO_ERROR;
466}
467
468status_t MediaPlayer::getCurrentPosition(int *msec)
469{
Steve Block3856b092011-10-20 11:56:00 +0100470 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 Mutex::Autolock _l(mLock);
472 if (mPlayer != 0) {
473 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100474 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 *msec = mCurrentPosition;
476 return NO_ERROR;
477 }
478 return mPlayer->getCurrentPosition(msec);
479 }
480 return INVALID_OPERATION;
481}
482
483status_t MediaPlayer::getDuration_l(int *msec)
484{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800485 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800486 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
487 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800489 int durationMs;
490 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700491
492 if (ret != OK) {
493 // Do not enter error state just because no duration was available.
494 durationMs = -1;
495 ret = OK;
496 }
497
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800498 if (msec) {
499 *msec = durationMs;
500 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501 return ret;
502 }
Steve Block29357bc2012-01-06 19:20:56 +0000503 ALOGE("Attempt to call getDuration without a valid mediaplayer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 return INVALID_OPERATION;
505}
506
507status_t MediaPlayer::getDuration(int *msec)
508{
509 Mutex::Autolock _l(mLock);
510 return getDuration_l(msec);
511}
512
513status_t MediaPlayer::seekTo_l(int msec)
514{
Steve Block3856b092011-10-20 11:56:00 +0100515 ALOGV("seekTo %d", msec);
Glenn Kastenb187de12014-12-30 08:18:15 -0800516 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
517 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000519 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800522
523 int durationMs;
524 status_t err = mPlayer->getDuration(&durationMs);
525
526 if (err != OK) {
527 ALOGW("Stream has no duration and is therefore not seekable.");
528 return err;
529 }
530
531 if (msec > durationMs) {
532 ALOGW("Attempt to seek to past end of file: request = %d, "
533 "durationMs = %d",
534 msec,
535 durationMs);
536
537 msec = durationMs;
538 }
539
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 // cache duration
541 mCurrentPosition = msec;
542 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543 mSeekPosition = msec;
544 return mPlayer->seekTo(msec);
545 }
546 else {
Steve Block3856b092011-10-20 11:56:00 +0100547 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 return NO_ERROR;
549 }
550 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800551 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
552 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 return INVALID_OPERATION;
554}
555
556status_t MediaPlayer::seekTo(int msec)
557{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700558 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700560 status_t result = seekTo_l(msec);
561 mLockThreadId = 0;
562
563 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564}
565
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700566status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 mLoop = false;
569 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
570 mPrepareSync = false;
571 if (mPlayer != 0) {
572 status_t ret = mPlayer->reset();
573 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000574 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
576 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700577 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 mCurrentState = MEDIA_PLAYER_IDLE;
579 }
James Donga1680bc2010-11-18 12:23:58 -0800580 // setDataSource has to be called again to create a
581 // new mediaplayer.
582 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583 return ret;
584 }
585 clear_l();
586 return NO_ERROR;
587}
588
John Grossmanc795b642012-02-22 15:38:35 -0800589status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
590 Mutex::Autolock _l(mLock);
591
592 if (player == NULL) {
593 return UNKNOWN_ERROR;
594 }
595
596 if (mRetransmitEndpointValid) {
597 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
598 }
599
600 return OK;
601}
602
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700603status_t MediaPlayer::reset()
604{
Steve Block3856b092011-10-20 11:56:00 +0100605 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700606 Mutex::Autolock _l(mLock);
607 return reset_l();
608}
609
Glenn Kastenfff6d712012-01-12 16:38:12 -0800610status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611{
Steve Block3856b092011-10-20 11:56:00 +0100612 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 Mutex::Autolock _l(mLock);
614 if (mStreamType == type) return NO_ERROR;
615 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
616 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
617 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000618 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619 return INVALID_OPERATION;
620 }
621 // cache
622 mStreamType = type;
623 return OK;
624}
625
John Spurlockde9453f2014-03-19 13:05:45 -0400626status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
627{
628 ALOGV("getAudioStreamType");
629 Mutex::Autolock _l(mLock);
630 *type = mStreamType;
631 return OK;
632}
633
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634status_t MediaPlayer::setLooping(int loop)
635{
Steve Block3856b092011-10-20 11:56:00 +0100636 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 Mutex::Autolock _l(mLock);
638 mLoop = (loop != 0);
639 if (mPlayer != 0) {
640 return mPlayer->setLooping(loop);
641 }
642 return OK;
643}
644
645bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100646 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 Mutex::Autolock _l(mLock);
648 if (mPlayer != 0) {
649 return mLoop;
650 }
Steve Block3856b092011-10-20 11:56:00 +0100651 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return false;
653}
654
655status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
656{
Steve Block3856b092011-10-20 11:56:00 +0100657 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658 Mutex::Autolock _l(mLock);
659 mLeftVolume = leftVolume;
660 mRightVolume = rightVolume;
661 if (mPlayer != 0) {
662 return mPlayer->setVolume(leftVolume, rightVolume);
663 }
664 return OK;
665}
666
Eric Laurenta514bdb2010-06-21 09:27:30 -0700667status_t MediaPlayer::setAudioSessionId(int sessionId)
668{
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700670 Mutex::Autolock _l(mLock);
671 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000672 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700673 return INVALID_OPERATION;
674 }
675 if (sessionId < 0) {
676 return BAD_VALUE;
677 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700678 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800679 AudioSystem::acquireAudioSessionId(sessionId, -1);
680 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700681 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700682 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700683 return NO_ERROR;
684}
685
686int MediaPlayer::getAudioSessionId()
687{
688 Mutex::Autolock _l(mLock);
689 return mAudioSessionId;
690}
691
Eric Laurent2beeb502010-07-16 07:43:46 -0700692status_t MediaPlayer::setAuxEffectSendLevel(float level)
693{
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700695 Mutex::Autolock _l(mLock);
696 mSendLevel = level;
697 if (mPlayer != 0) {
698 return mPlayer->setAuxEffectSendLevel(level);
699 }
700 return OK;
701}
702
703status_t MediaPlayer::attachAuxEffect(int effectId)
704{
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700706 Mutex::Autolock _l(mLock);
707 if (mPlayer == 0 ||
708 (mCurrentState & MEDIA_PLAYER_IDLE) ||
709 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Steve Block29357bc2012-01-06 19:20:56 +0000710 ALOGE("attachAuxEffect called in state %d", mCurrentState);
Eric Laurent2beeb502010-07-16 07:43:46 -0700711 return INVALID_OPERATION;
712 }
713
714 return mPlayer->attachAuxEffect(effectId);
715}
716
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700717// always call with lock held
718status_t MediaPlayer::checkStateForKeySet_l(int key)
719{
720 switch(key) {
721 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
722 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
723 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
724 // Can't change the audio attributes after prepare
725 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
726 return INVALID_OPERATION;
727 }
728 break;
729 default:
730 // parameter doesn't require player state check
731 break;
732 }
733 return OK;
734}
735
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700736status_t MediaPlayer::setParameter(int key, const Parcel& request)
737{
Steve Block3856b092011-10-20 11:56:00 +0100738 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700739 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700740 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700741 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700742 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700743 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700744 switch (key) {
745 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700746 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700747 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
748 mAudioAttributesParcel = new Parcel();
749 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700750 status = OK;
751 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700752 default:
Eric Laurent43562692015-07-15 16:49:07 -0700753 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
754 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700755 }
Eric Laurent43562692015-07-15 16:49:07 -0700756
757 if (mPlayer != NULL) {
758 status = mPlayer->setParameter(key, request);
759 }
760 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700761}
762
763status_t MediaPlayer::getParameter(int key, Parcel *reply)
764{
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700766 Mutex::Autolock _l(mLock);
767 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700768 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700769 }
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700771 return INVALID_OPERATION;
772}
773
John Grossmanc795b642012-02-22 15:38:35 -0800774status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
775 uint16_t port) {
776 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
777 addrString ? addrString : "(null)", port);
778
779 Mutex::Autolock _l(mLock);
780 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
781 return INVALID_OPERATION;
782
783 if (NULL == addrString) {
784 mRetransmitEndpointValid = false;
785 return OK;
786 }
787
788 struct in_addr saddr;
789 if(!inet_aton(addrString, &saddr)) {
790 return BAD_VALUE;
791 }
792
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800793 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800794 mRetransmitEndpoint.sin_family = AF_INET;
795 mRetransmitEndpoint.sin_addr = saddr;
796 mRetransmitEndpoint.sin_port = htons(port);
797 mRetransmitEndpointValid = true;
798
799 return OK;
800}
801
Gloria Wangb483c472011-04-11 17:23:27 -0700802void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800803{
Steve Block3856b092011-10-20 11:56:00 +0100804 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700806 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807
808 // TODO: In the future, we might be on the same thread if the app is
809 // running in the same process as the media server. In that case,
810 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700811 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700812 // The threadId hack below works around this for the care of prepare,
813 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700814 // FIXME: Remember, this is a hack, it's not even a hack that is applied
815 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700816 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700817 mLock.lock();
818 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700819 }
Jason Sams1af452f2009-03-24 18:45:22 -0700820
Eric Laurent3b268442010-08-03 07:49:49 -0700821 // Allows calls from JNI in idle state to notify errors
822 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100823 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700824 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800825 return;
826 }
827
828 switch (msg) {
829 case MEDIA_NOP: // interface test message
830 break;
831 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100832 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800833 mCurrentState = MEDIA_PLAYER_PREPARED;
834 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800836 mPrepareSync = false;
837 mPrepareStatus = NO_ERROR;
838 mSignal.signal();
839 }
840 break;
841 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100842 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700843 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000844 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700845 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800846 if (!mLoop) {
847 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
848 }
849 break;
850 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700851 // Always log errors.
852 // ext1: Media framework error code.
853 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000854 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
856 if (mPrepareSync)
857 {
Steve Block3856b092011-10-20 11:56:00 +0100858 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800859 mPrepareSync = false;
860 mPrepareStatus = ext1;
861 mSignal.signal();
862 send = false;
863 }
864 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700865 case MEDIA_INFO:
866 // ext1: Media framework error code.
867 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800868 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000869 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800870 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700871 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800874 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100875 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800876 mSeekPosition = -1;
877 seekTo_l(mCurrentPosition);
878 }
879 else {
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 mCurrentPosition = mSeekPosition = -1;
882 }
883 break;
884 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 break;
887 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100888 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800889 mVideoWidth = ext1;
890 mVideoHeight = ext2;
891 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700892 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100893 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700894 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700895 case MEDIA_SUBTITLE_DATA:
896 ALOGV("Received subtitle data message");
897 break;
Robert Shih08528432015-04-08 09:06:54 -0700898 case MEDIA_META_DATA:
899 ALOGV("Received timed metadata message");
900 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901 default:
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 break;
904 }
905
906 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700907 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908
909 // this prevents re-entrant calls into client code
910 if ((listener != 0) && send) {
911 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700913 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800915 }
916}
917
James Dongdd172fc2010-01-15 18:13:58 -0800918void MediaPlayer::died()
919{
Steve Block3856b092011-10-20 11:56:00 +0100920 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800921 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
922}
923
Marco Nelissen6b74d672012-02-28 16:07:44 -0800924status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
925 if (mPlayer == NULL) {
926 return NO_INIT;
927 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700928
929 if (next != NULL && !(next->mCurrentState &
930 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
931 ALOGE("next player is not prepared");
932 return INVALID_OPERATION;
933 }
934
Marco Nelissen6b74d672012-02-28 16:07:44 -0800935 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
936}
937
Glenn Kasten40bc9062015-03-20 09:09:33 -0700938} // namespace android