blob: 256f3baaa9ad01b0a9109dd472fcb0d9d3862329 [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
Andreas Huberbe5c74f2009-10-13 17:08:31 -070036struct CameraSourceListener : public CameraListener {
37 CameraSourceListener(const sp<CameraSource> &source);
38
39 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Li57c86182011-07-30 05:00:37 +080040 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr,
41 camera_frame_metadata_t *metadata);
Andreas Huberbe5c74f2009-10-13 17:08:31 -070042
43 virtual void postDataTimestamp(
44 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
45
46protected:
47 virtual ~CameraSourceListener();
48
49private:
50 wp<CameraSource> mSource;
51
52 CameraSourceListener(const CameraSourceListener &);
53 CameraSourceListener &operator=(const CameraSourceListener &);
54};
55
56CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source)
57 : mSource(source) {
58}
59
60CameraSourceListener::~CameraSourceListener() {
61}
62
63void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
64 LOGV("notify(%d, %d, %d)", msgType, ext1, ext2);
65}
66
Wu-cheng Li57c86182011-07-30 05:00:37 +080067void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr,
68 camera_frame_metadata_t *metadata) {
Andreas Huberbe5c74f2009-10-13 17:08:31 -070069 LOGV("postData(%d, ptr:%p, size:%d)",
70 msgType, dataPtr->pointer(), dataPtr->size());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070071
72 sp<CameraSource> source = mSource.promote();
73 if (source.get() != NULL) {
74 source->dataCallback(msgType, dataPtr);
75 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070076}
77
78void CameraSourceListener::postDataTimestamp(
79 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
James Dongc32cd792010-04-26 17:48:26 -070080
81 sp<CameraSource> source = mSource.promote();
82 if (source.get() != NULL) {
83 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
84 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070085}
86
James Dong653252b2010-06-03 11:48:31 -070087static int32_t getColorFormat(const char* colorFormat) {
James Donge2d8ba82010-09-15 16:52:51 -070088 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
89 return OMX_COLOR_FormatYUV420Planar;
90 }
91
James Dong653252b2010-06-03 11:48:31 -070092 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) {
93 return OMX_COLOR_FormatYUV422SemiPlanar;
94 }
95
96 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
97 return OMX_COLOR_FormatYUV420SemiPlanar;
98 }
99
100 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) {
101 return OMX_COLOR_FormatYCbYCr;
102 }
103
104 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) {
105 return OMX_COLOR_Format16bitRGB565;
106 }
107
Dandawate Saket1374edd2011-07-11 19:12:57 -0700108 if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) {
109 return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
110 }
111
James Donga1abc1a2010-09-13 16:30:51 -0700112 LOGE("Uknown color format (%s), please add it to "
113 "CameraSource::getColorFormat", colorFormat);
114
James Dong653252b2010-06-03 11:48:31 -0700115 CHECK_EQ(0, "Unknown color format");
116}
117
Andreas Huber20111aa2009-07-14 16:56:47 -0700118CameraSource *CameraSource::Create() {
James Dong54ff19a2010-10-08 11:59:32 -0700119 Size size;
120 size.width = -1;
121 size.height = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700122
James Dong54ff19a2010-10-08 11:59:32 -0700123 sp<ICamera> camera;
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800124 return new CameraSource(camera, NULL, 0, size, -1, NULL, false);
Andreas Huber20111aa2009-07-14 16:56:47 -0700125}
126
Andreas Huber30ab6622009-11-16 15:43:38 -0800127// static
James Dong54ff19a2010-10-08 11:59:32 -0700128CameraSource *CameraSource::CreateFromCamera(
129 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800130 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700131 int32_t cameraId,
132 Size videoSize,
133 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700134 const sp<Surface>& surface,
135 bool storeMetaDataInVideoBuffers) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800136
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800137 CameraSource *source = new CameraSource(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700138 videoSize, frameRate, surface,
139 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700140 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800141}
142
James Dong54ff19a2010-10-08 11:59:32 -0700143CameraSource::CameraSource(
144 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800145 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700146 int32_t cameraId,
147 Size videoSize,
148 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700149 const sp<Surface>& surface,
150 bool storeMetaDataInVideoBuffers)
James Dong54ff19a2010-10-08 11:59:32 -0700151 : mCameraFlags(0),
152 mVideoFrameRate(-1),
153 mCamera(0),
154 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700155 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700156 mLastFrameTimestampUs(0),
157 mStarted(false),
James Dong13aec892010-04-21 16:14:15 -0700158 mNumFramesEncoded(0),
James Dong7757f502011-01-25 16:31:28 -0800159 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700160 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700161 mNumGlitches(0),
162 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700163 mCollectStats(false) {
James Dong54ff19a2010-10-08 11:59:32 -0700164 mVideoSize.width = -1;
165 mVideoSize.height = -1;
166
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800167 mInitCheck = init(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700168 videoSize, frameRate,
169 storeMetaDataInVideoBuffers);
Wu-cheng Li95068be2011-06-29 15:17:11 +0800170 if (mInitCheck != OK) releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700171}
172
173status_t CameraSource::initCheck() const {
174 return mInitCheck;
175}
176
177status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800178 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
179 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700180
181 if (camera == 0) {
182 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800183 if (mCamera == 0) return -EBUSY;
James Dong54ff19a2010-10-08 11:59:32 -0700184 mCameraFlags &= ~FLAGS_HOT_CAMERA;
185 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800186 // We get the proxy from Camera, not ICamera. We need to get the proxy
187 // to the remote Camera owned by the application. Here mCamera is a
188 // local Camera object created by us. We cannot use the proxy from
189 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700190 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800191 if (mCamera == 0) return -EBUSY;
192 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700193 mCameraFlags |= FLAGS_HOT_CAMERA;
James Dong3bd30202011-07-19 20:24:22 -0700194 mDeathNotifier = new DeathNotifier();
195 // isBinderAlive needs linkToDeath to work.
196 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
James Dong54ff19a2010-10-08 11:59:32 -0700197 }
198
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800199 mCamera->lock();
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800200
James Dong54ff19a2010-10-08 11:59:32 -0700201 return OK;
202}
203
204
205/*
206 * Check to see whether the requested video width and height is one
207 * of the supported sizes.
208 * @param width the video frame width in pixels
209 * @param height the video frame height in pixels
210 * @param suppportedSizes the vector of sizes that we check against
211 * @return true if the dimension (width and height) is supported.
212 */
213static bool isVideoSizeSupported(
214 int32_t width, int32_t height,
215 const Vector<Size>& supportedSizes) {
216
217 LOGV("isVideoSizeSupported");
218 for (size_t i = 0; i < supportedSizes.size(); ++i) {
219 if (width == supportedSizes[i].width &&
220 height == supportedSizes[i].height) {
221 return true;
222 }
223 }
224 return false;
225}
226
227/*
228 * If the preview and video output is separate, we only set the
229 * the video size, and applications should set the preview size
230 * to some proper value, and the recording framework will not
231 * change the preview size; otherwise, if the video and preview
232 * output is the same, we need to set the preview to be the same
233 * as the requested video size.
234 *
235 */
236/*
237 * Query the camera to retrieve the supported video frame sizes
238 * and also to see whether CameraParameters::setVideoSize()
239 * is supported or not.
240 * @param params CameraParameters to retrieve the information
241 * @@param isSetVideoSizeSupported retunrs whether method
242 * CameraParameters::setVideoSize() is supported or not.
243 * @param sizes returns the vector of Size objects for the
244 * supported video frame sizes advertised by the camera.
245 */
246static void getSupportedVideoSizes(
247 const CameraParameters& params,
248 bool *isSetVideoSizeSupported,
249 Vector<Size>& sizes) {
250
251 *isSetVideoSizeSupported = true;
252 params.getSupportedVideoSizes(sizes);
253 if (sizes.size() == 0) {
254 LOGD("Camera does not support setVideoSize()");
255 params.getSupportedPreviewSizes(sizes);
256 *isSetVideoSizeSupported = false;
257 }
258}
259
260/*
261 * Check whether the camera has the supported color format
262 * @param params CameraParameters to retrieve the information
263 * @return OK if no error.
264 */
265status_t CameraSource::isCameraColorFormatSupported(
266 const CameraParameters& params) {
267 mColorFormat = getColorFormat(params.get(
268 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
269 if (mColorFormat == -1) {
270 return BAD_VALUE;
271 }
272 return OK;
273}
274
275/*
276 * Configure the camera to use the requested video size
277 * (width and height) and/or frame rate. If both width and
278 * height are -1, configuration on the video size is skipped.
279 * if frameRate is -1, configuration on the frame rate
280 * is skipped. Skipping the configuration allows one to
281 * use the current camera setting without the need to
282 * actually know the specific values (see Create() method).
283 *
284 * @param params the CameraParameters to be configured
285 * @param width the target video frame width in pixels
286 * @param height the target video frame height in pixels
287 * @param frameRate the target frame rate in frames per second.
288 * @return OK if no error.
289 */
290status_t CameraSource::configureCamera(
291 CameraParameters* params,
292 int32_t width, int32_t height,
293 int32_t frameRate) {
James Dong3bd30202011-07-19 20:24:22 -0700294 LOGV("configureCamera");
James Dong54ff19a2010-10-08 11:59:32 -0700295 Vector<Size> sizes;
296 bool isSetVideoSizeSupportedByCamera = true;
297 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
298 bool isCameraParamChanged = false;
299 if (width != -1 && height != -1) {
300 if (!isVideoSizeSupported(width, height, sizes)) {
301 LOGE("Video dimension (%dx%d) is unsupported", width, height);
302 return BAD_VALUE;
303 }
304 if (isSetVideoSizeSupportedByCamera) {
305 params->setVideoSize(width, height);
306 } else {
307 params->setPreviewSize(width, height);
308 }
309 isCameraParamChanged = true;
310 } else if ((width == -1 && height != -1) ||
311 (width != -1 && height == -1)) {
312 // If one and only one of the width and height is -1
313 // we reject such a request.
314 LOGE("Requested video size (%dx%d) is not supported", width, height);
315 return BAD_VALUE;
316 } else { // width == -1 && height == -1
317 // Do not configure the camera.
318 // Use the current width and height value setting from the camera.
319 }
320
321 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700322 CHECK(frameRate > 0 && frameRate <= 120);
323 const char* supportedFrameRates =
324 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
325 CHECK(supportedFrameRates != NULL);
326 LOGV("Supported frame rates: %s", supportedFrameRates);
327 char buf[4];
328 snprintf(buf, 4, "%d", frameRate);
329 if (strstr(supportedFrameRates, buf) == NULL) {
330 LOGE("Requested frame rate (%d) is not supported: %s",
331 frameRate, supportedFrameRates);
332 return BAD_VALUE;
333 }
334
335 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700336 params->setPreviewFrameRate(frameRate);
337 isCameraParamChanged = true;
338 } else { // frameRate == -1
339 // Do not configure the camera.
340 // Use the current frame rate value setting from the camera
341 }
342
343 if (isCameraParamChanged) {
344 // Either frame rate or frame size needs to be changed.
345 String8 s = params->flatten();
346 if (OK != mCamera->setParameters(s)) {
347 LOGE("Could not change settings."
348 " Someone else is using camera %p?", mCamera.get());
349 return -EBUSY;
350 }
351 }
352 return OK;
353}
354
355/*
356 * Check whether the requested video frame size
357 * has been successfully configured or not. If both width and height
358 * are -1, check on the current width and height value setting
359 * is performed.
360 *
361 * @param params CameraParameters to retrieve the information
362 * @param the target video frame width in pixels to check against
363 * @param the target video frame height in pixels to check against
364 * @return OK if no error
365 */
366status_t CameraSource::checkVideoSize(
367 const CameraParameters& params,
368 int32_t width, int32_t height) {
369
James Dong3bd30202011-07-19 20:24:22 -0700370 LOGV("checkVideoSize");
James Dongf96c9d12010-10-20 11:41:33 -0700371 // 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.
James Dong54ff19a2010-10-08 11:59:32 -0700375 int32_t frameWidthActual = -1;
376 int32_t frameHeightActual = -1;
James Dongf96c9d12010-10-20 11:41:33 -0700377 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 }
James Dong54ff19a2010-10-08 11:59:32 -0700386 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
James Dong3bd30202011-07-19 20:24:22 -0700422 LOGV("checkFrameRate");
James Dong54ff19a2010-10-08 11:59:32 -0700423 int32_t frameRateActual = params.getPreviewFrameRate();
424 if (frameRateActual < 0) {
425 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
426 return UNKNOWN_ERROR;
427 }
428
429 // Check the actual video frame rate against the target/requested
430 // video frame rate.
431 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
432 LOGE("Failed to set preview frame rate to %d fps. The actual "
433 "frame rate is %d", frameRate, frameRateActual);
434 return UNKNOWN_ERROR;
435 }
436
437 // Good now.
438 mVideoFrameRate = frameRateActual;
439 return OK;
440}
441
442/*
443 * Initialize the CameraSource to so that it becomes
444 * ready for providing the video input streams as requested.
445 * @param camera the camera object used for the video source
446 * @param cameraId if camera == 0, use camera with this id
447 * as the video source
448 * @param videoSize the target video frame size. If both
449 * width and height in videoSize is -1, use the current
450 * width and heigth settings by the camera
451 * @param frameRate the target frame rate in frames per second.
452 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700453 * @param storeMetaDataInVideoBuffers request to store meta
454 * data or real YUV data in video buffers. Request to
455 * store meta data in video buffers may not be honored
456 * if the source does not support this feature.
457 *
James Dong54ff19a2010-10-08 11:59:32 -0700458 * @return OK if no error.
459 */
460status_t CameraSource::init(
461 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800462 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700463 int32_t cameraId,
464 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700465 int32_t frameRate,
466 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700467
James Dong3bd30202011-07-19 20:24:22 -0700468 LOGV("init");
James Dong54ff19a2010-10-08 11:59:32 -0700469 status_t err = OK;
James Dongae4c1ac2011-07-08 17:59:29 -0700470 int64_t token = IPCThreadState::self()->clearCallingIdentity();
471 err = initWithCameraAccess(camera, proxy, cameraId,
472 videoSize, frameRate,
473 storeMetaDataInVideoBuffers);
474 IPCThreadState::self()->restoreCallingIdentity(token);
475 return err;
476}
477
478status_t CameraSource::initWithCameraAccess(
479 const sp<ICamera>& camera,
480 const sp<ICameraRecordingProxy>& proxy,
481 int32_t cameraId,
482 Size videoSize,
483 int32_t frameRate,
484 bool storeMetaDataInVideoBuffers) {
James Dong3bd30202011-07-19 20:24:22 -0700485 LOGV("initWithCameraAccess");
James Dongae4c1ac2011-07-08 17:59:29 -0700486 status_t err = OK;
James Dong54ff19a2010-10-08 11:59:32 -0700487
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800488 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
489 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700490 return err;
491 }
492 CameraParameters params(mCamera->getParameters());
493 if ((err = isCameraColorFormatSupported(params)) != OK) {
494 return err;
495 }
496
497 // Set the camera to use the requested video frame size
498 // and/or frame rate.
499 if ((err = configureCamera(&params,
500 videoSize.width, videoSize.height,
501 frameRate))) {
502 return err;
503 }
504
505 // Check on video frame size and frame rate.
506 CameraParameters newCameraParams(mCamera->getParameters());
507 if ((err = checkVideoSize(newCameraParams,
508 videoSize.width, videoSize.height)) != OK) {
509 return err;
510 }
511 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
512 return err;
513 }
514
515 // This CHECK is good, since we just passed the lock/unlock
516 // check earlier by calling mCamera->setParameters().
517 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700518
James Dongabdd2ba2010-12-10 13:09:05 -0800519 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700520 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800521 mCamera->storeMetaDataInBuffers(false);
522 if (storeMetaDataInVideoBuffers) {
523 if (OK == mCamera->storeMetaDataInBuffers(true)) {
524 mIsMetaDataStoredInVideoBuffers = true;
525 }
James Dong5c952312010-10-18 21:42:27 -0700526 }
527
James Dong54ff19a2010-10-08 11:59:32 -0700528 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700529 if (glitchDurationUs > mGlitchDurationThresholdUs) {
530 mGlitchDurationThresholdUs = glitchDurationUs;
531 }
532
James Dongddcc4a62010-06-08 11:58:53 -0700533 // XXX: query camera for the stride and slice height
534 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700535 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700536 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
537 mMeta->setInt32(kKeyColorFormat, mColorFormat);
538 mMeta->setInt32(kKeyWidth, mVideoSize.width);
539 mMeta->setInt32(kKeyHeight, mVideoSize.height);
540 mMeta->setInt32(kKeyStride, mVideoSize.width);
541 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800542 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700543 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700544}
545
546CameraSource::~CameraSource() {
547 if (mStarted) {
548 stop();
James Dongae4c1ac2011-07-08 17:59:29 -0700549 } else if (mInitCheck == OK) {
550 // Camera is initialized but because start() is never called,
551 // the lock on Camera is never released(). This makes sure
552 // Camera's lock is released in this case.
553 releaseCamera();
Andreas Huber20111aa2009-07-14 16:56:47 -0700554 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700555}
556
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700557void CameraSource::startCameraRecording() {
James Dong3bd30202011-07-19 20:24:22 -0700558 LOGV("startCameraRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800559 // Reset the identity to the current thread because media server owns the
560 // camera and recording is started by the applications. The applications
561 // will connect to the camera in ICameraRecordingProxy::startRecording.
562 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong3bd30202011-07-19 20:24:22 -0700563 if (mCameraFlags & FLAGS_HOT_CAMERA) {
564 mCamera->unlock();
565 mCamera.clear();
566 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
567 } else {
568 mCamera->setListener(new CameraSourceListener(this));
569 mCamera->startRecording();
570 CHECK(mCamera->recordingEnabled());
571 }
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800572 IPCThreadState::self()->restoreCallingIdentity(token);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700573}
574
James Dongf60cafe2010-06-19 09:04:18 -0700575status_t CameraSource::start(MetaData *meta) {
James Dong3bd30202011-07-19 20:24:22 -0700576 LOGV("start");
Andreas Huber0c891992009-08-26 14:48:20 -0700577 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700578 if (mInitCheck != OK) {
579 LOGE("CameraSource is not initialized yet");
580 return mInitCheck;
581 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700582
James Dong365a9632010-06-04 13:59:27 -0700583 char value[PROPERTY_VALUE_MAX];
584 if (property_get("media.stagefright.record-stats", value, NULL)
585 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
586 mCollectStats = true;
587 }
James Dong9d7f58a2010-06-09 15:57:48 -0700588
James Dongf60cafe2010-06-19 09:04:18 -0700589 mStartTimeUs = 0;
590 int64_t startTimeUs;
591 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
592 mStartTimeUs = startTimeUs;
593 }
594
James Dongc42478e2010-11-15 10:38:37 -0800595 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700596
597 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700598 return OK;
599}
600
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700601void CameraSource::stopCameraRecording() {
James Dong3bd30202011-07-19 20:24:22 -0700602 LOGV("stopCameraRecording");
603 if (mCameraFlags & FLAGS_HOT_CAMERA) {
604 mCameraRecordingProxy->stopRecording();
605 } else {
606 mCamera->setListener(NULL);
607 mCamera->stopRecording();
608 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700609}
610
James Dongea7b4852010-12-05 14:25:34 -0800611void CameraSource::releaseCamera() {
612 LOGV("releaseCamera");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800613 if (mCamera != 0) {
James Dongae4c1ac2011-07-08 17:59:29 -0700614 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800615 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
616 LOGV("Camera was cold when we started, stopping preview");
617 mCamera->stopPreview();
618 mCamera->disconnect();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800619 }
James Dong3bd30202011-07-19 20:24:22 -0700620 mCamera->unlock();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800621 mCamera.clear();
James Dong3bd30202011-07-19 20:24:22 -0700622 mCamera = 0;
James Dongae4c1ac2011-07-08 17:59:29 -0700623 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongea7b4852010-12-05 14:25:34 -0800624 }
Wu-cheng Li95068be2011-06-29 15:17:11 +0800625 if (mCameraRecordingProxy != 0) {
626 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
627 mCameraRecordingProxy.clear();
628 }
James Dongea7b4852010-12-05 14:25:34 -0800629 mCameraFlags = 0;
630}
631
Andreas Huber20111aa2009-07-14 16:56:47 -0700632status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800633 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700634 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700635 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700636 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700637
James Dong91974412011-08-24 19:50:36 -0700638 int64_t token;
639 bool isTokenValid = false;
640 if (mCamera != 0) {
641 token = IPCThreadState::self()->clearCallingIdentity();
642 isTokenValid = true;
643 }
James Dongc32cd792010-04-26 17:48:26 -0700644 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700645 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800646 if (NO_ERROR !=
647 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
648 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700649 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800650 }
James Dong7278cf32010-05-27 16:05:58 -0700651 }
James Dongd69c7f62010-12-09 11:24:22 -0800652 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800653 releaseCamera();
James Dong91974412011-08-24 19:50:36 -0700654 if (isTokenValid) {
655 IPCThreadState::self()->restoreCallingIdentity(token);
656 }
James Dong7278cf32010-05-27 16:05:58 -0700657
James Dong365a9632010-06-04 13:59:27 -0700658 if (mCollectStats) {
659 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
660 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
661 mLastFrameTimestampUs - mFirstFrameTimeUs);
662 }
James Dong13aec892010-04-21 16:14:15 -0700663
James Dongba290022010-12-09 15:04:33 -0800664 if (mNumGlitches > 0) {
James Donga4726132011-02-16 12:28:26 -0800665 LOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800666 }
667
James Dong13aec892010-04-21 16:14:15 -0700668 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800669 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700670 return OK;
671}
672
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700673void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
James Dong3bd30202011-07-19 20:24:22 -0700674 LOGV("releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800675 if (mCameraRecordingProxy != NULL) {
676 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dong334d0972011-08-05 17:19:29 -0700677 } else if (mCamera != NULL) {
James Dong3bd30202011-07-19 20:24:22 -0700678 int64_t token = IPCThreadState::self()->clearCallingIdentity();
679 mCamera->releaseRecordingFrame(frame);
680 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongd69c7f62010-12-09 11:24:22 -0800681 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700682}
683
James Dongc32cd792010-04-26 17:48:26 -0700684void CameraSource::releaseQueuedFrames() {
685 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700686 while (!mFramesReceived.empty()) {
687 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700688 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700689 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700690 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700691 }
692}
693
Andreas Huber20111aa2009-07-14 16:56:47 -0700694sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700695 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700696}
697
James Dongf60cafe2010-06-19 09:04:18 -0700698void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700699 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700700}
701
James Dong7278cf32010-05-27 16:05:58 -0700702void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
703 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700704 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700705 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
706 it != mFramesBeingEncoded.end(); ++it) {
707 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700708 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700709 mFramesBeingEncoded.erase(it);
710 ++mNumFramesEncoded;
711 buffer->setObserver(0);
712 buffer->release();
713 mFrameCompleteCondition.signal();
714 return;
715 }
716 }
717 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
718}
719
Andreas Huber20111aa2009-07-14 16:56:47 -0700720status_t CameraSource::read(
721 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700722 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700723
724 *buffer = NULL;
725
726 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700727 ReadOptions::SeekMode mode;
728 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700729 return ERROR_UNSUPPORTED;
730 }
731
732 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700733 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700734
735 {
736 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800737 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800738 if (NO_ERROR !=
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800739 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) {
James Dong3bd30202011-07-19 20:24:22 -0700740 if (mCameraRecordingProxy != 0 &&
741 !mCameraRecordingProxy->asBinder()->isBinderAlive()) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800742 LOGW("camera recording proxy is gone");
743 return ERROR_END_OF_STREAM;
744 }
James Dong41152ef2010-12-20 21:29:12 -0800745 LOGW("Timed out waiting for incoming camera video frames: %lld us",
746 mLastFrameTimestampUs);
747 }
James Dong542db5d2010-07-21 14:51:35 -0700748 }
James Dong79e23b42010-12-11 10:43:41 -0800749 if (!mStarted) {
750 return OK;
751 }
752 frame = *mFramesReceived.begin();
753 mFramesReceived.erase(mFramesReceived.begin());
754
755 frameTime = *mFrameTimes.begin();
756 mFrameTimes.erase(mFrameTimes.begin());
757 mFramesBeingEncoded.push_back(frame);
758 *buffer = new MediaBuffer(frame->pointer(), frame->size());
759 (*buffer)->setObserver(this);
760 (*buffer)->add_ref();
761 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700762 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700763 return OK;
764}
765
James Dongc32cd792010-04-26 17:48:26 -0700766void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
767 int32_t msgType, const sp<IMemory> &data) {
768 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700769 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800770 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
771 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700772 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700773 return;
774 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700775
James Dong98cfde02011-06-03 17:21:16 -0700776 if (mNumFramesReceived > 0) {
777 CHECK(timestampUs > mLastFrameTimestampUs);
778 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
779 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700780 }
James Dongf60cafe2010-06-19 09:04:18 -0700781 }
782
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700783 // May need to skip frame or modify timestamp. Currently implemented
784 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800785 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700786 releaseOneRecordingFrame(data);
787 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700788 }
789
James Dong365a9632010-06-04 13:59:27 -0700790 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700791 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700792 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700793 // Initial delay
794 if (mStartTimeUs > 0) {
795 if (timestampUs < mStartTimeUs) {
796 // Frame was captured before recording was started
797 // Drop it without updating the statistical data.
798 releaseOneRecordingFrame(data);
799 return;
800 }
801 mStartTimeUs = timestampUs - mStartTimeUs;
802 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700803 }
James Dong13aec892010-04-21 16:14:15 -0700804 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700805
James Dong98cfde02011-06-03 17:21:16 -0700806 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700807 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700808 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
809 mFrameTimes.push_back(timeUs);
810 LOGV("initial delay: %lld, current time stamp: %lld",
811 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700812 mFrameAvailableCondition.signal();
813}
814
James Dong5c952312010-10-18 21:42:27 -0700815bool CameraSource::isMetaDataStoredInVideoBuffers() const {
816 LOGV("isMetaDataStoredInVideoBuffers");
817 return mIsMetaDataStoredInVideoBuffers;
818}
819
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800820CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
821 mSource = source;
822}
823
824void CameraSource::ProxyListener::dataCallbackTimestamp(
825 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
826 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
827}
828
829void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
830 LOGI("Camera recording proxy died");
831}
832
Andreas Huber20111aa2009-07-14 16:56:47 -0700833} // namespace android