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