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 | // Proxy for media player implementations |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #define LOG_TAG "MediaPlayer2Manager" |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <dirent.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <cutils/atomic.h> |
| 33 | #include <cutils/properties.h> // for property_get |
| 34 | |
| 35 | #include <utils/misc.h> |
| 36 | |
| 37 | #include <binder/IPCThreadState.h> |
| 38 | #include <binder/IServiceManager.h> |
| 39 | #include <binder/MemoryHeapBase.h> |
| 40 | #include <binder/MemoryBase.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 41 | #include <utils/Errors.h> // for status_t |
| 42 | #include <utils/String8.h> |
| 43 | #include <utils/SystemClock.h> |
| 44 | #include <utils/Timers.h> |
| 45 | #include <utils/Vector.h> |
| 46 | |
| 47 | #include <media/AudioPolicyHelper.h> |
| 48 | #include <media/MediaHTTPService.h> |
| 49 | #include <media/MediaPlayer2EngineClient.h> |
| 50 | #include <media/MediaPlayer2Interface.h> |
| 51 | #include <media/Metadata.h> |
| 52 | #include <media/AudioTrack.h> |
| 53 | #include <media/MemoryLeakTrackUtil.h> |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 54 | #include <media/NdkWrapper.h> |
| 55 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 56 | #include <media/stagefright/InterfaceUtils.h> |
| 57 | #include <media/stagefright/MediaCodecList.h> |
| 58 | #include <media/stagefright/MediaErrors.h> |
| 59 | #include <media/stagefright/Utils.h> |
| 60 | #include <media/stagefright/foundation/ADebug.h> |
| 61 | #include <media/stagefright/foundation/ALooperRoster.h> |
| 62 | #include <media/stagefright/SurfaceUtils.h> |
| 63 | #include <mediautils/BatteryNotifier.h> |
| 64 | |
| 65 | #include <memunreachable/memunreachable.h> |
| 66 | #include <system/audio.h> |
Wei Jia | 4049f13 | 2018-01-22 10:37:31 -0800 | [diff] [blame^] | 67 | #include <system/window.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 68 | |
| 69 | #include <private/android_filesystem_config.h> |
| 70 | |
| 71 | #include "MediaPlayer2Manager.h" |
| 72 | #include "MediaPlayer2Factory.h" |
| 73 | |
| 74 | static const int kDumpLockRetries = 50; |
| 75 | static const int kDumpLockSleepUs = 20000; |
| 76 | |
| 77 | namespace { |
| 78 | using android::media::Metadata; |
| 79 | using android::status_t; |
| 80 | using android::OK; |
| 81 | using android::BAD_VALUE; |
| 82 | using android::NOT_ENOUGH_DATA; |
| 83 | using android::Parcel; |
| 84 | using android::media::VolumeShaper; |
| 85 | |
| 86 | // Max number of entries in the filter. |
| 87 | const int kMaxFilterSize = 64; // I pulled that out of thin air. |
| 88 | |
| 89 | const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup. |
| 90 | |
| 91 | // FIXME: Move all the metadata related function in the Metadata.cpp |
| 92 | |
| 93 | |
| 94 | // Unmarshall a filter from a Parcel. |
| 95 | // Filter format in a parcel: |
| 96 | // |
| 97 | // 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 |
| 98 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 99 | // | number of entries (n) | |
| 100 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 101 | // | metadata type 1 | |
| 102 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 103 | // | metadata type 2 | |
| 104 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 105 | // .... |
| 106 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 107 | // | metadata type n | |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // |
| 110 | // @param p Parcel that should start with a filter. |
| 111 | // @param[out] filter On exit contains the list of metadata type to be |
| 112 | // filtered. |
| 113 | // @param[out] status On exit contains the status code to be returned. |
| 114 | // @return true if the parcel starts with a valid filter. |
| 115 | bool unmarshallFilter(const Parcel& p, |
| 116 | Metadata::Filter *filter, |
| 117 | status_t *status) |
| 118 | { |
| 119 | int32_t val; |
| 120 | if (p.readInt32(&val) != OK) |
| 121 | { |
| 122 | ALOGE("Failed to read filter's length"); |
| 123 | *status = NOT_ENOUGH_DATA; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | if( val > kMaxFilterSize || val < 0) |
| 128 | { |
| 129 | ALOGE("Invalid filter len %d", val); |
| 130 | *status = BAD_VALUE; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | const size_t num = val; |
| 135 | |
| 136 | filter->clear(); |
| 137 | filter->setCapacity(num); |
| 138 | |
| 139 | size_t size = num * sizeof(Metadata::Type); |
| 140 | |
| 141 | |
| 142 | if (p.dataAvail() < size) |
| 143 | { |
| 144 | ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail()); |
| 145 | *status = NOT_ENOUGH_DATA; |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | const Metadata::Type *data = |
| 150 | static_cast<const Metadata::Type*>(p.readInplace(size)); |
| 151 | |
| 152 | if (NULL == data) |
| 153 | { |
| 154 | ALOGE("Filter had no data"); |
| 155 | *status = BAD_VALUE; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // TODO: The stl impl of vector would be more efficient here |
| 160 | // because it degenerates into a memcpy on pod types. Try to |
| 161 | // replace later or use stl::set. |
| 162 | for (size_t i = 0; i < num; ++i) |
| 163 | { |
| 164 | filter->add(*data); |
| 165 | ++data; |
| 166 | } |
| 167 | *status = OK; |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | // @param filter Of metadata type. |
| 172 | // @param val To be searched. |
| 173 | // @return true if a match was found. |
| 174 | bool findMetadata(const Metadata::Filter& filter, const int32_t val) |
| 175 | { |
| 176 | // Deal with empty and ANY right away |
| 177 | if (filter.isEmpty()) return false; |
| 178 | if (filter[0] == Metadata::kAny) return true; |
| 179 | |
| 180 | return filter.indexOf(val) >= 0; |
| 181 | } |
| 182 | |
| 183 | } // anonymous namespace |
| 184 | |
| 185 | |
| 186 | namespace { |
| 187 | using android::Parcel; |
| 188 | using android::String16; |
| 189 | |
| 190 | // marshalling tag indicating flattened utf16 tags |
| 191 | // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java |
| 192 | const int32_t kAudioAttributesMarshallTagFlattenTags = 1; |
| 193 | |
| 194 | // Audio attributes format in a parcel: |
| 195 | // |
| 196 | // 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 |
| 197 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 198 | // | usage | |
| 199 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 200 | // | content_type | |
| 201 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 202 | // | source | |
| 203 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 204 | // | flags | |
| 205 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 206 | // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found |
| 207 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 208 | // | flattened tags in UTF16 | |
| 209 | // | ... | |
| 210 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 211 | // |
| 212 | // @param p Parcel that contains audio attributes. |
| 213 | // @param[out] attributes On exit points to an initialized audio_attributes_t structure |
| 214 | // @param[out] status On exit contains the status code to be returned. |
| 215 | void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes) |
| 216 | { |
| 217 | attributes->usage = (audio_usage_t) parcel.readInt32(); |
| 218 | attributes->content_type = (audio_content_type_t) parcel.readInt32(); |
| 219 | attributes->source = (audio_source_t) parcel.readInt32(); |
| 220 | attributes->flags = (audio_flags_mask_t) parcel.readInt32(); |
| 221 | const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags); |
| 222 | if (hasFlattenedTag) { |
| 223 | // the tags are UTF16, convert to UTF8 |
| 224 | String16 tags = parcel.readString16(); |
| 225 | ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size()); |
| 226 | if (realTagSize <= 0) { |
| 227 | strcpy(attributes->tags, ""); |
| 228 | } else { |
| 229 | // copy the flattened string into the attributes as the destination for the conversion: |
| 230 | // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it |
| 231 | size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ? |
| 232 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize; |
| 233 | utf16_to_utf8(tags.string(), tagSize, attributes->tags, |
| 234 | sizeof(attributes->tags) / sizeof(attributes->tags[0])); |
| 235 | } |
| 236 | } else { |
| 237 | ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values"); |
| 238 | strcpy(attributes->tags, ""); |
| 239 | } |
| 240 | } |
| 241 | } // anonymous namespace |
| 242 | |
| 243 | |
| 244 | namespace android { |
| 245 | |
| 246 | extern ALooperRoster gLooperRoster; |
| 247 | |
| 248 | MediaPlayer2Manager gMediaPlayer2Manager; |
| 249 | |
| 250 | static bool checkPermission(const char* permissionString) { |
| 251 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 252 | bool ok = checkCallingPermission(String16(permissionString)); |
| 253 | if (!ok) ALOGE("Request requires %s", permissionString); |
| 254 | return ok; |
| 255 | } |
| 256 | |
| 257 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
| 258 | /* static */ int MediaPlayer2Manager::AudioOutput::mMinBufferCount = 4; |
| 259 | /* static */ bool MediaPlayer2Manager::AudioOutput::mIsOnEmulator = false; |
| 260 | |
| 261 | // static |
| 262 | MediaPlayer2Manager& MediaPlayer2Manager::get() { |
| 263 | return gMediaPlayer2Manager; |
| 264 | } |
| 265 | |
| 266 | MediaPlayer2Manager::MediaPlayer2Manager() { |
| 267 | ALOGV("MediaPlayer2Manager created"); |
| 268 | // TODO: remove all unnecessary pid/uid handling. |
| 269 | mPid = IPCThreadState::self()->getCallingPid(); |
| 270 | mUid = IPCThreadState::self()->getCallingUid(); |
| 271 | mNextConnId = 1; |
| 272 | |
| 273 | MediaPlayer2Factory::registerBuiltinFactories(); |
| 274 | } |
| 275 | |
| 276 | MediaPlayer2Manager::~MediaPlayer2Manager() { |
| 277 | ALOGV("MediaPlayer2Manager destroyed"); |
| 278 | } |
| 279 | |
| 280 | sp<MediaPlayer2Engine> MediaPlayer2Manager::create( |
| 281 | const sp<MediaPlayer2EngineClient>& client, |
| 282 | audio_session_t audioSessionId) |
| 283 | { |
| 284 | int32_t connId = android_atomic_inc(&mNextConnId); |
| 285 | |
| 286 | sp<Client> c = new Client( |
| 287 | mPid, connId, client, audioSessionId, mUid); |
| 288 | |
| 289 | ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, mPid, mUid); |
| 290 | |
| 291 | wp<Client> w = c; |
| 292 | { |
| 293 | Mutex::Autolock lock(mLock); |
| 294 | mClients.add(w); |
| 295 | } |
| 296 | return c; |
| 297 | } |
| 298 | |
| 299 | status_t MediaPlayer2Manager::AudioOutput::dump(int fd, const Vector<String16>& args) const |
| 300 | { |
| 301 | const size_t SIZE = 256; |
| 302 | char buffer[SIZE]; |
| 303 | String8 result; |
| 304 | |
| 305 | result.append(" AudioOutput\n"); |
| 306 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", |
| 307 | mStreamType, mLeftVolume, mRightVolume); |
| 308 | result.append(buffer); |
| 309 | snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n", |
| 310 | mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1); |
| 311 | result.append(buffer); |
| 312 | snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n", |
| 313 | mAuxEffectId, mSendLevel); |
| 314 | result.append(buffer); |
| 315 | |
| 316 | ::write(fd, result.string(), result.size()); |
| 317 | if (mTrack != 0) { |
| 318 | mTrack->dump(fd, args); |
| 319 | } |
| 320 | return NO_ERROR; |
| 321 | } |
| 322 | |
| 323 | status_t MediaPlayer2Manager::Client::dump(int fd, const Vector<String16>& args) |
| 324 | { |
| 325 | const size_t SIZE = 256; |
| 326 | char buffer[SIZE]; |
| 327 | String8 result; |
| 328 | result.append(" Client\n"); |
| 329 | snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n", |
| 330 | mPid, mConnId, mStatus, mLoop?"true": "false"); |
| 331 | result.append(buffer); |
| 332 | |
| 333 | sp<MediaPlayer2Base> p; |
| 334 | sp<AudioOutput> audioOutput; |
| 335 | bool locked = false; |
| 336 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 337 | if (mLock.tryLock() == NO_ERROR) { |
| 338 | locked = true; |
| 339 | break; |
| 340 | } |
| 341 | usleep(kDumpLockSleepUs); |
| 342 | } |
| 343 | |
| 344 | if (locked) { |
| 345 | p = mPlayer; |
| 346 | audioOutput = mAudioOutput; |
| 347 | mLock.unlock(); |
| 348 | } else { |
| 349 | result.append(" lock is taken, no dump from player and audio output\n"); |
| 350 | } |
| 351 | write(fd, result.string(), result.size()); |
| 352 | |
| 353 | if (p != NULL) { |
| 354 | p->dump(fd, args); |
| 355 | } |
| 356 | if (audioOutput != 0) { |
| 357 | audioOutput->dump(fd, args); |
| 358 | } |
| 359 | write(fd, "\n", 1); |
| 360 | return NO_ERROR; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * The only arguments this understands right now are -c, -von and -voff, |
| 365 | * which are parsed by ALooperRoster::dump() |
| 366 | */ |
| 367 | status_t MediaPlayer2Manager::dump(int fd, const Vector<String16>& args) |
| 368 | { |
| 369 | const size_t SIZE = 256; |
| 370 | char buffer[SIZE]; |
| 371 | String8 result; |
| 372 | SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction. |
| 373 | |
| 374 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 375 | snprintf(buffer, SIZE, "Permission Denial: " |
| 376 | "can't dump MediaPlayer2Manager from pid=%d, uid=%d\n", |
| 377 | mPid, mUid); |
| 378 | result.append(buffer); |
| 379 | } else { |
| 380 | Mutex::Autolock lock(mLock); |
| 381 | for (int i = 0, n = mClients.size(); i < n; ++i) { |
| 382 | sp<Client> c = mClients[i].promote(); |
| 383 | if (c != 0) c->dump(fd, args); |
| 384 | clients.add(c); |
| 385 | } |
| 386 | |
| 387 | result.append(" Files opened and/or mapped:\n"); |
| 388 | snprintf(buffer, SIZE, "/proc/%d/maps", getpid()); |
| 389 | FILE *f = fopen(buffer, "r"); |
| 390 | if (f) { |
| 391 | while (!feof(f)) { |
| 392 | fgets(buffer, SIZE, f); |
| 393 | if (strstr(buffer, " /storage/") || |
| 394 | strstr(buffer, " /system/sounds/") || |
| 395 | strstr(buffer, " /data/") || |
| 396 | strstr(buffer, " /system/media/")) { |
| 397 | result.append(" "); |
| 398 | result.append(buffer); |
| 399 | } |
| 400 | } |
| 401 | fclose(f); |
| 402 | } else { |
| 403 | result.append("couldn't open "); |
| 404 | result.append(buffer); |
| 405 | result.append("\n"); |
| 406 | } |
| 407 | |
| 408 | snprintf(buffer, SIZE, "/proc/%d/fd", getpid()); |
| 409 | DIR *d = opendir(buffer); |
| 410 | if (d) { |
| 411 | struct dirent *ent; |
| 412 | while((ent = readdir(d)) != NULL) { |
| 413 | if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) { |
| 414 | snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name); |
| 415 | struct stat s; |
| 416 | if (lstat(buffer, &s) == 0) { |
| 417 | if ((s.st_mode & S_IFMT) == S_IFLNK) { |
| 418 | char linkto[256]; |
| 419 | int len = readlink(buffer, linkto, sizeof(linkto)); |
| 420 | if(len > 0) { |
| 421 | if(len > 255) { |
| 422 | linkto[252] = '.'; |
| 423 | linkto[253] = '.'; |
| 424 | linkto[254] = '.'; |
| 425 | linkto[255] = 0; |
| 426 | } else { |
| 427 | linkto[len] = 0; |
| 428 | } |
| 429 | if (strstr(linkto, "/storage/") == linkto || |
| 430 | strstr(linkto, "/system/sounds/") == linkto || |
| 431 | strstr(linkto, "/data/") == linkto || |
| 432 | strstr(linkto, "/system/media/") == linkto) { |
| 433 | result.append(" "); |
| 434 | result.append(buffer); |
| 435 | result.append(" -> "); |
| 436 | result.append(linkto); |
| 437 | result.append("\n"); |
| 438 | } |
| 439 | } |
| 440 | } else { |
| 441 | result.append(" unexpected type for "); |
| 442 | result.append(buffer); |
| 443 | result.append("\n"); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | closedir(d); |
| 449 | } else { |
| 450 | result.append("couldn't open "); |
| 451 | result.append(buffer); |
| 452 | result.append("\n"); |
| 453 | } |
| 454 | |
| 455 | gLooperRoster.dump(fd, args); |
| 456 | |
| 457 | bool dumpMem = false; |
| 458 | bool unreachableMemory = false; |
| 459 | for (size_t i = 0; i < args.size(); i++) { |
| 460 | if (args[i] == String16("-m")) { |
| 461 | dumpMem = true; |
| 462 | } else if (args[i] == String16("--unreachable")) { |
| 463 | unreachableMemory = true; |
| 464 | } |
| 465 | } |
| 466 | if (dumpMem) { |
| 467 | result.append("\nDumping memory:\n"); |
| 468 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 469 | result.append(s.c_str(), s.size()); |
| 470 | } |
| 471 | if (unreachableMemory) { |
| 472 | result.append("\nDumping unreachable memory:\n"); |
| 473 | // TODO - should limit be an argument parameter? |
Wei Jia | 4049f13 | 2018-01-22 10:37:31 -0800 | [diff] [blame^] | 474 | // TODO: enable GetUnreachableMemoryString if it's part of stable API |
| 475 | //std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */); |
| 476 | //result.append(s.c_str(), s.size()); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | write(fd, result.string(), result.size()); |
| 480 | return NO_ERROR; |
| 481 | } |
| 482 | |
| 483 | void MediaPlayer2Manager::removeClient(const wp<Client>& client) |
| 484 | { |
| 485 | Mutex::Autolock lock(mLock); |
| 486 | mClients.remove(client); |
| 487 | } |
| 488 | |
| 489 | bool MediaPlayer2Manager::hasClient(wp<Client> client) |
| 490 | { |
| 491 | Mutex::Autolock lock(mLock); |
| 492 | return mClients.indexOf(client) != NAME_NOT_FOUND; |
| 493 | } |
| 494 | |
| 495 | MediaPlayer2Manager::Client::Client( |
| 496 | pid_t pid, |
| 497 | int32_t connId, |
| 498 | const sp<MediaPlayer2EngineClient>& client, |
| 499 | audio_session_t audioSessionId, |
| 500 | uid_t uid) |
| 501 | { |
| 502 | ALOGV("Client(%d) constructor", connId); |
| 503 | mPid = pid; |
| 504 | mConnId = connId; |
| 505 | mClient = client; |
| 506 | mLoop = false; |
| 507 | mStatus = NO_INIT; |
| 508 | mAudioSessionId = audioSessionId; |
| 509 | mUid = uid; |
| 510 | mRetransmitEndpointValid = false; |
| 511 | mAudioAttributes = NULL; |
| 512 | |
| 513 | #if CALLBACK_ANTAGONIZER |
| 514 | ALOGD("create Antagonizer"); |
| 515 | mAntagonizer = new Antagonizer(notify, this); |
| 516 | #endif |
| 517 | } |
| 518 | |
| 519 | MediaPlayer2Manager::Client::~Client() |
| 520 | { |
| 521 | ALOGV("Client(%d) destructor pid = %d", mConnId, mPid); |
| 522 | mAudioOutput.clear(); |
| 523 | wp<Client> client(this); |
| 524 | disconnect(); |
| 525 | gMediaPlayer2Manager.removeClient(client); |
| 526 | if (mAudioAttributes != NULL) { |
| 527 | free(mAudioAttributes); |
| 528 | } |
| 529 | mAudioDeviceUpdatedListener.clear(); |
| 530 | } |
| 531 | |
| 532 | void MediaPlayer2Manager::Client::disconnect() |
| 533 | { |
| 534 | ALOGV("disconnect(%d) from pid %d", mConnId, mPid); |
| 535 | // grab local reference and clear main reference to prevent future |
| 536 | // access to object |
| 537 | sp<MediaPlayer2Base> p; |
| 538 | { |
| 539 | Mutex::Autolock l(mLock); |
| 540 | p = mPlayer; |
| 541 | mClient.clear(); |
| 542 | mPlayer.clear(); |
| 543 | } |
| 544 | |
| 545 | // clear the notification to prevent callbacks to dead client |
| 546 | // and reset the player. We assume the player will serialize |
| 547 | // access to itself if necessary. |
| 548 | if (p != 0) { |
| 549 | p->setNotifyCallback(0, 0); |
| 550 | #if CALLBACK_ANTAGONIZER |
| 551 | ALOGD("kill Antagonizer"); |
| 552 | mAntagonizer->kill(); |
| 553 | #endif |
| 554 | p->reset(); |
| 555 | } |
| 556 | |
| 557 | { |
| 558 | Mutex::Autolock l(mLock); |
| 559 | disconnectNativeWindow_l(); |
| 560 | } |
| 561 | |
| 562 | IPCThreadState::self()->flushCommands(); |
| 563 | } |
| 564 | |
| 565 | sp<MediaPlayer2Base> MediaPlayer2Manager::Client::createPlayer(player2_type playerType) |
| 566 | { |
| 567 | // determine if we have the right player type |
| 568 | sp<MediaPlayer2Base> p = getPlayer(); |
| 569 | if ((p != NULL) && (p->playerType() != playerType)) { |
| 570 | ALOGV("delete player"); |
| 571 | p.clear(); |
| 572 | } |
| 573 | if (p == NULL) { |
| 574 | p = MediaPlayer2Factory::createPlayer(playerType, this, notify, mPid); |
| 575 | } |
| 576 | |
| 577 | if (p != NULL) { |
| 578 | p->setUID(mUid); |
| 579 | } |
| 580 | |
| 581 | return p; |
| 582 | } |
| 583 | |
| 584 | void MediaPlayer2Manager::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate( |
| 585 | audio_io_handle_t audioIo, |
| 586 | audio_port_handle_t deviceId) { |
| 587 | sp<MediaPlayer2Base> listener = mListener.promote(); |
| 588 | if (listener != NULL) { |
| 589 | listener->sendEvent(MEDIA2_AUDIO_ROUTING_CHANGED, audioIo, deviceId); |
| 590 | } else { |
| 591 | ALOGW("listener for process %d death is gone", MEDIA2_AUDIO_ROUTING_CHANGED); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | sp<MediaPlayer2Base> MediaPlayer2Manager::Client::setDataSource_pre( |
| 596 | player2_type playerType) |
| 597 | { |
| 598 | ALOGV("player type = %d", playerType); |
| 599 | |
| 600 | // create the right type of player |
| 601 | sp<MediaPlayer2Base> p = createPlayer(playerType); |
| 602 | if (p == NULL) { |
| 603 | return p; |
| 604 | } |
| 605 | |
| 606 | Mutex::Autolock lock(mLock); |
| 607 | |
| 608 | mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p); |
| 609 | |
| 610 | if (!p->hardwareOutput()) { |
| 611 | mAudioOutput = new AudioOutput(mAudioSessionId, mUid, |
| 612 | mPid, mAudioAttributes, mAudioDeviceUpdatedListener); |
| 613 | static_cast<MediaPlayer2Interface*>(p.get())->setAudioSink(mAudioOutput); |
| 614 | } |
| 615 | |
| 616 | return p; |
| 617 | } |
| 618 | |
| 619 | status_t MediaPlayer2Manager::Client::setDataSource_post( |
| 620 | const sp<MediaPlayer2Base>& p, |
| 621 | status_t status) |
| 622 | { |
| 623 | ALOGV(" setDataSource"); |
| 624 | if (status != OK) { |
| 625 | ALOGE(" error: %d", status); |
| 626 | return status; |
| 627 | } |
| 628 | |
| 629 | // Set the re-transmission endpoint if one was chosen. |
| 630 | if (mRetransmitEndpointValid) { |
| 631 | status = p->setRetransmitEndpoint(&mRetransmitEndpoint); |
| 632 | if (status != NO_ERROR) { |
| 633 | ALOGE("setRetransmitEndpoint error: %d", status); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (status == OK) { |
| 638 | Mutex::Autolock lock(mLock); |
| 639 | mPlayer = p; |
| 640 | } |
| 641 | return status; |
| 642 | } |
| 643 | |
| 644 | status_t MediaPlayer2Manager::Client::setDataSource( |
| 645 | const sp<MediaHTTPService> &httpService, |
| 646 | const char *url, |
| 647 | const KeyedVector<String8, String8> *headers) |
| 648 | { |
| 649 | ALOGV("setDataSource(%s)", url); |
| 650 | if (url == NULL) |
| 651 | return UNKNOWN_ERROR; |
| 652 | |
| 653 | if ((strncmp(url, "http://", 7) == 0) || |
| 654 | (strncmp(url, "https://", 8) == 0) || |
| 655 | (strncmp(url, "rtsp://", 7) == 0)) { |
| 656 | if (!checkPermission("android.permission.INTERNET")) { |
| 657 | return PERMISSION_DENIED; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if (strncmp(url, "content://", 10) == 0) { |
| 662 | ALOGE("setDataSource: content scheme is not supported here"); |
| 663 | mStatus = UNKNOWN_ERROR; |
| 664 | return mStatus; |
| 665 | } else { |
| 666 | player2_type playerType = MediaPlayer2Factory::getPlayerType(this, url); |
| 667 | sp<MediaPlayer2Base> p = setDataSource_pre(playerType); |
| 668 | if (p == NULL) { |
| 669 | return NO_INIT; |
| 670 | } |
| 671 | |
| 672 | return mStatus = |
| 673 | setDataSource_post( |
| 674 | p, p->setDataSource(httpService, url, headers)); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | status_t MediaPlayer2Manager::Client::setDataSource(int fd, int64_t offset, int64_t length) |
| 679 | { |
| 680 | ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld", |
| 681 | fd, nameForFd(fd).c_str(), (long long) offset, (long long) length); |
| 682 | struct stat sb; |
| 683 | int ret = fstat(fd, &sb); |
| 684 | if (ret != 0) { |
| 685 | ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); |
| 686 | return UNKNOWN_ERROR; |
| 687 | } |
| 688 | |
| 689 | ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev)); |
| 690 | ALOGV("st_mode = %u", sb.st_mode); |
| 691 | ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid)); |
| 692 | ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid)); |
| 693 | ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size)); |
| 694 | |
| 695 | if (offset >= sb.st_size) { |
| 696 | ALOGE("offset error"); |
| 697 | return UNKNOWN_ERROR; |
| 698 | } |
| 699 | if (offset + length > sb.st_size) { |
| 700 | length = sb.st_size - offset; |
| 701 | ALOGV("calculated length = %lld", (long long)length); |
| 702 | } |
| 703 | |
| 704 | player2_type playerType = MediaPlayer2Factory::getPlayerType(this, |
| 705 | fd, |
| 706 | offset, |
| 707 | length); |
| 708 | sp<MediaPlayer2Base> p = setDataSource_pre(playerType); |
| 709 | if (p == NULL) { |
| 710 | return NO_INIT; |
| 711 | } |
| 712 | |
| 713 | // now set data source |
| 714 | return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length)); |
| 715 | } |
| 716 | |
| 717 | status_t MediaPlayer2Manager::Client::setDataSource( |
| 718 | const sp<IStreamSource> &source) { |
| 719 | // create the right type of player |
| 720 | player2_type playerType = MediaPlayer2Factory::getPlayerType(this, source); |
| 721 | sp<MediaPlayer2Base> p = setDataSource_pre(playerType); |
| 722 | if (p == NULL) { |
| 723 | return NO_INIT; |
| 724 | } |
| 725 | |
| 726 | // now set data source |
| 727 | return mStatus = setDataSource_post(p, p->setDataSource(source)); |
| 728 | } |
| 729 | |
| 730 | status_t MediaPlayer2Manager::Client::setDataSource( |
Wei Jia | c5c79da | 2017-12-21 18:03:05 -0800 | [diff] [blame] | 731 | const sp<DataSource> &source) { |
| 732 | player2_type playerType = MediaPlayer2Factory::getPlayerType(this, source); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 733 | sp<MediaPlayer2Base> p = setDataSource_pre(playerType); |
| 734 | if (p == NULL) { |
| 735 | return NO_INIT; |
| 736 | } |
| 737 | // now set data source |
Wei Jia | c5c79da | 2017-12-21 18:03:05 -0800 | [diff] [blame] | 738 | return mStatus = setDataSource_post(p, p->setDataSource(source)); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void MediaPlayer2Manager::Client::disconnectNativeWindow_l() { |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 742 | if (mConnectedWindow != NULL && mConnectedWindow->getANativeWindow() != NULL) { |
Wei Jia | 4049f13 | 2018-01-22 10:37:31 -0800 | [diff] [blame^] | 743 | status_t err = native_window_api_disconnect( |
| 744 | mConnectedWindow->getANativeWindow(), NATIVE_WINDOW_API_MEDIA); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 745 | |
| 746 | if (err != OK) { |
| 747 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
| 748 | strerror(-err), err); |
| 749 | } |
| 750 | } |
| 751 | mConnectedWindow.clear(); |
| 752 | } |
| 753 | |
| 754 | status_t MediaPlayer2Manager::Client::setVideoSurfaceTexture( |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 755 | const sp<ANativeWindowWrapper>& nww) |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 756 | { |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 757 | ALOGV("[%d] setVideoSurfaceTexture(%p)", |
| 758 | mConnId, |
| 759 | (nww == NULL ? NULL : nww->getANativeWindow())); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 760 | sp<MediaPlayer2Base> p = getPlayer(); |
| 761 | if (p == 0) return UNKNOWN_ERROR; |
| 762 | |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 763 | if (nww != NULL && nww->getANativeWindow() != NULL) { |
| 764 | if (mConnectedWindow != NULL |
| 765 | && mConnectedWindow->getANativeWindow() == nww->getANativeWindow()) { |
| 766 | return OK; |
| 767 | } |
Wei Jia | 4049f13 | 2018-01-22 10:37:31 -0800 | [diff] [blame^] | 768 | status_t err = native_window_api_connect( |
| 769 | nww->getANativeWindow(), NATIVE_WINDOW_API_MEDIA); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 770 | |
| 771 | if (err != OK) { |
| 772 | ALOGE("setVideoSurfaceTexture failed: %d", err); |
| 773 | // Note that we must do the reset before disconnecting from the ANW. |
| 774 | // Otherwise queue/dequeue calls could be made on the disconnected |
| 775 | // ANW, which may result in errors. |
| 776 | reset(); |
| 777 | |
| 778 | Mutex::Autolock lock(mLock); |
| 779 | disconnectNativeWindow_l(); |
| 780 | |
| 781 | return err; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // Note that we must set the player's new GraphicBufferProducer before |
| 786 | // disconnecting the old one. Otherwise queue/dequeue calls could be made |
| 787 | // on the disconnected ANW, which may result in errors. |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 788 | status_t err = p->setVideoSurfaceTexture(nww); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 789 | |
| 790 | mLock.lock(); |
| 791 | disconnectNativeWindow_l(); |
| 792 | |
| 793 | if (err == OK) { |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 794 | mConnectedWindow = nww; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 795 | mLock.unlock(); |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 796 | } else if (nww != NULL) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 797 | mLock.unlock(); |
Wei Jia | 4049f13 | 2018-01-22 10:37:31 -0800 | [diff] [blame^] | 798 | status_t err = native_window_api_disconnect( |
| 799 | nww->getANativeWindow(), NATIVE_WINDOW_API_MEDIA); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 800 | |
| 801 | if (err != OK) { |
| 802 | ALOGW("nativeWindowDisconnect returned an error: %s (%d)", |
| 803 | strerror(-err), err); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return err; |
| 808 | } |
| 809 | |
| 810 | status_t MediaPlayer2Manager::Client::invoke(const Parcel& request, |
| 811 | Parcel *reply) |
| 812 | { |
| 813 | sp<MediaPlayer2Base> p = getPlayer(); |
| 814 | if (p == NULL) return UNKNOWN_ERROR; |
| 815 | return p->invoke(request, reply); |
| 816 | } |
| 817 | |
| 818 | // This call doesn't need to access the native player. |
| 819 | status_t MediaPlayer2Manager::Client::setMetadataFilter(const Parcel& filter) |
| 820 | { |
| 821 | status_t status; |
| 822 | media::Metadata::Filter allow, drop; |
| 823 | |
| 824 | if (unmarshallFilter(filter, &allow, &status) && |
| 825 | unmarshallFilter(filter, &drop, &status)) { |
| 826 | Mutex::Autolock lock(mLock); |
| 827 | |
| 828 | mMetadataAllow = allow; |
| 829 | mMetadataDrop = drop; |
| 830 | } |
| 831 | return status; |
| 832 | } |
| 833 | |
| 834 | status_t MediaPlayer2Manager::Client::getMetadata( |
| 835 | bool update_only, bool /*apply_filter*/, Parcel *reply) |
| 836 | { |
| 837 | sp<MediaPlayer2Base> player = getPlayer(); |
| 838 | if (player == 0) return UNKNOWN_ERROR; |
| 839 | |
| 840 | status_t status; |
| 841 | // Placeholder for the return code, updated by the caller. |
| 842 | reply->writeInt32(-1); |
| 843 | |
| 844 | media::Metadata::Filter ids; |
| 845 | |
| 846 | // We don't block notifications while we fetch the data. We clear |
| 847 | // mMetadataUpdated first so we don't lose notifications happening |
| 848 | // during the rest of this call. |
| 849 | { |
| 850 | Mutex::Autolock lock(mLock); |
| 851 | if (update_only) { |
| 852 | ids = mMetadataUpdated; |
| 853 | } |
| 854 | mMetadataUpdated.clear(); |
| 855 | } |
| 856 | |
| 857 | media::Metadata metadata(reply); |
| 858 | |
| 859 | metadata.appendHeader(); |
| 860 | status = player->getMetadata(ids, reply); |
| 861 | |
| 862 | if (status != OK) { |
| 863 | metadata.resetParcel(); |
| 864 | ALOGE("getMetadata failed %d", status); |
| 865 | return status; |
| 866 | } |
| 867 | |
| 868 | // FIXME: ement filtering on the result. Not critical since |
| 869 | // filtering takes place on the update notifications already. This |
| 870 | // would be when all the metadata are fetch and a filter is set. |
| 871 | |
| 872 | // Everything is fine, update the metadata length. |
| 873 | metadata.updateLength(); |
| 874 | return OK; |
| 875 | } |
| 876 | |
| 877 | status_t MediaPlayer2Manager::Client::setBufferingSettings( |
| 878 | const BufferingSettings& buffering) |
| 879 | { |
| 880 | ALOGV("[%d] setBufferingSettings{%s}", |
| 881 | mConnId, buffering.toString().string()); |
| 882 | sp<MediaPlayer2Base> p = getPlayer(); |
| 883 | if (p == 0) return UNKNOWN_ERROR; |
| 884 | return p->setBufferingSettings(buffering); |
| 885 | } |
| 886 | |
| 887 | status_t MediaPlayer2Manager::Client::getBufferingSettings( |
| 888 | BufferingSettings* buffering /* nonnull */) |
| 889 | { |
| 890 | sp<MediaPlayer2Base> p = getPlayer(); |
| 891 | // TODO: create mPlayer on demand. |
| 892 | if (p == 0) return UNKNOWN_ERROR; |
| 893 | status_t ret = p->getBufferingSettings(buffering); |
| 894 | if (ret == NO_ERROR) { |
| 895 | ALOGV("[%d] getBufferingSettings{%s}", |
| 896 | mConnId, buffering->toString().string()); |
| 897 | } else { |
| 898 | ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret); |
| 899 | } |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | status_t MediaPlayer2Manager::Client::prepareAsync() |
| 904 | { |
| 905 | ALOGV("[%d] prepareAsync", mConnId); |
| 906 | sp<MediaPlayer2Base> p = getPlayer(); |
| 907 | if (p == 0) return UNKNOWN_ERROR; |
| 908 | status_t ret = p->prepareAsync(); |
| 909 | #if CALLBACK_ANTAGONIZER |
| 910 | ALOGD("start Antagonizer"); |
| 911 | if (ret == NO_ERROR) mAntagonizer->start(); |
| 912 | #endif |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | status_t MediaPlayer2Manager::Client::start() |
| 917 | { |
| 918 | ALOGV("[%d] start", mConnId); |
| 919 | sp<MediaPlayer2Base> p = getPlayer(); |
| 920 | if (p == 0) return UNKNOWN_ERROR; |
| 921 | p->setLooping(mLoop); |
| 922 | return p->start(); |
| 923 | } |
| 924 | |
| 925 | status_t MediaPlayer2Manager::Client::stop() |
| 926 | { |
| 927 | ALOGV("[%d] stop", mConnId); |
| 928 | sp<MediaPlayer2Base> p = getPlayer(); |
| 929 | if (p == 0) return UNKNOWN_ERROR; |
| 930 | return p->stop(); |
| 931 | } |
| 932 | |
| 933 | status_t MediaPlayer2Manager::Client::pause() |
| 934 | { |
| 935 | ALOGV("[%d] pause", mConnId); |
| 936 | sp<MediaPlayer2Base> p = getPlayer(); |
| 937 | if (p == 0) return UNKNOWN_ERROR; |
| 938 | return p->pause(); |
| 939 | } |
| 940 | |
| 941 | status_t MediaPlayer2Manager::Client::isPlaying(bool* state) |
| 942 | { |
| 943 | *state = false; |
| 944 | sp<MediaPlayer2Base> p = getPlayer(); |
| 945 | if (p == 0) return UNKNOWN_ERROR; |
| 946 | *state = p->isPlaying(); |
| 947 | ALOGV("[%d] isPlaying: %d", mConnId, *state); |
| 948 | return NO_ERROR; |
| 949 | } |
| 950 | |
| 951 | status_t MediaPlayer2Manager::Client::setPlaybackSettings(const AudioPlaybackRate& rate) |
| 952 | { |
| 953 | ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)", |
| 954 | mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
| 955 | sp<MediaPlayer2Base> p = getPlayer(); |
| 956 | if (p == 0) return UNKNOWN_ERROR; |
| 957 | return p->setPlaybackSettings(rate); |
| 958 | } |
| 959 | |
| 960 | status_t MediaPlayer2Manager::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) |
| 961 | { |
| 962 | sp<MediaPlayer2Base> p = getPlayer(); |
| 963 | if (p == 0) return UNKNOWN_ERROR; |
| 964 | status_t ret = p->getPlaybackSettings(rate); |
| 965 | if (ret == NO_ERROR) { |
| 966 | ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)", |
| 967 | mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode); |
| 968 | } else { |
| 969 | ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret); |
| 970 | } |
| 971 | return ret; |
| 972 | } |
| 973 | |
| 974 | status_t MediaPlayer2Manager::Client::setSyncSettings( |
| 975 | const AVSyncSettings& sync, float videoFpsHint) |
| 976 | { |
| 977 | ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)", |
| 978 | mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint); |
| 979 | sp<MediaPlayer2Base> p = getPlayer(); |
| 980 | if (p == 0) return UNKNOWN_ERROR; |
| 981 | return p->setSyncSettings(sync, videoFpsHint); |
| 982 | } |
| 983 | |
| 984 | status_t MediaPlayer2Manager::Client::getSyncSettings( |
| 985 | AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) |
| 986 | { |
| 987 | sp<MediaPlayer2Base> p = getPlayer(); |
| 988 | if (p == 0) return UNKNOWN_ERROR; |
| 989 | status_t ret = p->getSyncSettings(sync, videoFps); |
| 990 | if (ret == NO_ERROR) { |
| 991 | ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)", |
| 992 | mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps); |
| 993 | } else { |
| 994 | ALOGV("[%d] getSyncSettings returned %d", mConnId, ret); |
| 995 | } |
| 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | status_t MediaPlayer2Manager::Client::getCurrentPosition(int *msec) |
| 1000 | { |
| 1001 | ALOGV("getCurrentPosition"); |
| 1002 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1003 | if (p == 0) return UNKNOWN_ERROR; |
| 1004 | status_t ret = p->getCurrentPosition(msec); |
| 1005 | if (ret == NO_ERROR) { |
| 1006 | ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec); |
| 1007 | } else { |
| 1008 | ALOGE("getCurrentPosition returned %d", ret); |
| 1009 | } |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | status_t MediaPlayer2Manager::Client::getDuration(int *msec) |
| 1014 | { |
| 1015 | ALOGV("getDuration"); |
| 1016 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1017 | if (p == 0) return UNKNOWN_ERROR; |
| 1018 | status_t ret = p->getDuration(msec); |
| 1019 | if (ret == NO_ERROR) { |
| 1020 | ALOGV("[%d] getDuration = %d", mConnId, *msec); |
| 1021 | } else { |
| 1022 | ALOGE("getDuration returned %d", ret); |
| 1023 | } |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | status_t MediaPlayer2Manager::Client::setNextPlayer(const sp<MediaPlayer2Engine>& player) { |
| 1028 | ALOGV("setNextPlayer"); |
| 1029 | Mutex::Autolock l(mLock); |
| 1030 | sp<Client> c = static_cast<Client*>(player.get()); |
| 1031 | if (c != NULL && !gMediaPlayer2Manager.hasClient(c)) { |
| 1032 | return BAD_VALUE; |
| 1033 | } |
| 1034 | |
| 1035 | mNextClient = c; |
| 1036 | |
| 1037 | if (c != NULL) { |
| 1038 | if (mAudioOutput != NULL) { |
| 1039 | mAudioOutput->setNextOutput(c->mAudioOutput); |
| 1040 | } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) { |
| 1041 | ALOGE("no current audio output"); |
| 1042 | } |
| 1043 | |
| 1044 | if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) { |
| 1045 | mPlayer->setNextPlayer(mNextClient->getPlayer()); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return OK; |
| 1050 | } |
| 1051 | |
| 1052 | VolumeShaper::Status MediaPlayer2Manager::Client::applyVolumeShaper( |
| 1053 | const sp<VolumeShaper::Configuration>& configuration, |
| 1054 | const sp<VolumeShaper::Operation>& operation) { |
| 1055 | // for hardware output, call player instead |
| 1056 | ALOGV("Client::applyVolumeShaper(%p)", this); |
| 1057 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1058 | { |
| 1059 | Mutex::Autolock l(mLock); |
| 1060 | if (p != 0 && p->hardwareOutput()) { |
| 1061 | // TODO: investigate internal implementation |
| 1062 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1063 | } |
| 1064 | if (mAudioOutput.get() != nullptr) { |
| 1065 | return mAudioOutput->applyVolumeShaper(configuration, operation); |
| 1066 | } |
| 1067 | } |
| 1068 | return VolumeShaper::Status(INVALID_OPERATION); |
| 1069 | } |
| 1070 | |
| 1071 | sp<VolumeShaper::State> MediaPlayer2Manager::Client::getVolumeShaperState(int id) { |
| 1072 | // for hardware output, call player instead |
| 1073 | ALOGV("Client::getVolumeShaperState(%p)", this); |
| 1074 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1075 | { |
| 1076 | Mutex::Autolock l(mLock); |
| 1077 | if (p != 0 && p->hardwareOutput()) { |
| 1078 | // TODO: investigate internal implementation. |
| 1079 | return nullptr; |
| 1080 | } |
| 1081 | if (mAudioOutput.get() != nullptr) { |
| 1082 | return mAudioOutput->getVolumeShaperState(id); |
| 1083 | } |
| 1084 | } |
| 1085 | return nullptr; |
| 1086 | } |
| 1087 | |
| 1088 | status_t MediaPlayer2Manager::Client::seekTo(int msec, MediaPlayer2SeekMode mode) |
| 1089 | { |
| 1090 | ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode); |
| 1091 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1092 | if (p == 0) return UNKNOWN_ERROR; |
| 1093 | return p->seekTo(msec, mode); |
| 1094 | } |
| 1095 | |
| 1096 | status_t MediaPlayer2Manager::Client::reset() |
| 1097 | { |
| 1098 | ALOGV("[%d] reset", mConnId); |
| 1099 | mRetransmitEndpointValid = false; |
| 1100 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1101 | if (p == 0) return UNKNOWN_ERROR; |
| 1102 | return p->reset(); |
| 1103 | } |
| 1104 | |
| 1105 | status_t MediaPlayer2Manager::Client::notifyAt(int64_t mediaTimeUs) |
| 1106 | { |
| 1107 | ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs); |
| 1108 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1109 | if (p == 0) return UNKNOWN_ERROR; |
| 1110 | return p->notifyAt(mediaTimeUs); |
| 1111 | } |
| 1112 | |
| 1113 | status_t MediaPlayer2Manager::Client::setAudioStreamType(audio_stream_type_t type) |
| 1114 | { |
| 1115 | ALOGV("[%d] setAudioStreamType(%d)", mConnId, type); |
| 1116 | // TODO: for hardware output, call player instead |
| 1117 | Mutex::Autolock l(mLock); |
| 1118 | if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type); |
| 1119 | return NO_ERROR; |
| 1120 | } |
| 1121 | |
| 1122 | status_t MediaPlayer2Manager::Client::setAudioAttributes_l(const Parcel &parcel) |
| 1123 | { |
| 1124 | if (mAudioAttributes != NULL) { free(mAudioAttributes); } |
| 1125 | mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1126 | if (mAudioAttributes == NULL) { |
| 1127 | return NO_MEMORY; |
| 1128 | } |
| 1129 | unmarshallAudioAttributes(parcel, mAudioAttributes); |
| 1130 | |
| 1131 | ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s", |
| 1132 | mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags, |
| 1133 | mAudioAttributes->tags); |
| 1134 | |
| 1135 | if (mAudioOutput != 0) { |
| 1136 | mAudioOutput->setAudioAttributes(mAudioAttributes); |
| 1137 | } |
| 1138 | return NO_ERROR; |
| 1139 | } |
| 1140 | |
| 1141 | status_t MediaPlayer2Manager::Client::setLooping(int loop) |
| 1142 | { |
| 1143 | ALOGV("[%d] setLooping(%d)", mConnId, loop); |
| 1144 | mLoop = loop; |
| 1145 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1146 | if (p != 0) return p->setLooping(loop); |
| 1147 | return NO_ERROR; |
| 1148 | } |
| 1149 | |
| 1150 | status_t MediaPlayer2Manager::Client::setVolume(float leftVolume, float rightVolume) |
| 1151 | { |
| 1152 | ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume); |
| 1153 | |
| 1154 | // for hardware output, call player instead |
| 1155 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1156 | { |
| 1157 | Mutex::Autolock l(mLock); |
| 1158 | if (p != 0 && p->hardwareOutput()) { |
| 1159 | MediaPlayerHWInterface* hwp = |
| 1160 | reinterpret_cast<MediaPlayerHWInterface*>(p.get()); |
| 1161 | return hwp->setVolume(leftVolume, rightVolume); |
| 1162 | } else { |
| 1163 | if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume); |
| 1164 | return NO_ERROR; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return NO_ERROR; |
| 1169 | } |
| 1170 | |
| 1171 | status_t MediaPlayer2Manager::Client::setAuxEffectSendLevel(float level) |
| 1172 | { |
| 1173 | ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level); |
| 1174 | Mutex::Autolock l(mLock); |
| 1175 | if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level); |
| 1176 | return NO_ERROR; |
| 1177 | } |
| 1178 | |
| 1179 | status_t MediaPlayer2Manager::Client::attachAuxEffect(int effectId) |
| 1180 | { |
| 1181 | ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId); |
| 1182 | Mutex::Autolock l(mLock); |
| 1183 | if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId); |
| 1184 | return NO_ERROR; |
| 1185 | } |
| 1186 | |
| 1187 | status_t MediaPlayer2Manager::Client::setParameter(int key, const Parcel &request) { |
| 1188 | ALOGV("[%d] setParameter(%d)", mConnId, key); |
| 1189 | switch (key) { |
| 1190 | case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES: |
| 1191 | { |
| 1192 | Mutex::Autolock l(mLock); |
| 1193 | return setAudioAttributes_l(request); |
| 1194 | } |
| 1195 | default: |
| 1196 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1197 | if (p == 0) { return UNKNOWN_ERROR; } |
| 1198 | return p->setParameter(key, request); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | status_t MediaPlayer2Manager::Client::getParameter(int key, Parcel *reply) { |
| 1203 | ALOGV("[%d] getParameter(%d)", mConnId, key); |
| 1204 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1205 | if (p == 0) return UNKNOWN_ERROR; |
| 1206 | return p->getParameter(key, reply); |
| 1207 | } |
| 1208 | |
| 1209 | status_t MediaPlayer2Manager::Client::setRetransmitEndpoint( |
| 1210 | const struct sockaddr_in* endpoint) { |
| 1211 | |
| 1212 | if (NULL != endpoint) { |
| 1213 | uint32_t a = ntohl(endpoint->sin_addr.s_addr); |
| 1214 | uint16_t p = ntohs(endpoint->sin_port); |
| 1215 | ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId, |
| 1216 | (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p); |
| 1217 | } else { |
| 1218 | ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId); |
| 1219 | } |
| 1220 | |
| 1221 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1222 | |
| 1223 | // Right now, the only valid time to set a retransmit endpoint is before |
| 1224 | // player selection has been made (since the presence or absence of a |
| 1225 | // retransmit endpoint is going to determine which player is selected during |
| 1226 | // setDataSource). |
| 1227 | if (p != 0) return INVALID_OPERATION; |
| 1228 | |
| 1229 | if (NULL != endpoint) { |
| 1230 | Mutex::Autolock lock(mLock); |
| 1231 | mRetransmitEndpoint = *endpoint; |
| 1232 | mRetransmitEndpointValid = true; |
| 1233 | } else { |
| 1234 | Mutex::Autolock lock(mLock); |
| 1235 | mRetransmitEndpointValid = false; |
| 1236 | } |
| 1237 | |
| 1238 | return NO_ERROR; |
| 1239 | } |
| 1240 | |
| 1241 | status_t MediaPlayer2Manager::Client::getRetransmitEndpoint( |
| 1242 | struct sockaddr_in* endpoint) |
| 1243 | { |
| 1244 | if (NULL == endpoint) |
| 1245 | return BAD_VALUE; |
| 1246 | |
| 1247 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1248 | |
| 1249 | if (p != NULL) |
| 1250 | return p->getRetransmitEndpoint(endpoint); |
| 1251 | |
| 1252 | Mutex::Autolock lock(mLock); |
| 1253 | if (!mRetransmitEndpointValid) |
| 1254 | return NO_INIT; |
| 1255 | |
| 1256 | *endpoint = mRetransmitEndpoint; |
| 1257 | |
| 1258 | return NO_ERROR; |
| 1259 | } |
| 1260 | |
| 1261 | void MediaPlayer2Manager::Client::notify( |
Pawin Vongmasa | 5096385 | 2017-12-12 06:24:42 -0800 | [diff] [blame] | 1262 | const wp<MediaPlayer2Engine> &listener, int msg, int ext1, int ext2, const Parcel *obj) |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1263 | { |
Pawin Vongmasa | 5096385 | 2017-12-12 06:24:42 -0800 | [diff] [blame] | 1264 | sp<MediaPlayer2Engine> spListener = listener.promote(); |
| 1265 | if (spListener == NULL) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1266 | return; |
| 1267 | } |
Pawin Vongmasa | 5096385 | 2017-12-12 06:24:42 -0800 | [diff] [blame] | 1268 | Client* client = static_cast<Client*>(spListener.get()); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1269 | |
| 1270 | sp<MediaPlayer2EngineClient> c; |
| 1271 | sp<Client> nextClient; |
| 1272 | status_t errStartNext = NO_ERROR; |
| 1273 | { |
| 1274 | Mutex::Autolock l(client->mLock); |
| 1275 | c = client->mClient; |
| 1276 | if (msg == MEDIA2_PLAYBACK_COMPLETE && client->mNextClient != NULL) { |
| 1277 | nextClient = client->mNextClient; |
| 1278 | |
| 1279 | if (client->mAudioOutput != NULL) |
| 1280 | client->mAudioOutput->switchToNextOutput(); |
| 1281 | |
| 1282 | errStartNext = nextClient->start(); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | if (nextClient != NULL) { |
| 1287 | sp<MediaPlayer2EngineClient> nc; |
| 1288 | { |
| 1289 | Mutex::Autolock l(nextClient->mLock); |
| 1290 | nc = nextClient->mClient; |
| 1291 | } |
| 1292 | if (nc != NULL) { |
| 1293 | if (errStartNext == NO_ERROR) { |
| 1294 | nc->notify(MEDIA2_INFO, MEDIA2_INFO_STARTED_AS_NEXT, 0, obj); |
| 1295 | } else { |
| 1296 | nc->notify(MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN , 0, obj); |
| 1297 | ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext); |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | if (MEDIA2_INFO == msg && |
| 1303 | MEDIA2_INFO_METADATA_UPDATE == ext1) { |
| 1304 | const media::Metadata::Type metadata_type = ext2; |
| 1305 | |
| 1306 | if(client->shouldDropMetadata(metadata_type)) { |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Update the list of metadata that have changed. getMetadata |
| 1311 | // also access mMetadataUpdated and clears it. |
| 1312 | client->addNewMetadataUpdate(metadata_type); |
| 1313 | } |
| 1314 | |
| 1315 | if (c != NULL) { |
Pawin Vongmasa | 5096385 | 2017-12-12 06:24:42 -0800 | [diff] [blame] | 1316 | ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, spListener.get(), msg, ext1, ext2); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1317 | c->notify(msg, ext1, ext2, obj); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | bool MediaPlayer2Manager::Client::shouldDropMetadata(media::Metadata::Type code) const |
| 1323 | { |
| 1324 | Mutex::Autolock lock(mLock); |
| 1325 | |
| 1326 | if (findMetadata(mMetadataDrop, code)) { |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
| 1330 | if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) { |
| 1331 | return false; |
| 1332 | } else { |
| 1333 | return true; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | void MediaPlayer2Manager::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) { |
| 1339 | Mutex::Autolock lock(mLock); |
| 1340 | if (mMetadataUpdated.indexOf(metadata_type) < 0) { |
| 1341 | mMetadataUpdated.add(metadata_type); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // Modular DRM |
| 1346 | status_t MediaPlayer2Manager::Client::prepareDrm(const uint8_t uuid[16], |
| 1347 | const Vector<uint8_t>& drmSessionId) |
| 1348 | { |
| 1349 | ALOGV("[%d] prepareDrm", mConnId); |
| 1350 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1351 | if (p == 0) return UNKNOWN_ERROR; |
| 1352 | |
| 1353 | status_t ret = p->prepareDrm(uuid, drmSessionId); |
| 1354 | ALOGV("prepareDrm ret: %d", ret); |
| 1355 | |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
| 1359 | status_t MediaPlayer2Manager::Client::releaseDrm() |
| 1360 | { |
| 1361 | ALOGV("[%d] releaseDrm", mConnId); |
| 1362 | sp<MediaPlayer2Base> p = getPlayer(); |
| 1363 | if (p == 0) return UNKNOWN_ERROR; |
| 1364 | |
| 1365 | status_t ret = p->releaseDrm(); |
| 1366 | ALOGV("releaseDrm ret: %d", ret); |
| 1367 | |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | status_t MediaPlayer2Manager::Client::setOutputDevice(audio_port_handle_t deviceId) |
| 1372 | { |
| 1373 | ALOGV("[%d] setOutputDevice", mConnId); |
| 1374 | { |
| 1375 | Mutex::Autolock l(mLock); |
| 1376 | if (mAudioOutput.get() != nullptr) { |
| 1377 | return mAudioOutput->setOutputDevice(deviceId); |
| 1378 | } |
| 1379 | } |
| 1380 | return NO_INIT; |
| 1381 | } |
| 1382 | |
| 1383 | status_t MediaPlayer2Manager::Client::getRoutedDeviceId(audio_port_handle_t* deviceId) |
| 1384 | { |
| 1385 | ALOGV("[%d] getRoutedDeviceId", mConnId); |
| 1386 | { |
| 1387 | Mutex::Autolock l(mLock); |
| 1388 | if (mAudioOutput.get() != nullptr) { |
| 1389 | return mAudioOutput->getRoutedDeviceId(deviceId); |
| 1390 | } |
| 1391 | } |
| 1392 | return NO_INIT; |
| 1393 | } |
| 1394 | |
| 1395 | status_t MediaPlayer2Manager::Client::enableAudioDeviceCallback(bool enabled) |
| 1396 | { |
| 1397 | ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled); |
| 1398 | { |
| 1399 | Mutex::Autolock l(mLock); |
| 1400 | if (mAudioOutput.get() != nullptr) { |
| 1401 | return mAudioOutput->enableAudioDeviceCallback(enabled); |
| 1402 | } |
| 1403 | } |
| 1404 | return NO_INIT; |
| 1405 | } |
| 1406 | |
| 1407 | #if CALLBACK_ANTAGONIZER |
| 1408 | const int Antagonizer::interval = 10000; // 10 msecs |
| 1409 | |
Pawin Vongmasa | 5096385 | 2017-12-12 06:24:42 -0800 | [diff] [blame] | 1410 | Antagonizer::Antagonizer( |
| 1411 | MediaPlayer2Manager::NotifyCallback cb, |
| 1412 | const wp<MediaPlayer2Engine> &client) : |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1413 | mExit(false), mActive(false), mClient(client), mCb(cb) |
| 1414 | { |
| 1415 | createThread(callbackThread, this); |
| 1416 | } |
| 1417 | |
| 1418 | void Antagonizer::kill() |
| 1419 | { |
| 1420 | Mutex::Autolock _l(mLock); |
| 1421 | mActive = false; |
| 1422 | mExit = true; |
| 1423 | mCondition.wait(mLock); |
| 1424 | } |
| 1425 | |
| 1426 | int Antagonizer::callbackThread(void* user) |
| 1427 | { |
| 1428 | ALOGD("Antagonizer started"); |
| 1429 | Antagonizer* p = reinterpret_cast<Antagonizer*>(user); |
| 1430 | while (!p->mExit) { |
| 1431 | if (p->mActive) { |
| 1432 | ALOGV("send event"); |
| 1433 | p->mCb(p->mClient, 0, 0, 0); |
| 1434 | } |
| 1435 | usleep(interval); |
| 1436 | } |
| 1437 | Mutex::Autolock _l(p->mLock); |
| 1438 | p->mCondition.signal(); |
| 1439 | ALOGD("Antagonizer stopped"); |
| 1440 | return 0; |
| 1441 | } |
| 1442 | #endif |
| 1443 | |
| 1444 | #undef LOG_TAG |
| 1445 | #define LOG_TAG "AudioSink" |
| 1446 | MediaPlayer2Manager::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid, |
| 1447 | const audio_attributes_t* attr, const sp<AudioSystem::AudioDeviceCallback>& deviceCallback) |
| 1448 | : mCallback(NULL), |
| 1449 | mCallbackCookie(NULL), |
| 1450 | mCallbackData(NULL), |
| 1451 | mStreamType(AUDIO_STREAM_MUSIC), |
| 1452 | mLeftVolume(1.0), |
| 1453 | mRightVolume(1.0), |
| 1454 | mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT), |
| 1455 | mSampleRateHz(0), |
| 1456 | mMsecsPerFrame(0), |
| 1457 | mFrameSize(0), |
| 1458 | mSessionId(sessionId), |
| 1459 | mUid(uid), |
| 1460 | mPid(pid), |
| 1461 | mSendLevel(0.0), |
| 1462 | mAuxEffectId(0), |
| 1463 | mFlags(AUDIO_OUTPUT_FLAG_NONE), |
| 1464 | mVolumeHandler(new media::VolumeHandler()), |
| 1465 | mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE), |
| 1466 | mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE), |
| 1467 | mDeviceCallbackEnabled(false), |
| 1468 | mDeviceCallback(deviceCallback) |
| 1469 | { |
| 1470 | ALOGV("AudioOutput(%d)", sessionId); |
| 1471 | if (attr != NULL) { |
| 1472 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1473 | if (mAttributes != NULL) { |
| 1474 | memcpy(mAttributes, attr, sizeof(audio_attributes_t)); |
| 1475 | mStreamType = audio_attributes_to_stream_type(attr); |
| 1476 | } |
| 1477 | } else { |
| 1478 | mAttributes = NULL; |
| 1479 | } |
| 1480 | |
| 1481 | setMinBufferCount(); |
| 1482 | } |
| 1483 | |
| 1484 | MediaPlayer2Manager::AudioOutput::~AudioOutput() |
| 1485 | { |
| 1486 | close(); |
| 1487 | free(mAttributes); |
| 1488 | delete mCallbackData; |
| 1489 | } |
| 1490 | |
| 1491 | //static |
| 1492 | void MediaPlayer2Manager::AudioOutput::setMinBufferCount() |
| 1493 | { |
| 1494 | char value[PROPERTY_VALUE_MAX]; |
| 1495 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 1496 | mIsOnEmulator = true; |
| 1497 | mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // static |
| 1502 | bool MediaPlayer2Manager::AudioOutput::isOnEmulator() |
| 1503 | { |
| 1504 | setMinBufferCount(); // benign race wrt other threads |
| 1505 | return mIsOnEmulator; |
| 1506 | } |
| 1507 | |
| 1508 | // static |
| 1509 | int MediaPlayer2Manager::AudioOutput::getMinBufferCount() |
| 1510 | { |
| 1511 | setMinBufferCount(); // benign race wrt other threads |
| 1512 | return mMinBufferCount; |
| 1513 | } |
| 1514 | |
| 1515 | ssize_t MediaPlayer2Manager::AudioOutput::bufferSize() const |
| 1516 | { |
| 1517 | Mutex::Autolock lock(mLock); |
| 1518 | if (mTrack == 0) return NO_INIT; |
| 1519 | return mTrack->frameCount() * mFrameSize; |
| 1520 | } |
| 1521 | |
| 1522 | ssize_t MediaPlayer2Manager::AudioOutput::frameCount() const |
| 1523 | { |
| 1524 | Mutex::Autolock lock(mLock); |
| 1525 | if (mTrack == 0) return NO_INIT; |
| 1526 | return mTrack->frameCount(); |
| 1527 | } |
| 1528 | |
| 1529 | ssize_t MediaPlayer2Manager::AudioOutput::channelCount() const |
| 1530 | { |
| 1531 | Mutex::Autolock lock(mLock); |
| 1532 | if (mTrack == 0) return NO_INIT; |
| 1533 | return mTrack->channelCount(); |
| 1534 | } |
| 1535 | |
| 1536 | ssize_t MediaPlayer2Manager::AudioOutput::frameSize() const |
| 1537 | { |
| 1538 | Mutex::Autolock lock(mLock); |
| 1539 | if (mTrack == 0) return NO_INIT; |
| 1540 | return mFrameSize; |
| 1541 | } |
| 1542 | |
| 1543 | uint32_t MediaPlayer2Manager::AudioOutput::latency () const |
| 1544 | { |
| 1545 | Mutex::Autolock lock(mLock); |
| 1546 | if (mTrack == 0) return 0; |
| 1547 | return mTrack->latency(); |
| 1548 | } |
| 1549 | |
| 1550 | float MediaPlayer2Manager::AudioOutput::msecsPerFrame() const |
| 1551 | { |
| 1552 | Mutex::Autolock lock(mLock); |
| 1553 | return mMsecsPerFrame; |
| 1554 | } |
| 1555 | |
| 1556 | status_t MediaPlayer2Manager::AudioOutput::getPosition(uint32_t *position) const |
| 1557 | { |
| 1558 | Mutex::Autolock lock(mLock); |
| 1559 | if (mTrack == 0) return NO_INIT; |
| 1560 | return mTrack->getPosition(position); |
| 1561 | } |
| 1562 | |
| 1563 | status_t MediaPlayer2Manager::AudioOutput::getTimestamp(AudioTimestamp &ts) const |
| 1564 | { |
| 1565 | Mutex::Autolock lock(mLock); |
| 1566 | if (mTrack == 0) return NO_INIT; |
| 1567 | return mTrack->getTimestamp(ts); |
| 1568 | } |
| 1569 | |
| 1570 | // TODO: Remove unnecessary calls to getPlayedOutDurationUs() |
| 1571 | // as it acquires locks and may query the audio driver. |
| 1572 | // |
| 1573 | // Some calls could conceivably retrieve extrapolated data instead of |
| 1574 | // accessing getTimestamp() or getPosition() every time a data buffer with |
| 1575 | // a media time is received. |
| 1576 | // |
| 1577 | // Calculate duration of played samples if played at normal rate (i.e., 1.0). |
| 1578 | int64_t MediaPlayer2Manager::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const |
| 1579 | { |
| 1580 | Mutex::Autolock lock(mLock); |
| 1581 | if (mTrack == 0 || mSampleRateHz == 0) { |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | uint32_t numFramesPlayed; |
| 1586 | int64_t numFramesPlayedAtUs; |
| 1587 | AudioTimestamp ts; |
| 1588 | |
| 1589 | status_t res = mTrack->getTimestamp(ts); |
| 1590 | if (res == OK) { // case 1: mixing audio tracks and offloaded tracks. |
| 1591 | numFramesPlayed = ts.mPosition; |
| 1592 | numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000; |
| 1593 | //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
| 1594 | } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track |
| 1595 | numFramesPlayed = 0; |
| 1596 | numFramesPlayedAtUs = nowUs; |
| 1597 | //ALOGD("getTimestamp: WOULD_BLOCK %d %lld", |
| 1598 | // numFramesPlayed, (long long)numFramesPlayedAtUs); |
| 1599 | } else { // case 3: transitory at new track or audio fast tracks. |
| 1600 | res = mTrack->getPosition(&numFramesPlayed); |
| 1601 | CHECK_EQ(res, (status_t)OK); |
| 1602 | numFramesPlayedAtUs = nowUs; |
| 1603 | numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */ |
| 1604 | //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs); |
| 1605 | } |
| 1606 | |
| 1607 | // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test |
| 1608 | // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours. |
| 1609 | int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz) |
| 1610 | + nowUs - numFramesPlayedAtUs; |
| 1611 | if (durationUs < 0) { |
| 1612 | // Occurs when numFramesPlayed position is very small and the following: |
| 1613 | // (1) In case 1, the time nowUs is computed before getTimestamp() is called and |
| 1614 | // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed. |
| 1615 | // (2) In case 3, using getPosition and adding mAudioSink->latency() to |
| 1616 | // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed. |
| 1617 | // |
| 1618 | // Both of these are transitory conditions. |
| 1619 | ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs); |
| 1620 | durationUs = 0; |
| 1621 | } |
| 1622 | ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)", |
| 1623 | (long long)durationUs, (long long)nowUs, |
| 1624 | numFramesPlayed, (long long)numFramesPlayedAtUs); |
| 1625 | return durationUs; |
| 1626 | } |
| 1627 | |
| 1628 | status_t MediaPlayer2Manager::AudioOutput::getFramesWritten(uint32_t *frameswritten) const |
| 1629 | { |
| 1630 | Mutex::Autolock lock(mLock); |
| 1631 | if (mTrack == 0) return NO_INIT; |
| 1632 | ExtendedTimestamp ets; |
| 1633 | status_t status = mTrack->getTimestamp(&ets); |
| 1634 | if (status == OK || status == WOULD_BLOCK) { |
| 1635 | *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT]; |
| 1636 | } |
| 1637 | return status; |
| 1638 | } |
| 1639 | |
| 1640 | status_t MediaPlayer2Manager::AudioOutput::setParameters(const String8& keyValuePairs) |
| 1641 | { |
| 1642 | Mutex::Autolock lock(mLock); |
| 1643 | if (mTrack == 0) return NO_INIT; |
| 1644 | return mTrack->setParameters(keyValuePairs); |
| 1645 | } |
| 1646 | |
| 1647 | String8 MediaPlayer2Manager::AudioOutput::getParameters(const String8& keys) |
| 1648 | { |
| 1649 | Mutex::Autolock lock(mLock); |
| 1650 | if (mTrack == 0) return String8::empty(); |
| 1651 | return mTrack->getParameters(keys); |
| 1652 | } |
| 1653 | |
| 1654 | void MediaPlayer2Manager::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) { |
| 1655 | Mutex::Autolock lock(mLock); |
| 1656 | if (attributes == NULL) { |
| 1657 | free(mAttributes); |
| 1658 | mAttributes = NULL; |
| 1659 | } else { |
| 1660 | if (mAttributes == NULL) { |
| 1661 | mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 1662 | } |
| 1663 | memcpy(mAttributes, attributes, sizeof(audio_attributes_t)); |
| 1664 | mStreamType = audio_attributes_to_stream_type(attributes); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | void MediaPlayer2Manager::AudioOutput::setAudioStreamType(audio_stream_type_t streamType) |
| 1669 | { |
| 1670 | Mutex::Autolock lock(mLock); |
| 1671 | // do not allow direct stream type modification if attributes have been set |
| 1672 | if (mAttributes == NULL) { |
| 1673 | mStreamType = streamType; |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | void MediaPlayer2Manager::AudioOutput::deleteRecycledTrack_l() |
| 1678 | { |
| 1679 | ALOGV("deleteRecycledTrack_l"); |
| 1680 | if (mRecycledTrack != 0) { |
| 1681 | |
| 1682 | if (mCallbackData != NULL) { |
| 1683 | mCallbackData->setOutput(NULL); |
| 1684 | mCallbackData->endTrackSwitch(); |
| 1685 | } |
| 1686 | |
| 1687 | if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) { |
| 1688 | int32_t msec = 0; |
| 1689 | if (!mRecycledTrack->stopped()) { // check if active |
| 1690 | (void)mRecycledTrack->pendingDuration(&msec); |
| 1691 | } |
| 1692 | mRecycledTrack->stop(); // ensure full data drain |
| 1693 | ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec); |
| 1694 | if (msec > 0) { |
| 1695 | static const int32_t WAIT_LIMIT_MS = 3000; |
| 1696 | if (msec > WAIT_LIMIT_MS) { |
| 1697 | msec = WAIT_LIMIT_MS; |
| 1698 | } |
| 1699 | usleep(msec * 1000LL); |
| 1700 | } |
| 1701 | } |
| 1702 | // An offloaded track isn't flushed because the STREAM_END is reported |
| 1703 | // slightly prematurely to allow time for the gapless track switch |
| 1704 | // but this means that if we decide not to recycle the track there |
| 1705 | // could be a small amount of residual data still playing. We leave |
| 1706 | // AudioFlinger to drain the track. |
| 1707 | |
| 1708 | mRecycledTrack.clear(); |
| 1709 | close_l(); |
| 1710 | delete mCallbackData; |
| 1711 | mCallbackData = NULL; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | void MediaPlayer2Manager::AudioOutput::close_l() |
| 1716 | { |
| 1717 | mTrack.clear(); |
| 1718 | } |
| 1719 | |
| 1720 | status_t MediaPlayer2Manager::AudioOutput::open( |
| 1721 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
| 1722 | audio_format_t format, int bufferCount, |
| 1723 | AudioCallback cb, void *cookie, |
| 1724 | audio_output_flags_t flags, |
| 1725 | const audio_offload_info_t *offloadInfo, |
| 1726 | bool doNotReconnect, |
| 1727 | uint32_t suggestedFrameCount) |
| 1728 | { |
| 1729 | ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask, |
| 1730 | format, bufferCount, mSessionId, flags); |
| 1731 | |
| 1732 | // offloading is only supported in callback mode for now. |
| 1733 | // offloadInfo must be present if offload flag is set |
| 1734 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) && |
| 1735 | ((cb == NULL) || (offloadInfo == NULL))) { |
| 1736 | return BAD_VALUE; |
| 1737 | } |
| 1738 | |
| 1739 | // compute frame count for the AudioTrack internal buffer |
| 1740 | size_t frameCount; |
| 1741 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1742 | frameCount = 0; // AudioTrack will get frame count from AudioFlinger |
| 1743 | } else { |
| 1744 | // try to estimate the buffer processing fetch size from AudioFlinger. |
| 1745 | // framesPerBuffer is approximate and generally correct, except when it's not :-). |
| 1746 | uint32_t afSampleRate; |
| 1747 | size_t afFrameCount; |
| 1748 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) { |
| 1749 | return NO_INIT; |
| 1750 | } |
| 1751 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) { |
| 1752 | return NO_INIT; |
| 1753 | } |
| 1754 | const size_t framesPerBuffer = |
| 1755 | (unsigned long long)sampleRate * afFrameCount / afSampleRate; |
| 1756 | |
| 1757 | if (bufferCount == 0) { |
| 1758 | // use suggestedFrameCount |
| 1759 | bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer; |
| 1760 | } |
| 1761 | // Check argument bufferCount against the mininum buffer count |
| 1762 | if (bufferCount != 0 && bufferCount < mMinBufferCount) { |
| 1763 | ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount); |
| 1764 | bufferCount = mMinBufferCount; |
| 1765 | } |
| 1766 | // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger |
| 1767 | // which will be the minimum size permitted. |
| 1768 | frameCount = bufferCount * framesPerBuffer; |
| 1769 | } |
| 1770 | |
| 1771 | if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { |
| 1772 | channelMask = audio_channel_out_mask_from_count(channelCount); |
| 1773 | if (0 == channelMask) { |
| 1774 | ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount); |
| 1775 | return NO_INIT; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | Mutex::Autolock lock(mLock); |
| 1780 | mCallback = cb; |
| 1781 | mCallbackCookie = cookie; |
| 1782 | |
| 1783 | // Check whether we can recycle the track |
| 1784 | bool reuse = false; |
| 1785 | bool bothOffloaded = false; |
| 1786 | |
| 1787 | if (mRecycledTrack != 0) { |
| 1788 | // check whether we are switching between two offloaded tracks |
| 1789 | bothOffloaded = (flags & mRecycledTrack->getFlags() |
| 1790 | & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; |
| 1791 | |
| 1792 | // check if the existing track can be reused as-is, or if a new track needs to be created. |
| 1793 | reuse = true; |
| 1794 | |
| 1795 | if ((mCallbackData == NULL && mCallback != NULL) || |
| 1796 | (mCallbackData != NULL && mCallback == NULL)) { |
| 1797 | // recycled track uses callbacks but the caller wants to use writes, or vice versa |
| 1798 | ALOGV("can't chain callback and write"); |
| 1799 | reuse = false; |
| 1800 | } else if ((mRecycledTrack->getSampleRate() != sampleRate) || |
| 1801 | (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) { |
| 1802 | ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch", |
| 1803 | mRecycledTrack->getSampleRate(), sampleRate, |
| 1804 | mRecycledTrack->channelCount(), channelCount); |
| 1805 | reuse = false; |
| 1806 | } else if (flags != mFlags) { |
| 1807 | ALOGV("output flags differ %08x/%08x", flags, mFlags); |
| 1808 | reuse = false; |
| 1809 | } else if (mRecycledTrack->format() != format) { |
| 1810 | reuse = false; |
| 1811 | } |
| 1812 | } else { |
| 1813 | ALOGV("no track available to recycle"); |
| 1814 | } |
| 1815 | |
| 1816 | ALOGV_IF(bothOffloaded, "both tracks offloaded"); |
| 1817 | |
| 1818 | // If we can't recycle and both tracks are offloaded |
| 1819 | // we must close the previous output before opening a new one |
| 1820 | if (bothOffloaded && !reuse) { |
| 1821 | ALOGV("both offloaded and not recycling"); |
| 1822 | deleteRecycledTrack_l(); |
| 1823 | } |
| 1824 | |
| 1825 | sp<AudioTrack> t; |
| 1826 | CallbackData *newcbd = NULL; |
| 1827 | |
| 1828 | // We don't attempt to create a new track if we are recycling an |
| 1829 | // offloaded track. But, if we are recycling a non-offloaded or we |
| 1830 | // are switching where one is offloaded and one isn't then we create |
| 1831 | // the new track in advance so that we can read additional stream info |
| 1832 | |
| 1833 | if (!(reuse && bothOffloaded)) { |
| 1834 | ALOGV("creating new AudioTrack"); |
| 1835 | |
| 1836 | if (mCallback != NULL) { |
| 1837 | newcbd = new CallbackData(this); |
| 1838 | t = new AudioTrack( |
| 1839 | mStreamType, |
| 1840 | sampleRate, |
| 1841 | format, |
| 1842 | channelMask, |
| 1843 | frameCount, |
| 1844 | flags, |
| 1845 | CallbackWrapper, |
| 1846 | newcbd, |
| 1847 | 0, // notification frames |
| 1848 | mSessionId, |
| 1849 | AudioTrack::TRANSFER_CALLBACK, |
| 1850 | offloadInfo, |
| 1851 | mUid, |
| 1852 | mPid, |
| 1853 | mAttributes, |
| 1854 | doNotReconnect, |
| 1855 | 1.0f, // default value for maxRequiredSpeed |
| 1856 | mSelectedDeviceId); |
| 1857 | } else { |
| 1858 | // TODO: Due to buffer memory concerns, we use a max target playback speed |
| 1859 | // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed), |
| 1860 | // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed. |
| 1861 | const float targetSpeed = |
| 1862 | std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed); |
| 1863 | ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed, |
| 1864 | "track target speed:%f clamped from playback speed:%f", |
| 1865 | targetSpeed, mPlaybackRate.mSpeed); |
| 1866 | t = new AudioTrack( |
| 1867 | mStreamType, |
| 1868 | sampleRate, |
| 1869 | format, |
| 1870 | channelMask, |
| 1871 | frameCount, |
| 1872 | flags, |
| 1873 | NULL, // callback |
| 1874 | NULL, // user data |
| 1875 | 0, // notification frames |
| 1876 | mSessionId, |
| 1877 | AudioTrack::TRANSFER_DEFAULT, |
| 1878 | NULL, // offload info |
| 1879 | mUid, |
| 1880 | mPid, |
| 1881 | mAttributes, |
| 1882 | doNotReconnect, |
| 1883 | targetSpeed, |
| 1884 | mSelectedDeviceId); |
| 1885 | } |
| 1886 | |
| 1887 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
| 1888 | ALOGE("Unable to create audio track"); |
| 1889 | delete newcbd; |
| 1890 | // t goes out of scope, so reference count drops to zero |
| 1891 | return NO_INIT; |
| 1892 | } else { |
| 1893 | // successful AudioTrack initialization implies a legacy stream type was generated |
| 1894 | // from the audio attributes |
| 1895 | mStreamType = t->streamType(); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | if (reuse) { |
| 1900 | CHECK(mRecycledTrack != NULL); |
| 1901 | |
| 1902 | if (!bothOffloaded) { |
| 1903 | if (mRecycledTrack->frameCount() != t->frameCount()) { |
| 1904 | ALOGV("framecount differs: %zu/%zu frames", |
| 1905 | mRecycledTrack->frameCount(), t->frameCount()); |
| 1906 | reuse = false; |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | if (reuse) { |
| 1911 | ALOGV("chaining to next output and recycling track"); |
| 1912 | close_l(); |
| 1913 | mTrack = mRecycledTrack; |
| 1914 | mRecycledTrack.clear(); |
| 1915 | if (mCallbackData != NULL) { |
| 1916 | mCallbackData->setOutput(this); |
| 1917 | } |
| 1918 | delete newcbd; |
| 1919 | return updateTrack(); |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | // we're not going to reuse the track, unblock and flush it |
| 1924 | // this was done earlier if both tracks are offloaded |
| 1925 | if (!bothOffloaded) { |
| 1926 | deleteRecycledTrack_l(); |
| 1927 | } |
| 1928 | |
| 1929 | CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL))); |
| 1930 | |
| 1931 | mCallbackData = newcbd; |
| 1932 | ALOGV("setVolume"); |
| 1933 | t->setVolume(mLeftVolume, mRightVolume); |
| 1934 | |
| 1935 | // Restore VolumeShapers for the MediaPlayer2 in case the track was recreated |
| 1936 | // due to an output sink error (e.g. offload to non-offload switch). |
| 1937 | mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status { |
| 1938 | sp<VolumeShaper::Operation> operationToEnd = |
| 1939 | new VolumeShaper::Operation(shaper.mOperation); |
| 1940 | // TODO: Ideally we would restore to the exact xOffset position |
| 1941 | // as returned by getVolumeShaperState(), but we don't have that |
| 1942 | // information when restoring at the client unless we periodically poll |
| 1943 | // the server or create shared memory state. |
| 1944 | // |
| 1945 | // For now, we simply advance to the end of the VolumeShaper effect |
| 1946 | // if it has been started. |
| 1947 | if (shaper.isStarted()) { |
| 1948 | operationToEnd->setNormalizedTime(1.f); |
| 1949 | } |
| 1950 | return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd); |
| 1951 | }); |
| 1952 | |
| 1953 | mSampleRateHz = sampleRate; |
| 1954 | mFlags = flags; |
| 1955 | mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate); |
| 1956 | mFrameSize = t->frameSize(); |
| 1957 | mTrack = t; |
| 1958 | |
| 1959 | return updateTrack(); |
| 1960 | } |
| 1961 | |
| 1962 | status_t MediaPlayer2Manager::AudioOutput::updateTrack() { |
| 1963 | if (mTrack == NULL) { |
| 1964 | return NO_ERROR; |
| 1965 | } |
| 1966 | |
| 1967 | status_t res = NO_ERROR; |
| 1968 | // Note some output devices may give us a direct track even though we don't specify it. |
| 1969 | // Example: Line application b/17459982. |
| 1970 | if ((mTrack->getFlags() |
| 1971 | & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) { |
| 1972 | res = mTrack->setPlaybackRate(mPlaybackRate); |
| 1973 | if (res == NO_ERROR) { |
| 1974 | mTrack->setAuxEffectSendLevel(mSendLevel); |
| 1975 | res = mTrack->attachAuxEffect(mAuxEffectId); |
| 1976 | } |
| 1977 | } |
| 1978 | mTrack->setOutputDevice(mSelectedDeviceId); |
| 1979 | if (mDeviceCallbackEnabled) { |
| 1980 | mTrack->addAudioDeviceCallback(mDeviceCallback.promote()); |
| 1981 | } |
| 1982 | ALOGV("updateTrack() DONE status %d", res); |
| 1983 | return res; |
| 1984 | } |
| 1985 | |
| 1986 | status_t MediaPlayer2Manager::AudioOutput::start() |
| 1987 | { |
| 1988 | ALOGV("start"); |
| 1989 | Mutex::Autolock lock(mLock); |
| 1990 | if (mCallbackData != NULL) { |
| 1991 | mCallbackData->endTrackSwitch(); |
| 1992 | } |
| 1993 | if (mTrack != 0) { |
| 1994 | mTrack->setVolume(mLeftVolume, mRightVolume); |
| 1995 | mTrack->setAuxEffectSendLevel(mSendLevel); |
| 1996 | status_t status = mTrack->start(); |
| 1997 | if (status == NO_ERROR) { |
| 1998 | mVolumeHandler->setStarted(); |
| 1999 | } |
| 2000 | return status; |
| 2001 | } |
| 2002 | return NO_INIT; |
| 2003 | } |
| 2004 | |
| 2005 | void MediaPlayer2Manager::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) { |
| 2006 | Mutex::Autolock lock(mLock); |
| 2007 | mNextOutput = nextOutput; |
| 2008 | } |
| 2009 | |
| 2010 | void MediaPlayer2Manager::AudioOutput::switchToNextOutput() { |
| 2011 | ALOGV("switchToNextOutput"); |
| 2012 | |
| 2013 | // Try to acquire the callback lock before moving track (without incurring deadlock). |
| 2014 | const unsigned kMaxSwitchTries = 100; |
| 2015 | Mutex::Autolock lock(mLock); |
| 2016 | for (unsigned tries = 0;;) { |
| 2017 | if (mTrack == 0) { |
| 2018 | return; |
| 2019 | } |
| 2020 | if (mNextOutput != NULL && mNextOutput != this) { |
| 2021 | if (mCallbackData != NULL) { |
| 2022 | // two alternative approaches |
| 2023 | #if 1 |
| 2024 | CallbackData *callbackData = mCallbackData; |
| 2025 | mLock.unlock(); |
| 2026 | // proper acquisition sequence |
| 2027 | callbackData->lock(); |
| 2028 | mLock.lock(); |
| 2029 | // Caution: it is unlikely that someone deleted our callback or changed our target |
| 2030 | if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) { |
| 2031 | // fatal if we are starved out. |
| 2032 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2033 | "switchToNextOutput() cannot obtain correct lock sequence"); |
| 2034 | callbackData->unlock(); |
| 2035 | continue; |
| 2036 | } |
| 2037 | callbackData->mSwitching = true; // begin track switch |
| 2038 | callbackData->setOutput(NULL); |
| 2039 | #else |
| 2040 | // tryBeginTrackSwitch() returns false if the callback has the lock. |
| 2041 | if (!mCallbackData->tryBeginTrackSwitch()) { |
| 2042 | // fatal if we are starved out. |
| 2043 | LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries, |
| 2044 | "switchToNextOutput() cannot obtain callback lock"); |
| 2045 | mLock.unlock(); |
| 2046 | usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput |
| 2047 | mLock.lock(); |
| 2048 | continue; |
| 2049 | } |
| 2050 | #endif |
| 2051 | } |
| 2052 | |
| 2053 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2054 | |
| 2055 | // If the next output track is not NULL, then it has been |
| 2056 | // opened already for playback. |
| 2057 | // This is possible even without the next player being started, |
| 2058 | // for example, the next player could be prepared and seeked. |
| 2059 | // |
| 2060 | // Presuming it isn't advisable to force the track over. |
| 2061 | if (mNextOutput->mTrack == NULL) { |
| 2062 | ALOGD("Recycling track for gapless playback"); |
| 2063 | delete mNextOutput->mCallbackData; |
| 2064 | mNextOutput->mCallbackData = mCallbackData; |
| 2065 | mNextOutput->mRecycledTrack = mTrack; |
| 2066 | mNextOutput->mSampleRateHz = mSampleRateHz; |
| 2067 | mNextOutput->mMsecsPerFrame = mMsecsPerFrame; |
| 2068 | mNextOutput->mFlags = mFlags; |
| 2069 | mNextOutput->mFrameSize = mFrameSize; |
| 2070 | close_l(); |
| 2071 | mCallbackData = NULL; // destruction handled by mNextOutput |
| 2072 | } else { |
| 2073 | ALOGW("Ignoring gapless playback because next player has already started"); |
| 2074 | // remove track in case resource needed for future players. |
| 2075 | if (mCallbackData != NULL) { |
| 2076 | mCallbackData->endTrackSwitch(); // release lock for callbacks before close. |
| 2077 | } |
| 2078 | close_l(); |
| 2079 | } |
| 2080 | } |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | ssize_t MediaPlayer2Manager::AudioOutput::write(const void* buffer, size_t size, bool blocking) |
| 2086 | { |
| 2087 | Mutex::Autolock lock(mLock); |
| 2088 | LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); |
| 2089 | |
| 2090 | //ALOGV("write(%p, %u)", buffer, size); |
| 2091 | if (mTrack != 0) { |
| 2092 | return mTrack->write(buffer, size, blocking); |
| 2093 | } |
| 2094 | return NO_INIT; |
| 2095 | } |
| 2096 | |
| 2097 | void MediaPlayer2Manager::AudioOutput::stop() |
| 2098 | { |
| 2099 | ALOGV("stop"); |
| 2100 | Mutex::Autolock lock(mLock); |
| 2101 | if (mTrack != 0) mTrack->stop(); |
| 2102 | } |
| 2103 | |
| 2104 | void MediaPlayer2Manager::AudioOutput::flush() |
| 2105 | { |
| 2106 | ALOGV("flush"); |
| 2107 | Mutex::Autolock lock(mLock); |
| 2108 | if (mTrack != 0) mTrack->flush(); |
| 2109 | } |
| 2110 | |
| 2111 | void MediaPlayer2Manager::AudioOutput::pause() |
| 2112 | { |
| 2113 | ALOGV("pause"); |
| 2114 | Mutex::Autolock lock(mLock); |
| 2115 | if (mTrack != 0) mTrack->pause(); |
| 2116 | } |
| 2117 | |
| 2118 | void MediaPlayer2Manager::AudioOutput::close() |
| 2119 | { |
| 2120 | ALOGV("close"); |
| 2121 | sp<AudioTrack> track; |
| 2122 | { |
| 2123 | Mutex::Autolock lock(mLock); |
| 2124 | track = mTrack; |
| 2125 | close_l(); // clears mTrack |
| 2126 | } |
| 2127 | // destruction of the track occurs outside of mutex. |
| 2128 | } |
| 2129 | |
| 2130 | void MediaPlayer2Manager::AudioOutput::setVolume(float left, float right) |
| 2131 | { |
| 2132 | ALOGV("setVolume(%f, %f)", left, right); |
| 2133 | Mutex::Autolock lock(mLock); |
| 2134 | mLeftVolume = left; |
| 2135 | mRightVolume = right; |
| 2136 | if (mTrack != 0) { |
| 2137 | mTrack->setVolume(left, right); |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | status_t MediaPlayer2Manager::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate) |
| 2142 | { |
| 2143 | ALOGV("setPlaybackRate(%f %f %d %d)", |
| 2144 | rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode); |
| 2145 | Mutex::Autolock lock(mLock); |
| 2146 | if (mTrack == 0) { |
| 2147 | // remember rate so that we can set it when the track is opened |
| 2148 | mPlaybackRate = rate; |
| 2149 | return OK; |
| 2150 | } |
| 2151 | status_t res = mTrack->setPlaybackRate(rate); |
| 2152 | if (res != NO_ERROR) { |
| 2153 | return res; |
| 2154 | } |
| 2155 | // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded |
| 2156 | CHECK_GT(rate.mSpeed, 0.f); |
| 2157 | mPlaybackRate = rate; |
| 2158 | if (mSampleRateHz != 0) { |
| 2159 | mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz); |
| 2160 | } |
| 2161 | return res; |
| 2162 | } |
| 2163 | |
| 2164 | status_t MediaPlayer2Manager::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate) |
| 2165 | { |
| 2166 | ALOGV("setPlaybackRate"); |
| 2167 | Mutex::Autolock lock(mLock); |
| 2168 | if (mTrack == 0) { |
| 2169 | return NO_INIT; |
| 2170 | } |
| 2171 | *rate = mTrack->getPlaybackRate(); |
| 2172 | return NO_ERROR; |
| 2173 | } |
| 2174 | |
| 2175 | status_t MediaPlayer2Manager::AudioOutput::setAuxEffectSendLevel(float level) |
| 2176 | { |
| 2177 | ALOGV("setAuxEffectSendLevel(%f)", level); |
| 2178 | Mutex::Autolock lock(mLock); |
| 2179 | mSendLevel = level; |
| 2180 | if (mTrack != 0) { |
| 2181 | return mTrack->setAuxEffectSendLevel(level); |
| 2182 | } |
| 2183 | return NO_ERROR; |
| 2184 | } |
| 2185 | |
| 2186 | status_t MediaPlayer2Manager::AudioOutput::attachAuxEffect(int effectId) |
| 2187 | { |
| 2188 | ALOGV("attachAuxEffect(%d)", effectId); |
| 2189 | Mutex::Autolock lock(mLock); |
| 2190 | mAuxEffectId = effectId; |
| 2191 | if (mTrack != 0) { |
| 2192 | return mTrack->attachAuxEffect(effectId); |
| 2193 | } |
| 2194 | return NO_ERROR; |
| 2195 | } |
| 2196 | |
| 2197 | status_t MediaPlayer2Manager::AudioOutput::setOutputDevice(audio_port_handle_t deviceId) |
| 2198 | { |
| 2199 | ALOGV("setOutputDevice(%d)", deviceId); |
| 2200 | Mutex::Autolock lock(mLock); |
| 2201 | mSelectedDeviceId = deviceId; |
| 2202 | if (mTrack != 0) { |
| 2203 | return mTrack->setOutputDevice(deviceId); |
| 2204 | } |
| 2205 | return NO_ERROR; |
| 2206 | } |
| 2207 | |
| 2208 | status_t MediaPlayer2Manager::AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId) |
| 2209 | { |
| 2210 | ALOGV("getRoutedDeviceId"); |
| 2211 | Mutex::Autolock lock(mLock); |
| 2212 | if (mTrack != 0) { |
| 2213 | mRoutedDeviceId = mTrack->getRoutedDeviceId(); |
| 2214 | } |
| 2215 | *deviceId = mRoutedDeviceId; |
| 2216 | return NO_ERROR; |
| 2217 | } |
| 2218 | |
| 2219 | status_t MediaPlayer2Manager::AudioOutput::enableAudioDeviceCallback(bool enabled) |
| 2220 | { |
| 2221 | ALOGV("enableAudioDeviceCallback, %d", enabled); |
| 2222 | Mutex::Autolock lock(mLock); |
| 2223 | mDeviceCallbackEnabled = enabled; |
| 2224 | if (mTrack != 0) { |
| 2225 | status_t status; |
| 2226 | if (enabled) { |
| 2227 | status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote()); |
| 2228 | } else { |
| 2229 | status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote()); |
| 2230 | } |
| 2231 | return status; |
| 2232 | } |
| 2233 | return NO_ERROR; |
| 2234 | } |
| 2235 | |
| 2236 | VolumeShaper::Status MediaPlayer2Manager::AudioOutput::applyVolumeShaper( |
| 2237 | const sp<VolumeShaper::Configuration>& configuration, |
| 2238 | const sp<VolumeShaper::Operation>& operation) |
| 2239 | { |
| 2240 | Mutex::Autolock lock(mLock); |
| 2241 | ALOGV("AudioOutput::applyVolumeShaper"); |
| 2242 | |
| 2243 | mVolumeHandler->setIdIfNecessary(configuration); |
| 2244 | |
| 2245 | VolumeShaper::Status status; |
| 2246 | if (mTrack != 0) { |
| 2247 | status = mTrack->applyVolumeShaper(configuration, operation); |
| 2248 | if (status >= 0) { |
| 2249 | (void)mVolumeHandler->applyVolumeShaper(configuration, operation); |
| 2250 | if (mTrack->isPlaying()) { // match local AudioTrack to properly restore. |
| 2251 | mVolumeHandler->setStarted(); |
| 2252 | } |
| 2253 | } |
| 2254 | } else { |
| 2255 | // VolumeShapers are not affected when a track moves between players for |
| 2256 | // gapless playback (setNextMediaPlayer). |
| 2257 | // We forward VolumeShaper operations that do not change configuration |
| 2258 | // to the new player so that unducking may occur as expected. |
| 2259 | // Unducking is an idempotent operation, same if applied back-to-back. |
| 2260 | if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID |
| 2261 | && mNextOutput != nullptr) { |
| 2262 | ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s", |
| 2263 | configuration->toString().c_str(), operation->toString().c_str()); |
| 2264 | Mutex::Autolock nextLock(mNextOutput->mLock); |
| 2265 | |
| 2266 | // recycled track should be forwarded from this AudioSink by switchToNextOutput |
| 2267 | sp<AudioTrack> track = mNextOutput->mRecycledTrack; |
| 2268 | if (track != nullptr) { |
| 2269 | ALOGD("Forward VolumeShaper operation to recycled track %p", track.get()); |
| 2270 | (void)track->applyVolumeShaper(configuration, operation); |
| 2271 | } else { |
| 2272 | // There is a small chance that the unduck occurs after the next |
| 2273 | // player has already started, but before it is registered to receive |
| 2274 | // the unduck command. |
| 2275 | track = mNextOutput->mTrack; |
| 2276 | if (track != nullptr) { |
| 2277 | ALOGD("Forward VolumeShaper operation to track %p", track.get()); |
| 2278 | (void)track->applyVolumeShaper(configuration, operation); |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | status = mVolumeHandler->applyVolumeShaper(configuration, operation); |
| 2283 | } |
| 2284 | return status; |
| 2285 | } |
| 2286 | |
| 2287 | sp<VolumeShaper::State> MediaPlayer2Manager::AudioOutput::getVolumeShaperState(int id) |
| 2288 | { |
| 2289 | Mutex::Autolock lock(mLock); |
| 2290 | if (mTrack != 0) { |
| 2291 | return mTrack->getVolumeShaperState(id); |
| 2292 | } else { |
| 2293 | return mVolumeHandler->getVolumeShaperState(id); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | // static |
| 2298 | void MediaPlayer2Manager::AudioOutput::CallbackWrapper( |
| 2299 | int event, void *cookie, void *info) { |
| 2300 | //ALOGV("callbackwrapper"); |
| 2301 | CallbackData *data = (CallbackData*)cookie; |
| 2302 | // lock to ensure we aren't caught in the middle of a track switch. |
| 2303 | data->lock(); |
| 2304 | AudioOutput *me = data->getOutput(); |
| 2305 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
| 2306 | if (me == NULL) { |
| 2307 | // no output set, likely because the track was scheduled to be reused |
| 2308 | // by another player, but the format turned out to be incompatible. |
| 2309 | data->unlock(); |
| 2310 | if (buffer != NULL) { |
| 2311 | buffer->size = 0; |
| 2312 | } |
| 2313 | return; |
| 2314 | } |
| 2315 | |
| 2316 | switch(event) { |
| 2317 | case AudioTrack::EVENT_MORE_DATA: { |
| 2318 | size_t actualSize = (*me->mCallback)( |
| 2319 | me, buffer->raw, buffer->size, me->mCallbackCookie, |
| 2320 | CB_EVENT_FILL_BUFFER); |
| 2321 | |
| 2322 | // Log when no data is returned from the callback. |
| 2323 | // (1) We may have no data (especially with network streaming sources). |
| 2324 | // (2) We may have reached the EOS and the audio track is not stopped yet. |
| 2325 | // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS. |
| 2326 | // NuPlayer2Renderer will return zero when it doesn't have data (it doesn't block to fill). |
| 2327 | // |
| 2328 | // This is a benign busy-wait, with the next data request generated 10 ms or more later; |
| 2329 | // nevertheless for power reasons, we don't want to see too many of these. |
| 2330 | |
| 2331 | ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned"); |
| 2332 | |
| 2333 | buffer->size = actualSize; |
| 2334 | } break; |
| 2335 | |
| 2336 | case AudioTrack::EVENT_STREAM_END: |
| 2337 | // currently only occurs for offloaded callbacks |
| 2338 | ALOGV("callbackwrapper: deliver EVENT_STREAM_END"); |
| 2339 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2340 | me->mCallbackCookie, CB_EVENT_STREAM_END); |
| 2341 | break; |
| 2342 | |
| 2343 | case AudioTrack::EVENT_NEW_IAUDIOTRACK : |
| 2344 | ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN"); |
| 2345 | (*me->mCallback)(me, NULL /* buffer */, 0 /* size */, |
| 2346 | me->mCallbackCookie, CB_EVENT_TEAR_DOWN); |
| 2347 | break; |
| 2348 | |
| 2349 | case AudioTrack::EVENT_UNDERRUN: |
| 2350 | // This occurs when there is no data available, typically |
| 2351 | // when there is a failure to supply data to the AudioTrack. It can also |
| 2352 | // occur in non-offloaded mode when the audio device comes out of standby. |
| 2353 | // |
| 2354 | // If an AudioTrack underruns it outputs silence. Since this happens suddenly |
| 2355 | // it may sound like an audible pop or glitch. |
| 2356 | // |
| 2357 | // The underrun event is sent once per track underrun; the condition is reset |
| 2358 | // when more data is sent to the AudioTrack. |
| 2359 | ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)"); |
| 2360 | break; |
| 2361 | |
| 2362 | default: |
| 2363 | ALOGE("received unknown event type: %d inside CallbackWrapper !", event); |
| 2364 | } |
| 2365 | |
| 2366 | data->unlock(); |
| 2367 | } |
| 2368 | |
| 2369 | audio_session_t MediaPlayer2Manager::AudioOutput::getSessionId() const |
| 2370 | { |
| 2371 | Mutex::Autolock lock(mLock); |
| 2372 | return mSessionId; |
| 2373 | } |
| 2374 | |
| 2375 | uint32_t MediaPlayer2Manager::AudioOutput::getSampleRate() const |
| 2376 | { |
| 2377 | Mutex::Autolock lock(mLock); |
| 2378 | if (mTrack == 0) return 0; |
| 2379 | return mTrack->getSampleRate(); |
| 2380 | } |
| 2381 | |
| 2382 | int64_t MediaPlayer2Manager::AudioOutput::getBufferDurationInUs() const |
| 2383 | { |
| 2384 | Mutex::Autolock lock(mLock); |
| 2385 | if (mTrack == 0) { |
| 2386 | return 0; |
| 2387 | } |
| 2388 | int64_t duration; |
| 2389 | if (mTrack->getBufferDurationInUs(&duration) != OK) { |
| 2390 | return 0; |
| 2391 | } |
| 2392 | return duration; |
| 2393 | } |
| 2394 | |
| 2395 | } // namespace android |