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