1 /* 2 * Copyright (C) 2009 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 "Utils" 19 #include <utils/Log.h> 20 #include <ctype.h> 21 22 #include "include/ESDS.h" 23 24 #include <arpa/inet.h> 25 #include <cutils/properties.h> 26 #include <media/openmax/OMX_Audio.h> 27 #include <media/stagefright/foundation/ABuffer.h> 28 #include <media/stagefright/foundation/ADebug.h> 29 #include <media/stagefright/foundation/AMessage.h> 30 #include <media/stagefright/MetaData.h> 31 #include <media/stagefright/MediaDefs.h> 32 #include <media/AudioSystem.h> 33 #include <media/MediaPlayerInterface.h> 34 #include <hardware/audio.h> 35 #include <media/stagefright/Utils.h> 36 #include <media/AudioParameter.h> 37 38 namespace android { 39 40 uint16_t U16_AT(const uint8_t *ptr) { 41 return ptr[0] << 8 | ptr[1]; 42 } 43 44 uint32_t U32_AT(const uint8_t *ptr) { 45 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3]; 46 } 47 48 uint64_t U64_AT(const uint8_t *ptr) { 49 return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4); 50 } 51 52 uint16_t U16LE_AT(const uint8_t *ptr) { 53 return ptr[0] | (ptr[1] << 8); 54 } 55 56 uint32_t U32LE_AT(const uint8_t *ptr) { 57 return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0]; 58 } 59 60 uint64_t U64LE_AT(const uint8_t *ptr) { 61 return ((uint64_t)U32LE_AT(ptr + 4)) << 32 | U32LE_AT(ptr); 62 } 63 64 // XXX warning: these won't work on big-endian host. 65 uint64_t ntoh64(uint64_t x) { 66 return ((uint64_t)ntohl(x & 0xffffffff) << 32) | ntohl(x >> 32); 67 } 68 69 uint64_t hton64(uint64_t x) { 70 return ((uint64_t)htonl(x & 0xffffffff) << 32) | htonl(x >> 32); 71 } 72 73 static status_t copyNALUToABuffer(sp<ABuffer> *buffer, const uint8_t *ptr, size_t length) { 74 if (((*buffer)->size() + 4 + length) > ((*buffer)->capacity() - (*buffer)->offset())) { 75 sp<ABuffer> tmpBuffer = new (std::nothrow) ABuffer((*buffer)->size() + 4 + length + 1024); 76 if (tmpBuffer.get() == NULL || tmpBuffer->base() == NULL) { 77 return NO_MEMORY; 78 } 79 memcpy(tmpBuffer->data(), (*buffer)->data(), (*buffer)->size()); 80 tmpBuffer->setRange(0, (*buffer)->size()); 81 (*buffer) = tmpBuffer; 82 } 83 84 memcpy((*buffer)->data() + (*buffer)->size(), "\x00\x00\x00\x01", 4); 85 memcpy((*buffer)->data() + (*buffer)->size() + 4, ptr, length); 86 (*buffer)->setRange((*buffer)->offset(), (*buffer)->size() + 4 + length); 87 return OK; 88 } 89 90 status_t convertMetaDataToMessage( 91 const sp<MetaData> &meta, sp<AMessage> *format) { 92 format->clear(); 93 94 const char *mime; 95 CHECK(meta->findCString(kKeyMIMEType, &mime)); 96 97 sp<AMessage> msg = new AMessage; 98 msg->setString("mime", mime); 99 100 int64_t durationUs; 101 if (meta->findInt64(kKeyDuration, &durationUs)) { 102 msg->setInt64("durationUs", durationUs); 103 } 104 105 int avgBitRate; 106 if (meta->findInt32(kKeyBitRate, &avgBitRate)) { 107 msg->setInt32("bit-rate", avgBitRate); 108 } 109 110 int32_t isSync; 111 if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) { 112 msg->setInt32("is-sync-frame", 1); 113 } 114 115 if (!strncasecmp("video/", mime, 6)) { 116 int32_t width, height; 117 CHECK(meta->findInt32(kKeyWidth, &width)); 118 CHECK(meta->findInt32(kKeyHeight, &height)); 119 120 msg->setInt32("width", width); 121 msg->setInt32("height", height); 122 123 int32_t sarWidth, sarHeight; 124 if (meta->findInt32(kKeySARWidth, &sarWidth) 125 && meta->findInt32(kKeySARHeight, &sarHeight)) { 126 msg->setInt32("sar-width", sarWidth); 127 msg->setInt32("sar-height", sarHeight); 128 } 129 130 int32_t colorFormat; 131 if (meta->findInt32(kKeyColorFormat, &colorFormat)) { 132 msg->setInt32("color-format", colorFormat); 133 } 134 135 int32_t cropLeft, cropTop, cropRight, cropBottom; 136 if (meta->findRect(kKeyCropRect, 137 &cropLeft, 138 &cropTop, 139 &cropRight, 140 &cropBottom)) { 141 msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom); 142 } 143 144 int32_t rotationDegrees; 145 if (meta->findInt32(kKeyRotation, &rotationDegrees)) { 146 msg->setInt32("rotation-degrees", rotationDegrees); 147 } 148 } else if (!strncasecmp("audio/", mime, 6)) { 149 int32_t numChannels, sampleRate; 150 CHECK(meta->findInt32(kKeyChannelCount, &numChannels)); 151 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 152 153 msg->setInt32("channel-count", numChannels); 154 msg->setInt32("sample-rate", sampleRate); 155 156 int32_t channelMask; 157 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 158 msg->setInt32("channel-mask", channelMask); 159 } 160 161 int32_t delay = 0; 162 if (meta->findInt32(kKeyEncoderDelay, &delay)) { 163 msg->setInt32("encoder-delay", delay); 164 } 165 int32_t padding = 0; 166 if (meta->findInt32(kKeyEncoderPadding, &padding)) { 167 msg->setInt32("encoder-padding", padding); 168 } 169 170 int32_t isADTS; 171 if (meta->findInt32(kKeyIsADTS, &isADTS)) { 172 msg->setInt32("is-adts", true); 173 } 174 175 int32_t aacProfile = -1; 176 if (meta->findInt32(kKeyAACAOT, &aacProfile)) { 177 msg->setInt32("aac-profile", aacProfile); 178 } 179 } 180 181 int32_t maxInputSize; 182 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) { 183 msg->setInt32("max-input-size", maxInputSize); 184 } 185 186 int32_t maxWidth; 187 if (meta->findInt32(kKeyMaxWidth, &maxWidth)) { 188 msg->setInt32("max-width", maxWidth); 189 } 190 191 int32_t maxHeight; 192 if (meta->findInt32(kKeyMaxHeight, &maxHeight)) { 193 msg->setInt32("max-height", maxHeight); 194 } 195 196 int32_t rotationDegrees; 197 if (meta->findInt32(kKeyRotation, &rotationDegrees)) { 198 msg->setInt32("rotation-degrees", rotationDegrees); 199 } 200 201 int32_t fps; 202 if (meta->findInt32(kKeyFrameRate, &fps)) { 203 msg->setInt32("frame-rate", fps); 204 } 205 206 uint32_t type; 207 const void *data; 208 size_t size; 209 if (meta->findData(kKeyAVCC, &type, &data, &size)) { 210 // Parse the AVCDecoderConfigurationRecord 211 212 const uint8_t *ptr = (const uint8_t *)data; 213 214 CHECK(size >= 7); 215 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 216 uint8_t profile __unused = ptr[1]; 217 uint8_t level __unused = ptr[3]; 218 219 // There is decodable content out there that fails the following 220 // assertion, let's be lenient for now... 221 // CHECK((ptr[4] >> 2) == 0x3f); // reserved 222 223 size_t lengthSize __unused = 1 + (ptr[4] & 3); 224 225 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp 226 // violates it... 227 // CHECK((ptr[5] >> 5) == 7); // reserved 228 229 size_t numSeqParameterSets = ptr[5] & 31; 230 231 ptr += 6; 232 size -= 6; 233 234 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024); 235 if (buffer.get() == NULL || buffer->base() == NULL) { 236 return NO_MEMORY; 237 } 238 buffer->setRange(0, 0); 239 240 for (size_t i = 0; i < numSeqParameterSets; ++i) { 241 CHECK(size >= 2); 242 size_t length = U16_AT(ptr); 243 244 ptr += 2; 245 size -= 2; 246 247 if (size < length) { 248 return BAD_VALUE; 249 } 250 status_t err = copyNALUToABuffer(&buffer, ptr, length); 251 if (err != OK) { 252 return err; 253 } 254 255 ptr += length; 256 size -= length; 257 } 258 259 buffer->meta()->setInt32("csd", true); 260 buffer->meta()->setInt64("timeUs", 0); 261 262 msg->setBuffer("csd-0", buffer); 263 264 buffer = new (std::nothrow) ABuffer(1024); 265 if (buffer.get() == NULL || buffer->base() == NULL) { 266 return NO_MEMORY; 267 } 268 buffer->setRange(0, 0); 269 270 CHECK(size >= 1); 271 size_t numPictureParameterSets = *ptr; 272 ++ptr; 273 --size; 274 275 for (size_t i = 0; i < numPictureParameterSets; ++i) { 276 CHECK(size >= 2); 277 size_t length = U16_AT(ptr); 278 279 ptr += 2; 280 size -= 2; 281 282 if (size < length) { 283 return BAD_VALUE; 284 } 285 status_t err = copyNALUToABuffer(&buffer, ptr, length); 286 if (err != OK) { 287 return err; 288 } 289 290 ptr += length; 291 size -= length; 292 } 293 294 buffer->meta()->setInt32("csd", true); 295 buffer->meta()->setInt64("timeUs", 0); 296 msg->setBuffer("csd-1", buffer); 297 } else if (meta->findData(kKeyHVCC, &type, &data, &size)) { 298 const uint8_t *ptr = (const uint8_t *)data; 299 300 CHECK(size >= 7); 301 CHECK_EQ((unsigned)ptr[0], 1u); // configurationVersion == 1 302 uint8_t profile __unused = ptr[1] & 31; 303 uint8_t level __unused = ptr[12]; 304 ptr += 22; 305 size -= 22; 306 307 308 size_t numofArrays = (char)ptr[0]; 309 ptr += 1; 310 size -= 1; 311 size_t j = 0, i = 0; 312 313 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024); 314 if (buffer.get() == NULL || buffer->base() == NULL) { 315 return NO_MEMORY; 316 } 317 buffer->setRange(0, 0); 318 319 for (i = 0; i < numofArrays; i++) { 320 ptr += 1; 321 size -= 1; 322 323 //Num of nals 324 size_t numofNals = U16_AT(ptr); 325 326 ptr += 2; 327 size -= 2; 328 329 for (j = 0; j < numofNals; j++) { 330 CHECK(size >= 2); 331 size_t length = U16_AT(ptr); 332 333 ptr += 2; 334 size -= 2; 335 336 if (size < length) { 337 return BAD_VALUE; 338 } 339 status_t err = copyNALUToABuffer(&buffer, ptr, length); 340 if (err != OK) { 341 return err; 342 } 343 344 ptr += length; 345 size -= length; 346 } 347 } 348 buffer->meta()->setInt32("csd", true); 349 buffer->meta()->setInt64("timeUs", 0); 350 msg->setBuffer("csd-0", buffer); 351 352 } else if (meta->findData(kKeyESDS, &type, &data, &size)) { 353 ESDS esds((const char *)data, size); 354 CHECK_EQ(esds.InitCheck(), (status_t)OK); 355 356 const void *codec_specific_data; 357 size_t codec_specific_data_size; 358 esds.getCodecSpecificInfo( 359 &codec_specific_data, &codec_specific_data_size); 360 361 sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size); 362 if (buffer.get() == NULL || buffer->base() == NULL) { 363 return NO_MEMORY; 364 } 365 366 memcpy(buffer->data(), codec_specific_data, 367 codec_specific_data_size); 368 369 buffer->meta()->setInt32("csd", true); 370 buffer->meta()->setInt64("timeUs", 0); 371 msg->setBuffer("csd-0", buffer); 372 } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) { 373 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 374 if (buffer.get() == NULL || buffer->base() == NULL) { 375 return NO_MEMORY; 376 } 377 memcpy(buffer->data(), data, size); 378 379 buffer->meta()->setInt32("csd", true); 380 buffer->meta()->setInt64("timeUs", 0); 381 msg->setBuffer("csd-0", buffer); 382 383 if (!meta->findData(kKeyVorbisBooks, &type, &data, &size)) { 384 return -EINVAL; 385 } 386 387 buffer = new (std::nothrow) ABuffer(size); 388 if (buffer.get() == NULL || buffer->base() == NULL) { 389 return NO_MEMORY; 390 } 391 memcpy(buffer->data(), data, size); 392 393 buffer->meta()->setInt32("csd", true); 394 buffer->meta()->setInt64("timeUs", 0); 395 msg->setBuffer("csd-1", buffer); 396 } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) { 397 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size); 398 if (buffer.get() == NULL || buffer->base() == NULL) { 399 return NO_MEMORY; 400 } 401 memcpy(buffer->data(), data, size); 402 403 buffer->meta()->setInt32("csd", true); 404 buffer->meta()->setInt64("timeUs", 0); 405 msg->setBuffer("csd-0", buffer); 406 407 if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) { 408 return -EINVAL; 409 } 410 411 buffer = new (std::nothrow) ABuffer(size); 412 if (buffer.get() == NULL || buffer->base() == NULL) { 413 return NO_MEMORY; 414 } 415 memcpy(buffer->data(), data, size); 416 417 buffer->meta()->setInt32("csd", true); 418 buffer->meta()->setInt64("timeUs", 0); 419 msg->setBuffer("csd-1", buffer); 420 421 if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) { 422 return -EINVAL; 423 } 424 425 buffer = new (std::nothrow) ABuffer(size); 426 if (buffer.get() == NULL || buffer->base() == NULL) { 427 return NO_MEMORY; 428 } 429 memcpy(buffer->data(), data, size); 430 431 buffer->meta()->setInt32("csd", true); 432 buffer->meta()->setInt64("timeUs", 0); 433 msg->setBuffer("csd-2", buffer); 434 } 435 436 *format = msg; 437 438 return OK; 439 } 440 441 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> csd1, char *avcc) { 442 443 avcc[0] = 1; // version 444 avcc[1] = 0x64; // profile 445 avcc[2] = 0; // unused (?) 446 avcc[3] = 0xd; // level 447 avcc[4] = 0xff; // reserved+size 448 449 size_t i = 0; 450 int numparams = 0; 451 int lastparamoffset = 0; 452 int avccidx = 6; 453 do { 454 if (i >= csd0->size() - 4 || 455 memcmp(csd0->data() + i, "\x00\x00\x00\x01", 4) == 0) { 456 if (i >= csd0->size() - 4) { 457 // there can't be another param here, so use all the rest 458 i = csd0->size(); 459 } 460 ALOGV("block at %zu, last was %d", i, lastparamoffset); 461 if (lastparamoffset > 0) { 462 int size = i - lastparamoffset; 463 avcc[avccidx++] = size >> 8; 464 avcc[avccidx++] = size & 0xff; 465 memcpy(avcc+avccidx, csd0->data() + lastparamoffset, size); 466 avccidx += size; 467 numparams++; 468 } 469 i += 4; 470 lastparamoffset = i; 471 } else { 472 i++; 473 } 474 } while(i < csd0->size()); 475 ALOGV("csd0 contains %d params", numparams); 476 477 avcc[5] = 0xe0 | numparams; 478 //and now csd-1 479 i = 0; 480 numparams = 0; 481 lastparamoffset = 0; 482 int numpicparamsoffset = avccidx; 483 avccidx++; 484 do { 485 if (i >= csd1->size() - 4 || 486 memcmp(csd1->data() + i, "\x00\x00\x00\x01", 4) == 0) { 487 if (i >= csd1->size() - 4) { 488 // there can't be another param here, so use all the rest 489 i = csd1->size(); 490 } 491 ALOGV("block at %zu, last was %d", i, lastparamoffset); 492 if (lastparamoffset > 0) { 493 int size = i - lastparamoffset; 494 avcc[avccidx++] = size >> 8; 495 avcc[avccidx++] = size & 0xff; 496 memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size); 497 avccidx += size; 498 numparams++; 499 } 500 i += 4; 501 lastparamoffset = i; 502 } else { 503 i++; 504 } 505 } while(i < csd1->size()); 506 avcc[numpicparamsoffset] = numparams; 507 return avccidx; 508 } 509 510 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) { 511 int csd0size = csd0->size(); 512 esds[0] = 3; // kTag_ESDescriptor; 513 int esdescriptorsize = 26 + csd0size; 514 CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1 515 esds[1] = 0x80 | (esdescriptorsize >> 21); 516 esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f); 517 esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f); 518 esds[4] = (esdescriptorsize & 0x7f); 519 esds[5] = esds[6] = 0; // es id 520 esds[7] = 0; // flags 521 esds[8] = 4; // kTag_DecoderConfigDescriptor 522 int configdescriptorsize = 18 + csd0size; 523 esds[9] = 0x80 | (configdescriptorsize >> 21); 524 esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f); 525 esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f); 526 esds[12] = (configdescriptorsize & 0x7f); 527 esds[13] = 0x40; // objectTypeIndication 528 esds[14] = 0x15; // not sure what 14-25 mean, they are ignored by ESDS.cpp, 529 esds[15] = 0x00; // but the actual values here were taken from a real file. 530 esds[16] = 0x18; 531 esds[17] = 0x00; 532 esds[18] = 0x00; 533 esds[19] = 0x00; 534 esds[20] = 0xfa; 535 esds[21] = 0x00; 536 esds[22] = 0x00; 537 esds[23] = 0x00; 538 esds[24] = 0xfa; 539 esds[25] = 0x00; 540 esds[26] = 5; // kTag_DecoderSpecificInfo; 541 esds[27] = 0x80 | (csd0size >> 21); 542 esds[28] = 0x80 | ((csd0size >> 14) & 0x7f); 543 esds[29] = 0x80 | ((csd0size >> 7) & 0x7f); 544 esds[30] = (csd0size & 0x7f); 545 memcpy((void*)&esds[31], csd0->data(), csd0size); 546 // data following this is ignored, so don't bother appending it 547 548 } 549 550 void convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) { 551 AString mime; 552 if (msg->findString("mime", &mime)) { 553 meta->setCString(kKeyMIMEType, mime.c_str()); 554 } else { 555 ALOGW("did not find mime type"); 556 } 557 558 int64_t durationUs; 559 if (msg->findInt64("durationUs", &durationUs)) { 560 meta->setInt64(kKeyDuration, durationUs); 561 } 562 563 int32_t isSync; 564 if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) { 565 meta->setInt32(kKeyIsSyncFrame, 1); 566 } 567 568 if (mime.startsWith("video/")) { 569 int32_t width; 570 int32_t height; 571 if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) { 572 meta->setInt32(kKeyWidth, width); 573 meta->setInt32(kKeyHeight, height); 574 } else { 575 ALOGW("did not find width and/or height"); 576 } 577 578 int32_t sarWidth, sarHeight; 579 if (msg->findInt32("sar-width", &sarWidth) 580 && msg->findInt32("sar-height", &sarHeight)) { 581 meta->setInt32(kKeySARWidth, sarWidth); 582 meta->setInt32(kKeySARHeight, sarHeight); 583 } 584 585 int32_t colorFormat; 586 if (msg->findInt32("color-format", &colorFormat)) { 587 meta->setInt32(kKeyColorFormat, colorFormat); 588 } 589 590 int32_t cropLeft, cropTop, cropRight, cropBottom; 591 if (msg->findRect("crop", 592 &cropLeft, 593 &cropTop, 594 &cropRight, 595 &cropBottom)) { 596 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom); 597 } 598 599 int32_t rotationDegrees; 600 if (msg->findInt32("rotation-degrees", &rotationDegrees)) { 601 meta->setInt32(kKeyRotation, rotationDegrees); 602 } 603 } else if (mime.startsWith("audio/")) { 604 int32_t numChannels; 605 if (msg->findInt32("channel-count", &numChannels)) { 606 meta->setInt32(kKeyChannelCount, numChannels); 607 } 608 int32_t sampleRate; 609 if (msg->findInt32("sample-rate", &sampleRate)) { 610 meta->setInt32(kKeySampleRate, sampleRate); 611 } 612 int32_t channelMask; 613 if (msg->findInt32("channel-mask", &channelMask)) { 614 meta->setInt32(kKeyChannelMask, channelMask); 615 } 616 int32_t delay = 0; 617 if (msg->findInt32("encoder-delay", &delay)) { 618 meta->setInt32(kKeyEncoderDelay, delay); 619 } 620 int32_t padding = 0; 621 if (msg->findInt32("encoder-padding", &padding)) { 622 meta->setInt32(kKeyEncoderPadding, padding); 623 } 624 625 int32_t isADTS; 626 if (msg->findInt32("is-adts", &isADTS)) { 627 meta->setInt32(kKeyIsADTS, isADTS); 628 } 629 } 630 631 int32_t maxInputSize; 632 if (msg->findInt32("max-input-size", &maxInputSize)) { 633 meta->setInt32(kKeyMaxInputSize, maxInputSize); 634 } 635 636 int32_t maxWidth; 637 if (msg->findInt32("max-width", &maxWidth)) { 638 meta->setInt32(kKeyMaxWidth, maxWidth); 639 } 640 641 int32_t maxHeight; 642 if (msg->findInt32("max-height", &maxHeight)) { 643 meta->setInt32(kKeyMaxHeight, maxHeight); 644 } 645 646 int32_t fps; 647 if (msg->findInt32("frame-rate", &fps)) { 648 meta->setInt32(kKeyFrameRate, fps); 649 } 650 651 // reassemble the csd data into its original form 652 sp<ABuffer> csd0; 653 if (msg->findBuffer("csd-0", &csd0)) { 654 if (mime == MEDIA_MIMETYPE_VIDEO_AVC) { 655 sp<ABuffer> csd1; 656 if (msg->findBuffer("csd-1", &csd1)) { 657 char avcc[1024]; // that oughta be enough, right? 658 size_t outsize = reassembleAVCC(csd0, csd1, avcc); 659 meta->setData(kKeyAVCC, kKeyAVCC, avcc, outsize); 660 } 661 } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC || mime == MEDIA_MIMETYPE_VIDEO_MPEG4) { 662 int csd0size = csd0->size(); 663 char esds[csd0size + 31]; 664 // The written ESDS is actually for an audio stream, but it's enough 665 // for transporting the CSD to muxers. 666 reassembleESDS(csd0, esds); 667 meta->setData(kKeyESDS, kKeyESDS, esds, sizeof(esds)); 668 } 669 } 670 671 int32_t timeScale; 672 if (msg->findInt32("time-scale", &timeScale)) { 673 meta->setInt32(kKeyTimeScale, timeScale); 674 } 675 676 // XXX TODO add whatever other keys there are 677 678 #if 0 679 ALOGI("converted %s to:", msg->debugString(0).c_str()); 680 meta->dumpToLog(); 681 #endif 682 } 683 684 AString MakeUserAgent() { 685 AString ua; 686 ua.append("stagefright/1.2 (Linux;Android "); 687 688 #if (PROPERTY_VALUE_MAX < 8) 689 #error "PROPERTY_VALUE_MAX must be at least 8" 690 #endif 691 692 char value[PROPERTY_VALUE_MAX]; 693 property_get("ro.build.version.release", value, "Unknown"); 694 ua.append(value); 695 ua.append(")"); 696 697 return ua; 698 } 699 700 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink, 701 const sp<MetaData>& meta) 702 { 703 int32_t sampleRate = 0; 704 int32_t bitRate = 0; 705 int32_t channelMask = 0; 706 int32_t delaySamples = 0; 707 int32_t paddingSamples = 0; 708 709 AudioParameter param = AudioParameter(); 710 711 if (meta->findInt32(kKeySampleRate, &sampleRate)) { 712 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate); 713 } 714 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 715 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask); 716 } 717 if (meta->findInt32(kKeyBitRate, &bitRate)) { 718 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate); 719 } 720 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) { 721 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples); 722 } 723 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) { 724 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples); 725 } 726 727 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d," 728 "delaySample %d, paddingSample %d", bitRate, sampleRate, 729 channelMask, delaySamples, paddingSamples); 730 731 sink->setParameters(param.toString()); 732 return OK; 733 } 734 735 struct mime_conv_t { 736 const char* mime; 737 audio_format_t format; 738 }; 739 740 static const struct mime_conv_t mimeLookup[] = { 741 { MEDIA_MIMETYPE_AUDIO_MPEG, AUDIO_FORMAT_MP3 }, 742 { MEDIA_MIMETYPE_AUDIO_RAW, AUDIO_FORMAT_PCM_16_BIT }, 743 { MEDIA_MIMETYPE_AUDIO_AMR_NB, AUDIO_FORMAT_AMR_NB }, 744 { MEDIA_MIMETYPE_AUDIO_AMR_WB, AUDIO_FORMAT_AMR_WB }, 745 { MEDIA_MIMETYPE_AUDIO_AAC, AUDIO_FORMAT_AAC }, 746 { MEDIA_MIMETYPE_AUDIO_VORBIS, AUDIO_FORMAT_VORBIS }, 747 { MEDIA_MIMETYPE_AUDIO_OPUS, AUDIO_FORMAT_OPUS}, 748 { 0, AUDIO_FORMAT_INVALID } 749 }; 750 751 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime ) 752 { 753 const struct mime_conv_t* p = &mimeLookup[0]; 754 while (p->mime != NULL) { 755 if (0 == strcasecmp(mime, p->mime)) { 756 format = p->format; 757 return OK; 758 } 759 ++p; 760 } 761 762 return BAD_VALUE; 763 } 764 765 struct aac_format_conv_t { 766 OMX_AUDIO_AACPROFILETYPE eAacProfileType; 767 audio_format_t format; 768 }; 769 770 static const struct aac_format_conv_t profileLookup[] = { 771 { OMX_AUDIO_AACObjectMain, AUDIO_FORMAT_AAC_MAIN}, 772 { OMX_AUDIO_AACObjectLC, AUDIO_FORMAT_AAC_LC}, 773 { OMX_AUDIO_AACObjectSSR, AUDIO_FORMAT_AAC_SSR}, 774 { OMX_AUDIO_AACObjectLTP, AUDIO_FORMAT_AAC_LTP}, 775 { OMX_AUDIO_AACObjectHE, AUDIO_FORMAT_AAC_HE_V1}, 776 { OMX_AUDIO_AACObjectScalable, AUDIO_FORMAT_AAC_SCALABLE}, 777 { OMX_AUDIO_AACObjectERLC, AUDIO_FORMAT_AAC_ERLC}, 778 { OMX_AUDIO_AACObjectLD, AUDIO_FORMAT_AAC_LD}, 779 { OMX_AUDIO_AACObjectHE_PS, AUDIO_FORMAT_AAC_HE_V2}, 780 { OMX_AUDIO_AACObjectELD, AUDIO_FORMAT_AAC_ELD}, 781 { OMX_AUDIO_AACObjectNull, AUDIO_FORMAT_AAC}, 782 }; 783 784 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile) 785 { 786 const struct aac_format_conv_t* p = &profileLookup[0]; 787 while (p->eAacProfileType != OMX_AUDIO_AACObjectNull) { 788 if (eAacProfile == p->eAacProfileType) { 789 format = p->format; 790 return; 791 } 792 ++p; 793 } 794 format = AUDIO_FORMAT_AAC; 795 return; 796 } 797 798 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo, 799 bool isStreaming, audio_stream_type_t streamType) 800 { 801 const char *mime; 802 if (meta == NULL) { 803 return false; 804 } 805 CHECK(meta->findCString(kKeyMIMEType, &mime)); 806 807 audio_offload_info_t info = AUDIO_INFO_INITIALIZER; 808 809 info.format = AUDIO_FORMAT_INVALID; 810 if (mapMimeToAudioFormat(info.format, mime) != OK) { 811 ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime); 812 return false; 813 } else { 814 ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format); 815 } 816 817 if (AUDIO_FORMAT_INVALID == info.format) { 818 // can't offload if we don't know what the source format is 819 ALOGE("mime type \"%s\" not a known audio format", mime); 820 return false; 821 } 822 823 // Redefine aac format according to its profile 824 // Offloading depends on audio DSP capabilities. 825 int32_t aacaot = -1; 826 if (meta->findInt32(kKeyAACAOT, &aacaot)) { 827 mapAACProfileToAudioFormat(info.format,(OMX_AUDIO_AACPROFILETYPE) aacaot); 828 } 829 830 int32_t srate = -1; 831 if (!meta->findInt32(kKeySampleRate, &srate)) { 832 ALOGV("track of type '%s' does not publish sample rate", mime); 833 } 834 info.sample_rate = srate; 835 836 int32_t cmask = 0; 837 if (!meta->findInt32(kKeyChannelMask, &cmask)) { 838 ALOGV("track of type '%s' does not publish channel mask", mime); 839 840 // Try a channel count instead 841 int32_t channelCount; 842 if (!meta->findInt32(kKeyChannelCount, &channelCount)) { 843 ALOGV("track of type '%s' does not publish channel count", mime); 844 } else { 845 cmask = audio_channel_out_mask_from_count(channelCount); 846 } 847 } 848 info.channel_mask = cmask; 849 850 int64_t duration = 0; 851 if (!meta->findInt64(kKeyDuration, &duration)) { 852 ALOGV("track of type '%s' does not publish duration", mime); 853 } 854 info.duration_us = duration; 855 856 int32_t brate = -1; 857 if (!meta->findInt32(kKeyBitRate, &brate)) { 858 ALOGV("track of type '%s' does not publish bitrate", mime); 859 } 860 info.bit_rate = brate; 861 862 863 info.stream_type = streamType; 864 info.has_video = hasVideo; 865 info.is_streaming = isStreaming; 866 867 // Check if offload is possible for given format, stream type, sample rate, 868 // bit rate, duration, video and streaming 869 return AudioSystem::isOffloadSupported(info); 870 } 871 872 AString uriDebugString(const AString &uri, bool incognito) { 873 if (incognito) { 874 return AString("<URI suppressed>"); 875 } 876 877 char prop[PROPERTY_VALUE_MAX]; 878 if (property_get("media.stagefright.log-uri", prop, "false") && 879 (!strcmp(prop, "1") || !strcmp(prop, "true"))) { 880 return uri; 881 } 882 883 // find scheme 884 AString scheme; 885 const char *chars = uri.c_str(); 886 for (size_t i = 0; i < uri.size(); i++) { 887 const char c = chars[i]; 888 if (!isascii(c)) { 889 break; 890 } else if (isalpha(c)) { 891 continue; 892 } else if (i == 0) { 893 // first character must be a letter 894 break; 895 } else if (isdigit(c) || c == '+' || c == '.' || c =='-') { 896 continue; 897 } else if (c != ':') { 898 break; 899 } 900 scheme = AString(uri, 0, i); 901 scheme.append("://<suppressed>"); 902 return scheme; 903 } 904 return AString("<no-scheme URI suppressed>"); 905 } 906 907 HLSTime::HLSTime(const sp<AMessage>& meta) : 908 mSeq(-1), 909 mTimeUs(-1ll), 910 mMeta(meta) { 911 if (meta != NULL) { 912 CHECK(meta->findInt32("discontinuitySeq", &mSeq)); 913 CHECK(meta->findInt64("timeUs", &mTimeUs)); 914 } 915 } 916 917 int64_t HLSTime::getSegmentTimeUs() const { 918 int64_t segmentStartTimeUs = -1ll; 919 if (mMeta != NULL) { 920 CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs)); 921 922 int64_t segmentFirstTimeUs; 923 if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) { 924 segmentStartTimeUs += mTimeUs - segmentFirstTimeUs; 925 } 926 927 // adjust segment time by playlist age (for live streaming) 928 int64_t playlistTimeUs; 929 if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) { 930 int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs; 931 932 int64_t durationUs; 933 CHECK(mMeta->findInt64("segmentDurationUs", &durationUs)); 934 935 // round to nearest whole segment 936 playlistAgeUs = (playlistAgeUs + durationUs / 2) 937 / durationUs * durationUs; 938 939 segmentStartTimeUs -= playlistAgeUs; 940 if (segmentStartTimeUs < 0) { 941 segmentStartTimeUs = 0; 942 } 943 } 944 } 945 return segmentStartTimeUs; 946 } 947 948 bool operator <(const HLSTime &t0, const HLSTime &t1) { 949 // we can only compare discontinuity sequence and timestamp. 950 // (mSegmentTimeUs is not reliable in live streaming case, it's the 951 // time starting from beginning of playlist but playlist could change.) 952 return t0.mSeq < t1.mSeq 953 || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs); 954 } 955 956 void writeToAMessage(sp<AMessage> msg, const AudioPlaybackRate &rate) { 957 msg->setFloat("speed", rate.mSpeed); 958 msg->setFloat("pitch", rate.mPitch); 959 msg->setInt32("audio-fallback-mode", rate.mFallbackMode); 960 msg->setInt32("audio-stretch-mode", rate.mStretchMode); 961 } 962 963 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) { 964 *rate = AUDIO_PLAYBACK_RATE_DEFAULT; 965 CHECK(msg->findFloat("speed", &rate->mSpeed)); 966 CHECK(msg->findFloat("pitch", &rate->mPitch)); 967 CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode)); 968 CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode)); 969 } 970 971 void writeToAMessage(sp<AMessage> msg, const AVSyncSettings &sync, float videoFpsHint) { 972 msg->setInt32("sync-source", sync.mSource); 973 msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode); 974 msg->setFloat("tolerance", sync.mTolerance); 975 msg->setFloat("video-fps", videoFpsHint); 976 } 977 978 void readFromAMessage( 979 const sp<AMessage> &msg, 980 AVSyncSettings *sync /* nonnull */, 981 float *videoFps /* nonnull */) { 982 AVSyncSettings settings; 983 CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource)); 984 CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode)); 985 CHECK(msg->findFloat("tolerance", &settings.mTolerance)); 986 CHECK(msg->findFloat("video-fps", videoFps)); 987 *sync = settings; 988 } 989 990 } // namespace android 991 992