blob: de66d99b7bf2f5a17d11b44d64e26b35d54a1580 [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);
40 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr);
41
42 virtual void postDataTimestamp(
43 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
44
45protected:
46 virtual ~CameraSourceListener();
47
48private:
49 wp<CameraSource> mSource;
50
51 CameraSourceListener(const CameraSourceListener &);
52 CameraSourceListener &operator=(const CameraSourceListener &);
53};
54
55CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source)
56 : mSource(source) {
57}
58
59CameraSourceListener::~CameraSourceListener() {
60}
61
62void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
63 LOGV("notify(%d, %d, %d)", msgType, ext1, ext2);
64}
65
66void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr) {
67 LOGV("postData(%d, ptr:%p, size:%d)",
68 msgType, dataPtr->pointer(), dataPtr->size());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070069
70 sp<CameraSource> source = mSource.promote();
71 if (source.get() != NULL) {
72 source->dataCallback(msgType, dataPtr);
73 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070074}
75
76void CameraSourceListener::postDataTimestamp(
77 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
James Dongc32cd792010-04-26 17:48:26 -070078
79 sp<CameraSource> source = mSource.promote();
80 if (source.get() != NULL) {
81 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
82 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070083}
84
James Dong653252b2010-06-03 11:48:31 -070085static int32_t getColorFormat(const char* colorFormat) {
James Donge2d8ba82010-09-15 16:52:51 -070086 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
87 return OMX_COLOR_FormatYUV420Planar;
88 }
89
James Dong653252b2010-06-03 11:48:31 -070090 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) {
91 return OMX_COLOR_FormatYUV422SemiPlanar;
92 }
93
94 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
95 return OMX_COLOR_FormatYUV420SemiPlanar;
96 }
97
98 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) {
99 return OMX_COLOR_FormatYCbYCr;
100 }
101
102 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) {
103 return OMX_COLOR_Format16bitRGB565;
104 }
105
Dandawate Saket1374edd2011-07-11 19:12:57 -0700106 if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) {
107 return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
108 }
109
James Donga1abc1a2010-09-13 16:30:51 -0700110 LOGE("Uknown color format (%s), please add it to "
111 "CameraSource::getColorFormat", colorFormat);
112
James Dong653252b2010-06-03 11:48:31 -0700113 CHECK_EQ(0, "Unknown color format");
114}
115
Andreas Huber20111aa2009-07-14 16:56:47 -0700116CameraSource *CameraSource::Create() {
James Dong54ff19a2010-10-08 11:59:32 -0700117 Size size;
118 size.width = -1;
119 size.height = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700120
James Dong54ff19a2010-10-08 11:59:32 -0700121 sp<ICamera> camera;
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800122 return new CameraSource(camera, NULL, 0, size, -1, NULL, false);
Andreas Huber20111aa2009-07-14 16:56:47 -0700123}
124
Andreas Huber30ab6622009-11-16 15:43:38 -0800125// static
James Dong54ff19a2010-10-08 11:59:32 -0700126CameraSource *CameraSource::CreateFromCamera(
127 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800128 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700129 int32_t cameraId,
130 Size videoSize,
131 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700132 const sp<Surface>& surface,
133 bool storeMetaDataInVideoBuffers) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800134
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800135 CameraSource *source = new CameraSource(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700136 videoSize, frameRate, surface,
137 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700138 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800139}
140
James Dong54ff19a2010-10-08 11:59:32 -0700141CameraSource::CameraSource(
142 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800143 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700144 int32_t cameraId,
145 Size videoSize,
146 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700147 const sp<Surface>& surface,
148 bool storeMetaDataInVideoBuffers)
James Dong54ff19a2010-10-08 11:59:32 -0700149 : mCameraFlags(0),
150 mVideoFrameRate(-1),
151 mCamera(0),
152 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700153 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700154 mLastFrameTimestampUs(0),
155 mStarted(false),
James Dong13aec892010-04-21 16:14:15 -0700156 mNumFramesEncoded(0),
James Dong7757f502011-01-25 16:31:28 -0800157 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700158 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700159 mNumGlitches(0),
160 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700161 mCollectStats(false) {
James Dong54ff19a2010-10-08 11:59:32 -0700162 mVideoSize.width = -1;
163 mVideoSize.height = -1;
164
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800165 mInitCheck = init(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700166 videoSize, frameRate,
167 storeMetaDataInVideoBuffers);
Wu-cheng Li95068be2011-06-29 15:17:11 +0800168 if (mInitCheck != OK) releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700169}
170
171status_t CameraSource::initCheck() const {
172 return mInitCheck;
173}
174
175status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800176 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
177 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700178
179 if (camera == 0) {
180 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800181 if (mCamera == 0) return -EBUSY;
James Dong54ff19a2010-10-08 11:59:32 -0700182 mCameraFlags &= ~FLAGS_HOT_CAMERA;
183 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800184 // We get the proxy from Camera, not ICamera. We need to get the proxy
185 // to the remote Camera owned by the application. Here mCamera is a
186 // local Camera object created by us. We cannot use the proxy from
187 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700188 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800189 if (mCamera == 0) return -EBUSY;
190 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700191 mCameraFlags |= FLAGS_HOT_CAMERA;
James Dong3bd30202011-07-19 20:24:22 -0700192 mDeathNotifier = new DeathNotifier();
193 // isBinderAlive needs linkToDeath to work.
194 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
James Dong54ff19a2010-10-08 11:59:32 -0700195 }
196
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800197 mCamera->lock();
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800198
James Dong54ff19a2010-10-08 11:59:32 -0700199 return OK;
200}
201
202
203/*
204 * Check to see whether the requested video width and height is one
205 * of the supported sizes.
206 * @param width the video frame width in pixels
207 * @param height the video frame height in pixels
208 * @param suppportedSizes the vector of sizes that we check against
209 * @return true if the dimension (width and height) is supported.
210 */
211static bool isVideoSizeSupported(
212 int32_t width, int32_t height,
213 const Vector<Size>& supportedSizes) {
214
215 LOGV("isVideoSizeSupported");
216 for (size_t i = 0; i < supportedSizes.size(); ++i) {
217 if (width == supportedSizes[i].width &&
218 height == supportedSizes[i].height) {
219 return true;
220 }
221 }
222 return false;
223}
224
225/*
226 * If the preview and video output is separate, we only set the
227 * the video size, and applications should set the preview size
228 * to some proper value, and the recording framework will not
229 * change the preview size; otherwise, if the video and preview
230 * output is the same, we need to set the preview to be the same
231 * as the requested video size.
232 *
233 */
234/*
235 * Query the camera to retrieve the supported video frame sizes
236 * and also to see whether CameraParameters::setVideoSize()
237 * is supported or not.
238 * @param params CameraParameters to retrieve the information
239 * @@param isSetVideoSizeSupported retunrs whether method
240 * CameraParameters::setVideoSize() is supported or not.
241 * @param sizes returns the vector of Size objects for the
242 * supported video frame sizes advertised by the camera.
243 */
244static void getSupportedVideoSizes(
245 const CameraParameters& params,
246 bool *isSetVideoSizeSupported,
247 Vector<Size>& sizes) {
248
249 *isSetVideoSizeSupported = true;
250 params.getSupportedVideoSizes(sizes);
251 if (sizes.size() == 0) {
252 LOGD("Camera does not support setVideoSize()");
253 params.getSupportedPreviewSizes(sizes);
254 *isSetVideoSizeSupported = false;
255 }
256}
257
258/*
259 * Check whether the camera has the supported color format
260 * @param params CameraParameters to retrieve the information
261 * @return OK if no error.
262 */
263status_t CameraSource::isCameraColorFormatSupported(
264 const CameraParameters& params) {
265 mColorFormat = getColorFormat(params.get(
266 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
267 if (mColorFormat == -1) {
268 return BAD_VALUE;
269 }
270 return OK;
271}
272
273/*
274 * Configure the camera to use the requested video size
275 * (width and height) and/or frame rate. If both width and
276 * height are -1, configuration on the video size is skipped.
277 * if frameRate is -1, configuration on the frame rate
278 * is skipped. Skipping the configuration allows one to
279 * use the current camera setting without the need to
280 * actually know the specific values (see Create() method).
281 *
282 * @param params the CameraParameters to be configured
283 * @param width the target video frame width in pixels
284 * @param height the target video frame height in pixels
285 * @param frameRate the target frame rate in frames per second.
286 * @return OK if no error.
287 */
288status_t CameraSource::configureCamera(
289 CameraParameters* params,
290 int32_t width, int32_t height,
291 int32_t frameRate) {
James Dong3bd30202011-07-19 20:24:22 -0700292 LOGV("configureCamera");
James Dong54ff19a2010-10-08 11:59:32 -0700293 Vector<Size> sizes;
294 bool isSetVideoSizeSupportedByCamera = true;
295 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
296 bool isCameraParamChanged = false;
297 if (width != -1 && height != -1) {
298 if (!isVideoSizeSupported(width, height, sizes)) {
299 LOGE("Video dimension (%dx%d) is unsupported", width, height);
300 return BAD_VALUE;
301 }
302 if (isSetVideoSizeSupportedByCamera) {
303 params->setVideoSize(width, height);
304 } else {
305 params->setPreviewSize(width, height);
306 }
307 isCameraParamChanged = true;
308 } else if ((width == -1 && height != -1) ||
309 (width != -1 && height == -1)) {
310 // If one and only one of the width and height is -1
311 // we reject such a request.
312 LOGE("Requested video size (%dx%d) is not supported", width, height);
313 return BAD_VALUE;
314 } else { // width == -1 && height == -1
315 // Do not configure the camera.
316 // Use the current width and height value setting from the camera.
317 }
318
319 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700320 CHECK(frameRate > 0 && frameRate <= 120);
321 const char* supportedFrameRates =
322 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
323 CHECK(supportedFrameRates != NULL);
324 LOGV("Supported frame rates: %s", supportedFrameRates);
325 char buf[4];
326 snprintf(buf, 4, "%d", frameRate);
327 if (strstr(supportedFrameRates, buf) == NULL) {
328 LOGE("Requested frame rate (%d) is not supported: %s",
329 frameRate, supportedFrameRates);
330 return BAD_VALUE;
331 }
332
333 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700334 params->setPreviewFrameRate(frameRate);
335 isCameraParamChanged = true;
336 } else { // frameRate == -1
337 // Do not configure the camera.
338 // Use the current frame rate value setting from the camera
339 }
340
341 if (isCameraParamChanged) {
342 // Either frame rate or frame size needs to be changed.
343 String8 s = params->flatten();
344 if (OK != mCamera->setParameters(s)) {
345 LOGE("Could not change settings."
346 " Someone else is using camera %p?", mCamera.get());
347 return -EBUSY;
348 }
349 }
350 return OK;
351}
352
353/*
354 * Check whether the requested video frame size
355 * has been successfully configured or not. If both width and height
356 * are -1, check on the current width and height value setting
357 * is performed.
358 *
359 * @param params CameraParameters to retrieve the information
360 * @param the target video frame width in pixels to check against
361 * @param the target video frame height in pixels to check against
362 * @return OK if no error
363 */
364status_t CameraSource::checkVideoSize(
365 const CameraParameters& params,
366 int32_t width, int32_t height) {
367
James Dong3bd30202011-07-19 20:24:22 -0700368 LOGV("checkVideoSize");
James Dongf96c9d12010-10-20 11:41:33 -0700369 // The actual video size is the same as the preview size
370 // if the camera hal does not support separate video and
371 // preview output. In this case, we retrieve the video
372 // size from preview.
James Dong54ff19a2010-10-08 11:59:32 -0700373 int32_t frameWidthActual = -1;
374 int32_t frameHeightActual = -1;
James Dongf96c9d12010-10-20 11:41:33 -0700375 Vector<Size> sizes;
376 params.getSupportedVideoSizes(sizes);
377 if (sizes.size() == 0) {
378 // video size is the same as preview size
379 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
380 } else {
381 // video size may not be the same as preview
382 params.getVideoSize(&frameWidthActual, &frameHeightActual);
383 }
James Dong54ff19a2010-10-08 11:59:32 -0700384 if (frameWidthActual < 0 || frameHeightActual < 0) {
385 LOGE("Failed to retrieve video frame size (%dx%d)",
386 frameWidthActual, frameHeightActual);
387 return UNKNOWN_ERROR;
388 }
389
390 // Check the actual video frame size against the target/requested
391 // video frame size.
392 if (width != -1 && height != -1) {
393 if (frameWidthActual != width || frameHeightActual != height) {
394 LOGE("Failed to set video frame size to %dx%d. "
395 "The actual video size is %dx%d ", width, height,
396 frameWidthActual, frameHeightActual);
397 return UNKNOWN_ERROR;
398 }
399 }
400
401 // Good now.
402 mVideoSize.width = frameWidthActual;
403 mVideoSize.height = frameHeightActual;
404 return OK;
405}
406
407/*
408 * Check the requested frame rate has been successfully configured or not.
409 * If the target frameRate is -1, check on the current frame rate value
410 * setting is performed.
411 *
412 * @param params CameraParameters to retrieve the information
413 * @param the target video frame rate to check against
414 * @return OK if no error.
415 */
416status_t CameraSource::checkFrameRate(
417 const CameraParameters& params,
418 int32_t frameRate) {
419
James Dong3bd30202011-07-19 20:24:22 -0700420 LOGV("checkFrameRate");
James Dong54ff19a2010-10-08 11:59:32 -0700421 int32_t frameRateActual = params.getPreviewFrameRate();
422 if (frameRateActual < 0) {
423 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
424 return UNKNOWN_ERROR;
425 }
426
427 // Check the actual video frame rate against the target/requested
428 // video frame rate.
429 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
430 LOGE("Failed to set preview frame rate to %d fps. The actual "
431 "frame rate is %d", frameRate, frameRateActual);
432 return UNKNOWN_ERROR;
433 }
434
435 // Good now.
436 mVideoFrameRate = frameRateActual;
437 return OK;
438}
439
440/*
441 * Initialize the CameraSource to so that it becomes
442 * ready for providing the video input streams as requested.
443 * @param camera the camera object used for the video source
444 * @param cameraId if camera == 0, use camera with this id
445 * as the video source
446 * @param videoSize the target video frame size. If both
447 * width and height in videoSize is -1, use the current
448 * width and heigth settings by the camera
449 * @param frameRate the target frame rate in frames per second.
450 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700451 * @param storeMetaDataInVideoBuffers request to store meta
452 * data or real YUV data in video buffers. Request to
453 * store meta data in video buffers may not be honored
454 * if the source does not support this feature.
455 *
James Dong54ff19a2010-10-08 11:59:32 -0700456 * @return OK if no error.
457 */
458status_t CameraSource::init(
459 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800460 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700461 int32_t cameraId,
462 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700463 int32_t frameRate,
464 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700465
James Dong3bd30202011-07-19 20:24:22 -0700466 LOGV("init");
James Dong54ff19a2010-10-08 11:59:32 -0700467 status_t err = OK;
James Dongae4c1ac2011-07-08 17:59:29 -0700468 int64_t token = IPCThreadState::self()->clearCallingIdentity();
469 err = initWithCameraAccess(camera, proxy, cameraId,
470 videoSize, frameRate,
471 storeMetaDataInVideoBuffers);
472 IPCThreadState::self()->restoreCallingIdentity(token);
473 return err;
474}
475
476status_t CameraSource::initWithCameraAccess(
477 const sp<ICamera>& camera,
478 const sp<ICameraRecordingProxy>& proxy,
479 int32_t cameraId,
480 Size videoSize,
481 int32_t frameRate,
482 bool storeMetaDataInVideoBuffers) {
James Dong3bd30202011-07-19 20:24:22 -0700483 LOGV("initWithCameraAccess");
James Dongae4c1ac2011-07-08 17:59:29 -0700484 status_t err = OK;
James Dong54ff19a2010-10-08 11:59:32 -0700485
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800486 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
487 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700488 return err;
489 }
490 CameraParameters params(mCamera->getParameters());
491 if ((err = isCameraColorFormatSupported(params)) != OK) {
492 return err;
493 }
494
495 // Set the camera to use the requested video frame size
496 // and/or frame rate.
497 if ((err = configureCamera(&params,
498 videoSize.width, videoSize.height,
499 frameRate))) {
500 return err;
501 }
502
503 // Check on video frame size and frame rate.
504 CameraParameters newCameraParams(mCamera->getParameters());
505 if ((err = checkVideoSize(newCameraParams,
506 videoSize.width, videoSize.height)) != OK) {
507 return err;
508 }
509 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
510 return err;
511 }
512
513 // This CHECK is good, since we just passed the lock/unlock
514 // check earlier by calling mCamera->setParameters().
515 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700516
James Dongabdd2ba2010-12-10 13:09:05 -0800517 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700518 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800519 mCamera->storeMetaDataInBuffers(false);
520 if (storeMetaDataInVideoBuffers) {
521 if (OK == mCamera->storeMetaDataInBuffers(true)) {
522 mIsMetaDataStoredInVideoBuffers = true;
523 }
James Dong5c952312010-10-18 21:42:27 -0700524 }
525
James Dong54ff19a2010-10-08 11:59:32 -0700526 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700527 if (glitchDurationUs > mGlitchDurationThresholdUs) {
528 mGlitchDurationThresholdUs = glitchDurationUs;
529 }
530
James Dongddcc4a62010-06-08 11:58:53 -0700531 // XXX: query camera for the stride and slice height
532 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700533 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700534 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
535 mMeta->setInt32(kKeyColorFormat, mColorFormat);
536 mMeta->setInt32(kKeyWidth, mVideoSize.width);
537 mMeta->setInt32(kKeyHeight, mVideoSize.height);
538 mMeta->setInt32(kKeyStride, mVideoSize.width);
539 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800540 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700541 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700542}
543
544CameraSource::~CameraSource() {
545 if (mStarted) {
546 stop();
James Dongae4c1ac2011-07-08 17:59:29 -0700547 } else if (mInitCheck == OK) {
548 // Camera is initialized but because start() is never called,
549 // the lock on Camera is never released(). This makes sure
550 // Camera's lock is released in this case.
551 releaseCamera();
Andreas Huber20111aa2009-07-14 16:56:47 -0700552 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700553}
554
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700555void CameraSource::startCameraRecording() {
James Dong3bd30202011-07-19 20:24:22 -0700556 LOGV("startCameraRecording");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800557 // Reset the identity to the current thread because media server owns the
558 // camera and recording is started by the applications. The applications
559 // will connect to the camera in ICameraRecordingProxy::startRecording.
560 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong3bd30202011-07-19 20:24:22 -0700561 if (mCameraFlags & FLAGS_HOT_CAMERA) {
562 mCamera->unlock();
563 mCamera.clear();
564 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
565 } else {
566 mCamera->setListener(new CameraSourceListener(this));
567 mCamera->startRecording();
568 CHECK(mCamera->recordingEnabled());
569 }
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800570 IPCThreadState::self()->restoreCallingIdentity(token);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700571}
572
James Dongf60cafe2010-06-19 09:04:18 -0700573status_t CameraSource::start(MetaData *meta) {
James Dong3bd30202011-07-19 20:24:22 -0700574 LOGV("start");
Andreas Huber0c891992009-08-26 14:48:20 -0700575 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700576 if (mInitCheck != OK) {
577 LOGE("CameraSource is not initialized yet");
578 return mInitCheck;
579 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700580
James Dong365a9632010-06-04 13:59:27 -0700581 char value[PROPERTY_VALUE_MAX];
582 if (property_get("media.stagefright.record-stats", value, NULL)
583 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
584 mCollectStats = true;
585 }
James Dong9d7f58a2010-06-09 15:57:48 -0700586
James Dongf60cafe2010-06-19 09:04:18 -0700587 mStartTimeUs = 0;
588 int64_t startTimeUs;
589 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
590 mStartTimeUs = startTimeUs;
591 }
592
James Dongc42478e2010-11-15 10:38:37 -0800593 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700594
595 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700596 return OK;
597}
598
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700599void CameraSource::stopCameraRecording() {
James Dong3bd30202011-07-19 20:24:22 -0700600 LOGV("stopCameraRecording");
601 if (mCameraFlags & FLAGS_HOT_CAMERA) {
602 mCameraRecordingProxy->stopRecording();
603 } else {
604 mCamera->setListener(NULL);
605 mCamera->stopRecording();
606 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700607}
608
James Dongea7b4852010-12-05 14:25:34 -0800609void CameraSource::releaseCamera() {
610 LOGV("releaseCamera");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800611 if (mCamera != 0) {
James Dongae4c1ac2011-07-08 17:59:29 -0700612 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800613 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
614 LOGV("Camera was cold when we started, stopping preview");
615 mCamera->stopPreview();
616 mCamera->disconnect();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800617 }
James Dong3bd30202011-07-19 20:24:22 -0700618 mCamera->unlock();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800619 mCamera.clear();
James Dong3bd30202011-07-19 20:24:22 -0700620 mCamera = 0;
James Dongae4c1ac2011-07-08 17:59:29 -0700621 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongea7b4852010-12-05 14:25:34 -0800622 }
Wu-cheng Li95068be2011-06-29 15:17:11 +0800623 if (mCameraRecordingProxy != 0) {
624 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
625 mCameraRecordingProxy.clear();
626 }
James Dongea7b4852010-12-05 14:25:34 -0800627 mCameraFlags = 0;
628}
629
Andreas Huber20111aa2009-07-14 16:56:47 -0700630status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800631 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700632 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700633 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700634 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700635
James Dongc32cd792010-04-26 17:48:26 -0700636 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700637 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800638 if (NO_ERROR !=
639 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
640 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700641 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800642 }
James Dong7278cf32010-05-27 16:05:58 -0700643 }
James Dongd69c7f62010-12-09 11:24:22 -0800644 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800645 releaseCamera();
James Dong7278cf32010-05-27 16:05:58 -0700646
James Dong365a9632010-06-04 13:59:27 -0700647 if (mCollectStats) {
648 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
649 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
650 mLastFrameTimestampUs - mFirstFrameTimeUs);
651 }
James Dong13aec892010-04-21 16:14:15 -0700652
James Dongba290022010-12-09 15:04:33 -0800653 if (mNumGlitches > 0) {
James Donga4726132011-02-16 12:28:26 -0800654 LOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800655 }
656
James Dong13aec892010-04-21 16:14:15 -0700657 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800658 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700659 return OK;
660}
661
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700662void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
James Dong3bd30202011-07-19 20:24:22 -0700663 LOGV("releaseRecordingFrame");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800664 if (mCameraRecordingProxy != NULL) {
665 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dong3bd30202011-07-19 20:24:22 -0700666 } else {
667 int64_t token = IPCThreadState::self()->clearCallingIdentity();
668 mCamera->releaseRecordingFrame(frame);
669 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongd69c7f62010-12-09 11:24:22 -0800670 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700671}
672
James Dongc32cd792010-04-26 17:48:26 -0700673void CameraSource::releaseQueuedFrames() {
674 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700675 while (!mFramesReceived.empty()) {
676 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700677 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700678 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700679 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700680 }
681}
682
Andreas Huber20111aa2009-07-14 16:56:47 -0700683sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700684 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700685}
686
James Dongf60cafe2010-06-19 09:04:18 -0700687void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700688 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700689}
690
James Dong7278cf32010-05-27 16:05:58 -0700691void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
692 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700693 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700694 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
695 it != mFramesBeingEncoded.end(); ++it) {
696 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700697 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700698 mFramesBeingEncoded.erase(it);
699 ++mNumFramesEncoded;
700 buffer->setObserver(0);
701 buffer->release();
702 mFrameCompleteCondition.signal();
703 return;
704 }
705 }
706 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
707}
708
Andreas Huber20111aa2009-07-14 16:56:47 -0700709status_t CameraSource::read(
710 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700711 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700712
713 *buffer = NULL;
714
715 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700716 ReadOptions::SeekMode mode;
717 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700718 return ERROR_UNSUPPORTED;
719 }
720
721 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700722 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700723
724 {
725 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800726 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800727 if (NO_ERROR !=
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800728 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) {
James Dong3bd30202011-07-19 20:24:22 -0700729 if (mCameraRecordingProxy != 0 &&
730 !mCameraRecordingProxy->asBinder()->isBinderAlive()) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800731 LOGW("camera recording proxy is gone");
732 return ERROR_END_OF_STREAM;
733 }
James Dong41152ef2010-12-20 21:29:12 -0800734 LOGW("Timed out waiting for incoming camera video frames: %lld us",
735 mLastFrameTimestampUs);
736 }
James Dong542db5d2010-07-21 14:51:35 -0700737 }
James Dong79e23b42010-12-11 10:43:41 -0800738 if (!mStarted) {
739 return OK;
740 }
741 frame = *mFramesReceived.begin();
742 mFramesReceived.erase(mFramesReceived.begin());
743
744 frameTime = *mFrameTimes.begin();
745 mFrameTimes.erase(mFrameTimes.begin());
746 mFramesBeingEncoded.push_back(frame);
747 *buffer = new MediaBuffer(frame->pointer(), frame->size());
748 (*buffer)->setObserver(this);
749 (*buffer)->add_ref();
750 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700751 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700752 return OK;
753}
754
James Dongc32cd792010-04-26 17:48:26 -0700755void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
756 int32_t msgType, const sp<IMemory> &data) {
757 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700758 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800759 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
760 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700761 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700762 return;
763 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700764
James Dong98cfde02011-06-03 17:21:16 -0700765 if (mNumFramesReceived > 0) {
766 CHECK(timestampUs > mLastFrameTimestampUs);
767 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
768 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700769 }
James Dongf60cafe2010-06-19 09:04:18 -0700770 }
771
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700772 // May need to skip frame or modify timestamp. Currently implemented
773 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800774 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700775 releaseOneRecordingFrame(data);
776 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700777 }
778
James Dong365a9632010-06-04 13:59:27 -0700779 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700780 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700781 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700782 // Initial delay
783 if (mStartTimeUs > 0) {
784 if (timestampUs < mStartTimeUs) {
785 // Frame was captured before recording was started
786 // Drop it without updating the statistical data.
787 releaseOneRecordingFrame(data);
788 return;
789 }
790 mStartTimeUs = timestampUs - mStartTimeUs;
791 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700792 }
James Dong13aec892010-04-21 16:14:15 -0700793 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700794
James Dong98cfde02011-06-03 17:21:16 -0700795 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700796 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700797 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
798 mFrameTimes.push_back(timeUs);
799 LOGV("initial delay: %lld, current time stamp: %lld",
800 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700801 mFrameAvailableCondition.signal();
802}
803
James Dong5c952312010-10-18 21:42:27 -0700804bool CameraSource::isMetaDataStoredInVideoBuffers() const {
805 LOGV("isMetaDataStoredInVideoBuffers");
806 return mIsMetaDataStoredInVideoBuffers;
807}
808
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800809CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
810 mSource = source;
811}
812
813void CameraSource::ProxyListener::dataCallbackTimestamp(
814 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
815 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
816}
817
818void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
819 LOGI("Camera recording proxy died");
820}
821
Andreas Huber20111aa2009-07-14 16:56:47 -0700822} // namespace android