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 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 56 | // marshalling tag indicating flattened utf16 tags |
| 57 | // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java |
| 58 | const int32_t kAudioAttributesMarshallTagFlattenTags = 1; |
| 59 | |
| 60 | // Audio attributes format in a parcel: |
| 61 | // |
| 62 | // 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 |
| 63 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | // | usage | |
| 65 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | // | content_type | |
| 67 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | // | source | |
| 69 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | // | flags | |
| 71 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found |
| 73 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 74 | // | flattened tags in UTF16 | |
| 75 | // | ... | |
| 76 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 77 | // |
| 78 | // @param p Parcel that contains audio attributes. |
| 79 | // @param[out] attributes On exit points to an initialized audio_attributes_t structure |
| 80 | // @param[out] status On exit contains the status code to be returned. |
| 81 | void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) { |
| 82 | attributes->usage = (audio_usage_t) parcel.readInt32(); |
| 83 | attributes->content_type = (audio_content_type_t) parcel.readInt32(); |
| 84 | attributes->source = (audio_source_t) parcel.readInt32(); |
| 85 | attributes->flags = (audio_flags_mask_t) parcel.readInt32(); |
| 86 | const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags); |
| 87 | if (hasFlattenedTag) { |
| 88 | // the tags are UTF16, convert to UTF8 |
| 89 | String16 tags = parcel.readString16(); |
| 90 | ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size()); |
| 91 | if (realTagSize <= 0) { |
| 92 | strcpy(attributes->tags, ""); |
| 93 | } else { |
| 94 | // copy the flattened string into the attributes as the destination for the conversion: |
| 95 | // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it |
| 96 | size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ? |
| 97 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize; |
| 98 | utf16_to_utf8(tags.string(), tagSize, attributes->tags, |
| 99 | sizeof(attributes->tags) / sizeof(attributes->tags[0])); |
| 100 | } |
| 101 | } else { |
| 102 | ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values"); |
| 103 | strcpy(attributes->tags, ""); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | class AudioDeviceUpdatedNotifier: public AudioSystem::AudioDeviceCallback { |
| 108 | public: |
| 109 | AudioDeviceUpdatedNotifier(const sp<MediaPlayer2Interface>& listener) |
| 110 | : mListener(listener) { } |
| 111 | |
| 112 | ~AudioDeviceUpdatedNotifier() { } |
| 113 | |
| 114 | virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo, |
| 115 | audio_port_handle_t deviceId) override { |
| 116 | sp<MediaPlayer2Interface> listener = mListener.promote(); |
| 117 | if (listener != NULL) { |
| 118 | listener->sendEvent(0, MEDIA2_AUDIO_ROUTING_CHANGED, audioIo, deviceId); |
| 119 | } else { |
| 120 | ALOGW("listener for process %d death is gone", MEDIA2_AUDIO_ROUTING_CHANGED); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | wp<MediaPlayer2Interface> mListener; |
| 126 | }; |
| 127 | |
| 128 | class proxyListener : public MediaPlayer2InterfaceListener { |
| 129 | public: |
| 130 | proxyListener(const wp<MediaPlayer2> &player) |
| 131 | : mPlayer(player) { } |
| 132 | |
| 133 | ~proxyListener() { }; |
| 134 | |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 135 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, |
| 136 | const PlayerMessage *obj) override { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 137 | sp<MediaPlayer2> player = mPlayer.promote(); |
| 138 | if (player != NULL) { |
| 139 | player->notify(srcId, msg, ext1, ext2, obj); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | wp<MediaPlayer2> mPlayer; |
| 145 | }; |
| 146 | |
| 147 | Mutex sRecordLock; |
| 148 | SortedVector<wp<MediaPlayer2> > *sPlayers; |
| 149 | |
| 150 | void ensureInit_l() { |
| 151 | if (sPlayers == NULL) { |
| 152 | sPlayers = new SortedVector<wp<MediaPlayer2> >(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void addPlayer(const wp<MediaPlayer2>& player) { |
| 157 | Mutex::Autolock lock(sRecordLock); |
| 158 | ensureInit_l(); |
| 159 | sPlayers->add(player); |
| 160 | } |
| 161 | |
| 162 | void removePlayer(const wp<MediaPlayer2>& player) { |
| 163 | Mutex::Autolock lock(sRecordLock); |
| 164 | ensureInit_l(); |
| 165 | sPlayers->remove(player); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * The only arguments this understands right now are -c, -von and -voff, |
| 170 | * which are parsed by ALooperRoster::dump() |
| 171 | */ |
| 172 | status_t dumpPlayers(int fd, const Vector<String16>& args) { |
| 173 | const size_t SIZE = 256; |
| 174 | char buffer[SIZE]; |
| 175 | String8 result; |
| 176 | SortedVector< sp<MediaPlayer2> > players; //to serialise the mutex unlock & client destruction. |
| 177 | |
| 178 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 179 | snprintf(buffer, SIZE, "Permission Denial: can't dump MediaPlayer2\n"); |
| 180 | result.append(buffer); |
| 181 | } else { |
| 182 | { |
| 183 | Mutex::Autolock lock(sRecordLock); |
| 184 | ensureInit_l(); |
| 185 | for (int i = 0, n = sPlayers->size(); i < n; ++i) { |
| 186 | sp<MediaPlayer2> p = (*sPlayers)[i].promote(); |
| 187 | if (p != 0) { |
| 188 | p->dump(fd, args); |
| 189 | } |
| 190 | players.add(p); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | result.append(" Files opened and/or mapped:\n"); |
| 195 | snprintf(buffer, SIZE, "/proc/%d/maps", getpid()); |
| 196 | FILE *f = fopen(buffer, "r"); |
| 197 | if (f) { |
| 198 | while (!feof(f)) { |
| 199 | fgets(buffer, SIZE, f); |
| 200 | if (strstr(buffer, " /storage/") || |
| 201 | strstr(buffer, " /system/sounds/") || |
| 202 | strstr(buffer, " /data/") || |
| 203 | strstr(buffer, " /system/media/")) { |
| 204 | result.append(" "); |
| 205 | result.append(buffer); |
| 206 | } |
| 207 | } |
| 208 | fclose(f); |
| 209 | } else { |
| 210 | result.append("couldn't open "); |
| 211 | result.append(buffer); |
| 212 | result.append("\n"); |
| 213 | } |
| 214 | |
| 215 | snprintf(buffer, SIZE, "/proc/%d/fd", getpid()); |
| 216 | DIR *d = opendir(buffer); |
| 217 | if (d) { |
| 218 | struct dirent *ent; |
| 219 | while((ent = readdir(d)) != NULL) { |
| 220 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
| 221 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name); |
| 222 | struct stat s; |
| 223 | if (lstat(buffer, &s) == 0) { |
| 224 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 225 | char linkto[256]; |
| 226 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 227 | if(len > 0) { |
| 228 | if(len > 255) { |
| 229 | linkto[252] = '.'; |
| 230 | linkto[253] = '.'; |
| 231 | linkto[254] = '.'; |
| 232 | linkto[255] = 0; |
| 233 | } else { |
| 234 | linkto[len] = 0; |
| 235 | } |
| 236 | if (strstr(linkto, "/storage/") == linkto || |
| 237 | strstr(linkto, "/system/sounds/") == linkto || |
| 238 | strstr(linkto, "/data/") == linkto || |
| 239 | strstr(linkto, "/system/media/") == linkto) { |
| 240 | result.append(" "); |
| 241 | result.append(buffer); |
| 242 | result.append(" -> "); |
| 243 | result.append(linkto); |
| 244 | result.append("\n"); |
| 245 | } |
| 246 | } |
| 247 | } else { |
| 248 | result.append(" unexpected type for "); |
| 249 | result.append(buffer); |
| 250 | result.append("\n"); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | closedir(d); |
| 256 | } else { |
| 257 | result.append("couldn't open "); |
| 258 | result.append(buffer); |
| 259 | result.append("\n"); |
| 260 | } |
| 261 | |
| 262 | gLooperRoster.dump(fd, args); |
| 263 | |
| 264 | bool dumpMem = false; |
| 265 | bool unreachableMemory = false; |
| 266 | for (size_t i = 0; i < args.size(); i++) { |
| 267 | if (args[i] == String16("-m")) { |
| 268 | dumpMem = true; |
| 269 | } else if (args[i] == String16("--unreachable")) { |
| 270 | unreachableMemory = true; |
| 271 | } |
| 272 | } |
| 273 | if (dumpMem) { |
| 274 | result.append("\nDumping memory:\n"); |
| 275 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 276 | result.append(s.c_str(), s.size()); |
| 277 | } |
| 278 | if (unreachableMemory) { |
| 279 | result.append("\nDumping unreachable memory:\n"); |
| 280 | // TODO - should limit be an argument parameter? |
| 281 | // TODO: enable GetUnreachableMemoryString if it's part of stable API |
| 282 | //std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */); |
| 283 | //result.append(s.c_str(), s.size()); |
| 284 | } |
| 285 | } |
| 286 | write(fd, result.string(), result.size()); |
| 287 | return NO_ERROR; |
| 288 | } |
| 289 | |
| 290 | } // anonymous namespace |
| 291 | |
| 292 | //static |
| 293 | sp<MediaPlayer2> MediaPlayer2::Create() { |
| 294 | sp<MediaPlayer2> player = new MediaPlayer2(); |
| 295 | |
| 296 | if (!player->init()) { |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | ALOGV("Create new player(%p)", player.get()); |
| 301 | |
| 302 | addPlayer(player); |
| 303 | return player; |
| 304 | } |
| 305 | |
| 306 | // static |
| 307 | status_t MediaPlayer2::DumpAll(int fd, const Vector<String16>& args) { |
| 308 | return dumpPlayers(fd, args); |
| 309 | } |
| 310 | |
| 311 | MediaPlayer2::MediaPlayer2() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 312 | ALOGV("constructor"); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 313 | mSrcId = 0; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 314 | mLockThreadId = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 315 | mListener = NULL; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 316 | mStreamType = AUDIO_STREAM_MUSIC; |
| 317 | mAudioAttributesParcel = NULL; |
| 318 | mCurrentPosition = -1; |
| 319 | mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 320 | mSeekPosition = -1; |
| 321 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 322 | mCurrentState = MEDIA_PLAYER2_IDLE; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 323 | mLoop = false; |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 324 | mVolume = 1.0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 325 | mVideoWidth = mVideoHeight = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 326 | mAudioSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 327 | AudioSystem::acquireAudioSessionId(mAudioSessionId, -1); |
| 328 | mSendLevel = 0; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 329 | |
| 330 | // TODO: get pid and uid from JAVA |
| 331 | mPid = IPCThreadState::self()->getCallingPid(); |
| 332 | mUid = IPCThreadState::self()->getCallingUid(); |
| 333 | |
| 334 | mAudioAttributes = NULL; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 337 | MediaPlayer2::~MediaPlayer2() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 338 | ALOGV("destructor"); |
| 339 | if (mAudioAttributesParcel != NULL) { |
| 340 | delete mAudioAttributesParcel; |
| 341 | mAudioAttributesParcel = NULL; |
| 342 | } |
| 343 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
| 344 | disconnect(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 345 | removePlayer(this); |
| 346 | if (mAudioAttributes != NULL) { |
| 347 | free(mAudioAttributes); |
| 348 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 351 | bool MediaPlayer2::init() { |
| 352 | // TODO: after merge with NuPlayer2Driver, MediaPlayer2 will have its own |
| 353 | // looper for notification. |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | void MediaPlayer2::disconnect() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 358 | ALOGV("disconnect"); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 359 | sp<MediaPlayer2Interface> p; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 360 | { |
| 361 | Mutex::Autolock _l(mLock); |
| 362 | p = mPlayer; |
| 363 | mPlayer.clear(); |
| 364 | } |
| 365 | |
| 366 | if (p != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 367 | p->setListener(NULL); |
| 368 | p->reset(); |
| 369 | } |
| 370 | |
| 371 | { |
| 372 | Mutex::Autolock _l(mLock); |
| 373 | disconnectNativeWindow_l(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 377 | void MediaPlayer2::clear_l() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 378 | mCurrentPosition = -1; |
| 379 | mCurrentSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 380 | mSeekPosition = -1; |
| 381 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 382 | mVideoWidth = mVideoHeight = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 385 | status_t MediaPlayer2::setListener(const sp<MediaPlayer2Listener>& listener) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 386 | ALOGV("setListener"); |
| 387 | Mutex::Autolock _l(mLock); |
| 388 | mListener = listener; |
| 389 | return NO_ERROR; |
| 390 | } |
| 391 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 392 | status_t MediaPlayer2::getSrcId(int64_t *srcId) { |
| 393 | if (srcId == NULL) { |
| 394 | return BAD_VALUE; |
| 395 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 396 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 397 | Mutex::Autolock _l(mLock); |
| 398 | *srcId = mSrcId; |
| 399 | return OK; |
| 400 | } |
| 401 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 402 | status_t MediaPlayer2::setDataSource(const sp<DataSourceDesc> &dsd) { |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 403 | if (dsd == NULL) { |
| 404 | return BAD_VALUE; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 405 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 406 | ALOGV("setDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId); |
| 407 | |
| 408 | sp<MediaPlayer2Interface> oldPlayer; |
| 409 | |
| 410 | Mutex::Autolock _l(mLock); |
| 411 | { |
| 412 | if (!((mCurrentState & MEDIA_PLAYER2_IDLE) |
| 413 | || mCurrentState == MEDIA_PLAYER2_STATE_ERROR)) { |
| 414 | ALOGE("setDataSource called in wrong state %d", mCurrentState); |
| 415 | return INVALID_OPERATION; |
| 416 | } |
| 417 | |
| 418 | sp<MediaPlayer2Interface> player = new NuPlayer2Driver(mPid, mUid); |
| 419 | status_t err = player->initCheck(); |
| 420 | if (err != NO_ERROR) { |
| 421 | ALOGE("Failed to create player object, initCheck failed(%d)", err); |
| 422 | return err; |
| 423 | } |
| 424 | |
| 425 | clear_l(); |
| 426 | |
| 427 | player->setListener(new proxyListener(this)); |
| 428 | mAudioOutput = new MediaPlayer2AudioOutput(mAudioSessionId, mUid, |
| 429 | mPid, mAudioAttributes, new AudioDeviceUpdatedNotifier(player)); |
| 430 | player->setAudioSink(mAudioOutput); |
| 431 | |
| 432 | err = player->setDataSource(dsd); |
| 433 | if (err != OK) { |
| 434 | ALOGE("setDataSource error: %d", err); |
| 435 | return err; |
| 436 | } |
| 437 | |
| 438 | sp<MediaPlayer2Interface> oldPlayer = mPlayer; |
| 439 | mPlayer = player; |
| 440 | mSrcId = dsd->mId; |
| 441 | mCurrentState = MEDIA_PLAYER2_INITIALIZED; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 442 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 443 | |
| 444 | if (oldPlayer != NULL) { |
| 445 | oldPlayer->setListener(NULL); |
| 446 | oldPlayer->reset(); |
| 447 | } |
| 448 | |
| 449 | return OK; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 452 | status_t MediaPlayer2::prepareNextDataSource(const sp<DataSourceDesc> &dsd) { |
| 453 | if (dsd == NULL) { |
| 454 | return BAD_VALUE; |
| 455 | } |
| 456 | ALOGV("prepareNextDataSource type(%d), srcId(%lld)", dsd->mType, (long long)dsd->mId); |
| 457 | |
| 458 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 459 | if (mPlayer == NULL) { |
| 460 | ALOGE("prepareNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 461 | return INVALID_OPERATION; |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 462 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 463 | return mPlayer->prepareNextDataSource(dsd); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | status_t MediaPlayer2::playNextDataSource(int64_t srcId) { |
| 467 | ALOGV("playNextDataSource srcId(%lld)", (long long)srcId); |
| 468 | |
| 469 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 470 | if (mPlayer == NULL) { |
| 471 | ALOGE("playNextDataSource failed: state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 472 | return INVALID_OPERATION; |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 473 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 474 | mSrcId = srcId; |
| 475 | return mPlayer->playNextDataSource(srcId); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Dongwon Kang | 9f63198 | 2018-07-10 12:34:41 -0700 | [diff] [blame] | 478 | status_t MediaPlayer2::invoke(const PlayerMessage &request, PlayerMessage *reply) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 479 | Mutex::Autolock _l(mLock); |
| 480 | const bool hasBeenInitialized = |
| 481 | (mCurrentState != MEDIA_PLAYER2_STATE_ERROR) && |
| 482 | ((mCurrentState & MEDIA_PLAYER2_IDLE) != MEDIA_PLAYER2_IDLE); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 483 | if ((mPlayer == NULL) || !hasBeenInitialized) { |
Dongwon Kang | 9f63198 | 2018-07-10 12:34:41 -0700 | [diff] [blame] | 484 | ALOGE("invoke() failed: wrong state %X, mPlayer(%p)", mCurrentState, mPlayer.get()); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 485 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 486 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 487 | return mPlayer->invoke(request, reply); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 490 | void MediaPlayer2::disconnectNativeWindow_l() { |
| 491 | if (mConnectedWindow != NULL && mConnectedWindow->getANativeWindow() != NULL) { |
| 492 | status_t err = native_window_api_disconnect( |
| 493 | mConnectedWindow->getANativeWindow(), NATIVE_WINDOW_API_MEDIA); |
| 494 | |
| 495 | if (err != OK) { |
| 496 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
| 497 | strerror(-err), err); |
| 498 | } |
| 499 | } |
| 500 | mConnectedWindow.clear(); |
| 501 | } |
| 502 | |
| 503 | status_t MediaPlayer2::setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww) { |
| 504 | ANativeWindow *anw = (nww == NULL ? NULL : nww->getANativeWindow()); |
| 505 | ALOGV("setVideoSurfaceTexture(%p)", anw); |
| 506 | Mutex::Autolock _l(mLock); |
| 507 | if (mPlayer == 0) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 508 | return NO_INIT; |
| 509 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 510 | |
| 511 | if (anw != NULL) { |
| 512 | if (mConnectedWindow != NULL |
| 513 | && mConnectedWindow->getANativeWindow() == anw) { |
| 514 | return OK; |
| 515 | } |
| 516 | status_t err = native_window_api_connect(anw, NATIVE_WINDOW_API_MEDIA); |
| 517 | |
| 518 | if (err != OK) { |
| 519 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
| 520 | // Note that we must do the reset before disconnecting from the ANW. |
| 521 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 522 | // ANW, which may result in errors. |
| 523 | mPlayer->reset(); |
| 524 | disconnectNativeWindow_l(); |
| 525 | return err; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Note that we must set the player's new GraphicBufferProducer before |
| 530 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 531 | // on the disconnected ANW, which may result in errors. |
| 532 | status_t err = mPlayer->setVideoSurfaceTexture(nww); |
| 533 | |
| 534 | disconnectNativeWindow_l(); |
| 535 | |
| 536 | if (err == OK) { |
| 537 | mConnectedWindow = nww; |
| 538 | mLock.unlock(); |
| 539 | } else if (anw != NULL) { |
| 540 | mLock.unlock(); |
| 541 | status_t err = native_window_api_disconnect(anw, NATIVE_WINDOW_API_MEDIA); |
| 542 | |
| 543 | if (err != OK) { |
| 544 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
| 545 | strerror(-err), err); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return err; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 552 | status_t MediaPlayer2::getBufferingSettings(BufferingSettings* buffering /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 553 | ALOGV("getBufferingSettings"); |
| 554 | |
| 555 | Mutex::Autolock _l(mLock); |
| 556 | if (mPlayer == 0) { |
| 557 | return NO_INIT; |
| 558 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 559 | |
| 560 | status_t ret = mPlayer->getBufferingSettings(buffering); |
| 561 | if (ret == NO_ERROR) { |
| 562 | ALOGV("getBufferingSettings{%s}", buffering->toString().string()); |
| 563 | } else { |
| 564 | ALOGE("getBufferingSettings returned %d", ret); |
| 565 | } |
| 566 | return ret; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 569 | status_t MediaPlayer2::setBufferingSettings(const BufferingSettings& buffering) { |
| 570 | ALOGV("setBufferingSettings{%s}", buffering.toString().string()); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 571 | |
| 572 | Mutex::Autolock _l(mLock); |
| 573 | if (mPlayer == 0) { |
| 574 | return NO_INIT; |
| 575 | } |
| 576 | return mPlayer->setBufferingSettings(buffering); |
| 577 | } |
| 578 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 579 | status_t MediaPlayer2::setAudioAttributes_l(const Parcel &parcel) { |
| 580 | if (mAudioAttributes != NULL) { |
| 581 | free(mAudioAttributes); |
| 582 | } |
| 583 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 584 | if (mAudioAttributes == NULL) { |
| 585 | return NO_MEMORY; |
| 586 | } |
| 587 | unmarshallAudioAttributes(parcel, mAudioAttributes); |
| 588 | |
| 589 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", |
| 590 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, |
| 591 | mAudioAttributes->tags); |
| 592 | |
| 593 | if (mAudioOutput != 0) { |
| 594 | mAudioOutput->setAudioAttributes(mAudioAttributes); |
| 595 | } |
| 596 | return NO_ERROR; |
| 597 | } |
| 598 | |
| 599 | status_t MediaPlayer2::prepareAsync() { |
| 600 | ALOGV("prepareAsync"); |
| 601 | Mutex::Autolock _l(mLock); |
Wei Jia | 0151ef4 | 2018-08-24 16:40:21 -0700 | [diff] [blame] | 602 | if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER2_INITIALIZED)) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 603 | if (mAudioAttributesParcel != NULL) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 604 | status_t err = setAudioAttributes_l(*mAudioAttributesParcel); |
| 605 | if (err != OK) { |
| 606 | return err; |
| 607 | } |
| 608 | } else if (mAudioOutput != 0) { |
| 609 | mAudioOutput->setAudioStreamType(mStreamType); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 610 | } |
| 611 | mCurrentState = MEDIA_PLAYER2_PREPARING; |
| 612 | return mPlayer->prepareAsync(); |
| 613 | } |
| 614 | ALOGE("prepareAsync called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 615 | return INVALID_OPERATION; |
| 616 | } |
| 617 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 618 | status_t MediaPlayer2::start() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 619 | ALOGV("start"); |
| 620 | |
| 621 | status_t ret = NO_ERROR; |
| 622 | Mutex::Autolock _l(mLock); |
| 623 | |
| 624 | mLockThreadId = getThreadId(); |
| 625 | |
| 626 | if (mCurrentState & MEDIA_PLAYER2_STARTED) { |
| 627 | ret = NO_ERROR; |
| 628 | } else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER2_PREPARED | |
| 629 | MEDIA_PLAYER2_PLAYBACK_COMPLETE | MEDIA_PLAYER2_PAUSED ) ) ) { |
| 630 | mPlayer->setLooping(mLoop); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 631 | |
| 632 | if (mAudioOutput != 0) { |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 633 | mAudioOutput->setVolume(mVolume); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (mAudioOutput != 0) { |
| 637 | mAudioOutput->setAuxEffectSendLevel(mSendLevel); |
| 638 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 639 | mCurrentState = MEDIA_PLAYER2_STARTED; |
| 640 | ret = mPlayer->start(); |
| 641 | if (ret != NO_ERROR) { |
| 642 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 643 | } else { |
| 644 | if (mCurrentState == MEDIA_PLAYER2_PLAYBACK_COMPLETE) { |
| 645 | ALOGV("playback completed immediately following start()"); |
| 646 | } |
| 647 | } |
| 648 | } else { |
| 649 | ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 650 | ret = INVALID_OPERATION; |
| 651 | } |
| 652 | |
| 653 | mLockThreadId = 0; |
| 654 | |
| 655 | return ret; |
| 656 | } |
| 657 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 658 | status_t MediaPlayer2::pause() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 659 | ALOGV("pause"); |
| 660 | Mutex::Autolock _l(mLock); |
| 661 | if (mCurrentState & (MEDIA_PLAYER2_PAUSED|MEDIA_PLAYER2_PLAYBACK_COMPLETE)) |
| 662 | return NO_ERROR; |
Wei Jia | 6376cd5 | 2018-09-26 11:42:55 -0700 | [diff] [blame] | 663 | if ((mPlayer != 0) && (mCurrentState & (MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED))) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 664 | status_t ret = mPlayer->pause(); |
| 665 | if (ret != NO_ERROR) { |
| 666 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 667 | } else { |
| 668 | mCurrentState = MEDIA_PLAYER2_PAUSED; |
| 669 | } |
| 670 | return ret; |
| 671 | } |
| 672 | ALOGE("pause called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 673 | return INVALID_OPERATION; |
| 674 | } |
| 675 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 676 | bool MediaPlayer2::isPlaying() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 677 | Mutex::Autolock _l(mLock); |
| 678 | if (mPlayer != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 679 | bool temp = mPlayer->isPlaying(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 680 | ALOGV("isPlaying: %d", temp); |
| 681 | if ((mCurrentState & MEDIA_PLAYER2_STARTED) && ! temp) { |
| 682 | ALOGE("internal/external state mismatch corrected"); |
| 683 | mCurrentState = MEDIA_PLAYER2_PAUSED; |
| 684 | } else if ((mCurrentState & MEDIA_PLAYER2_PAUSED) && temp) { |
| 685 | ALOGE("internal/external state mismatch corrected"); |
| 686 | mCurrentState = MEDIA_PLAYER2_STARTED; |
| 687 | } |
| 688 | return temp; |
| 689 | } |
| 690 | ALOGV("isPlaying: no active player"); |
| 691 | return false; |
| 692 | } |
| 693 | |
Wei Jia | 1f043e4 | 2018-06-20 16:52:50 -0700 | [diff] [blame] | 694 | mediaplayer2_states MediaPlayer2::getState() { |
Wei Jia | 98787a7 | 2018-03-02 14:33:06 -0800 | [diff] [blame] | 695 | Mutex::Autolock _l(mLock); |
| 696 | if (mCurrentState & MEDIA_PLAYER2_STATE_ERROR) { |
| 697 | return MEDIAPLAYER2_STATE_ERROR; |
| 698 | } |
| 699 | if (mPlayer == 0 |
| 700 | || (mCurrentState & |
| 701 | (MEDIA_PLAYER2_IDLE | MEDIA_PLAYER2_INITIALIZED | MEDIA_PLAYER2_PREPARING))) { |
| 702 | return MEDIAPLAYER2_STATE_IDLE; |
| 703 | } |
| 704 | if (mCurrentState & MEDIA_PLAYER2_STARTED) { |
| 705 | return MEDIAPLAYER2_STATE_PLAYING; |
| 706 | } |
Wei Jia | 0151ef4 | 2018-08-24 16:40:21 -0700 | [diff] [blame] | 707 | if (mCurrentState & (MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE)) { |
Wei Jia | 98787a7 | 2018-03-02 14:33:06 -0800 | [diff] [blame] | 708 | return MEDIAPLAYER2_STATE_PAUSED; |
| 709 | } |
| 710 | // now only mCurrentState & MEDIA_PLAYER2_PREPARED is true |
| 711 | return MEDIAPLAYER2_STATE_PREPARED; |
| 712 | } |
| 713 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 714 | status_t MediaPlayer2::setPlaybackSettings(const AudioPlaybackRate& rate) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 715 | ALOGV("setPlaybackSettings: %f %f %d %d", |
| 716 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
| 717 | // Negative speed and pitch does not make sense. Further validation will |
| 718 | // be done by the respective mediaplayers. |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 719 | if (rate.mSpeed <= 0.f || rate.mPitch < 0.f) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 720 | return BAD_VALUE; |
| 721 | } |
| 722 | Mutex::Autolock _l(mLock); |
Wei Jia | 0151ef4 | 2018-08-24 16:40:21 -0700 | [diff] [blame] | 723 | if (mPlayer == 0) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 724 | return INVALID_OPERATION; |
| 725 | } |
| 726 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 727 | status_t err = mPlayer->setPlaybackSettings(rate); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 728 | return err; |
| 729 | } |
| 730 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 731 | status_t MediaPlayer2::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 732 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 733 | if (mPlayer == 0) { |
| 734 | return INVALID_OPERATION; |
| 735 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 736 | status_t ret = mPlayer->getPlaybackSettings(rate); |
| 737 | if (ret == NO_ERROR) { |
| 738 | ALOGV("getPlaybackSettings(%f, %f, %d, %d)", |
| 739 | rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); |
| 740 | } else { |
| 741 | ALOGV("getPlaybackSettings returned %d", ret); |
| 742 | } |
| 743 | return ret; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 746 | status_t MediaPlayer2::setSyncSettings(const AVSyncSettings& sync, float videoFpsHint) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 747 | ALOGV("setSyncSettings: %u %u %f %f", |
| 748 | sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 749 | Mutex::Autolock _l(mLock); |
| 750 | if (mPlayer == 0) return INVALID_OPERATION; |
| 751 | return mPlayer->setSyncSettings(sync, videoFpsHint); |
| 752 | } |
| 753 | |
| 754 | status_t MediaPlayer2::getSyncSettings( |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 755 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 756 | Mutex::Autolock _l(mLock); |
Wei Jia | 334adf0 | 2018-03-23 14:08:23 -0700 | [diff] [blame] | 757 | if (mPlayer == 0) { |
| 758 | return INVALID_OPERATION; |
| 759 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 760 | status_t ret = mPlayer->getSyncSettings(sync, videoFps); |
| 761 | if (ret == NO_ERROR) { |
| 762 | ALOGV("getSyncSettings(%u, %u, %f, %f)", |
| 763 | sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); |
| 764 | } else { |
| 765 | ALOGV("getSyncSettings returned %d", ret); |
| 766 | } |
| 767 | return ret; |
| 768 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 771 | status_t MediaPlayer2::getVideoWidth(int *w) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 772 | ALOGV("getVideoWidth"); |
| 773 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 774 | if (mPlayer == 0) { |
| 775 | return INVALID_OPERATION; |
| 776 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 777 | *w = mVideoWidth; |
| 778 | return NO_ERROR; |
| 779 | } |
| 780 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 781 | status_t MediaPlayer2::getVideoHeight(int *h) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 782 | ALOGV("getVideoHeight"); |
| 783 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 784 | if (mPlayer == 0) { |
| 785 | return INVALID_OPERATION; |
| 786 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 787 | *h = mVideoHeight; |
| 788 | return NO_ERROR; |
| 789 | } |
| 790 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 791 | status_t MediaPlayer2::getCurrentPosition(int64_t *msec) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 792 | ALOGV("getCurrentPosition"); |
| 793 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 794 | if (mPlayer == 0) { |
| 795 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 796 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 797 | if (mCurrentPosition >= 0) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 798 | ALOGV("Using cached seek position: %lld", (long long)mCurrentPosition); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 799 | *msec = mCurrentPosition; |
| 800 | return NO_ERROR; |
| 801 | } |
| 802 | status_t ret = mPlayer->getCurrentPosition(msec); |
| 803 | if (ret == NO_ERROR) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 804 | ALOGV("getCurrentPosition = %lld", (long long)*msec); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 805 | } else { |
| 806 | ALOGE("getCurrentPosition returned %d", ret); |
| 807 | } |
| 808 | return ret; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 811 | status_t MediaPlayer2::getDuration(int64_t *msec) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 812 | Mutex::Autolock _l(mLock); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 813 | ALOGV("getDuration_l"); |
| 814 | bool isValidState = (mCurrentState & (MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
Wei Jia | 0151ef4 | 2018-08-24 16:40:21 -0700 | [diff] [blame] | 815 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE)); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 816 | if (mPlayer == 0 || !isValidState) { |
| 817 | ALOGE("Attempt to call getDuration in wrong state: mPlayer=%p, mCurrentState=%u", |
| 818 | mPlayer.get(), mCurrentState); |
| 819 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 820 | } |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 821 | int64_t durationMs; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 822 | status_t ret = mPlayer->getDuration(&durationMs); |
| 823 | |
| 824 | if (ret == NO_ERROR) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 825 | ALOGV("getDuration = %lld", (long long)durationMs); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 826 | } else { |
| 827 | ALOGE("getDuration returned %d", ret); |
| 828 | // Do not enter error state just because no duration was available. |
| 829 | durationMs = -1; |
| 830 | } |
| 831 | |
| 832 | if (msec) { |
| 833 | *msec = durationMs; |
| 834 | } |
| 835 | return OK; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 838 | status_t MediaPlayer2::seekTo_l(int64_t msec, MediaPlayer2SeekMode mode) { |
| 839 | ALOGV("seekTo (%lld, %d)", (long long)msec, mode); |
| 840 | if ((mPlayer == 0) || !(mCurrentState & (MEDIA_PLAYER2_STARTED | MEDIA_PLAYER2_PREPARED | |
| 841 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE))) { |
| 842 | ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", |
| 843 | mPlayer.get(), mCurrentState); |
| 844 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 845 | } |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 846 | if (msec < 0) { |
| 847 | ALOGW("Attempt to seek to invalid position: %lld", (long long)msec); |
| 848 | msec = 0; |
| 849 | } |
| 850 | |
| 851 | int64_t durationMs; |
| 852 | status_t err = mPlayer->getDuration(&durationMs); |
| 853 | |
| 854 | if (err != OK) { |
| 855 | ALOGW("Stream has no duration and is therefore not seekable."); |
| 856 | return err; |
| 857 | } |
| 858 | |
| 859 | if (msec > durationMs) { |
| 860 | ALOGW("Attempt to seek to past end of file: request = %lld, durationMs = %lld", |
| 861 | (long long)msec, (long long)durationMs); |
| 862 | |
| 863 | msec = durationMs; |
| 864 | } |
| 865 | |
| 866 | // cache duration |
| 867 | mCurrentPosition = msec; |
| 868 | mCurrentSeekMode = mode; |
| 869 | if (mSeekPosition < 0) { |
| 870 | mSeekPosition = msec; |
| 871 | mSeekMode = mode; |
| 872 | return mPlayer->seekTo(msec, mode); |
| 873 | } |
| 874 | ALOGV("Seek in progress - queue up seekTo[%lld, %d]", (long long)msec, mode); |
| 875 | return NO_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 878 | status_t MediaPlayer2::seekTo(int64_t msec, MediaPlayer2SeekMode mode) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 879 | mLockThreadId = getThreadId(); |
| 880 | Mutex::Autolock _l(mLock); |
| 881 | status_t result = seekTo_l(msec, mode); |
| 882 | mLockThreadId = 0; |
| 883 | |
| 884 | return result; |
| 885 | } |
| 886 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 887 | status_t MediaPlayer2::notifyAt(int64_t mediaTimeUs) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 888 | Mutex::Autolock _l(mLock); |
| 889 | if (mPlayer != 0) { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 890 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 891 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 892 | |
| 893 | return mPlayer->notifyAt(mediaTimeUs); |
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::reset_l() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 897 | mLoop = false; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 898 | if (mCurrentState == MEDIA_PLAYER2_IDLE) { |
| 899 | return NO_ERROR; |
| 900 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 901 | if (mPlayer != 0) { |
| 902 | status_t ret = mPlayer->reset(); |
| 903 | if (ret != NO_ERROR) { |
| 904 | ALOGE("reset() failed with return code (%d)", ret); |
| 905 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
| 906 | } else { |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 907 | mPlayer->setListener(NULL); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 908 | mCurrentState = MEDIA_PLAYER2_IDLE; |
| 909 | } |
| 910 | // setDataSource has to be called again to create a |
| 911 | // new mediaplayer. |
| 912 | mPlayer = 0; |
| 913 | return ret; |
| 914 | } |
| 915 | clear_l(); |
| 916 | return NO_ERROR; |
| 917 | } |
| 918 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 919 | status_t MediaPlayer2::reset() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 920 | ALOGV("reset"); |
| 921 | mLockThreadId = getThreadId(); |
| 922 | Mutex::Autolock _l(mLock); |
| 923 | status_t result = reset_l(); |
| 924 | mLockThreadId = 0; |
| 925 | |
| 926 | return result; |
| 927 | } |
| 928 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 929 | status_t MediaPlayer2::setAudioStreamType(audio_stream_type_t type) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 930 | ALOGV("MediaPlayer2::setAudioStreamType"); |
| 931 | Mutex::Autolock _l(mLock); |
| 932 | if (mStreamType == type) return NO_ERROR; |
| 933 | if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
| 934 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE ) ) { |
| 935 | // Can't change the stream type after prepare |
| 936 | ALOGE("setAudioStream called in state %d", mCurrentState); |
| 937 | return INVALID_OPERATION; |
| 938 | } |
| 939 | // cache |
| 940 | mStreamType = type; |
| 941 | return OK; |
| 942 | } |
| 943 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 944 | status_t MediaPlayer2::getAudioStreamType(audio_stream_type_t *type) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 945 | ALOGV("getAudioStreamType"); |
| 946 | Mutex::Autolock _l(mLock); |
| 947 | *type = mStreamType; |
| 948 | return OK; |
| 949 | } |
| 950 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 951 | status_t MediaPlayer2::setLooping(int loop) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 952 | ALOGV("MediaPlayer2::setLooping"); |
| 953 | Mutex::Autolock _l(mLock); |
| 954 | mLoop = (loop != 0); |
| 955 | if (mPlayer != 0) { |
| 956 | return mPlayer->setLooping(loop); |
| 957 | } |
| 958 | return OK; |
| 959 | } |
| 960 | |
| 961 | bool MediaPlayer2::isLooping() { |
| 962 | ALOGV("isLooping"); |
| 963 | Mutex::Autolock _l(mLock); |
| 964 | if (mPlayer != 0) { |
| 965 | return mLoop; |
| 966 | } |
| 967 | ALOGV("isLooping: no active player"); |
| 968 | return false; |
| 969 | } |
| 970 | |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 971 | status_t MediaPlayer2::setVolume(float volume) { |
| 972 | ALOGV("MediaPlayer2::setVolume(%f)", volume); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 973 | Mutex::Autolock _l(mLock); |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 974 | mVolume = volume; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 975 | if (mAudioOutput != 0) { |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 976 | mAudioOutput->setVolume(volume); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 977 | } |
| 978 | return OK; |
| 979 | } |
| 980 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 981 | status_t MediaPlayer2::setAudioSessionId(audio_session_t sessionId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 982 | ALOGV("MediaPlayer2::setAudioSessionId(%d)", sessionId); |
| 983 | Mutex::Autolock _l(mLock); |
| 984 | if (!(mCurrentState & MEDIA_PLAYER2_IDLE)) { |
| 985 | ALOGE("setAudioSessionId called in state %d", mCurrentState); |
| 986 | return INVALID_OPERATION; |
| 987 | } |
| 988 | if (sessionId < 0) { |
| 989 | return BAD_VALUE; |
| 990 | } |
| 991 | if (sessionId != mAudioSessionId) { |
| 992 | AudioSystem::acquireAudioSessionId(sessionId, -1); |
| 993 | AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); |
| 994 | mAudioSessionId = sessionId; |
| 995 | } |
| 996 | return NO_ERROR; |
| 997 | } |
| 998 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 999 | audio_session_t MediaPlayer2::getAudioSessionId() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1000 | Mutex::Autolock _l(mLock); |
| 1001 | return mAudioSessionId; |
| 1002 | } |
| 1003 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1004 | status_t MediaPlayer2::setAuxEffectSendLevel(float level) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1005 | ALOGV("MediaPlayer2::setAuxEffectSendLevel(%f)", level); |
| 1006 | Mutex::Autolock _l(mLock); |
| 1007 | mSendLevel = level; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1008 | if (mAudioOutput != 0) { |
| 1009 | return mAudioOutput->setAuxEffectSendLevel(level); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1010 | } |
| 1011 | return OK; |
| 1012 | } |
| 1013 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1014 | status_t MediaPlayer2::attachAuxEffect(int effectId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1015 | ALOGV("MediaPlayer2::attachAuxEffect(%d)", effectId); |
| 1016 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1017 | if (mAudioOutput == 0 || |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1018 | (mCurrentState & MEDIA_PLAYER2_IDLE) || |
| 1019 | (mCurrentState == MEDIA_PLAYER2_STATE_ERROR )) { |
| 1020 | ALOGE("attachAuxEffect called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get()); |
| 1021 | return INVALID_OPERATION; |
| 1022 | } |
| 1023 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1024 | return mAudioOutput->attachAuxEffect(effectId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | // always call with lock held |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1028 | status_t MediaPlayer2::checkStateForKeySet_l(int key) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1029 | switch(key) { |
| 1030 | case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1031 | if (mCurrentState & ( MEDIA_PLAYER2_PREPARED | MEDIA_PLAYER2_STARTED | |
| 1032 | MEDIA_PLAYER2_PAUSED | MEDIA_PLAYER2_PLAYBACK_COMPLETE) ) { |
| 1033 | // Can't change the audio attributes after prepare |
| 1034 | ALOGE("trying to set audio attributes called in state %d", mCurrentState); |
| 1035 | return INVALID_OPERATION; |
| 1036 | } |
| 1037 | break; |
| 1038 | default: |
| 1039 | // parameter doesn't require player state check |
| 1040 | break; |
| 1041 | } |
| 1042 | return OK; |
| 1043 | } |
| 1044 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1045 | status_t MediaPlayer2::setParameter(int key, const Parcel& request) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1046 | ALOGV("MediaPlayer2::setParameter(%d)", key); |
| 1047 | status_t status = INVALID_OPERATION; |
| 1048 | Mutex::Autolock _l(mLock); |
| 1049 | if (checkStateForKeySet_l(key) != OK) { |
| 1050 | return status; |
| 1051 | } |
| 1052 | switch (key) { |
| 1053 | case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1054 | // save the marshalled audio attributes |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1055 | if (mAudioAttributesParcel != NULL) { |
| 1056 | delete mAudioAttributesParcel; |
| 1057 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1058 | mAudioAttributesParcel = new Parcel(); |
| 1059 | mAudioAttributesParcel->appendFrom(&request, 0, request.dataSize()); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1060 | status = setAudioAttributes_l(request); |
| 1061 | if (status != OK) { |
| 1062 | return status; |
| 1063 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1064 | break; |
| 1065 | default: |
| 1066 | ALOGV_IF(mPlayer == NULL, "setParameter: no active player"); |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | if (mPlayer != NULL) { |
| 1071 | status = mPlayer->setParameter(key, request); |
| 1072 | } |
| 1073 | return status; |
| 1074 | } |
| 1075 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1076 | status_t MediaPlayer2::getParameter(int key, Parcel *reply) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1077 | ALOGV("MediaPlayer2::getParameter(%d)", key); |
| 1078 | Mutex::Autolock _l(mLock); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 1079 | if (key == MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES) { |
| 1080 | if (reply == NULL) { |
| 1081 | return BAD_VALUE; |
| 1082 | } |
| 1083 | if (mAudioAttributesParcel != NULL) { |
| 1084 | reply->appendFrom(mAudioAttributesParcel, 0, mAudioAttributesParcel->dataSize()); |
| 1085 | } |
| 1086 | return OK; |
| 1087 | } |
| 1088 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1089 | if (mPlayer == NULL) { |
| 1090 | ALOGV("getParameter: no active player"); |
| 1091 | return INVALID_OPERATION; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1092 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1093 | |
| 1094 | status_t status = mPlayer->getParameter(key, reply); |
| 1095 | if (status != OK) { |
| 1096 | ALOGD("getParameter returns %d", status); |
| 1097 | } |
| 1098 | return status; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 1101 | void MediaPlayer2::notify(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *obj) { |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1102 | ALOGV("message received srcId=%lld, msg=%d, ext1=%d, ext2=%d", |
| 1103 | (long long)srcId, msg, ext1, ext2); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1104 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1105 | bool send = true; |
| 1106 | bool locked = false; |
| 1107 | |
| 1108 | // TODO: In the future, we might be on the same thread if the app is |
| 1109 | // running in the same process as the media server. In that case, |
| 1110 | // this will deadlock. |
| 1111 | // |
| 1112 | // The threadId hack below works around this for the care of prepare, |
| 1113 | // seekTo, start, and reset within the same process. |
| 1114 | // FIXME: Remember, this is a hack, it's not even a hack that is applied |
| 1115 | // consistently for all use-cases, this needs to be revisited. |
| 1116 | if (mLockThreadId != getThreadId()) { |
| 1117 | mLock.lock(); |
| 1118 | locked = true; |
| 1119 | } |
| 1120 | |
| 1121 | // Allows calls from JNI in idle state to notify errors |
| 1122 | if (!(msg == MEDIA2_ERROR && mCurrentState == MEDIA_PLAYER2_IDLE) && mPlayer == 0) { |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1123 | ALOGV("notify(%lld, %d, %d, %d) callback on disconnected mediaplayer", |
| 1124 | (long long)srcId, msg, ext1, ext2); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1125 | if (locked) mLock.unlock(); // release the lock when done. |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | switch (msg) { |
| 1130 | case MEDIA2_NOP: // interface test message |
| 1131 | break; |
| 1132 | case MEDIA2_PREPARED: |
| 1133 | ALOGV("MediaPlayer2::notify() prepared"); |
| 1134 | mCurrentState = MEDIA_PLAYER2_PREPARED; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1135 | break; |
| 1136 | case MEDIA2_DRM_INFO: |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1137 | ALOGV("MediaPlayer2::notify() MEDIA2_DRM_INFO(%lld, %d, %d, %d, %p)", |
| 1138 | (long long)srcId, msg, ext1, ext2, obj); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1139 | break; |
| 1140 | case MEDIA2_PLAYBACK_COMPLETE: |
| 1141 | ALOGV("playback complete"); |
| 1142 | if (mCurrentState == MEDIA_PLAYER2_IDLE) { |
| 1143 | ALOGE("playback complete in idle state"); |
| 1144 | } |
| 1145 | if (!mLoop) { |
| 1146 | mCurrentState = MEDIA_PLAYER2_PLAYBACK_COMPLETE; |
| 1147 | } |
| 1148 | break; |
| 1149 | case MEDIA2_ERROR: |
| 1150 | // Always log errors. |
| 1151 | // ext1: Media framework error code. |
| 1152 | // ext2: Implementation dependant error code. |
| 1153 | ALOGE("error (%d, %d)", ext1, ext2); |
| 1154 | mCurrentState = MEDIA_PLAYER2_STATE_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1155 | break; |
| 1156 | case MEDIA2_INFO: |
| 1157 | // ext1: Media framework error code. |
| 1158 | // ext2: Implementation dependant error code. |
| 1159 | if (ext1 != MEDIA2_INFO_VIDEO_TRACK_LAGGING) { |
| 1160 | ALOGW("info/warning (%d, %d)", ext1, ext2); |
| 1161 | } |
| 1162 | break; |
| 1163 | case MEDIA2_SEEK_COMPLETE: |
| 1164 | ALOGV("Received seek complete"); |
| 1165 | if (mSeekPosition != mCurrentPosition || (mSeekMode != mCurrentSeekMode)) { |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 1166 | ALOGV("Executing queued seekTo(%lld, %d)", |
| 1167 | (long long)mCurrentPosition, mCurrentSeekMode); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1168 | mSeekPosition = -1; |
| 1169 | mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 1170 | seekTo_l(mCurrentPosition, mCurrentSeekMode); |
| 1171 | } |
| 1172 | else { |
| 1173 | ALOGV("All seeks complete - return to regularly scheduled program"); |
| 1174 | mCurrentPosition = mSeekPosition = -1; |
| 1175 | mCurrentSeekMode = mSeekMode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC; |
| 1176 | } |
| 1177 | break; |
| 1178 | case MEDIA2_BUFFERING_UPDATE: |
| 1179 | ALOGV("buffering %d", ext1); |
| 1180 | break; |
| 1181 | case MEDIA2_SET_VIDEO_SIZE: |
| 1182 | ALOGV("New video size %d x %d", ext1, ext2); |
| 1183 | mVideoWidth = ext1; |
| 1184 | mVideoHeight = ext2; |
| 1185 | break; |
| 1186 | case MEDIA2_NOTIFY_TIME: |
| 1187 | ALOGV("Received notify time message"); |
| 1188 | break; |
| 1189 | case MEDIA2_TIMED_TEXT: |
| 1190 | ALOGV("Received timed text message"); |
| 1191 | break; |
| 1192 | case MEDIA2_SUBTITLE_DATA: |
| 1193 | ALOGV("Received subtitle data message"); |
| 1194 | break; |
| 1195 | case MEDIA2_META_DATA: |
| 1196 | ALOGV("Received timed metadata message"); |
| 1197 | break; |
| 1198 | default: |
| 1199 | ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | sp<MediaPlayer2Listener> listener = mListener; |
| 1204 | if (locked) mLock.unlock(); |
| 1205 | |
| 1206 | // this prevents re-entrant calls into client code |
| 1207 | if ((listener != 0) && send) { |
| 1208 | Mutex::Autolock _l(mNotifyLock); |
| 1209 | ALOGV("callback application"); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 1210 | listener->notify(srcId, msg, ext1, ext2, obj); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1211 | ALOGV("back from callback"); |
| 1212 | } |
| 1213 | } |
| 1214 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1215 | // Modular DRM |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1216 | 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] | 1217 | // TODO change to ALOGV |
| 1218 | ALOGD("prepareDrm: uuid: %p drmSessionId: %p(%zu)", uuid, |
| 1219 | drmSessionId.array(), drmSessionId.size()); |
| 1220 | Mutex::Autolock _l(mLock); |
| 1221 | if (mPlayer == NULL) { |
| 1222 | return NO_INIT; |
| 1223 | } |
| 1224 | |
| 1225 | // Only allowed it in player's preparing/prepared state. |
| 1226 | // We get here only if MEDIA_DRM_INFO has already arrived (e.g., prepare is half-way through or |
| 1227 | // completed) so the state change to "prepared" might not have happened yet (e.g., buffering). |
| 1228 | // Still, we can allow prepareDrm for the use case of being called in OnDrmInfoListener. |
| 1229 | if (!(mCurrentState & (MEDIA_PLAYER2_PREPARING | MEDIA_PLAYER2_PREPARED))) { |
| 1230 | ALOGE("prepareDrm is called in the wrong state (%d).", mCurrentState); |
| 1231 | return INVALID_OPERATION; |
| 1232 | } |
| 1233 | |
| 1234 | if (drmSessionId.isEmpty()) { |
| 1235 | ALOGE("prepareDrm: Unexpected. Can't proceed with crypto. Empty drmSessionId."); |
| 1236 | return INVALID_OPERATION; |
| 1237 | } |
| 1238 | |
| 1239 | // Passing down to mediaserver mainly for creating the crypto |
| 1240 | status_t status = mPlayer->prepareDrm(uuid, drmSessionId); |
| 1241 | ALOGE_IF(status != OK, "prepareDrm: Failed at mediaserver with ret: %d", status); |
| 1242 | |
| 1243 | // TODO change to ALOGV |
| 1244 | ALOGD("prepareDrm: mediaserver::prepareDrm ret=%d", status); |
| 1245 | |
| 1246 | return status; |
| 1247 | } |
| 1248 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1249 | status_t MediaPlayer2::releaseDrm() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1250 | Mutex::Autolock _l(mLock); |
| 1251 | if (mPlayer == NULL) { |
| 1252 | return NO_INIT; |
| 1253 | } |
| 1254 | |
| 1255 | // Not allowing releaseDrm in an active/resumable state |
| 1256 | if (mCurrentState & (MEDIA_PLAYER2_STARTED | |
| 1257 | MEDIA_PLAYER2_PAUSED | |
| 1258 | MEDIA_PLAYER2_PLAYBACK_COMPLETE | |
| 1259 | MEDIA_PLAYER2_STATE_ERROR)) { |
| 1260 | ALOGE("releaseDrm Unexpected state %d. Can only be called in stopped/idle.", mCurrentState); |
| 1261 | return INVALID_OPERATION; |
| 1262 | } |
| 1263 | |
| 1264 | status_t status = mPlayer->releaseDrm(); |
| 1265 | // TODO change to ALOGV |
| 1266 | ALOGD("releaseDrm: mediaserver::releaseDrm ret: %d", status); |
| 1267 | if (status != OK) { |
| 1268 | ALOGE("releaseDrm: Failed at mediaserver with ret: %d", status); |
| 1269 | // Overriding to OK so the client proceed with its own cleanup |
| 1270 | // Client can't do more cleanup. mediaserver release its crypto at end of session anyway. |
| 1271 | status = OK; |
| 1272 | } |
| 1273 | |
| 1274 | return status; |
| 1275 | } |
| 1276 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1277 | status_t MediaPlayer2::setOutputDevice(audio_port_handle_t deviceId) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1278 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1279 | if (mAudioOutput == NULL) { |
| 1280 | ALOGV("setOutputDevice: audio sink not init"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1281 | return NO_INIT; |
| 1282 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1283 | return mAudioOutput->setOutputDevice(deviceId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1286 | audio_port_handle_t MediaPlayer2::getRoutedDeviceId() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1287 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1288 | if (mAudioOutput == NULL) { |
| 1289 | ALOGV("getRoutedDeviceId: audio sink not init"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1290 | return AUDIO_PORT_HANDLE_NONE; |
| 1291 | } |
| 1292 | audio_port_handle_t deviceId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1293 | status_t status = mAudioOutput->getRoutedDeviceId(&deviceId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1294 | if (status != NO_ERROR) { |
| 1295 | return AUDIO_PORT_HANDLE_NONE; |
| 1296 | } |
| 1297 | return deviceId; |
| 1298 | } |
| 1299 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1300 | status_t MediaPlayer2::enableAudioDeviceCallback(bool enabled) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1301 | Mutex::Autolock _l(mLock); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1302 | if (mAudioOutput == NULL) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1303 | ALOGV("addAudioDeviceCallback: player not init"); |
| 1304 | return NO_INIT; |
| 1305 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 1306 | return mAudioOutput->enableAudioDeviceCallback(enabled); |
| 1307 | } |
| 1308 | |
| 1309 | status_t MediaPlayer2::dump(int fd, const Vector<String16>& args) { |
| 1310 | const size_t SIZE = 256; |
| 1311 | char buffer[SIZE]; |
| 1312 | String8 result; |
| 1313 | result.append(" MediaPlayer2\n"); |
| 1314 | snprintf(buffer, 255, " pid(%d), looping(%s)\n", mPid, mLoop?"true": "false"); |
| 1315 | result.append(buffer); |
| 1316 | |
| 1317 | sp<MediaPlayer2Interface> player; |
| 1318 | sp<MediaPlayer2AudioOutput> audioOutput; |
| 1319 | bool locked = false; |
| 1320 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1321 | if (mLock.tryLock() == NO_ERROR) { |
| 1322 | locked = true; |
| 1323 | break; |
| 1324 | } |
| 1325 | usleep(kDumpLockSleepUs); |
| 1326 | } |
| 1327 | |
| 1328 | if (locked) { |
| 1329 | player = mPlayer; |
| 1330 | audioOutput = mAudioOutput; |
| 1331 | mLock.unlock(); |
| 1332 | } else { |
| 1333 | result.append(" lock is taken, no dump from player and audio output\n"); |
| 1334 | } |
| 1335 | write(fd, result.string(), result.size()); |
| 1336 | |
| 1337 | if (player != NULL) { |
| 1338 | player->dump(fd, args); |
| 1339 | } |
| 1340 | if (audioOutput != 0) { |
| 1341 | audioOutput->dump(fd, args); |
| 1342 | } |
| 1343 | write(fd, "\n", 1); |
| 1344 | return NO_ERROR; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | } // namespace android |