LiveSession.cpp revision 43c3e6ce02215ca99d506458f596cb1211639f29
1/* 2 * Copyright (C) 2010 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 "LiveSession" 19#include <utils/Log.h> 20 21#include "include/LiveSession.h" 22 23#include "LiveDataSource.h" 24 25#include "include/M3UParser.h" 26#include "include/NuHTTPDataSource.h" 27 28#include <cutils/properties.h> 29#include <media/stagefright/foundation/hexdump.h> 30#include <media/stagefright/foundation/ABuffer.h> 31#include <media/stagefright/foundation/ADebug.h> 32#include <media/stagefright/foundation/AMessage.h> 33#include <media/stagefright/DataSource.h> 34#include <media/stagefright/FileSource.h> 35#include <media/stagefright/MediaErrors.h> 36 37#include <ctype.h> 38#include <openssl/aes.h> 39 40namespace android { 41 42const int64_t LiveSession::kMaxPlaylistAgeUs = 15000000ll; 43 44LiveSession::LiveSession() 45 : mDataSource(new LiveDataSource), 46 mHTTPDataSource(new NuHTTPDataSource), 47 mPrevBandwidthIndex(-1), 48 mLastPlaylistFetchTimeUs(-1), 49 mSeqNumber(-1), 50 mSeekTimeUs(-1), 51 mNumRetries(0), 52 mDurationUs(-1), 53 mSeekDone(false), 54 mMonitorQueueGeneration(0) { 55} 56 57LiveSession::~LiveSession() { 58} 59 60sp<DataSource> LiveSession::getDataSource() { 61 return mDataSource; 62} 63 64void LiveSession::connect(const char *url) { 65 sp<AMessage> msg = new AMessage(kWhatConnect, id()); 66 msg->setString("url", url); 67 msg->post(); 68} 69 70void LiveSession::disconnect() { 71 (new AMessage(kWhatDisconnect, id()))->post(); 72} 73 74void LiveSession::seekTo(int64_t timeUs) { 75 Mutex::Autolock autoLock(mLock); 76 mSeekDone = false; 77 78 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 79 msg->setInt64("timeUs", timeUs); 80 msg->post(); 81 82 while (!mSeekDone) { 83 mCondition.wait(mLock); 84 } 85} 86 87void LiveSession::onMessageReceived(const sp<AMessage> &msg) { 88 switch (msg->what()) { 89 case kWhatConnect: 90 onConnect(msg); 91 break; 92 93 case kWhatDisconnect: 94 onDisconnect(); 95 break; 96 97 case kWhatMonitorQueue: 98 { 99 int32_t generation; 100 CHECK(msg->findInt32("generation", &generation)); 101 102 if (generation != mMonitorQueueGeneration) { 103 // Stale event 104 break; 105 } 106 107 onMonitorQueue(); 108 break; 109 } 110 111 case kWhatSeek: 112 onSeek(msg); 113 break; 114 115 default: 116 TRESPASS(); 117 break; 118 } 119} 120 121// static 122int LiveSession::SortByBandwidth(const BandwidthItem *a, const BandwidthItem *b) { 123 if (a->mBandwidth < b->mBandwidth) { 124 return -1; 125 } else if (a->mBandwidth == b->mBandwidth) { 126 return 0; 127 } 128 129 return 1; 130} 131 132void LiveSession::onConnect(const sp<AMessage> &msg) { 133 AString url; 134 CHECK(msg->findString("url", &url)); 135 136 LOGI("onConnect '%s'", url.c_str()); 137 138 mMasterURL = url; 139 140 sp<M3UParser> playlist = fetchPlaylist(url.c_str()); 141 CHECK(playlist != NULL); 142 143 if (playlist->isVariantPlaylist()) { 144 for (size_t i = 0; i < playlist->size(); ++i) { 145 BandwidthItem item; 146 147 sp<AMessage> meta; 148 playlist->itemAt(i, &item.mURI, &meta); 149 150 unsigned long bandwidth; 151 CHECK(meta->findInt32("bandwidth", (int32_t *)&item.mBandwidth)); 152 153 mBandwidthItems.push(item); 154 } 155 156 CHECK_GT(mBandwidthItems.size(), 0u); 157 158 mBandwidthItems.sort(SortByBandwidth); 159 160 char value[PROPERTY_VALUE_MAX]; 161 if (!property_get("media.httplive.enable-nuplayer", value, NULL) 162 || (strcasecmp(value, "true") && strcmp(value, "1"))) { 163 // The "legacy" player cannot deal with audio format changes, 164 // some streams use different audio encoding parameters for 165 // their lowest bandwidth stream. 166 if (mBandwidthItems.size() > 1) { 167 // XXX Remove the lowest bitrate stream for now... 168 mBandwidthItems.removeAt(0); 169 } 170 } 171 } 172 173 postMonitorQueue(); 174} 175 176void LiveSession::onDisconnect() { 177 LOGI("onDisconnect"); 178 179 mDataSource->queueEOS(ERROR_END_OF_STREAM); 180} 181 182status_t LiveSession::fetchFile(const char *url, sp<ABuffer> *out) { 183 *out = NULL; 184 185 sp<DataSource> source; 186 187 if (!strncasecmp(url, "file://", 7)) { 188 source = new FileSource(url + 7); 189 } else if (strncasecmp(url, "http://", 7)) { 190 return ERROR_UNSUPPORTED; 191 } else { 192 status_t err = mHTTPDataSource->connect(url); 193 194 if (err != OK) { 195 return err; 196 } 197 198 source = mHTTPDataSource; 199 } 200 201 off64_t size; 202 status_t err = source->getSize(&size); 203 204 if (err != OK) { 205 size = 65536; 206 } 207 208 sp<ABuffer> buffer = new ABuffer(size); 209 buffer->setRange(0, 0); 210 211 for (;;) { 212 size_t bufferRemaining = buffer->capacity() - buffer->size(); 213 214 if (bufferRemaining == 0) { 215 bufferRemaining = 32768; 216 217 LOGV("increasing download buffer to %d bytes", 218 buffer->size() + bufferRemaining); 219 220 sp<ABuffer> copy = new ABuffer(buffer->size() + bufferRemaining); 221 memcpy(copy->data(), buffer->data(), buffer->size()); 222 copy->setRange(0, buffer->size()); 223 224 buffer = copy; 225 } 226 227 ssize_t n = source->readAt( 228 buffer->size(), buffer->data() + buffer->size(), 229 bufferRemaining); 230 231 if (n < 0) { 232 return err; 233 } 234 235 if (n == 0) { 236 break; 237 } 238 239 buffer->setRange(0, buffer->size() + (size_t)n); 240 } 241 242 *out = buffer; 243 244 return OK; 245} 246 247sp<M3UParser> LiveSession::fetchPlaylist(const char *url) { 248 sp<ABuffer> buffer; 249 status_t err = fetchFile(url, &buffer); 250 251 if (err != OK) { 252 return NULL; 253 } 254 255 sp<M3UParser> playlist = 256 new M3UParser(url, buffer->data(), buffer->size()); 257 258 if (playlist->initCheck() != OK) { 259 return NULL; 260 } 261 262 return playlist; 263} 264 265static double uniformRand() { 266 return (double)rand() / RAND_MAX; 267} 268 269size_t LiveSession::getBandwidthIndex() { 270 if (mBandwidthItems.size() == 0) { 271 return 0; 272 } 273 274#if 1 275 int32_t bandwidthBps; 276 if (mHTTPDataSource != NULL 277 && mHTTPDataSource->estimateBandwidth(&bandwidthBps)) { 278 LOGV("bandwidth estimated at %.2f kbps", bandwidthBps / 1024.0f); 279 } else { 280 LOGV("no bandwidth estimate."); 281 return 0; // Pick the lowest bandwidth stream by default. 282 } 283 284 char value[PROPERTY_VALUE_MAX]; 285 if (property_get("media.httplive.max-bw", value, NULL)) { 286 char *end; 287 long maxBw = strtoul(value, &end, 10); 288 if (end > value && *end == '\0') { 289 if (maxBw > 0 && bandwidthBps > maxBw) { 290 LOGV("bandwidth capped to %ld bps", maxBw); 291 bandwidthBps = maxBw; 292 } 293 } 294 } 295 296 // Consider only 80% of the available bandwidth usable. 297 bandwidthBps = (bandwidthBps * 8) / 10; 298 299 // Pick the highest bandwidth stream below or equal to estimated bandwidth. 300 301 size_t index = mBandwidthItems.size() - 1; 302 while (index > 0 && mBandwidthItems.itemAt(index).mBandwidth 303 > (size_t)bandwidthBps) { 304 --index; 305 } 306#elif 0 307 // Change bandwidth at random() 308 size_t index = uniformRand() * mBandwidthItems.size(); 309#elif 0 310 // There's a 50% chance to stay on the current bandwidth and 311 // a 50% chance to switch to the next higher bandwidth (wrapping around 312 // to lowest) 313 const size_t kMinIndex = 0; 314 315 size_t index; 316 if (mPrevBandwidthIndex < 0) { 317 index = kMinIndex; 318 } else if (uniformRand() < 0.5) { 319 index = (size_t)mPrevBandwidthIndex; 320 } else { 321 index = mPrevBandwidthIndex + 1; 322 if (index == mBandwidthItems.size()) { 323 index = kMinIndex; 324 } 325 } 326#elif 0 327 // Pick the highest bandwidth stream below or equal to 1.2 Mbit/sec 328 329 size_t index = mBandwidthItems.size() - 1; 330 while (index > 0 && mBandwidthItems.itemAt(index).mBandwidth > 1200000) { 331 --index; 332 } 333#else 334 size_t index = mBandwidthItems.size() - 1; // Highest bandwidth stream 335#endif 336 337 return index; 338} 339 340void LiveSession::onDownloadNext() { 341 size_t bandwidthIndex = getBandwidthIndex(); 342 343 int64_t nowUs = ALooper::GetNowUs(); 344 345 if (mLastPlaylistFetchTimeUs < 0 346 || (ssize_t)bandwidthIndex != mPrevBandwidthIndex 347 || (!mPlaylist->isComplete() 348 && mLastPlaylistFetchTimeUs + kMaxPlaylistAgeUs <= nowUs)) { 349 AString url; 350 if (mBandwidthItems.size() > 0) { 351 url = mBandwidthItems.editItemAt(bandwidthIndex).mURI; 352 } else { 353 url = mMasterURL; 354 } 355 356 bool firstTime = (mPlaylist == NULL); 357 358 mPlaylist = fetchPlaylist(url.c_str()); 359 if (mPlaylist == NULL) { 360 LOGE("failed to load playlist at url '%s'", url.c_str()); 361 mDataSource->queueEOS(ERROR_IO); 362 return; 363 } 364 365 if (firstTime) { 366 Mutex::Autolock autoLock(mLock); 367 368 int32_t targetDuration; 369 if (!mPlaylist->isComplete() 370 || !mPlaylist->meta()->findInt32( 371 "target-duration", &targetDuration)) { 372 mDurationUs = -1; 373 } else { 374 mDurationUs = 1000000ll * targetDuration * mPlaylist->size(); 375 } 376 } 377 378 mLastPlaylistFetchTimeUs = ALooper::GetNowUs(); 379 } 380 381 int32_t firstSeqNumberInPlaylist; 382 if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32( 383 "media-sequence", &firstSeqNumberInPlaylist)) { 384 firstSeqNumberInPlaylist = 0; 385 } 386 387 bool explicitDiscontinuity = false; 388 389 if (mSeekTimeUs >= 0) { 390 int32_t targetDuration; 391 if (mPlaylist->isComplete() && 392 mPlaylist->meta()->findInt32( 393 "target-duration", &targetDuration)) { 394 int64_t seekTimeSecs = (mSeekTimeUs + 500000ll) / 1000000ll; 395 int64_t index = seekTimeSecs / targetDuration; 396 397 if (index >= 0 && index < mPlaylist->size()) { 398 int32_t newSeqNumber = firstSeqNumberInPlaylist + index; 399 400 if (newSeqNumber != mSeqNumber) { 401 LOGI("seeking to seq no %d", newSeqNumber); 402 403 mSeqNumber = newSeqNumber; 404 405 mDataSource->reset(); 406 407 explicitDiscontinuity = true; 408 } 409 } 410 } 411 412 mSeekTimeUs = -1; 413 414 Mutex::Autolock autoLock(mLock); 415 mSeekDone = true; 416 mCondition.broadcast(); 417 } 418 419 if (mSeqNumber < 0) { 420 if (mPlaylist->isComplete()) { 421 mSeqNumber = firstSeqNumberInPlaylist; 422 } else { 423 mSeqNumber = firstSeqNumberInPlaylist + mPlaylist->size() / 2; 424 } 425 } 426 427 int32_t lastSeqNumberInPlaylist = 428 firstSeqNumberInPlaylist + (int32_t)mPlaylist->size() - 1; 429 430 if (mSeqNumber < firstSeqNumberInPlaylist 431 || mSeqNumber > lastSeqNumberInPlaylist) { 432 if (!mPlaylist->isComplete() 433 && mSeqNumber > lastSeqNumberInPlaylist 434 && mNumRetries < kMaxNumRetries) { 435 ++mNumRetries; 436 437 mLastPlaylistFetchTimeUs = -1; 438 postMonitorQueue(3000000ll); 439 return; 440 } 441 442 LOGE("Cannot find sequence number %d in playlist " 443 "(contains %d - %d)", 444 mSeqNumber, firstSeqNumberInPlaylist, 445 firstSeqNumberInPlaylist + mPlaylist->size() - 1); 446 447 mDataSource->queueEOS(ERROR_END_OF_STREAM); 448 return; 449 } 450 451 mNumRetries = 0; 452 453 AString uri; 454 sp<AMessage> itemMeta; 455 CHECK(mPlaylist->itemAt( 456 mSeqNumber - firstSeqNumberInPlaylist, 457 &uri, 458 &itemMeta)); 459 460 int32_t val; 461 if (itemMeta->findInt32("discontinuity", &val) && val != 0) { 462 explicitDiscontinuity = true; 463 } 464 465 sp<ABuffer> buffer; 466 status_t err = fetchFile(uri.c_str(), &buffer); 467 if (err != OK) { 468 LOGE("failed to fetch .ts segment at url '%s'", uri.c_str()); 469 mDataSource->queueEOS(err); 470 return; 471 } 472 473 CHECK(buffer != NULL); 474 475 CHECK_EQ((status_t)OK, 476 decryptBuffer(mSeqNumber - firstSeqNumberInPlaylist, buffer)); 477 478 if (buffer->size() == 0 || buffer->data()[0] != 0x47) { 479 // Not a transport stream??? 480 481 LOGE("This doesn't look like a transport stream..."); 482 483 mDataSource->queueEOS(ERROR_UNSUPPORTED); 484 return; 485 } 486 487 bool bandwidthChanged = 488 mPrevBandwidthIndex >= 0 489 && (size_t)mPrevBandwidthIndex != bandwidthIndex; 490 491 if (explicitDiscontinuity || bandwidthChanged) { 492 // Signal discontinuity. 493 494 LOGI("queueing discontinuity (explicit=%d, bandwidthChanged=%d)", 495 explicitDiscontinuity, bandwidthChanged); 496 497 sp<ABuffer> tmp = new ABuffer(188); 498 memset(tmp->data(), 0, tmp->size()); 499 tmp->data()[1] = bandwidthChanged; 500 501 mDataSource->queueBuffer(tmp); 502 } 503 504 mDataSource->queueBuffer(buffer); 505 506 mPrevBandwidthIndex = bandwidthIndex; 507 ++mSeqNumber; 508 509 postMonitorQueue(); 510} 511 512void LiveSession::onMonitorQueue() { 513 if (mSeekTimeUs >= 0 514 || mDataSource->countQueuedBuffers() < kMaxNumQueuedFragments) { 515 onDownloadNext(); 516 } else { 517 postMonitorQueue(1000000ll); 518 } 519} 520 521status_t LiveSession::decryptBuffer( 522 size_t playlistIndex, const sp<ABuffer> &buffer) { 523 sp<AMessage> itemMeta; 524 bool found = false; 525 AString method; 526 527 for (ssize_t i = playlistIndex; i >= 0; --i) { 528 AString uri; 529 CHECK(mPlaylist->itemAt(i, &uri, &itemMeta)); 530 531 if (itemMeta->findString("cipher-method", &method)) { 532 found = true; 533 break; 534 } 535 } 536 537 if (!found) { 538 method = "NONE"; 539 } 540 541 if (method == "NONE") { 542 return OK; 543 } else if (!(method == "AES-128")) { 544 LOGE("Unsupported cipher method '%s'", method.c_str()); 545 return ERROR_UNSUPPORTED; 546 } 547 548 AString keyURI; 549 if (!itemMeta->findString("cipher-uri", &keyURI)) { 550 LOGE("Missing key uri"); 551 return ERROR_MALFORMED; 552 } 553 554 ssize_t index = mAESKeyForURI.indexOfKey(keyURI); 555 556 sp<ABuffer> key; 557 if (index >= 0) { 558 key = mAESKeyForURI.valueAt(index); 559 } else { 560 key = new ABuffer(16); 561 562 sp<NuHTTPDataSource> keySource = new NuHTTPDataSource; 563 status_t err = keySource->connect(keyURI.c_str()); 564 565 if (err == OK) { 566 size_t offset = 0; 567 while (offset < 16) { 568 ssize_t n = keySource->readAt( 569 offset, key->data() + offset, 16 - offset); 570 if (n <= 0) { 571 err = ERROR_IO; 572 break; 573 } 574 575 offset += n; 576 } 577 } 578 579 if (err != OK) { 580 LOGE("failed to fetch cipher key from '%s'.", keyURI.c_str()); 581 return ERROR_IO; 582 } 583 584 mAESKeyForURI.add(keyURI, key); 585 } 586 587 AES_KEY aes_key; 588 if (AES_set_decrypt_key(key->data(), 128, &aes_key) != 0) { 589 LOGE("failed to set AES decryption key."); 590 return UNKNOWN_ERROR; 591 } 592 593 unsigned char aes_ivec[16]; 594 595 AString iv; 596 if (itemMeta->findString("cipher-iv", &iv)) { 597 if ((!iv.startsWith("0x") && !iv.startsWith("0X")) 598 || iv.size() != 16 * 2 + 2) { 599 LOGE("malformed cipher IV '%s'.", iv.c_str()); 600 return ERROR_MALFORMED; 601 } 602 603 memset(aes_ivec, 0, sizeof(aes_ivec)); 604 for (size_t i = 0; i < 16; ++i) { 605 char c1 = tolower(iv.c_str()[2 + 2 * i]); 606 char c2 = tolower(iv.c_str()[3 + 2 * i]); 607 if (!isxdigit(c1) || !isxdigit(c2)) { 608 LOGE("malformed cipher IV '%s'.", iv.c_str()); 609 return ERROR_MALFORMED; 610 } 611 uint8_t nibble1 = isdigit(c1) ? c1 - '0' : c1 - 'a' + 10; 612 uint8_t nibble2 = isdigit(c2) ? c2 - '0' : c2 - 'a' + 10; 613 614 aes_ivec[i] = nibble1 << 4 | nibble2; 615 } 616 } else { 617 memset(aes_ivec, 0, sizeof(aes_ivec)); 618 aes_ivec[15] = mSeqNumber & 0xff; 619 aes_ivec[14] = (mSeqNumber >> 8) & 0xff; 620 aes_ivec[13] = (mSeqNumber >> 16) & 0xff; 621 aes_ivec[12] = (mSeqNumber >> 24) & 0xff; 622 } 623 624 AES_cbc_encrypt( 625 buffer->data(), buffer->data(), buffer->size(), 626 &aes_key, aes_ivec, AES_DECRYPT); 627 628 // hexdump(buffer->data(), buffer->size()); 629 630 size_t n = buffer->size(); 631 CHECK_GT(n, 0u); 632 633 size_t pad = buffer->data()[n - 1]; 634 635 CHECK_GT(pad, 0u); 636 CHECK_LE(pad, 16u); 637 CHECK_GE((size_t)n, pad); 638 for (size_t i = 0; i < pad; ++i) { 639 CHECK_EQ((unsigned)buffer->data()[n - 1 - i], pad); 640 } 641 642 n -= pad; 643 644 buffer->setRange(buffer->offset(), n); 645 646 return OK; 647} 648 649void LiveSession::postMonitorQueue(int64_t delayUs) { 650 sp<AMessage> msg = new AMessage(kWhatMonitorQueue, id()); 651 msg->setInt32("generation", ++mMonitorQueueGeneration); 652 msg->post(delayUs); 653} 654 655void LiveSession::onSeek(const sp<AMessage> &msg) { 656 int64_t timeUs; 657 CHECK(msg->findInt64("timeUs", &timeUs)); 658 659 mSeekTimeUs = timeUs; 660 postMonitorQueue(); 661} 662 663status_t LiveSession::getDuration(int64_t *durationUs) { 664 Mutex::Autolock autoLock(mLock); 665 *durationUs = mDurationUs; 666 667 return OK; 668} 669 670bool LiveSession::isSeekable() { 671 int64_t durationUs; 672 return getDuration(&durationUs) == OK && durationUs >= 0; 673} 674 675} // namespace android 676 677