Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/IPCThreadState.h> |
| 23 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 24 | #include <media/AudioSystem.h> |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 25 | #include <media/DataSourceDesc.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 26 | #include <media/MediaAnalyticsItem.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 27 | #include <media/MemoryLeakTrackUtil.h> |
| 28 | #include <media/Metadata.h> |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 29 | #include <media/NdkWrapper.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 30 | #include <media/stagefright/foundation/ADebug.h> |
| 31 | #include <media/stagefright/foundation/ALooperRoster.h> |
| 32 | #include <mediaplayer2/MediaPlayer2AudioOutput.h> |
Wei Jia | 51b6956 | 2018-02-05 16:17:13 -0800 | [diff] [blame] | 33 | #include <mediaplayer2/mediaplayer2.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 34 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 35 | #include <utils/Log.h> |
| 36 | #include <utils/SortedVector.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 37 | #include <utils/String8.h> |
| 38 | |
| 39 | #include <system/audio.h> |
| 40 | #include <system/window.h> |
| 41 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 42 | #include <nuplayer2/NuPlayer2Driver.h> |
| 43 | |
| 44 | #include <dirent.h> |
| 45 | #include <sys/stat.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 49 | extern ALooperRoster gLooperRoster; |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | const int kDumpLockRetries = 50; |
| 54 | const int kDumpLockSleepUs = 20000; |
| 55 | |
| 56 | // Max number of entries in the filter. |
| 57 | const 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. |
| 82 | bool 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. |
| 135 | bool 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 |
| 149 | const 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. |
| 172 | void 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 | |
| 198 | class AudioDeviceUpdatedNotifier: public AudioSystem::AudioDeviceCallback { |
| 199 | public: |
| 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 | |
| 215 | private: |
| 216 | wp<MediaPlayer2Interface> mListener; |
| 217 | }; |
| 218 | |
| 219 | class proxyListener : public MediaPlayer2InterfaceListener { |
| 220 | public: |
| 221 | proxyListener(const wp<MediaPlayer2> &player) |
| 222 | : mPlayer(player) { } |
| 223 | |
| 224 | ~proxyListener() { }; |
| 225 | |
| 226 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) override { |
| 227 | sp<MediaPlayer2> player = mPlayer.promote(); |
| 228 | if (player != NULL) { |
| 229 | player->notify(srcId, msg, ext1, ext2, obj); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | wp<MediaPlayer2> mPlayer; |
| 235 | }; |
| 236 | |
| 237 | Mutex sRecordLock; |
| 238 | SortedVector<wp<MediaPlayer2> > *sPlayers; |
| 239 | |
| 240 | void ensureInit_l() { |
| 241 | if (sPlayers == NULL) { |
| 242 | sPlayers = new SortedVector<wp<MediaPlayer2> >(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void addPlayer(const wp<MediaPlayer2>& player) { |
| 247 | Mutex::Autolock lock(sRecordLock); |
| 248 | ensureInit_l(); |
| 249 | sPlayers->add(player); |
| 250 | } |
| 251 | |
| 252 | void removePlayer(const wp<MediaPlayer2>& player) { |
| 253 | Mutex::Autolock lock(sRecordLock); |
| 254 | ensureInit_l(); |
| 255 | sPlayers->remove(player); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * The only arguments this understands right now are -c, -von and -voff, |
| 260 | * which are parsed by ALooperRoster::dump() |
| 261 | */ |
| 262 | status_t dumpPlayers(int fd, const Vector<String16>& args) { |
| 263 | const size_t SIZE = 256; |
| 264 | char buffer[SIZE]; |
| 265 | String8 result; |
| 266 | SortedVector< sp<MediaPlayer2> > players; //to serialise the mutex unlock & client destruction. |
| 267 | |
| 268 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 269 | snprintf(buffer, SIZE, "Permission Denial: can't dump MediaPlayer2\n"); |
| 270 | result.append(buffer); |
| 271 | } else { |
| 272 | { |
| 273 | Mutex::Autolock lock(sRecordLock); |
| 274 | ensureInit_l(); |
| 275 | for (int i = 0, n = sPlayers->size(); i < n; ++i) { |
| 276 | sp<MediaPlayer2> p = (*sPlayers)[i].promote(); |
| 277 | if (p != 0) { |
| 278 | p->dump(fd, args); |
| 279 | } |
| 280 | players.add(p); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | result.append(" Files opened and/or mapped:\n"); |
| 285 | snprintf(buffer, SIZE, "/proc/%d/maps", getpid()); |
| 286 | FILE *f = fopen(buffer, "r"); |
| 287 | if (f) { |
| 288 | while (!feof(f)) { |
| 289 | fgets(buffer, SIZE, f); |
| 290 | if (strstr(buffer, " /storage/") || |
| 291 | strstr(buffer, " /system/sounds/") || |
| 292 | strstr(buffer, " /data/") || |
| 293 | strstr(buffer, " /system/media/")) { |
| 294 | result.append(" "); |
| 295 | result.append(buffer); |
| 296 | } |
| 297 | } |
| 298 | fclose(f); |
| 299 | } else { |
| 300 | result.append("couldn't open "); |
| 301 | result.append(buffer); |
| 302 | result.append("\n"); |
| 303 | } |
| 304 | |
| 305 | snprintf(buffer, SIZE, "/proc/%d/fd", getpid()); |
| 306 | DIR *d = opendir(buffer); |
| 307 | if (d) { |
| 308 | struct dirent *ent; |
| 309 | while((ent = readdir(d)) != NULL) { |
| 310 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
| 311 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name); |
| 312 | struct stat s; |
| 313 | if (lstat(buffer, &s) == 0) { |
| 314 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 315 | char linkto[256]; |
| 316 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 317 | if(len > 0) { |
| 318 | if(len > 255) { |
| 319 | linkto[252] = '.'; |
| 320 | linkto[253] = '.'; |
| 321 | linkto[254] = '.'; |
| 322 | linkto[255] = 0; |
| 323 | } else { |
| 324 | linkto[len] = 0; |
| 325 | } |
| 326 | if (strstr(linkto, "/storage/") == linkto || |
| 327 | strstr(linkto, "/system/sounds/") == linkto || |
| 328 | strstr(linkto, "/data/") == linkto || |
| 329 | strstr(linkto, "/system/media/") == linkto) { |
| 330 | result.append(" "); |
| 331 | result.append(buffer); |
| 332 | result.append(" -> "); |
| 333 | result.append(linkto); |
| 334 | result.append("\n"); |
| 335 | } |
| 336 | } |
| 337 | } else { |
| 338 | result.append(" unexpected type for "); |
| 339 | result.append(buffer); |
| 340 | result.append("\n"); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | closedir(d); |
| 346 | } else { |
| 347 | result.append("couldn't open "); |
| 348 | result.append(buffer); |
| 349 | result.append("\n"); |
| 350 | } |
| 351 | |
| 352 | gLooperRoster.dump(fd, args); |
| 353 | |
| 354 | bool dumpMem = false; |
| 355 | bool unreachableMemory = false; |
| 356 | for (size_t i = 0; i < args.size(); i++) { |
| 357 | if (args[i] == String16("-m")) { |
| 358 | dumpMem = true; |
| 359 | } else if (args[i] == String16("--unreachable")) { |
| 360 | unreachableMemory = true; |
| 361 | } |
| 362 | } |
| 363 | if (dumpMem) { |
| 364 | result.append("\nDumping memory:\n"); |
| 365 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 366 | result.append(s.c_str(), s.size()); |
| 367 | } |
| 368 | if (unreachableMemory) { |
| 369 | result.append("\nDumping unreachable memory:\n"); |
| 370 | // TODO - should limit be an argument parameter? |
| 371 | // TODO: enable GetUnreachableMemoryString if it's part of stable API |
| 372 | //std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */); |
| 373 | //result.append(s.c_str(), s.size()); |
| 374 | } |
| 375 | } |
| 376 | write(fd, result.string(), result.size()); |
| 377 | return NO_ERROR; |
| 378 | } |
| 379 | |
| 380 | } // anonymous namespace |
| 381 | |
| 382 | //static |
| 383 | sp<MediaPlayer2> MediaPlayer2::Create() { |
| 384 | sp<MediaPlayer2> player = new MediaPlayer2(); |
| 385 | |
| 386 | if (!player->init()) { |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | ALOGV("Create new player(%p)", player.get()); |
| 391 | |
| 392 | addPlayer(player); |
| 393 | return player; |
| 394 | } |
| 395 | |
| 396 | // static |
| 397 | status_t MediaPlayer2::DumpAll(int fd, const Vector<String16>& args) { |
| 398 | return dumpPlayers(fd, args); |
| 399 | } |
| 400 | |
| 401 | MediaPlayer2::MediaPlayer2() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 402 | ALOGV("constructor"); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 403 | mSrcId = 0; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 404 | mLockThreadId = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 405 | mListener = NULL; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 406 | mStreamType = AUDIO_STREAM_MUSIC; |
| 407 | mAudioAttributesParcel = NULL; |
| 408 | mCurrentPosition = -1; |
| 409 | mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 410 | mSeekPosition = -1; |
| 411 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 412 | mCurrentState = MEDIA_PLAYER2_IDLE; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 413 | mLoop = false; |
| 414 | mLeftVolume = mRightVolume = 1.0; |
| 415 | mVideoWidth = mVideoHeight = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 416 | mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 417 | AudioSystem::acquireAudioSessionId(mAudioSessionId, -1); |
| 418 | mSendLevel = 0; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 419 | |
| 420 | // TODO: get pid and uid from JAVA |
| 421 | mPid = IPCThreadState::self()->getCallingPid(); |
| 422 | mUid = IPCThreadState::self()->getCallingUid(); |
| 423 | |
| 424 | mAudioAttributes = NULL; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 427 | MediaPlayer2::~MediaPlayer2() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 428 | ALOGV("destructor"); |
| 429 | if (mAudioAttributesParcel != NULL) { |
| 430 | delete mAudioAttributesParcel; |
| 431 | mAudioAttributesParcel = NULL; |
| 432 | } |
| 433 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
| 434 | disconnect(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 435 | removePlayer(this); |
| 436 | if (mAudioAttributes != NULL) { |
| 437 | free(mAudioAttributes); |
| 438 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 441 | bool MediaPlayer2::init() { |
| 442 | // TODO: after merge with NuPlayer2Driver, MediaPlayer2 will have its own |
| 443 | // looper for notification. |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | void MediaPlayer2::disconnect() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 448 | ALOGV("disconnect"); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 449 | sp<MediaPlayer2Interface> p; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 450 | { |
| 451 | Mutex::Autolock _l(mLock); |
| 452 | p = mPlayer; |
| 453 | mPlayer.clear(); |
| 454 | } |
| 455 | |
| 456 | if (p != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 457 | p->setListener(NULL); |
| 458 | p->reset(); |
| 459 | } |
| 460 | |
| 461 | { |
| 462 | Mutex::Autolock _l(mLock); |
| 463 | disconnectNativeWindow_l(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 467 | void MediaPlayer2::clear_l() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 468 | mCurrentPosition = -1; |
| 469 | mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 470 | mSeekPosition = -1; |
| 471 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 472 | mVideoWidth = mVideoHeight = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 475 | status_t MediaPlayer2::setListener(const sp<MediaPlayer2Listener>& listener) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 476 | ALOGV("setListener"); |
| 477 | Mutex::Autolock _l(mLock); |
| 478 | mListener = listener; |
| 479 | return NO_ERROR; |
| 480 | } |
| 481 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 482 | status_t MediaPlayer2::getSrcId(int64_t *srcId) { |
| 483 | if (srcId == NULL) { |
| 484 | return BAD_VALUE; |
| 485 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 486 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 487 | Mutex::Autolock _l(mLock); |
| 488 | *srcId = mSrcId; |
| 489 | return OK; |
| 490 | } |
| 491 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 492 | status_t MediaPlayer2::setDataSource(const sp<DataSourceDesc> &dsd) { |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 493 | if (dsd == NULL) { |
| 494 | return BAD_VALUE; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 495 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 496 | ALOGV("setDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId); |
| 497 | |
| 498 | sp<MediaPlayer2Interface> oldPlayer; |
| 499 | |
| 500 | Mutex::Autolock _l(mLock); |
| 501 | { |
| 502 | if (!((mCurrentState & MEDIA_PLAYER2_IDLE) |
| 503 | || mCurrentState == MEDIA_PLAYER2_STATE_ERROR)) { |
| 504 | ALOGE("setDataSource called in wrong state %d", mCurrentState); |
| 505 | return INVALID_OPERATION; |
| 506 | } |
| 507 | |
| 508 | sp<MediaPlayer2Interface> player = new NuPlayer2Driver(mPid, mUid); |
| 509 | status_t err = player->initCheck(); |
| 510 | if (err != NO_ERROR) { |
| 511 | ALOGE("Failed to create player object, initCheck failed(%d)", err); |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | clear_l(); |
| 516 | |
| 517 | player->setListener(new proxyListener(this)); |
| 518 | mAudioOutput = new MediaPlayer2AudioOutput(mAudioSessionId, mUid, |
| 519 | mPid, mAudioAttributes, new AudioDeviceUpdatedNotifier(player)); |
| 520 | player->setAudioSink(mAudioOutput); |
| 521 | |
| 522 | err = player->setDataSource(dsd); |
| 523 | if (err != OK) { |
| 524 | ALOGE("setDataSource error: %d", err); |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | sp<MediaPlayer2Interface> oldPlayer = mPlayer; |
| 529 | mPlayer = player; |
| 530 | mSrcId = dsd->mId; |
| 531 | mCurrentState = MEDIA_PLAYER2_INITIALIZED; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 532 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 533 | |
| 534 | if (oldPlayer != NULL) { |
| 535 | oldPlayer->setListener(NULL); |
| 536 | oldPlayer->reset(); |
| 537 | } |
| 538 | |
| 539 | return OK; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 542 | status_t MediaPlayer2::prepareNextDataSource(const sp<DataSourceDesc> &dsd) { |
| 543 | if (dsd == NULL) { |
| 544 | return BAD_VALUE; |
| 545 | } |
| 546 | ALOGV("prepareNextDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId); |
| 547 | |
| 548 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 549 | if (mPlayer == NULL) { |
| 550 | ALOGE("prepareNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 551 | return INVALID_OPERATION; |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 552 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 553 | return mPlayer->prepareNextDataSource(dsd); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | status_t MediaPlayer2::playNextDataSource(int64_t srcId) { |
| 557 | ALOGV("playNextDataSource srcId(%lld)", (long long)srcId); |
| 558 | |
| 559 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 560 | if (mPlayer == NULL) { |
| 561 | ALOGE("playNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 562 | return INVALID_OPERATION; |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 563 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 564 | mSrcId = srcId; |
| 565 | return mPlayer->playNextDataSource(srcId); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 568 | status_t MediaPlayer2::invoke(const Parcel& request, Parcel *reply) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 569 | Mutex::Autolock _l(mLock); |
| 570 | const bool hasBeenInitialized = |
| 571 | (mCurrentState != MEDIA_PLAYER2_STATE_ERROR) && |
| 572 | ((mCurrentState & MEDIA_PLAYER2_IDLE) != MEDIA_PLAYER2_IDLE); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 573 | if ((mPlayer == NULL) || !hasBeenInitialized) { |
| 574 | ALOGE("invoke failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 575 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 576 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 577 | ALOGV("invoke %zu", request.dataSize()); |
| 578 | return mPlayer->invoke(request, reply); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 581 | // This call doesn't need to access the native player. |
| 582 | status_t MediaPlayer2::setMetadataFilter(const Parcel& filter) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 583 | ALOGD("setMetadataFilter"); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 584 | |
| 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 Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 594 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 595 | return status; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 598 | status_t MediaPlayer2::getMetadata(bool update_only, bool /* apply_filter */, Parcel *reply) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 599 | ALOGD("getMetadata"); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 600 | sp<MediaPlayer2Interface> player; |
| 601 | media::Metadata::Filter ids; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 602 | Mutex::Autolock lock(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 603 | { |
| 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 | |
| 641 | void 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 | |
| 654 | status_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 Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 659 | return NO_INIT; |
| 660 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 661 | |
| 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 Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 703 | status_t MediaPlayer2::getBufferingSettings(BufferingSettings* buffering /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 704 | ALOGV("getBufferingSettings"); |
| 705 | |
| 706 | Mutex::Autolock _l(mLock); |
| 707 | if (mPlayer == 0) { |
| 708 | return NO_INIT; |
| 709 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 710 | |
| 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 Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 718 | } |
| 719 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 720 | status_t MediaPlayer2::setBufferingSettings(const BufferingSettings& buffering) { |
| 721 | ALOGV("setBufferingSettings{%s}", buffering.toString().string()); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 722 | |
| 723 | Mutex::Autolock _l(mLock); |
| 724 | if (mPlayer == 0) { |
| 725 | return NO_INIT; |
| 726 | } |
| 727 | return mPlayer->setBufferingSettings(buffering); |
| 728 | } |
| 729 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 730 | status_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 | |
| 750 | status_t MediaPlayer2::prepareAsync() { |
| 751 | ALOGV("prepareAsync"); |
| 752 | Mutex::Autolock _l(mLock); |
| 753 | if ((mPlayer != 0) && (mCurrentState & (MEDIA_PLAYER2_INITIALIZED | MEDIA_PLAYER2_STOPPED))) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 754 | if (mAudioAttributesParcel != NULL) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 755 | status_t err = setAudioAttributes_l(*mAudioAttributesParcel); |
| 756 | if (err != OK) { |
| 757 | return err; |
| 758 | } |
| 759 | } else if (mAudioOutput != 0) { |
| 760 | mAudioOutput->setAudioStreamType(mStreamType); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 761 | } |
| 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 Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 769 | status_t MediaPlayer2::start() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 770 | 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 Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 782 | |
| 783 | if (mAudioOutput != 0) { |
| 784 | mAudioOutput->setVolume(mLeftVolume, mRightVolume); |
| 785 | } |
| 786 | |
| 787 | if (mAudioOutput != 0) { |
| 788 | mAudioOutput->setAuxEffectSendLevel(mSendLevel); |
| 789 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 790 | 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 Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 809 | status_t MediaPlayer2::stop() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 810 | ALOGV("stop"); |
| 811 | Mutex::Autolock _l(mLock); |
| 812 | if (mCurrentState & MEDIA_PLAYER2_STOPPED) return NO_ERROR; |
| 813 | if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED | |
| 814 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) ) { |
| 815 | status_t ret = mPlayer->stop(); |
| 816 | if (ret != NO_ERROR) { |
| 817 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 818 | } else { |
| 819 | mCurrentState = MEDIA_PLAYER2_STOPPED; |
| 820 | } |
| 821 | return ret; |
| 822 | } |
| 823 | ALOGE("stop called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 824 | return INVALID_OPERATION; |
| 825 | } |
| 826 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 827 | status_t MediaPlayer2::pause() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 828 | ALOGV("pause"); |
| 829 | Mutex::Autolock _l(mLock); |
| 830 | if (mCurrentState & (MEDIA_PLAYER2_PAUSED|MEDIA_PLAYER2_PLAYBACK_COMPLETE)) |
| 831 | return NO_ERROR; |
| 832 | if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER2_STARTED)) { |
| 833 | status_t ret = mPlayer->pause(); |
| 834 | if (ret != NO_ERROR) { |
| 835 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 836 | } else { |
| 837 | mCurrentState = MEDIA_PLAYER2_PAUSED; |
| 838 | } |
| 839 | return ret; |
| 840 | } |
| 841 | ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 842 | return INVALID_OPERATION; |
| 843 | } |
| 844 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 845 | bool MediaPlayer2::isPlaying() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 846 | Mutex::Autolock _l(mLock); |
| 847 | if (mPlayer != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 848 | bool temp = mPlayer->isPlaying(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 849 | ALOGV("isPlaying: %d", temp); |
| 850 | if ((mCurrentState & MEDIA_PLAYER2_STARTED) && ! temp) { |
| 851 | ALOGE("internal/external state mismatch corrected"); |
| 852 | mCurrentState = MEDIA_PLAYER2_PAUSED; |
| 853 | } else if ((mCurrentState & MEDIA_PLAYER2_PAUSED) && temp) { |
| 854 | ALOGE("internal/external state mismatch corrected"); |
| 855 | mCurrentState = MEDIA_PLAYER2_STARTED; |
| 856 | } |
| 857 | return temp; |
| 858 | } |
| 859 | ALOGV("isPlaying: no active player"); |
| 860 | return false; |
| 861 | } |
| 862 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 863 | status_t MediaPlayer2::setPlaybackSettings(const AudioPlaybackRate& rate) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 864 | ALOGV("setPlaybackSettings: %f %f %d %d", |
| 865 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
| 866 | // Negative speed and pitch does not make sense. Further validation will |
| 867 | // be done by the respective mediaplayers. |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 868 | if (rate.mSpeed <= 0.f || rate.mPitch < 0.f) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 869 | return BAD_VALUE; |
| 870 | } |
| 871 | Mutex::Autolock _l(mLock); |
| 872 | if (mPlayer == 0 || (mCurrentState & MEDIA_PLAYER2_STOPPED)) { |
| 873 | return INVALID_OPERATION; |
| 874 | } |
| 875 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 876 | status_t err = mPlayer->setPlaybackSettings(rate); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 877 | return err; |
| 878 | } |
| 879 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 880 | status_t MediaPlayer2::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 881 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 882 | if (mPlayer == 0) { |
| 883 | return INVALID_OPERATION; |
| 884 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 885 | return mPlayer->getPlaybackSettings(rate); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 886 | status_t ret = mPlayer->getPlaybackSettings(rate); |
| 887 | if (ret == NO_ERROR) { |
| 888 | ALOGV("getPlaybackSettings(%f, %f, %d, %d)", |
| 889 | rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); |
| 890 | } else { |
| 891 | ALOGV("getPlaybackSettings returned %d", ret); |
| 892 | } |
| 893 | return ret; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 894 | } |
| 895 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 896 | status_t MediaPlayer2::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 897 | ALOGV("setSyncSettings: %u %u %f %f", |
| 898 | sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 899 | Mutex::Autolock _l(mLock); |
| 900 | if (mPlayer == 0) return INVALID_OPERATION; |
| 901 | return mPlayer->setSyncSettings(sync, videoFpsHint); |
| 902 | } |
| 903 | |
| 904 | status_t MediaPlayer2::getSyncSettings( |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 905 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 906 | Mutex::Autolock _l(mLock); |
| 907 | if (mPlayer == 0) return INVALID_OPERATION; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 908 | status_t ret = mPlayer->getSyncSettings(sync, videoFps); |
| 909 | if (ret == NO_ERROR) { |
| 910 | ALOGV("getSyncSettings(%u, %u, %f, %f)", |
| 911 | sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); |
| 912 | } else { |
| 913 | ALOGV("getSyncSettings returned %d", ret); |
| 914 | } |
| 915 | return ret; |
| 916 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 919 | status_t MediaPlayer2::getVideoWidth(int *w) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 920 | ALOGV("getVideoWidth"); |
| 921 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 922 | if (mPlayer == 0) { |
| 923 | return INVALID_OPERATION; |
| 924 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 925 | *w = mVideoWidth; |
| 926 | return NO_ERROR; |
| 927 | } |
| 928 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 929 | status_t MediaPlayer2::getVideoHeight(int *h) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 930 | ALOGV("getVideoHeight"); |
| 931 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 932 | if (mPlayer == 0) { |
| 933 | return INVALID_OPERATION; |
| 934 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 935 | *h = mVideoHeight; |
| 936 | return NO_ERROR; |
| 937 | } |
| 938 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 939 | status_t MediaPlayer2::getCurrentPosition(int64_t *msec) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 940 | ALOGV("getCurrentPosition"); |
| 941 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 942 | if (mPlayer == 0) { |
| 943 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 944 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 945 | if (mCurrentPosition >= 0) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 946 | ALOGV("Using cached seek position: %lld", (long long)mCurrentPosition); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 947 | *msec = mCurrentPosition; |
| 948 | return NO_ERROR; |
| 949 | } |
| 950 | status_t ret = mPlayer->getCurrentPosition(msec); |
| 951 | if (ret == NO_ERROR) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 952 | ALOGV("getCurrentPosition = %lld", (long long)*msec); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 953 | } else { |
| 954 | ALOGE("getCurrentPosition returned %d", ret); |
| 955 | } |
| 956 | return ret; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 959 | status_t MediaPlayer2::getDuration(int64_t *msec) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 960 | Mutex::Autolock _l(mLock); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 961 | ALOGV("getDuration_l"); |
| 962 | bool isValidState = (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
| 963 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_STOPPED | MEDIA_PLAYER2_PLAYBACK_COMPLETE)); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 964 | if (mPlayer == 0 || !isValidState) { |
| 965 | ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u", |
| 966 | mPlayer.get(), mCurrentState); |
| 967 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 968 | } |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 969 | int64_t durationMs; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 970 | status_t ret = mPlayer->getDuration(&durationMs); |
| 971 | |
| 972 | if (ret == NO_ERROR) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 973 | ALOGV("getDuration = %lld", (long long)durationMs); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 974 | } else { |
| 975 | ALOGE("getDuration returned %d", ret); |
| 976 | // Do not enter error state just because no duration was available. |
| 977 | durationMs = -1; |
| 978 | } |
| 979 | |
| 980 | if (msec) { |
| 981 | *msec = durationMs; |
| 982 | } |
| 983 | return OK; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 986 | status_t MediaPlayer2::seekTo_l(int64_t msec, MediaPlayer2SeekMode mode) { |
| 987 | ALOGV("seekTo (%lld, %d)", (long long)msec, mode); |
| 988 | if ((mPlayer == 0) || !(mCurrentState & (MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED | |
| 989 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) { |
| 990 | ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", |
| 991 | mPlayer.get(), mCurrentState); |
| 992 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 993 | } |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 994 | if (msec < 0) { |
| 995 | ALOGW("Attempt to seek to invalid position: %lld", (long long)msec); |
| 996 | msec = 0; |
| 997 | } |
| 998 | |
| 999 | int64_t durationMs; |
| 1000 | status_t err = mPlayer->getDuration(&durationMs); |
| 1001 | |
| 1002 | if (err != OK) { |
| 1003 | ALOGW("Stream has no duration and is therefore not seekable."); |
| 1004 | return err; |
| 1005 | } |
| 1006 | |
| 1007 | if (msec > durationMs) { |
| 1008 | ALOGW("Attempt to seek to past end of file: request = %lld, durationMs = %lld", |
| 1009 | (long long)msec, (long long)durationMs); |
| 1010 | |
| 1011 | msec = durationMs; |
| 1012 | } |
| 1013 | |
| 1014 | // cache duration |
| 1015 | mCurrentPosition = msec; |
| 1016 | mCurrentSeekMode = mode; |
| 1017 | if (mSeekPosition < 0) { |
| 1018 | mSeekPosition = msec; |
| 1019 | mSeekMode = mode; |
| 1020 | return mPlayer->seekTo(msec, mode); |
| 1021 | } |
| 1022 | ALOGV("Seek in progress - queue up seekTo[%lld, %d]", (long long)msec, mode); |
| 1023 | return NO_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 1026 | status_t MediaPlayer2::seekTo(int64_t msec, MediaPlayer2SeekMode mode) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1027 | mLockThreadId = getThreadId(); |
| 1028 | Mutex::Autolock _l(mLock); |
| 1029 | status_t result = seekTo_l(msec, mode); |
| 1030 | mLockThreadId = 0; |
| 1031 | |
| 1032 | return result; |
| 1033 | } |
| 1034 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1035 | status_t MediaPlayer2::notifyAt(int64_t mediaTimeUs) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1036 | Mutex::Autolock _l(mLock); |
| 1037 | if (mPlayer != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1038 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1039 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1040 | |
| 1041 | return mPlayer->notifyAt(mediaTimeUs); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1044 | status_t MediaPlayer2::reset_l() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1045 | mLoop = false; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1046 | if (mCurrentState == MEDIA_PLAYER2_IDLE) { |
| 1047 | return NO_ERROR; |
| 1048 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1049 | if (mPlayer != 0) { |
| 1050 | status_t ret = mPlayer->reset(); |
| 1051 | if (ret != NO_ERROR) { |
| 1052 | ALOGE("reset() failed with return code (%d)", ret); |
| 1053 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 1054 | } else { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1055 | mPlayer->setListener(NULL); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1056 | mCurrentState = MEDIA_PLAYER2_IDLE; |
| 1057 | } |
| 1058 | // setDataSource has to be called again to create a |
| 1059 | // new mediaplayer. |
| 1060 | mPlayer = 0; |
| 1061 | return ret; |
| 1062 | } |
| 1063 | clear_l(); |
| 1064 | return NO_ERROR; |
| 1065 | } |
| 1066 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1067 | status_t MediaPlayer2::reset() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1068 | ALOGV("reset"); |
| 1069 | mLockThreadId = getThreadId(); |
| 1070 | Mutex::Autolock _l(mLock); |
| 1071 | status_t result = reset_l(); |
| 1072 | mLockThreadId = 0; |
| 1073 | |
| 1074 | return result; |
| 1075 | } |
| 1076 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1077 | status_t MediaPlayer2::setAudioStreamType(audio_stream_type_t type) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1078 | ALOGV("MediaPlayer2::setAudioStreamType"); |
| 1079 | Mutex::Autolock _l(mLock); |
| 1080 | if (mStreamType == type) return NO_ERROR; |
| 1081 | if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
| 1082 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) { |
| 1083 | // Can't change the stream type after prepare |
| 1084 | ALOGE("setAudioStream called in state %d", mCurrentState); |
| 1085 | return INVALID_OPERATION; |
| 1086 | } |
| 1087 | // cache |
| 1088 | mStreamType = type; |
| 1089 | return OK; |
| 1090 | } |
| 1091 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1092 | status_t MediaPlayer2::getAudioStreamType(audio_stream_type_t *type) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1093 | ALOGV("getAudioStreamType"); |
| 1094 | Mutex::Autolock _l(mLock); |
| 1095 | *type = mStreamType; |
| 1096 | return OK; |
| 1097 | } |
| 1098 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1099 | status_t MediaPlayer2::setLooping(int loop) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1100 | ALOGV("MediaPlayer2::setLooping"); |
| 1101 | Mutex::Autolock _l(mLock); |
| 1102 | mLoop = (loop != 0); |
| 1103 | if (mPlayer != 0) { |
| 1104 | return mPlayer->setLooping(loop); |
| 1105 | } |
| 1106 | return OK; |
| 1107 | } |
| 1108 | |
| 1109 | bool MediaPlayer2::isLooping() { |
| 1110 | ALOGV("isLooping"); |
| 1111 | Mutex::Autolock _l(mLock); |
| 1112 | if (mPlayer != 0) { |
| 1113 | return mLoop; |
| 1114 | } |
| 1115 | ALOGV("isLooping: no active player"); |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1119 | status_t MediaPlayer2::setVolume(float leftVolume, float rightVolume) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1120 | ALOGV("MediaPlayer2::setVolume(%f, %f)", leftVolume, rightVolume); |
| 1121 | Mutex::Autolock _l(mLock); |
| 1122 | mLeftVolume = leftVolume; |
| 1123 | mRightVolume = rightVolume; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1124 | if (mAudioOutput != 0) { |
| 1125 | mAudioOutput->setVolume(leftVolume, rightVolume); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1126 | } |
| 1127 | return OK; |
| 1128 | } |
| 1129 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1130 | status_t MediaPlayer2::setAudioSessionId(audio_session_t sessionId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1131 | ALOGV("MediaPlayer2::setAudioSessionId(%d)", sessionId); |
| 1132 | Mutex::Autolock _l(mLock); |
| 1133 | if (!(mCurrentState & MEDIA_PLAYER2_IDLE)) { |
| 1134 | ALOGE("setAudioSessionId called in state %d", mCurrentState); |
| 1135 | return INVALID_OPERATION; |
| 1136 | } |
| 1137 | if (sessionId < 0) { |
| 1138 | return BAD_VALUE; |
| 1139 | } |
| 1140 | if (sessionId != mAudioSessionId) { |
| 1141 | AudioSystem::acquireAudioSessionId(sessionId, -1); |
| 1142 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
| 1143 | mAudioSessionId = sessionId; |
| 1144 | } |
| 1145 | return NO_ERROR; |
| 1146 | } |
| 1147 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1148 | audio_session_t MediaPlayer2::getAudioSessionId() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1149 | Mutex::Autolock _l(mLock); |
| 1150 | return mAudioSessionId; |
| 1151 | } |
| 1152 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1153 | status_t MediaPlayer2::setAuxEffectSendLevel(float level) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1154 | ALOGV("MediaPlayer2::setAuxEffectSendLevel(%f)", level); |
| 1155 | Mutex::Autolock _l(mLock); |
| 1156 | mSendLevel = level; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1157 | if (mAudioOutput != 0) { |
| 1158 | return mAudioOutput->setAuxEffectSendLevel(level); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1159 | } |
| 1160 | return OK; |
| 1161 | } |
| 1162 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1163 | status_t MediaPlayer2::attachAuxEffect(int effectId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1164 | ALOGV("MediaPlayer2::attachAuxEffect(%d)", effectId); |
| 1165 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1166 | if (mAudioOutput == 0 || |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1167 | (mCurrentState & MEDIA_PLAYER2_IDLE) || |
| 1168 | (mCurrentState == MEDIA_PLAYER2_STATE_ERROR )) { |
| 1169 | ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 1170 | return INVALID_OPERATION; |
| 1171 | } |
| 1172 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1173 | return mAudioOutput->attachAuxEffect(effectId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | // always call with lock held |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1177 | status_t MediaPlayer2::checkStateForKeySet_l(int key) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1178 | switch(key) { |
| 1179 | case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1180 | if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
| 1181 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE) ) { |
| 1182 | // Can't change the audio attributes after prepare |
| 1183 | ALOGE("trying to set audio attributes called in state %d", mCurrentState); |
| 1184 | return INVALID_OPERATION; |
| 1185 | } |
| 1186 | break; |
| 1187 | default: |
| 1188 | // parameter doesn't require player state check |
| 1189 | break; |
| 1190 | } |
| 1191 | return OK; |
| 1192 | } |
| 1193 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1194 | status_t MediaPlayer2::setParameter(int key, const Parcel& request) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1195 | ALOGV("MediaPlayer2::setParameter(%d)", key); |
| 1196 | status_t status = INVALID_OPERATION; |
| 1197 | Mutex::Autolock _l(mLock); |
| 1198 | if (checkStateForKeySet_l(key) != OK) { |
| 1199 | return status; |
| 1200 | } |
| 1201 | switch (key) { |
| 1202 | case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1203 | // save the marshalled audio attributes |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1204 | if (mAudioAttributesParcel != NULL) { |
| 1205 | delete mAudioAttributesParcel; |
| 1206 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1207 | mAudioAttributesParcel = new Parcel(); |
| 1208 | mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize()); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1209 | status = setAudioAttributes_l(request); |
| 1210 | if (status != OK) { |
| 1211 | return status; |
| 1212 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1213 | break; |
| 1214 | default: |
| 1215 | ALOGV_IF(mPlayer == NULL, "setParameter: no active player"); |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | if (mPlayer != NULL) { |
| 1220 | status = mPlayer->setParameter(key, request); |
| 1221 | } |
| 1222 | return status; |
| 1223 | } |
| 1224 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1225 | status_t MediaPlayer2::getParameter(int key, Parcel *reply) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1226 | ALOGV("MediaPlayer2::getParameter(%d)", key); |
| 1227 | Mutex::Autolock _l(mLock); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 1228 | if (key == MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES) { |
| 1229 | if (reply == NULL) { |
| 1230 | return BAD_VALUE; |
| 1231 | } |
| 1232 | if (mAudioAttributesParcel != NULL) { |
| 1233 | reply->appendFrom(mAudioAttributesParcel, 0, mAudioAttributesParcel->dataSize()); |
| 1234 | } |
| 1235 | return OK; |
| 1236 | } |
| 1237 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1238 | if (mPlayer == NULL) { |
| 1239 | ALOGV("getParameter: no active player"); |
| 1240 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1241 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1242 | |
| 1243 | status_t status = mPlayer->getParameter(key, reply); |
| 1244 | if (status != OK) { |
| 1245 | ALOGD("getParameter returns %d", status); |
| 1246 | } |
| 1247 | return status; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1250 | bool MediaPlayer2::shouldDropMetadata(media::Metadata::Type code) const { |
| 1251 | Mutex::Autolock lock(mLock); |
| 1252 | |
| 1253 | if (findMetadata(mMetadataDrop, code)) { |
| 1254 | return true; |
| 1255 | } |
| 1256 | |
| 1257 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
| 1258 | return false; |
| 1259 | } else { |
| 1260 | return true; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | void MediaPlayer2::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
| 1266 | Mutex::Autolock lock(mLock); |
| 1267 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1268 | mMetadataUpdated.add(metadata_type); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | void MediaPlayer2::notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) { |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1273 | ALOGV("message received srcId=%lld, msg=%d, ext1=%d, ext2=%d", |
| 1274 | (long long)srcId, msg, ext1, ext2); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1275 | |
| 1276 | if (MEDIA2_INFO == msg && MEDIA2_INFO_METADATA_UPDATE == ext1) { |
| 1277 | const media::Metadata::Type metadata_type = ext2; |
| 1278 | |
| 1279 | if(shouldDropMetadata(metadata_type)) { |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | // Update the list of metadata that have changed. getMetadata |
| 1284 | // also access mMetadataUpdated and clears it. |
| 1285 | addNewMetadataUpdate(metadata_type); |
| 1286 | } |
| 1287 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1288 | bool send = true; |
| 1289 | bool locked = false; |
| 1290 | |
| 1291 | // TODO: In the future, we might be on the same thread if the app is |
| 1292 | // running in the same process as the media server. In that case, |
| 1293 | // this will deadlock. |
| 1294 | // |
| 1295 | // The threadId hack below works around this for the care of prepare, |
| 1296 | // seekTo, start, and reset within the same process. |
| 1297 | // FIXME: Remember, this is a hack, it's not even a hack that is applied |
| 1298 | // consistently for all use-cases, this needs to be revisited. |
| 1299 | if (mLockThreadId != getThreadId()) { |
| 1300 | mLock.lock(); |
| 1301 | locked = true; |
| 1302 | } |
| 1303 | |
| 1304 | // Allows calls from JNI in idle state to notify errors |
| 1305 | if (!(msg == MEDIA2_ERROR && mCurrentState == MEDIA_PLAYER2_IDLE) && mPlayer == 0) { |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1306 | ALOGV("notify(%lld, %d, %d, %d) callback on disconnected mediaplayer", |
| 1307 | (long long)srcId, msg, ext1, ext2); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1308 | if (locked) mLock.unlock(); // release the lock when done. |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | switch (msg) { |
| 1313 | case MEDIA2_NOP: // interface test message |
| 1314 | break; |
| 1315 | case MEDIA2_PREPARED: |
| 1316 | ALOGV("MediaPlayer2::notify() prepared"); |
| 1317 | mCurrentState = MEDIA_PLAYER2_PREPARED; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1318 | break; |
| 1319 | case MEDIA2_DRM_INFO: |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1320 | ALOGV("MediaPlayer2::notify() MEDIA2_DRM_INFO(%lld, %d, %d, %d, %p)", |
| 1321 | (long long)srcId, msg, ext1, ext2, obj); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1322 | break; |
| 1323 | case MEDIA2_PLAYBACK_COMPLETE: |
| 1324 | ALOGV("playback complete"); |
| 1325 | if (mCurrentState == MEDIA_PLAYER2_IDLE) { |
| 1326 | ALOGE("playback complete in idle state"); |
| 1327 | } |
| 1328 | if (!mLoop) { |
| 1329 | mCurrentState = MEDIA_PLAYER2_PLAYBACK_COMPLETE; |
| 1330 | } |
| 1331 | break; |
| 1332 | case MEDIA2_ERROR: |
| 1333 | // Always log errors. |
| 1334 | // ext1: Media framework error code. |
| 1335 | // ext2: Implementation dependant error code. |
| 1336 | ALOGE("error (%d, %d)", ext1, ext2); |
| 1337 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1338 | break; |
| 1339 | case MEDIA2_INFO: |
| 1340 | // ext1: Media framework error code. |
| 1341 | // ext2: Implementation dependant error code. |
| 1342 | if (ext1 != MEDIA2_INFO_VIDEO_TRACK_LAGGING) { |
| 1343 | ALOGW("info/warning (%d, %d)", ext1, ext2); |
| 1344 | } |
| 1345 | break; |
| 1346 | case MEDIA2_SEEK_COMPLETE: |
| 1347 | ALOGV("Received seek complete"); |
| 1348 | if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 1349 | ALOGV("Executing queued seekTo(%lld, %d)", |
| 1350 | (long long)mCurrentPosition, mCurrentSeekMode); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1351 | mSeekPosition = -1; |
| 1352 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 1353 | seekTo_l(mCurrentPosition, mCurrentSeekMode); |
| 1354 | } |
| 1355 | else { |
| 1356 | ALOGV("All seeks complete - return to regularly scheduled program"); |
| 1357 | mCurrentPosition = mSeekPosition = -1; |
| 1358 | mCurrentSeekMode = mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 1359 | } |
| 1360 | break; |
| 1361 | case MEDIA2_BUFFERING_UPDATE: |
| 1362 | ALOGV("buffering %d", ext1); |
| 1363 | break; |
| 1364 | case MEDIA2_SET_VIDEO_SIZE: |
| 1365 | ALOGV("New video size %d x %d", ext1, ext2); |
| 1366 | mVideoWidth = ext1; |
| 1367 | mVideoHeight = ext2; |
| 1368 | break; |
| 1369 | case MEDIA2_NOTIFY_TIME: |
| 1370 | ALOGV("Received notify time message"); |
| 1371 | break; |
| 1372 | case MEDIA2_TIMED_TEXT: |
| 1373 | ALOGV("Received timed text message"); |
| 1374 | break; |
| 1375 | case MEDIA2_SUBTITLE_DATA: |
| 1376 | ALOGV("Received subtitle data message"); |
| 1377 | break; |
| 1378 | case MEDIA2_META_DATA: |
| 1379 | ALOGV("Received timed metadata message"); |
| 1380 | break; |
| 1381 | default: |
| 1382 | ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | sp<MediaPlayer2Listener> listener = mListener; |
| 1387 | if (locked) mLock.unlock(); |
| 1388 | |
| 1389 | // this prevents re-entrant calls into client code |
| 1390 | if ((listener != 0) && send) { |
| 1391 | Mutex::Autolock _l(mNotifyLock); |
| 1392 | ALOGV("callback application"); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1393 | listener->notify(srcId, msg, ext1, ext2, obj); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1394 | ALOGV("back from callback"); |
| 1395 | } |
| 1396 | } |
| 1397 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1398 | // Modular DRM |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1399 | status_t MediaPlayer2::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1400 | // TODO change to ALOGV |
| 1401 | ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid, |
| 1402 | drmSessionId.array(), drmSessionId.size()); |
| 1403 | Mutex::Autolock _l(mLock); |
| 1404 | if (mPlayer == NULL) { |
| 1405 | return NO_INIT; |
| 1406 | } |
| 1407 | |
| 1408 | // Only allowed it in player's preparing/prepared state. |
| 1409 | // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or |
| 1410 | // completed) so the state change to "prepared" might not have happened yet (e.g., buffering). |
| 1411 | // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener. |
| 1412 | if (!(mCurrentState & (MEDIA_PLAYER2_PREPARING | MEDIA_PLAYER2_PREPARED))) { |
| 1413 | ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState); |
| 1414 | return INVALID_OPERATION; |
| 1415 | } |
| 1416 | |
| 1417 | if (drmSessionId.isEmpty()) { |
| 1418 | ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId."); |
| 1419 | return INVALID_OPERATION; |
| 1420 | } |
| 1421 | |
| 1422 | // Passing down to mediaserver mainly for creating the crypto |
| 1423 | status_t status = mPlayer->prepareDrm(uuid, drmSessionId); |
| 1424 | ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status); |
| 1425 | |
| 1426 | // TODO change to ALOGV |
| 1427 | ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status); |
| 1428 | |
| 1429 | return status; |
| 1430 | } |
| 1431 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1432 | status_t MediaPlayer2::releaseDrm() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1433 | Mutex::Autolock _l(mLock); |
| 1434 | if (mPlayer == NULL) { |
| 1435 | return NO_INIT; |
| 1436 | } |
| 1437 | |
| 1438 | // Not allowing releaseDrm in an active/resumable state |
| 1439 | if (mCurrentState & (MEDIA_PLAYER2_STARTED | |
| 1440 | MEDIA_PLAYER2_PAUSED | |
| 1441 | MEDIA_PLAYER2_PLAYBACK_COMPLETE | |
| 1442 | MEDIA_PLAYER2_STATE_ERROR)) { |
| 1443 | ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState); |
| 1444 | return INVALID_OPERATION; |
| 1445 | } |
| 1446 | |
| 1447 | status_t status = mPlayer->releaseDrm(); |
| 1448 | // TODO change to ALOGV |
| 1449 | ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status); |
| 1450 | if (status != OK) { |
| 1451 | ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status); |
| 1452 | // Overriding to OK so the client proceed with its own cleanup |
| 1453 | // Client can't do more cleanup. mediaserver release its crypto at end of session anyway. |
| 1454 | status = OK; |
| 1455 | } |
| 1456 | |
| 1457 | return status; |
| 1458 | } |
| 1459 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1460 | status_t MediaPlayer2::setOutputDevice(audio_port_handle_t deviceId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1461 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1462 | if (mAudioOutput == NULL) { |
| 1463 | ALOGV("setOutputDevice: audio sink not init"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1464 | return NO_INIT; |
| 1465 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1466 | return mAudioOutput->setOutputDevice(deviceId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1469 | audio_port_handle_t MediaPlayer2::getRoutedDeviceId() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1470 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1471 | if (mAudioOutput == NULL) { |
| 1472 | ALOGV("getRoutedDeviceId: audio sink not init"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1473 | return AUDIO_PORT_HANDLE_NONE; |
| 1474 | } |
| 1475 | audio_port_handle_t deviceId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1476 | status_t status = mAudioOutput->getRoutedDeviceId(&deviceId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1477 | if (status != NO_ERROR) { |
| 1478 | return AUDIO_PORT_HANDLE_NONE; |
| 1479 | } |
| 1480 | return deviceId; |
| 1481 | } |
| 1482 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1483 | status_t MediaPlayer2::enableAudioDeviceCallback(bool enabled) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1484 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1485 | if (mAudioOutput == NULL) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1486 | ALOGV("addAudioDeviceCallback: player not init"); |
| 1487 | return NO_INIT; |
| 1488 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1489 | return mAudioOutput->enableAudioDeviceCallback(enabled); |
| 1490 | } |
| 1491 | |
| 1492 | status_t MediaPlayer2::dump(int fd, const Vector<String16>& args) { |
| 1493 | const size_t SIZE = 256; |
| 1494 | char buffer[SIZE]; |
| 1495 | String8 result; |
| 1496 | result.append(" MediaPlayer2\n"); |
| 1497 | snprintf(buffer, 255, " pid(%d), looping(%s)\n", mPid, mLoop?"true": "false"); |
| 1498 | result.append(buffer); |
| 1499 | |
| 1500 | sp<MediaPlayer2Interface> player; |
| 1501 | sp<MediaPlayer2AudioOutput> audioOutput; |
| 1502 | bool locked = false; |
| 1503 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1504 | if (mLock.tryLock() == NO_ERROR) { |
| 1505 | locked = true; |
| 1506 | break; |
| 1507 | } |
| 1508 | usleep(kDumpLockSleepUs); |
| 1509 | } |
| 1510 | |
| 1511 | if (locked) { |
| 1512 | player = mPlayer; |
| 1513 | audioOutput = mAudioOutput; |
| 1514 | mLock.unlock(); |
| 1515 | } else { |
| 1516 | result.append(" lock is taken, no dump from player and audio output\n"); |
| 1517 | } |
| 1518 | write(fd, result.string(), result.size()); |
| 1519 | |
| 1520 | if (player != NULL) { |
| 1521 | player->dump(fd, args); |
| 1522 | } |
| 1523 | if (audioOutput != 0) { |
| 1524 | audioOutput->dump(fd, args); |
| 1525 | } |
| 1526 | write(fd, "\n", 1); |
| 1527 | return NO_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | } // namespace android |