blob: 8d0df8ae96c0487aa5b98d63a7b359f38d17d92a [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2**
3** Copyright 2017, 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 "MediaPlayer2Native"
20
Wei Jia53692fa2017-12-11 10:33:46 -080021#include <binder/IServiceManager.h>
22#include <binder/IPCThreadState.h>
23
Wei Jia53692fa2017-12-11 10:33:46 -080024#include <media/AudioSystem.h>
Wei Jiac2636032018-02-01 09:15:25 -080025#include <media/DataSourceDesc.h>
Wei Jia53692fa2017-12-11 10:33:46 -080026#include <media/MediaAnalyticsItem.h>
Wei Jiaec044b02018-02-19 12:41:23 -080027#include <media/MemoryLeakTrackUtil.h>
28#include <media/Metadata.h>
Wei Jia28288fb2017-12-15 13:45:29 -080029#include <media/NdkWrapper.h>
Wei Jiaec044b02018-02-19 12:41:23 -080030#include <media/stagefright/foundation/ADebug.h>
31#include <media/stagefright/foundation/ALooperRoster.h>
32#include <mediaplayer2/MediaPlayer2AudioOutput.h>
Wei Jia51b69562018-02-05 16:17:13 -080033#include <mediaplayer2/mediaplayer2.h>
Wei Jia53692fa2017-12-11 10:33:46 -080034
Wei Jiaec044b02018-02-19 12:41:23 -080035#include <utils/Log.h>
36#include <utils/SortedVector.h>
Wei Jia53692fa2017-12-11 10:33:46 -080037#include <utils/String8.h>
38
39#include <system/audio.h>
40#include <system/window.h>
41
Wei Jiaec044b02018-02-19 12:41:23 -080042#include <nuplayer2/NuPlayer2Driver.h>
43
44#include <dirent.h>
45#include <sys/stat.h>
Wei Jia53692fa2017-12-11 10:33:46 -080046
47namespace android {
48
Wei Jiaec044b02018-02-19 12:41:23 -080049extern ALooperRoster gLooperRoster;
50
51namespace {
52
53const int kDumpLockRetries = 50;
54const int kDumpLockSleepUs = 20000;
55
56// Max number of entries in the filter.
57const int kMaxFilterSize = 64; // I pulled that out of thin air.
58
59// FIXME: Move all the metadata related function in the Metadata.cpp
60
61// Unmarshall a filter from a Parcel.
62// Filter format in a parcel:
63//
64// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
65// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66// | number of entries (n) |
67// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68// | metadata type 1 |
69// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70// | metadata type 2 |
71// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72// ....
73// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74// | metadata type n |
75// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76//
77// @param p Parcel that should start with a filter.
78// @param[out] filter On exit contains the list of metadata type to be
79// filtered.
80// @param[out] status On exit contains the status code to be returned.
81// @return true if the parcel starts with a valid filter.
82bool unmarshallFilter(const Parcel& p,
83 media::Metadata::Filter *filter,
84 status_t *status) {
85 int32_t val;
86 if (p.readInt32(&val) != OK) {
87 ALOGE("Failed to read filter's length");
88 *status = NOT_ENOUGH_DATA;
89 return false;
90 }
91
92 if (val > kMaxFilterSize || val < 0) {
93 ALOGE("Invalid filter len %d", val);
94 *status = BAD_VALUE;
95 return false;
96 }
97
98 const size_t num = val;
99
100 filter->clear();
101 filter->setCapacity(num);
102
103 size_t size = num * sizeof(media::Metadata::Type);
104
105
106 if (p.dataAvail() < size) {
107 ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail());
108 *status = NOT_ENOUGH_DATA;
109 return false;
110 }
111
112 const media::Metadata::Type *data =
113 static_cast<const media::Metadata::Type*>(p.readInplace(size));
114
115 if (NULL == data) {
116 ALOGE("Filter had no data");
117 *status = BAD_VALUE;
118 return false;
119 }
120
121 // TODO: The stl impl of vector would be more efficient here
122 // because it degenerates into a memcpy on pod types. Try to
123 // replace later or use stl::set.
124 for (size_t i = 0; i < num; ++i) {
125 filter->add(*data);
126 ++data;
127 }
128 *status = OK;
129 return true;
130}
131
132// @param filter Of metadata type.
133// @param val To be searched.
134// @return true if a match was found.
135bool findMetadata(const media::Metadata::Filter& filter, const int32_t val) {
136 // Deal with empty and ANY right away
137 if (filter.isEmpty()) {
138 return false;
139 }
140 if (filter[0] == media::Metadata::kAny) {
141 return true;
142 }
143
144 return filter.indexOf(val) >= 0;
145}
146
147// marshalling tag indicating flattened utf16 tags
148// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
149const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
150
151// Audio attributes format in a parcel:
152//
153// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
154// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155// | usage |
156// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
157// | content_type |
158// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159// | source |
160// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161// | flags |
162// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
164// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165// | flattened tags in UTF16 |
166// | ... |
167// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168//
169// @param p Parcel that contains audio attributes.
170// @param[out] attributes On exit points to an initialized audio_attributes_t structure
171// @param[out] status On exit contains the status code to be returned.
172void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) {
173 attributes->usage = (audio_usage_t) parcel.readInt32();
174 attributes->content_type = (audio_content_type_t) parcel.readInt32();
175 attributes->source = (audio_source_t) parcel.readInt32();
176 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
177 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
178 if (hasFlattenedTag) {
179 // the tags are UTF16, convert to UTF8
180 String16 tags = parcel.readString16();
181 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
182 if (realTagSize <= 0) {
183 strcpy(attributes->tags, "");
184 } else {
185 // copy the flattened string into the attributes as the destination for the conversion:
186 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
187 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
188 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
189 utf16_to_utf8(tags.string(), tagSize, attributes->tags,
190 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
191 }
192 } else {
193 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
194 strcpy(attributes->tags, "");
195 }
196}
197
198class AudioDeviceUpdatedNotifier: public AudioSystem::AudioDeviceCallback {
199public:
200 AudioDeviceUpdatedNotifier(const sp<MediaPlayer2Interface>& listener)
201 : mListener(listener) { }
202
203 ~AudioDeviceUpdatedNotifier() { }
204
205 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo,
206 audio_port_handle_t deviceId) override {
207 sp<MediaPlayer2Interface> listener = mListener.promote();
208 if (listener != NULL) {
209 listener->sendEvent(0, MEDIA2_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
210 } else {
211 ALOGW("listener for process %d death is gone", MEDIA2_AUDIO_ROUTING_CHANGED);
212 }
213 }
214
215private:
216 wp<MediaPlayer2Interface> mListener;
217};
218
219class proxyListener : public MediaPlayer2InterfaceListener {
220public:
221 proxyListener(const wp<MediaPlayer2> &player)
222 : mPlayer(player) { }
223
224 ~proxyListener() { };
225
Dongwon Kang41929fb2018-09-09 08:29:56 -0700226 virtual void notify(int64_t srcId, int msg, int ext1, int ext2,
227 const PlayerMessage *obj) override {
Wei Jiaec044b02018-02-19 12:41:23 -0800228 sp<MediaPlayer2> player = mPlayer.promote();
229 if (player != NULL) {
230 player->notify(srcId, msg, ext1, ext2, obj);
231 }
232 }
233
234private:
235 wp<MediaPlayer2> mPlayer;
236};
237
238Mutex sRecordLock;
239SortedVector<wp<MediaPlayer2> > *sPlayers;
240
241void ensureInit_l() {
242 if (sPlayers == NULL) {
243 sPlayers = new SortedVector<wp<MediaPlayer2> >();
244 }
245}
246
247void addPlayer(const wp<MediaPlayer2>& player) {
248 Mutex::Autolock lock(sRecordLock);
249 ensureInit_l();
250 sPlayers->add(player);
251}
252
253void removePlayer(const wp<MediaPlayer2>& player) {
254 Mutex::Autolock lock(sRecordLock);
255 ensureInit_l();
256 sPlayers->remove(player);
257}
258
259/**
260 * The only arguments this understands right now are -c, -von and -voff,
261 * which are parsed by ALooperRoster::dump()
262 */
263status_t dumpPlayers(int fd, const Vector<String16>& args) {
264 const size_t SIZE = 256;
265 char buffer[SIZE];
266 String8 result;
267 SortedVector< sp<MediaPlayer2> > players; //to serialise the mutex unlock & client destruction.
268
269 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
270 snprintf(buffer, SIZE, "Permission Denial: can't dump MediaPlayer2\n");
271 result.append(buffer);
272 } else {
273 {
274 Mutex::Autolock lock(sRecordLock);
275 ensureInit_l();
276 for (int i = 0, n = sPlayers->size(); i < n; ++i) {
277 sp<MediaPlayer2> p = (*sPlayers)[i].promote();
278 if (p != 0) {
279 p->dump(fd, args);
280 }
281 players.add(p);
282 }
283 }
284
285 result.append(" Files opened and/or mapped:\n");
286 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
287 FILE *f = fopen(buffer, "r");
288 if (f) {
289 while (!feof(f)) {
290 fgets(buffer, SIZE, f);
291 if (strstr(buffer, " /storage/") ||
292 strstr(buffer, " /system/sounds/") ||
293 strstr(buffer, " /data/") ||
294 strstr(buffer, " /system/media/")) {
295 result.append(" ");
296 result.append(buffer);
297 }
298 }
299 fclose(f);
300 } else {
301 result.append("couldn't open ");
302 result.append(buffer);
303 result.append("\n");
304 }
305
306 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
307 DIR *d = opendir(buffer);
308 if (d) {
309 struct dirent *ent;
310 while((ent = readdir(d)) != NULL) {
311 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
312 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
313 struct stat s;
314 if (lstat(buffer, &s) == 0) {
315 if ((s.st_mode & S_IFMT) == S_IFLNK) {
316 char linkto[256];
317 int len = readlink(buffer, linkto, sizeof(linkto));
318 if(len > 0) {
319 if(len > 255) {
320 linkto[252] = '.';
321 linkto[253] = '.';
322 linkto[254] = '.';
323 linkto[255] = 0;
324 } else {
325 linkto[len] = 0;
326 }
327 if (strstr(linkto, "/storage/") == linkto ||
328 strstr(linkto, "/system/sounds/") == linkto ||
329 strstr(linkto, "/data/") == linkto ||
330 strstr(linkto, "/system/media/") == linkto) {
331 result.append(" ");
332 result.append(buffer);
333 result.append(" -> ");
334 result.append(linkto);
335 result.append("\n");
336 }
337 }
338 } else {
339 result.append(" unexpected type for ");
340 result.append(buffer);
341 result.append("\n");
342 }
343 }
344 }
345 }
346 closedir(d);
347 } else {
348 result.append("couldn't open ");
349 result.append(buffer);
350 result.append("\n");
351 }
352
353 gLooperRoster.dump(fd, args);
354
355 bool dumpMem = false;
356 bool unreachableMemory = false;
357 for (size_t i = 0; i < args.size(); i++) {
358 if (args[i] == String16("-m")) {
359 dumpMem = true;
360 } else if (args[i] == String16("--unreachable")) {
361 unreachableMemory = true;
362 }
363 }
364 if (dumpMem) {
365 result.append("\nDumping memory:\n");
366 std::string s = dumpMemoryAddresses(100 /* limit */);
367 result.append(s.c_str(), s.size());
368 }
369 if (unreachableMemory) {
370 result.append("\nDumping unreachable memory:\n");
371 // TODO - should limit be an argument parameter?
372 // TODO: enable GetUnreachableMemoryString if it's part of stable API
373 //std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
374 //result.append(s.c_str(), s.size());
375 }
376 }
377 write(fd, result.string(), result.size());
378 return NO_ERROR;
379}
380
381} // anonymous namespace
382
383//static
384sp<MediaPlayer2> MediaPlayer2::Create() {
385 sp<MediaPlayer2> player = new MediaPlayer2();
386
387 if (!player->init()) {
388 return NULL;
389 }
390
391 ALOGV("Create new player(%p)", player.get());
392
393 addPlayer(player);
394 return player;
395}
396
397// static
398status_t MediaPlayer2::DumpAll(int fd, const Vector<String16>& args) {
399 return dumpPlayers(fd, args);
400}
401
402MediaPlayer2::MediaPlayer2() {
Wei Jia53692fa2017-12-11 10:33:46 -0800403 ALOGV("constructor");
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800404 mSrcId = 0;
Wei Jiaec044b02018-02-19 12:41:23 -0800405 mLockThreadId = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800406 mListener = NULL;
Wei Jia53692fa2017-12-11 10:33:46 -0800407 mStreamType = AUDIO_STREAM_MUSIC;
408 mAudioAttributesParcel = NULL;
409 mCurrentPosition = -1;
410 mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
411 mSeekPosition = -1;
412 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
413 mCurrentState = MEDIA_PLAYER2_IDLE;
Wei Jia53692fa2017-12-11 10:33:46 -0800414 mLoop = false;
415 mLeftVolume = mRightVolume = 1.0;
416 mVideoWidth = mVideoHeight = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800417 mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
418 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1);
419 mSendLevel = 0;
Wei Jiaec044b02018-02-19 12:41:23 -0800420
421 // TODO: get pid and uid from JAVA
422 mPid = IPCThreadState::self()->getCallingPid();
423 mUid = IPCThreadState::self()->getCallingUid();
424
425 mAudioAttributes = NULL;
Wei Jia53692fa2017-12-11 10:33:46 -0800426}
427
Wei Jiaec044b02018-02-19 12:41:23 -0800428MediaPlayer2::~MediaPlayer2() {
Wei Jia53692fa2017-12-11 10:33:46 -0800429 ALOGV("destructor");
430 if (mAudioAttributesParcel != NULL) {
431 delete mAudioAttributesParcel;
432 mAudioAttributesParcel = NULL;
433 }
434 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
435 disconnect();
Wei Jiaec044b02018-02-19 12:41:23 -0800436 removePlayer(this);
437 if (mAudioAttributes != NULL) {
438 free(mAudioAttributes);
439 }
Wei Jia53692fa2017-12-11 10:33:46 -0800440}
441
Wei Jiaec044b02018-02-19 12:41:23 -0800442bool MediaPlayer2::init() {
443 // TODO: after merge with NuPlayer2Driver, MediaPlayer2 will have its own
444 // looper for notification.
445 return true;
446}
447
448void MediaPlayer2::disconnect() {
Wei Jia53692fa2017-12-11 10:33:46 -0800449 ALOGV("disconnect");
Wei Jiaec044b02018-02-19 12:41:23 -0800450 sp<MediaPlayer2Interface> p;
Wei Jia53692fa2017-12-11 10:33:46 -0800451 {
452 Mutex::Autolock _l(mLock);
453 p = mPlayer;
454 mPlayer.clear();
455 }
456
457 if (p != 0) {
Wei Jiaec044b02018-02-19 12:41:23 -0800458 p->setListener(NULL);
459 p->reset();
460 }
461
462 {
463 Mutex::Autolock _l(mLock);
464 disconnectNativeWindow_l();
Wei Jia53692fa2017-12-11 10:33:46 -0800465 }
466}
467
Wei Jiaec044b02018-02-19 12:41:23 -0800468void MediaPlayer2::clear_l() {
Wei Jia53692fa2017-12-11 10:33:46 -0800469 mCurrentPosition = -1;
470 mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
471 mSeekPosition = -1;
472 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
473 mVideoWidth = mVideoHeight = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800474}
475
Wei Jiaec044b02018-02-19 12:41:23 -0800476status_t MediaPlayer2::setListener(const sp<MediaPlayer2Listener>& listener) {
Wei Jia53692fa2017-12-11 10:33:46 -0800477 ALOGV("setListener");
478 Mutex::Autolock _l(mLock);
479 mListener = listener;
480 return NO_ERROR;
481}
482
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800483status_t MediaPlayer2::getSrcId(int64_t *srcId) {
484 if (srcId == NULL) {
485 return BAD_VALUE;
486 }
Wei Jia53692fa2017-12-11 10:33:46 -0800487
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800488 Mutex::Autolock _l(mLock);
489 *srcId = mSrcId;
490 return OK;
491}
492
Wei Jiaec044b02018-02-19 12:41:23 -0800493status_t MediaPlayer2::setDataSource(const sp<DataSourceDesc> &dsd) {
Wei Jiac2636032018-02-01 09:15:25 -0800494 if (dsd == NULL) {
495 return BAD_VALUE;
Wei Jia53692fa2017-12-11 10:33:46 -0800496 }
Wei Jiaec044b02018-02-19 12:41:23 -0800497 ALOGV("setDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId);
498
499 sp<MediaPlayer2Interface> oldPlayer;
500
501 Mutex::Autolock _l(mLock);
502 {
503 if (!((mCurrentState & MEDIA_PLAYER2_IDLE)
504 || mCurrentState == MEDIA_PLAYER2_STATE_ERROR)) {
505 ALOGE("setDataSource called in wrong state %d", mCurrentState);
506 return INVALID_OPERATION;
507 }
508
509 sp<MediaPlayer2Interface> player = new NuPlayer2Driver(mPid, mUid);
510 status_t err = player->initCheck();
511 if (err != NO_ERROR) {
512 ALOGE("Failed to create player object, initCheck failed(%d)", err);
513 return err;
514 }
515
516 clear_l();
517
518 player->setListener(new proxyListener(this));
519 mAudioOutput = new MediaPlayer2AudioOutput(mAudioSessionId, mUid,
520 mPid, mAudioAttributes, new AudioDeviceUpdatedNotifier(player));
521 player->setAudioSink(mAudioOutput);
522
523 err = player->setDataSource(dsd);
524 if (err != OK) {
525 ALOGE("setDataSource error: %d", err);
526 return err;
527 }
528
529 sp<MediaPlayer2Interface> oldPlayer = mPlayer;
530 mPlayer = player;
531 mSrcId = dsd->mId;
532 mCurrentState = MEDIA_PLAYER2_INITIALIZED;
Wei Jia53692fa2017-12-11 10:33:46 -0800533 }
Wei Jiaec044b02018-02-19 12:41:23 -0800534
535 if (oldPlayer != NULL) {
536 oldPlayer->setListener(NULL);
537 oldPlayer->reset();
538 }
539
540 return OK;
Wei Jia53692fa2017-12-11 10:33:46 -0800541}
542
Wei Jia57aeffd2018-02-15 16:01:14 -0800543status_t MediaPlayer2::prepareNextDataSource(const sp<DataSourceDesc> &dsd) {
544 if (dsd == NULL) {
545 return BAD_VALUE;
546 }
547 ALOGV("prepareNextDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId);
548
549 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800550 if (mPlayer == NULL) {
551 ALOGE("prepareNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
552 return INVALID_OPERATION;
Wei Jia57aeffd2018-02-15 16:01:14 -0800553 }
Wei Jiaec044b02018-02-19 12:41:23 -0800554 return mPlayer->prepareNextDataSource(dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -0800555}
556
557status_t MediaPlayer2::playNextDataSource(int64_t srcId) {
558 ALOGV("playNextDataSource srcId(%lld)", (long long)srcId);
559
560 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800561 if (mPlayer == NULL) {
562 ALOGE("playNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
563 return INVALID_OPERATION;
Wei Jia57aeffd2018-02-15 16:01:14 -0800564 }
Wei Jiaec044b02018-02-19 12:41:23 -0800565 mSrcId = srcId;
566 return mPlayer->playNextDataSource(srcId);
Wei Jia57aeffd2018-02-15 16:01:14 -0800567}
568
Dongwon Kang9f631982018-07-10 12:34:41 -0700569status_t MediaPlayer2::invoke(const PlayerMessage &request, PlayerMessage *reply) {
Wei Jia53692fa2017-12-11 10:33:46 -0800570 Mutex::Autolock _l(mLock);
571 const bool hasBeenInitialized =
572 (mCurrentState != MEDIA_PLAYER2_STATE_ERROR) &&
573 ((mCurrentState & MEDIA_PLAYER2_IDLE) != MEDIA_PLAYER2_IDLE);
Wei Jiaec044b02018-02-19 12:41:23 -0800574 if ((mPlayer == NULL) || !hasBeenInitialized) {
Dongwon Kang9f631982018-07-10 12:34:41 -0700575 ALOGE("invoke() failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get());
Wei Jiaec044b02018-02-19 12:41:23 -0800576 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -0800577 }
Wei Jiaec044b02018-02-19 12:41:23 -0800578 return mPlayer->invoke(request, reply);
Wei Jia53692fa2017-12-11 10:33:46 -0800579}
580
Wei Jiaec044b02018-02-19 12:41:23 -0800581// This call doesn't need to access the native player.
582status_t MediaPlayer2::setMetadataFilter(const Parcel& filter) {
Wei Jia53692fa2017-12-11 10:33:46 -0800583 ALOGD("setMetadataFilter");
Wei Jiaec044b02018-02-19 12:41:23 -0800584
585 status_t status;
586 media::Metadata::Filter allow, drop;
587
588 if (unmarshallFilter(filter, &allow, &status) &&
589 unmarshallFilter(filter, &drop, &status)) {
590 Mutex::Autolock lock(mLock);
591
592 mMetadataAllow = allow;
593 mMetadataDrop = drop;
Wei Jia53692fa2017-12-11 10:33:46 -0800594 }
Wei Jiaec044b02018-02-19 12:41:23 -0800595 return status;
Wei Jia53692fa2017-12-11 10:33:46 -0800596}
597
Wei Jiaec044b02018-02-19 12:41:23 -0800598status_t MediaPlayer2::getMetadata(bool update_only, bool /* apply_filter */, Parcel *reply) {
Wei Jia53692fa2017-12-11 10:33:46 -0800599 ALOGD("getMetadata");
Wei Jiaec044b02018-02-19 12:41:23 -0800600 sp<MediaPlayer2Interface> player;
601 media::Metadata::Filter ids;
Wei Jia53692fa2017-12-11 10:33:46 -0800602 Mutex::Autolock lock(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800603 {
604 if (mPlayer == NULL) {
605 return NO_INIT;
606 }
607
608 player = mPlayer;
609 // Placeholder for the return code, updated by the caller.
610 reply->writeInt32(-1);
611
612 // We don't block notifications while we fetch the data. We clear
613 // mMetadataUpdated first so we don't lose notifications happening
614 // during the rest of this call.
615 if (update_only) {
616 ids = mMetadataUpdated;
617 }
618 mMetadataUpdated.clear();
619 }
620
621 media::Metadata metadata(reply);
622
623 metadata.appendHeader();
624 status_t status = player->getMetadata(ids, reply);
625
626 if (status != OK) {
627 metadata.resetParcel();
628 ALOGE("getMetadata failed %d", status);
629 return status;
630 }
631
632 // FIXME: ement filtering on the result. Not critical since
633 // filtering takes place on the update notifications already. This
634 // would be when all the metadata are fetch and a filter is set.
635
636 // Everything is fine, update the metadata length.
637 metadata.updateLength();
638 return OK;
639}
640
641void MediaPlayer2::disconnectNativeWindow_l() {
642 if (mConnectedWindow != NULL && mConnectedWindow->getANativeWindow() != NULL) {
643 status_t err = native_window_api_disconnect(
644 mConnectedWindow->getANativeWindow(), NATIVE_WINDOW_API_MEDIA);
645
646 if (err != OK) {
647 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
648 strerror(-err), err);
649 }
650 }
651 mConnectedWindow.clear();
652}
653
654status_t MediaPlayer2::setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww) {
655 ANativeWindow *anw = (nww == NULL ? NULL : nww->getANativeWindow());
656 ALOGV("setVideoSurfaceTexture(%p)", anw);
657 Mutex::Autolock _l(mLock);
658 if (mPlayer == 0) {
Wei Jia53692fa2017-12-11 10:33:46 -0800659 return NO_INIT;
660 }
Wei Jiaec044b02018-02-19 12:41:23 -0800661
662 if (anw != NULL) {
663 if (mConnectedWindow != NULL
664 && mConnectedWindow->getANativeWindow() == anw) {
665 return OK;
666 }
667 status_t err = native_window_api_connect(anw, NATIVE_WINDOW_API_MEDIA);
668
669 if (err != OK) {
670 ALOGE("setVideoSurfaceTexture failed: %d", err);
671 // Note that we must do the reset before disconnecting from the ANW.
672 // Otherwise queue/dequeue calls could be made on the disconnected
673 // ANW, which may result in errors.
674 mPlayer->reset();
675 disconnectNativeWindow_l();
676 return err;
677 }
678 }
679
680 // Note that we must set the player's new GraphicBufferProducer before
681 // disconnecting the old one. Otherwise queue/dequeue calls could be made
682 // on the disconnected ANW, which may result in errors.
683 status_t err = mPlayer->setVideoSurfaceTexture(nww);
684
685 disconnectNativeWindow_l();
686
687 if (err == OK) {
688 mConnectedWindow = nww;
689 mLock.unlock();
690 } else if (anw != NULL) {
691 mLock.unlock();
692 status_t err = native_window_api_disconnect(anw, NATIVE_WINDOW_API_MEDIA);
693
694 if (err != OK) {
695 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
696 strerror(-err), err);
697 }
698 }
699
700 return err;
Wei Jia53692fa2017-12-11 10:33:46 -0800701}
702
Wei Jiaec044b02018-02-19 12:41:23 -0800703status_t MediaPlayer2::getBufferingSettings(BufferingSettings* buffering /* nonnull */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800704 ALOGV("getBufferingSettings");
705
706 Mutex::Autolock _l(mLock);
707 if (mPlayer == 0) {
708 return NO_INIT;
709 }
Wei Jiaec044b02018-02-19 12:41:23 -0800710
711 status_t ret = mPlayer->getBufferingSettings(buffering);
712 if (ret == NO_ERROR) {
713 ALOGV("getBufferingSettings{%s}", buffering->toString().string());
714 } else {
715 ALOGE("getBufferingSettings returned %d", ret);
716 }
717 return ret;
Wei Jia53692fa2017-12-11 10:33:46 -0800718}
719
Wei Jiaec044b02018-02-19 12:41:23 -0800720status_t MediaPlayer2::setBufferingSettings(const BufferingSettings& buffering) {
721 ALOGV("setBufferingSettings{%s}", buffering.toString().string());
Wei Jia53692fa2017-12-11 10:33:46 -0800722
723 Mutex::Autolock _l(mLock);
724 if (mPlayer == 0) {
725 return NO_INIT;
726 }
727 return mPlayer->setBufferingSettings(buffering);
728}
729
Wei Jiaec044b02018-02-19 12:41:23 -0800730status_t MediaPlayer2::setAudioAttributes_l(const Parcel &parcel) {
731 if (mAudioAttributes != NULL) {
732 free(mAudioAttributes);
733 }
734 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
735 if (mAudioAttributes == NULL) {
736 return NO_MEMORY;
737 }
738 unmarshallAudioAttributes(parcel, mAudioAttributes);
739
740 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
741 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
742 mAudioAttributes->tags);
743
744 if (mAudioOutput != 0) {
745 mAudioOutput->setAudioAttributes(mAudioAttributes);
746 }
747 return NO_ERROR;
748}
749
750status_t MediaPlayer2::prepareAsync() {
751 ALOGV("prepareAsync");
752 Mutex::Autolock _l(mLock);
Wei Jia0151ef42018-08-24 16:40:21 -0700753 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER2_INITIALIZED)) {
Wei Jia53692fa2017-12-11 10:33:46 -0800754 if (mAudioAttributesParcel != NULL) {
Wei Jiaec044b02018-02-19 12:41:23 -0800755 status_t err = setAudioAttributes_l(*mAudioAttributesParcel);
756 if (err != OK) {
757 return err;
758 }
759 } else if (mAudioOutput != 0) {
760 mAudioOutput->setAudioStreamType(mStreamType);
Wei Jia53692fa2017-12-11 10:33:46 -0800761 }
762 mCurrentState = MEDIA_PLAYER2_PREPARING;
763 return mPlayer->prepareAsync();
764 }
765 ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
766 return INVALID_OPERATION;
767}
768
Wei Jiaec044b02018-02-19 12:41:23 -0800769status_t MediaPlayer2::start() {
Wei Jia53692fa2017-12-11 10:33:46 -0800770 ALOGV("start");
771
772 status_t ret = NO_ERROR;
773 Mutex::Autolock _l(mLock);
774
775 mLockThreadId = getThreadId();
776
777 if (mCurrentState & MEDIA_PLAYER2_STARTED) {
778 ret = NO_ERROR;
779 } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_PREPARED |
780 MEDIA_PLAYER2_PLAYBACK_COMPLETE | MEDIA_PLAYER2_PAUSED ) ) ) {
781 mPlayer->setLooping(mLoop);
Wei Jiaec044b02018-02-19 12:41:23 -0800782
783 if (mAudioOutput != 0) {
784 mAudioOutput->setVolume(mLeftVolume, mRightVolume);
785 }
786
787 if (mAudioOutput != 0) {
788 mAudioOutput->setAuxEffectSendLevel(mSendLevel);
789 }
Wei Jia53692fa2017-12-11 10:33:46 -0800790 mCurrentState = MEDIA_PLAYER2_STARTED;
791 ret = mPlayer->start();
792 if (ret != NO_ERROR) {
793 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
794 } else {
795 if (mCurrentState == MEDIA_PLAYER2_PLAYBACK_COMPLETE) {
796 ALOGV("playback completed immediately following start()");
797 }
798 }
799 } else {
800 ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
801 ret = INVALID_OPERATION;
802 }
803
804 mLockThreadId = 0;
805
806 return ret;
807}
808
Wei Jiaec044b02018-02-19 12:41:23 -0800809status_t MediaPlayer2::pause() {
Wei Jia53692fa2017-12-11 10:33:46 -0800810 ALOGV("pause");
811 Mutex::Autolock _l(mLock);
812 if (mCurrentState & (MEDIA_PLAYER2_PAUSED|MEDIA_PLAYER2_PLAYBACK_COMPLETE))
813 return NO_ERROR;
814 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER2_STARTED)) {
815 status_t ret = mPlayer->pause();
816 if (ret != NO_ERROR) {
817 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
818 } else {
819 mCurrentState = MEDIA_PLAYER2_PAUSED;
820 }
821 return ret;
822 }
823 ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
824 return INVALID_OPERATION;
825}
826
Wei Jiaec044b02018-02-19 12:41:23 -0800827bool MediaPlayer2::isPlaying() {
Wei Jia53692fa2017-12-11 10:33:46 -0800828 Mutex::Autolock _l(mLock);
829 if (mPlayer != 0) {
Wei Jiaec044b02018-02-19 12:41:23 -0800830 bool temp = mPlayer->isPlaying();
Wei Jia53692fa2017-12-11 10:33:46 -0800831 ALOGV("isPlaying: %d", temp);
832 if ((mCurrentState & MEDIA_PLAYER2_STARTED) && ! temp) {
833 ALOGE("internal/external state mismatch corrected");
834 mCurrentState = MEDIA_PLAYER2_PAUSED;
835 } else if ((mCurrentState & MEDIA_PLAYER2_PAUSED) && temp) {
836 ALOGE("internal/external state mismatch corrected");
837 mCurrentState = MEDIA_PLAYER2_STARTED;
838 }
839 return temp;
840 }
841 ALOGV("isPlaying: no active player");
842 return false;
843}
844
Wei Jia1f043e42018-06-20 16:52:50 -0700845mediaplayer2_states MediaPlayer2::getState() {
Wei Jia98787a72018-03-02 14:33:06 -0800846 Mutex::Autolock _l(mLock);
847 if (mCurrentState & MEDIA_PLAYER2_STATE_ERROR) {
848 return MEDIAPLAYER2_STATE_ERROR;
849 }
850 if (mPlayer == 0
851 || (mCurrentState &
852 (MEDIA_PLAYER2_IDLE | MEDIA_PLAYER2_INITIALIZED | MEDIA_PLAYER2_PREPARING))) {
853 return MEDIAPLAYER2_STATE_IDLE;
854 }
855 if (mCurrentState & MEDIA_PLAYER2_STARTED) {
856 return MEDIAPLAYER2_STATE_PLAYING;
857 }
Wei Jia0151ef42018-08-24 16:40:21 -0700858 if (mCurrentState & (MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE)) {
Wei Jia98787a72018-03-02 14:33:06 -0800859 return MEDIAPLAYER2_STATE_PAUSED;
860 }
861 // now only mCurrentState & MEDIA_PLAYER2_PREPARED is true
862 return MEDIAPLAYER2_STATE_PREPARED;
863}
864
Wei Jiaec044b02018-02-19 12:41:23 -0800865status_t MediaPlayer2::setPlaybackSettings(const AudioPlaybackRate& rate) {
Wei Jia53692fa2017-12-11 10:33:46 -0800866 ALOGV("setPlaybackSettings: %f %f %d %d",
867 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
868 // Negative speed and pitch does not make sense. Further validation will
869 // be done by the respective mediaplayers.
Wei Jiaec044b02018-02-19 12:41:23 -0800870 if (rate.mSpeed <= 0.f || rate.mPitch < 0.f) {
Wei Jia53692fa2017-12-11 10:33:46 -0800871 return BAD_VALUE;
872 }
873 Mutex::Autolock _l(mLock);
Wei Jia0151ef42018-08-24 16:40:21 -0700874 if (mPlayer == 0) {
Wei Jia53692fa2017-12-11 10:33:46 -0800875 return INVALID_OPERATION;
876 }
877
Wei Jia53692fa2017-12-11 10:33:46 -0800878 status_t err = mPlayer->setPlaybackSettings(rate);
Wei Jia53692fa2017-12-11 10:33:46 -0800879 return err;
880}
881
Wei Jiaec044b02018-02-19 12:41:23 -0800882status_t MediaPlayer2::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800883 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800884 if (mPlayer == 0) {
885 return INVALID_OPERATION;
886 }
Wei Jiaec044b02018-02-19 12:41:23 -0800887 status_t ret = mPlayer->getPlaybackSettings(rate);
888 if (ret == NO_ERROR) {
889 ALOGV("getPlaybackSettings(%f, %f, %d, %d)",
890 rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
891 } else {
892 ALOGV("getPlaybackSettings returned %d", ret);
893 }
894 return ret;
Wei Jia53692fa2017-12-11 10:33:46 -0800895}
896
Wei Jiaec044b02018-02-19 12:41:23 -0800897status_t MediaPlayer2::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint) {
Wei Jia53692fa2017-12-11 10:33:46 -0800898 ALOGV("setSyncSettings: %u %u %f %f",
899 sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
900 Mutex::Autolock _l(mLock);
901 if (mPlayer == 0) return INVALID_OPERATION;
902 return mPlayer->setSyncSettings(sync, videoFpsHint);
903}
904
905status_t MediaPlayer2::getSyncSettings(
Wei Jiaec044b02018-02-19 12:41:23 -0800906 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800907 Mutex::Autolock _l(mLock);
Wei Jia334adf02018-03-23 14:08:23 -0700908 if (mPlayer == 0) {
909 return INVALID_OPERATION;
910 }
Wei Jiaec044b02018-02-19 12:41:23 -0800911 status_t ret = mPlayer->getSyncSettings(sync, videoFps);
912 if (ret == NO_ERROR) {
913 ALOGV("getSyncSettings(%u, %u, %f, %f)",
914 sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
915 } else {
916 ALOGV("getSyncSettings returned %d", ret);
917 }
918 return ret;
919
Wei Jia53692fa2017-12-11 10:33:46 -0800920}
921
Wei Jiaec044b02018-02-19 12:41:23 -0800922status_t MediaPlayer2::getVideoWidth(int *w) {
Wei Jia53692fa2017-12-11 10:33:46 -0800923 ALOGV("getVideoWidth");
924 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800925 if (mPlayer == 0) {
926 return INVALID_OPERATION;
927 }
Wei Jia53692fa2017-12-11 10:33:46 -0800928 *w = mVideoWidth;
929 return NO_ERROR;
930}
931
Wei Jiaec044b02018-02-19 12:41:23 -0800932status_t MediaPlayer2::getVideoHeight(int *h) {
Wei Jia53692fa2017-12-11 10:33:46 -0800933 ALOGV("getVideoHeight");
934 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800935 if (mPlayer == 0) {
936 return INVALID_OPERATION;
937 }
Wei Jia53692fa2017-12-11 10:33:46 -0800938 *h = mVideoHeight;
939 return NO_ERROR;
940}
941
Wei Jia800fe372018-02-20 15:00:45 -0800942status_t MediaPlayer2::getCurrentPosition(int64_t *msec) {
Wei Jia53692fa2017-12-11 10:33:46 -0800943 ALOGV("getCurrentPosition");
944 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -0800945 if (mPlayer == 0) {
946 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -0800947 }
Wei Jiaec044b02018-02-19 12:41:23 -0800948 if (mCurrentPosition >= 0) {
Wei Jia800fe372018-02-20 15:00:45 -0800949 ALOGV("Using cached seek position: %lld", (long long)mCurrentPosition);
Wei Jiaec044b02018-02-19 12:41:23 -0800950 *msec = mCurrentPosition;
951 return NO_ERROR;
952 }
953 status_t ret = mPlayer->getCurrentPosition(msec);
954 if (ret == NO_ERROR) {
Wei Jia800fe372018-02-20 15:00:45 -0800955 ALOGV("getCurrentPosition = %lld", (long long)*msec);
Wei Jiaec044b02018-02-19 12:41:23 -0800956 } else {
957 ALOGE("getCurrentPosition returned %d", ret);
958 }
959 return ret;
Wei Jia53692fa2017-12-11 10:33:46 -0800960}
961
Wei Jia800fe372018-02-20 15:00:45 -0800962status_t MediaPlayer2::getDuration(int64_t *msec) {
Wei Jiaec044b02018-02-19 12:41:23 -0800963 Mutex::Autolock _l(mLock);
Wei Jia53692fa2017-12-11 10:33:46 -0800964 ALOGV("getDuration_l");
965 bool isValidState = (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
Wei Jia0151ef42018-08-24 16:40:21 -0700966 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE));
Wei Jiaec044b02018-02-19 12:41:23 -0800967 if (mPlayer == 0 || !isValidState) {
968 ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u",
969 mPlayer.get(), mCurrentState);
970 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -0800971 }
Wei Jia800fe372018-02-20 15:00:45 -0800972 int64_t durationMs;
Wei Jiaec044b02018-02-19 12:41:23 -0800973 status_t ret = mPlayer->getDuration(&durationMs);
974
975 if (ret == NO_ERROR) {
Wei Jia800fe372018-02-20 15:00:45 -0800976 ALOGV("getDuration = %lld", (long long)durationMs);
Wei Jiaec044b02018-02-19 12:41:23 -0800977 } else {
978 ALOGE("getDuration returned %d", ret);
979 // Do not enter error state just because no duration was available.
980 durationMs = -1;
981 }
982
983 if (msec) {
984 *msec = durationMs;
985 }
986 return OK;
Wei Jia53692fa2017-12-11 10:33:46 -0800987}
988
Wei Jia800fe372018-02-20 15:00:45 -0800989status_t MediaPlayer2::seekTo_l(int64_t msec, MediaPlayer2SeekMode mode) {
990 ALOGV("seekTo (%lld, %d)", (long long)msec, mode);
991 if ((mPlayer == 0) || !(mCurrentState & (MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED |
992 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) {
993 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u",
994 mPlayer.get(), mCurrentState);
995 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -0800996 }
Wei Jia800fe372018-02-20 15:00:45 -0800997 if (msec < 0) {
998 ALOGW("Attempt to seek to invalid position: %lld", (long long)msec);
999 msec = 0;
1000 }
1001
1002 int64_t durationMs;
1003 status_t err = mPlayer->getDuration(&durationMs);
1004
1005 if (err != OK) {
1006 ALOGW("Stream has no duration and is therefore not seekable.");
1007 return err;
1008 }
1009
1010 if (msec > durationMs) {
1011 ALOGW("Attempt to seek to past end of file: request = %lld, durationMs = %lld",
1012 (long long)msec, (long long)durationMs);
1013
1014 msec = durationMs;
1015 }
1016
1017 // cache duration
1018 mCurrentPosition = msec;
1019 mCurrentSeekMode = mode;
1020 if (mSeekPosition < 0) {
1021 mSeekPosition = msec;
1022 mSeekMode = mode;
1023 return mPlayer->seekTo(msec, mode);
1024 }
1025 ALOGV("Seek in progress - queue up seekTo[%lld, %d]", (long long)msec, mode);
1026 return NO_ERROR;
Wei Jia53692fa2017-12-11 10:33:46 -08001027}
1028
Wei Jia800fe372018-02-20 15:00:45 -08001029status_t MediaPlayer2::seekTo(int64_t msec, MediaPlayer2SeekMode mode) {
Wei Jia53692fa2017-12-11 10:33:46 -08001030 mLockThreadId = getThreadId();
1031 Mutex::Autolock _l(mLock);
1032 status_t result = seekTo_l(msec, mode);
1033 mLockThreadId = 0;
1034
1035 return result;
1036}
1037
Wei Jiaec044b02018-02-19 12:41:23 -08001038status_t MediaPlayer2::notifyAt(int64_t mediaTimeUs) {
Wei Jia53692fa2017-12-11 10:33:46 -08001039 Mutex::Autolock _l(mLock);
1040 if (mPlayer != 0) {
Wei Jiaec044b02018-02-19 12:41:23 -08001041 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -08001042 }
Wei Jiaec044b02018-02-19 12:41:23 -08001043
1044 return mPlayer->notifyAt(mediaTimeUs);
Wei Jia53692fa2017-12-11 10:33:46 -08001045}
1046
Wei Jiaec044b02018-02-19 12:41:23 -08001047status_t MediaPlayer2::reset_l() {
Wei Jia53692fa2017-12-11 10:33:46 -08001048 mLoop = false;
Wei Jiaec044b02018-02-19 12:41:23 -08001049 if (mCurrentState == MEDIA_PLAYER2_IDLE) {
1050 return NO_ERROR;
1051 }
Wei Jia53692fa2017-12-11 10:33:46 -08001052 if (mPlayer != 0) {
1053 status_t ret = mPlayer->reset();
1054 if (ret != NO_ERROR) {
1055 ALOGE("reset() failed with return code (%d)", ret);
1056 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
1057 } else {
Wei Jiaec044b02018-02-19 12:41:23 -08001058 mPlayer->setListener(NULL);
Wei Jia53692fa2017-12-11 10:33:46 -08001059 mCurrentState = MEDIA_PLAYER2_IDLE;
1060 }
1061 // setDataSource has to be called again to create a
1062 // new mediaplayer.
1063 mPlayer = 0;
1064 return ret;
1065 }
1066 clear_l();
1067 return NO_ERROR;
1068}
1069
Wei Jiaec044b02018-02-19 12:41:23 -08001070status_t MediaPlayer2::reset() {
Wei Jia53692fa2017-12-11 10:33:46 -08001071 ALOGV("reset");
1072 mLockThreadId = getThreadId();
1073 Mutex::Autolock _l(mLock);
1074 status_t result = reset_l();
1075 mLockThreadId = 0;
1076
1077 return result;
1078}
1079
Wei Jiaec044b02018-02-19 12:41:23 -08001080status_t MediaPlayer2::setAudioStreamType(audio_stream_type_t type) {
Wei Jia53692fa2017-12-11 10:33:46 -08001081 ALOGV("MediaPlayer2::setAudioStreamType");
1082 Mutex::Autolock _l(mLock);
1083 if (mStreamType == type) return NO_ERROR;
1084 if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
1085 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) {
1086 // Can't change the stream type after prepare
1087 ALOGE("setAudioStream called in state %d", mCurrentState);
1088 return INVALID_OPERATION;
1089 }
1090 // cache
1091 mStreamType = type;
1092 return OK;
1093}
1094
Wei Jiaec044b02018-02-19 12:41:23 -08001095status_t MediaPlayer2::getAudioStreamType(audio_stream_type_t *type) {
Wei Jia53692fa2017-12-11 10:33:46 -08001096 ALOGV("getAudioStreamType");
1097 Mutex::Autolock _l(mLock);
1098 *type = mStreamType;
1099 return OK;
1100}
1101
Wei Jiaec044b02018-02-19 12:41:23 -08001102status_t MediaPlayer2::setLooping(int loop) {
Wei Jia53692fa2017-12-11 10:33:46 -08001103 ALOGV("MediaPlayer2::setLooping");
1104 Mutex::Autolock _l(mLock);
1105 mLoop = (loop != 0);
1106 if (mPlayer != 0) {
1107 return mPlayer->setLooping(loop);
1108 }
1109 return OK;
1110}
1111
1112bool MediaPlayer2::isLooping() {
1113 ALOGV("isLooping");
1114 Mutex::Autolock _l(mLock);
1115 if (mPlayer != 0) {
1116 return mLoop;
1117 }
1118 ALOGV("isLooping: no active player");
1119 return false;
1120}
1121
Wei Jiaec044b02018-02-19 12:41:23 -08001122status_t MediaPlayer2::setVolume(float leftVolume, float rightVolume) {
Wei Jia53692fa2017-12-11 10:33:46 -08001123 ALOGV("MediaPlayer2::setVolume(%f, %f)", leftVolume, rightVolume);
1124 Mutex::Autolock _l(mLock);
1125 mLeftVolume = leftVolume;
1126 mRightVolume = rightVolume;
Wei Jiaec044b02018-02-19 12:41:23 -08001127 if (mAudioOutput != 0) {
1128 mAudioOutput->setVolume(leftVolume, rightVolume);
Wei Jia53692fa2017-12-11 10:33:46 -08001129 }
1130 return OK;
1131}
1132
Wei Jiaec044b02018-02-19 12:41:23 -08001133status_t MediaPlayer2::setAudioSessionId(audio_session_t sessionId) {
Wei Jia53692fa2017-12-11 10:33:46 -08001134 ALOGV("MediaPlayer2::setAudioSessionId(%d)", sessionId);
1135 Mutex::Autolock _l(mLock);
1136 if (!(mCurrentState & MEDIA_PLAYER2_IDLE)) {
1137 ALOGE("setAudioSessionId called in state %d", mCurrentState);
1138 return INVALID_OPERATION;
1139 }
1140 if (sessionId < 0) {
1141 return BAD_VALUE;
1142 }
1143 if (sessionId != mAudioSessionId) {
1144 AudioSystem::acquireAudioSessionId(sessionId, -1);
1145 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1);
1146 mAudioSessionId = sessionId;
1147 }
1148 return NO_ERROR;
1149}
1150
Wei Jiaec044b02018-02-19 12:41:23 -08001151audio_session_t MediaPlayer2::getAudioSessionId() {
Wei Jia53692fa2017-12-11 10:33:46 -08001152 Mutex::Autolock _l(mLock);
1153 return mAudioSessionId;
1154}
1155
Wei Jiaec044b02018-02-19 12:41:23 -08001156status_t MediaPlayer2::setAuxEffectSendLevel(float level) {
Wei Jia53692fa2017-12-11 10:33:46 -08001157 ALOGV("MediaPlayer2::setAuxEffectSendLevel(%f)", level);
1158 Mutex::Autolock _l(mLock);
1159 mSendLevel = level;
Wei Jiaec044b02018-02-19 12:41:23 -08001160 if (mAudioOutput != 0) {
1161 return mAudioOutput->setAuxEffectSendLevel(level);
Wei Jia53692fa2017-12-11 10:33:46 -08001162 }
1163 return OK;
1164}
1165
Wei Jiaec044b02018-02-19 12:41:23 -08001166status_t MediaPlayer2::attachAuxEffect(int effectId) {
Wei Jia53692fa2017-12-11 10:33:46 -08001167 ALOGV("MediaPlayer2::attachAuxEffect(%d)", effectId);
1168 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -08001169 if (mAudioOutput == 0 ||
Wei Jia53692fa2017-12-11 10:33:46 -08001170 (mCurrentState & MEDIA_PLAYER2_IDLE) ||
1171 (mCurrentState == MEDIA_PLAYER2_STATE_ERROR )) {
1172 ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
1173 return INVALID_OPERATION;
1174 }
1175
Wei Jiaec044b02018-02-19 12:41:23 -08001176 return mAudioOutput->attachAuxEffect(effectId);
Wei Jia53692fa2017-12-11 10:33:46 -08001177}
1178
1179// always call with lock held
Wei Jiaec044b02018-02-19 12:41:23 -08001180status_t MediaPlayer2::checkStateForKeySet_l(int key) {
Wei Jia53692fa2017-12-11 10:33:46 -08001181 switch(key) {
1182 case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES:
1183 if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED |
1184 MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE) ) {
1185 // Can't change the audio attributes after prepare
1186 ALOGE("trying to set audio attributes called in state %d", mCurrentState);
1187 return INVALID_OPERATION;
1188 }
1189 break;
1190 default:
1191 // parameter doesn't require player state check
1192 break;
1193 }
1194 return OK;
1195}
1196
Wei Jiaec044b02018-02-19 12:41:23 -08001197status_t MediaPlayer2::setParameter(int key, const Parcel& request) {
Wei Jia53692fa2017-12-11 10:33:46 -08001198 ALOGV("MediaPlayer2::setParameter(%d)", key);
1199 status_t status = INVALID_OPERATION;
1200 Mutex::Autolock _l(mLock);
1201 if (checkStateForKeySet_l(key) != OK) {
1202 return status;
1203 }
1204 switch (key) {
1205 case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES:
1206 // save the marshalled audio attributes
Wei Jiaec044b02018-02-19 12:41:23 -08001207 if (mAudioAttributesParcel != NULL) {
1208 delete mAudioAttributesParcel;
1209 }
Wei Jia53692fa2017-12-11 10:33:46 -08001210 mAudioAttributesParcel = new Parcel();
1211 mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize());
Wei Jiaec044b02018-02-19 12:41:23 -08001212 status = setAudioAttributes_l(request);
1213 if (status != OK) {
1214 return status;
1215 }
Wei Jia53692fa2017-12-11 10:33:46 -08001216 break;
1217 default:
1218 ALOGV_IF(mPlayer == NULL, "setParameter: no active player");
1219 break;
1220 }
1221
1222 if (mPlayer != NULL) {
1223 status = mPlayer->setParameter(key, request);
1224 }
1225 return status;
1226}
1227
Wei Jiaec044b02018-02-19 12:41:23 -08001228status_t MediaPlayer2::getParameter(int key, Parcel *reply) {
Wei Jia53692fa2017-12-11 10:33:46 -08001229 ALOGV("MediaPlayer2::getParameter(%d)", key);
1230 Mutex::Autolock _l(mLock);
Wei Jia800fe372018-02-20 15:00:45 -08001231 if (key == MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES) {
1232 if (reply == NULL) {
1233 return BAD_VALUE;
1234 }
1235 if (mAudioAttributesParcel != NULL) {
1236 reply->appendFrom(mAudioAttributesParcel, 0, mAudioAttributesParcel->dataSize());
1237 }
1238 return OK;
1239 }
1240
Wei Jiaec044b02018-02-19 12:41:23 -08001241 if (mPlayer == NULL) {
1242 ALOGV("getParameter: no active player");
1243 return INVALID_OPERATION;
Wei Jia53692fa2017-12-11 10:33:46 -08001244 }
Wei Jiaec044b02018-02-19 12:41:23 -08001245
1246 status_t status = mPlayer->getParameter(key, reply);
1247 if (status != OK) {
1248 ALOGD("getParameter returns %d", status);
1249 }
1250 return status;
Wei Jia53692fa2017-12-11 10:33:46 -08001251}
1252
Wei Jiaec044b02018-02-19 12:41:23 -08001253bool MediaPlayer2::shouldDropMetadata(media::Metadata::Type code) const {
1254 Mutex::Autolock lock(mLock);
1255
1256 if (findMetadata(mMetadataDrop, code)) {
1257 return true;
1258 }
1259
1260 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1261 return false;
1262 } else {
1263 return true;
1264 }
1265}
1266
1267
1268void MediaPlayer2::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1269 Mutex::Autolock lock(mLock);
1270 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1271 mMetadataUpdated.add(metadata_type);
1272 }
1273}
1274
Dongwon Kang41929fb2018-09-09 08:29:56 -07001275void MediaPlayer2::notify(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *obj) {
Wei Jiad2bb1bd2018-02-08 09:47:37 -08001276 ALOGV("message received srcId=%lld, msg=%d, ext1=%d, ext2=%d",
1277 (long long)srcId, msg, ext1, ext2);
Wei Jiaec044b02018-02-19 12:41:23 -08001278
1279 if (MEDIA2_INFO == msg && MEDIA2_INFO_METADATA_UPDATE == ext1) {
1280 const media::Metadata::Type metadata_type = ext2;
1281
1282 if(shouldDropMetadata(metadata_type)) {
1283 return;
1284 }
1285
1286 // Update the list of metadata that have changed. getMetadata
1287 // also access mMetadataUpdated and clears it.
1288 addNewMetadataUpdate(metadata_type);
1289 }
1290
Wei Jia53692fa2017-12-11 10:33:46 -08001291 bool send = true;
1292 bool locked = false;
1293
1294 // TODO: In the future, we might be on the same thread if the app is
1295 // running in the same process as the media server. In that case,
1296 // this will deadlock.
1297 //
1298 // The threadId hack below works around this for the care of prepare,
1299 // seekTo, start, and reset within the same process.
1300 // FIXME: Remember, this is a hack, it's not even a hack that is applied
1301 // consistently for all use-cases, this needs to be revisited.
1302 if (mLockThreadId != getThreadId()) {
1303 mLock.lock();
1304 locked = true;
1305 }
1306
1307 // Allows calls from JNI in idle state to notify errors
1308 if (!(msg == MEDIA2_ERROR && mCurrentState == MEDIA_PLAYER2_IDLE) && mPlayer == 0) {
Wei Jiad2bb1bd2018-02-08 09:47:37 -08001309 ALOGV("notify(%lld, %d, %d, %d) callback on disconnected mediaplayer",
1310 (long long)srcId, msg, ext1, ext2);
Wei Jia53692fa2017-12-11 10:33:46 -08001311 if (locked) mLock.unlock(); // release the lock when done.
1312 return;
1313 }
1314
1315 switch (msg) {
1316 case MEDIA2_NOP: // interface test message
1317 break;
1318 case MEDIA2_PREPARED:
1319 ALOGV("MediaPlayer2::notify() prepared");
1320 mCurrentState = MEDIA_PLAYER2_PREPARED;
Wei Jia53692fa2017-12-11 10:33:46 -08001321 break;
1322 case MEDIA2_DRM_INFO:
Wei Jiad2bb1bd2018-02-08 09:47:37 -08001323 ALOGV("MediaPlayer2::notify() MEDIA2_DRM_INFO(%lld, %d, %d, %d, %p)",
1324 (long long)srcId, msg, ext1, ext2, obj);
Wei Jia53692fa2017-12-11 10:33:46 -08001325 break;
1326 case MEDIA2_PLAYBACK_COMPLETE:
1327 ALOGV("playback complete");
1328 if (mCurrentState == MEDIA_PLAYER2_IDLE) {
1329 ALOGE("playback complete in idle state");
1330 }
1331 if (!mLoop) {
1332 mCurrentState = MEDIA_PLAYER2_PLAYBACK_COMPLETE;
1333 }
1334 break;
1335 case MEDIA2_ERROR:
1336 // Always log errors.
1337 // ext1: Media framework error code.
1338 // ext2: Implementation dependant error code.
1339 ALOGE("error (%d, %d)", ext1, ext2);
1340 mCurrentState = MEDIA_PLAYER2_STATE_ERROR;
Wei Jia53692fa2017-12-11 10:33:46 -08001341 break;
1342 case MEDIA2_INFO:
1343 // ext1: Media framework error code.
1344 // ext2: Implementation dependant error code.
1345 if (ext1 != MEDIA2_INFO_VIDEO_TRACK_LAGGING) {
1346 ALOGW("info/warning (%d, %d)", ext1, ext2);
1347 }
1348 break;
1349 case MEDIA2_SEEK_COMPLETE:
1350 ALOGV("Received seek complete");
1351 if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) {
Wei Jia800fe372018-02-20 15:00:45 -08001352 ALOGV("Executing queued seekTo(%lld, %d)",
1353 (long long)mCurrentPosition, mCurrentSeekMode);
Wei Jia53692fa2017-12-11 10:33:46 -08001354 mSeekPosition = -1;
1355 mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
1356 seekTo_l(mCurrentPosition, mCurrentSeekMode);
1357 }
1358 else {
1359 ALOGV("All seeks complete - return to regularly scheduled program");
1360 mCurrentPosition = mSeekPosition = -1;
1361 mCurrentSeekMode = mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC;
1362 }
1363 break;
1364 case MEDIA2_BUFFERING_UPDATE:
1365 ALOGV("buffering %d", ext1);
1366 break;
1367 case MEDIA2_SET_VIDEO_SIZE:
1368 ALOGV("New video size %d x %d", ext1, ext2);
1369 mVideoWidth = ext1;
1370 mVideoHeight = ext2;
1371 break;
1372 case MEDIA2_NOTIFY_TIME:
1373 ALOGV("Received notify time message");
1374 break;
1375 case MEDIA2_TIMED_TEXT:
1376 ALOGV("Received timed text message");
1377 break;
1378 case MEDIA2_SUBTITLE_DATA:
1379 ALOGV("Received subtitle data message");
1380 break;
1381 case MEDIA2_META_DATA:
1382 ALOGV("Received timed metadata message");
1383 break;
1384 default:
1385 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
1386 break;
1387 }
1388
1389 sp<MediaPlayer2Listener> listener = mListener;
1390 if (locked) mLock.unlock();
1391
1392 // this prevents re-entrant calls into client code
1393 if ((listener != 0) && send) {
1394 Mutex::Autolock _l(mNotifyLock);
1395 ALOGV("callback application");
Wei Jiad2bb1bd2018-02-08 09:47:37 -08001396 listener->notify(srcId, msg, ext1, ext2, obj);
Wei Jia53692fa2017-12-11 10:33:46 -08001397 ALOGV("back from callback");
1398 }
1399}
1400
Wei Jia53692fa2017-12-11 10:33:46 -08001401// Modular DRM
Wei Jiaec044b02018-02-19 12:41:23 -08001402status_t MediaPlayer2::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId) {
Wei Jia53692fa2017-12-11 10:33:46 -08001403 // TODO change to ALOGV
1404 ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid,
1405 drmSessionId.array(), drmSessionId.size());
1406 Mutex::Autolock _l(mLock);
1407 if (mPlayer == NULL) {
1408 return NO_INIT;
1409 }
1410
1411 // Only allowed it in player's preparing/prepared state.
1412 // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or
1413 // completed) so the state change to "prepared" might not have happened yet (e.g., buffering).
1414 // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener.
1415 if (!(mCurrentState & (MEDIA_PLAYER2_PREPARING | MEDIA_PLAYER2_PREPARED))) {
1416 ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState);
1417 return INVALID_OPERATION;
1418 }
1419
1420 if (drmSessionId.isEmpty()) {
1421 ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId.");
1422 return INVALID_OPERATION;
1423 }
1424
1425 // Passing down to mediaserver mainly for creating the crypto
1426 status_t status = mPlayer->prepareDrm(uuid, drmSessionId);
1427 ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status);
1428
1429 // TODO change to ALOGV
1430 ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status);
1431
1432 return status;
1433}
1434
Wei Jiaec044b02018-02-19 12:41:23 -08001435status_t MediaPlayer2::releaseDrm() {
Wei Jia53692fa2017-12-11 10:33:46 -08001436 Mutex::Autolock _l(mLock);
1437 if (mPlayer == NULL) {
1438 return NO_INIT;
1439 }
1440
1441 // Not allowing releaseDrm in an active/resumable state
1442 if (mCurrentState & (MEDIA_PLAYER2_STARTED |
1443 MEDIA_PLAYER2_PAUSED |
1444 MEDIA_PLAYER2_PLAYBACK_COMPLETE |
1445 MEDIA_PLAYER2_STATE_ERROR)) {
1446 ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState);
1447 return INVALID_OPERATION;
1448 }
1449
1450 status_t status = mPlayer->releaseDrm();
1451 // TODO change to ALOGV
1452 ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status);
1453 if (status != OK) {
1454 ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status);
1455 // Overriding to OK so the client proceed with its own cleanup
1456 // Client can't do more cleanup. mediaserver release its crypto at end of session anyway.
1457 status = OK;
1458 }
1459
1460 return status;
1461}
1462
Wei Jiaec044b02018-02-19 12:41:23 -08001463status_t MediaPlayer2::setOutputDevice(audio_port_handle_t deviceId) {
Wei Jia53692fa2017-12-11 10:33:46 -08001464 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -08001465 if (mAudioOutput == NULL) {
1466 ALOGV("setOutputDevice: audio sink not init");
Wei Jia53692fa2017-12-11 10:33:46 -08001467 return NO_INIT;
1468 }
Wei Jiaec044b02018-02-19 12:41:23 -08001469 return mAudioOutput->setOutputDevice(deviceId);
Wei Jia53692fa2017-12-11 10:33:46 -08001470}
1471
Wei Jiaec044b02018-02-19 12:41:23 -08001472audio_port_handle_t MediaPlayer2::getRoutedDeviceId() {
Wei Jia53692fa2017-12-11 10:33:46 -08001473 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -08001474 if (mAudioOutput == NULL) {
1475 ALOGV("getRoutedDeviceId: audio sink not init");
Wei Jia53692fa2017-12-11 10:33:46 -08001476 return AUDIO_PORT_HANDLE_NONE;
1477 }
1478 audio_port_handle_t deviceId;
Wei Jiaec044b02018-02-19 12:41:23 -08001479 status_t status = mAudioOutput->getRoutedDeviceId(&deviceId);
Wei Jia53692fa2017-12-11 10:33:46 -08001480 if (status != NO_ERROR) {
1481 return AUDIO_PORT_HANDLE_NONE;
1482 }
1483 return deviceId;
1484}
1485
Wei Jiaec044b02018-02-19 12:41:23 -08001486status_t MediaPlayer2::enableAudioDeviceCallback(bool enabled) {
Wei Jia53692fa2017-12-11 10:33:46 -08001487 Mutex::Autolock _l(mLock);
Wei Jiaec044b02018-02-19 12:41:23 -08001488 if (mAudioOutput == NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -08001489 ALOGV("addAudioDeviceCallback: player not init");
1490 return NO_INIT;
1491 }
Wei Jiaec044b02018-02-19 12:41:23 -08001492 return mAudioOutput->enableAudioDeviceCallback(enabled);
1493}
1494
1495status_t MediaPlayer2::dump(int fd, const Vector<String16>& args) {
1496 const size_t SIZE = 256;
1497 char buffer[SIZE];
1498 String8 result;
1499 result.append(" MediaPlayer2\n");
1500 snprintf(buffer, 255, " pid(%d), looping(%s)\n", mPid, mLoop?"true": "false");
1501 result.append(buffer);
1502
1503 sp<MediaPlayer2Interface> player;
1504 sp<MediaPlayer2AudioOutput> audioOutput;
1505 bool locked = false;
1506 for (int i = 0; i < kDumpLockRetries; ++i) {
1507 if (mLock.tryLock() == NO_ERROR) {
1508 locked = true;
1509 break;
1510 }
1511 usleep(kDumpLockSleepUs);
1512 }
1513
1514 if (locked) {
1515 player = mPlayer;
1516 audioOutput = mAudioOutput;
1517 mLock.unlock();
1518 } else {
1519 result.append(" lock is taken, no dump from player and audio output\n");
1520 }
1521 write(fd, result.string(), result.size());
1522
1523 if (player != NULL) {
1524 player->dump(fd, args);
1525 }
1526 if (audioOutput != 0) {
1527 audioOutput->dump(fd, args);
1528 }
1529 write(fd, "\n", 1);
1530 return NO_ERROR;
Wei Jia53692fa2017-12-11 10:33:46 -08001531}
1532
1533} // namespace android