2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "CameraSource"
21 #include <utils/Log.h>
23 #include <OMX_Component.h>
24 #include <binder/IPCThreadState.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 #include <media/stagefright/CameraSource.h>
27 #include <media/stagefright/MediaDefs.h>
28 #include <media/stagefright/MediaErrors.h>
29 #include <media/stagefright/MetaData.h>
30 #include <camera/Camera.h>
31 #include <camera/CameraParameters.h>
32 #include <gui/Surface.h>
33 #include <utils/String8.h>
34 #include <cutils/properties.h>
37 #define UNUSED_UNLESS_VERBOSE(x) (void)(x)
39 #define UNUSED_UNLESS_VERBOSE(x)
44 static const int64_t CAMERA_SOURCE_TIMEOUT_NS = 3000000000LL;
46 struct CameraSourceListener : public CameraListener {
47 CameraSourceListener(const sp<CameraSource> &source);
49 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
50 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr,
51 camera_frame_metadata_t *metadata);
53 virtual void postDataTimestamp(
54 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
57 virtual ~CameraSourceListener();
60 wp<CameraSource> mSource;
62 CameraSourceListener(const CameraSourceListener &);
63 CameraSourceListener &operator=(const CameraSourceListener &);
66 CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source)
70 CameraSourceListener::~CameraSourceListener() {
73 void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
74 UNUSED_UNLESS_VERBOSE(msgType);
75 UNUSED_UNLESS_VERBOSE(ext1);
76 UNUSED_UNLESS_VERBOSE(ext2);
77 ALOGV("notify(%d, %d, %d)", msgType, ext1, ext2);
80 void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr,
81 camera_frame_metadata_t * /* metadata */) {
82 ALOGV("postData(%d, ptr:%p, size:%zu)",
83 msgType, dataPtr->pointer(), dataPtr->size());
85 sp<CameraSource> source = mSource.promote();
86 if (source.get() != NULL) {
87 source->dataCallback(msgType, dataPtr);
91 void CameraSourceListener::postDataTimestamp(
92 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
94 sp<CameraSource> source = mSource.promote();
95 if (source.get() != NULL) {
96 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
100 static int32_t getColorFormat(const char* colorFormat) {
101 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
102 return OMX_COLOR_FormatYUV420Planar;
105 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) {
106 return OMX_COLOR_FormatYUV422SemiPlanar;
109 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
110 return OMX_COLOR_FormatYUV420SemiPlanar;
113 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) {
114 return OMX_COLOR_FormatYCbYCr;
117 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) {
118 return OMX_COLOR_Format16bitRGB565;
121 if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) {
122 return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
125 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_ANDROID_OPAQUE)) {
126 return OMX_COLOR_FormatAndroidOpaque;
129 ALOGE("Uknown color format (%s), please add it to "
130 "CameraSource::getColorFormat", colorFormat);
132 CHECK(!"Unknown color format");
135 CameraSource *CameraSource::Create(const String16 &clientName) {
141 return new CameraSource(camera, NULL, 0, clientName, -1,
142 size, -1, NULL, false);
146 CameraSource *CameraSource::CreateFromCamera(
147 const sp<ICamera>& camera,
148 const sp<ICameraRecordingProxy>& proxy,
150 const String16& clientName,
154 const sp<IGraphicBufferProducer>& surface,
155 bool storeMetaDataInVideoBuffers) {
157 CameraSource *source = new CameraSource(camera, proxy, cameraId,
158 clientName, clientUid, videoSize, frameRate, surface,
159 storeMetaDataInVideoBuffers);
163 CameraSource::CameraSource(
164 const sp<ICamera>& camera,
165 const sp<ICameraRecordingProxy>& proxy,
167 const String16& clientName,
171 const sp<IGraphicBufferProducer>& surface,
172 bool storeMetaDataInVideoBuffers)
178 mNumFramesReceived(0),
179 mLastFrameTimestampUs(0),
181 mNumFramesEncoded(0),
182 mTimeBetweenFrameCaptureUs(0),
183 mFirstFrameTimeUs(0),
184 mNumFramesDropped(0),
186 mGlitchDurationThresholdUs(200000),
187 mCollectStats(false) {
188 mVideoSize.width = -1;
189 mVideoSize.height = -1;
191 mInitCheck = init(camera, proxy, cameraId,
192 clientName, clientUid,
193 videoSize, frameRate,
194 storeMetaDataInVideoBuffers);
195 if (mInitCheck != OK) releaseCamera();
198 status_t CameraSource::initCheck() const {
202 status_t CameraSource::isCameraAvailable(
203 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
204 int32_t cameraId, const String16& clientName, uid_t clientUid) {
207 mCamera = Camera::connect(cameraId, clientName, clientUid);
208 if (mCamera == 0) return -EBUSY;
209 mCameraFlags &= ~FLAGS_HOT_CAMERA;
211 // We get the proxy from Camera, not ICamera. We need to get the proxy
212 // to the remote Camera owned by the application. Here mCamera is a
213 // local Camera object created by us. We cannot use the proxy from
215 mCamera = Camera::create(camera);
216 if (mCamera == 0) return -EBUSY;
217 mCameraRecordingProxy = proxy;
218 mCameraFlags |= FLAGS_HOT_CAMERA;
219 mDeathNotifier = new DeathNotifier();
220 // isBinderAlive needs linkToDeath to work.
221 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
231 * Check to see whether the requested video width and height is one
232 * of the supported sizes.
233 * @param width the video frame width in pixels
234 * @param height the video frame height in pixels
235 * @param suppportedSizes the vector of sizes that we check against
236 * @return true if the dimension (width and height) is supported.
238 static bool isVideoSizeSupported(
239 int32_t width, int32_t height,
240 const Vector<Size>& supportedSizes) {
242 ALOGV("isVideoSizeSupported");
243 for (size_t i = 0; i < supportedSizes.size(); ++i) {
244 if (width == supportedSizes[i].width &&
245 height == supportedSizes[i].height) {
253 * If the preview and video output is separate, we only set the
254 * the video size, and applications should set the preview size
255 * to some proper value, and the recording framework will not
256 * change the preview size; otherwise, if the video and preview
257 * output is the same, we need to set the preview to be the same
258 * as the requested video size.
262 * Query the camera to retrieve the supported video frame sizes
263 * and also to see whether CameraParameters::setVideoSize()
264 * is supported or not.
265 * @param params CameraParameters to retrieve the information
266 * @@param isSetVideoSizeSupported retunrs whether method
267 * CameraParameters::setVideoSize() is supported or not.
268 * @param sizes returns the vector of Size objects for the
269 * supported video frame sizes advertised by the camera.
271 static void getSupportedVideoSizes(
272 const CameraParameters& params,
273 bool *isSetVideoSizeSupported,
274 Vector<Size>& sizes) {
276 *isSetVideoSizeSupported = true;
277 params.getSupportedVideoSizes(sizes);
278 if (sizes.size() == 0) {
279 ALOGD("Camera does not support setVideoSize()");
280 params.getSupportedPreviewSizes(sizes);
281 *isSetVideoSizeSupported = false;
286 * Check whether the camera has the supported color format
287 * @param params CameraParameters to retrieve the information
288 * @return OK if no error.
290 status_t CameraSource::isCameraColorFormatSupported(
291 const CameraParameters& params) {
292 mColorFormat = getColorFormat(params.get(
293 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
294 if (mColorFormat == -1) {
301 * Configure the camera to use the requested video size
302 * (width and height) and/or frame rate. If both width and
303 * height are -1, configuration on the video size is skipped.
304 * if frameRate is -1, configuration on the frame rate
305 * is skipped. Skipping the configuration allows one to
306 * use the current camera setting without the need to
307 * actually know the specific values (see Create() method).
309 * @param params the CameraParameters to be configured
310 * @param width the target video frame width in pixels
311 * @param height the target video frame height in pixels
312 * @param frameRate the target frame rate in frames per second.
313 * @return OK if no error.
315 status_t CameraSource::configureCamera(
316 CameraParameters* params,
317 int32_t width, int32_t height,
319 ALOGV("configureCamera");
321 bool isSetVideoSizeSupportedByCamera = true;
322 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
323 bool isCameraParamChanged = false;
324 if (width != -1 && height != -1) {
325 if (!isVideoSizeSupported(width, height, sizes)) {
326 ALOGE("Video dimension (%dx%d) is unsupported", width, height);
329 if (isSetVideoSizeSupportedByCamera) {
330 params->setVideoSize(width, height);
332 params->setPreviewSize(width, height);
334 isCameraParamChanged = true;
335 } else if ((width == -1 && height != -1) ||
336 (width != -1 && height == -1)) {
337 // If one and only one of the width and height is -1
338 // we reject such a request.
339 ALOGE("Requested video size (%dx%d) is not supported", width, height);
341 } else { // width == -1 && height == -1
342 // Do not configure the camera.
343 // Use the current width and height value setting from the camera.
346 if (frameRate != -1) {
347 CHECK(frameRate > 0 && frameRate <= 120);
348 const char* supportedFrameRates =
349 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
350 CHECK(supportedFrameRates != NULL);
351 ALOGV("Supported frame rates: %s", supportedFrameRates);
353 snprintf(buf, 4, "%d", frameRate);
354 if (strstr(supportedFrameRates, buf) == NULL) {
355 ALOGE("Requested frame rate (%d) is not supported: %s",
356 frameRate, supportedFrameRates);
360 // The frame rate is supported, set the camera to the requested value.
361 params->setPreviewFrameRate(frameRate);
362 isCameraParamChanged = true;
363 } else { // frameRate == -1
364 // Do not configure the camera.
365 // Use the current frame rate value setting from the camera
368 if (isCameraParamChanged) {
369 // Either frame rate or frame size needs to be changed.
370 String8 s = params->flatten();
371 if (OK != mCamera->setParameters(s)) {
372 ALOGE("Could not change settings."
373 " Someone else is using camera %p?", mCamera.get());
381 * Check whether the requested video frame size
382 * has been successfully configured or not. If both width and height
383 * are -1, check on the current width and height value setting
386 * @param params CameraParameters to retrieve the information
387 * @param the target video frame width in pixels to check against
388 * @param the target video frame height in pixels to check against
389 * @return OK if no error
391 status_t CameraSource::checkVideoSize(
392 const CameraParameters& params,
393 int32_t width, int32_t height) {
395 ALOGV("checkVideoSize");
396 // The actual video size is the same as the preview size
397 // if the camera hal does not support separate video and
398 // preview output. In this case, we retrieve the video
399 // size from preview.
400 int32_t frameWidthActual = -1;
401 int32_t frameHeightActual = -1;
403 params.getSupportedVideoSizes(sizes);
404 if (sizes.size() == 0) {
405 // video size is the same as preview size
406 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
408 // video size may not be the same as preview
409 params.getVideoSize(&frameWidthActual, &frameHeightActual);
411 if (frameWidthActual < 0 || frameHeightActual < 0) {
412 ALOGE("Failed to retrieve video frame size (%dx%d)",
413 frameWidthActual, frameHeightActual);
414 return UNKNOWN_ERROR;
417 // Check the actual video frame size against the target/requested
419 if (width != -1 && height != -1) {
420 if (frameWidthActual != width || frameHeightActual != height) {
421 ALOGE("Failed to set video frame size to %dx%d. "
422 "The actual video size is %dx%d ", width, height,
423 frameWidthActual, frameHeightActual);
424 return UNKNOWN_ERROR;
429 mVideoSize.width = frameWidthActual;
430 mVideoSize.height = frameHeightActual;
435 * Check the requested frame rate has been successfully configured or not.
436 * If the target frameRate is -1, check on the current frame rate value
437 * setting is performed.
439 * @param params CameraParameters to retrieve the information
440 * @param the target video frame rate to check against
441 * @return OK if no error.
443 status_t CameraSource::checkFrameRate(
444 const CameraParameters& params,
447 ALOGV("checkFrameRate");
448 int32_t frameRateActual = params.getPreviewFrameRate();
449 if (frameRateActual < 0) {
450 ALOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
451 return UNKNOWN_ERROR;
454 // Check the actual video frame rate against the target/requested
456 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
457 ALOGE("Failed to set preview frame rate to %d fps. The actual "
458 "frame rate is %d", frameRate, frameRateActual);
459 return UNKNOWN_ERROR;
463 mVideoFrameRate = frameRateActual;
468 * Initialize the CameraSource to so that it becomes
469 * ready for providing the video input streams as requested.
470 * @param camera the camera object used for the video source
471 * @param cameraId if camera == 0, use camera with this id
472 * as the video source
473 * @param videoSize the target video frame size. If both
474 * width and height in videoSize is -1, use the current
475 * width and heigth settings by the camera
476 * @param frameRate the target frame rate in frames per second.
477 * if it is -1, use the current camera frame rate setting.
478 * @param storeMetaDataInVideoBuffers request to store meta
479 * data or real YUV data in video buffers. Request to
480 * store meta data in video buffers may not be honored
481 * if the source does not support this feature.
483 * @return OK if no error.
485 status_t CameraSource::init(
486 const sp<ICamera>& camera,
487 const sp<ICameraRecordingProxy>& proxy,
489 const String16& clientName,
493 bool storeMetaDataInVideoBuffers) {
497 int64_t token = IPCThreadState::self()->clearCallingIdentity();
498 err = initWithCameraAccess(camera, proxy, cameraId, clientName, clientUid,
499 videoSize, frameRate,
500 storeMetaDataInVideoBuffers);
501 IPCThreadState::self()->restoreCallingIdentity(token);
505 status_t CameraSource::initWithCameraAccess(
506 const sp<ICamera>& camera,
507 const sp<ICameraRecordingProxy>& proxy,
509 const String16& clientName,
513 bool storeMetaDataInVideoBuffers) {
514 ALOGV("initWithCameraAccess");
517 if ((err = isCameraAvailable(camera, proxy, cameraId,
518 clientName, clientUid)) != OK) {
519 ALOGE("Camera connection could not be established.");
522 CameraParameters params(mCamera->getParameters());
523 if ((err = isCameraColorFormatSupported(params)) != OK) {
527 // Set the camera to use the requested video frame size
528 // and/or frame rate.
529 if ((err = configureCamera(¶ms,
530 videoSize.width, videoSize.height,
535 // Check on video frame size and frame rate.
536 CameraParameters newCameraParams(mCamera->getParameters());
537 if ((err = checkVideoSize(newCameraParams,
538 videoSize.width, videoSize.height)) != OK) {
541 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
545 // Set the preview display. Skip this if mSurface is null because
546 // applications may already set a surface to the camera.
547 if (mSurface != NULL) {
548 // This CHECK is good, since we just passed the lock/unlock
549 // check earlier by calling mCamera->setParameters().
550 CHECK_EQ((status_t)OK, mCamera->setPreviewTarget(mSurface));
553 // By default, do not store metadata in video buffers
554 mIsMetaDataStoredInVideoBuffers = false;
555 mCamera->storeMetaDataInBuffers(false);
556 if (storeMetaDataInVideoBuffers) {
557 if (OK == mCamera->storeMetaDataInBuffers(true)) {
558 mIsMetaDataStoredInVideoBuffers = true;
562 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
563 if (glitchDurationUs > mGlitchDurationThresholdUs) {
564 mGlitchDurationThresholdUs = glitchDurationUs;
567 // XXX: query camera for the stride and slice height
568 // when the capability becomes available.
569 mMeta = new MetaData;
570 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
571 mMeta->setInt32(kKeyColorFormat, mColorFormat);
572 mMeta->setInt32(kKeyWidth, mVideoSize.width);
573 mMeta->setInt32(kKeyHeight, mVideoSize.height);
574 mMeta->setInt32(kKeyStride, mVideoSize.width);
575 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
576 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
580 CameraSource::~CameraSource() {
583 } else if (mInitCheck == OK) {
584 // Camera is initialized but because start() is never called,
585 // the lock on Camera is never released(). This makes sure
586 // Camera's lock is released in this case.
591 status_t CameraSource::startCameraRecording() {
592 ALOGV("startCameraRecording");
593 // Reset the identity to the current thread because media server owns the
594 // camera and recording is started by the applications. The applications
595 // will connect to the camera in ICameraRecordingProxy::startRecording.
596 int64_t token = IPCThreadState::self()->clearCallingIdentity();
598 if (mNumInputBuffers > 0) {
599 err = mCamera->sendCommand(
600 CAMERA_CMD_SET_VIDEO_BUFFER_COUNT, mNumInputBuffers, 0);
602 // This could happen for CameraHAL1 clients; thus the failure is
605 ALOGW("Failed to set video buffer count to %d due to %d",
606 mNumInputBuffers, err);
611 if (mCameraFlags & FLAGS_HOT_CAMERA) {
614 if ((err = mCameraRecordingProxy->startRecording(
615 new ProxyListener(this))) != OK) {
616 ALOGE("Failed to start recording, received error: %s (%d)",
617 strerror(-err), err);
620 mCamera->setListener(new CameraSourceListener(this));
621 mCamera->startRecording();
622 if (!mCamera->recordingEnabled()) {
624 ALOGE("Failed to start recording");
627 IPCThreadState::self()->restoreCallingIdentity(token);
631 status_t CameraSource::start(MetaData *meta) {
634 if (mInitCheck != OK) {
635 ALOGE("CameraSource is not initialized yet");
639 char value[PROPERTY_VALUE_MAX];
640 if (property_get("media.stagefright.record-stats", value, NULL)
641 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
642 mCollectStats = true;
646 mNumInputBuffers = 0;
649 if (meta->findInt64(kKeyTime, &startTimeUs)) {
650 mStartTimeUs = startTimeUs;
654 if (meta->findInt32(kKeyNumBuffers, &nBuffers)) {
655 CHECK_GT(nBuffers, 0);
656 mNumInputBuffers = nBuffers;
661 if ((err = startCameraRecording()) == OK) {
668 void CameraSource::stopCameraRecording() {
669 ALOGV("stopCameraRecording");
670 if (mCameraFlags & FLAGS_HOT_CAMERA) {
671 mCameraRecordingProxy->stopRecording();
673 mCamera->setListener(NULL);
674 mCamera->stopRecording();
678 void CameraSource::releaseCamera() {
679 ALOGV("releaseCamera");
681 bool coldCamera = false;
683 Mutex::Autolock autoLock(mLock);
684 // get a local ref and clear ref to mCamera now
687 coldCamera = (mCameraFlags & FLAGS_HOT_CAMERA) == 0;
691 int64_t token = IPCThreadState::self()->clearCallingIdentity();
693 ALOGV("Camera was cold when we started, stopping preview");
694 camera->stopPreview();
695 camera->disconnect();
698 IPCThreadState::self()->restoreCallingIdentity(token);
702 Mutex::Autolock autoLock(mLock);
703 if (mCameraRecordingProxy != 0) {
704 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
705 mCameraRecordingProxy.clear();
711 status_t CameraSource::reset() {
715 Mutex::Autolock autoLock(mLock);
717 mFrameAvailableCondition.signal();
720 bool isTokenValid = false;
722 token = IPCThreadState::self()->clearCallingIdentity();
725 releaseQueuedFrames();
726 while (!mFramesBeingEncoded.empty()) {
728 mFrameCompleteCondition.waitRelative(mLock,
729 mTimeBetweenFrameCaptureUs * 1000LL + CAMERA_SOURCE_TIMEOUT_NS)) {
730 ALOGW("Timed out waiting for outstanding frames being encoded: %zu",
731 mFramesBeingEncoded.size());
734 stopCameraRecording();
736 IPCThreadState::self()->restoreCallingIdentity(token);
740 ALOGI("Frames received/encoded/dropped: %d/%d/%d in %" PRId64 " us",
741 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
742 mLastFrameTimestampUs - mFirstFrameTimeUs);
745 if (mNumGlitches > 0) {
746 ALOGW("%d long delays between neighboring video frames", mNumGlitches);
749 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
758 void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
759 ALOGV("releaseRecordingFrame");
760 if (mCameraRecordingProxy != NULL) {
761 mCameraRecordingProxy->releaseRecordingFrame(frame);
762 } else if (mCamera != NULL) {
763 int64_t token = IPCThreadState::self()->clearCallingIdentity();
764 mCamera->releaseRecordingFrame(frame);
765 IPCThreadState::self()->restoreCallingIdentity(token);
769 void CameraSource::releaseQueuedFrames() {
770 List<sp<IMemory> >::iterator it;
771 while (!mFramesReceived.empty()) {
772 it = mFramesReceived.begin();
773 releaseRecordingFrame(*it);
774 mFramesReceived.erase(it);
779 sp<MetaData> CameraSource::getFormat() {
783 void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
784 releaseRecordingFrame(frame);
787 void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
788 ALOGV("signalBufferReturned: %p", buffer->data());
789 Mutex::Autolock autoLock(mLock);
790 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
791 it != mFramesBeingEncoded.end(); ++it) {
792 if ((*it)->pointer() == buffer->data()) {
793 releaseOneRecordingFrame((*it));
794 mFramesBeingEncoded.erase(it);
796 buffer->setObserver(0);
798 mFrameCompleteCondition.signal();
802 CHECK(!"signalBufferReturned: bogus buffer");
805 status_t CameraSource::read(
806 MediaBuffer **buffer, const ReadOptions *options) {
812 ReadOptions::SeekMode mode;
813 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
814 return ERROR_UNSUPPORTED;
821 Mutex::Autolock autoLock(mLock);
822 while (mStarted && mFramesReceived.empty()) {
824 mFrameAvailableCondition.waitRelative(mLock,
825 mTimeBetweenFrameCaptureUs * 1000LL + CAMERA_SOURCE_TIMEOUT_NS)) {
826 if (mCameraRecordingProxy != 0 &&
827 !mCameraRecordingProxy->asBinder()->isBinderAlive()) {
828 ALOGW("camera recording proxy is gone");
829 return ERROR_END_OF_STREAM;
831 ALOGW("Timed out waiting for incoming camera video frames: %" PRId64 " us",
832 mLastFrameTimestampUs);
838 frame = *mFramesReceived.begin();
839 mFramesReceived.erase(mFramesReceived.begin());
841 frameTime = *mFrameTimes.begin();
842 mFrameTimes.erase(mFrameTimes.begin());
843 mFramesBeingEncoded.push_back(frame);
844 *buffer = new MediaBuffer(frame->pointer(), frame->size());
845 (*buffer)->setObserver(this);
846 (*buffer)->add_ref();
847 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
852 void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
853 int32_t msgType, const sp<IMemory> &data) {
854 ALOGV("dataCallbackTimestamp: timestamp %" PRId64 " us", timestampUs);
855 Mutex::Autolock autoLock(mLock);
856 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
857 ALOGV("Drop frame at %" PRId64 "/%" PRId64 " us", timestampUs, mStartTimeUs);
858 releaseOneRecordingFrame(data);
862 if (mNumFramesReceived > 0) {
863 CHECK(timestampUs > mLastFrameTimestampUs);
864 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
869 // May need to skip frame or modify timestamp. Currently implemented
870 // by the subclass CameraSourceTimeLapse.
871 if (skipCurrentFrame(timestampUs)) {
872 releaseOneRecordingFrame(data);
876 mLastFrameTimestampUs = timestampUs;
877 if (mNumFramesReceived == 0) {
878 mFirstFrameTimeUs = timestampUs;
880 if (mStartTimeUs > 0) {
881 if (timestampUs < mStartTimeUs) {
882 // Frame was captured before recording was started
883 // Drop it without updating the statistical data.
884 releaseOneRecordingFrame(data);
887 mStartTimeUs = timestampUs - mStartTimeUs;
890 ++mNumFramesReceived;
892 CHECK(data != NULL && data->size() > 0);
893 mFramesReceived.push_back(data);
894 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
895 mFrameTimes.push_back(timeUs);
896 ALOGV("initial delay: %" PRId64 ", current time stamp: %" PRId64,
897 mStartTimeUs, timeUs);
898 mFrameAvailableCondition.signal();
901 bool CameraSource::isMetaDataStoredInVideoBuffers() const {
902 ALOGV("isMetaDataStoredInVideoBuffers");
903 return mIsMetaDataStoredInVideoBuffers;
906 CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
910 void CameraSource::ProxyListener::dataCallbackTimestamp(
911 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
912 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
915 void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
916 ALOGI("Camera recording proxy died");
919 } // namespace android