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