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