CameraSource.cpp revision 1374eddc4455b26d1dffdca10fc70534b3f08c1d
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "CameraSource" 19#include <utils/Log.h> 20 21#include <OMX_Component.h> 22#include <binder/IPCThreadState.h> 23#include <media/stagefright/CameraSource.h> 24#include <media/stagefright/MediaDebug.h> 25#include <media/stagefright/MediaDefs.h> 26#include <media/stagefright/MediaErrors.h> 27#include <media/stagefright/MetaData.h> 28#include <camera/Camera.h> 29#include <camera/CameraParameters.h> 30#include <surfaceflinger/Surface.h> 31#include <utils/String8.h> 32#include <cutils/properties.h> 33 34namespace android { 35 36struct CameraSourceListener : public CameraListener { 37 CameraSourceListener(const sp<CameraSource> &source); 38 39 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2); 40 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr); 41 42 virtual void postDataTimestamp( 43 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 44 45protected: 46 virtual ~CameraSourceListener(); 47 48private: 49 wp<CameraSource> mSource; 50 51 CameraSourceListener(const CameraSourceListener &); 52 CameraSourceListener &operator=(const CameraSourceListener &); 53}; 54 55CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source) 56 : mSource(source) { 57} 58 59CameraSourceListener::~CameraSourceListener() { 60} 61 62void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) { 63 LOGV("notify(%d, %d, %d)", msgType, ext1, ext2); 64} 65 66void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr) { 67 LOGV("postData(%d, ptr:%p, size:%d)", 68 msgType, dataPtr->pointer(), dataPtr->size()); 69 70 sp<CameraSource> source = mSource.promote(); 71 if (source.get() != NULL) { 72 source->dataCallback(msgType, dataPtr); 73 } 74} 75 76void CameraSourceListener::postDataTimestamp( 77 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { 78 79 sp<CameraSource> source = mSource.promote(); 80 if (source.get() != NULL) { 81 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr); 82 } 83} 84 85static int32_t getColorFormat(const char* colorFormat) { 86 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) { 87 return OMX_COLOR_FormatYUV420Planar; 88 } 89 90 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) { 91 return OMX_COLOR_FormatYUV422SemiPlanar; 92 } 93 94 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) { 95 return OMX_COLOR_FormatYUV420SemiPlanar; 96 } 97 98 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) { 99 return OMX_COLOR_FormatYCbYCr; 100 } 101 102 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) { 103 return OMX_COLOR_Format16bitRGB565; 104 } 105 106 if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) { 107 return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar; 108 } 109 110 LOGE("Uknown color format (%s), please add it to " 111 "CameraSource::getColorFormat", colorFormat); 112 113 CHECK_EQ(0, "Unknown color format"); 114} 115 116CameraSource *CameraSource::Create() { 117 Size size; 118 size.width = -1; 119 size.height = -1; 120 121 sp<ICamera> camera; 122 return new CameraSource(camera, NULL, 0, size, -1, NULL, false); 123} 124 125// static 126CameraSource *CameraSource::CreateFromCamera( 127 const sp<ICamera>& camera, 128 const sp<ICameraRecordingProxy>& proxy, 129 int32_t cameraId, 130 Size videoSize, 131 int32_t frameRate, 132 const sp<Surface>& surface, 133 bool storeMetaDataInVideoBuffers) { 134 135 CameraSource *source = new CameraSource(camera, proxy, cameraId, 136 videoSize, frameRate, surface, 137 storeMetaDataInVideoBuffers); 138 return source; 139} 140 141CameraSource::CameraSource( 142 const sp<ICamera>& camera, 143 const sp<ICameraRecordingProxy>& proxy, 144 int32_t cameraId, 145 Size videoSize, 146 int32_t frameRate, 147 const sp<Surface>& surface, 148 bool storeMetaDataInVideoBuffers) 149 : mCameraFlags(0), 150 mVideoFrameRate(-1), 151 mCamera(0), 152 mSurface(surface), 153 mNumFramesReceived(0), 154 mLastFrameTimestampUs(0), 155 mStarted(false), 156 mNumFramesEncoded(0), 157 mFirstFrameTimeUs(0), 158 mNumFramesDropped(0), 159 mNumGlitches(0), 160 mGlitchDurationThresholdUs(200000), 161 mCollectStats(false) { 162 mVideoSize.width = -1; 163 mVideoSize.height = -1; 164 165 mInitCheck = init(camera, proxy, cameraId, 166 videoSize, frameRate, 167 storeMetaDataInVideoBuffers); 168 if (mInitCheck != OK) releaseCamera(); 169} 170 171status_t CameraSource::initCheck() const { 172 return mInitCheck; 173} 174 175status_t CameraSource::isCameraAvailable( 176 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, 177 int32_t cameraId) { 178 179 if (camera == 0) { 180 mCamera = Camera::connect(cameraId); 181 if (mCamera == 0) return -EBUSY; 182 // If proxy is not passed in by applications, still use the proxy of 183 // our own Camera to simplify the code. 184 mCameraRecordingProxy = mCamera->getRecordingProxy(); 185 mCameraFlags &= ~FLAGS_HOT_CAMERA; 186 } else { 187 // We get the proxy from Camera, not ICamera. We need to get the proxy 188 // to the remote Camera owned by the application. Here mCamera is a 189 // local Camera object created by us. We cannot use the proxy from 190 // mCamera here. 191 mCamera = Camera::create(camera); 192 if (mCamera == 0) return -EBUSY; 193 mCameraRecordingProxy = proxy; 194 mCameraFlags |= FLAGS_HOT_CAMERA; 195 } 196 197 mCamera->lock(); 198 mDeathNotifier = new DeathNotifier(); 199 // isBinderAlive needs linkToDeath to work. 200 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier); 201 202 return OK; 203} 204 205 206/* 207 * Check to see whether the requested video width and height is one 208 * of the supported sizes. 209 * @param width the video frame width in pixels 210 * @param height the video frame height in pixels 211 * @param suppportedSizes the vector of sizes that we check against 212 * @return true if the dimension (width and height) is supported. 213 */ 214static bool isVideoSizeSupported( 215 int32_t width, int32_t height, 216 const Vector<Size>& supportedSizes) { 217 218 LOGV("isVideoSizeSupported"); 219 for (size_t i = 0; i < supportedSizes.size(); ++i) { 220 if (width == supportedSizes[i].width && 221 height == supportedSizes[i].height) { 222 return true; 223 } 224 } 225 return false; 226} 227 228/* 229 * If the preview and video output is separate, we only set the 230 * the video size, and applications should set the preview size 231 * to some proper value, and the recording framework will not 232 * change the preview size; otherwise, if the video and preview 233 * output is the same, we need to set the preview to be the same 234 * as the requested video size. 235 * 236 */ 237/* 238 * Query the camera to retrieve the supported video frame sizes 239 * and also to see whether CameraParameters::setVideoSize() 240 * is supported or not. 241 * @param params CameraParameters to retrieve the information 242 * @@param isSetVideoSizeSupported retunrs whether method 243 * CameraParameters::setVideoSize() is supported or not. 244 * @param sizes returns the vector of Size objects for the 245 * supported video frame sizes advertised by the camera. 246 */ 247static void getSupportedVideoSizes( 248 const CameraParameters& params, 249 bool *isSetVideoSizeSupported, 250 Vector<Size>& sizes) { 251 252 *isSetVideoSizeSupported = true; 253 params.getSupportedVideoSizes(sizes); 254 if (sizes.size() == 0) { 255 LOGD("Camera does not support setVideoSize()"); 256 params.getSupportedPreviewSizes(sizes); 257 *isSetVideoSizeSupported = false; 258 } 259} 260 261/* 262 * Check whether the camera has the supported color format 263 * @param params CameraParameters to retrieve the information 264 * @return OK if no error. 265 */ 266status_t CameraSource::isCameraColorFormatSupported( 267 const CameraParameters& params) { 268 mColorFormat = getColorFormat(params.get( 269 CameraParameters::KEY_VIDEO_FRAME_FORMAT)); 270 if (mColorFormat == -1) { 271 return BAD_VALUE; 272 } 273 return OK; 274} 275 276/* 277 * Configure the camera to use the requested video size 278 * (width and height) and/or frame rate. If both width and 279 * height are -1, configuration on the video size is skipped. 280 * if frameRate is -1, configuration on the frame rate 281 * is skipped. Skipping the configuration allows one to 282 * use the current camera setting without the need to 283 * actually know the specific values (see Create() method). 284 * 285 * @param params the CameraParameters to be configured 286 * @param width the target video frame width in pixels 287 * @param height the target video frame height in pixels 288 * @param frameRate the target frame rate in frames per second. 289 * @return OK if no error. 290 */ 291status_t CameraSource::configureCamera( 292 CameraParameters* params, 293 int32_t width, int32_t height, 294 int32_t frameRate) { 295 296 Vector<Size> sizes; 297 bool isSetVideoSizeSupportedByCamera = true; 298 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes); 299 bool isCameraParamChanged = false; 300 if (width != -1 && height != -1) { 301 if (!isVideoSizeSupported(width, height, sizes)) { 302 LOGE("Video dimension (%dx%d) is unsupported", width, height); 303 return BAD_VALUE; 304 } 305 if (isSetVideoSizeSupportedByCamera) { 306 params->setVideoSize(width, height); 307 } else { 308 params->setPreviewSize(width, height); 309 } 310 isCameraParamChanged = true; 311 } else if ((width == -1 && height != -1) || 312 (width != -1 && height == -1)) { 313 // If one and only one of the width and height is -1 314 // we reject such a request. 315 LOGE("Requested video size (%dx%d) is not supported", width, height); 316 return BAD_VALUE; 317 } else { // width == -1 && height == -1 318 // Do not configure the camera. 319 // Use the current width and height value setting from the camera. 320 } 321 322 if (frameRate != -1) { 323 CHECK(frameRate > 0 && frameRate <= 120); 324 const char* supportedFrameRates = 325 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES); 326 CHECK(supportedFrameRates != NULL); 327 LOGV("Supported frame rates: %s", supportedFrameRates); 328 char buf[4]; 329 snprintf(buf, 4, "%d", frameRate); 330 if (strstr(supportedFrameRates, buf) == NULL) { 331 LOGE("Requested frame rate (%d) is not supported: %s", 332 frameRate, supportedFrameRates); 333 return BAD_VALUE; 334 } 335 336 // The frame rate is supported, set the camera to the requested value. 337 params->setPreviewFrameRate(frameRate); 338 isCameraParamChanged = true; 339 } else { // frameRate == -1 340 // Do not configure the camera. 341 // Use the current frame rate value setting from the camera 342 } 343 344 if (isCameraParamChanged) { 345 // Either frame rate or frame size needs to be changed. 346 String8 s = params->flatten(); 347 if (OK != mCamera->setParameters(s)) { 348 LOGE("Could not change settings." 349 " Someone else is using camera %p?", mCamera.get()); 350 return -EBUSY; 351 } 352 } 353 return OK; 354} 355 356/* 357 * Check whether the requested video frame size 358 * has been successfully configured or not. If both width and height 359 * are -1, check on the current width and height value setting 360 * is performed. 361 * 362 * @param params CameraParameters to retrieve the information 363 * @param the target video frame width in pixels to check against 364 * @param the target video frame height in pixels to check against 365 * @return OK if no error 366 */ 367status_t CameraSource::checkVideoSize( 368 const CameraParameters& params, 369 int32_t width, int32_t height) { 370 371 // The actual video size is the same as the preview size 372 // if the camera hal does not support separate video and 373 // preview output. In this case, we retrieve the video 374 // size from preview. 375 int32_t frameWidthActual = -1; 376 int32_t frameHeightActual = -1; 377 Vector<Size> sizes; 378 params.getSupportedVideoSizes(sizes); 379 if (sizes.size() == 0) { 380 // video size is the same as preview size 381 params.getPreviewSize(&frameWidthActual, &frameHeightActual); 382 } else { 383 // video size may not be the same as preview 384 params.getVideoSize(&frameWidthActual, &frameHeightActual); 385 } 386 if (frameWidthActual < 0 || frameHeightActual < 0) { 387 LOGE("Failed to retrieve video frame size (%dx%d)", 388 frameWidthActual, frameHeightActual); 389 return UNKNOWN_ERROR; 390 } 391 392 // Check the actual video frame size against the target/requested 393 // video frame size. 394 if (width != -1 && height != -1) { 395 if (frameWidthActual != width || frameHeightActual != height) { 396 LOGE("Failed to set video frame size to %dx%d. " 397 "The actual video size is %dx%d ", width, height, 398 frameWidthActual, frameHeightActual); 399 return UNKNOWN_ERROR; 400 } 401 } 402 403 // Good now. 404 mVideoSize.width = frameWidthActual; 405 mVideoSize.height = frameHeightActual; 406 return OK; 407} 408 409/* 410 * Check the requested frame rate has been successfully configured or not. 411 * If the target frameRate is -1, check on the current frame rate value 412 * setting is performed. 413 * 414 * @param params CameraParameters to retrieve the information 415 * @param the target video frame rate to check against 416 * @return OK if no error. 417 */ 418status_t CameraSource::checkFrameRate( 419 const CameraParameters& params, 420 int32_t frameRate) { 421 422 int32_t frameRateActual = params.getPreviewFrameRate(); 423 if (frameRateActual < 0) { 424 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual); 425 return UNKNOWN_ERROR; 426 } 427 428 // Check the actual video frame rate against the target/requested 429 // video frame rate. 430 if (frameRate != -1 && (frameRateActual - frameRate) != 0) { 431 LOGE("Failed to set preview frame rate to %d fps. The actual " 432 "frame rate is %d", frameRate, frameRateActual); 433 return UNKNOWN_ERROR; 434 } 435 436 // Good now. 437 mVideoFrameRate = frameRateActual; 438 return OK; 439} 440 441/* 442 * Initialize the CameraSource to so that it becomes 443 * ready for providing the video input streams as requested. 444 * @param camera the camera object used for the video source 445 * @param cameraId if camera == 0, use camera with this id 446 * as the video source 447 * @param videoSize the target video frame size. If both 448 * width and height in videoSize is -1, use the current 449 * width and heigth settings by the camera 450 * @param frameRate the target frame rate in frames per second. 451 * if it is -1, use the current camera frame rate setting. 452 * @param storeMetaDataInVideoBuffers request to store meta 453 * data or real YUV data in video buffers. Request to 454 * store meta data in video buffers may not be honored 455 * if the source does not support this feature. 456 * 457 * @return OK if no error. 458 */ 459status_t CameraSource::init( 460 const sp<ICamera>& camera, 461 const sp<ICameraRecordingProxy>& proxy, 462 int32_t cameraId, 463 Size videoSize, 464 int32_t frameRate, 465 bool storeMetaDataInVideoBuffers) { 466 467 status_t err = OK; 468 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 469 err = initWithCameraAccess(camera, proxy, cameraId, 470 videoSize, frameRate, 471 storeMetaDataInVideoBuffers); 472 IPCThreadState::self()->restoreCallingIdentity(token); 473 return err; 474} 475 476status_t CameraSource::initWithCameraAccess( 477 const sp<ICamera>& camera, 478 const sp<ICameraRecordingProxy>& proxy, 479 int32_t cameraId, 480 Size videoSize, 481 int32_t frameRate, 482 bool storeMetaDataInVideoBuffers) { 483 status_t err = OK; 484 485 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) { 486 LOGE("Camera connection could not be established."); 487 return err; 488 } 489 CameraParameters params(mCamera->getParameters()); 490 if ((err = isCameraColorFormatSupported(params)) != OK) { 491 return err; 492 } 493 494 // Set the camera to use the requested video frame size 495 // and/or frame rate. 496 if ((err = configureCamera(¶ms, 497 videoSize.width, videoSize.height, 498 frameRate))) { 499 return err; 500 } 501 502 // Check on video frame size and frame rate. 503 CameraParameters newCameraParams(mCamera->getParameters()); 504 if ((err = checkVideoSize(newCameraParams, 505 videoSize.width, videoSize.height)) != OK) { 506 return err; 507 } 508 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) { 509 return err; 510 } 511 512 // This CHECK is good, since we just passed the lock/unlock 513 // check earlier by calling mCamera->setParameters(). 514 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface)); 515 516 // By default, do not store metadata in video buffers 517 mIsMetaDataStoredInVideoBuffers = false; 518 mCamera->storeMetaDataInBuffers(false); 519 if (storeMetaDataInVideoBuffers) { 520 if (OK == mCamera->storeMetaDataInBuffers(true)) { 521 mIsMetaDataStoredInVideoBuffers = true; 522 } 523 } 524 525 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate); 526 if (glitchDurationUs > mGlitchDurationThresholdUs) { 527 mGlitchDurationThresholdUs = glitchDurationUs; 528 } 529 530 // XXX: query camera for the stride and slice height 531 // when the capability becomes available. 532 mMeta = new MetaData; 533 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 534 mMeta->setInt32(kKeyColorFormat, mColorFormat); 535 mMeta->setInt32(kKeyWidth, mVideoSize.width); 536 mMeta->setInt32(kKeyHeight, mVideoSize.height); 537 mMeta->setInt32(kKeyStride, mVideoSize.width); 538 mMeta->setInt32(kKeySliceHeight, mVideoSize.height); 539 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate); 540 return OK; 541} 542 543CameraSource::~CameraSource() { 544 if (mStarted) { 545 stop(); 546 } else if (mInitCheck == OK) { 547 // Camera is initialized but because start() is never called, 548 // the lock on Camera is never released(). This makes sure 549 // Camera's lock is released in this case. 550 releaseCamera(); 551 } 552} 553 554void CameraSource::startCameraRecording() { 555 // Reset the identity to the current thread because media server owns the 556 // camera and recording is started by the applications. The applications 557 // will connect to the camera in ICameraRecordingProxy::startRecording. 558 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 559 mCamera->unlock(); 560 mCamera.clear(); 561 IPCThreadState::self()->restoreCallingIdentity(token); 562 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this))); 563} 564 565status_t CameraSource::start(MetaData *meta) { 566 CHECK(!mStarted); 567 if (mInitCheck != OK) { 568 LOGE("CameraSource is not initialized yet"); 569 return mInitCheck; 570 } 571 572 char value[PROPERTY_VALUE_MAX]; 573 if (property_get("media.stagefright.record-stats", value, NULL) 574 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { 575 mCollectStats = true; 576 } 577 578 mStartTimeUs = 0; 579 int64_t startTimeUs; 580 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) { 581 mStartTimeUs = startTimeUs; 582 } 583 584 startCameraRecording(); 585 586 mStarted = true; 587 return OK; 588} 589 590void CameraSource::stopCameraRecording() { 591 mCameraRecordingProxy->stopRecording(); 592} 593 594void CameraSource::releaseCamera() { 595 LOGV("releaseCamera"); 596 if (mCamera != 0) { 597 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 598 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) { 599 LOGV("Camera was cold when we started, stopping preview"); 600 mCamera->stopPreview(); 601 mCamera->disconnect(); 602 } else { 603 // Unlock the camera so the application can lock it back. 604 mCamera->unlock(); 605 } 606 mCamera.clear(); 607 IPCThreadState::self()->restoreCallingIdentity(token); 608 } 609 if (mCameraRecordingProxy != 0) { 610 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier); 611 mCameraRecordingProxy.clear(); 612 } 613 mCameraFlags = 0; 614} 615 616status_t CameraSource::stop() { 617 LOGD("stop: E"); 618 Mutex::Autolock autoLock(mLock); 619 mStarted = false; 620 mFrameAvailableCondition.signal(); 621 622 releaseQueuedFrames(); 623 while (!mFramesBeingEncoded.empty()) { 624 if (NO_ERROR != 625 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) { 626 LOGW("Timed out waiting for outstanding frames being encoded: %d", 627 mFramesBeingEncoded.size()); 628 } 629 } 630 stopCameraRecording(); 631 releaseCamera(); 632 633 if (mCollectStats) { 634 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us", 635 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped, 636 mLastFrameTimestampUs - mFirstFrameTimeUs); 637 } 638 639 if (mNumGlitches > 0) { 640 LOGW("%d long delays between neighboring video frames", mNumGlitches); 641 } 642 643 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped); 644 LOGD("stop: X"); 645 return OK; 646} 647 648void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) { 649 if (mCameraRecordingProxy != NULL) { 650 mCameraRecordingProxy->releaseRecordingFrame(frame); 651 } 652} 653 654void CameraSource::releaseQueuedFrames() { 655 List<sp<IMemory> >::iterator it; 656 while (!mFramesReceived.empty()) { 657 it = mFramesReceived.begin(); 658 releaseRecordingFrame(*it); 659 mFramesReceived.erase(it); 660 ++mNumFramesDropped; 661 } 662} 663 664sp<MetaData> CameraSource::getFormat() { 665 return mMeta; 666} 667 668void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) { 669 releaseRecordingFrame(frame); 670} 671 672void CameraSource::signalBufferReturned(MediaBuffer *buffer) { 673 LOGV("signalBufferReturned: %p", buffer->data()); 674 Mutex::Autolock autoLock(mLock); 675 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); 676 it != mFramesBeingEncoded.end(); ++it) { 677 if ((*it)->pointer() == buffer->data()) { 678 releaseOneRecordingFrame((*it)); 679 mFramesBeingEncoded.erase(it); 680 ++mNumFramesEncoded; 681 buffer->setObserver(0); 682 buffer->release(); 683 mFrameCompleteCondition.signal(); 684 return; 685 } 686 } 687 CHECK_EQ(0, "signalBufferReturned: bogus buffer"); 688} 689 690status_t CameraSource::read( 691 MediaBuffer **buffer, const ReadOptions *options) { 692 LOGV("read"); 693 694 *buffer = NULL; 695 696 int64_t seekTimeUs; 697 ReadOptions::SeekMode mode; 698 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 699 return ERROR_UNSUPPORTED; 700 } 701 702 sp<IMemory> frame; 703 int64_t frameTime; 704 705 { 706 Mutex::Autolock autoLock(mLock); 707 while (mStarted && mFramesReceived.empty()) { 708 if (NO_ERROR != 709 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) { 710 if (!mCameraRecordingProxy->asBinder()->isBinderAlive()) { 711 LOGW("camera recording proxy is gone"); 712 return ERROR_END_OF_STREAM; 713 } 714 LOGW("Timed out waiting for incoming camera video frames: %lld us", 715 mLastFrameTimestampUs); 716 } 717 } 718 if (!mStarted) { 719 return OK; 720 } 721 frame = *mFramesReceived.begin(); 722 mFramesReceived.erase(mFramesReceived.begin()); 723 724 frameTime = *mFrameTimes.begin(); 725 mFrameTimes.erase(mFrameTimes.begin()); 726 mFramesBeingEncoded.push_back(frame); 727 *buffer = new MediaBuffer(frame->pointer(), frame->size()); 728 (*buffer)->setObserver(this); 729 (*buffer)->add_ref(); 730 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); 731 } 732 return OK; 733} 734 735void CameraSource::dataCallbackTimestamp(int64_t timestampUs, 736 int32_t msgType, const sp<IMemory> &data) { 737 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); 738 Mutex::Autolock autoLock(mLock); 739 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) { 740 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs); 741 releaseOneRecordingFrame(data); 742 return; 743 } 744 745 if (mNumFramesReceived > 0) { 746 CHECK(timestampUs > mLastFrameTimestampUs); 747 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { 748 ++mNumGlitches; 749 } 750 } 751 752 // May need to skip frame or modify timestamp. Currently implemented 753 // by the subclass CameraSourceTimeLapse. 754 if (skipCurrentFrame(timestampUs)) { 755 releaseOneRecordingFrame(data); 756 return; 757 } 758 759 mLastFrameTimestampUs = timestampUs; 760 if (mNumFramesReceived == 0) { 761 mFirstFrameTimeUs = timestampUs; 762 // Initial delay 763 if (mStartTimeUs > 0) { 764 if (timestampUs < mStartTimeUs) { 765 // Frame was captured before recording was started 766 // Drop it without updating the statistical data. 767 releaseOneRecordingFrame(data); 768 return; 769 } 770 mStartTimeUs = timestampUs - mStartTimeUs; 771 } 772 } 773 ++mNumFramesReceived; 774 775 CHECK(data != NULL && data->size() > 0); 776 mFramesReceived.push_back(data); 777 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); 778 mFrameTimes.push_back(timeUs); 779 LOGV("initial delay: %lld, current time stamp: %lld", 780 mStartTimeUs, timeUs); 781 mFrameAvailableCondition.signal(); 782} 783 784bool CameraSource::isMetaDataStoredInVideoBuffers() const { 785 LOGV("isMetaDataStoredInVideoBuffers"); 786 return mIsMetaDataStoredInVideoBuffers; 787} 788 789CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) { 790 mSource = source; 791} 792 793void CameraSource::ProxyListener::dataCallbackTimestamp( 794 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { 795 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr); 796} 797 798void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) { 799 LOGI("Camera recording proxy died"); 800} 801 802} // namespace android 803