1 /* 2 * Copyright 2017, 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 "NdkWrapper" 19 20 #include <media/NdkWrapper.h> 21 22 #include <android/native_window.h> 23 #include <log/log.h> 24 #include <media/NdkMediaCodec.h> 25 #include <media/NdkMediaCrypto.h> 26 #include <media/NdkMediaDrm.h> 27 #include <media/NdkMediaFormat.h> 28 #include <media/NdkMediaExtractor.h> 29 #include <media/stagefright/MetaData.h> 30 #include <media/stagefright/foundation/ABuffer.h> 31 #include <media/stagefright/foundation/AMessage.h> 32 #include <utils/Errors.h> 33 34 // TODO: remove forward declaration when AMediaExtractor_disconnect is offcially added to NDK 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 media_status_t AMediaExtractor_disconnect(AMediaExtractor *); 41 42 #ifdef __cplusplus 43 } // extern "C" 44 #endif 45 46 namespace android { 47 48 static const size_t kAESBlockSize = 16; // AES_BLOCK_SIZE 49 50 static const char *AMediaFormatKeyGroupInt32[] = { 51 AMEDIAFORMAT_KEY_AAC_DRC_ATTENUATION_FACTOR, 52 AMEDIAFORMAT_KEY_AAC_DRC_BOOST_FACTOR, 53 AMEDIAFORMAT_KEY_AAC_DRC_HEAVY_COMPRESSION, 54 AMEDIAFORMAT_KEY_AAC_DRC_TARGET_REFERENCE_LEVEL, 55 AMEDIAFORMAT_KEY_AAC_ENCODED_TARGET_LEVEL, 56 AMEDIAFORMAT_KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT, 57 AMEDIAFORMAT_KEY_AAC_PROFILE, 58 AMEDIAFORMAT_KEY_AAC_SBR_MODE, 59 AMEDIAFORMAT_KEY_AUDIO_SESSION_ID, 60 AMEDIAFORMAT_KEY_BITRATE_MODE, 61 AMEDIAFORMAT_KEY_BIT_RATE, 62 AMEDIAFORMAT_KEY_CAPTURE_RATE, 63 AMEDIAFORMAT_KEY_CHANNEL_COUNT, 64 AMEDIAFORMAT_KEY_CHANNEL_MASK, 65 AMEDIAFORMAT_KEY_COLOR_FORMAT, 66 AMEDIAFORMAT_KEY_COLOR_RANGE, 67 AMEDIAFORMAT_KEY_COLOR_STANDARD, 68 AMEDIAFORMAT_KEY_COLOR_TRANSFER, 69 AMEDIAFORMAT_KEY_COMPLEXITY, 70 AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL, 71 AMEDIAFORMAT_KEY_GRID_COLUMNS, 72 AMEDIAFORMAT_KEY_GRID_ROWS, 73 AMEDIAFORMAT_KEY_HEIGHT, 74 AMEDIAFORMAT_KEY_INTRA_REFRESH_PERIOD, 75 AMEDIAFORMAT_KEY_IS_ADTS, 76 AMEDIAFORMAT_KEY_IS_AUTOSELECT, 77 AMEDIAFORMAT_KEY_IS_DEFAULT, 78 AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE, 79 AMEDIAFORMAT_KEY_LATENCY, 80 AMEDIAFORMAT_KEY_LEVEL, 81 AMEDIAFORMAT_KEY_MAX_HEIGHT, 82 AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, 83 AMEDIAFORMAT_KEY_MAX_WIDTH, 84 AMEDIAFORMAT_KEY_PCM_ENCODING, 85 AMEDIAFORMAT_KEY_PRIORITY, 86 AMEDIAFORMAT_KEY_PROFILE, 87 AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP, 88 AMEDIAFORMAT_KEY_ROTATION, 89 AMEDIAFORMAT_KEY_SAMPLE_RATE, 90 AMEDIAFORMAT_KEY_SLICE_HEIGHT, 91 AMEDIAFORMAT_KEY_STRIDE, 92 AMEDIAFORMAT_KEY_TRACK_ID, 93 AMEDIAFORMAT_KEY_WIDTH, 94 AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, 95 AMEDIAFORMAT_KEY_DISPLAY_WIDTH, 96 AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, 97 AMEDIAFORMAT_KEY_TILE_HEIGHT, 98 AMEDIAFORMAT_KEY_TILE_WIDTH, 99 AMEDIAFORMAT_KEY_TRACK_INDEX, 100 }; 101 102 static const char *AMediaFormatKeyGroupInt64[] = { 103 AMEDIAFORMAT_KEY_DURATION, 104 AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER, 105 AMEDIAFORMAT_KEY_TIME_US, 106 }; 107 108 static const char *AMediaFormatKeyGroupString[] = { 109 AMEDIAFORMAT_KEY_LANGUAGE, 110 AMEDIAFORMAT_KEY_MIME, 111 AMEDIAFORMAT_KEY_TEMPORAL_LAYERING, 112 }; 113 114 static const char *AMediaFormatKeyGroupBuffer[] = { 115 AMEDIAFORMAT_KEY_HDR_STATIC_INFO, 116 AMEDIAFORMAT_KEY_SEI, 117 AMEDIAFORMAT_KEY_MPEG_USER_DATA, 118 }; 119 120 static const char *AMediaFormatKeyGroupCsd[] = { 121 AMEDIAFORMAT_KEY_CSD_0, 122 AMEDIAFORMAT_KEY_CSD_1, 123 AMEDIAFORMAT_KEY_CSD_2, 124 }; 125 126 static const char *AMediaFormatKeyGroupRect[] = { 127 AMEDIAFORMAT_KEY_DISPLAY_CROP, 128 }; 129 130 static const char *AMediaFormatKeyGroupFloatInt32[] = { 131 AMEDIAFORMAT_KEY_FRAME_RATE, 132 AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 133 AMEDIAFORMAT_KEY_OPERATING_RATE, 134 }; 135 136 static status_t translateErrorCode(media_status_t err) { 137 if (err == AMEDIA_OK) { 138 return OK; 139 } else if (err == AMEDIA_ERROR_END_OF_STREAM) { 140 return ERROR_END_OF_STREAM; 141 } else if (err == AMEDIA_ERROR_IO) { 142 return ERROR_IO; 143 } else if (err == AMEDIACODEC_INFO_TRY_AGAIN_LATER) { 144 return -EAGAIN; 145 } 146 147 ALOGE("ndk error code: %d", err); 148 return UNKNOWN_ERROR; 149 } 150 151 static int32_t translateActionCode(int32_t actionCode) { 152 if (AMediaCodecActionCode_isTransient(actionCode)) { 153 return ACTION_CODE_TRANSIENT; 154 } else if (AMediaCodecActionCode_isRecoverable(actionCode)) { 155 return ACTION_CODE_RECOVERABLE; 156 } 157 return ACTION_CODE_FATAL; 158 } 159 160 static CryptoPlugin::Mode translateToCryptoPluginMode(cryptoinfo_mode_t mode) { 161 CryptoPlugin::Mode ret = CryptoPlugin::kMode_Unencrypted; 162 switch (mode) { 163 case AMEDIACODECRYPTOINFO_MODE_AES_CTR: { 164 ret = CryptoPlugin::kMode_AES_CTR; 165 break; 166 } 167 168 case AMEDIACODECRYPTOINFO_MODE_AES_WV: { 169 ret = CryptoPlugin::kMode_AES_WV; 170 break; 171 } 172 173 case AMEDIACODECRYPTOINFO_MODE_AES_CBC: { 174 ret = CryptoPlugin::kMode_AES_CBC; 175 break; 176 } 177 178 default: 179 break; 180 } 181 182 return ret; 183 } 184 185 static cryptoinfo_mode_t translateToCryptoInfoMode(CryptoPlugin::Mode mode) { 186 cryptoinfo_mode_t ret = AMEDIACODECRYPTOINFO_MODE_CLEAR; 187 switch (mode) { 188 case CryptoPlugin::kMode_AES_CTR: { 189 ret = AMEDIACODECRYPTOINFO_MODE_AES_CTR; 190 break; 191 } 192 193 case CryptoPlugin::kMode_AES_WV: { 194 ret = AMEDIACODECRYPTOINFO_MODE_AES_WV; 195 break; 196 } 197 198 case CryptoPlugin::kMode_AES_CBC: { 199 ret = AMEDIACODECRYPTOINFO_MODE_AES_CBC; 200 break; 201 } 202 203 default: 204 break; 205 } 206 207 return ret; 208 } 209 210 //////////// AMediaFormatWrapper 211 // static 212 sp<AMediaFormatWrapper> AMediaFormatWrapper::Create(const sp<AMessage> &message) { 213 sp<AMediaFormatWrapper> aMediaFormat = new AMediaFormatWrapper(); 214 215 for (size_t i = 0; i < message->countEntries(); ++i) { 216 AMessage::Type valueType; 217 const char *key = message->getEntryNameAt(i, &valueType); 218 219 switch (valueType) { 220 case AMessage::kTypeInt32: { 221 int32_t val; 222 if (!message->findInt32(key, &val)) { 223 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 224 continue; 225 } 226 aMediaFormat->setInt32(key, val); 227 break; 228 } 229 230 case AMessage::kTypeInt64: { 231 int64_t val; 232 if (!message->findInt64(key, &val)) { 233 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 234 continue; 235 } 236 aMediaFormat->setInt64(key, val); 237 break; 238 } 239 240 case AMessage::kTypeFloat: { 241 float val; 242 if (!message->findFloat(key, &val)) { 243 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 244 continue; 245 } 246 aMediaFormat->setFloat(key, val); 247 break; 248 } 249 250 case AMessage::kTypeDouble: { 251 double val; 252 if (!message->findDouble(key, &val)) { 253 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 254 continue; 255 } 256 aMediaFormat->setDouble(key, val); 257 break; 258 } 259 260 case AMessage::kTypeSize: { 261 size_t val; 262 if (!message->findSize(key, &val)) { 263 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 264 continue; 265 } 266 aMediaFormat->setSize(key, val); 267 break; 268 } 269 270 case AMessage::kTypeRect: { 271 int32_t left, top, right, bottom; 272 if (!message->findRect(key, &left, &top, &right, &bottom)) { 273 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 274 continue; 275 } 276 aMediaFormat->setRect(key, left, top, right, bottom); 277 break; 278 } 279 280 case AMessage::kTypeString: { 281 AString val; 282 if (!message->findString(key, &val)) { 283 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 284 continue; 285 } 286 aMediaFormat->setString(key, val); 287 break; 288 } 289 290 case AMessage::kTypeBuffer: { 291 sp<ABuffer> val; 292 if (!message->findBuffer(key, &val)) { 293 ALOGE("AMediaFormatWrapper::Create: error at item %zu", i); 294 continue; 295 } 296 aMediaFormat->setBuffer(key, val->data(), val->size()); 297 break; 298 } 299 300 default: { 301 break; 302 } 303 } 304 } 305 306 return aMediaFormat; 307 } 308 309 AMediaFormatWrapper::AMediaFormatWrapper() { 310 mAMediaFormat = AMediaFormat_new(); 311 } 312 313 AMediaFormatWrapper::AMediaFormatWrapper(AMediaFormat *aMediaFormat) 314 : mAMediaFormat(aMediaFormat) { 315 } 316 317 AMediaFormatWrapper::~AMediaFormatWrapper() { 318 release(); 319 } 320 321 status_t AMediaFormatWrapper::release() { 322 if (mAMediaFormat != NULL) { 323 media_status_t err = AMediaFormat_delete(mAMediaFormat); 324 mAMediaFormat = NULL; 325 return translateErrorCode(err); 326 } 327 return OK; 328 } 329 330 AMediaFormat *AMediaFormatWrapper::getAMediaFormat() const { 331 return mAMediaFormat; 332 } 333 334 sp<AMessage> AMediaFormatWrapper::toAMessage() const { 335 sp<AMessage> msg; 336 writeToAMessage(msg); 337 return msg; 338 } 339 340 void AMediaFormatWrapper::writeToAMessage(sp<AMessage> &msg) const { 341 if (mAMediaFormat == NULL) { 342 msg = NULL; 343 } 344 345 if (msg == NULL) { 346 msg = new AMessage; 347 } 348 for (auto& key : AMediaFormatKeyGroupInt32) { 349 int32_t val; 350 if (getInt32(key, &val)) { 351 msg->setInt32(key, val); 352 } 353 } 354 for (auto& key : AMediaFormatKeyGroupInt64) { 355 int64_t val; 356 if (getInt64(key, &val)) { 357 msg->setInt64(key, val); 358 } 359 } 360 for (auto& key : AMediaFormatKeyGroupString) { 361 AString val; 362 if (getString(key, &val)) { 363 msg->setString(key, val); 364 } 365 } 366 for (auto& key : AMediaFormatKeyGroupBuffer) { 367 void *data; 368 size_t size; 369 if (getBuffer(key, &data, &size)) { 370 sp<ABuffer> buffer = ABuffer::CreateAsCopy(data, size); 371 msg->setBuffer(key, buffer); 372 } 373 } 374 for (auto& key : AMediaFormatKeyGroupCsd) { 375 void *data; 376 size_t size; 377 if (getBuffer(key, &data, &size)) { 378 sp<ABuffer> buffer = ABuffer::CreateAsCopy(data, size); 379 buffer->meta()->setInt32(AMEDIAFORMAT_KEY_CSD, 1); 380 buffer->meta()->setInt64(AMEDIAFORMAT_KEY_TIME_US, 0); 381 msg->setBuffer(key, buffer); 382 } 383 } 384 for (auto& key : AMediaFormatKeyGroupRect) { 385 int32_t left, top, right, bottom; 386 if (getRect(key, &left, &top, &right, &bottom)) { 387 msg->setRect(key, left, top, right, bottom); 388 } 389 } 390 for (auto& key : AMediaFormatKeyGroupFloatInt32) { 391 float valFloat; 392 if (getFloat(key, &valFloat)) { 393 msg->setFloat(key, valFloat); 394 } else { 395 int32_t valInt32; 396 if (getInt32(key, &valInt32)) { 397 msg->setFloat(key, (float)valInt32); 398 } 399 } 400 } 401 } 402 403 const char* AMediaFormatWrapper::toString() const { 404 if (mAMediaFormat == NULL) { 405 return NULL; 406 } 407 return AMediaFormat_toString(mAMediaFormat); 408 } 409 410 bool AMediaFormatWrapper::getInt32(const char *name, int32_t *out) const { 411 if (mAMediaFormat == NULL) { 412 return false; 413 } 414 return AMediaFormat_getInt32(mAMediaFormat, name, out); 415 } 416 417 bool AMediaFormatWrapper::getInt64(const char *name, int64_t *out) const { 418 if (mAMediaFormat == NULL) { 419 return false; 420 } 421 return AMediaFormat_getInt64(mAMediaFormat, name, out); 422 } 423 424 bool AMediaFormatWrapper::getFloat(const char *name, float *out) const { 425 if (mAMediaFormat == NULL) { 426 return false; 427 } 428 return AMediaFormat_getFloat(mAMediaFormat, name, out); 429 } 430 431 bool AMediaFormatWrapper::getDouble(const char *name, double *out) const { 432 if (mAMediaFormat == NULL) { 433 return false; 434 } 435 return AMediaFormat_getDouble(mAMediaFormat, name, out); 436 } 437 438 bool AMediaFormatWrapper::getSize(const char *name, size_t *out) const { 439 if (mAMediaFormat == NULL) { 440 return false; 441 } 442 return AMediaFormat_getSize(mAMediaFormat, name, out); 443 } 444 445 bool AMediaFormatWrapper::getRect( 446 const char *name, int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const { 447 if (mAMediaFormat == NULL) { 448 return false; 449 } 450 return AMediaFormat_getRect(mAMediaFormat, name, left, top, right, bottom); 451 } 452 453 bool AMediaFormatWrapper::getBuffer(const char *name, void** data, size_t *outSize) const { 454 if (mAMediaFormat == NULL) { 455 return false; 456 } 457 return AMediaFormat_getBuffer(mAMediaFormat, name, data, outSize); 458 } 459 460 bool AMediaFormatWrapper::getString(const char *name, AString *out) const { 461 if (mAMediaFormat == NULL) { 462 return false; 463 } 464 const char *outChar = NULL; 465 bool ret = AMediaFormat_getString(mAMediaFormat, name, &outChar); 466 if (ret) { 467 *out = AString(outChar); 468 } 469 return ret; 470 } 471 472 void AMediaFormatWrapper::setInt32(const char* name, int32_t value) { 473 if (mAMediaFormat != NULL) { 474 AMediaFormat_setInt32(mAMediaFormat, name, value); 475 } 476 } 477 478 void AMediaFormatWrapper::setInt64(const char* name, int64_t value) { 479 if (mAMediaFormat != NULL) { 480 AMediaFormat_setInt64(mAMediaFormat, name, value); 481 } 482 } 483 484 void AMediaFormatWrapper::setFloat(const char* name, float value) { 485 if (mAMediaFormat != NULL) { 486 AMediaFormat_setFloat(mAMediaFormat, name, value); 487 } 488 } 489 490 void AMediaFormatWrapper::setDouble(const char* name, double value) { 491 if (mAMediaFormat != NULL) { 492 AMediaFormat_setDouble(mAMediaFormat, name, value); 493 } 494 } 495 496 void AMediaFormatWrapper::setSize(const char* name, size_t value) { 497 if (mAMediaFormat != NULL) { 498 AMediaFormat_setSize(mAMediaFormat, name, value); 499 } 500 } 501 502 void AMediaFormatWrapper::setRect( 503 const char* name, int32_t left, int32_t top, int32_t right, int32_t bottom) { 504 if (mAMediaFormat != NULL) { 505 AMediaFormat_setRect(mAMediaFormat, name, left, top, right, bottom); 506 } 507 } 508 509 void AMediaFormatWrapper::setString(const char* name, const AString &value) { 510 if (mAMediaFormat != NULL) { 511 AMediaFormat_setString(mAMediaFormat, name, value.c_str()); 512 } 513 } 514 515 void AMediaFormatWrapper::setBuffer(const char* name, void* data, size_t size) { 516 if (mAMediaFormat != NULL) { 517 AMediaFormat_setBuffer(mAMediaFormat, name, data, size); 518 } 519 } 520 521 522 //////////// ANativeWindowWrapper 523 ANativeWindowWrapper::ANativeWindowWrapper(ANativeWindow *aNativeWindow) 524 : mANativeWindow(aNativeWindow) { 525 if (aNativeWindow != NULL) { 526 ANativeWindow_acquire(aNativeWindow); 527 } 528 } 529 530 ANativeWindowWrapper::~ANativeWindowWrapper() { 531 release(); 532 } 533 534 status_t ANativeWindowWrapper::release() { 535 if (mANativeWindow != NULL) { 536 ANativeWindow_release(mANativeWindow); 537 mANativeWindow = NULL; 538 } 539 return OK; 540 } 541 542 ANativeWindow *ANativeWindowWrapper::getANativeWindow() const { 543 return mANativeWindow; 544 } 545 546 547 //////////// AMediaDrmWrapper 548 AMediaDrmWrapper::AMediaDrmWrapper(const uint8_t uuid[16]) { 549 mAMediaDrm = AMediaDrm_createByUUID(uuid); 550 } 551 552 AMediaDrmWrapper::AMediaDrmWrapper(AMediaDrm *aMediaDrm) 553 : mAMediaDrm(aMediaDrm) { 554 } 555 556 AMediaDrmWrapper::~AMediaDrmWrapper() { 557 release(); 558 } 559 560 status_t AMediaDrmWrapper::release() { 561 if (mAMediaDrm != NULL) { 562 AMediaDrm_release(mAMediaDrm); 563 mAMediaDrm = NULL; 564 } 565 return OK; 566 } 567 568 AMediaDrm *AMediaDrmWrapper::getAMediaDrm() const { 569 return mAMediaDrm; 570 } 571 572 // static 573 bool AMediaDrmWrapper::isCryptoSchemeSupported( 574 const uint8_t uuid[16], 575 const char *mimeType) { 576 return AMediaDrm_isCryptoSchemeSupported(uuid, mimeType); 577 } 578 579 580 //////////// AMediaCryptoWrapper 581 AMediaCryptoWrapper::AMediaCryptoWrapper( 582 const uint8_t uuid[16], const void *initData, size_t initDataSize) { 583 mAMediaCrypto = AMediaCrypto_new(uuid, initData, initDataSize); 584 } 585 586 AMediaCryptoWrapper::AMediaCryptoWrapper(AMediaCrypto *aMediaCrypto) 587 : mAMediaCrypto(aMediaCrypto) { 588 } 589 590 AMediaCryptoWrapper::~AMediaCryptoWrapper() { 591 release(); 592 } 593 594 status_t AMediaCryptoWrapper::release() { 595 if (mAMediaCrypto != NULL) { 596 AMediaCrypto_delete(mAMediaCrypto); 597 mAMediaCrypto = NULL; 598 } 599 return OK; 600 } 601 602 AMediaCrypto *AMediaCryptoWrapper::getAMediaCrypto() const { 603 return mAMediaCrypto; 604 } 605 606 bool AMediaCryptoWrapper::isCryptoSchemeSupported(const uint8_t uuid[16]) { 607 if (mAMediaCrypto == NULL) { 608 return false; 609 } 610 return AMediaCrypto_isCryptoSchemeSupported(uuid); 611 } 612 613 bool AMediaCryptoWrapper::requiresSecureDecoderComponent(const char *mime) { 614 if (mAMediaCrypto == NULL) { 615 return false; 616 } 617 return AMediaCrypto_requiresSecureDecoderComponent(mime); 618 } 619 620 621 //////////// AMediaCodecCryptoInfoWrapper 622 // static 623 sp<AMediaCodecCryptoInfoWrapper> AMediaCodecCryptoInfoWrapper::Create(MetaDataBase &meta) { 624 625 uint32_t type; 626 const void *crypteddata; 627 size_t cryptedsize; 628 629 if (!meta.findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) { 630 return NULL; 631 } 632 633 int numSubSamples = cryptedsize / sizeof(size_t); 634 635 if (numSubSamples <= 0) { 636 ALOGE("Create: INVALID numSubSamples: %d", numSubSamples); 637 return NULL; 638 } 639 640 const void *cleardata; 641 size_t clearsize; 642 if (meta.findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) { 643 if (clearsize != cryptedsize) { 644 // The two must be of the same length. 645 ALOGE("Create: mismatch cryptedsize: %zu != clearsize: %zu", cryptedsize, clearsize); 646 return NULL; 647 } 648 } 649 650 const void *key; 651 size_t keysize; 652 if (meta.findData(kKeyCryptoKey, &type, &key, &keysize)) { 653 if (keysize != kAESBlockSize) { 654 // Keys must be 16 bytes in length. 655 ALOGE("Create: Keys must be %zu bytes in length: %zu", kAESBlockSize, keysize); 656 return NULL; 657 } 658 } 659 660 const void *iv; 661 size_t ivsize; 662 if (meta.findData(kKeyCryptoIV, &type, &iv, &ivsize)) { 663 if (ivsize != kAESBlockSize) { 664 // IVs must be 16 bytes in length. 665 ALOGE("Create: IV must be %zu bytes in length: %zu", kAESBlockSize, ivsize); 666 return NULL; 667 } 668 } 669 670 int32_t mode; 671 if (!meta.findInt32(kKeyCryptoMode, &mode)) { 672 mode = CryptoPlugin::kMode_AES_CTR; 673 } 674 675 return new AMediaCodecCryptoInfoWrapper( 676 numSubSamples, 677 (uint8_t*) key, 678 (uint8_t*) iv, 679 (CryptoPlugin::Mode)mode, 680 (size_t*) cleardata, 681 (size_t*) crypteddata); 682 } 683 684 AMediaCodecCryptoInfoWrapper::AMediaCodecCryptoInfoWrapper( 685 int numsubsamples, 686 uint8_t key[16], 687 uint8_t iv[16], 688 CryptoPlugin::Mode mode, 689 size_t *clearbytes, 690 size_t *encryptedbytes) { 691 mAMediaCodecCryptoInfo = 692 AMediaCodecCryptoInfo_new(numsubsamples, 693 key, 694 iv, 695 translateToCryptoInfoMode(mode), 696 clearbytes, 697 encryptedbytes); 698 } 699 700 AMediaCodecCryptoInfoWrapper::AMediaCodecCryptoInfoWrapper( 701 AMediaCodecCryptoInfo *aMediaCodecCryptoInfo) 702 : mAMediaCodecCryptoInfo(aMediaCodecCryptoInfo) { 703 } 704 705 AMediaCodecCryptoInfoWrapper::~AMediaCodecCryptoInfoWrapper() { 706 release(); 707 } 708 709 status_t AMediaCodecCryptoInfoWrapper::release() { 710 if (mAMediaCodecCryptoInfo != NULL) { 711 media_status_t err = AMediaCodecCryptoInfo_delete(mAMediaCodecCryptoInfo); 712 mAMediaCodecCryptoInfo = NULL; 713 return translateErrorCode(err); 714 } 715 return OK; 716 } 717 718 AMediaCodecCryptoInfo *AMediaCodecCryptoInfoWrapper::getAMediaCodecCryptoInfo() const { 719 return mAMediaCodecCryptoInfo; 720 } 721 722 void AMediaCodecCryptoInfoWrapper::setPattern(CryptoPlugin::Pattern *pattern) { 723 if (mAMediaCodecCryptoInfo == NULL || pattern == NULL) { 724 return; 725 } 726 cryptoinfo_pattern_t ndkPattern = {(int32_t)pattern->mEncryptBlocks, 727 (int32_t)pattern->mSkipBlocks }; 728 return AMediaCodecCryptoInfo_setPattern(mAMediaCodecCryptoInfo, &ndkPattern); 729 } 730 731 size_t AMediaCodecCryptoInfoWrapper::getNumSubSamples() { 732 if (mAMediaCodecCryptoInfo == NULL) { 733 return 0; 734 } 735 return AMediaCodecCryptoInfo_getNumSubSamples(mAMediaCodecCryptoInfo); 736 } 737 738 status_t AMediaCodecCryptoInfoWrapper::getKey(uint8_t *dst) { 739 if (mAMediaCodecCryptoInfo == NULL) { 740 return DEAD_OBJECT; 741 } 742 if (dst == NULL) { 743 return BAD_VALUE; 744 } 745 return translateErrorCode( 746 AMediaCodecCryptoInfo_getKey(mAMediaCodecCryptoInfo, dst)); 747 } 748 749 status_t AMediaCodecCryptoInfoWrapper::getIV(uint8_t *dst) { 750 if (mAMediaCodecCryptoInfo == NULL) { 751 return DEAD_OBJECT; 752 } 753 if (dst == NULL) { 754 return BAD_VALUE; 755 } 756 return translateErrorCode( 757 AMediaCodecCryptoInfo_getIV(mAMediaCodecCryptoInfo, dst)); 758 } 759 760 CryptoPlugin::Mode AMediaCodecCryptoInfoWrapper::getMode() { 761 if (mAMediaCodecCryptoInfo == NULL) { 762 return CryptoPlugin::kMode_Unencrypted; 763 } 764 return translateToCryptoPluginMode( 765 AMediaCodecCryptoInfo_getMode(mAMediaCodecCryptoInfo)); 766 } 767 768 status_t AMediaCodecCryptoInfoWrapper::getClearBytes(size_t *dst) { 769 if (mAMediaCodecCryptoInfo == NULL) { 770 return DEAD_OBJECT; 771 } 772 if (dst == NULL) { 773 return BAD_VALUE; 774 } 775 return translateErrorCode( 776 AMediaCodecCryptoInfo_getClearBytes(mAMediaCodecCryptoInfo, dst)); 777 } 778 779 status_t AMediaCodecCryptoInfoWrapper::getEncryptedBytes(size_t *dst) { 780 if (mAMediaCodecCryptoInfo == NULL) { 781 return DEAD_OBJECT; 782 } 783 if (dst == NULL) { 784 return BAD_VALUE; 785 } 786 return translateErrorCode( 787 AMediaCodecCryptoInfo_getEncryptedBytes(mAMediaCodecCryptoInfo, dst)); 788 } 789 790 791 //////////// AMediaCodecWrapper 792 // static 793 sp<AMediaCodecWrapper> AMediaCodecWrapper::CreateCodecByName(const AString &name) { 794 AMediaCodec *aMediaCodec = AMediaCodec_createCodecByName(name.c_str()); 795 return new AMediaCodecWrapper(aMediaCodec); 796 } 797 798 // static 799 sp<AMediaCodecWrapper> AMediaCodecWrapper::CreateDecoderByType(const AString &mimeType) { 800 AMediaCodec *aMediaCodec = AMediaCodec_createDecoderByType(mimeType.c_str()); 801 return new AMediaCodecWrapper(aMediaCodec); 802 } 803 804 // static 805 void AMediaCodecWrapper::OnInputAvailableCB( 806 AMediaCodec * /* aMediaCodec */, 807 void *userdata, 808 int32_t index) { 809 ALOGV("OnInputAvailableCB: index(%d)", index); 810 sp<AMessage> msg = sp<AMessage>((AMessage *)userdata)->dup(); 811 msg->setInt32("callbackID", CB_INPUT_AVAILABLE); 812 msg->setInt32("index", index); 813 msg->post(); 814 } 815 816 // static 817 void AMediaCodecWrapper::OnOutputAvailableCB( 818 AMediaCodec * /* aMediaCodec */, 819 void *userdata, 820 int32_t index, 821 AMediaCodecBufferInfo *bufferInfo) { 822 ALOGV("OnOutputAvailableCB: index(%d), (%d, %d, %lld, 0x%x)", 823 index, bufferInfo->offset, bufferInfo->size, 824 (long long)bufferInfo->presentationTimeUs, bufferInfo->flags); 825 sp<AMessage> msg = sp<AMessage>((AMessage *)userdata)->dup(); 826 msg->setInt32("callbackID", CB_OUTPUT_AVAILABLE); 827 msg->setInt32("index", index); 828 msg->setSize("offset", (size_t)(bufferInfo->offset)); 829 msg->setSize("size", (size_t)(bufferInfo->size)); 830 msg->setInt64("timeUs", bufferInfo->presentationTimeUs); 831 msg->setInt32("flags", (int32_t)(bufferInfo->flags)); 832 msg->post(); 833 } 834 835 // static 836 void AMediaCodecWrapper::OnFormatChangedCB( 837 AMediaCodec * /* aMediaCodec */, 838 void *userdata, 839 AMediaFormat *format) { 840 sp<AMediaFormatWrapper> formatWrapper = new AMediaFormatWrapper(format); 841 sp<AMessage> outputFormat = formatWrapper->toAMessage(); 842 ALOGV("OnFormatChangedCB: format(%s)", outputFormat->debugString().c_str()); 843 844 sp<AMessage> msg = sp<AMessage>((AMessage *)userdata)->dup(); 845 msg->setInt32("callbackID", CB_OUTPUT_FORMAT_CHANGED); 846 msg->setMessage("format", outputFormat); 847 msg->post(); 848 } 849 850 // static 851 void AMediaCodecWrapper::OnErrorCB( 852 AMediaCodec * /* aMediaCodec */, 853 void *userdata, 854 media_status_t err, 855 int32_t actionCode, 856 const char *detail) { 857 ALOGV("OnErrorCB: err(%d), actionCode(%d), detail(%s)", err, actionCode, detail); 858 sp<AMessage> msg = sp<AMessage>((AMessage *)userdata)->dup(); 859 msg->setInt32("callbackID", CB_ERROR); 860 msg->setInt32("err", translateErrorCode(err)); 861 msg->setInt32("actionCode", translateActionCode(actionCode)); 862 msg->setString("detail", detail); 863 msg->post(); 864 } 865 866 AMediaCodecWrapper::AMediaCodecWrapper(AMediaCodec *aMediaCodec) 867 : mAMediaCodec(aMediaCodec) { 868 } 869 870 AMediaCodecWrapper::~AMediaCodecWrapper() { 871 release(); 872 } 873 874 status_t AMediaCodecWrapper::release() { 875 if (mAMediaCodec != NULL) { 876 AMediaCodecOnAsyncNotifyCallback aCB = {}; 877 AMediaCodec_setAsyncNotifyCallback(mAMediaCodec, aCB, NULL); 878 mCallback = NULL; 879 880 media_status_t err = AMediaCodec_delete(mAMediaCodec); 881 mAMediaCodec = NULL; 882 return translateErrorCode(err); 883 } 884 return OK; 885 } 886 887 AMediaCodec *AMediaCodecWrapper::getAMediaCodec() const { 888 return mAMediaCodec; 889 } 890 891 status_t AMediaCodecWrapper::getName(AString *outComponentName) const { 892 if (mAMediaCodec == NULL) { 893 return DEAD_OBJECT; 894 } 895 char *name = NULL; 896 media_status_t err = AMediaCodec_getName(mAMediaCodec, &name); 897 if (err != AMEDIA_OK) { 898 return translateErrorCode(err); 899 } 900 901 *outComponentName = AString(name); 902 AMediaCodec_releaseName(mAMediaCodec, name); 903 return OK; 904 } 905 906 status_t AMediaCodecWrapper::configure( 907 const sp<AMediaFormatWrapper> &format, 908 const sp<ANativeWindowWrapper> &nww, 909 const sp<AMediaCryptoWrapper> &crypto, 910 uint32_t flags) { 911 if (mAMediaCodec == NULL) { 912 return DEAD_OBJECT; 913 } 914 915 media_status_t err = AMediaCodec_configure( 916 mAMediaCodec, 917 format->getAMediaFormat(), 918 (nww == NULL ? NULL : nww->getANativeWindow()), 919 crypto == NULL ? NULL : crypto->getAMediaCrypto(), 920 flags); 921 922 return translateErrorCode(err); 923 } 924 925 status_t AMediaCodecWrapper::setCallback(const sp<AMessage> &callback) { 926 if (mAMediaCodec == NULL) { 927 return DEAD_OBJECT; 928 } 929 930 mCallback = callback; 931 932 AMediaCodecOnAsyncNotifyCallback aCB = { 933 OnInputAvailableCB, 934 OnOutputAvailableCB, 935 OnFormatChangedCB, 936 OnErrorCB 937 }; 938 939 return translateErrorCode( 940 AMediaCodec_setAsyncNotifyCallback(mAMediaCodec, aCB, callback.get())); 941 } 942 943 status_t AMediaCodecWrapper::releaseCrypto() { 944 if (mAMediaCodec == NULL) { 945 return DEAD_OBJECT; 946 } 947 return translateErrorCode(AMediaCodec_releaseCrypto(mAMediaCodec)); 948 } 949 950 status_t AMediaCodecWrapper::start() { 951 if (mAMediaCodec == NULL) { 952 return DEAD_OBJECT; 953 } 954 return translateErrorCode(AMediaCodec_start(mAMediaCodec)); 955 } 956 957 status_t AMediaCodecWrapper::stop() { 958 if (mAMediaCodec == NULL) { 959 return DEAD_OBJECT; 960 } 961 return translateErrorCode(AMediaCodec_stop(mAMediaCodec)); 962 } 963 964 status_t AMediaCodecWrapper::flush() { 965 if (mAMediaCodec == NULL) { 966 return DEAD_OBJECT; 967 } 968 return translateErrorCode(AMediaCodec_flush(mAMediaCodec)); 969 } 970 971 uint8_t* AMediaCodecWrapper::getInputBuffer(size_t idx, size_t *out_size) { 972 if (mAMediaCodec == NULL) { 973 return NULL; 974 } 975 return AMediaCodec_getInputBuffer(mAMediaCodec, idx, out_size); 976 } 977 978 uint8_t* AMediaCodecWrapper::getOutputBuffer(size_t idx, size_t *out_size) { 979 if (mAMediaCodec == NULL) { 980 return NULL; 981 } 982 return AMediaCodec_getOutputBuffer(mAMediaCodec, idx, out_size); 983 } 984 985 status_t AMediaCodecWrapper::queueInputBuffer( 986 size_t idx, 987 size_t offset, 988 size_t size, 989 uint64_t time, 990 uint32_t flags) { 991 if (mAMediaCodec == NULL) { 992 return DEAD_OBJECT; 993 } 994 return translateErrorCode( 995 AMediaCodec_queueInputBuffer(mAMediaCodec, idx, offset, size, time, flags)); 996 } 997 998 status_t AMediaCodecWrapper::queueSecureInputBuffer( 999 size_t idx, 1000 size_t offset, 1001 sp<AMediaCodecCryptoInfoWrapper> &codecCryptoInfo, 1002 uint64_t time, 1003 uint32_t flags) { 1004 if (mAMediaCodec == NULL) { 1005 return DEAD_OBJECT; 1006 } 1007 return translateErrorCode( 1008 AMediaCodec_queueSecureInputBuffer( 1009 mAMediaCodec, 1010 idx, 1011 offset, 1012 codecCryptoInfo->getAMediaCodecCryptoInfo(), 1013 time, 1014 flags)); 1015 } 1016 1017 sp<AMediaFormatWrapper> AMediaCodecWrapper::getOutputFormat() { 1018 if (mAMediaCodec == NULL) { 1019 return NULL; 1020 } 1021 return new AMediaFormatWrapper(AMediaCodec_getOutputFormat(mAMediaCodec)); 1022 } 1023 1024 sp<AMediaFormatWrapper> AMediaCodecWrapper::getInputFormat() { 1025 if (mAMediaCodec == NULL) { 1026 return NULL; 1027 } 1028 return new AMediaFormatWrapper(AMediaCodec_getInputFormat(mAMediaCodec)); 1029 } 1030 1031 status_t AMediaCodecWrapper::releaseOutputBuffer(size_t idx, bool render) { 1032 if (mAMediaCodec == NULL) { 1033 return DEAD_OBJECT; 1034 } 1035 return translateErrorCode( 1036 AMediaCodec_releaseOutputBuffer(mAMediaCodec, idx, render)); 1037 } 1038 1039 status_t AMediaCodecWrapper::setOutputSurface(const sp<ANativeWindowWrapper> &nww) { 1040 if (mAMediaCodec == NULL) { 1041 return DEAD_OBJECT; 1042 } 1043 return translateErrorCode( 1044 AMediaCodec_setOutputSurface(mAMediaCodec, 1045 (nww == NULL ? NULL : nww->getANativeWindow()))); 1046 } 1047 1048 status_t AMediaCodecWrapper::releaseOutputBufferAtTime(size_t idx, int64_t timestampNs) { 1049 if (mAMediaCodec == NULL) { 1050 return DEAD_OBJECT; 1051 } 1052 return translateErrorCode( 1053 AMediaCodec_releaseOutputBufferAtTime(mAMediaCodec, idx, timestampNs)); 1054 } 1055 1056 status_t AMediaCodecWrapper::setParameters(const sp<AMediaFormatWrapper> ¶ms) { 1057 if (mAMediaCodec == NULL) { 1058 return DEAD_OBJECT; 1059 } 1060 return translateErrorCode( 1061 AMediaCodec_setParameters(mAMediaCodec, params->getAMediaFormat())); 1062 } 1063 1064 //////////// AMediaExtractorWrapper 1065 1066 AMediaExtractorWrapper::AMediaExtractorWrapper(AMediaExtractor *aMediaExtractor) 1067 : mAMediaExtractor(aMediaExtractor) { 1068 } 1069 1070 AMediaExtractorWrapper::~AMediaExtractorWrapper() { 1071 release(); 1072 } 1073 1074 status_t AMediaExtractorWrapper::release() { 1075 if (mAMediaExtractor != NULL) { 1076 media_status_t err = AMediaExtractor_delete(mAMediaExtractor); 1077 mAMediaExtractor = NULL; 1078 return translateErrorCode(err); 1079 } 1080 return OK; 1081 } 1082 1083 status_t AMediaExtractorWrapper::disconnect() { 1084 if (mAMediaExtractor != NULL) { 1085 media_status_t err = AMediaExtractor_disconnect(mAMediaExtractor); 1086 return translateErrorCode(err); 1087 } 1088 return DEAD_OBJECT; 1089 } 1090 1091 AMediaExtractor *AMediaExtractorWrapper::getAMediaExtractor() const { 1092 return mAMediaExtractor; 1093 } 1094 1095 status_t AMediaExtractorWrapper::setDataSource(int fd, off64_t offset, off64_t length) { 1096 if (mAMediaExtractor == NULL) { 1097 return DEAD_OBJECT; 1098 } 1099 return translateErrorCode(AMediaExtractor_setDataSourceFd( 1100 mAMediaExtractor, fd, offset, length)); 1101 } 1102 1103 status_t AMediaExtractorWrapper::setDataSource(const char *location) { 1104 if (mAMediaExtractor == NULL) { 1105 return DEAD_OBJECT; 1106 } 1107 return translateErrorCode(AMediaExtractor_setDataSource(mAMediaExtractor, location)); 1108 } 1109 1110 status_t AMediaExtractorWrapper::setDataSource(AMediaDataSource *source) { 1111 if (mAMediaExtractor == NULL) { 1112 return DEAD_OBJECT; 1113 } 1114 return translateErrorCode(AMediaExtractor_setDataSourceCustom(mAMediaExtractor, source)); 1115 } 1116 1117 size_t AMediaExtractorWrapper::getTrackCount() { 1118 if (mAMediaExtractor == NULL) { 1119 return 0; 1120 } 1121 return AMediaExtractor_getTrackCount(mAMediaExtractor); 1122 } 1123 1124 sp<AMediaFormatWrapper> AMediaExtractorWrapper::getFormat() { 1125 if (mAMediaExtractor == NULL) { 1126 return NULL; 1127 } 1128 return new AMediaFormatWrapper(AMediaExtractor_getFileFormat(mAMediaExtractor)); 1129 } 1130 1131 sp<AMediaFormatWrapper> AMediaExtractorWrapper::getTrackFormat(size_t idx) { 1132 if (mAMediaExtractor == NULL) { 1133 return NULL; 1134 } 1135 return new AMediaFormatWrapper(AMediaExtractor_getTrackFormat(mAMediaExtractor, idx)); 1136 } 1137 1138 status_t AMediaExtractorWrapper::selectTrack(size_t idx) { 1139 if (mAMediaExtractor == NULL) { 1140 return DEAD_OBJECT; 1141 } 1142 return translateErrorCode(AMediaExtractor_selectTrack(mAMediaExtractor, idx)); 1143 } 1144 1145 status_t AMediaExtractorWrapper::unselectTrack(size_t idx) { 1146 if (mAMediaExtractor == NULL) { 1147 return DEAD_OBJECT; 1148 } 1149 return translateErrorCode(AMediaExtractor_unselectTrack(mAMediaExtractor, idx)); 1150 } 1151 1152 status_t AMediaExtractorWrapper::selectSingleTrack(size_t idx) { 1153 if (mAMediaExtractor == NULL) { 1154 return DEAD_OBJECT; 1155 } 1156 for (size_t i = 0; i < AMediaExtractor_getTrackCount(mAMediaExtractor); ++i) { 1157 if (i == idx) { 1158 media_status_t err = AMediaExtractor_selectTrack(mAMediaExtractor, i); 1159 if (err != AMEDIA_OK) { 1160 return translateErrorCode(err); 1161 } 1162 } else { 1163 media_status_t err = AMediaExtractor_unselectTrack(mAMediaExtractor, i); 1164 if (err != AMEDIA_OK) { 1165 return translateErrorCode(err); 1166 } 1167 } 1168 } 1169 return OK; 1170 } 1171 1172 ssize_t AMediaExtractorWrapper::readSampleData(const sp<ABuffer> &buffer) { 1173 if (mAMediaExtractor == NULL) { 1174 return -1; 1175 } 1176 return AMediaExtractor_readSampleData(mAMediaExtractor, buffer->data(), buffer->capacity()); 1177 } 1178 1179 ssize_t AMediaExtractorWrapper::getSampleSize() { 1180 if (mAMediaExtractor == NULL) { 1181 return 0; 1182 } 1183 return AMediaExtractor_getSampleSize(mAMediaExtractor); 1184 } 1185 1186 uint32_t AMediaExtractorWrapper::getSampleFlags() { 1187 if (mAMediaExtractor == NULL) { 1188 return 0; 1189 } 1190 return AMediaExtractor_getSampleFlags(mAMediaExtractor); 1191 } 1192 1193 int AMediaExtractorWrapper::getSampleTrackIndex() { 1194 if (mAMediaExtractor == NULL) { 1195 return -1; 1196 } 1197 return AMediaExtractor_getSampleTrackIndex(mAMediaExtractor); 1198 } 1199 1200 int64_t AMediaExtractorWrapper::getSampleTime() { 1201 if (mAMediaExtractor == NULL) { 1202 return -1; 1203 } 1204 return AMediaExtractor_getSampleTime(mAMediaExtractor); 1205 } 1206 1207 status_t AMediaExtractorWrapper::getSampleFormat(sp<AMediaFormatWrapper> &formatWrapper) { 1208 if (mAMediaExtractor == NULL) { 1209 return DEAD_OBJECT; 1210 } 1211 AMediaFormat *format = AMediaFormat_new(); 1212 formatWrapper = new AMediaFormatWrapper(format); 1213 return translateErrorCode(AMediaExtractor_getSampleFormat(mAMediaExtractor, format)); 1214 } 1215 1216 int64_t AMediaExtractorWrapper::getCachedDuration() { 1217 if (mAMediaExtractor == NULL) { 1218 return -1; 1219 } 1220 return AMediaExtractor_getCachedDuration(mAMediaExtractor); 1221 } 1222 1223 bool AMediaExtractorWrapper::advance() { 1224 if (mAMediaExtractor == NULL) { 1225 return false; 1226 } 1227 return AMediaExtractor_advance(mAMediaExtractor); 1228 } 1229 1230 status_t AMediaExtractorWrapper::seekTo(int64_t seekPosUs, MediaSource::ReadOptions::SeekMode mode) { 1231 if (mAMediaExtractor == NULL) { 1232 return DEAD_OBJECT; 1233 } 1234 1235 SeekMode aMode; 1236 switch (mode) { 1237 case MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC: { 1238 aMode = AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC; 1239 break; 1240 } 1241 case MediaSource::ReadOptions::SEEK_NEXT_SYNC: { 1242 aMode = AMEDIAEXTRACTOR_SEEK_NEXT_SYNC; 1243 break; 1244 } 1245 default: { 1246 aMode = AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC; 1247 break; 1248 } 1249 } 1250 return AMediaExtractor_seekTo(mAMediaExtractor, seekPosUs, aMode); 1251 } 1252 1253 PsshInfo* AMediaExtractorWrapper::getPsshInfo() { 1254 if (mAMediaExtractor == NULL) { 1255 return NULL; 1256 } 1257 return AMediaExtractor_getPsshInfo(mAMediaExtractor); 1258 } 1259 1260 sp<AMediaCodecCryptoInfoWrapper> AMediaExtractorWrapper::getSampleCryptoInfo() { 1261 if (mAMediaExtractor == NULL) { 1262 return NULL; 1263 } 1264 return new AMediaCodecCryptoInfoWrapper(AMediaExtractor_getSampleCryptoInfo(mAMediaExtractor)); 1265 } 1266 1267 ssize_t AMediaDataSourceWrapper::AMediaDataSourceWrapper_getSize(void *userdata) { 1268 DataSource *source = static_cast<DataSource *>(userdata); 1269 off64_t size = -1; 1270 source->getSize(&size); 1271 return size; 1272 } 1273 1274 ssize_t AMediaDataSourceWrapper::AMediaDataSourceWrapper_readAt(void *userdata, off64_t offset, void * buf, size_t size) { 1275 DataSource *source = static_cast<DataSource *>(userdata); 1276 return source->readAt(offset, buf, size); 1277 } 1278 1279 void AMediaDataSourceWrapper::AMediaDataSourceWrapper_close(void *userdata) { 1280 DataSource *source = static_cast<DataSource *>(userdata); 1281 source->close(); 1282 } 1283 1284 AMediaDataSourceWrapper::AMediaDataSourceWrapper(const sp<DataSource> &dataSource) 1285 : mDataSource(dataSource), 1286 mAMediaDataSource(AMediaDataSource_new()) { 1287 ALOGV("setDataSource (source: %p)", dataSource.get()); 1288 AMediaDataSource_setUserdata(mAMediaDataSource, dataSource.get()); 1289 AMediaDataSource_setReadAt(mAMediaDataSource, AMediaDataSourceWrapper_readAt); 1290 AMediaDataSource_setGetSize(mAMediaDataSource, AMediaDataSourceWrapper_getSize); 1291 AMediaDataSource_setClose(mAMediaDataSource, AMediaDataSourceWrapper_close); 1292 } 1293 1294 AMediaDataSourceWrapper::~AMediaDataSourceWrapper() { 1295 if (mAMediaDataSource == NULL) { 1296 return; 1297 } 1298 AMediaDataSource_delete(mAMediaDataSource); 1299 mAMediaDataSource = NULL; 1300 } 1301 1302 AMediaDataSource* AMediaDataSourceWrapper::getAMediaDataSource() { 1303 return mAMediaDataSource; 1304 } 1305 1306 } // namespace android 1307