blob: fbe749ce5cb3bb705faa981856909d61824af523 [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;
Glenn Kastend848eb42016-03-08 13:42:11 -080066 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
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) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700155 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 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;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700172 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 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
Chris Watkins99f31602015-03-20 13:06:33 -0700184status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
185{
186 ALOGV("setDataSource(IDataSource)");
187 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700188 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700189 if (service != 0) {
190 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
191 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
192 (NO_ERROR != player->setDataSource(source))) {
193 player.clear();
194 }
195 err = attachNewPlayer(player);
196 }
197 return err;
198}
199
Nicolas Catania1d187f12009-05-12 23:25:55 -0700200status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
201{
202 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800203 const bool hasBeenInitialized =
204 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
205 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
206 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700207 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700208 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700209 }
Wei Jia848ebc62016-03-23 14:06:46 -0700210 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700211 return INVALID_OPERATION;
212}
213
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700214status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
215{
Steve Blockb8a80522011-12-20 16:23:08 +0000216 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700217 Mutex::Autolock lock(mLock);
218 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700219 return NO_INIT;
220 }
221 return mPlayer->setMetadataFilter(filter);
222}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700223
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700224status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
225{
Steve Blockb8a80522011-12-20 16:23:08 +0000226 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700227 Mutex::Autolock lock(mLock);
228 if (mPlayer == NULL) {
229 return NO_INIT;
230 }
231 return mPlayer->getMetadata(update_only, apply_filter, metadata);
232}
233
Glenn Kasten11731182011-02-08 17:26:17 -0800234status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800235 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800236{
Steve Block3856b092011-10-20 11:56:00 +0100237 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800238 Mutex::Autolock _l(mLock);
239 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800240 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800241}
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243// must call with lock held
244status_t MediaPlayer::prepareAsync_l()
245{
Glenn Kastenb187de12014-12-30 08:18:15 -0800246 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700247 if (mAudioAttributesParcel != NULL) {
248 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700249 } else {
250 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700251 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 mCurrentState = MEDIA_PLAYER_PREPARING;
253 return mPlayer->prepareAsync();
254 }
Wei Jia848ebc62016-03-23 14:06:46 -0700255 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 return INVALID_OPERATION;
257}
258
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700259// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
260// one defined in the Android framework and one provided by the implementation
261// that generated the error. The sync version of prepare returns only 1 error
262// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263status_t MediaPlayer::prepare()
264{
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700267 mLockThreadId = getThreadId();
268 if (mPrepareSync) {
269 mLockThreadId = 0;
270 return -EALREADY;
271 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 mPrepareSync = true;
273 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700274 if (ret != NO_ERROR) {
275 mLockThreadId = 0;
276 return ret;
277 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800278
279 if (mPrepareSync) {
280 mSignal.wait(mLock); // wait for prepare done
281 mPrepareSync = false;
282 }
Steve Block3856b092011-10-20 11:56:00 +0100283 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700284 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 return mPrepareStatus;
286}
287
288status_t MediaPlayer::prepareAsync()
289{
Steve Block3856b092011-10-20 11:56:00 +0100290 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 Mutex::Autolock _l(mLock);
292 return prepareAsync_l();
293}
294
295status_t MediaPlayer::start()
296{
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700298
299 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700301
302 mLockThreadId = getThreadId();
303
304 if (mCurrentState & MEDIA_PLAYER_STARTED) {
305 ret = NO_ERROR;
306 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
308 mPlayer->setLooping(mLoop);
309 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700310 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700312 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 if (ret != NO_ERROR) {
314 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
315 } else {
316 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 }
319 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700320 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700321 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700322 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700324
325 mLockThreadId = 0;
326
327 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328}
329
330status_t MediaPlayer::stop()
331{
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 Mutex::Autolock _l(mLock);
334 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
335 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
336 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
337 status_t ret = mPlayer->stop();
338 if (ret != NO_ERROR) {
339 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
340 } else {
341 mCurrentState = MEDIA_PLAYER_STOPPED;
342 }
343 return ret;
344 }
Wei Jia848ebc62016-03-23 14:06:46 -0700345 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346 return INVALID_OPERATION;
347}
348
349status_t MediaPlayer::pause()
350{
Steve Block3856b092011-10-20 11:56:00 +0100351 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800353 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 return NO_ERROR;
355 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
356 status_t ret = mPlayer->pause();
357 if (ret != NO_ERROR) {
358 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
359 } else {
360 mCurrentState = MEDIA_PLAYER_PAUSED;
361 }
362 return ret;
363 }
Wei Jia848ebc62016-03-23 14:06:46 -0700364 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 return INVALID_OPERATION;
366}
367
368bool MediaPlayer::isPlaying()
369{
370 Mutex::Autolock _l(mLock);
371 if (mPlayer != 0) {
372 bool temp = false;
373 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100374 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000376 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700378 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
379 ALOGE("internal/external state mismatch corrected");
380 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 }
382 return temp;
383 }
Steve Block3856b092011-10-20 11:56:00 +0100384 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 return false;
386}
387
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700388status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800389{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700390 ALOGV("setPlaybackSettings: %f %f %d %d",
391 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
392 // Negative speed and pitch does not make sense. Further validation will
393 // be done by the respective mediaplayers.
394 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800395 return BAD_VALUE;
396 }
397 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700398 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
399 return INVALID_OPERATION;
400 }
Wei Jia5be109f2016-06-10 16:15:07 -0700401
402 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
403 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
404 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
405 mPlayer->setLooping(mLoop);
406 mPlayer->setVolume(mLeftVolume, mRightVolume);
407 mPlayer->setAuxEffectSendLevel(mSendLevel);
408 }
409
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700410 status_t err = mPlayer->setPlaybackSettings(rate);
411 if (err == OK) {
412 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
413 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700414 } else if (rate.mSpeed != 0.f
415 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
416 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700417 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800418 }
Wei Jia98160162015-02-04 17:01:11 -0800419 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700420 return err;
421}
422
423status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
424{
425 Mutex::Autolock _l(mLock);
426 if (mPlayer == 0) return INVALID_OPERATION;
427 return mPlayer->getPlaybackSettings(rate);
428}
429
430status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
431{
432 ALOGV("setSyncSettings: %u %u %f %f",
433 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
434 Mutex::Autolock _l(mLock);
435 if (mPlayer == 0) return INVALID_OPERATION;
436 return mPlayer->setSyncSettings(sync, videoFpsHint);
437}
438
439status_t MediaPlayer::getSyncSettings(
440 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
441{
442 Mutex::Autolock _l(mLock);
443 if (mPlayer == 0) return INVALID_OPERATION;
444 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800445}
446
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447status_t MediaPlayer::getVideoWidth(int *w)
448{
Steve Block3856b092011-10-20 11:56:00 +0100449 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 Mutex::Autolock _l(mLock);
451 if (mPlayer == 0) return INVALID_OPERATION;
452 *w = mVideoWidth;
453 return NO_ERROR;
454}
455
456status_t MediaPlayer::getVideoHeight(int *h)
457{
Steve Block3856b092011-10-20 11:56:00 +0100458 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 Mutex::Autolock _l(mLock);
460 if (mPlayer == 0) return INVALID_OPERATION;
461 *h = mVideoHeight;
462 return NO_ERROR;
463}
464
465status_t MediaPlayer::getCurrentPosition(int *msec)
466{
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 Mutex::Autolock _l(mLock);
469 if (mPlayer != 0) {
470 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100471 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 *msec = mCurrentPosition;
473 return NO_ERROR;
474 }
475 return mPlayer->getCurrentPosition(msec);
476 }
477 return INVALID_OPERATION;
478}
479
480status_t MediaPlayer::getDuration_l(int *msec)
481{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800482 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800483 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
484 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800486 int durationMs;
487 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700488
489 if (ret != OK) {
490 // Do not enter error state just because no duration was available.
491 durationMs = -1;
492 ret = OK;
493 }
494
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800495 if (msec) {
496 *msec = durationMs;
497 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 return ret;
499 }
Wei Jia848ebc62016-03-23 14:06:46 -0700500 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
501 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 return INVALID_OPERATION;
503}
504
505status_t MediaPlayer::getDuration(int *msec)
506{
507 Mutex::Autolock _l(mLock);
508 return getDuration_l(msec);
509}
510
511status_t MediaPlayer::seekTo_l(int msec)
512{
Steve Block3856b092011-10-20 11:56:00 +0100513 ALOGV("seekTo %d", msec);
Glenn Kastenb187de12014-12-30 08:18:15 -0800514 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
515 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000517 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800520
521 int durationMs;
522 status_t err = mPlayer->getDuration(&durationMs);
523
524 if (err != OK) {
525 ALOGW("Stream has no duration and is therefore not seekable.");
526 return err;
527 }
528
529 if (msec > durationMs) {
530 ALOGW("Attempt to seek to past end of file: request = %d, "
531 "durationMs = %d",
532 msec,
533 durationMs);
534
535 msec = durationMs;
536 }
537
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 // cache duration
539 mCurrentPosition = msec;
540 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 mSeekPosition = msec;
542 return mPlayer->seekTo(msec);
543 }
544 else {
Steve Block3856b092011-10-20 11:56:00 +0100545 ALOGV("Seek in progress - queue up seekTo[%d]", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 return NO_ERROR;
547 }
548 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800549 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
550 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 return INVALID_OPERATION;
552}
553
554status_t MediaPlayer::seekTo(int msec)
555{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700556 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 Mutex::Autolock _l(mLock);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700558 status_t result = seekTo_l(msec);
559 mLockThreadId = 0;
560
561 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562}
563
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700564status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 mLoop = false;
567 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
568 mPrepareSync = false;
569 if (mPlayer != 0) {
570 status_t ret = mPlayer->reset();
571 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000572 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
574 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700575 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 mCurrentState = MEDIA_PLAYER_IDLE;
577 }
James Donga1680bc2010-11-18 12:23:58 -0800578 // setDataSource has to be called again to create a
579 // new mediaplayer.
580 mPlayer = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 return ret;
582 }
583 clear_l();
584 return NO_ERROR;
585}
586
John Grossmanc795b642012-02-22 15:38:35 -0800587status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
588 Mutex::Autolock _l(mLock);
589
590 if (player == NULL) {
591 return UNKNOWN_ERROR;
592 }
593
594 if (mRetransmitEndpointValid) {
595 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
596 }
597
598 return OK;
599}
600
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700601status_t MediaPlayer::reset()
602{
Steve Block3856b092011-10-20 11:56:00 +0100603 ALOGV("reset");
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700604 Mutex::Autolock _l(mLock);
605 return reset_l();
606}
607
Glenn Kastenfff6d712012-01-12 16:38:12 -0800608status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609{
Steve Block3856b092011-10-20 11:56:00 +0100610 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611 Mutex::Autolock _l(mLock);
612 if (mStreamType == type) return NO_ERROR;
613 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
614 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
615 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000616 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 return INVALID_OPERATION;
618 }
619 // cache
620 mStreamType = type;
621 return OK;
622}
623
John Spurlockde9453f2014-03-19 13:05:45 -0400624status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
625{
626 ALOGV("getAudioStreamType");
627 Mutex::Autolock _l(mLock);
628 *type = mStreamType;
629 return OK;
630}
631
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632status_t MediaPlayer::setLooping(int loop)
633{
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 Mutex::Autolock _l(mLock);
636 mLoop = (loop != 0);
637 if (mPlayer != 0) {
638 return mPlayer->setLooping(loop);
639 }
640 return OK;
641}
642
643bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100644 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 Mutex::Autolock _l(mLock);
646 if (mPlayer != 0) {
647 return mLoop;
648 }
Steve Block3856b092011-10-20 11:56:00 +0100649 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 return false;
651}
652
653status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
654{
Steve Block3856b092011-10-20 11:56:00 +0100655 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 Mutex::Autolock _l(mLock);
657 mLeftVolume = leftVolume;
658 mRightVolume = rightVolume;
659 if (mPlayer != 0) {
660 return mPlayer->setVolume(leftVolume, rightVolume);
661 }
662 return OK;
663}
664
Glenn Kastend848eb42016-03-08 13:42:11 -0800665status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700666{
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700668 Mutex::Autolock _l(mLock);
669 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000670 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700671 return INVALID_OPERATION;
672 }
673 if (sessionId < 0) {
674 return BAD_VALUE;
675 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700676 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800677 AudioSystem::acquireAudioSessionId(sessionId, -1);
678 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700679 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700680 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700681 return NO_ERROR;
682}
683
Glenn Kastend848eb42016-03-08 13:42:11 -0800684audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700685{
686 Mutex::Autolock _l(mLock);
687 return mAudioSessionId;
688}
689
Eric Laurent2beeb502010-07-16 07:43:46 -0700690status_t MediaPlayer::setAuxEffectSendLevel(float level)
691{
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700693 Mutex::Autolock _l(mLock);
694 mSendLevel = level;
695 if (mPlayer != 0) {
696 return mPlayer->setAuxEffectSendLevel(level);
697 }
698 return OK;
699}
700
701status_t MediaPlayer::attachAuxEffect(int effectId)
702{
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700704 Mutex::Autolock _l(mLock);
705 if (mPlayer == 0 ||
706 (mCurrentState & MEDIA_PLAYER_IDLE) ||
707 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700708 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700709 return INVALID_OPERATION;
710 }
711
712 return mPlayer->attachAuxEffect(effectId);
713}
714
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700715// always call with lock held
716status_t MediaPlayer::checkStateForKeySet_l(int key)
717{
718 switch(key) {
719 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
720 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
721 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
722 // Can't change the audio attributes after prepare
723 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
724 return INVALID_OPERATION;
725 }
726 break;
727 default:
728 // parameter doesn't require player state check
729 break;
730 }
731 return OK;
732}
733
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700734status_t MediaPlayer::setParameter(int key, const Parcel& request)
735{
Steve Block3856b092011-10-20 11:56:00 +0100736 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700737 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700738 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700739 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700740 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700741 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700742 switch (key) {
743 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700744 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700745 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
746 mAudioAttributesParcel = new Parcel();
747 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700748 status = OK;
749 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700750 default:
Eric Laurent43562692015-07-15 16:49:07 -0700751 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
752 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700753 }
Eric Laurent43562692015-07-15 16:49:07 -0700754
755 if (mPlayer != NULL) {
756 status = mPlayer->setParameter(key, request);
757 }
758 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700759}
760
761status_t MediaPlayer::getParameter(int key, Parcel *reply)
762{
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700764 Mutex::Autolock _l(mLock);
765 if (mPlayer != NULL) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700766 return mPlayer->getParameter(key, reply);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700767 }
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700769 return INVALID_OPERATION;
770}
771
John Grossmanc795b642012-02-22 15:38:35 -0800772status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
773 uint16_t port) {
774 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
775 addrString ? addrString : "(null)", port);
776
777 Mutex::Autolock _l(mLock);
778 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
779 return INVALID_OPERATION;
780
781 if (NULL == addrString) {
782 mRetransmitEndpointValid = false;
783 return OK;
784 }
785
786 struct in_addr saddr;
787 if(!inet_aton(addrString, &saddr)) {
788 return BAD_VALUE;
789 }
790
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800791 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800792 mRetransmitEndpoint.sin_family = AF_INET;
793 mRetransmitEndpoint.sin_addr = saddr;
794 mRetransmitEndpoint.sin_port = htons(port);
795 mRetransmitEndpointValid = true;
796
797 return OK;
798}
799
Gloria Wangb483c472011-04-11 17:23:27 -0700800void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801{
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800803 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700804 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805
806 // TODO: In the future, we might be on the same thread if the app is
807 // running in the same process as the media server. In that case,
808 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700809 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700810 // The threadId hack below works around this for the care of prepare,
811 // seekTo and start within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700812 // FIXME: Remember, this is a hack, it's not even a hack that is applied
813 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700814 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700815 mLock.lock();
816 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700817 }
Jason Sams1af452f2009-03-24 18:45:22 -0700818
Eric Laurent3b268442010-08-03 07:49:49 -0700819 // Allows calls from JNI in idle state to notify errors
820 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700822 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823 return;
824 }
825
826 switch (msg) {
827 case MEDIA_NOP: // interface test message
828 break;
829 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831 mCurrentState = MEDIA_PLAYER_PREPARED;
832 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100833 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800834 mPrepareSync = false;
835 mPrepareStatus = NO_ERROR;
836 mSignal.signal();
837 }
838 break;
839 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100840 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700841 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000842 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700843 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844 if (!mLoop) {
845 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
846 }
847 break;
848 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700849 // Always log errors.
850 // ext1: Media framework error code.
851 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000852 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
854 if (mPrepareSync)
855 {
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857 mPrepareSync = false;
858 mPrepareStatus = ext1;
859 mSignal.signal();
860 send = false;
861 }
862 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700863 case MEDIA_INFO:
864 // ext1: Media framework error code.
865 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800866 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000867 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800868 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700869 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("Received seek complete");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 if (mSeekPosition != mCurrentPosition) {
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("Executing queued seekTo(%d)", mSeekPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800874 mSeekPosition = -1;
875 seekTo_l(mCurrentPosition);
876 }
877 else {
Steve Block3856b092011-10-20 11:56:00 +0100878 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800879 mCurrentPosition = mSeekPosition = -1;
880 }
881 break;
882 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800884 break;
885 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800887 mVideoWidth = ext1;
888 mVideoHeight = ext2;
889 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700890 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700892 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700893 case MEDIA_SUBTITLE_DATA:
894 ALOGV("Received subtitle data message");
895 break;
Robert Shih08528432015-04-08 09:06:54 -0700896 case MEDIA_META_DATA:
897 ALOGV("Received timed metadata message");
898 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899 default:
Steve Block3856b092011-10-20 11:56:00 +0100900 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901 break;
902 }
903
904 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700905 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906
907 // this prevents re-entrant calls into client code
908 if ((listener != 0) && send) {
909 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700911 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800913 }
914}
915
James Dongdd172fc2010-01-15 18:13:58 -0800916void MediaPlayer::died()
917{
Steve Block3856b092011-10-20 11:56:00 +0100918 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800919 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
920}
921
Marco Nelissen6b74d672012-02-28 16:07:44 -0800922status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700923 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800924 if (mPlayer == NULL) {
925 return NO_INIT;
926 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700927
928 if (next != NULL && !(next->mCurrentState &
929 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
930 ALOGE("next player is not prepared");
931 return INVALID_OPERATION;
932 }
933
Marco Nelissen6b74d672012-02-28 16:07:44 -0800934 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
935}
936
Glenn Kasten40bc9062015-03-20 09:09:33 -0700937} // namespace android