blob: 49101d190b103a23eb205d512e88b54729a2839a [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
Hassan Shojaniacefac142017-02-06 21:02:02 -080019#define LOG_TAG "MediaPlayerNative"
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>
Ray Essickdb122142017-01-25 17:51:55 -080039#include <media/MediaAnalyticsItem.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040
Mathias Agopian75624082009-05-19 19:08:10 -070041#include <binder/MemoryBase.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042
Andreas Huber2db84552010-01-28 11:19:57 -080043#include <utils/KeyedVector.h>
44#include <utils/String8.h>
45
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Jamie Gennis61c7ef52011-07-13 12:59:34 -070047#include <system/window.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070048
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049namespace android {
50
Ivan Lozano8cf3a072017-08-09 09:01:33 -070051using media::VolumeShaper;
52
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053MediaPlayer::MediaPlayer()
54{
Steve Block3856b092011-10-20 11:56:00 +010055 ALOGV("constructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056 mListener = NULL;
57 mCookie = NULL;
Dima Zavinfce7a472011-04-19 22:30:36 -070058 mStreamType = AUDIO_STREAM_MUSIC;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070059 mAudioAttributesParcel = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080061 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -080063 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 mCurrentState = MEDIA_PLAYER_IDLE;
65 mPrepareSync = false;
66 mPrepareStatus = NO_ERROR;
67 mLoop = false;
68 mLeftVolume = mRightVolume = 1.0;
69 mVideoWidth = mVideoHeight = 0;
Jason Sams1af452f2009-03-24 18:45:22 -070070 mLockThreadId = 0;
Glenn Kastend848eb42016-03-08 13:42:11 -080071 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Marco Nelissend457c972014-02-11 08:47:07 -080072 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
Eric Laurent8c563ed2010-10-07 18:23:03 -070073 mSendLevel = 0;
John Grossmanc795b642012-02-22 15:38:35 -080074 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075}
76
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077MediaPlayer::~MediaPlayer()
78{
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("destructor");
Jean-Michel Trivi640adb32014-09-05 11:20:11 -070080 if (mAudioAttributesParcel != NULL) {
81 delete mAudioAttributesParcel;
82 mAudioAttributesParcel = NULL;
83 }
Marco Nelissend457c972014-02-11 08:47:07 -080084 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 disconnect();
86 IPCThreadState::self()->flushCommands();
87}
88
89void MediaPlayer::disconnect()
90{
Steve Block3856b092011-10-20 11:56:00 +010091 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 sp<IMediaPlayer> p;
93 {
94 Mutex::Autolock _l(mLock);
95 p = mPlayer;
96 mPlayer.clear();
97 }
98
99 if (p != 0) {
100 p->disconnect();
101 }
102}
103
104// always call with lock held
105void MediaPlayer::clear_l()
106{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 mCurrentPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800108 mCurrentSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800110 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 mVideoWidth = mVideoHeight = 0;
John Grossmanc795b642012-02-22 15:38:35 -0800112 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113}
114
115status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener)
116{
Steve Block3856b092011-10-20 11:56:00 +0100117 ALOGV("setListener");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 Mutex::Autolock _l(mLock);
119 mListener = listener;
120 return NO_ERROR;
121}
122
123
Dave Burked681bbb2011-08-30 14:39:17 +0100124status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125{
126 status_t err = UNKNOWN_ERROR;
127 sp<IMediaPlayer> p;
128 { // scope for the lock
129 Mutex::Autolock _l(mLock);
130
Marco Nelissen83ff1432010-03-10 10:53:16 -0800131 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) ||
132 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) {
Steve Block29357bc2012-01-06 19:20:56 +0000133 ALOGE("attachNewPlayer called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 return INVALID_OPERATION;
135 }
136
137 clear_l();
138 p = mPlayer;
139 mPlayer = player;
140 if (player != 0) {
141 mCurrentState = MEDIA_PLAYER_INITIALIZED;
Wei Jiaed320862017-01-18 14:03:40 -0800142 player->getDefaultBufferingSettings(&mCurrentBufferingSettings);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 err = NO_ERROR;
144 } else {
Wei Jiaed320862017-01-18 14:03:40 -0800145 mCurrentBufferingSettings = BufferingSettings();
Masaki Muranakaf65fa172013-06-06 09:36:34 +0000146 ALOGE("Unable to create media player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 }
148 }
149
150 if (p != 0) {
151 p->disconnect();
152 }
153
154 return err;
155}
156
Andreas Huber2db84552010-01-28 11:19:57 -0800157status_t MediaPlayer::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800158 const sp<IMediaHTTPService> &httpService,
Andreas Huber2db84552010-01-28 11:19:57 -0800159 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160{
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 status_t err = BAD_VALUE;
163 if (url != NULL) {
Marco Nelissenfc908d02016-06-07 12:26:43 -0700164 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800166 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800167 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
Andreas Huber1b86fe02014-01-29 11:13:26 -0800168 (NO_ERROR != player->setDataSource(httpService, url, headers))) {
Dave Burke06620672011-09-06 20:39:47 +0100169 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100170 }
Dave Burke06620672011-09-06 20:39:47 +0100171 err = attachNewPlayer(player);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 }
173 }
174 return err;
175}
176
177status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length)
178{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700179 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700181 const sp<IMediaPlayerService> service(getMediaPlayerService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 if (service != 0) {
Glenn Kastenf37971f2012-02-03 11:06:53 -0800183 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
John Grossmanc795b642012-02-22 15:38:35 -0800184 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
185 (NO_ERROR != player->setDataSource(fd, offset, length))) {
Dave Burke06620672011-09-06 20:39:47 +0100186 player.clear();
Dave Burked681bbb2011-08-30 14:39:17 +0100187 }
Dave Burke06620672011-09-06 20:39:47 +0100188 err = attachNewPlayer(player);
Dave Burked681bbb2011-08-30 14:39:17 +0100189 }
190 return err;
191}
192
Chris Watkins99f31602015-03-20 13:06:33 -0700193status_t MediaPlayer::setDataSource(const sp<IDataSource> &source)
194{
195 ALOGV("setDataSource(IDataSource)");
196 status_t err = UNKNOWN_ERROR;
Marco Nelissenfc908d02016-06-07 12:26:43 -0700197 const sp<IMediaPlayerService> service(getMediaPlayerService());
Chris Watkins99f31602015-03-20 13:06:33 -0700198 if (service != 0) {
199 sp<IMediaPlayer> player(service->create(this, mAudioSessionId));
200 if ((NO_ERROR != doSetRetransmitEndpoint(player)) ||
201 (NO_ERROR != player->setDataSource(source))) {
202 player.clear();
203 }
204 err = attachNewPlayer(player);
205 }
206 return err;
207}
208
Nicolas Catania1d187f12009-05-12 23:25:55 -0700209status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply)
210{
211 Mutex::Autolock _l(mLock);
Nicolas Catania40234932010-03-10 10:41:04 -0800212 const bool hasBeenInitialized =
213 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) &&
214 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE);
215 if ((mPlayer != NULL) && hasBeenInitialized) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700216 ALOGV("invoke %zu", request.dataSize());
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700217 return mPlayer->invoke(request, reply);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700218 }
Wei Jia848ebc62016-03-23 14:06:46 -0700219 ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Nicolas Catania1d187f12009-05-12 23:25:55 -0700220 return INVALID_OPERATION;
221}
222
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700223status_t MediaPlayer::setMetadataFilter(const Parcel& filter)
224{
Steve Blockb8a80522011-12-20 16:23:08 +0000225 ALOGD("setMetadataFilter");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700226 Mutex::Autolock lock(mLock);
227 if (mPlayer == NULL) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700228 return NO_INIT;
229 }
230 return mPlayer->setMetadataFilter(filter);
231}
Nicolas Catania1d187f12009-05-12 23:25:55 -0700232
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700233status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata)
234{
Steve Blockb8a80522011-12-20 16:23:08 +0000235 ALOGD("getMetadata");
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700236 Mutex::Autolock lock(mLock);
237 if (mPlayer == NULL) {
238 return NO_INIT;
239 }
240 return mPlayer->getMetadata(update_only, apply_filter, metadata);
241}
242
Glenn Kasten11731182011-02-08 17:26:17 -0800243status_t MediaPlayer::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800244 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800245{
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("setVideoSurfaceTexture");
Glenn Kasten11731182011-02-08 17:26:17 -0800247 Mutex::Autolock _l(mLock);
248 if (mPlayer == 0) return NO_INIT;
Andy McFadden484566c2012-12-18 09:46:54 -0800249 return mPlayer->setVideoSurfaceTexture(bufferProducer);
Glenn Kasten11731182011-02-08 17:26:17 -0800250}
251
Wei Jiadc6f3402017-01-09 15:04:18 -0800252status_t MediaPlayer::getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */)
253{
254 ALOGV("getDefaultBufferingSettings");
255
256 Mutex::Autolock _l(mLock);
257 if (mPlayer == 0) {
258 return NO_INIT;
259 }
260 return mPlayer->getDefaultBufferingSettings(buffering);
261}
262
Wei Jiaed320862017-01-18 14:03:40 -0800263status_t MediaPlayer::getBufferingSettings(BufferingSettings* buffering /* nonnull */)
264{
265 ALOGV("getBufferingSettings");
266
267 Mutex::Autolock _l(mLock);
268 if (mPlayer == 0) {
269 return NO_INIT;
270 }
271 *buffering = mCurrentBufferingSettings;
272 return NO_ERROR;
273}
274
Wei Jiadc6f3402017-01-09 15:04:18 -0800275status_t MediaPlayer::setBufferingSettings(const BufferingSettings& buffering)
276{
277 ALOGV("setBufferingSettings");
278
279 Mutex::Autolock _l(mLock);
280 if (mPlayer == 0) {
281 return NO_INIT;
282 }
Wei Jiaed320862017-01-18 14:03:40 -0800283 status_t err = mPlayer->setBufferingSettings(buffering);
284 if (err == NO_ERROR) {
285 mCurrentBufferingSettings = buffering;
286 }
287 return err;
Wei Jiadc6f3402017-01-09 15:04:18 -0800288}
289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290// must call with lock held
291status_t MediaPlayer::prepareAsync_l()
292{
Glenn Kastenb187de12014-12-30 08:18:15 -0800293 if ( (mPlayer != 0) && ( mCurrentState & (MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) {
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700294 if (mAudioAttributesParcel != NULL) {
295 mPlayer->setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, *mAudioAttributesParcel);
Eric Laurent43562692015-07-15 16:49:07 -0700296 } else {
297 mPlayer->setAudioStreamType(mStreamType);
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700298 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 mCurrentState = MEDIA_PLAYER_PREPARING;
300 return mPlayer->prepareAsync();
301 }
Wei Jia848ebc62016-03-23 14:06:46 -0700302 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 return INVALID_OPERATION;
304}
305
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700306// TODO: In case of error, prepareAsync provides the caller with 2 error codes,
307// one defined in the Android framework and one provided by the implementation
308// that generated the error. The sync version of prepare returns only 1 error
309// code.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310status_t MediaPlayer::prepare()
311{
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 Mutex::Autolock _l(mLock);
Jason Sams1af452f2009-03-24 18:45:22 -0700314 mLockThreadId = getThreadId();
315 if (mPrepareSync) {
316 mLockThreadId = 0;
317 return -EALREADY;
318 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 mPrepareSync = true;
320 status_t ret = prepareAsync_l();
Jason Sams1af452f2009-03-24 18:45:22 -0700321 if (ret != NO_ERROR) {
322 mLockThreadId = 0;
323 return ret;
324 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325
326 if (mPrepareSync) {
327 mSignal.wait(mLock); // wait for prepare done
328 mPrepareSync = false;
329 }
Steve Block3856b092011-10-20 11:56:00 +0100330 ALOGV("prepare complete - status=%d", mPrepareStatus);
Jason Sams1af452f2009-03-24 18:45:22 -0700331 mLockThreadId = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 return mPrepareStatus;
333}
334
335status_t MediaPlayer::prepareAsync()
336{
Steve Block3856b092011-10-20 11:56:00 +0100337 ALOGV("prepareAsync");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 Mutex::Autolock _l(mLock);
339 return prepareAsync_l();
340}
341
342status_t MediaPlayer::start()
343{
Steve Block3856b092011-10-20 11:56:00 +0100344 ALOGV("start");
Chong Zhangd88adb92014-07-23 11:43:46 -0700345
346 status_t ret = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 Mutex::Autolock _l(mLock);
Chong Zhangd88adb92014-07-23 11:43:46 -0700348
349 mLockThreadId = getThreadId();
350
351 if (mCurrentState & MEDIA_PLAYER_STARTED) {
352 ret = NO_ERROR;
353 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
355 mPlayer->setLooping(mLoop);
356 mPlayer->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -0700357 mPlayer->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 mCurrentState = MEDIA_PLAYER_STARTED;
Chong Zhangd88adb92014-07-23 11:43:46 -0700359 ret = mPlayer->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 if (ret != NO_ERROR) {
361 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
362 } else {
363 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
Steve Block3856b092011-10-20 11:56:00 +0100364 ALOGV("playback completed immediately following start()");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 }
366 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700367 } else {
Wei Jia848ebc62016-03-23 14:06:46 -0700368 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Chong Zhangd88adb92014-07-23 11:43:46 -0700369 ret = INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 }
Chong Zhangd88adb92014-07-23 11:43:46 -0700371
372 mLockThreadId = 0;
373
374 return ret;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375}
376
377status_t MediaPlayer::stop()
378{
Steve Block3856b092011-10-20 11:56:00 +0100379 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 Mutex::Autolock _l(mLock);
381 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR;
382 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
383 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) {
384 status_t ret = mPlayer->stop();
385 if (ret != NO_ERROR) {
386 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
387 } else {
388 mCurrentState = MEDIA_PLAYER_STOPPED;
389 }
390 return ret;
391 }
Wei Jia848ebc62016-03-23 14:06:46 -0700392 ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 return INVALID_OPERATION;
394}
395
396status_t MediaPlayer::pause()
397{
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 Mutex::Autolock _l(mLock);
Marco Nelissen698f4762010-02-26 13:16:23 -0800400 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE))
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 return NO_ERROR;
402 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) {
403 status_t ret = mPlayer->pause();
404 if (ret != NO_ERROR) {
405 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
406 } else {
407 mCurrentState = MEDIA_PLAYER_PAUSED;
408 }
409 return ret;
410 }
Wei Jia848ebc62016-03-23 14:06:46 -0700411 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 return INVALID_OPERATION;
413}
414
415bool MediaPlayer::isPlaying()
416{
417 Mutex::Autolock _l(mLock);
418 if (mPlayer != 0) {
419 bool temp = false;
420 mPlayer->isPlaying(&temp);
Steve Block3856b092011-10-20 11:56:00 +0100421 ALOGV("isPlaying: %d", temp);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) {
Steve Block29357bc2012-01-06 19:20:56 +0000423 ALOGE("internal/external state mismatch corrected");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 mCurrentState = MEDIA_PLAYER_PAUSED;
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700425 } else if ((mCurrentState & MEDIA_PLAYER_PAUSED) && temp) {
426 ALOGE("internal/external state mismatch corrected");
427 mCurrentState = MEDIA_PLAYER_STARTED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 }
429 return temp;
430 }
Steve Block3856b092011-10-20 11:56:00 +0100431 ALOGV("isPlaying: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 return false;
433}
434
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700435status_t MediaPlayer::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -0800436{
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700437 ALOGV("setPlaybackSettings: %f %f %d %d",
438 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
439 // Negative speed and pitch does not make sense. Further validation will
440 // be done by the respective mediaplayers.
441 if (rate.mSpeed < 0.f || rate.mPitch < 0.f) {
Wei Jia98160162015-02-04 17:01:11 -0800442 return BAD_VALUE;
443 }
444 Mutex::Autolock _l(mLock);
Wei Jia5af6a9b2016-06-16 12:07:26 -0700445 if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER_STOPPED)) {
446 return INVALID_OPERATION;
447 }
Wei Jia5be109f2016-06-10 16:15:07 -0700448
449 if (rate.mSpeed != 0.f && !(mCurrentState & MEDIA_PLAYER_STARTED)
450 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
451 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
452 mPlayer->setLooping(mLoop);
453 mPlayer->setVolume(mLeftVolume, mRightVolume);
454 mPlayer->setAuxEffectSendLevel(mSendLevel);
455 }
456
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700457 status_t err = mPlayer->setPlaybackSettings(rate);
458 if (err == OK) {
459 if (rate.mSpeed == 0.f && mCurrentState == MEDIA_PLAYER_STARTED) {
460 mCurrentState = MEDIA_PLAYER_PAUSED;
Wei Jia5be109f2016-06-10 16:15:07 -0700461 } else if (rate.mSpeed != 0.f
462 && (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED
463 | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700464 mCurrentState = MEDIA_PLAYER_STARTED;
Wei Jia98160162015-02-04 17:01:11 -0800465 }
Wei Jia98160162015-02-04 17:01:11 -0800466 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700467 return err;
468}
469
470status_t MediaPlayer::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
471{
472 Mutex::Autolock _l(mLock);
473 if (mPlayer == 0) return INVALID_OPERATION;
474 return mPlayer->getPlaybackSettings(rate);
475}
476
477status_t MediaPlayer::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint)
478{
479 ALOGV("setSyncSettings: %u %u %f %f",
480 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
481 Mutex::Autolock _l(mLock);
482 if (mPlayer == 0) return INVALID_OPERATION;
483 return mPlayer->setSyncSettings(sync, videoFpsHint);
484}
485
486status_t MediaPlayer::getSyncSettings(
487 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
488{
489 Mutex::Autolock _l(mLock);
490 if (mPlayer == 0) return INVALID_OPERATION;
491 return mPlayer->getSyncSettings(sync, videoFps);
Wei Jia98160162015-02-04 17:01:11 -0800492}
493
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494status_t MediaPlayer::getVideoWidth(int *w)
495{
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("getVideoWidth");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497 Mutex::Autolock _l(mLock);
498 if (mPlayer == 0) return INVALID_OPERATION;
499 *w = mVideoWidth;
500 return NO_ERROR;
501}
502
503status_t MediaPlayer::getVideoHeight(int *h)
504{
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("getVideoHeight");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 Mutex::Autolock _l(mLock);
507 if (mPlayer == 0) return INVALID_OPERATION;
508 *h = mVideoHeight;
509 return NO_ERROR;
510}
511
512status_t MediaPlayer::getCurrentPosition(int *msec)
513{
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 Mutex::Autolock _l(mLock);
516 if (mPlayer != 0) {
517 if (mCurrentPosition >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100518 ALOGV("Using cached seek position: %d", mCurrentPosition);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 *msec = mCurrentPosition;
520 return NO_ERROR;
521 }
522 return mPlayer->getCurrentPosition(msec);
523 }
524 return INVALID_OPERATION;
525}
526
527status_t MediaPlayer::getDuration_l(int *msec)
528{
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800529 ALOGV("getDuration_l");
Glenn Kastenb187de12014-12-30 08:18:15 -0800530 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
531 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 if (mPlayer != 0 && isValidState) {
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800533 int durationMs;
534 status_t ret = mPlayer->getDuration(&durationMs);
Andreas Huber20702542013-04-11 11:07:55 -0700535
536 if (ret != OK) {
537 // Do not enter error state just because no duration was available.
538 durationMs = -1;
539 ret = OK;
540 }
541
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800542 if (msec) {
543 *msec = durationMs;
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 return ret;
546 }
Wei Jia848ebc62016-03-23 14:06:46 -0700547 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
548 mPlayer.get(), mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 return INVALID_OPERATION;
550}
551
552status_t MediaPlayer::getDuration(int *msec)
553{
554 Mutex::Autolock _l(mLock);
555 return getDuration_l(msec);
556}
557
Wei Jiac5de0912016-11-18 10:22:14 -0800558status_t MediaPlayer::seekTo_l(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559{
Wei Jiac5de0912016-11-18 10:22:14 -0800560 ALOGV("seekTo (%d, %d)", msec, mode);
Glenn Kastenb187de12014-12-30 08:18:15 -0800561 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED |
562 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 if ( msec < 0 ) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000564 ALOGW("Attempt to seek to invalid position: %d", msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 msec = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 }
Andreas Hubera4c5bc02012-11-27 15:02:53 -0800567
568 int durationMs;
569 status_t err = mPlayer->getDuration(&durationMs);
570
571 if (err != OK) {
572 ALOGW("Stream has no duration and is therefore not seekable.");
573 return err;
574 }
575
576 if (msec > durationMs) {
577 ALOGW("Attempt to seek to past end of file: request = %d, "
578 "durationMs = %d",
579 msec,
580 durationMs);
581
582 msec = durationMs;
583 }
584
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 // cache duration
586 mCurrentPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800587 mCurrentSeekMode = mode;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 if (mSeekPosition < 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 mSeekPosition = msec;
Wei Jiac5de0912016-11-18 10:22:14 -0800590 mSeekMode = mode;
591 return mPlayer->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592 }
593 else {
Wei Jiac5de0912016-11-18 10:22:14 -0800594 ALOGV("Seek in progress - queue up seekTo[%d, %d]", msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 return NO_ERROR;
596 }
597 }
Glenn Kastenb187de12014-12-30 08:18:15 -0800598 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(),
599 mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 return INVALID_OPERATION;
601}
602
Wei Jiac5de0912016-11-18 10:22:14 -0800603status_t MediaPlayer::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604{
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700605 mLockThreadId = getThreadId();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 Mutex::Autolock _l(mLock);
Wei Jiac5de0912016-11-18 10:22:14 -0800607 status_t result = seekTo_l(msec, mode);
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700608 mLockThreadId = 0;
609
610 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611}
612
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700613status_t MediaPlayer::reset_l()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 mLoop = false;
616 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR;
617 mPrepareSync = false;
618 if (mPlayer != 0) {
619 status_t ret = mPlayer->reset();
620 if (ret != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000621 ALOGE("reset() failed with return code (%d)", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
623 } else {
Robert Shih2b95bda2015-07-22 10:11:51 -0700624 mPlayer->disconnect();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 mCurrentState = MEDIA_PLAYER_IDLE;
626 }
James Donga1680bc2010-11-18 12:23:58 -0800627 // setDataSource has to be called again to create a
628 // new mediaplayer.
629 mPlayer = 0;
Wei Jiaed320862017-01-18 14:03:40 -0800630 mCurrentBufferingSettings = BufferingSettings();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 return ret;
632 }
633 clear_l();
634 return NO_ERROR;
635}
636
John Grossmanc795b642012-02-22 15:38:35 -0800637status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) {
638 Mutex::Autolock _l(mLock);
639
640 if (player == NULL) {
641 return UNKNOWN_ERROR;
642 }
643
644 if (mRetransmitEndpointValid) {
645 return player->setRetransmitEndpoint(&mRetransmitEndpoint);
646 }
647
648 return OK;
649}
650
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700651status_t MediaPlayer::reset()
652{
Steve Block3856b092011-10-20 11:56:00 +0100653 ALOGV("reset");
Wonsik Kim5f648972017-07-21 15:42:59 -0700654 mLockThreadId = getThreadId();
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700655 Mutex::Autolock _l(mLock);
Wonsik Kim5f648972017-07-21 15:42:59 -0700656 status_t result = reset_l();
657 mLockThreadId = 0;
658
659 return result;
Jamie Gennis61c7ef52011-07-13 12:59:34 -0700660}
661
Glenn Kastenfff6d712012-01-12 16:38:12 -0800662status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663{
Steve Block3856b092011-10-20 11:56:00 +0100664 ALOGV("MediaPlayer::setAudioStreamType");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 Mutex::Autolock _l(mLock);
666 if (mStreamType == type) return NO_ERROR;
667 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
668 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) {
669 // Can't change the stream type after prepare
Steve Block29357bc2012-01-06 19:20:56 +0000670 ALOGE("setAudioStream called in state %d", mCurrentState);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return INVALID_OPERATION;
672 }
673 // cache
674 mStreamType = type;
675 return OK;
676}
677
John Spurlockde9453f2014-03-19 13:05:45 -0400678status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type)
679{
680 ALOGV("getAudioStreamType");
681 Mutex::Autolock _l(mLock);
682 *type = mStreamType;
683 return OK;
684}
685
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686status_t MediaPlayer::setLooping(int loop)
687{
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("MediaPlayer::setLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 Mutex::Autolock _l(mLock);
690 mLoop = (loop != 0);
691 if (mPlayer != 0) {
692 return mPlayer->setLooping(loop);
693 }
694 return OK;
695}
696
697bool MediaPlayer::isLooping() {
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("isLooping");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699 Mutex::Autolock _l(mLock);
700 if (mPlayer != 0) {
701 return mLoop;
702 }
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("isLooping: no active player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 return false;
705}
706
707status_t MediaPlayer::setVolume(float leftVolume, float rightVolume)
708{
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710 Mutex::Autolock _l(mLock);
711 mLeftVolume = leftVolume;
712 mRightVolume = rightVolume;
713 if (mPlayer != 0) {
714 return mPlayer->setVolume(leftVolume, rightVolume);
715 }
716 return OK;
717}
718
Glenn Kastend848eb42016-03-08 13:42:11 -0800719status_t MediaPlayer::setAudioSessionId(audio_session_t sessionId)
Eric Laurenta514bdb2010-06-21 09:27:30 -0700720{
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700722 Mutex::Autolock _l(mLock);
723 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) {
Steve Block29357bc2012-01-06 19:20:56 +0000724 ALOGE("setAudioSessionId called in state %d", mCurrentState);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700725 return INVALID_OPERATION;
726 }
727 if (sessionId < 0) {
728 return BAD_VALUE;
729 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700730 if (sessionId != mAudioSessionId) {
Marco Nelissend457c972014-02-11 08:47:07 -0800731 AudioSystem::acquireAudioSessionId(sessionId, -1);
732 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700733 mAudioSessionId = sessionId;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700734 }
Eric Laurenta514bdb2010-06-21 09:27:30 -0700735 return NO_ERROR;
736}
737
Glenn Kastend848eb42016-03-08 13:42:11 -0800738audio_session_t MediaPlayer::getAudioSessionId()
Eric Laurenta514bdb2010-06-21 09:27:30 -0700739{
740 Mutex::Autolock _l(mLock);
741 return mAudioSessionId;
742}
743
Eric Laurent2beeb502010-07-16 07:43:46 -0700744status_t MediaPlayer::setAuxEffectSendLevel(float level)
745{
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700747 Mutex::Autolock _l(mLock);
748 mSendLevel = level;
749 if (mPlayer != 0) {
750 return mPlayer->setAuxEffectSendLevel(level);
751 }
752 return OK;
753}
754
755status_t MediaPlayer::attachAuxEffect(int effectId)
756{
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700758 Mutex::Autolock _l(mLock);
759 if (mPlayer == 0 ||
760 (mCurrentState & MEDIA_PLAYER_IDLE) ||
761 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) {
Wei Jia848ebc62016-03-23 14:06:46 -0700762 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
Eric Laurent2beeb502010-07-16 07:43:46 -0700763 return INVALID_OPERATION;
764 }
765
766 return mPlayer->attachAuxEffect(effectId);
767}
768
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700769// always call with lock held
770status_t MediaPlayer::checkStateForKeySet_l(int key)
771{
772 switch(key) {
773 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
774 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED |
775 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) {
776 // Can't change the audio attributes after prepare
777 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
778 return INVALID_OPERATION;
779 }
780 break;
781 default:
782 // parameter doesn't require player state check
783 break;
784 }
785 return OK;
786}
787
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700788status_t MediaPlayer::setParameter(int key, const Parcel& request)
789{
Steve Block3856b092011-10-20 11:56:00 +0100790 ALOGV("MediaPlayer::setParameter(%d)", key);
Eric Laurent43562692015-07-15 16:49:07 -0700791 status_t status = INVALID_OPERATION;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700792 Mutex::Autolock _l(mLock);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700793 if (checkStateForKeySet_l(key) != OK) {
Eric Laurent43562692015-07-15 16:49:07 -0700794 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700795 }
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700796 switch (key) {
797 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
Eric Laurent43562692015-07-15 16:49:07 -0700798 // save the marshalled audio attributes
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700799 if (mAudioAttributesParcel != NULL) { delete mAudioAttributesParcel; };
800 mAudioAttributesParcel = new Parcel();
801 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Eric Laurent43562692015-07-15 16:49:07 -0700802 status = OK;
803 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700804 default:
Eric Laurent43562692015-07-15 16:49:07 -0700805 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
806 break;
Jean-Michel Trivi640adb32014-09-05 11:20:11 -0700807 }
Eric Laurent43562692015-07-15 16:49:07 -0700808
809 if (mPlayer != NULL) {
810 status = mPlayer->setParameter(key, request);
811 }
812 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700813}
814
815status_t MediaPlayer::getParameter(int key, Parcel *reply)
816{
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("MediaPlayer::getParameter(%d)", key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700818 Mutex::Autolock _l(mLock);
819 if (mPlayer != NULL) {
Ray Essickdb122142017-01-25 17:51:55 -0800820 status_t status = mPlayer->getParameter(key, reply);
821 if (status != OK) {
822 ALOGD("getParameter returns %d", status);
823 }
824 return status;
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700825 }
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("getParameter: no active player");
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700827 return INVALID_OPERATION;
828}
829
John Grossmanc795b642012-02-22 15:38:35 -0800830status_t MediaPlayer::setRetransmitEndpoint(const char* addrString,
831 uint16_t port) {
832 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)",
833 addrString ? addrString : "(null)", port);
834
835 Mutex::Autolock _l(mLock);
836 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE))
837 return INVALID_OPERATION;
838
839 if (NULL == addrString) {
840 mRetransmitEndpointValid = false;
841 return OK;
842 }
843
844 struct in_addr saddr;
845 if(!inet_aton(addrString, &saddr)) {
846 return BAD_VALUE;
847 }
848
Glenn Kastenbe08f6a2013-12-19 09:09:33 -0800849 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint));
John Grossmanc795b642012-02-22 15:38:35 -0800850 mRetransmitEndpoint.sin_family = AF_INET;
851 mRetransmitEndpoint.sin_addr = saddr;
852 mRetransmitEndpoint.sin_port = htons(port);
853 mRetransmitEndpointValid = true;
854
855 return OK;
856}
857
Gloria Wangb483c472011-04-11 17:23:27 -0700858void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800859{
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 bool send = true;
Jason Sams1af452f2009-03-24 18:45:22 -0700862 bool locked = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863
864 // TODO: In the future, we might be on the same thread if the app is
865 // running in the same process as the media server. In that case,
866 // this will deadlock.
Nicolas Catania66095182009-06-11 16:33:49 -0700867 //
Chong Zhangd88adb92014-07-23 11:43:46 -0700868 // The threadId hack below works around this for the care of prepare,
Wonsik Kim5f648972017-07-21 15:42:59 -0700869 // seekTo, start, and reset within the same process.
Andreas Huber5cb07aa2009-03-24 20:48:51 -0700870 // FIXME: Remember, this is a hack, it's not even a hack that is applied
871 // consistently for all use-cases, this needs to be revisited.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700872 if (mLockThreadId != getThreadId()) {
Jason Sams1af452f2009-03-24 18:45:22 -0700873 mLock.lock();
874 locked = true;
Nicolas Catania66095182009-06-11 16:33:49 -0700875 }
Jason Sams1af452f2009-03-24 18:45:22 -0700876
Eric Laurent3b268442010-08-03 07:49:49 -0700877 // Allows calls from JNI in idle state to notify errors
878 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2);
Jason Sams1af452f2009-03-24 18:45:22 -0700880 if (locked) mLock.unlock(); // release the lock when done.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 return;
882 }
883
884 switch (msg) {
885 case MEDIA_NOP: // interface test message
886 break;
887 case MEDIA_PREPARED:
Hassan Shojania071437a2017-01-23 09:19:40 -0800888 ALOGV("MediaPlayer::notify() prepared");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800889 mCurrentState = MEDIA_PLAYER_PREPARED;
890 if (mPrepareSync) {
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892 mPrepareSync = false;
893 mPrepareStatus = NO_ERROR;
894 mSignal.signal();
895 }
896 break;
Hassan Shojania071437a2017-01-23 09:19:40 -0800897 case MEDIA_DRM_INFO:
898 ALOGV("MediaPlayer::notify() MEDIA_DRM_INFO(%d, %d, %d, %p)", msg, ext1, ext2, obj);
899 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800900 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100901 ALOGV("playback complete");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700902 if (mCurrentState == MEDIA_PLAYER_IDLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("playback complete in idle state");
Marco Nelissen1c1503c2010-09-17 15:04:01 -0700904 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905 if (!mLoop) {
906 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE;
907 }
908 break;
909 case MEDIA_ERROR:
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700910 // Always log errors.
911 // ext1: Media framework error code.
912 // ext2: Implementation dependant error code.
Steve Block29357bc2012-01-06 19:20:56 +0000913 ALOGE("error (%d, %d)", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 mCurrentState = MEDIA_PLAYER_STATE_ERROR;
915 if (mPrepareSync)
916 {
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("signal application thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 mPrepareSync = false;
919 mPrepareStatus = ext1;
920 mSignal.signal();
921 send = false;
922 }
923 break;
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700924 case MEDIA_INFO:
925 // ext1: Media framework error code.
926 // ext2: Implementation dependant error code.
Andreas Huber145e68f2011-01-11 15:05:28 -0800927 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000928 ALOGW("info/warning (%d, %d)", ext1, ext2);
Andreas Huber145e68f2011-01-11 15:05:28 -0800929 }
The Android Open Source Project65e731f2009-03-11 12:11:56 -0700930 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800931 case MEDIA_SEEK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +0100932 ALOGV("Received seek complete");
Wei Jiac5de0912016-11-18 10:22:14 -0800933 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
934 ALOGV("Executing queued seekTo(%d, %d)", mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800936 mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
937 seekTo_l(mCurrentPosition, mCurrentSeekMode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938 }
939 else {
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("All seeks complete - return to regularly scheduled program");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 mCurrentPosition = mSeekPosition = -1;
Wei Jiac5de0912016-11-18 10:22:14 -0800942 mCurrentSeekMode = mSeekMode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 }
944 break;
945 case MEDIA_BUFFERING_UPDATE:
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("buffering %d", ext1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 break;
948 case MEDIA_SET_VIDEO_SIZE:
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("New video size %d x %d", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 mVideoWidth = ext1;
951 mVideoHeight = ext2;
952 break;
Gloria Wangb483c472011-04-11 17:23:27 -0700953 case MEDIA_TIMED_TEXT:
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("Received timed text message");
Gloria Wangb483c472011-04-11 17:23:27 -0700955 break;
Chong Zhangdcb89b32013-08-06 09:44:47 -0700956 case MEDIA_SUBTITLE_DATA:
957 ALOGV("Received subtitle data message");
958 break;
Robert Shih08528432015-04-08 09:06:54 -0700959 case MEDIA_META_DATA:
960 ALOGV("Received timed metadata message");
961 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 default:
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 break;
965 }
966
967 sp<MediaPlayerListener> listener = mListener;
Jason Sams1af452f2009-03-24 18:45:22 -0700968 if (locked) mLock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969
970 // this prevents re-entrant calls into client code
971 if ((listener != 0) && send) {
972 Mutex::Autolock _l(mNotifyLock);
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("callback application");
Gloria Wangb483c472011-04-11 17:23:27 -0700974 listener->notify(msg, ext1, ext2, obj);
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("back from callback");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 }
977}
978
James Dongdd172fc2010-01-15 18:13:58 -0800979void MediaPlayer::died()
980{
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("died");
James Dongdd172fc2010-01-15 18:13:58 -0800982 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0);
983}
984
Marco Nelissen6b74d672012-02-28 16:07:44 -0800985status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) {
Wei Jia12438692016-03-28 14:12:57 -0700986 Mutex::Autolock _l(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -0800987 if (mPlayer == NULL) {
988 return NO_INIT;
989 }
Marco Nelissenb13820f2013-08-05 12:22:43 -0700990
991 if (next != NULL && !(next->mCurrentState &
992 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) {
993 ALOGE("next player is not prepared");
994 return INVALID_OPERATION;
995 }
996
Marco Nelissen6b74d672012-02-28 16:07:44 -0800997 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer);
998}
999
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001000VolumeShaper::Status MediaPlayer::applyVolumeShaper(
1001 const sp<VolumeShaper::Configuration>& configuration,
1002 const sp<VolumeShaper::Operation>& operation)
1003{
1004 Mutex::Autolock _l(mLock);
1005 if (mPlayer == nullptr) {
1006 return VolumeShaper::Status(NO_INIT);
1007 }
1008 VolumeShaper::Status status = mPlayer->applyVolumeShaper(configuration, operation);
1009 return status;
1010}
1011
1012sp<VolumeShaper::State> MediaPlayer::getVolumeShaperState(int id)
1013{
1014 Mutex::Autolock _l(mLock);
1015 if (mPlayer == nullptr) {
1016 return nullptr;
1017 }
1018 return mPlayer->getVolumeShaperState(id);
1019}
1020
Hassan Shojaniacefac142017-02-06 21:02:02 -08001021// Modular DRM
1022status_t MediaPlayer::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId)
Hassan Shojania071437a2017-01-23 09:19:40 -08001023{
Hassan Shojaniacefac142017-02-06 21:02:02 -08001024 // TODO change to ALOGV
1025 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1026 drmSessionId.array(), drmSessionId.size());
Hassan Shojania071437a2017-01-23 09:19:40 -08001027 Mutex::Autolock _l(mLock);
1028 if (mPlayer == NULL) {
1029 return NO_INIT;
1030 }
1031
Hassan Shojania838be392017-05-23 14:14:24 -07001032 // Only allowed it in player's preparing/prepared state.
1033 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1034 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1035 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1036 if (!(mCurrentState & (MEDIA_PLAYER_PREPARING | MEDIA_PLAYER_PREPARED))) {
1037 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001038 return INVALID_OPERATION;
1039 }
1040
Hassan Shojaniacefac142017-02-06 21:02:02 -08001041 if (drmSessionId.isEmpty()) {
1042 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1043 return INVALID_OPERATION;
1044 }
Hassan Shojania071437a2017-01-23 09:19:40 -08001045
Hassan Shojaniacefac142017-02-06 21:02:02 -08001046 // Passing down to mediaserver mainly for creating the crypto
1047 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1048 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1049
1050 // TODO change to ALOGV
1051 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1052
1053 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001054}
1055
1056status_t MediaPlayer::releaseDrm()
1057{
1058 Mutex::Autolock _l(mLock);
1059 if (mPlayer == NULL) {
1060 return NO_INIT;
1061 }
1062
Hassan Shojaniacefac142017-02-06 21:02:02 -08001063 // Not allowing releaseDrm in an active/resumable state
1064 if (mCurrentState & (MEDIA_PLAYER_STARTED |
1065 MEDIA_PLAYER_PAUSED |
1066 MEDIA_PLAYER_PLAYBACK_COMPLETE |
1067 MEDIA_PLAYER_STATE_ERROR)) {
1068 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
Hassan Shojania071437a2017-01-23 09:19:40 -08001069 return INVALID_OPERATION;
1070 }
1071
Hassan Shojaniacefac142017-02-06 21:02:02 -08001072 status_t status = mPlayer->releaseDrm();
1073 // TODO change to ALOGV
1074 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1075 if (status != OK) {
1076 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1077 // Overriding to OK so the client proceed with its own cleanup
1078 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1079 status = OK;
Hassan Shojania071437a2017-01-23 09:19:40 -08001080 }
1081
Hassan Shojaniacefac142017-02-06 21:02:02 -08001082 return status;
Hassan Shojania071437a2017-01-23 09:19:40 -08001083}
1084
Glenn Kasten40bc9062015-03-20 09:09:33 -07001085} // namespace android