blob: 94d18fa86c412a0d7451b55ebbe7e3fe7e6264c9 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
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
James Dongc32cd792010-04-26 17:48:26 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "CameraSource"
19#include <utils/Log.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070020
Andreas Huber20111aa2009-07-14 16:56:47 -070021#include <OMX_Component.h>
James Dong9d7f58a2010-06-09 15:57:48 -070022#include <binder/IPCThreadState.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070023#include <media/stagefright/CameraSource.h>
Andreas Huber0c891992009-08-26 14:48:20 -070024#include <media/stagefright/MediaDebug.h>
Andreas Huberbe5c74f2009-10-13 17:08:31 -070025#include <media/stagefright/MediaDefs.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070026#include <media/stagefright/MediaErrors.h>
27#include <media/stagefright/MetaData.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080028#include <camera/Camera.h>
29#include <camera/CameraParameters.h>
James Dong54ff19a2010-10-08 11:59:32 -070030#include <surfaceflinger/Surface.h>
Andreas Huberbe5c74f2009-10-13 17:08:31 -070031#include <utils/String8.h>
James Dong365a9632010-06-04 13:59:27 -070032#include <cutils/properties.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070033
34namespace android {
35
James Donge8e5f862011-11-20 09:45:44 -080036static const int64_t CAMERA_SOURCE_TIMEOUT_NS = 3000000000LL;
37
Andreas Huberbe5c74f2009-10-13 17:08:31 -070038struct CameraSourceListener : public CameraListener {
39 CameraSourceListener(const sp<CameraSource> &source);
40
41 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Li57c86182011-07-30 05:00:37 +080042 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr,
43 camera_frame_metadata_t *metadata);
Andreas Huberbe5c74f2009-10-13 17:08:31 -070044
45 virtual void postDataTimestamp(
46 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
47
48protected:
49 virtual ~CameraSourceListener();
50
51private:
52 wp<CameraSource> mSource;
53
54 CameraSourceListener(const CameraSourceListener &);
55 CameraSourceListener &operator=(const CameraSourceListener &);
56};
57
58CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source)
59 : mSource(source) {
60}
61
62CameraSourceListener::~CameraSourceListener() {
63}
64
65void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
Steve Block3856b092011-10-20 11:56:00 +010066 ALOGV("notify(%d, %d, %d)", msgType, ext1, ext2);
Andreas Huberbe5c74f2009-10-13 17:08:31 -070067}
68
Wu-cheng Li57c86182011-07-30 05:00:37 +080069void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr,
70 camera_frame_metadata_t *metadata) {
Steve Block3856b092011-10-20 11:56:00 +010071 ALOGV("postData(%d, ptr:%p, size:%d)",
Andreas Huberbe5c74f2009-10-13 17:08:31 -070072 msgType, dataPtr->pointer(), dataPtr->size());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070073
74 sp<CameraSource> source = mSource.promote();
75 if (source.get() != NULL) {
76 source->dataCallback(msgType, dataPtr);
77 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070078}
79
80void CameraSourceListener::postDataTimestamp(
81 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
James Dongc32cd792010-04-26 17:48:26 -070082
83 sp<CameraSource> source = mSource.promote();
84 if (source.get() != NULL) {
85 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
86 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070087}
88
James Dong653252b2010-06-03 11:48:31 -070089static int32_t getColorFormat(const char* colorFormat) {
James Donge2d8ba82010-09-15 16:52:51 -070090 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
91 return OMX_COLOR_FormatYUV420Planar;
92 }
93
James Dong653252b2010-06-03 11:48:31 -070094 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) {
95 return OMX_COLOR_FormatYUV422SemiPlanar;
96 }
97
98 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
99 return OMX_COLOR_FormatYUV420SemiPlanar;
100 }
101
102 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) {
103 return OMX_COLOR_FormatYCbYCr;
104 }
105
106 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) {
107 return OMX_COLOR_Format16bitRGB565;
108 }
109
Dandawate Saket1374edd2011-07-11 19:12:57 -0700110 if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) {
111 return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
112 }
113
James Donga1abc1a2010-09-13 16:30:51 -0700114 LOGE("Uknown color format (%s), please add it to "
115 "CameraSource::getColorFormat", colorFormat);
116
James Dong653252b2010-06-03 11:48:31 -0700117 CHECK_EQ(0, "Unknown color format");
118}
119
Andreas Huber20111aa2009-07-14 16:56:47 -0700120CameraSource *CameraSource::Create() {
James Dong54ff19a2010-10-08 11:59:32 -0700121 Size size;
122 size.width = -1;
123 size.height = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700124
James Dong54ff19a2010-10-08 11:59:32 -0700125 sp<ICamera> camera;
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800126 return new CameraSource(camera, NULL, 0, size, -1, NULL, false);
Andreas Huber20111aa2009-07-14 16:56:47 -0700127}
128
Andreas Huber30ab6622009-11-16 15:43:38 -0800129// static
James Dong54ff19a2010-10-08 11:59:32 -0700130CameraSource *CameraSource::CreateFromCamera(
131 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800132 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700133 int32_t cameraId,
134 Size videoSize,
135 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700136 const sp<Surface>& surface,
137 bool storeMetaDataInVideoBuffers) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800138
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800139 CameraSource *source = new CameraSource(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700140 videoSize, frameRate, surface,
141 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700142 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800143}
144
James Dong54ff19a2010-10-08 11:59:32 -0700145CameraSource::CameraSource(
146 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800147 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700148 int32_t cameraId,
149 Size videoSize,
150 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700151 const sp<Surface>& surface,
152 bool storeMetaDataInVideoBuffers)
James Dong54ff19a2010-10-08 11:59:32 -0700153 : mCameraFlags(0),
154 mVideoFrameRate(-1),
155 mCamera(0),
156 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700157 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700158 mLastFrameTimestampUs(0),
159 mStarted(false),
James Dong13aec892010-04-21 16:14:15 -0700160 mNumFramesEncoded(0),
James Donge8e5f862011-11-20 09:45:44 -0800161 mTimeBetweenFrameCaptureUs(0),
James Dong7757f502011-01-25 16:31:28 -0800162 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700163 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700164 mNumGlitches(0),
165 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700166 mCollectStats(false) {
James Dong54ff19a2010-10-08 11:59:32 -0700167 mVideoSize.width = -1;
168 mVideoSize.height = -1;
169
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800170 mInitCheck = init(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700171 videoSize, frameRate,
172 storeMetaDataInVideoBuffers);
Wu-cheng Li95068be2011-06-29 15:17:11 +0800173 if (mInitCheck != OK) releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700174}
175
176status_t CameraSource::initCheck() const {
177 return mInitCheck;
178}
179
180status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800181 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
182 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700183
184 if (camera == 0) {
185 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800186 if (mCamera == 0) return -EBUSY;
James Dong54ff19a2010-10-08 11:59:32 -0700187 mCameraFlags &= ~FLAGS_HOT_CAMERA;
188 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800189 // We get the proxy from Camera, not ICamera. We need to get the proxy
190 // to the remote Camera owned by the application. Here mCamera is a
191 // local Camera object created by us. We cannot use the proxy from
192 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700193 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800194 if (mCamera == 0) return -EBUSY;
195 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700196 mCameraFlags |= FLAGS_HOT_CAMERA;
James Dong3bd30202011-07-19 20:24:22 -0700197 mDeathNotifier = new DeathNotifier();
198 // isBinderAlive needs linkToDeath to work.
199 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
James Dong54ff19a2010-10-08 11:59:32 -0700200 }
201
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800202 mCamera->lock();
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800203
James Dong54ff19a2010-10-08 11:59:32 -0700204 return OK;
205}
206
207
208/*
209 * Check to see whether the requested video width and height is one
210 * of the supported sizes.
211 * @param width the video frame width in pixels
212 * @param height the video frame height in pixels
213 * @param suppportedSizes the vector of sizes that we check against
214 * @return true if the dimension (width and height) is supported.
215 */
216static bool isVideoSizeSupported(
217 int32_t width, int32_t height,
218 const Vector<Size>& supportedSizes) {
219
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("isVideoSizeSupported");
James Dong54ff19a2010-10-08 11:59:32 -0700221 for (size_t i = 0; i < supportedSizes.size(); ++i) {
222 if (width == supportedSizes[i].width &&
223 height == supportedSizes[i].height) {
224 return true;
225 }
226 }
227 return false;
228}
229
230/*
231 * If the preview and video output is separate, we only set the
232 * the video size, and applications should set the preview size
233 * to some proper value, and the recording framework will not
234 * change the preview size; otherwise, if the video and preview
235 * output is the same, we need to set the preview to be the same
236 * as the requested video size.
237 *
238 */
239/*
240 * Query the camera to retrieve the supported video frame sizes
241 * and also to see whether CameraParameters::setVideoSize()
242 * is supported or not.
243 * @param params CameraParameters to retrieve the information
244 * @@param isSetVideoSizeSupported retunrs whether method
245 * CameraParameters::setVideoSize() is supported or not.
246 * @param sizes returns the vector of Size objects for the
247 * supported video frame sizes advertised by the camera.
248 */
249static void getSupportedVideoSizes(
250 const CameraParameters& params,
251 bool *isSetVideoSizeSupported,
252 Vector<Size>& sizes) {
253
254 *isSetVideoSizeSupported = true;
255 params.getSupportedVideoSizes(sizes);
256 if (sizes.size() == 0) {
Steve Blockb8a80522011-12-20 16:23:08 +0000257 ALOGD("Camera does not support setVideoSize()");
James Dong54ff19a2010-10-08 11:59:32 -0700258 params.getSupportedPreviewSizes(sizes);
259 *isSetVideoSizeSupported = false;
260 }
261}
262
263/*
264 * Check whether the camera has the supported color format
265 * @param params CameraParameters to retrieve the information
266 * @return OK if no error.
267 */
268status_t CameraSource::isCameraColorFormatSupported(
269 const CameraParameters& params) {
270 mColorFormat = getColorFormat(params.get(
271 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
272 if (mColorFormat == -1) {
273 return BAD_VALUE;
274 }
275 return OK;
276}
277
278/*
279 * Configure the camera to use the requested video size
280 * (width and height) and/or frame rate. If both width and
281 * height are -1, configuration on the video size is skipped.
282 * if frameRate is -1, configuration on the frame rate
283 * is skipped. Skipping the configuration allows one to
284 * use the current camera setting without the need to
285 * actually know the specific values (see Create() method).
286 *
287 * @param params the CameraParameters to be configured
288 * @param width the target video frame width in pixels
289 * @param height the target video frame height in pixels
290 * @param frameRate the target frame rate in frames per second.
291 * @return OK if no error.
292 */
293status_t CameraSource::configureCamera(
294 CameraParameters* params,
295 int32_t width, int32_t height,
296 int32_t frameRate) {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("configureCamera");
James Dong54ff19a2010-10-08 11:59:32 -0700298 Vector<Size> sizes;
299 bool isSetVideoSizeSupportedByCamera = true;
300 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
301 bool isCameraParamChanged = false;
302 if (width != -1 && height != -1) {
303 if (!isVideoSizeSupported(width, height, sizes)) {
304 LOGE("Video dimension (%dx%d) is unsupported", width, height);
305 return BAD_VALUE;
306 }
307 if (isSetVideoSizeSupportedByCamera) {
308 params->setVideoSize(width, height);
309 } else {
310 params->setPreviewSize(width, height);
311 }
312 isCameraParamChanged = true;
313 } else if ((width == -1 && height != -1) ||
314 (width != -1 && height == -1)) {
315 // If one and only one of the width and height is -1
316 // we reject such a request.
317 LOGE("Requested video size (%dx%d) is not supported", width, height);
318 return BAD_VALUE;
319 } else { // width == -1 && height == -1
320 // Do not configure the camera.
321 // Use the current width and height value setting from the camera.
322 }
323
324 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700325 CHECK(frameRate > 0 && frameRate <= 120);
326 const char* supportedFrameRates =
327 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
328 CHECK(supportedFrameRates != NULL);
Steve Block3856b092011-10-20 11:56:00 +0100329 ALOGV("Supported frame rates: %s", supportedFrameRates);
James Dong63573082010-10-24 09:32:39 -0700330 char buf[4];
331 snprintf(buf, 4, "%d", frameRate);
332 if (strstr(supportedFrameRates, buf) == NULL) {
333 LOGE("Requested frame rate (%d) is not supported: %s",
334 frameRate, supportedFrameRates);
335 return BAD_VALUE;
336 }
337
338 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700339 params->setPreviewFrameRate(frameRate);
340 isCameraParamChanged = true;
341 } else { // frameRate == -1
342 // Do not configure the camera.
343 // Use the current frame rate value setting from the camera
344 }
345
346 if (isCameraParamChanged) {
347 // Either frame rate or frame size needs to be changed.
348 String8 s = params->flatten();
349 if (OK != mCamera->setParameters(s)) {
350 LOGE("Could not change settings."
351 " Someone else is using camera %p?", mCamera.get());
352 return -EBUSY;
353 }
354 }
355 return OK;
356}
357
358/*
359 * Check whether the requested video frame size
360 * has been successfully configured or not. If both width and height
361 * are -1, check on the current width and height value setting
362 * is performed.
363 *
364 * @param params CameraParameters to retrieve the information
365 * @param the target video frame width in pixels to check against
366 * @param the target video frame height in pixels to check against
367 * @return OK if no error
368 */
369status_t CameraSource::checkVideoSize(
370 const CameraParameters& params,
371 int32_t width, int32_t height) {
372
Steve Block3856b092011-10-20 11:56:00 +0100373 ALOGV("checkVideoSize");
James Dongf96c9d12010-10-20 11:41:33 -0700374 // The actual video size is the same as the preview size
375 // if the camera hal does not support separate video and
376 // preview output. In this case, we retrieve the video
377 // size from preview.
James Dong54ff19a2010-10-08 11:59:32 -0700378 int32_t frameWidthActual = -1;
379 int32_t frameHeightActual = -1;
James Dongf96c9d12010-10-20 11:41:33 -0700380 Vector<Size> sizes;
381 params.getSupportedVideoSizes(sizes);
382 if (sizes.size() == 0) {
383 // video size is the same as preview size
384 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
385 } else {
386 // video size may not be the same as preview
387 params.getVideoSize(&frameWidthActual, &frameHeightActual);
388 }
James Dong54ff19a2010-10-08 11:59:32 -0700389 if (frameWidthActual < 0 || frameHeightActual < 0) {
390 LOGE("Failed to retrieve video frame size (%dx%d)",
391 frameWidthActual, frameHeightActual);
392 return UNKNOWN_ERROR;
393 }
394
395 // Check the actual video frame size against the target/requested
396 // video frame size.
397 if (width != -1 && height != -1) {
398 if (frameWidthActual != width || frameHeightActual != height) {
399 LOGE("Failed to set video frame size to %dx%d. "
400 "The actual video size is %dx%d ", width, height,
401 frameWidthActual, frameHeightActual);
402 return UNKNOWN_ERROR;
403 }
404 }
405
406 // Good now.
407 mVideoSize.width = frameWidthActual;
408 mVideoSize.height = frameHeightActual;
409 return OK;
410}
411
412/*
413 * Check the requested frame rate has been successfully configured or not.
414 * If the target frameRate is -1, check on the current frame rate value
415 * setting is performed.
416 *
417 * @param params CameraParameters to retrieve the information
418 * @param the target video frame rate to check against
419 * @return OK if no error.
420 */
421status_t CameraSource::checkFrameRate(
422 const CameraParameters& params,
423 int32_t frameRate) {
424
Steve Block3856b092011-10-20 11:56:00 +0100425 ALOGV("checkFrameRate");
James Dong54ff19a2010-10-08 11:59:32 -0700426 int32_t frameRateActual = params.getPreviewFrameRate();
427 if (frameRateActual < 0) {
428 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
429 return UNKNOWN_ERROR;
430 }
431
432 // Check the actual video frame rate against the target/requested
433 // video frame rate.
434 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
435 LOGE("Failed to set preview frame rate to %d fps. The actual "
436 "frame rate is %d", frameRate, frameRateActual);
437 return UNKNOWN_ERROR;
438 }
439
440 // Good now.
441 mVideoFrameRate = frameRateActual;
442 return OK;
443}
444
445/*
446 * Initialize the CameraSource to so that it becomes
447 * ready for providing the video input streams as requested.
448 * @param camera the camera object used for the video source
449 * @param cameraId if camera == 0, use camera with this id
450 * as the video source
451 * @param videoSize the target video frame size. If both
452 * width and height in videoSize is -1, use the current
453 * width and heigth settings by the camera
454 * @param frameRate the target frame rate in frames per second.
455 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700456 * @param storeMetaDataInVideoBuffers request to store meta
457 * data or real YUV data in video buffers. Request to
458 * store meta data in video buffers may not be honored
459 * if the source does not support this feature.
460 *
James Dong54ff19a2010-10-08 11:59:32 -0700461 * @return OK if no error.
462 */
463status_t CameraSource::init(
464 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800465 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700466 int32_t cameraId,
467 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700468 int32_t frameRate,
469 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700470
Steve Block3856b092011-10-20 11:56:00 +0100471 ALOGV("init");
James Dong54ff19a2010-10-08 11:59:32 -0700472 status_t err = OK;
James Dongae4c1ac2011-07-08 17:59:29 -0700473 int64_t token = IPCThreadState::self()->clearCallingIdentity();
474 err = initWithCameraAccess(camera, proxy, cameraId,
475 videoSize, frameRate,
476 storeMetaDataInVideoBuffers);
477 IPCThreadState::self()->restoreCallingIdentity(token);
478 return err;
479}
480
481status_t CameraSource::initWithCameraAccess(
482 const sp<ICamera>& camera,
483 const sp<ICameraRecordingProxy>& proxy,
484 int32_t cameraId,
485 Size videoSize,
486 int32_t frameRate,
487 bool storeMetaDataInVideoBuffers) {
Steve Block3856b092011-10-20 11:56:00 +0100488 ALOGV("initWithCameraAccess");
James Dongae4c1ac2011-07-08 17:59:29 -0700489 status_t err = OK;
James Dong54ff19a2010-10-08 11:59:32 -0700490
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800491 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
492 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700493 return err;
494 }
495 CameraParameters params(mCamera->getParameters());
496 if ((err = isCameraColorFormatSupported(params)) != OK) {
497 return err;
498 }
499
500 // Set the camera to use the requested video frame size
501 // and/or frame rate.
502 if ((err = configureCamera(&params,
503 videoSize.width, videoSize.height,
504 frameRate))) {
505 return err;
506 }
507
508 // Check on video frame size and frame rate.
509 CameraParameters newCameraParams(mCamera->getParameters());
510 if ((err = checkVideoSize(newCameraParams,
511 videoSize.width, videoSize.height)) != OK) {
512 return err;
513 }
514 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
515 return err;
516 }
517
518 // This CHECK is good, since we just passed the lock/unlock
519 // check earlier by calling mCamera->setParameters().
520 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700521
James Dongabdd2ba2010-12-10 13:09:05 -0800522 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700523 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800524 mCamera->storeMetaDataInBuffers(false);
525 if (storeMetaDataInVideoBuffers) {
526 if (OK == mCamera->storeMetaDataInBuffers(true)) {
527 mIsMetaDataStoredInVideoBuffers = true;
528 }
James Dong5c952312010-10-18 21:42:27 -0700529 }
530
James Dong54ff19a2010-10-08 11:59:32 -0700531 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700532 if (glitchDurationUs > mGlitchDurationThresholdUs) {
533 mGlitchDurationThresholdUs = glitchDurationUs;
534 }
535
James Dongddcc4a62010-06-08 11:58:53 -0700536 // XXX: query camera for the stride and slice height
537 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700538 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700539 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
540 mMeta->setInt32(kKeyColorFormat, mColorFormat);
541 mMeta->setInt32(kKeyWidth, mVideoSize.width);
542 mMeta->setInt32(kKeyHeight, mVideoSize.height);
543 mMeta->setInt32(kKeyStride, mVideoSize.width);
544 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800545 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700546 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700547}
548
549CameraSource::~CameraSource() {
550 if (mStarted) {
551 stop();
James Dongae4c1ac2011-07-08 17:59:29 -0700552 } else if (mInitCheck == OK) {
553 // Camera is initialized but because start() is never called,
554 // the lock on Camera is never released(). This makes sure
555 // Camera's lock is released in this case.
556 releaseCamera();
Andreas Huber20111aa2009-07-14 16:56:47 -0700557 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700558}
559
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700560void CameraSource::startCameraRecording() {
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("startCameraRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800562 // Reset the identity to the current thread because media server owns the
563 // camera and recording is started by the applications. The applications
564 // will connect to the camera in ICameraRecordingProxy::startRecording.
565 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong3bd30202011-07-19 20:24:22 -0700566 if (mCameraFlags & FLAGS_HOT_CAMERA) {
567 mCamera->unlock();
568 mCamera.clear();
569 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
570 } else {
571 mCamera->setListener(new CameraSourceListener(this));
572 mCamera->startRecording();
573 CHECK(mCamera->recordingEnabled());
574 }
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800575 IPCThreadState::self()->restoreCallingIdentity(token);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700576}
577
James Dongf60cafe2010-06-19 09:04:18 -0700578status_t CameraSource::start(MetaData *meta) {
Steve Block3856b092011-10-20 11:56:00 +0100579 ALOGV("start");
Andreas Huber0c891992009-08-26 14:48:20 -0700580 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700581 if (mInitCheck != OK) {
582 LOGE("CameraSource is not initialized yet");
583 return mInitCheck;
584 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700585
James Dong365a9632010-06-04 13:59:27 -0700586 char value[PROPERTY_VALUE_MAX];
587 if (property_get("media.stagefright.record-stats", value, NULL)
588 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
589 mCollectStats = true;
590 }
James Dong9d7f58a2010-06-09 15:57:48 -0700591
James Dongf60cafe2010-06-19 09:04:18 -0700592 mStartTimeUs = 0;
593 int64_t startTimeUs;
594 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
595 mStartTimeUs = startTimeUs;
596 }
597
James Dongc42478e2010-11-15 10:38:37 -0800598 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700599
600 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700601 return OK;
602}
603
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700604void CameraSource::stopCameraRecording() {
Steve Block3856b092011-10-20 11:56:00 +0100605 ALOGV("stopCameraRecording");
James Dong3bd30202011-07-19 20:24:22 -0700606 if (mCameraFlags & FLAGS_HOT_CAMERA) {
607 mCameraRecordingProxy->stopRecording();
608 } else {
609 mCamera->setListener(NULL);
610 mCamera->stopRecording();
611 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700612}
613
James Dongea7b4852010-12-05 14:25:34 -0800614void CameraSource::releaseCamera() {
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("releaseCamera");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800616 if (mCamera != 0) {
James Dongae4c1ac2011-07-08 17:59:29 -0700617 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800618 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100619 ALOGV("Camera was cold when we started, stopping preview");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800620 mCamera->stopPreview();
621 mCamera->disconnect();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800622 }
James Dong3bd30202011-07-19 20:24:22 -0700623 mCamera->unlock();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800624 mCamera.clear();
James Dong3bd30202011-07-19 20:24:22 -0700625 mCamera = 0;
James Dongae4c1ac2011-07-08 17:59:29 -0700626 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongea7b4852010-12-05 14:25:34 -0800627 }
Wu-cheng Li95068be2011-06-29 15:17:11 +0800628 if (mCameraRecordingProxy != 0) {
629 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
630 mCameraRecordingProxy.clear();
631 }
James Dongea7b4852010-12-05 14:25:34 -0800632 mCameraFlags = 0;
633}
634
Andreas Huber20111aa2009-07-14 16:56:47 -0700635status_t CameraSource::stop() {
Steve Blockb8a80522011-12-20 16:23:08 +0000636 ALOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700637 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700638 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700639 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700640
James Dong91974412011-08-24 19:50:36 -0700641 int64_t token;
642 bool isTokenValid = false;
643 if (mCamera != 0) {
644 token = IPCThreadState::self()->clearCallingIdentity();
645 isTokenValid = true;
646 }
James Dongc32cd792010-04-26 17:48:26 -0700647 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700648 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800649 if (NO_ERROR !=
James Donge8e5f862011-11-20 09:45:44 -0800650 mFrameCompleteCondition.waitRelative(mLock,
651 mTimeBetweenFrameCaptureUs * 1000LL + CAMERA_SOURCE_TIMEOUT_NS)) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000652 ALOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700653 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800654 }
James Dong7278cf32010-05-27 16:05:58 -0700655 }
James Dongd69c7f62010-12-09 11:24:22 -0800656 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800657 releaseCamera();
James Dong91974412011-08-24 19:50:36 -0700658 if (isTokenValid) {
659 IPCThreadState::self()->restoreCallingIdentity(token);
660 }
James Dong7278cf32010-05-27 16:05:58 -0700661
James Dong365a9632010-06-04 13:59:27 -0700662 if (mCollectStats) {
Steve Blockdf64d152012-01-04 20:05:49 +0000663 ALOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
James Dong365a9632010-06-04 13:59:27 -0700664 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
665 mLastFrameTimestampUs - mFirstFrameTimeUs);
666 }
James Dong13aec892010-04-21 16:14:15 -0700667
James Dongba290022010-12-09 15:04:33 -0800668 if (mNumGlitches > 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000669 ALOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800670 }
671
James Dong13aec892010-04-21 16:14:15 -0700672 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
Steve Blockb8a80522011-12-20 16:23:08 +0000673 ALOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700674 return OK;
675}
676
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700677void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800679 if (mCameraRecordingProxy != NULL) {
680 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dong334d0972011-08-05 17:19:29 -0700681 } else if (mCamera != NULL) {
James Dong3bd30202011-07-19 20:24:22 -0700682 int64_t token = IPCThreadState::self()->clearCallingIdentity();
683 mCamera->releaseRecordingFrame(frame);
684 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongd69c7f62010-12-09 11:24:22 -0800685 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700686}
687
James Dongc32cd792010-04-26 17:48:26 -0700688void CameraSource::releaseQueuedFrames() {
689 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700690 while (!mFramesReceived.empty()) {
691 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700692 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700693 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700694 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700695 }
696}
697
Andreas Huber20111aa2009-07-14 16:56:47 -0700698sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700699 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700700}
701
James Dongf60cafe2010-06-19 09:04:18 -0700702void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700703 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700704}
705
James Dong7278cf32010-05-27 16:05:58 -0700706void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700708 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700709 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
710 it != mFramesBeingEncoded.end(); ++it) {
711 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700712 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700713 mFramesBeingEncoded.erase(it);
714 ++mNumFramesEncoded;
715 buffer->setObserver(0);
716 buffer->release();
717 mFrameCompleteCondition.signal();
718 return;
719 }
720 }
721 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
722}
723
Andreas Huber20111aa2009-07-14 16:56:47 -0700724status_t CameraSource::read(
725 MediaBuffer **buffer, const ReadOptions *options) {
Steve Block3856b092011-10-20 11:56:00 +0100726 ALOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700727
728 *buffer = NULL;
729
730 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700731 ReadOptions::SeekMode mode;
732 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700733 return ERROR_UNSUPPORTED;
734 }
735
736 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700737 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700738
739 {
740 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800741 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800742 if (NO_ERROR !=
James Donge8e5f862011-11-20 09:45:44 -0800743 mFrameAvailableCondition.waitRelative(mLock,
744 mTimeBetweenFrameCaptureUs * 1000LL + CAMERA_SOURCE_TIMEOUT_NS)) {
James Dong3bd30202011-07-19 20:24:22 -0700745 if (mCameraRecordingProxy != 0 &&
746 !mCameraRecordingProxy->asBinder()->isBinderAlive()) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000747 ALOGW("camera recording proxy is gone");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800748 return ERROR_END_OF_STREAM;
749 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000750 ALOGW("Timed out waiting for incoming camera video frames: %lld us",
James Dong41152ef2010-12-20 21:29:12 -0800751 mLastFrameTimestampUs);
752 }
James Dong542db5d2010-07-21 14:51:35 -0700753 }
James Dong79e23b42010-12-11 10:43:41 -0800754 if (!mStarted) {
755 return OK;
756 }
757 frame = *mFramesReceived.begin();
758 mFramesReceived.erase(mFramesReceived.begin());
759
760 frameTime = *mFrameTimes.begin();
761 mFrameTimes.erase(mFrameTimes.begin());
762 mFramesBeingEncoded.push_back(frame);
763 *buffer = new MediaBuffer(frame->pointer(), frame->size());
764 (*buffer)->setObserver(this);
765 (*buffer)->add_ref();
766 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700767 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700768 return OK;
769}
770
James Dongc32cd792010-04-26 17:48:26 -0700771void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
772 int32_t msgType, const sp<IMemory> &data) {
Steve Block3856b092011-10-20 11:56:00 +0100773 ALOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700774 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800775 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
Steve Block3856b092011-10-20 11:56:00 +0100776 ALOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700777 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700778 return;
779 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700780
James Dong98cfde02011-06-03 17:21:16 -0700781 if (mNumFramesReceived > 0) {
782 CHECK(timestampUs > mLastFrameTimestampUs);
783 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
784 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700785 }
James Dongf60cafe2010-06-19 09:04:18 -0700786 }
787
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700788 // May need to skip frame or modify timestamp. Currently implemented
789 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800790 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700791 releaseOneRecordingFrame(data);
792 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700793 }
794
James Dong365a9632010-06-04 13:59:27 -0700795 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700796 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700797 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700798 // Initial delay
799 if (mStartTimeUs > 0) {
800 if (timestampUs < mStartTimeUs) {
801 // Frame was captured before recording was started
802 // Drop it without updating the statistical data.
803 releaseOneRecordingFrame(data);
804 return;
805 }
806 mStartTimeUs = timestampUs - mStartTimeUs;
807 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700808 }
James Dong13aec892010-04-21 16:14:15 -0700809 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700810
James Dong98cfde02011-06-03 17:21:16 -0700811 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700812 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700813 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
814 mFrameTimes.push_back(timeUs);
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("initial delay: %lld, current time stamp: %lld",
James Dongf60cafe2010-06-19 09:04:18 -0700816 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700817 mFrameAvailableCondition.signal();
818}
819
James Dong5c952312010-10-18 21:42:27 -0700820bool CameraSource::isMetaDataStoredInVideoBuffers() const {
Steve Block3856b092011-10-20 11:56:00 +0100821 ALOGV("isMetaDataStoredInVideoBuffers");
James Dong5c952312010-10-18 21:42:27 -0700822 return mIsMetaDataStoredInVideoBuffers;
823}
824
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800825CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
826 mSource = source;
827}
828
829void CameraSource::ProxyListener::dataCallbackTimestamp(
830 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
831 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
832}
833
834void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
Steve Blockdf64d152012-01-04 20:05:49 +0000835 ALOGI("Camera recording proxy died");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800836}
837
Andreas Huber20111aa2009-07-14 16:56:47 -0700838} // namespace android