MediaCodec.cpp revision 1d15ab58bf8239069ef343de6cb21aabf3ef7d78
1/* 2 * Copyright 2012, 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 "MediaCodec" 19#include <inttypes.h> 20 21#include "include/avc_utils.h" 22#include "include/SoftwareRenderer.h" 23 24#include <binder/IBatteryStats.h> 25#include <binder/IServiceManager.h> 26#include <gui/Surface.h> 27#include <media/ICrypto.h> 28#include <media/stagefright/foundation/ABuffer.h> 29#include <media/stagefright/foundation/ADebug.h> 30#include <media/stagefright/foundation/AMessage.h> 31#include <media/stagefright/foundation/AString.h> 32#include <media/stagefright/foundation/hexdump.h> 33#include <media/stagefright/ACodec.h> 34#include <media/stagefright/BufferProducerWrapper.h> 35#include <media/stagefright/MediaCodec.h> 36#include <media/stagefright/MediaCodecList.h> 37#include <media/stagefright/MediaDefs.h> 38#include <media/stagefright/MediaErrors.h> 39#include <media/stagefright/MediaFilter.h> 40#include <media/stagefright/MetaData.h> 41#include <media/stagefright/NativeWindowWrapper.h> 42#include <private/android_filesystem_config.h> 43#include <utils/Log.h> 44#include <utils/Singleton.h> 45 46namespace android { 47 48struct MediaCodec::BatteryNotifier : public Singleton<BatteryNotifier> { 49 BatteryNotifier(); 50 51 void noteStartVideo(); 52 void noteStopVideo(); 53 void noteStartAudio(); 54 void noteStopAudio(); 55 56private: 57 int32_t mVideoRefCount; 58 int32_t mAudioRefCount; 59 sp<IBatteryStats> mBatteryStatService; 60}; 61 62ANDROID_SINGLETON_STATIC_INSTANCE(MediaCodec::BatteryNotifier) 63 64MediaCodec::BatteryNotifier::BatteryNotifier() : 65 mVideoRefCount(0), 66 mAudioRefCount(0) { 67 // get battery service 68 const sp<IServiceManager> sm(defaultServiceManager()); 69 if (sm != NULL) { 70 const String16 name("batterystats"); 71 mBatteryStatService = interface_cast<IBatteryStats>(sm->getService(name)); 72 if (mBatteryStatService == NULL) { 73 ALOGE("batterystats service unavailable!"); 74 } 75 } 76} 77 78void MediaCodec::BatteryNotifier::noteStartVideo() { 79 if (mVideoRefCount == 0 && mBatteryStatService != NULL) { 80 mBatteryStatService->noteStartVideo(AID_MEDIA); 81 } 82 mVideoRefCount++; 83} 84 85void MediaCodec::BatteryNotifier::noteStopVideo() { 86 if (mVideoRefCount == 0) { 87 ALOGW("BatteryNotifier::noteStop(): video refcount is broken!"); 88 return; 89 } 90 91 mVideoRefCount--; 92 if (mVideoRefCount == 0 && mBatteryStatService != NULL) { 93 mBatteryStatService->noteStopVideo(AID_MEDIA); 94 } 95} 96 97void MediaCodec::BatteryNotifier::noteStartAudio() { 98 if (mAudioRefCount == 0 && mBatteryStatService != NULL) { 99 mBatteryStatService->noteStartAudio(AID_MEDIA); 100 } 101 mAudioRefCount++; 102} 103 104void MediaCodec::BatteryNotifier::noteStopAudio() { 105 if (mAudioRefCount == 0) { 106 ALOGW("BatteryNotifier::noteStop(): audio refcount is broken!"); 107 return; 108 } 109 110 mAudioRefCount--; 111 if (mAudioRefCount == 0 && mBatteryStatService != NULL) { 112 mBatteryStatService->noteStopAudio(AID_MEDIA); 113 } 114} 115// static 116sp<MediaCodec> MediaCodec::CreateByType( 117 const sp<ALooper> &looper, const char *mime, bool encoder, status_t *err) { 118 sp<MediaCodec> codec = new MediaCodec(looper); 119 120 const status_t ret = codec->init(mime, true /* nameIsType */, encoder); 121 if (err != NULL) { 122 *err = ret; 123 } 124 return ret == OK ? codec : NULL; // NULL deallocates codec. 125} 126 127// static 128sp<MediaCodec> MediaCodec::CreateByComponentName( 129 const sp<ALooper> &looper, const char *name, status_t *err) { 130 sp<MediaCodec> codec = new MediaCodec(looper); 131 132 const status_t ret = codec->init(name, false /* nameIsType */, false /* encoder */); 133 if (err != NULL) { 134 *err = ret; 135 } 136 return ret == OK ? codec : NULL; // NULL deallocates codec. 137} 138 139MediaCodec::MediaCodec(const sp<ALooper> &looper) 140 : mState(UNINITIALIZED), 141 mLooper(looper), 142 mCodec(NULL), 143 mReplyID(0), 144 mFlags(0), 145 mStickyError(OK), 146 mSoftRenderer(NULL), 147 mBatteryStatNotified(false), 148 mIsVideo(false), 149 mDequeueInputTimeoutGeneration(0), 150 mDequeueInputReplyID(0), 151 mDequeueOutputTimeoutGeneration(0), 152 mDequeueOutputReplyID(0), 153 mHaveInputSurface(false) { 154} 155 156MediaCodec::~MediaCodec() { 157 CHECK_EQ(mState, UNINITIALIZED); 158} 159 160// static 161status_t MediaCodec::PostAndAwaitResponse( 162 const sp<AMessage> &msg, sp<AMessage> *response) { 163 status_t err = msg->postAndAwaitResponse(response); 164 165 if (err != OK) { 166 return err; 167 } 168 169 if (!(*response)->findInt32("err", &err)) { 170 err = OK; 171 } 172 173 return err; 174} 175 176// static 177void MediaCodec::PostReplyWithError(int32_t replyID, int32_t err) { 178 sp<AMessage> response = new AMessage; 179 response->setInt32("err", err); 180 response->postReply(replyID); 181} 182 183status_t MediaCodec::init(const AString &name, bool nameIsType, bool encoder) { 184 // save init parameters for reset 185 mInitName = name; 186 mInitNameIsType = nameIsType; 187 mInitIsEncoder = encoder; 188 189 // Current video decoders do not return from OMX_FillThisBuffer 190 // quickly, violating the OpenMAX specs, until that is remedied 191 // we need to invest in an extra looper to free the main event 192 // queue. 193 194 if (nameIsType || !strncasecmp(name.c_str(), "omx.", 4)) { 195 mCodec = new ACodec; 196 } else if (!nameIsType 197 && !strncasecmp(name.c_str(), "android.filter.", 15)) { 198 mCodec = new MediaFilter; 199 } else { 200 return NAME_NOT_FOUND; 201 } 202 203 bool needDedicatedLooper = false; 204 if (nameIsType && !strncasecmp(name.c_str(), "video/", 6)) { 205 needDedicatedLooper = true; 206 } else { 207 AString tmp = name; 208 if (tmp.endsWith(".secure")) { 209 tmp.erase(tmp.size() - 7, 7); 210 } 211 const sp<IMediaCodecList> mcl = MediaCodecList::getInstance(); 212 ssize_t codecIdx = mcl->findCodecByName(tmp.c_str()); 213 if (codecIdx >= 0) { 214 const sp<MediaCodecInfo> info = mcl->getCodecInfo(codecIdx); 215 Vector<AString> mimes; 216 info->getSupportedMimes(&mimes); 217 for (size_t i = 0; i < mimes.size(); i++) { 218 if (mimes[i].startsWith("video/")) { 219 needDedicatedLooper = true; 220 break; 221 } 222 } 223 } 224 } 225 226 if (needDedicatedLooper) { 227 if (mCodecLooper == NULL) { 228 mCodecLooper = new ALooper; 229 mCodecLooper->setName("CodecLooper"); 230 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 231 } 232 233 mCodecLooper->registerHandler(mCodec); 234 } else { 235 mLooper->registerHandler(mCodec); 236 } 237 238 mLooper->registerHandler(this); 239 240 mCodec->setNotificationMessage(new AMessage(kWhatCodecNotify, this)); 241 242 sp<AMessage> msg = new AMessage(kWhatInit, this); 243 msg->setString("name", name); 244 msg->setInt32("nameIsType", nameIsType); 245 246 if (nameIsType) { 247 msg->setInt32("encoder", encoder); 248 } 249 250 sp<AMessage> response; 251 return PostAndAwaitResponse(msg, &response); 252} 253 254status_t MediaCodec::setCallback(const sp<AMessage> &callback) { 255 sp<AMessage> msg = new AMessage(kWhatSetCallback, this); 256 msg->setMessage("callback", callback); 257 258 sp<AMessage> response; 259 return PostAndAwaitResponse(msg, &response); 260} 261 262status_t MediaCodec::configure( 263 const sp<AMessage> &format, 264 const sp<Surface> &nativeWindow, 265 const sp<ICrypto> &crypto, 266 uint32_t flags) { 267 sp<AMessage> msg = new AMessage(kWhatConfigure, this); 268 269 msg->setMessage("format", format); 270 msg->setInt32("flags", flags); 271 272 if (nativeWindow != NULL) { 273 msg->setObject( 274 "native-window", 275 new NativeWindowWrapper(nativeWindow)); 276 } 277 278 if (crypto != NULL) { 279 msg->setPointer("crypto", crypto.get()); 280 } 281 282 sp<AMessage> response; 283 status_t err = PostAndAwaitResponse(msg, &response); 284 285 if (err != OK && err != INVALID_OPERATION) { 286 // MediaCodec now set state to UNINITIALIZED upon any fatal error. 287 // To maintain backward-compatibility, do a reset() to put codec 288 // back into INITIALIZED state. 289 // But don't reset if the err is INVALID_OPERATION, which means 290 // the configure failure is due to wrong state. 291 292 ALOGE("configure failed with err 0x%08x, resetting...", err); 293 reset(); 294 } 295 296 return err; 297} 298 299status_t MediaCodec::createInputSurface( 300 sp<IGraphicBufferProducer>* bufferProducer) { 301 sp<AMessage> msg = new AMessage(kWhatCreateInputSurface, this); 302 303 sp<AMessage> response; 304 status_t err = PostAndAwaitResponse(msg, &response); 305 if (err == NO_ERROR) { 306 // unwrap the sp<IGraphicBufferProducer> 307 sp<RefBase> obj; 308 bool found = response->findObject("input-surface", &obj); 309 CHECK(found); 310 sp<BufferProducerWrapper> wrapper( 311 static_cast<BufferProducerWrapper*>(obj.get())); 312 *bufferProducer = wrapper->getBufferProducer(); 313 } else { 314 ALOGW("createInputSurface failed, err=%d", err); 315 } 316 return err; 317} 318 319status_t MediaCodec::start() { 320 sp<AMessage> msg = new AMessage(kWhatStart, this); 321 322 sp<AMessage> response; 323 return PostAndAwaitResponse(msg, &response); 324} 325 326status_t MediaCodec::stop() { 327 sp<AMessage> msg = new AMessage(kWhatStop, this); 328 329 sp<AMessage> response; 330 return PostAndAwaitResponse(msg, &response); 331} 332 333status_t MediaCodec::release() { 334 sp<AMessage> msg = new AMessage(kWhatRelease, this); 335 336 sp<AMessage> response; 337 return PostAndAwaitResponse(msg, &response); 338} 339 340status_t MediaCodec::reset() { 341 /* When external-facing MediaCodec object is created, 342 it is already initialized. Thus, reset is essentially 343 release() followed by init(), plus clearing the state */ 344 345 status_t err = release(); 346 347 // unregister handlers 348 if (mCodec != NULL) { 349 if (mCodecLooper != NULL) { 350 mCodecLooper->unregisterHandler(mCodec->id()); 351 } else { 352 mLooper->unregisterHandler(mCodec->id()); 353 } 354 mCodec = NULL; 355 } 356 mLooper->unregisterHandler(id()); 357 358 mFlags = 0; // clear all flags 359 mStickyError = OK; 360 361 // reset state not reset by setState(UNINITIALIZED) 362 mReplyID = 0; 363 mDequeueInputReplyID = 0; 364 mDequeueOutputReplyID = 0; 365 mDequeueInputTimeoutGeneration = 0; 366 mDequeueOutputTimeoutGeneration = 0; 367 mHaveInputSurface = false; 368 369 if (err == OK) { 370 err = init(mInitName, mInitNameIsType, mInitIsEncoder); 371 } 372 return err; 373} 374 375status_t MediaCodec::queueInputBuffer( 376 size_t index, 377 size_t offset, 378 size_t size, 379 int64_t presentationTimeUs, 380 uint32_t flags, 381 AString *errorDetailMsg) { 382 if (errorDetailMsg != NULL) { 383 errorDetailMsg->clear(); 384 } 385 386 sp<AMessage> msg = new AMessage(kWhatQueueInputBuffer, this); 387 msg->setSize("index", index); 388 msg->setSize("offset", offset); 389 msg->setSize("size", size); 390 msg->setInt64("timeUs", presentationTimeUs); 391 msg->setInt32("flags", flags); 392 msg->setPointer("errorDetailMsg", errorDetailMsg); 393 394 sp<AMessage> response; 395 return PostAndAwaitResponse(msg, &response); 396} 397 398status_t MediaCodec::queueSecureInputBuffer( 399 size_t index, 400 size_t offset, 401 const CryptoPlugin::SubSample *subSamples, 402 size_t numSubSamples, 403 const uint8_t key[16], 404 const uint8_t iv[16], 405 CryptoPlugin::Mode mode, 406 int64_t presentationTimeUs, 407 uint32_t flags, 408 AString *errorDetailMsg) { 409 if (errorDetailMsg != NULL) { 410 errorDetailMsg->clear(); 411 } 412 413 sp<AMessage> msg = new AMessage(kWhatQueueInputBuffer, this); 414 msg->setSize("index", index); 415 msg->setSize("offset", offset); 416 msg->setPointer("subSamples", (void *)subSamples); 417 msg->setSize("numSubSamples", numSubSamples); 418 msg->setPointer("key", (void *)key); 419 msg->setPointer("iv", (void *)iv); 420 msg->setInt32("mode", mode); 421 msg->setInt64("timeUs", presentationTimeUs); 422 msg->setInt32("flags", flags); 423 msg->setPointer("errorDetailMsg", errorDetailMsg); 424 425 sp<AMessage> response; 426 status_t err = PostAndAwaitResponse(msg, &response); 427 428 return err; 429} 430 431status_t MediaCodec::dequeueInputBuffer(size_t *index, int64_t timeoutUs) { 432 sp<AMessage> msg = new AMessage(kWhatDequeueInputBuffer, this); 433 msg->setInt64("timeoutUs", timeoutUs); 434 435 sp<AMessage> response; 436 status_t err; 437 if ((err = PostAndAwaitResponse(msg, &response)) != OK) { 438 return err; 439 } 440 441 CHECK(response->findSize("index", index)); 442 443 return OK; 444} 445 446status_t MediaCodec::dequeueOutputBuffer( 447 size_t *index, 448 size_t *offset, 449 size_t *size, 450 int64_t *presentationTimeUs, 451 uint32_t *flags, 452 int64_t timeoutUs) { 453 sp<AMessage> msg = new AMessage(kWhatDequeueOutputBuffer, this); 454 msg->setInt64("timeoutUs", timeoutUs); 455 456 sp<AMessage> response; 457 status_t err; 458 if ((err = PostAndAwaitResponse(msg, &response)) != OK) { 459 return err; 460 } 461 462 CHECK(response->findSize("index", index)); 463 CHECK(response->findSize("offset", offset)); 464 CHECK(response->findSize("size", size)); 465 CHECK(response->findInt64("timeUs", presentationTimeUs)); 466 CHECK(response->findInt32("flags", (int32_t *)flags)); 467 468 return OK; 469} 470 471status_t MediaCodec::renderOutputBufferAndRelease(size_t index) { 472 sp<AMessage> msg = new AMessage(kWhatReleaseOutputBuffer, this); 473 msg->setSize("index", index); 474 msg->setInt32("render", true); 475 476 sp<AMessage> response; 477 return PostAndAwaitResponse(msg, &response); 478} 479 480status_t MediaCodec::renderOutputBufferAndRelease(size_t index, int64_t timestampNs) { 481 sp<AMessage> msg = new AMessage(kWhatReleaseOutputBuffer, this); 482 msg->setSize("index", index); 483 msg->setInt32("render", true); 484 msg->setInt64("timestampNs", timestampNs); 485 486 sp<AMessage> response; 487 return PostAndAwaitResponse(msg, &response); 488} 489 490status_t MediaCodec::releaseOutputBuffer(size_t index) { 491 sp<AMessage> msg = new AMessage(kWhatReleaseOutputBuffer, this); 492 msg->setSize("index", index); 493 494 sp<AMessage> response; 495 return PostAndAwaitResponse(msg, &response); 496} 497 498status_t MediaCodec::signalEndOfInputStream() { 499 sp<AMessage> msg = new AMessage(kWhatSignalEndOfInputStream, this); 500 501 sp<AMessage> response; 502 return PostAndAwaitResponse(msg, &response); 503} 504 505status_t MediaCodec::getOutputFormat(sp<AMessage> *format) const { 506 sp<AMessage> msg = new AMessage(kWhatGetOutputFormat, this); 507 508 sp<AMessage> response; 509 status_t err; 510 if ((err = PostAndAwaitResponse(msg, &response)) != OK) { 511 return err; 512 } 513 514 CHECK(response->findMessage("format", format)); 515 516 return OK; 517} 518 519status_t MediaCodec::getInputFormat(sp<AMessage> *format) const { 520 sp<AMessage> msg = new AMessage(kWhatGetInputFormat, this); 521 522 sp<AMessage> response; 523 status_t err; 524 if ((err = PostAndAwaitResponse(msg, &response)) != OK) { 525 return err; 526 } 527 528 CHECK(response->findMessage("format", format)); 529 530 return OK; 531} 532 533status_t MediaCodec::getName(AString *name) const { 534 sp<AMessage> msg = new AMessage(kWhatGetName, this); 535 536 sp<AMessage> response; 537 status_t err; 538 if ((err = PostAndAwaitResponse(msg, &response)) != OK) { 539 return err; 540 } 541 542 CHECK(response->findString("name", name)); 543 544 return OK; 545} 546 547status_t MediaCodec::getInputBuffers(Vector<sp<ABuffer> > *buffers) const { 548 sp<AMessage> msg = new AMessage(kWhatGetBuffers, this); 549 msg->setInt32("portIndex", kPortIndexInput); 550 msg->setPointer("buffers", buffers); 551 552 sp<AMessage> response; 553 return PostAndAwaitResponse(msg, &response); 554} 555 556status_t MediaCodec::getOutputBuffers(Vector<sp<ABuffer> > *buffers) const { 557 sp<AMessage> msg = new AMessage(kWhatGetBuffers, this); 558 msg->setInt32("portIndex", kPortIndexOutput); 559 msg->setPointer("buffers", buffers); 560 561 sp<AMessage> response; 562 return PostAndAwaitResponse(msg, &response); 563} 564 565status_t MediaCodec::getOutputBuffer(size_t index, sp<ABuffer> *buffer) { 566 sp<AMessage> format; 567 return getBufferAndFormat(kPortIndexOutput, index, buffer, &format); 568} 569 570status_t MediaCodec::getOutputFormat(size_t index, sp<AMessage> *format) { 571 sp<ABuffer> buffer; 572 return getBufferAndFormat(kPortIndexOutput, index, &buffer, format); 573} 574 575status_t MediaCodec::getInputBuffer(size_t index, sp<ABuffer> *buffer) { 576 sp<AMessage> format; 577 return getBufferAndFormat(kPortIndexInput, index, buffer, &format); 578} 579 580bool MediaCodec::isExecuting() const { 581 return mState == STARTED || mState == FLUSHED; 582} 583 584status_t MediaCodec::getBufferAndFormat( 585 size_t portIndex, size_t index, 586 sp<ABuffer> *buffer, sp<AMessage> *format) { 587 // use mutex instead of a context switch 588 589 buffer->clear(); 590 format->clear(); 591 if (!isExecuting()) { 592 return INVALID_OPERATION; 593 } 594 595 // we do not want mPortBuffers to change during this section 596 // we also don't want mOwnedByClient to change during this 597 Mutex::Autolock al(mBufferLock); 598 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 599 if (index < buffers->size()) { 600 const BufferInfo &info = buffers->itemAt(index); 601 if (info.mOwnedByClient) { 602 // by the time buffers array is initialized, crypto is set 603 if (portIndex == kPortIndexInput && mCrypto != NULL) { 604 *buffer = info.mEncryptedData; 605 } else { 606 *buffer = info.mData; 607 } 608 *format = info.mFormat; 609 } 610 } 611 return OK; 612} 613 614status_t MediaCodec::flush() { 615 sp<AMessage> msg = new AMessage(kWhatFlush, this); 616 617 sp<AMessage> response; 618 return PostAndAwaitResponse(msg, &response); 619} 620 621status_t MediaCodec::requestIDRFrame() { 622 (new AMessage(kWhatRequestIDRFrame, this))->post(); 623 624 return OK; 625} 626 627void MediaCodec::requestActivityNotification(const sp<AMessage> ¬ify) { 628 sp<AMessage> msg = new AMessage(kWhatRequestActivityNotification, this); 629 msg->setMessage("notify", notify); 630 msg->post(); 631} 632 633//////////////////////////////////////////////////////////////////////////////// 634 635void MediaCodec::cancelPendingDequeueOperations() { 636 if (mFlags & kFlagDequeueInputPending) { 637 PostReplyWithError(mDequeueInputReplyID, INVALID_OPERATION); 638 639 ++mDequeueInputTimeoutGeneration; 640 mDequeueInputReplyID = 0; 641 mFlags &= ~kFlagDequeueInputPending; 642 } 643 644 if (mFlags & kFlagDequeueOutputPending) { 645 PostReplyWithError(mDequeueOutputReplyID, INVALID_OPERATION); 646 647 ++mDequeueOutputTimeoutGeneration; 648 mDequeueOutputReplyID = 0; 649 mFlags &= ~kFlagDequeueOutputPending; 650 } 651} 652 653bool MediaCodec::handleDequeueInputBuffer(uint32_t replyID, bool newRequest) { 654 if (!isExecuting() || (mFlags & kFlagIsAsync) 655 || (newRequest && (mFlags & kFlagDequeueInputPending))) { 656 PostReplyWithError(replyID, INVALID_OPERATION); 657 return true; 658 } else if (mFlags & kFlagStickyError) { 659 PostReplyWithError(replyID, getStickyError()); 660 return true; 661 } 662 663 ssize_t index = dequeuePortBuffer(kPortIndexInput); 664 665 if (index < 0) { 666 CHECK_EQ(index, -EAGAIN); 667 return false; 668 } 669 670 sp<AMessage> response = new AMessage; 671 response->setSize("index", index); 672 response->postReply(replyID); 673 674 return true; 675} 676 677bool MediaCodec::handleDequeueOutputBuffer(uint32_t replyID, bool newRequest) { 678 sp<AMessage> response = new AMessage; 679 680 if (!isExecuting() || (mFlags & kFlagIsAsync) 681 || (newRequest && (mFlags & kFlagDequeueOutputPending))) { 682 response->setInt32("err", INVALID_OPERATION); 683 } else if (mFlags & kFlagStickyError) { 684 response->setInt32("err", getStickyError()); 685 } else if (mFlags & kFlagOutputBuffersChanged) { 686 response->setInt32("err", INFO_OUTPUT_BUFFERS_CHANGED); 687 mFlags &= ~kFlagOutputBuffersChanged; 688 } else if (mFlags & kFlagOutputFormatChanged) { 689 response->setInt32("err", INFO_FORMAT_CHANGED); 690 mFlags &= ~kFlagOutputFormatChanged; 691 } else { 692 ssize_t index = dequeuePortBuffer(kPortIndexOutput); 693 694 if (index < 0) { 695 CHECK_EQ(index, -EAGAIN); 696 return false; 697 } 698 699 const sp<ABuffer> &buffer = 700 mPortBuffers[kPortIndexOutput].itemAt(index).mData; 701 702 response->setSize("index", index); 703 response->setSize("offset", buffer->offset()); 704 response->setSize("size", buffer->size()); 705 706 int64_t timeUs; 707 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 708 709 response->setInt64("timeUs", timeUs); 710 711 int32_t omxFlags; 712 CHECK(buffer->meta()->findInt32("omxFlags", &omxFlags)); 713 714 uint32_t flags = 0; 715 if (omxFlags & OMX_BUFFERFLAG_SYNCFRAME) { 716 flags |= BUFFER_FLAG_SYNCFRAME; 717 } 718 if (omxFlags & OMX_BUFFERFLAG_CODECCONFIG) { 719 flags |= BUFFER_FLAG_CODECCONFIG; 720 } 721 if (omxFlags & OMX_BUFFERFLAG_EOS) { 722 flags |= BUFFER_FLAG_EOS; 723 } 724 725 response->setInt32("flags", flags); 726 } 727 728 response->postReply(replyID); 729 730 return true; 731} 732 733void MediaCodec::onMessageReceived(const sp<AMessage> &msg) { 734 switch (msg->what()) { 735 case kWhatCodecNotify: 736 { 737 int32_t what; 738 CHECK(msg->findInt32("what", &what)); 739 740 switch (what) { 741 case CodecBase::kWhatError: 742 { 743 int32_t err, actionCode; 744 CHECK(msg->findInt32("err", &err)); 745 CHECK(msg->findInt32("actionCode", &actionCode)); 746 747 ALOGE("Codec reported err %#x, actionCode %d, while in state %d", 748 err, actionCode, mState); 749 if (err == DEAD_OBJECT) { 750 mFlags |= kFlagSawMediaServerDie; 751 mFlags &= ~kFlagIsComponentAllocated; 752 } 753 754 bool sendErrorResponse = true; 755 756 switch (mState) { 757 case INITIALIZING: 758 { 759 setState(UNINITIALIZED); 760 break; 761 } 762 763 case CONFIGURING: 764 { 765 setState(actionCode == ACTION_CODE_FATAL ? 766 UNINITIALIZED : INITIALIZED); 767 break; 768 } 769 770 case STARTING: 771 { 772 setState(actionCode == ACTION_CODE_FATAL ? 773 UNINITIALIZED : CONFIGURED); 774 break; 775 } 776 777 case STOPPING: 778 case RELEASING: 779 { 780 // Ignore the error, assuming we'll still get 781 // the shutdown complete notification. 782 783 sendErrorResponse = false; 784 785 if (mFlags & kFlagSawMediaServerDie) { 786 // MediaServer died, there definitely won't 787 // be a shutdown complete notification after 788 // all. 789 790 // note that we're directly going from 791 // STOPPING->UNINITIALIZED, instead of the 792 // usual STOPPING->INITIALIZED state. 793 setState(UNINITIALIZED); 794 if (mState == RELEASING) { 795 mComponentName.clear(); 796 } 797 (new AMessage)->postReply(mReplyID); 798 } 799 break; 800 } 801 802 case FLUSHING: 803 { 804 if (actionCode == ACTION_CODE_FATAL) { 805 setState(UNINITIALIZED); 806 } else { 807 setState( 808 (mFlags & kFlagIsAsync) ? FLUSHED : STARTED); 809 } 810 break; 811 } 812 813 case FLUSHED: 814 case STARTED: 815 { 816 sendErrorResponse = false; 817 818 setStickyError(err); 819 postActivityNotificationIfPossible(); 820 821 cancelPendingDequeueOperations(); 822 823 if (mFlags & kFlagIsAsync) { 824 onError(err, actionCode); 825 } 826 switch (actionCode) { 827 case ACTION_CODE_TRANSIENT: 828 break; 829 case ACTION_CODE_RECOVERABLE: 830 setState(INITIALIZED); 831 break; 832 default: 833 setState(UNINITIALIZED); 834 break; 835 } 836 break; 837 } 838 839 default: 840 { 841 sendErrorResponse = false; 842 843 setStickyError(err); 844 postActivityNotificationIfPossible(); 845 846 // actionCode in an uninitialized state is always fatal. 847 if (mState == UNINITIALIZED) { 848 actionCode = ACTION_CODE_FATAL; 849 } 850 if (mFlags & kFlagIsAsync) { 851 onError(err, actionCode); 852 } 853 switch (actionCode) { 854 case ACTION_CODE_TRANSIENT: 855 break; 856 case ACTION_CODE_RECOVERABLE: 857 setState(INITIALIZED); 858 break; 859 default: 860 setState(UNINITIALIZED); 861 break; 862 } 863 break; 864 } 865 } 866 867 if (sendErrorResponse) { 868 PostReplyWithError(mReplyID, err); 869 } 870 break; 871 } 872 873 case CodecBase::kWhatComponentAllocated: 874 { 875 CHECK_EQ(mState, INITIALIZING); 876 setState(INITIALIZED); 877 mFlags |= kFlagIsComponentAllocated; 878 879 CHECK(msg->findString("componentName", &mComponentName)); 880 881 if (mComponentName.startsWith("OMX.google.")) { 882 mFlags |= kFlagUsesSoftwareRenderer; 883 } else { 884 mFlags &= ~kFlagUsesSoftwareRenderer; 885 } 886 887 if (mComponentName.endsWith(".secure")) { 888 mFlags |= kFlagIsSecure; 889 } else { 890 mFlags &= ~kFlagIsSecure; 891 } 892 893 (new AMessage)->postReply(mReplyID); 894 break; 895 } 896 897 case CodecBase::kWhatComponentConfigured: 898 { 899 CHECK_EQ(mState, CONFIGURING); 900 901 // reset input surface flag 902 mHaveInputSurface = false; 903 904 CHECK(msg->findMessage("input-format", &mInputFormat)); 905 CHECK(msg->findMessage("output-format", &mOutputFormat)); 906 907 int32_t usingSwRenderer; 908 if (mOutputFormat->findInt32("using-sw-renderer", &usingSwRenderer) 909 && usingSwRenderer) { 910 mFlags |= kFlagUsesSoftwareRenderer; 911 } 912 setState(CONFIGURED); 913 (new AMessage)->postReply(mReplyID); 914 break; 915 } 916 917 case CodecBase::kWhatInputSurfaceCreated: 918 { 919 // response to initiateCreateInputSurface() 920 status_t err = NO_ERROR; 921 sp<AMessage> response = new AMessage(); 922 if (!msg->findInt32("err", &err)) { 923 sp<RefBase> obj; 924 msg->findObject("input-surface", &obj); 925 CHECK(obj != NULL); 926 response->setObject("input-surface", obj); 927 mHaveInputSurface = true; 928 } else { 929 response->setInt32("err", err); 930 } 931 response->postReply(mReplyID); 932 break; 933 } 934 935 case CodecBase::kWhatSignaledInputEOS: 936 { 937 // response to signalEndOfInputStream() 938 sp<AMessage> response = new AMessage(); 939 status_t err; 940 if (msg->findInt32("err", &err)) { 941 response->setInt32("err", err); 942 } 943 response->postReply(mReplyID); 944 break; 945 } 946 947 948 case CodecBase::kWhatBuffersAllocated: 949 { 950 Mutex::Autolock al(mBufferLock); 951 int32_t portIndex; 952 CHECK(msg->findInt32("portIndex", &portIndex)); 953 954 ALOGV("%s buffers allocated", 955 portIndex == kPortIndexInput ? "input" : "output"); 956 957 CHECK(portIndex == kPortIndexInput 958 || portIndex == kPortIndexOutput); 959 960 mPortBuffers[portIndex].clear(); 961 962 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 963 964 sp<RefBase> obj; 965 CHECK(msg->findObject("portDesc", &obj)); 966 967 sp<CodecBase::PortDescription> portDesc = 968 static_cast<CodecBase::PortDescription *>(obj.get()); 969 970 size_t numBuffers = portDesc->countBuffers(); 971 972 for (size_t i = 0; i < numBuffers; ++i) { 973 BufferInfo info; 974 info.mBufferID = portDesc->bufferIDAt(i); 975 info.mOwnedByClient = false; 976 info.mData = portDesc->bufferAt(i); 977 978 if (portIndex == kPortIndexInput && mCrypto != NULL) { 979 info.mEncryptedData = 980 new ABuffer(info.mData->capacity()); 981 } 982 983 buffers->push_back(info); 984 } 985 986 if (portIndex == kPortIndexOutput) { 987 if (mState == STARTING) { 988 // We're always allocating output buffers after 989 // allocating input buffers, so this is a good 990 // indication that now all buffers are allocated. 991 setState(STARTED); 992 (new AMessage)->postReply(mReplyID); 993 } else { 994 mFlags |= kFlagOutputBuffersChanged; 995 postActivityNotificationIfPossible(); 996 } 997 } 998 break; 999 } 1000 1001 case CodecBase::kWhatOutputFormatChanged: 1002 { 1003 ALOGV("codec output format changed"); 1004 1005 if (mSoftRenderer == NULL && 1006 mNativeWindow != NULL && 1007 (mFlags & kFlagUsesSoftwareRenderer)) { 1008 AString mime; 1009 CHECK(msg->findString("mime", &mime)); 1010 1011 if (mime.startsWithIgnoreCase("video/")) { 1012 mSoftRenderer = new SoftwareRenderer(mNativeWindow); 1013 } 1014 } 1015 1016 mOutputFormat = msg; 1017 1018 if (mFlags & kFlagIsEncoder) { 1019 // Before we announce the format change we should 1020 // collect codec specific data and amend the output 1021 // format as necessary. 1022 mFlags |= kFlagGatherCodecSpecificData; 1023 } else if (mFlags & kFlagIsAsync) { 1024 onOutputFormatChanged(); 1025 } else { 1026 mFlags |= kFlagOutputFormatChanged; 1027 postActivityNotificationIfPossible(); 1028 } 1029 1030 // Notify mCrypto of video resolution changes 1031 if (mCrypto != NULL) { 1032 int32_t left, top, right, bottom, width, height; 1033 if (mOutputFormat->findRect("crop", &left, &top, &right, &bottom)) { 1034 mCrypto->notifyResolution(right - left + 1, bottom - top + 1); 1035 } else if (mOutputFormat->findInt32("width", &width) 1036 && mOutputFormat->findInt32("height", &height)) { 1037 mCrypto->notifyResolution(width, height); 1038 } 1039 } 1040 1041 break; 1042 } 1043 1044 case CodecBase::kWhatFillThisBuffer: 1045 { 1046 /* size_t index = */updateBuffers(kPortIndexInput, msg); 1047 1048 if (mState == FLUSHING 1049 || mState == STOPPING 1050 || mState == RELEASING) { 1051 returnBuffersToCodecOnPort(kPortIndexInput); 1052 break; 1053 } 1054 1055 if (!mCSD.empty()) { 1056 ssize_t index = dequeuePortBuffer(kPortIndexInput); 1057 CHECK_GE(index, 0); 1058 1059 // If codec specific data had been specified as 1060 // part of the format in the call to configure and 1061 // if there's more csd left, we submit it here 1062 // clients only get access to input buffers once 1063 // this data has been exhausted. 1064 1065 status_t err = queueCSDInputBuffer(index); 1066 1067 if (err != OK) { 1068 ALOGE("queueCSDInputBuffer failed w/ error %d", 1069 err); 1070 1071 setStickyError(err); 1072 postActivityNotificationIfPossible(); 1073 1074 cancelPendingDequeueOperations(); 1075 } 1076 break; 1077 } 1078 1079 if (mFlags & kFlagIsAsync) { 1080 if (!mHaveInputSurface) { 1081 onInputBufferAvailable(); 1082 } 1083 } else if (mFlags & kFlagDequeueInputPending) { 1084 CHECK(handleDequeueInputBuffer(mDequeueInputReplyID)); 1085 1086 ++mDequeueInputTimeoutGeneration; 1087 mFlags &= ~kFlagDequeueInputPending; 1088 mDequeueInputReplyID = 0; 1089 } else { 1090 postActivityNotificationIfPossible(); 1091 } 1092 break; 1093 } 1094 1095 case CodecBase::kWhatDrainThisBuffer: 1096 { 1097 /* size_t index = */updateBuffers(kPortIndexOutput, msg); 1098 1099 if (mState == FLUSHING 1100 || mState == STOPPING 1101 || mState == RELEASING) { 1102 returnBuffersToCodecOnPort(kPortIndexOutput); 1103 break; 1104 } 1105 1106 sp<ABuffer> buffer; 1107 CHECK(msg->findBuffer("buffer", &buffer)); 1108 1109 int32_t omxFlags; 1110 CHECK(msg->findInt32("flags", &omxFlags)); 1111 1112 buffer->meta()->setInt32("omxFlags", omxFlags); 1113 1114 if (mFlags & kFlagGatherCodecSpecificData) { 1115 // This is the very first output buffer after a 1116 // format change was signalled, it'll either contain 1117 // the one piece of codec specific data we can expect 1118 // or there won't be codec specific data. 1119 if (omxFlags & OMX_BUFFERFLAG_CODECCONFIG) { 1120 status_t err = 1121 amendOutputFormatWithCodecSpecificData(buffer); 1122 1123 if (err != OK) { 1124 ALOGE("Codec spit out malformed codec " 1125 "specific data!"); 1126 } 1127 } 1128 1129 mFlags &= ~kFlagGatherCodecSpecificData; 1130 if (mFlags & kFlagIsAsync) { 1131 onOutputFormatChanged(); 1132 } else { 1133 mFlags |= kFlagOutputFormatChanged; 1134 } 1135 } 1136 1137 if (mFlags & kFlagIsAsync) { 1138 onOutputBufferAvailable(); 1139 } else if (mFlags & kFlagDequeueOutputPending) { 1140 CHECK(handleDequeueOutputBuffer(mDequeueOutputReplyID)); 1141 1142 ++mDequeueOutputTimeoutGeneration; 1143 mFlags &= ~kFlagDequeueOutputPending; 1144 mDequeueOutputReplyID = 0; 1145 } else { 1146 postActivityNotificationIfPossible(); 1147 } 1148 1149 break; 1150 } 1151 1152 case CodecBase::kWhatEOS: 1153 { 1154 // We already notify the client of this by using the 1155 // corresponding flag in "onOutputBufferReady". 1156 break; 1157 } 1158 1159 case CodecBase::kWhatShutdownCompleted: 1160 { 1161 if (mState == STOPPING) { 1162 setState(INITIALIZED); 1163 } else { 1164 CHECK_EQ(mState, RELEASING); 1165 setState(UNINITIALIZED); 1166 mComponentName.clear(); 1167 } 1168 mFlags &= ~kFlagIsComponentAllocated; 1169 1170 (new AMessage)->postReply(mReplyID); 1171 break; 1172 } 1173 1174 case CodecBase::kWhatFlushCompleted: 1175 { 1176 if (mState != FLUSHING) { 1177 ALOGW("received FlushCompleted message in state %d", 1178 mState); 1179 break; 1180 } 1181 1182 if (mFlags & kFlagIsAsync) { 1183 setState(FLUSHED); 1184 } else { 1185 setState(STARTED); 1186 mCodec->signalResume(); 1187 } 1188 1189 (new AMessage)->postReply(mReplyID); 1190 break; 1191 } 1192 1193 default: 1194 TRESPASS(); 1195 } 1196 break; 1197 } 1198 1199 case kWhatInit: 1200 { 1201 uint32_t replyID; 1202 CHECK(msg->senderAwaitsResponse(&replyID)); 1203 1204 if (mState != UNINITIALIZED) { 1205 PostReplyWithError(replyID, INVALID_OPERATION); 1206 break; 1207 } 1208 1209 mReplyID = replyID; 1210 setState(INITIALIZING); 1211 1212 AString name; 1213 CHECK(msg->findString("name", &name)); 1214 1215 int32_t nameIsType; 1216 int32_t encoder = false; 1217 CHECK(msg->findInt32("nameIsType", &nameIsType)); 1218 if (nameIsType) { 1219 CHECK(msg->findInt32("encoder", &encoder)); 1220 } 1221 1222 sp<AMessage> format = new AMessage; 1223 1224 if (nameIsType) { 1225 format->setString("mime", name.c_str()); 1226 format->setInt32("encoder", encoder); 1227 } else { 1228 format->setString("componentName", name.c_str()); 1229 } 1230 1231 mCodec->initiateAllocateComponent(format); 1232 break; 1233 } 1234 1235 case kWhatSetCallback: 1236 { 1237 uint32_t replyID; 1238 CHECK(msg->senderAwaitsResponse(&replyID)); 1239 1240 if (mState == UNINITIALIZED 1241 || mState == INITIALIZING 1242 || isExecuting()) { 1243 // callback can't be set after codec is executing, 1244 // or before it's initialized (as the callback 1245 // will be cleared when it goes to INITIALIZED) 1246 PostReplyWithError(replyID, INVALID_OPERATION); 1247 break; 1248 } 1249 1250 sp<AMessage> callback; 1251 CHECK(msg->findMessage("callback", &callback)); 1252 1253 mCallback = callback; 1254 1255 if (mCallback != NULL) { 1256 ALOGI("MediaCodec will operate in async mode"); 1257 mFlags |= kFlagIsAsync; 1258 } else { 1259 mFlags &= ~kFlagIsAsync; 1260 } 1261 1262 sp<AMessage> response = new AMessage; 1263 response->postReply(replyID); 1264 break; 1265 } 1266 1267 case kWhatConfigure: 1268 { 1269 uint32_t replyID; 1270 CHECK(msg->senderAwaitsResponse(&replyID)); 1271 1272 if (mState != INITIALIZED) { 1273 PostReplyWithError(replyID, INVALID_OPERATION); 1274 break; 1275 } 1276 1277 sp<RefBase> obj; 1278 if (!msg->findObject("native-window", &obj)) { 1279 obj.clear(); 1280 } 1281 1282 sp<AMessage> format; 1283 CHECK(msg->findMessage("format", &format)); 1284 1285 if (obj != NULL) { 1286 format->setObject("native-window", obj); 1287 1288 status_t err = setNativeWindow( 1289 static_cast<NativeWindowWrapper *>(obj.get()) 1290 ->getSurfaceTextureClient()); 1291 1292 if (err != OK) { 1293 PostReplyWithError(replyID, err); 1294 break; 1295 } 1296 } else { 1297 setNativeWindow(NULL); 1298 } 1299 1300 mReplyID = replyID; 1301 setState(CONFIGURING); 1302 1303 void *crypto; 1304 if (!msg->findPointer("crypto", &crypto)) { 1305 crypto = NULL; 1306 } 1307 1308 mCrypto = static_cast<ICrypto *>(crypto); 1309 1310 uint32_t flags; 1311 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 1312 1313 if (flags & CONFIGURE_FLAG_ENCODE) { 1314 format->setInt32("encoder", true); 1315 mFlags |= kFlagIsEncoder; 1316 } 1317 1318 extractCSD(format); 1319 1320 mCodec->initiateConfigureComponent(format); 1321 break; 1322 } 1323 1324 case kWhatCreateInputSurface: 1325 { 1326 uint32_t replyID; 1327 CHECK(msg->senderAwaitsResponse(&replyID)); 1328 1329 // Must be configured, but can't have been started yet. 1330 if (mState != CONFIGURED) { 1331 PostReplyWithError(replyID, INVALID_OPERATION); 1332 break; 1333 } 1334 1335 mReplyID = replyID; 1336 mCodec->initiateCreateInputSurface(); 1337 break; 1338 } 1339 1340 case kWhatStart: 1341 { 1342 uint32_t replyID; 1343 CHECK(msg->senderAwaitsResponse(&replyID)); 1344 1345 if (mState == FLUSHED) { 1346 setState(STARTED); 1347 mCodec->signalResume(); 1348 PostReplyWithError(replyID, OK); 1349 break; 1350 } else if (mState != CONFIGURED) { 1351 PostReplyWithError(replyID, INVALID_OPERATION); 1352 break; 1353 } 1354 1355 mReplyID = replyID; 1356 setState(STARTING); 1357 1358 mCodec->initiateStart(); 1359 break; 1360 } 1361 1362 case kWhatStop: 1363 case kWhatRelease: 1364 { 1365 State targetState = 1366 (msg->what() == kWhatStop) ? INITIALIZED : UNINITIALIZED; 1367 1368 uint32_t replyID; 1369 CHECK(msg->senderAwaitsResponse(&replyID)); 1370 1371 if (!((mFlags & kFlagIsComponentAllocated) && targetState == UNINITIALIZED) // See 1 1372 && mState != INITIALIZED 1373 && mState != CONFIGURED && !isExecuting()) { 1374 // 1) Permit release to shut down the component if allocated. 1375 // 1376 // 2) We may be in "UNINITIALIZED" state already and 1377 // also shutdown the encoder/decoder without the 1378 // client being aware of this if media server died while 1379 // we were being stopped. The client would assume that 1380 // after stop() returned, it would be safe to call release() 1381 // and it should be in this case, no harm to allow a release() 1382 // if we're already uninitialized. 1383 sp<AMessage> response = new AMessage; 1384 status_t err = mState == targetState ? OK : INVALID_OPERATION; 1385 response->setInt32("err", err); 1386 if (err == OK && targetState == UNINITIALIZED) { 1387 mComponentName.clear(); 1388 } 1389 response->postReply(replyID); 1390 break; 1391 } 1392 1393 if (mFlags & kFlagSawMediaServerDie) { 1394 // It's dead, Jim. Don't expect initiateShutdown to yield 1395 // any useful results now... 1396 setState(UNINITIALIZED); 1397 if (targetState == UNINITIALIZED) { 1398 mComponentName.clear(); 1399 } 1400 (new AMessage)->postReply(replyID); 1401 break; 1402 } 1403 1404 mReplyID = replyID; 1405 setState(msg->what() == kWhatStop ? STOPPING : RELEASING); 1406 1407 mCodec->initiateShutdown( 1408 msg->what() == kWhatStop /* keepComponentAllocated */); 1409 1410 returnBuffersToCodec(); 1411 break; 1412 } 1413 1414 case kWhatDequeueInputBuffer: 1415 { 1416 uint32_t replyID; 1417 CHECK(msg->senderAwaitsResponse(&replyID)); 1418 1419 if (mFlags & kFlagIsAsync) { 1420 ALOGE("dequeueOutputBuffer can't be used in async mode"); 1421 PostReplyWithError(replyID, INVALID_OPERATION); 1422 break; 1423 } 1424 1425 if (mHaveInputSurface) { 1426 ALOGE("dequeueInputBuffer can't be used with input surface"); 1427 PostReplyWithError(replyID, INVALID_OPERATION); 1428 break; 1429 } 1430 1431 if (handleDequeueInputBuffer(replyID, true /* new request */)) { 1432 break; 1433 } 1434 1435 int64_t timeoutUs; 1436 CHECK(msg->findInt64("timeoutUs", &timeoutUs)); 1437 1438 if (timeoutUs == 0ll) { 1439 PostReplyWithError(replyID, -EAGAIN); 1440 break; 1441 } 1442 1443 mFlags |= kFlagDequeueInputPending; 1444 mDequeueInputReplyID = replyID; 1445 1446 if (timeoutUs > 0ll) { 1447 sp<AMessage> timeoutMsg = 1448 new AMessage(kWhatDequeueInputTimedOut, this); 1449 timeoutMsg->setInt32( 1450 "generation", ++mDequeueInputTimeoutGeneration); 1451 timeoutMsg->post(timeoutUs); 1452 } 1453 break; 1454 } 1455 1456 case kWhatDequeueInputTimedOut: 1457 { 1458 int32_t generation; 1459 CHECK(msg->findInt32("generation", &generation)); 1460 1461 if (generation != mDequeueInputTimeoutGeneration) { 1462 // Obsolete 1463 break; 1464 } 1465 1466 CHECK(mFlags & kFlagDequeueInputPending); 1467 1468 PostReplyWithError(mDequeueInputReplyID, -EAGAIN); 1469 1470 mFlags &= ~kFlagDequeueInputPending; 1471 mDequeueInputReplyID = 0; 1472 break; 1473 } 1474 1475 case kWhatQueueInputBuffer: 1476 { 1477 uint32_t replyID; 1478 CHECK(msg->senderAwaitsResponse(&replyID)); 1479 1480 if (!isExecuting()) { 1481 PostReplyWithError(replyID, INVALID_OPERATION); 1482 break; 1483 } else if (mFlags & kFlagStickyError) { 1484 PostReplyWithError(replyID, getStickyError()); 1485 break; 1486 } 1487 1488 status_t err = onQueueInputBuffer(msg); 1489 1490 PostReplyWithError(replyID, err); 1491 break; 1492 } 1493 1494 case kWhatDequeueOutputBuffer: 1495 { 1496 uint32_t replyID; 1497 CHECK(msg->senderAwaitsResponse(&replyID)); 1498 1499 if (mFlags & kFlagIsAsync) { 1500 ALOGE("dequeueOutputBuffer can't be used in async mode"); 1501 PostReplyWithError(replyID, INVALID_OPERATION); 1502 break; 1503 } 1504 1505 if (handleDequeueOutputBuffer(replyID, true /* new request */)) { 1506 break; 1507 } 1508 1509 int64_t timeoutUs; 1510 CHECK(msg->findInt64("timeoutUs", &timeoutUs)); 1511 1512 if (timeoutUs == 0ll) { 1513 PostReplyWithError(replyID, -EAGAIN); 1514 break; 1515 } 1516 1517 mFlags |= kFlagDequeueOutputPending; 1518 mDequeueOutputReplyID = replyID; 1519 1520 if (timeoutUs > 0ll) { 1521 sp<AMessage> timeoutMsg = 1522 new AMessage(kWhatDequeueOutputTimedOut, this); 1523 timeoutMsg->setInt32( 1524 "generation", ++mDequeueOutputTimeoutGeneration); 1525 timeoutMsg->post(timeoutUs); 1526 } 1527 break; 1528 } 1529 1530 case kWhatDequeueOutputTimedOut: 1531 { 1532 int32_t generation; 1533 CHECK(msg->findInt32("generation", &generation)); 1534 1535 if (generation != mDequeueOutputTimeoutGeneration) { 1536 // Obsolete 1537 break; 1538 } 1539 1540 CHECK(mFlags & kFlagDequeueOutputPending); 1541 1542 PostReplyWithError(mDequeueOutputReplyID, -EAGAIN); 1543 1544 mFlags &= ~kFlagDequeueOutputPending; 1545 mDequeueOutputReplyID = 0; 1546 break; 1547 } 1548 1549 case kWhatReleaseOutputBuffer: 1550 { 1551 uint32_t replyID; 1552 CHECK(msg->senderAwaitsResponse(&replyID)); 1553 1554 if (!isExecuting()) { 1555 PostReplyWithError(replyID, INVALID_OPERATION); 1556 break; 1557 } else if (mFlags & kFlagStickyError) { 1558 PostReplyWithError(replyID, getStickyError()); 1559 break; 1560 } 1561 1562 status_t err = onReleaseOutputBuffer(msg); 1563 1564 PostReplyWithError(replyID, err); 1565 break; 1566 } 1567 1568 case kWhatSignalEndOfInputStream: 1569 { 1570 uint32_t replyID; 1571 CHECK(msg->senderAwaitsResponse(&replyID)); 1572 1573 if (!isExecuting()) { 1574 PostReplyWithError(replyID, INVALID_OPERATION); 1575 break; 1576 } else if (mFlags & kFlagStickyError) { 1577 PostReplyWithError(replyID, getStickyError()); 1578 break; 1579 } 1580 1581 mReplyID = replyID; 1582 mCodec->signalEndOfInputStream(); 1583 break; 1584 } 1585 1586 case kWhatGetBuffers: 1587 { 1588 uint32_t replyID; 1589 CHECK(msg->senderAwaitsResponse(&replyID)); 1590 1591 if (!isExecuting() || (mFlags & kFlagIsAsync)) { 1592 PostReplyWithError(replyID, INVALID_OPERATION); 1593 break; 1594 } else if (mFlags & kFlagStickyError) { 1595 PostReplyWithError(replyID, getStickyError()); 1596 break; 1597 } 1598 1599 int32_t portIndex; 1600 CHECK(msg->findInt32("portIndex", &portIndex)); 1601 1602 Vector<sp<ABuffer> > *dstBuffers; 1603 CHECK(msg->findPointer("buffers", (void **)&dstBuffers)); 1604 1605 dstBuffers->clear(); 1606 const Vector<BufferInfo> &srcBuffers = mPortBuffers[portIndex]; 1607 1608 for (size_t i = 0; i < srcBuffers.size(); ++i) { 1609 const BufferInfo &info = srcBuffers.itemAt(i); 1610 1611 dstBuffers->push_back( 1612 (portIndex == kPortIndexInput && mCrypto != NULL) 1613 ? info.mEncryptedData : info.mData); 1614 } 1615 1616 (new AMessage)->postReply(replyID); 1617 break; 1618 } 1619 1620 case kWhatFlush: 1621 { 1622 uint32_t replyID; 1623 CHECK(msg->senderAwaitsResponse(&replyID)); 1624 1625 if (!isExecuting()) { 1626 PostReplyWithError(replyID, INVALID_OPERATION); 1627 break; 1628 } else if (mFlags & kFlagStickyError) { 1629 PostReplyWithError(replyID, getStickyError()); 1630 break; 1631 } 1632 1633 mReplyID = replyID; 1634 // TODO: skip flushing if already FLUSHED 1635 setState(FLUSHING); 1636 1637 mCodec->signalFlush(); 1638 returnBuffersToCodec(); 1639 break; 1640 } 1641 1642 case kWhatGetInputFormat: 1643 case kWhatGetOutputFormat: 1644 { 1645 sp<AMessage> format = 1646 (msg->what() == kWhatGetOutputFormat ? mOutputFormat : mInputFormat); 1647 1648 uint32_t replyID; 1649 CHECK(msg->senderAwaitsResponse(&replyID)); 1650 1651 if ((mState != CONFIGURED && mState != STARTING && 1652 mState != STARTED && mState != FLUSHING && 1653 mState != FLUSHED) 1654 || format == NULL) { 1655 PostReplyWithError(replyID, INVALID_OPERATION); 1656 break; 1657 } else if (mFlags & kFlagStickyError) { 1658 PostReplyWithError(replyID, getStickyError()); 1659 break; 1660 } 1661 1662 sp<AMessage> response = new AMessage; 1663 response->setMessage("format", format); 1664 response->postReply(replyID); 1665 break; 1666 } 1667 1668 case kWhatRequestIDRFrame: 1669 { 1670 mCodec->signalRequestIDRFrame(); 1671 break; 1672 } 1673 1674 case kWhatRequestActivityNotification: 1675 { 1676 CHECK(mActivityNotify == NULL); 1677 CHECK(msg->findMessage("notify", &mActivityNotify)); 1678 1679 postActivityNotificationIfPossible(); 1680 break; 1681 } 1682 1683 case kWhatGetName: 1684 { 1685 uint32_t replyID; 1686 CHECK(msg->senderAwaitsResponse(&replyID)); 1687 1688 if (mComponentName.empty()) { 1689 PostReplyWithError(replyID, INVALID_OPERATION); 1690 break; 1691 } 1692 1693 sp<AMessage> response = new AMessage; 1694 response->setString("name", mComponentName.c_str()); 1695 response->postReply(replyID); 1696 break; 1697 } 1698 1699 case kWhatSetParameters: 1700 { 1701 uint32_t replyID; 1702 CHECK(msg->senderAwaitsResponse(&replyID)); 1703 1704 sp<AMessage> params; 1705 CHECK(msg->findMessage("params", ¶ms)); 1706 1707 status_t err = onSetParameters(params); 1708 1709 PostReplyWithError(replyID, err); 1710 break; 1711 } 1712 1713 default: 1714 TRESPASS(); 1715 } 1716} 1717 1718void MediaCodec::extractCSD(const sp<AMessage> &format) { 1719 mCSD.clear(); 1720 1721 size_t i = 0; 1722 for (;;) { 1723 sp<ABuffer> csd; 1724 if (!format->findBuffer(AStringPrintf("csd-%u", i).c_str(), &csd)) { 1725 break; 1726 } 1727 1728 mCSD.push_back(csd); 1729 ++i; 1730 } 1731 1732 ALOGV("Found %zu pieces of codec specific data.", mCSD.size()); 1733} 1734 1735status_t MediaCodec::queueCSDInputBuffer(size_t bufferIndex) { 1736 CHECK(!mCSD.empty()); 1737 1738 const BufferInfo *info = 1739 &mPortBuffers[kPortIndexInput].itemAt(bufferIndex); 1740 1741 sp<ABuffer> csd = *mCSD.begin(); 1742 mCSD.erase(mCSD.begin()); 1743 1744 const sp<ABuffer> &codecInputData = 1745 (mCrypto != NULL) ? info->mEncryptedData : info->mData; 1746 1747 if (csd->size() > codecInputData->capacity()) { 1748 return -EINVAL; 1749 } 1750 1751 memcpy(codecInputData->data(), csd->data(), csd->size()); 1752 1753 AString errorDetailMsg; 1754 1755 sp<AMessage> msg = new AMessage(kWhatQueueInputBuffer, this); 1756 msg->setSize("index", bufferIndex); 1757 msg->setSize("offset", 0); 1758 msg->setSize("size", csd->size()); 1759 msg->setInt64("timeUs", 0ll); 1760 msg->setInt32("flags", BUFFER_FLAG_CODECCONFIG); 1761 msg->setPointer("errorDetailMsg", &errorDetailMsg); 1762 1763 return onQueueInputBuffer(msg); 1764} 1765 1766void MediaCodec::setState(State newState) { 1767 if (newState == INITIALIZED || newState == UNINITIALIZED) { 1768 delete mSoftRenderer; 1769 mSoftRenderer = NULL; 1770 1771 mCrypto.clear(); 1772 setNativeWindow(NULL); 1773 1774 mInputFormat.clear(); 1775 mOutputFormat.clear(); 1776 mFlags &= ~kFlagOutputFormatChanged; 1777 mFlags &= ~kFlagOutputBuffersChanged; 1778 mFlags &= ~kFlagStickyError; 1779 mFlags &= ~kFlagIsEncoder; 1780 mFlags &= ~kFlagGatherCodecSpecificData; 1781 mFlags &= ~kFlagIsAsync; 1782 mStickyError = OK; 1783 1784 mActivityNotify.clear(); 1785 mCallback.clear(); 1786 } 1787 1788 if (newState == UNINITIALIZED) { 1789 // return any straggling buffers, e.g. if we got here on an error 1790 returnBuffersToCodec(); 1791 1792 // The component is gone, mediaserver's probably back up already 1793 // but should definitely be back up should we try to instantiate 1794 // another component.. and the cycle continues. 1795 mFlags &= ~kFlagSawMediaServerDie; 1796 } 1797 1798 mState = newState; 1799 1800 cancelPendingDequeueOperations(); 1801 1802 updateBatteryStat(); 1803} 1804 1805void MediaCodec::returnBuffersToCodec() { 1806 returnBuffersToCodecOnPort(kPortIndexInput); 1807 returnBuffersToCodecOnPort(kPortIndexOutput); 1808} 1809 1810void MediaCodec::returnBuffersToCodecOnPort(int32_t portIndex) { 1811 CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput); 1812 Mutex::Autolock al(mBufferLock); 1813 1814 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 1815 1816 for (size_t i = 0; i < buffers->size(); ++i) { 1817 BufferInfo *info = &buffers->editItemAt(i); 1818 1819 if (info->mNotify != NULL) { 1820 sp<AMessage> msg = info->mNotify; 1821 info->mNotify = NULL; 1822 info->mOwnedByClient = false; 1823 1824 if (portIndex == kPortIndexInput) { 1825 /* no error, just returning buffers */ 1826 msg->setInt32("err", OK); 1827 } 1828 msg->post(); 1829 } 1830 } 1831 1832 mAvailPortBuffers[portIndex].clear(); 1833} 1834 1835size_t MediaCodec::updateBuffers( 1836 int32_t portIndex, const sp<AMessage> &msg) { 1837 CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput); 1838 1839 uint32_t bufferID; 1840 CHECK(msg->findInt32("buffer-id", (int32_t*)&bufferID)); 1841 1842 Vector<BufferInfo> *buffers = &mPortBuffers[portIndex]; 1843 1844 for (size_t i = 0; i < buffers->size(); ++i) { 1845 BufferInfo *info = &buffers->editItemAt(i); 1846 1847 if (info->mBufferID == bufferID) { 1848 CHECK(info->mNotify == NULL); 1849 CHECK(msg->findMessage("reply", &info->mNotify)); 1850 1851 info->mFormat = 1852 (portIndex == kPortIndexInput) ? mInputFormat : mOutputFormat; 1853 mAvailPortBuffers[portIndex].push_back(i); 1854 1855 return i; 1856 } 1857 } 1858 1859 TRESPASS(); 1860 1861 return 0; 1862} 1863 1864status_t MediaCodec::onQueueInputBuffer(const sp<AMessage> &msg) { 1865 size_t index; 1866 size_t offset; 1867 size_t size; 1868 int64_t timeUs; 1869 uint32_t flags; 1870 CHECK(msg->findSize("index", &index)); 1871 CHECK(msg->findSize("offset", &offset)); 1872 CHECK(msg->findInt64("timeUs", &timeUs)); 1873 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 1874 1875 const CryptoPlugin::SubSample *subSamples; 1876 size_t numSubSamples; 1877 const uint8_t *key; 1878 const uint8_t *iv; 1879 CryptoPlugin::Mode mode = CryptoPlugin::kMode_Unencrypted; 1880 1881 // We allow the simpler queueInputBuffer API to be used even in 1882 // secure mode, by fabricating a single unencrypted subSample. 1883 CryptoPlugin::SubSample ss; 1884 1885 if (msg->findSize("size", &size)) { 1886 if (mCrypto != NULL) { 1887 ss.mNumBytesOfClearData = size; 1888 ss.mNumBytesOfEncryptedData = 0; 1889 1890 subSamples = &ss; 1891 numSubSamples = 1; 1892 key = NULL; 1893 iv = NULL; 1894 } 1895 } else { 1896 if (mCrypto == NULL) { 1897 return -EINVAL; 1898 } 1899 1900 CHECK(msg->findPointer("subSamples", (void **)&subSamples)); 1901 CHECK(msg->findSize("numSubSamples", &numSubSamples)); 1902 CHECK(msg->findPointer("key", (void **)&key)); 1903 CHECK(msg->findPointer("iv", (void **)&iv)); 1904 1905 int32_t tmp; 1906 CHECK(msg->findInt32("mode", &tmp)); 1907 1908 mode = (CryptoPlugin::Mode)tmp; 1909 1910 size = 0; 1911 for (size_t i = 0; i < numSubSamples; ++i) { 1912 size += subSamples[i].mNumBytesOfClearData; 1913 size += subSamples[i].mNumBytesOfEncryptedData; 1914 } 1915 } 1916 1917 if (index >= mPortBuffers[kPortIndexInput].size()) { 1918 return -ERANGE; 1919 } 1920 1921 BufferInfo *info = &mPortBuffers[kPortIndexInput].editItemAt(index); 1922 1923 if (info->mNotify == NULL || !info->mOwnedByClient) { 1924 return -EACCES; 1925 } 1926 1927 if (offset + size > info->mData->capacity()) { 1928 return -EINVAL; 1929 } 1930 1931 sp<AMessage> reply = info->mNotify; 1932 info->mData->setRange(offset, size); 1933 info->mData->meta()->setInt64("timeUs", timeUs); 1934 1935 if (flags & BUFFER_FLAG_EOS) { 1936 info->mData->meta()->setInt32("eos", true); 1937 } 1938 1939 if (flags & BUFFER_FLAG_CODECCONFIG) { 1940 info->mData->meta()->setInt32("csd", true); 1941 } 1942 1943 if (mCrypto != NULL) { 1944 if (size > info->mEncryptedData->capacity()) { 1945 return -ERANGE; 1946 } 1947 1948 AString *errorDetailMsg; 1949 CHECK(msg->findPointer("errorDetailMsg", (void **)&errorDetailMsg)); 1950 1951 ssize_t result = mCrypto->decrypt( 1952 (mFlags & kFlagIsSecure) != 0, 1953 key, 1954 iv, 1955 mode, 1956 info->mEncryptedData->base() + offset, 1957 subSamples, 1958 numSubSamples, 1959 info->mData->base(), 1960 errorDetailMsg); 1961 1962 if (result < 0) { 1963 return result; 1964 } 1965 1966 info->mData->setRange(0, result); 1967 } 1968 1969 // synchronization boundary for getBufferAndFormat 1970 { 1971 Mutex::Autolock al(mBufferLock); 1972 info->mOwnedByClient = false; 1973 } 1974 reply->setBuffer("buffer", info->mData); 1975 reply->post(); 1976 1977 info->mNotify = NULL; 1978 1979 return OK; 1980} 1981 1982status_t MediaCodec::onReleaseOutputBuffer(const sp<AMessage> &msg) { 1983 size_t index; 1984 CHECK(msg->findSize("index", &index)); 1985 1986 int32_t render; 1987 if (!msg->findInt32("render", &render)) { 1988 render = 0; 1989 } 1990 1991 if (!isExecuting()) { 1992 return -EINVAL; 1993 } 1994 1995 if (index >= mPortBuffers[kPortIndexOutput].size()) { 1996 return -ERANGE; 1997 } 1998 1999 BufferInfo *info = &mPortBuffers[kPortIndexOutput].editItemAt(index); 2000 2001 if (info->mNotify == NULL || !info->mOwnedByClient) { 2002 return -EACCES; 2003 } 2004 2005 // synchronization boundary for getBufferAndFormat 2006 { 2007 Mutex::Autolock al(mBufferLock); 2008 info->mOwnedByClient = false; 2009 } 2010 2011 if (render && info->mData != NULL && info->mData->size() != 0) { 2012 info->mNotify->setInt32("render", true); 2013 2014 int64_t timestampNs = 0; 2015 if (msg->findInt64("timestampNs", ×tampNs)) { 2016 info->mNotify->setInt64("timestampNs", timestampNs); 2017 } else { 2018 // TODO: it seems like we should use the timestamp 2019 // in the (media)buffer as it potentially came from 2020 // an input surface, but we did not propagate it prior to 2021 // API 20. Perhaps check for target SDK version. 2022#if 0 2023 if (info->mData->meta()->findInt64("timeUs", ×tampNs)) { 2024 ALOGV("using buffer PTS of %" PRId64, timestampNs); 2025 timestampNs *= 1000; 2026 } 2027#endif 2028 } 2029 2030 if (mSoftRenderer != NULL) { 2031 mSoftRenderer->render( 2032 info->mData->data(), info->mData->size(), 2033 timestampNs, NULL, info->mFormat); 2034 } 2035 } 2036 2037 info->mNotify->post(); 2038 info->mNotify = NULL; 2039 2040 return OK; 2041} 2042 2043ssize_t MediaCodec::dequeuePortBuffer(int32_t portIndex) { 2044 CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput); 2045 2046 List<size_t> *availBuffers = &mAvailPortBuffers[portIndex]; 2047 2048 if (availBuffers->empty()) { 2049 return -EAGAIN; 2050 } 2051 2052 size_t index = *availBuffers->begin(); 2053 availBuffers->erase(availBuffers->begin()); 2054 2055 BufferInfo *info = &mPortBuffers[portIndex].editItemAt(index); 2056 CHECK(!info->mOwnedByClient); 2057 { 2058 Mutex::Autolock al(mBufferLock); 2059 info->mOwnedByClient = true; 2060 2061 // set image-data 2062 if (info->mFormat != NULL) { 2063 sp<ABuffer> imageData; 2064 if (info->mFormat->findBuffer("image-data", &imageData)) { 2065 info->mData->meta()->setBuffer("image-data", imageData); 2066 } 2067 int32_t left, top, right, bottom; 2068 if (info->mFormat->findRect("crop", &left, &top, &right, &bottom)) { 2069 info->mData->meta()->setRect("crop-rect", left, top, right, bottom); 2070 } 2071 } 2072 } 2073 2074 return index; 2075} 2076 2077status_t MediaCodec::setNativeWindow( 2078 const sp<Surface> &surfaceTextureClient) { 2079 status_t err; 2080 2081 if (mNativeWindow != NULL) { 2082 err = native_window_api_disconnect( 2083 mNativeWindow.get(), NATIVE_WINDOW_API_MEDIA); 2084 2085 if (err != OK) { 2086 ALOGW("native_window_api_disconnect returned an error: %s (%d)", 2087 strerror(-err), err); 2088 } 2089 2090 mNativeWindow.clear(); 2091 } 2092 2093 if (surfaceTextureClient != NULL) { 2094 err = native_window_api_connect( 2095 surfaceTextureClient.get(), NATIVE_WINDOW_API_MEDIA); 2096 2097 if (err != OK) { 2098 ALOGE("native_window_api_connect returned an error: %s (%d)", 2099 strerror(-err), err); 2100 2101 return err; 2102 } 2103 2104 mNativeWindow = surfaceTextureClient; 2105 } 2106 2107 return OK; 2108} 2109 2110void MediaCodec::onInputBufferAvailable() { 2111 int32_t index; 2112 while ((index = dequeuePortBuffer(kPortIndexInput)) >= 0) { 2113 sp<AMessage> msg = mCallback->dup(); 2114 msg->setInt32("callbackID", CB_INPUT_AVAILABLE); 2115 msg->setInt32("index", index); 2116 msg->post(); 2117 } 2118} 2119 2120void MediaCodec::onOutputBufferAvailable() { 2121 int32_t index; 2122 while ((index = dequeuePortBuffer(kPortIndexOutput)) >= 0) { 2123 const sp<ABuffer> &buffer = 2124 mPortBuffers[kPortIndexOutput].itemAt(index).mData; 2125 sp<AMessage> msg = mCallback->dup(); 2126 msg->setInt32("callbackID", CB_OUTPUT_AVAILABLE); 2127 msg->setInt32("index", index); 2128 msg->setSize("offset", buffer->offset()); 2129 msg->setSize("size", buffer->size()); 2130 2131 int64_t timeUs; 2132 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2133 2134 msg->setInt64("timeUs", timeUs); 2135 2136 int32_t omxFlags; 2137 CHECK(buffer->meta()->findInt32("omxFlags", &omxFlags)); 2138 2139 uint32_t flags = 0; 2140 if (omxFlags & OMX_BUFFERFLAG_SYNCFRAME) { 2141 flags |= BUFFER_FLAG_SYNCFRAME; 2142 } 2143 if (omxFlags & OMX_BUFFERFLAG_CODECCONFIG) { 2144 flags |= BUFFER_FLAG_CODECCONFIG; 2145 } 2146 if (omxFlags & OMX_BUFFERFLAG_EOS) { 2147 flags |= BUFFER_FLAG_EOS; 2148 } 2149 2150 msg->setInt32("flags", flags); 2151 2152 msg->post(); 2153 } 2154} 2155 2156void MediaCodec::onError(status_t err, int32_t actionCode, const char *detail) { 2157 if (mCallback != NULL) { 2158 sp<AMessage> msg = mCallback->dup(); 2159 msg->setInt32("callbackID", CB_ERROR); 2160 msg->setInt32("err", err); 2161 msg->setInt32("actionCode", actionCode); 2162 2163 if (detail != NULL) { 2164 msg->setString("detail", detail); 2165 } 2166 2167 msg->post(); 2168 } 2169} 2170 2171void MediaCodec::onOutputFormatChanged() { 2172 if (mCallback != NULL) { 2173 sp<AMessage> msg = mCallback->dup(); 2174 msg->setInt32("callbackID", CB_OUTPUT_FORMAT_CHANGED); 2175 msg->setMessage("format", mOutputFormat); 2176 msg->post(); 2177 } 2178} 2179 2180 2181void MediaCodec::postActivityNotificationIfPossible() { 2182 if (mActivityNotify == NULL) { 2183 return; 2184 } 2185 2186 bool isErrorOrOutputChanged = 2187 (mFlags & (kFlagStickyError 2188 | kFlagOutputBuffersChanged 2189 | kFlagOutputFormatChanged)); 2190 2191 if (isErrorOrOutputChanged 2192 || !mAvailPortBuffers[kPortIndexInput].empty() 2193 || !mAvailPortBuffers[kPortIndexOutput].empty()) { 2194 mActivityNotify->setInt32("input-buffers", 2195 mAvailPortBuffers[kPortIndexInput].size()); 2196 2197 if (isErrorOrOutputChanged) { 2198 // we want consumer to dequeue as many times as it can 2199 mActivityNotify->setInt32("output-buffers", INT32_MAX); 2200 } else { 2201 mActivityNotify->setInt32("output-buffers", 2202 mAvailPortBuffers[kPortIndexOutput].size()); 2203 } 2204 mActivityNotify->post(); 2205 mActivityNotify.clear(); 2206 } 2207} 2208 2209status_t MediaCodec::setParameters(const sp<AMessage> ¶ms) { 2210 sp<AMessage> msg = new AMessage(kWhatSetParameters, this); 2211 msg->setMessage("params", params); 2212 2213 sp<AMessage> response; 2214 return PostAndAwaitResponse(msg, &response); 2215} 2216 2217status_t MediaCodec::onSetParameters(const sp<AMessage> ¶ms) { 2218 mCodec->signalSetParameters(params); 2219 2220 return OK; 2221} 2222 2223status_t MediaCodec::amendOutputFormatWithCodecSpecificData( 2224 const sp<ABuffer> &buffer) { 2225 AString mime; 2226 CHECK(mOutputFormat->findString("mime", &mime)); 2227 2228 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_VIDEO_AVC)) { 2229 // Codec specific data should be SPS and PPS in a single buffer, 2230 // each prefixed by a startcode (0x00 0x00 0x00 0x01). 2231 // We separate the two and put them into the output format 2232 // under the keys "csd-0" and "csd-1". 2233 2234 unsigned csdIndex = 0; 2235 2236 const uint8_t *data = buffer->data(); 2237 size_t size = buffer->size(); 2238 2239 const uint8_t *nalStart; 2240 size_t nalSize; 2241 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) { 2242 sp<ABuffer> csd = new ABuffer(nalSize + 4); 2243 memcpy(csd->data(), "\x00\x00\x00\x01", 4); 2244 memcpy(csd->data() + 4, nalStart, nalSize); 2245 2246 mOutputFormat->setBuffer( 2247 AStringPrintf("csd-%u", csdIndex).c_str(), csd); 2248 2249 ++csdIndex; 2250 } 2251 2252 if (csdIndex != 2) { 2253 return ERROR_MALFORMED; 2254 } 2255 } else { 2256 // For everything else we just stash the codec specific data into 2257 // the output format as a single piece of csd under "csd-0". 2258 mOutputFormat->setBuffer("csd-0", buffer); 2259 } 2260 2261 return OK; 2262} 2263 2264void MediaCodec::updateBatteryStat() { 2265 if (mState == CONFIGURED && !mBatteryStatNotified) { 2266 AString mime; 2267 CHECK(mOutputFormat != NULL && 2268 mOutputFormat->findString("mime", &mime)); 2269 2270 mIsVideo = mime.startsWithIgnoreCase("video/"); 2271 2272 BatteryNotifier& notifier(BatteryNotifier::getInstance()); 2273 2274 if (mIsVideo) { 2275 notifier.noteStartVideo(); 2276 } else { 2277 notifier.noteStartAudio(); 2278 } 2279 2280 mBatteryStatNotified = true; 2281 } else if (mState == UNINITIALIZED && mBatteryStatNotified) { 2282 BatteryNotifier& notifier(BatteryNotifier::getInstance()); 2283 2284 if (mIsVideo) { 2285 notifier.noteStopVideo(); 2286 } else { 2287 notifier.noteStopAudio(); 2288 } 2289 2290 mBatteryStatNotified = false; 2291 } 2292} 2293 2294} // namespace android 2295