GenericSource.cpp revision a6bf21f2a7cdd33c78464019dc1105ef01248b51
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "GenericSource" 19 20#include "GenericSource.h" 21 22#include "AnotherPacketSource.h" 23 24#include <media/IMediaHTTPService.h> 25#include <media/stagefright/foundation/ABuffer.h> 26#include <media/stagefright/foundation/ADebug.h> 27#include <media/stagefright/foundation/AMessage.h> 28#include <media/stagefright/DataSource.h> 29#include <media/stagefright/FileSource.h> 30#include <media/stagefright/MediaBuffer.h> 31#include <media/stagefright/MediaDefs.h> 32#include <media/stagefright/MediaExtractor.h> 33#include <media/stagefright/MediaSource.h> 34#include <media/stagefright/MetaData.h> 35#include <media/stagefright/Utils.h> 36#include "../../libstagefright/include/DRMExtractor.h" 37#include "../../libstagefright/include/NuCachedSource2.h" 38#include "../../libstagefright/include/WVMExtractor.h" 39#include "../../libstagefright/include/HTTPBase.h" 40 41namespace android { 42 43NuPlayer::GenericSource::GenericSource( 44 const sp<AMessage> ¬ify, 45 bool uidValid, 46 uid_t uid) 47 : Source(notify), 48 mAudioTimeUs(0), 49 mAudioLastDequeueTimeUs(0), 50 mVideoTimeUs(0), 51 mVideoLastDequeueTimeUs(0), 52 mFetchSubtitleDataGeneration(0), 53 mFetchTimedTextDataGeneration(0), 54 mDurationUs(0ll), 55 mAudioIsVorbis(false), 56 mIsWidevine(false), 57 mUIDValid(uidValid), 58 mUID(uid), 59 mFd(-1), 60 mDrmManagerClient(NULL), 61 mMetaDataSize(-1ll), 62 mBitrate(-1ll), 63 mPollBufferingGeneration(0), 64 mPendingReadBufferTypes(0) { 65 resetDataSource(); 66 DataSource::RegisterDefaultSniffers(); 67} 68 69void NuPlayer::GenericSource::resetDataSource() { 70 mHTTPService.clear(); 71 mHttpSource.clear(); 72 mUri.clear(); 73 mUriHeaders.clear(); 74 if (mFd >= 0) { 75 close(mFd); 76 mFd = -1; 77 } 78 mOffset = 0; 79 mLength = 0; 80 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0); 81 mDecryptHandle = NULL; 82 mDrmManagerClient = NULL; 83 mStarted = false; 84 mStopRead = true; 85} 86 87status_t NuPlayer::GenericSource::setDataSource( 88 const sp<IMediaHTTPService> &httpService, 89 const char *url, 90 const KeyedVector<String8, String8> *headers) { 91 resetDataSource(); 92 93 mHTTPService = httpService; 94 mUri = url; 95 96 if (headers) { 97 mUriHeaders = *headers; 98 } 99 100 // delay data source creation to prepareAsync() to avoid blocking 101 // the calling thread in setDataSource for any significant time. 102 return OK; 103} 104 105status_t NuPlayer::GenericSource::setDataSource( 106 int fd, int64_t offset, int64_t length) { 107 resetDataSource(); 108 109 mFd = dup(fd); 110 mOffset = offset; 111 mLength = length; 112 113 // delay data source creation to prepareAsync() to avoid blocking 114 // the calling thread in setDataSource for any significant time. 115 return OK; 116} 117 118sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const { 119 return mFileMeta; 120} 121 122status_t NuPlayer::GenericSource::initFromDataSource() { 123 sp<MediaExtractor> extractor; 124 125 CHECK(mDataSource != NULL); 126 127 if (mIsWidevine) { 128 String8 mimeType; 129 float confidence; 130 sp<AMessage> dummy; 131 bool success; 132 133 success = SniffWVM(mDataSource, &mimeType, &confidence, &dummy); 134 if (!success 135 || strcasecmp( 136 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) { 137 ALOGE("unsupported widevine mime: %s", mimeType.string()); 138 return UNKNOWN_ERROR; 139 } 140 141 mWVMExtractor = new WVMExtractor(mDataSource); 142 mWVMExtractor->setAdaptiveStreamingMode(true); 143 if (mUIDValid) { 144 mWVMExtractor->setUID(mUID); 145 } 146 extractor = mWVMExtractor; 147 } else { 148 extractor = MediaExtractor::Create(mDataSource, 149 mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str()); 150 } 151 152 if (extractor == NULL) { 153 return UNKNOWN_ERROR; 154 } 155 156 if (extractor->getDrmFlag()) { 157 checkDrmStatus(mDataSource); 158 } 159 160 mFileMeta = extractor->getMetaData(); 161 if (mFileMeta != NULL) { 162 int64_t duration; 163 if (mFileMeta->findInt64(kKeyDuration, &duration)) { 164 mDurationUs = duration; 165 } 166 } 167 168 int32_t totalBitrate = 0; 169 170 size_t numtracks = extractor->countTracks(); 171 if (numtracks == 0) { 172 return UNKNOWN_ERROR; 173 } 174 175 for (size_t i = 0; i < numtracks; ++i) { 176 sp<MediaSource> track = extractor->getTrack(i); 177 178 sp<MetaData> meta = extractor->getTrackMetaData(i); 179 180 const char *mime; 181 CHECK(meta->findCString(kKeyMIMEType, &mime)); 182 183 // Do the string compare immediately with "mime", 184 // we can't assume "mime" would stay valid after another 185 // extractor operation, some extractors might modify meta 186 // during getTrack() and make it invalid. 187 if (!strncasecmp(mime, "audio/", 6)) { 188 if (mAudioTrack.mSource == NULL) { 189 mAudioTrack.mIndex = i; 190 mAudioTrack.mSource = track; 191 mAudioTrack.mPackets = 192 new AnotherPacketSource(mAudioTrack.mSource->getFormat()); 193 194 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { 195 mAudioIsVorbis = true; 196 } else { 197 mAudioIsVorbis = false; 198 } 199 } 200 } else if (!strncasecmp(mime, "video/", 6)) { 201 if (mVideoTrack.mSource == NULL) { 202 mVideoTrack.mIndex = i; 203 mVideoTrack.mSource = track; 204 mVideoTrack.mPackets = 205 new AnotherPacketSource(mVideoTrack.mSource->getFormat()); 206 207 // check if the source requires secure buffers 208 int32_t secure; 209 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure) 210 && secure) { 211 mIsWidevine = true; 212 if (mUIDValid) { 213 extractor->setUID(mUID); 214 } 215 } 216 } 217 } 218 219 if (track != NULL) { 220 mSources.push(track); 221 int64_t durationUs; 222 if (meta->findInt64(kKeyDuration, &durationUs)) { 223 if (durationUs > mDurationUs) { 224 mDurationUs = durationUs; 225 } 226 } 227 228 int32_t bitrate; 229 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) { 230 totalBitrate += bitrate; 231 } else { 232 totalBitrate = -1; 233 } 234 } 235 } 236 237 mBitrate = totalBitrate; 238 239 return OK; 240} 241 242void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) { 243 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient); 244 if (mDecryptHandle != NULL) { 245 CHECK(mDrmManagerClient); 246 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) { 247 sp<AMessage> msg = dupNotify(); 248 msg->setInt32("what", kWhatDrmNoLicense); 249 msg->post(); 250 } 251 } 252} 253 254int64_t NuPlayer::GenericSource::getLastReadPosition() { 255 if (mAudioTrack.mSource != NULL) { 256 return mAudioTimeUs; 257 } else if (mVideoTrack.mSource != NULL) { 258 return mVideoTimeUs; 259 } else { 260 return 0; 261 } 262} 263 264status_t NuPlayer::GenericSource::setBuffers( 265 bool audio, Vector<MediaBuffer *> &buffers) { 266 if (mIsWidevine && !audio) { 267 return mVideoTrack.mSource->setBuffers(buffers); 268 } 269 return INVALID_OPERATION; 270} 271 272NuPlayer::GenericSource::~GenericSource() { 273 if (mLooper != NULL) { 274 mLooper->unregisterHandler(id()); 275 mLooper->stop(); 276 } 277 resetDataSource(); 278} 279 280void NuPlayer::GenericSource::prepareAsync() { 281 if (mLooper == NULL) { 282 mLooper = new ALooper; 283 mLooper->setName("generic"); 284 mLooper->start(); 285 286 mLooper->registerHandler(this); 287 } 288 289 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id()); 290 msg->post(); 291} 292 293void NuPlayer::GenericSource::onPrepareAsync() { 294 // delayed data source creation 295 if (mDataSource == NULL) { 296 if (!mUri.empty()) { 297 const char* uri = mUri.c_str(); 298 mIsWidevine = !strncasecmp(uri, "widevine://", 11); 299 300 if (!strncasecmp("http://", uri, 7) 301 || !strncasecmp("https://", uri, 8) 302 || mIsWidevine) { 303 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService); 304 if (mHttpSource == NULL) { 305 ALOGE("Failed to create http source!"); 306 notifyPreparedAndCleanup(UNKNOWN_ERROR); 307 return; 308 } 309 } 310 311 mDataSource = DataSource::CreateFromURI( 312 mHTTPService, uri, &mUriHeaders, &mContentType, 313 static_cast<HTTPBase *>(mHttpSource.get())); 314 } else { 315 // set to false first, if the extractor 316 // comes back as secure, set it to true then. 317 mIsWidevine = false; 318 319 mDataSource = new FileSource(mFd, mOffset, mLength); 320 mFd = -1; 321 } 322 323 if (mDataSource == NULL) { 324 ALOGE("Failed to create data source!"); 325 notifyPreparedAndCleanup(UNKNOWN_ERROR); 326 return; 327 } 328 329 if (mDataSource->flags() & DataSource::kIsCachingDataSource) { 330 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get()); 331 } 332 333 if (mIsWidevine || mCachedSource != NULL) { 334 schedulePollBuffering(); 335 } 336 } 337 338 // check initial caching status 339 status_t err = prefillCacheIfNecessary(); 340 if (err != OK) { 341 if (err == -EAGAIN) { 342 (new AMessage(kWhatPrepareAsync, id()))->post(200000); 343 } else { 344 ALOGE("Failed to prefill data cache!"); 345 notifyPreparedAndCleanup(UNKNOWN_ERROR); 346 } 347 return; 348 } 349 350 // init extrator from data source 351 err = initFromDataSource(); 352 353 if (err != OK) { 354 ALOGE("Failed to init from data source!"); 355 notifyPreparedAndCleanup(err); 356 return; 357 } 358 359 if (mVideoTrack.mSource != NULL) { 360 sp<MetaData> meta = doGetFormatMeta(false /* audio */); 361 sp<AMessage> msg = new AMessage; 362 err = convertMetaDataToMessage(meta, &msg); 363 if(err != OK) { 364 notifyPreparedAndCleanup(err); 365 return; 366 } 367 notifyVideoSizeChanged(msg); 368 } 369 370 notifyFlagsChanged( 371 (mIsWidevine ? FLAG_SECURE : 0) 372 | FLAG_CAN_PAUSE 373 | FLAG_CAN_SEEK_BACKWARD 374 | FLAG_CAN_SEEK_FORWARD 375 | FLAG_CAN_SEEK); 376 377 notifyPrepared(); 378} 379 380void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) { 381 if (err != OK) { 382 mMetaDataSize = -1ll; 383 mContentType = ""; 384 mSniffedMIME = ""; 385 mDataSource.clear(); 386 mCachedSource.clear(); 387 mHttpSource.clear(); 388 389 cancelPollBuffering(); 390 } 391 notifyPrepared(err); 392} 393 394status_t NuPlayer::GenericSource::prefillCacheIfNecessary() { 395 CHECK(mDataSource != NULL); 396 397 if (mCachedSource == NULL) { 398 // no prefill if the data source is not cached 399 return OK; 400 } 401 402 // We're not doing this for streams that appear to be audio-only 403 // streams to ensure that even low bandwidth streams start 404 // playing back fairly instantly. 405 if (!strncasecmp(mContentType.string(), "audio/", 6)) { 406 return OK; 407 } 408 409 // We're going to prefill the cache before trying to instantiate 410 // the extractor below, as the latter is an operation that otherwise 411 // could block on the datasource for a significant amount of time. 412 // During that time we'd be unable to abort the preparation phase 413 // without this prefill. 414 415 // Initially make sure we have at least 192 KB for the sniff 416 // to complete without blocking. 417 static const size_t kMinBytesForSniffing = 192 * 1024; 418 static const size_t kDefaultMetaSize = 200000; 419 420 status_t finalStatus; 421 422 size_t cachedDataRemaining = 423 mCachedSource->approxDataRemaining(&finalStatus); 424 425 if (finalStatus != OK || (mMetaDataSize >= 0 426 && (off64_t)cachedDataRemaining >= mMetaDataSize)) { 427 ALOGV("stop caching, status %d, " 428 "metaDataSize %lld, cachedDataRemaining %zu", 429 finalStatus, mMetaDataSize, cachedDataRemaining); 430 return OK; 431 } 432 433 ALOGV("now cached %zu bytes of data", cachedDataRemaining); 434 435 if (mMetaDataSize < 0 436 && cachedDataRemaining >= kMinBytesForSniffing) { 437 String8 tmp; 438 float confidence; 439 sp<AMessage> meta; 440 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) { 441 return UNKNOWN_ERROR; 442 } 443 444 // We successfully identified the file's extractor to 445 // be, remember this mime type so we don't have to 446 // sniff it again when we call MediaExtractor::Create() 447 mSniffedMIME = tmp.string(); 448 449 if (meta == NULL 450 || !meta->findInt64("meta-data-size", 451 reinterpret_cast<int64_t*>(&mMetaDataSize))) { 452 mMetaDataSize = kDefaultMetaSize; 453 } 454 455 if (mMetaDataSize < 0ll) { 456 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize); 457 return UNKNOWN_ERROR; 458 } 459 } 460 461 return -EAGAIN; 462} 463 464void NuPlayer::GenericSource::start() { 465 ALOGI("start"); 466 467 mStopRead = false; 468 if (mAudioTrack.mSource != NULL) { 469 CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK); 470 471 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO); 472 } 473 474 if (mVideoTrack.mSource != NULL) { 475 CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK); 476 477 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); 478 } 479 480 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000); 481 mStarted = true; 482} 483 484void NuPlayer::GenericSource::stop() { 485 // nothing to do, just account for DRM playback status 486 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0); 487 mStarted = false; 488 if (mIsWidevine) { 489 // For a widevine source we need to prevent any further reads. 490 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id()); 491 sp<AMessage> response; 492 (void) msg->postAndAwaitResponse(&response); 493 } 494} 495 496void NuPlayer::GenericSource::pause() { 497 // nothing to do, just account for DRM playback status 498 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0); 499 mStarted = false; 500} 501 502void NuPlayer::GenericSource::resume() { 503 // nothing to do, just account for DRM playback status 504 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000); 505 mStarted = true; 506} 507 508void NuPlayer::GenericSource::disconnect() { 509 if (mDataSource != NULL) { 510 // disconnect data source 511 if (mDataSource->flags() & DataSource::kIsCachingDataSource) { 512 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect(); 513 } 514 } else if (mHttpSource != NULL) { 515 static_cast<HTTPBase *>(mHttpSource.get())->disconnect(); 516 } 517} 518 519void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) { 520 if (mDecryptHandle != NULL) { 521 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position); 522 } 523 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL); 524 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL); 525} 526 527status_t NuPlayer::GenericSource::feedMoreTSData() { 528 return OK; 529} 530 531void NuPlayer::GenericSource::schedulePollBuffering() { 532 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id()); 533 msg->setInt32("generation", mPollBufferingGeneration); 534 msg->post(1000000ll); 535} 536 537void NuPlayer::GenericSource::cancelPollBuffering() { 538 ++mPollBufferingGeneration; 539} 540 541void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) { 542 sp<AMessage> msg = dupNotify(); 543 msg->setInt32("what", kWhatBufferingUpdate); 544 msg->setInt32("percentage", percentage); 545 msg->post(); 546} 547 548void NuPlayer::GenericSource::onPollBuffering() { 549 status_t finalStatus = UNKNOWN_ERROR; 550 int64_t cachedDurationUs = 0ll; 551 552 if (mCachedSource != NULL) { 553 size_t cachedDataRemaining = 554 mCachedSource->approxDataRemaining(&finalStatus); 555 556 if (finalStatus == OK) { 557 off64_t size; 558 int64_t bitrate = 0ll; 559 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) { 560 bitrate = size * 8000000ll / mDurationUs; 561 } else if (mBitrate > 0) { 562 bitrate = mBitrate; 563 } 564 if (bitrate > 0) { 565 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate; 566 } 567 } 568 } else if (mWVMExtractor != NULL) { 569 cachedDurationUs 570 = mWVMExtractor->getCachedDurationUs(&finalStatus); 571 } 572 573 if (finalStatus == ERROR_END_OF_STREAM) { 574 notifyBufferingUpdate(100); 575 cancelPollBuffering(); 576 return; 577 } else if (cachedDurationUs > 0ll && mDurationUs > 0ll) { 578 int percentage = 100.0 * cachedDurationUs / mDurationUs; 579 if (percentage > 100) { 580 percentage = 100; 581 } 582 583 notifyBufferingUpdate(percentage); 584 } 585 586 schedulePollBuffering(); 587} 588 589 590void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) { 591 switch (msg->what()) { 592 case kWhatPrepareAsync: 593 { 594 onPrepareAsync(); 595 break; 596 } 597 case kWhatFetchSubtitleData: 598 { 599 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, 600 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); 601 break; 602 } 603 604 case kWhatFetchTimedTextData: 605 { 606 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, 607 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); 608 break; 609 } 610 611 case kWhatSendSubtitleData: 612 { 613 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, 614 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); 615 break; 616 } 617 618 case kWhatSendTimedTextData: 619 { 620 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, 621 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); 622 break; 623 } 624 625 case kWhatChangeAVSource: 626 { 627 int32_t trackIndex; 628 CHECK(msg->findInt32("trackIndex", &trackIndex)); 629 const sp<MediaSource> source = mSources.itemAt(trackIndex); 630 631 Track* track; 632 const char *mime; 633 media_track_type trackType, counterpartType; 634 sp<MetaData> meta = source->getFormat(); 635 meta->findCString(kKeyMIMEType, &mime); 636 if (!strncasecmp(mime, "audio/", 6)) { 637 track = &mAudioTrack; 638 trackType = MEDIA_TRACK_TYPE_AUDIO; 639 counterpartType = MEDIA_TRACK_TYPE_VIDEO;; 640 } else { 641 CHECK(!strncasecmp(mime, "video/", 6)); 642 track = &mVideoTrack; 643 trackType = MEDIA_TRACK_TYPE_VIDEO; 644 counterpartType = MEDIA_TRACK_TYPE_AUDIO;; 645 } 646 647 648 if (track->mSource != NULL) { 649 track->mSource->stop(); 650 } 651 track->mSource = source; 652 track->mSource->start(); 653 track->mIndex = trackIndex; 654 655 int64_t timeUs, actualTimeUs; 656 const bool formatChange = true; 657 if (trackType == MEDIA_TRACK_TYPE_AUDIO) { 658 timeUs = mAudioLastDequeueTimeUs; 659 } else { 660 timeUs = mVideoLastDequeueTimeUs; 661 } 662 readBuffer(trackType, timeUs, &actualTimeUs, formatChange); 663 readBuffer(counterpartType, -1, NULL, formatChange); 664 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs); 665 666 break; 667 } 668 case kWhatPollBuffering: 669 { 670 int32_t generation; 671 CHECK(msg->findInt32("generation", &generation)); 672 if (generation == mPollBufferingGeneration) { 673 onPollBuffering(); 674 } 675 break; 676 } 677 678 case kWhatGetFormat: 679 { 680 onGetFormatMeta(msg); 681 break; 682 } 683 684 case kWhatGetSelectedTrack: 685 { 686 onGetSelectedTrack(msg); 687 break; 688 } 689 690 case kWhatSelectTrack: 691 { 692 onSelectTrack(msg); 693 break; 694 } 695 696 case kWhatSeek: 697 { 698 onSeek(msg); 699 break; 700 } 701 702 case kWhatReadBuffer: 703 { 704 onReadBuffer(msg); 705 break; 706 } 707 708 case kWhatStopWidevine: 709 { 710 // mStopRead is only used for Widevine to prevent the video source 711 // from being read while the associated video decoder is shutting down. 712 mStopRead = true; 713 if (mVideoTrack.mSource != NULL) { 714 mVideoTrack.mPackets->clear(); 715 } 716 sp<AMessage> response = new AMessage; 717 uint32_t replyID; 718 CHECK(msg->senderAwaitsResponse(&replyID)); 719 response->postReply(replyID); 720 break; 721 } 722 default: 723 Source::onMessageReceived(msg); 724 break; 725 } 726} 727 728void NuPlayer::GenericSource::fetchTextData( 729 uint32_t sendWhat, 730 media_track_type type, 731 int32_t curGen, 732 sp<AnotherPacketSource> packets, 733 sp<AMessage> msg) { 734 int32_t msgGeneration; 735 CHECK(msg->findInt32("generation", &msgGeneration)); 736 if (msgGeneration != curGen) { 737 // stale 738 return; 739 } 740 741 int32_t avail; 742 if (packets->hasBufferAvailable(&avail)) { 743 return; 744 } 745 746 int64_t timeUs; 747 CHECK(msg->findInt64("timeUs", &timeUs)); 748 749 int64_t subTimeUs; 750 readBuffer(type, timeUs, &subTimeUs); 751 752 int64_t delayUs = subTimeUs - timeUs; 753 if (msg->what() == kWhatFetchSubtitleData) { 754 const int64_t oneSecUs = 1000000ll; 755 delayUs -= oneSecUs; 756 } 757 sp<AMessage> msg2 = new AMessage(sendWhat, id()); 758 msg2->setInt32("generation", msgGeneration); 759 msg2->post(delayUs < 0 ? 0 : delayUs); 760} 761 762void NuPlayer::GenericSource::sendTextData( 763 uint32_t what, 764 media_track_type type, 765 int32_t curGen, 766 sp<AnotherPacketSource> packets, 767 sp<AMessage> msg) { 768 int32_t msgGeneration; 769 CHECK(msg->findInt32("generation", &msgGeneration)); 770 if (msgGeneration != curGen) { 771 // stale 772 return; 773 } 774 775 int64_t subTimeUs; 776 if (packets->nextBufferTime(&subTimeUs) != OK) { 777 return; 778 } 779 780 int64_t nextSubTimeUs; 781 readBuffer(type, -1, &nextSubTimeUs); 782 783 sp<ABuffer> buffer; 784 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer); 785 if (dequeueStatus == OK) { 786 sp<AMessage> notify = dupNotify(); 787 notify->setInt32("what", what); 788 notify->setBuffer("buffer", buffer); 789 notify->post(); 790 791 const int64_t delayUs = nextSubTimeUs - subTimeUs; 792 msg->post(delayUs < 0 ? 0 : delayUs); 793 } 794} 795 796sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) { 797 sp<AMessage> msg = new AMessage(kWhatGetFormat, id()); 798 msg->setInt32("audio", audio); 799 800 sp<AMessage> response; 801 void *format; 802 status_t err = msg->postAndAwaitResponse(&response); 803 if (err == OK && response != NULL) { 804 CHECK(response->findPointer("format", &format)); 805 return (MetaData *)format; 806 } else { 807 return NULL; 808 } 809} 810 811void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const { 812 int32_t audio; 813 CHECK(msg->findInt32("audio", &audio)); 814 815 sp<AMessage> response = new AMessage; 816 sp<MetaData> format = doGetFormatMeta(audio); 817 response->setPointer("format", format.get()); 818 819 uint32_t replyID; 820 CHECK(msg->senderAwaitsResponse(&replyID)); 821 response->postReply(replyID); 822} 823 824sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const { 825 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource; 826 827 if (source == NULL) { 828 return NULL; 829 } 830 831 return source->getFormat(); 832} 833 834status_t NuPlayer::GenericSource::dequeueAccessUnit( 835 bool audio, sp<ABuffer> *accessUnit) { 836 Track *track = audio ? &mAudioTrack : &mVideoTrack; 837 838 if (track->mSource == NULL) { 839 return -EWOULDBLOCK; 840 } 841 842 if (mIsWidevine && !audio) { 843 // try to read a buffer as we may not have been able to the last time 844 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); 845 } 846 847 status_t finalResult; 848 if (!track->mPackets->hasBufferAvailable(&finalResult)) { 849 return (finalResult == OK ? -EWOULDBLOCK : finalResult); 850 } 851 852 status_t result = track->mPackets->dequeueAccessUnit(accessUnit); 853 854 if (!track->mPackets->hasBufferAvailable(&finalResult)) { 855 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); 856 } 857 858 if (result != OK) { 859 if (mSubtitleTrack.mSource != NULL) { 860 mSubtitleTrack.mPackets->clear(); 861 mFetchSubtitleDataGeneration++; 862 } 863 if (mTimedTextTrack.mSource != NULL) { 864 mTimedTextTrack.mPackets->clear(); 865 mFetchTimedTextDataGeneration++; 866 } 867 return result; 868 } 869 870 int64_t timeUs; 871 status_t eosResult; // ignored 872 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); 873 if (audio) { 874 mAudioLastDequeueTimeUs = timeUs; 875 } else { 876 mVideoLastDequeueTimeUs = timeUs; 877 } 878 879 if (mSubtitleTrack.mSource != NULL 880 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) { 881 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id()); 882 msg->setInt64("timeUs", timeUs); 883 msg->setInt32("generation", mFetchSubtitleDataGeneration); 884 msg->post(); 885 } 886 887 if (mTimedTextTrack.mSource != NULL 888 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) { 889 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id()); 890 msg->setInt64("timeUs", timeUs); 891 msg->setInt32("generation", mFetchTimedTextDataGeneration); 892 msg->post(); 893 } 894 895 return result; 896} 897 898status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) { 899 *durationUs = mDurationUs; 900 return OK; 901} 902 903size_t NuPlayer::GenericSource::getTrackCount() const { 904 return mSources.size(); 905} 906 907sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const { 908 size_t trackCount = mSources.size(); 909 if (trackIndex >= trackCount) { 910 return NULL; 911 } 912 913 sp<AMessage> format = new AMessage(); 914 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat(); 915 916 const char *mime; 917 CHECK(meta->findCString(kKeyMIMEType, &mime)); 918 919 int32_t trackType; 920 if (!strncasecmp(mime, "video/", 6)) { 921 trackType = MEDIA_TRACK_TYPE_VIDEO; 922 } else if (!strncasecmp(mime, "audio/", 6)) { 923 trackType = MEDIA_TRACK_TYPE_AUDIO; 924 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) { 925 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT; 926 } else { 927 trackType = MEDIA_TRACK_TYPE_UNKNOWN; 928 } 929 format->setInt32("type", trackType); 930 931 const char *lang; 932 if (!meta->findCString(kKeyMediaLanguage, &lang)) { 933 lang = "und"; 934 } 935 format->setString("language", lang); 936 937 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 938 format->setString("mime", mime); 939 940 int32_t isAutoselect = 1, isDefault = 0, isForced = 0; 941 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect); 942 meta->findInt32(kKeyTrackIsDefault, &isDefault); 943 meta->findInt32(kKeyTrackIsForced, &isForced); 944 945 format->setInt32("auto", !!isAutoselect); 946 format->setInt32("default", !!isDefault); 947 format->setInt32("forced", !!isForced); 948 } 949 950 return format; 951} 952 953ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const { 954 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id()); 955 msg->setInt32("type", type); 956 957 sp<AMessage> response; 958 int32_t index; 959 status_t err = msg->postAndAwaitResponse(&response); 960 if (err == OK && response != NULL) { 961 CHECK(response->findInt32("index", &index)); 962 return index; 963 } else { 964 return -1; 965 } 966} 967 968void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const { 969 int32_t tmpType; 970 CHECK(msg->findInt32("type", &tmpType)); 971 media_track_type type = (media_track_type)tmpType; 972 973 sp<AMessage> response = new AMessage; 974 ssize_t index = doGetSelectedTrack(type); 975 response->setInt32("index", index); 976 977 uint32_t replyID; 978 CHECK(msg->senderAwaitsResponse(&replyID)); 979 response->postReply(replyID); 980} 981 982ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const { 983 const Track *track = NULL; 984 switch (type) { 985 case MEDIA_TRACK_TYPE_VIDEO: 986 track = &mVideoTrack; 987 break; 988 case MEDIA_TRACK_TYPE_AUDIO: 989 track = &mAudioTrack; 990 break; 991 case MEDIA_TRACK_TYPE_TIMEDTEXT: 992 track = &mTimedTextTrack; 993 break; 994 case MEDIA_TRACK_TYPE_SUBTITLE: 995 track = &mSubtitleTrack; 996 break; 997 default: 998 break; 999 } 1000 1001 if (track != NULL && track->mSource != NULL) { 1002 return track->mIndex; 1003 } 1004 1005 return -1; 1006} 1007 1008status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select) { 1009 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex); 1010 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id()); 1011 msg->setInt32("trackIndex", trackIndex); 1012 msg->setInt32("select", select); 1013 1014 sp<AMessage> response; 1015 status_t err = msg->postAndAwaitResponse(&response); 1016 if (err == OK && response != NULL) { 1017 CHECK(response->findInt32("err", &err)); 1018 } 1019 1020 return err; 1021} 1022 1023void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) { 1024 int32_t trackIndex, select; 1025 CHECK(msg->findInt32("trackIndex", &trackIndex)); 1026 CHECK(msg->findInt32("select", &select)); 1027 1028 sp<AMessage> response = new AMessage; 1029 status_t err = doSelectTrack(trackIndex, select); 1030 response->setInt32("err", err); 1031 1032 uint32_t replyID; 1033 CHECK(msg->senderAwaitsResponse(&replyID)); 1034 response->postReply(replyID); 1035} 1036 1037status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select) { 1038 if (trackIndex >= mSources.size()) { 1039 return BAD_INDEX; 1040 } 1041 1042 if (!select) { 1043 Track* track = NULL; 1044 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) { 1045 track = &mSubtitleTrack; 1046 mFetchSubtitleDataGeneration++; 1047 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) { 1048 track = &mTimedTextTrack; 1049 mFetchTimedTextDataGeneration++; 1050 } 1051 if (track == NULL) { 1052 return INVALID_OPERATION; 1053 } 1054 track->mSource->stop(); 1055 track->mSource = NULL; 1056 track->mPackets->clear(); 1057 return OK; 1058 } 1059 1060 const sp<MediaSource> source = mSources.itemAt(trackIndex); 1061 sp<MetaData> meta = source->getFormat(); 1062 const char *mime; 1063 CHECK(meta->findCString(kKeyMIMEType, &mime)); 1064 if (!strncasecmp(mime, "text/", 5)) { 1065 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP); 1066 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack; 1067 if (track->mSource != NULL && track->mIndex == trackIndex) { 1068 return OK; 1069 } 1070 track->mIndex = trackIndex; 1071 if (track->mSource != NULL) { 1072 track->mSource->stop(); 1073 } 1074 track->mSource = mSources.itemAt(trackIndex); 1075 track->mSource->start(); 1076 if (track->mPackets == NULL) { 1077 track->mPackets = new AnotherPacketSource(track->mSource->getFormat()); 1078 } else { 1079 track->mPackets->clear(); 1080 track->mPackets->setFormat(track->mSource->getFormat()); 1081 1082 } 1083 1084 if (isSubtitle) { 1085 mFetchSubtitleDataGeneration++; 1086 } else { 1087 mFetchTimedTextDataGeneration++; 1088 } 1089 1090 return OK; 1091 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) { 1092 bool audio = !strncasecmp(mime, "audio/", 6); 1093 Track *track = audio ? &mAudioTrack : &mVideoTrack; 1094 if (track->mSource != NULL && track->mIndex == trackIndex) { 1095 return OK; 1096 } 1097 1098 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id()); 1099 msg->setInt32("trackIndex", trackIndex); 1100 msg->post(); 1101 return OK; 1102 } 1103 1104 return INVALID_OPERATION; 1105} 1106 1107status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) { 1108 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 1109 msg->setInt64("seekTimeUs", seekTimeUs); 1110 1111 sp<AMessage> response; 1112 status_t err = msg->postAndAwaitResponse(&response); 1113 if (err == OK && response != NULL) { 1114 CHECK(response->findInt32("err", &err)); 1115 } 1116 1117 return err; 1118} 1119 1120void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) { 1121 int64_t seekTimeUs; 1122 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1123 1124 sp<AMessage> response = new AMessage; 1125 status_t err = doSeek(seekTimeUs); 1126 response->setInt32("err", err); 1127 1128 uint32_t replyID; 1129 CHECK(msg->senderAwaitsResponse(&replyID)); 1130 response->postReply(replyID); 1131} 1132 1133status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) { 1134 // If the Widevine source is stopped, do not attempt to read any 1135 // more buffers. 1136 if (mStopRead) { 1137 return INVALID_OPERATION; 1138 } 1139 if (mVideoTrack.mSource != NULL) { 1140 int64_t actualTimeUs; 1141 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs); 1142 1143 seekTimeUs = actualTimeUs; 1144 mVideoLastDequeueTimeUs = seekTimeUs; 1145 } 1146 1147 if (mAudioTrack.mSource != NULL) { 1148 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs); 1149 mAudioLastDequeueTimeUs = seekTimeUs; 1150 } 1151 1152 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000); 1153 if (!mStarted) { 1154 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0); 1155 } 1156 return OK; 1157} 1158 1159sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer( 1160 MediaBuffer* mb, 1161 media_track_type trackType, 1162 int64_t *actualTimeUs) { 1163 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO; 1164 size_t outLength = mb->range_length(); 1165 1166 if (audio && mAudioIsVorbis) { 1167 outLength += sizeof(int32_t); 1168 } 1169 1170 sp<ABuffer> ab; 1171 if (mIsWidevine && !audio) { 1172 // data is already provided in the buffer 1173 ab = new ABuffer(NULL, mb->range_length()); 1174 mb->add_ref(); 1175 ab->setMediaBufferBase(mb); 1176 } else { 1177 ab = new ABuffer(outLength); 1178 memcpy(ab->data(), 1179 (const uint8_t *)mb->data() + mb->range_offset(), 1180 mb->range_length()); 1181 } 1182 1183 if (audio && mAudioIsVorbis) { 1184 int32_t numPageSamples; 1185 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) { 1186 numPageSamples = -1; 1187 } 1188 1189 uint8_t* abEnd = ab->data() + mb->range_length(); 1190 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples)); 1191 } 1192 1193 sp<AMessage> meta = ab->meta(); 1194 1195 int64_t timeUs; 1196 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs)); 1197 meta->setInt64("timeUs", timeUs); 1198 1199 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) { 1200 const char *mime; 1201 CHECK(mTimedTextTrack.mSource != NULL 1202 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime)); 1203 meta->setString("mime", mime); 1204 } 1205 1206 int64_t durationUs; 1207 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) { 1208 meta->setInt64("durationUs", durationUs); 1209 } 1210 1211 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 1212 meta->setInt32("trackIndex", mSubtitleTrack.mIndex); 1213 } 1214 1215 if (actualTimeUs) { 1216 *actualTimeUs = timeUs; 1217 } 1218 1219 mb->release(); 1220 mb = NULL; 1221 1222 return ab; 1223} 1224 1225void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) { 1226 Mutex::Autolock _l(mReadBufferLock); 1227 1228 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) { 1229 mPendingReadBufferTypes |= (1 << trackType); 1230 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id()); 1231 msg->setInt32("trackType", trackType); 1232 msg->post(); 1233 } 1234} 1235 1236void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) { 1237 int32_t tmpType; 1238 CHECK(msg->findInt32("trackType", &tmpType)); 1239 media_track_type trackType = (media_track_type)tmpType; 1240 { 1241 // only protect the variable change, as readBuffer may 1242 // take considerable time. This may result in one extra 1243 // read being processed, but that is benign. 1244 Mutex::Autolock _l(mReadBufferLock); 1245 mPendingReadBufferTypes &= ~(1 << trackType); 1246 } 1247 readBuffer(trackType); 1248} 1249 1250void NuPlayer::GenericSource::readBuffer( 1251 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) { 1252 // Do not read data if Widevine source is stopped 1253 if (mStopRead) { 1254 return; 1255 } 1256 Track *track; 1257 size_t maxBuffers = 1; 1258 switch (trackType) { 1259 case MEDIA_TRACK_TYPE_VIDEO: 1260 track = &mVideoTrack; 1261 if (mIsWidevine) { 1262 maxBuffers = 2; 1263 } 1264 break; 1265 case MEDIA_TRACK_TYPE_AUDIO: 1266 track = &mAudioTrack; 1267 if (mIsWidevine) { 1268 maxBuffers = 8; 1269 } else { 1270 maxBuffers = 64; 1271 } 1272 break; 1273 case MEDIA_TRACK_TYPE_SUBTITLE: 1274 track = &mSubtitleTrack; 1275 break; 1276 case MEDIA_TRACK_TYPE_TIMEDTEXT: 1277 track = &mTimedTextTrack; 1278 break; 1279 default: 1280 TRESPASS(); 1281 } 1282 1283 if (track->mSource == NULL) { 1284 return; 1285 } 1286 1287 if (actualTimeUs) { 1288 *actualTimeUs = seekTimeUs; 1289 } 1290 1291 MediaSource::ReadOptions options; 1292 1293 bool seeking = false; 1294 1295 if (seekTimeUs >= 0) { 1296 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC); 1297 seeking = true; 1298 } 1299 1300 if (mIsWidevine && trackType != MEDIA_TRACK_TYPE_AUDIO) { 1301 options.setNonBlocking(); 1302 } 1303 1304 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) { 1305 MediaBuffer *mbuf; 1306 status_t err = track->mSource->read(&mbuf, &options); 1307 1308 options.clearSeekTo(); 1309 1310 if (err == OK) { 1311 int64_t timeUs; 1312 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs)); 1313 if (trackType == MEDIA_TRACK_TYPE_AUDIO) { 1314 mAudioTimeUs = timeUs; 1315 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { 1316 mVideoTimeUs = timeUs; 1317 } 1318 1319 // formatChange && seeking: track whose source is changed during selection 1320 // formatChange && !seeking: track whose source is not changed during selection 1321 // !formatChange: normal seek 1322 if ((seeking || formatChange) 1323 && (trackType == MEDIA_TRACK_TYPE_AUDIO 1324 || trackType == MEDIA_TRACK_TYPE_VIDEO)) { 1325 ATSParser::DiscontinuityType type = (formatChange && seeking) 1326 ? ATSParser::DISCONTINUITY_FORMATCHANGE 1327 : ATSParser::DISCONTINUITY_NONE; 1328 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */); 1329 } 1330 1331 sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType, actualTimeUs); 1332 track->mPackets->queueAccessUnit(buffer); 1333 formatChange = false; 1334 seeking = false; 1335 ++numBuffers; 1336 } else if (err == WOULD_BLOCK) { 1337 break; 1338 } else if (err == INFO_FORMAT_CHANGED) { 1339#if 0 1340 track->mPackets->queueDiscontinuity( 1341 ATSParser::DISCONTINUITY_FORMATCHANGE, 1342 NULL, 1343 false /* discard */); 1344#endif 1345 } else { 1346 track->mPackets->signalEOS(err); 1347 break; 1348 } 1349 } 1350} 1351 1352} // namespace android 1353