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