blob: 371c21f098327ba1b1bb52624d8679f4c829eadf [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
James Donga1abc1a2010-09-13 16:30:51 -0700106 LOGE("Uknown color format (%s), please add it to "
107 "CameraSource::getColorFormat", colorFormat);
108
James Dong653252b2010-06-03 11:48:31 -0700109 CHECK_EQ(0, "Unknown color format");
110}
111
Andreas Huber20111aa2009-07-14 16:56:47 -0700112CameraSource *CameraSource::Create() {
James Dong54ff19a2010-10-08 11:59:32 -0700113 Size size;
114 size.width = -1;
115 size.height = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700116
James Dong54ff19a2010-10-08 11:59:32 -0700117 sp<ICamera> camera;
James Dong5c952312010-10-18 21:42:27 -0700118 return new CameraSource(camera, 0, size, -1, NULL, false);
Andreas Huber20111aa2009-07-14 16:56:47 -0700119}
120
Andreas Huber30ab6622009-11-16 15:43:38 -0800121// static
James Dong54ff19a2010-10-08 11:59:32 -0700122CameraSource *CameraSource::CreateFromCamera(
123 const sp<ICamera>& camera,
124 int32_t cameraId,
125 Size videoSize,
126 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700127 const sp<Surface>& surface,
128 bool storeMetaDataInVideoBuffers) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800129
James Dong54ff19a2010-10-08 11:59:32 -0700130 CameraSource *source = new CameraSource(camera, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700131 videoSize, frameRate, surface,
132 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700133 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800134}
135
James Dong54ff19a2010-10-08 11:59:32 -0700136CameraSource::CameraSource(
137 const sp<ICamera>& camera,
138 int32_t cameraId,
139 Size videoSize,
140 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700141 const sp<Surface>& surface,
142 bool storeMetaDataInVideoBuffers)
James Dong54ff19a2010-10-08 11:59:32 -0700143 : mCameraFlags(0),
144 mVideoFrameRate(-1),
145 mCamera(0),
146 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700147 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700148 mLastFrameTimestampUs(0),
149 mStarted(false),
150 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700151 mNumFramesEncoded(0),
152 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700153 mNumGlitches(0),
154 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700155 mCollectStats(false) {
James Dong9d7f58a2010-06-09 15:57:48 -0700156
James Dong54ff19a2010-10-08 11:59:32 -0700157 mVideoSize.width = -1;
158 mVideoSize.height = -1;
159
James Dong5c952312010-10-18 21:42:27 -0700160 mInitCheck = init(camera, cameraId,
161 videoSize, frameRate,
162 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700163}
164
165status_t CameraSource::initCheck() const {
166 return mInitCheck;
167}
168
169status_t CameraSource::isCameraAvailable(
170 const sp<ICamera>& camera, int32_t cameraId) {
171
172 if (camera == 0) {
173 mCamera = Camera::connect(cameraId);
174 mCameraFlags &= ~FLAGS_HOT_CAMERA;
175 } else {
176 mCamera = Camera::create(camera);
177 mCameraFlags |= FLAGS_HOT_CAMERA;
178 }
179
180 // Is camera available?
181 if (mCamera == 0) {
182 LOGE("Camera connection could not be established.");
183 return -EBUSY;
184 }
185 if (!(mCameraFlags & FLAGS_HOT_CAMERA)) {
186 mCamera->lock();
187 }
188 return OK;
189}
190
191
192/*
193 * Check to see whether the requested video width and height is one
194 * of the supported sizes.
195 * @param width the video frame width in pixels
196 * @param height the video frame height in pixels
197 * @param suppportedSizes the vector of sizes that we check against
198 * @return true if the dimension (width and height) is supported.
199 */
200static bool isVideoSizeSupported(
201 int32_t width, int32_t height,
202 const Vector<Size>& supportedSizes) {
203
204 LOGV("isVideoSizeSupported");
205 for (size_t i = 0; i < supportedSizes.size(); ++i) {
206 if (width == supportedSizes[i].width &&
207 height == supportedSizes[i].height) {
208 return true;
209 }
210 }
211 return false;
212}
213
214/*
215 * If the preview and video output is separate, we only set the
216 * the video size, and applications should set the preview size
217 * to some proper value, and the recording framework will not
218 * change the preview size; otherwise, if the video and preview
219 * output is the same, we need to set the preview to be the same
220 * as the requested video size.
221 *
222 */
223/*
224 * Query the camera to retrieve the supported video frame sizes
225 * and also to see whether CameraParameters::setVideoSize()
226 * is supported or not.
227 * @param params CameraParameters to retrieve the information
228 * @@param isSetVideoSizeSupported retunrs whether method
229 * CameraParameters::setVideoSize() is supported or not.
230 * @param sizes returns the vector of Size objects for the
231 * supported video frame sizes advertised by the camera.
232 */
233static void getSupportedVideoSizes(
234 const CameraParameters& params,
235 bool *isSetVideoSizeSupported,
236 Vector<Size>& sizes) {
237
238 *isSetVideoSizeSupported = true;
239 params.getSupportedVideoSizes(sizes);
240 if (sizes.size() == 0) {
241 LOGD("Camera does not support setVideoSize()");
242 params.getSupportedPreviewSizes(sizes);
243 *isSetVideoSizeSupported = false;
244 }
245}
246
247/*
248 * Check whether the camera has the supported color format
249 * @param params CameraParameters to retrieve the information
250 * @return OK if no error.
251 */
252status_t CameraSource::isCameraColorFormatSupported(
253 const CameraParameters& params) {
254 mColorFormat = getColorFormat(params.get(
255 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
256 if (mColorFormat == -1) {
257 return BAD_VALUE;
258 }
259 return OK;
260}
261
262/*
263 * Configure the camera to use the requested video size
264 * (width and height) and/or frame rate. If both width and
265 * height are -1, configuration on the video size is skipped.
266 * if frameRate is -1, configuration on the frame rate
267 * is skipped. Skipping the configuration allows one to
268 * use the current camera setting without the need to
269 * actually know the specific values (see Create() method).
270 *
271 * @param params the CameraParameters to be configured
272 * @param width the target video frame width in pixels
273 * @param height the target video frame height in pixels
274 * @param frameRate the target frame rate in frames per second.
275 * @return OK if no error.
276 */
277status_t CameraSource::configureCamera(
278 CameraParameters* params,
279 int32_t width, int32_t height,
280 int32_t frameRate) {
281
282 Vector<Size> sizes;
283 bool isSetVideoSizeSupportedByCamera = true;
284 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
285 bool isCameraParamChanged = false;
286 if (width != -1 && height != -1) {
287 if (!isVideoSizeSupported(width, height, sizes)) {
288 LOGE("Video dimension (%dx%d) is unsupported", width, height);
James Dongea7b4852010-12-05 14:25:34 -0800289 releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700290 return BAD_VALUE;
291 }
292 if (isSetVideoSizeSupportedByCamera) {
293 params->setVideoSize(width, height);
294 } else {
295 params->setPreviewSize(width, height);
296 }
297 isCameraParamChanged = true;
298 } else if ((width == -1 && height != -1) ||
299 (width != -1 && height == -1)) {
300 // If one and only one of the width and height is -1
301 // we reject such a request.
302 LOGE("Requested video size (%dx%d) is not supported", width, height);
James Dongea7b4852010-12-05 14:25:34 -0800303 releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700304 return BAD_VALUE;
305 } else { // width == -1 && height == -1
306 // Do not configure the camera.
307 // Use the current width and height value setting from the camera.
308 }
309
310 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700311 CHECK(frameRate > 0 && frameRate <= 120);
312 const char* supportedFrameRates =
313 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
314 CHECK(supportedFrameRates != NULL);
315 LOGV("Supported frame rates: %s", supportedFrameRates);
316 char buf[4];
317 snprintf(buf, 4, "%d", frameRate);
318 if (strstr(supportedFrameRates, buf) == NULL) {
319 LOGE("Requested frame rate (%d) is not supported: %s",
320 frameRate, supportedFrameRates);
James Dongea7b4852010-12-05 14:25:34 -0800321 releaseCamera();
James Dong63573082010-10-24 09:32:39 -0700322 return BAD_VALUE;
323 }
324
325 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700326 params->setPreviewFrameRate(frameRate);
327 isCameraParamChanged = true;
328 } else { // frameRate == -1
329 // Do not configure the camera.
330 // Use the current frame rate value setting from the camera
331 }
332
333 if (isCameraParamChanged) {
334 // Either frame rate or frame size needs to be changed.
335 String8 s = params->flatten();
336 if (OK != mCamera->setParameters(s)) {
337 LOGE("Could not change settings."
338 " Someone else is using camera %p?", mCamera.get());
339 return -EBUSY;
340 }
341 }
342 return OK;
343}
344
345/*
346 * Check whether the requested video frame size
347 * has been successfully configured or not. If both width and height
348 * are -1, check on the current width and height value setting
349 * is performed.
350 *
351 * @param params CameraParameters to retrieve the information
352 * @param the target video frame width in pixels to check against
353 * @param the target video frame height in pixels to check against
354 * @return OK if no error
355 */
356status_t CameraSource::checkVideoSize(
357 const CameraParameters& params,
358 int32_t width, int32_t height) {
359
James Dongf96c9d12010-10-20 11:41:33 -0700360 // The actual video size is the same as the preview size
361 // if the camera hal does not support separate video and
362 // preview output. In this case, we retrieve the video
363 // size from preview.
James Dong54ff19a2010-10-08 11:59:32 -0700364 int32_t frameWidthActual = -1;
365 int32_t frameHeightActual = -1;
James Dongf96c9d12010-10-20 11:41:33 -0700366 Vector<Size> sizes;
367 params.getSupportedVideoSizes(sizes);
368 if (sizes.size() == 0) {
369 // video size is the same as preview size
370 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
371 } else {
372 // video size may not be the same as preview
373 params.getVideoSize(&frameWidthActual, &frameHeightActual);
374 }
James Dong54ff19a2010-10-08 11:59:32 -0700375 if (frameWidthActual < 0 || frameHeightActual < 0) {
376 LOGE("Failed to retrieve video frame size (%dx%d)",
377 frameWidthActual, frameHeightActual);
378 return UNKNOWN_ERROR;
379 }
380
381 // Check the actual video frame size against the target/requested
382 // video frame size.
383 if (width != -1 && height != -1) {
384 if (frameWidthActual != width || frameHeightActual != height) {
385 LOGE("Failed to set video frame size to %dx%d. "
386 "The actual video size is %dx%d ", width, height,
387 frameWidthActual, frameHeightActual);
388 return UNKNOWN_ERROR;
389 }
390 }
391
392 // Good now.
393 mVideoSize.width = frameWidthActual;
394 mVideoSize.height = frameHeightActual;
395 return OK;
396}
397
398/*
399 * Check the requested frame rate has been successfully configured or not.
400 * If the target frameRate is -1, check on the current frame rate value
401 * setting is performed.
402 *
403 * @param params CameraParameters to retrieve the information
404 * @param the target video frame rate to check against
405 * @return OK if no error.
406 */
407status_t CameraSource::checkFrameRate(
408 const CameraParameters& params,
409 int32_t frameRate) {
410
411 int32_t frameRateActual = params.getPreviewFrameRate();
412 if (frameRateActual < 0) {
413 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
414 return UNKNOWN_ERROR;
415 }
416
417 // Check the actual video frame rate against the target/requested
418 // video frame rate.
419 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
420 LOGE("Failed to set preview frame rate to %d fps. The actual "
421 "frame rate is %d", frameRate, frameRateActual);
422 return UNKNOWN_ERROR;
423 }
424
425 // Good now.
426 mVideoFrameRate = frameRateActual;
427 return OK;
428}
429
430/*
431 * Initialize the CameraSource to so that it becomes
432 * ready for providing the video input streams as requested.
433 * @param camera the camera object used for the video source
434 * @param cameraId if camera == 0, use camera with this id
435 * as the video source
436 * @param videoSize the target video frame size. If both
437 * width and height in videoSize is -1, use the current
438 * width and heigth settings by the camera
439 * @param frameRate the target frame rate in frames per second.
440 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700441 * @param storeMetaDataInVideoBuffers request to store meta
442 * data or real YUV data in video buffers. Request to
443 * store meta data in video buffers may not be honored
444 * if the source does not support this feature.
445 *
James Dong54ff19a2010-10-08 11:59:32 -0700446 * @return OK if no error.
447 */
448status_t CameraSource::init(
449 const sp<ICamera>& camera,
450 int32_t cameraId,
451 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700452 int32_t frameRate,
453 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700454
455 status_t err = OK;
James Dong9d7f58a2010-06-09 15:57:48 -0700456 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong54ff19a2010-10-08 11:59:32 -0700457
458 if ((err = isCameraAvailable(camera, cameraId)) != OK) {
459 return err;
460 }
461 CameraParameters params(mCamera->getParameters());
462 if ((err = isCameraColorFormatSupported(params)) != OK) {
463 return err;
464 }
465
466 // Set the camera to use the requested video frame size
467 // and/or frame rate.
468 if ((err = configureCamera(&params,
469 videoSize.width, videoSize.height,
470 frameRate))) {
471 return err;
472 }
473
474 // Check on video frame size and frame rate.
475 CameraParameters newCameraParams(mCamera->getParameters());
476 if ((err = checkVideoSize(newCameraParams,
477 videoSize.width, videoSize.height)) != OK) {
478 return err;
479 }
480 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
481 return err;
482 }
483
484 // This CHECK is good, since we just passed the lock/unlock
485 // check earlier by calling mCamera->setParameters().
486 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700487
James Dong5c952312010-10-18 21:42:27 -0700488 mIsMetaDataStoredInVideoBuffers = false;
489 if (storeMetaDataInVideoBuffers &&
490 OK == mCamera->storeMetaDataInBuffers(true)) {
491 mIsMetaDataStoredInVideoBuffers = true;
492 }
493
James Dong9d7f58a2010-06-09 15:57:48 -0700494 IPCThreadState::self()->restoreCallingIdentity(token);
495
James Dong54ff19a2010-10-08 11:59:32 -0700496 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700497 if (glitchDurationUs > mGlitchDurationThresholdUs) {
498 mGlitchDurationThresholdUs = glitchDurationUs;
499 }
500
James Dongddcc4a62010-06-08 11:58:53 -0700501 // XXX: query camera for the stride and slice height
502 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700503 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700504 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
505 mMeta->setInt32(kKeyColorFormat, mColorFormat);
506 mMeta->setInt32(kKeyWidth, mVideoSize.width);
507 mMeta->setInt32(kKeyHeight, mVideoSize.height);
508 mMeta->setInt32(kKeyStride, mVideoSize.width);
509 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800510 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700511 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700512}
513
514CameraSource::~CameraSource() {
515 if (mStarted) {
516 stop();
517 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700518}
519
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700520void CameraSource::startCameraRecording() {
521 CHECK_EQ(OK, mCamera->startRecording());
James Dong2b37ced2010-10-09 01:16:58 -0700522 CHECK(mCamera->recordingEnabled());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700523}
524
James Dongf60cafe2010-06-19 09:04:18 -0700525status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700526 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700527 if (mInitCheck != OK) {
528 LOGE("CameraSource is not initialized yet");
529 return mInitCheck;
530 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700531
James Dong365a9632010-06-04 13:59:27 -0700532 char value[PROPERTY_VALUE_MAX];
533 if (property_get("media.stagefright.record-stats", value, NULL)
534 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
535 mCollectStats = true;
536 }
James Dong9d7f58a2010-06-09 15:57:48 -0700537
James Dongf60cafe2010-06-19 09:04:18 -0700538 mStartTimeUs = 0;
539 int64_t startTimeUs;
540 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
541 mStartTimeUs = startTimeUs;
542 }
543
James Dongc42478e2010-11-15 10:38:37 -0800544 // Call setListener first before calling startCameraRecording()
545 // to avoid recording frames being dropped.
James Dong9d7f58a2010-06-09 15:57:48 -0700546 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700547 mCamera->setListener(new CameraSourceListener(this));
James Dongc42478e2010-11-15 10:38:37 -0800548 startCameraRecording();
James Dong9d7f58a2010-06-09 15:57:48 -0700549 IPCThreadState::self()->restoreCallingIdentity(token);
Andreas Huber20111aa2009-07-14 16:56:47 -0700550
551 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700552 return OK;
553}
554
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700555void CameraSource::stopCameraRecording() {
Nipun Kwatra78eff722010-09-15 15:08:49 -0700556 mCamera->setListener(NULL);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700557 mCamera->stopRecording();
558}
559
James Dongea7b4852010-12-05 14:25:34 -0800560void CameraSource::releaseCamera() {
561 LOGV("releaseCamera");
562 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
563 LOGV("Camera was cold when we started, stopping preview");
564 mCamera->stopPreview();
565 }
566 mCamera->unlock();
567 mCamera.clear();
568 mCamera = 0;
569 mCameraFlags = 0;
570}
571
Andreas Huber20111aa2009-07-14 16:56:47 -0700572status_t CameraSource::stop() {
James Dongc32cd792010-04-26 17:48:26 -0700573 LOGV("stop");
574 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700575 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700576 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700577
James Dong9d7f58a2010-06-09 15:57:48 -0700578 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700579 stopCameraRecording();
James Dongc32cd792010-04-26 17:48:26 -0700580 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700581 while (!mFramesBeingEncoded.empty()) {
James Dong365a9632010-06-04 13:59:27 -0700582 LOGI("Waiting for outstanding frames being encoded: %d",
583 mFramesBeingEncoded.size());
James Dong7278cf32010-05-27 16:05:58 -0700584 mFrameCompleteCondition.wait(mLock);
585 }
James Dongea7b4852010-12-05 14:25:34 -0800586 releaseCamera();
James Dong9d7f58a2010-06-09 15:57:48 -0700587 IPCThreadState::self()->restoreCallingIdentity(token);
James Dong7278cf32010-05-27 16:05:58 -0700588
James Dong365a9632010-06-04 13:59:27 -0700589 if (mCollectStats) {
590 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
591 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
592 mLastFrameTimestampUs - mFirstFrameTimeUs);
593 }
James Dong13aec892010-04-21 16:14:15 -0700594
James Dongba290022010-12-09 15:04:33 -0800595 if (mNumGlitches > 0) {
596 LOGW("%d long delays between neighboring video frames during",
597 mNumGlitches);
598 }
599
James Dong13aec892010-04-21 16:14:15 -0700600 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
Andreas Huber20111aa2009-07-14 16:56:47 -0700601 return OK;
602}
603
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700604void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
605 mCamera->releaseRecordingFrame(frame);
606}
607
James Dongc32cd792010-04-26 17:48:26 -0700608void CameraSource::releaseQueuedFrames() {
609 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700610 while (!mFramesReceived.empty()) {
611 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700612 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700613 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700614 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700615 }
616}
617
Andreas Huber20111aa2009-07-14 16:56:47 -0700618sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700619 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700620}
621
James Dongf60cafe2010-06-19 09:04:18 -0700622void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
623 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700624 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700625 IPCThreadState::self()->restoreCallingIdentity(token);
626}
627
James Dong7278cf32010-05-27 16:05:58 -0700628void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
629 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700630 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700631 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
632 it != mFramesBeingEncoded.end(); ++it) {
633 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700634 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700635 mFramesBeingEncoded.erase(it);
636 ++mNumFramesEncoded;
637 buffer->setObserver(0);
638 buffer->release();
639 mFrameCompleteCondition.signal();
640 return;
641 }
642 }
643 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
644}
645
Andreas Huber20111aa2009-07-14 16:56:47 -0700646status_t CameraSource::read(
647 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700648 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700649
650 *buffer = NULL;
651
652 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700653 ReadOptions::SeekMode mode;
654 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700655 return ERROR_UNSUPPORTED;
656 }
657
658 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700659 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700660
661 {
662 Mutex::Autolock autoLock(mLock);
James Dong542db5d2010-07-21 14:51:35 -0700663 while (mStarted) {
664 while(mFramesReceived.empty()) {
665 mFrameAvailableCondition.wait(mLock);
666 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700667
James Dong542db5d2010-07-21 14:51:35 -0700668 if (!mStarted) {
669 return OK;
670 }
James Dong7278cf32010-05-27 16:05:58 -0700671
James Dong542db5d2010-07-21 14:51:35 -0700672 frame = *mFramesReceived.begin();
673 mFramesReceived.erase(mFramesReceived.begin());
674
675 frameTime = *mFrameTimes.begin();
676 mFrameTimes.erase(mFrameTimes.begin());
677 int64_t skipTimeUs;
678 if (!options || !options->getSkipFrame(&skipTimeUs)) {
679 skipTimeUs = frameTime;
680 }
681 if (skipTimeUs > frameTime) {
682 LOGV("skipTimeUs: %lld us > frameTime: %lld us",
683 skipTimeUs, frameTime);
684 releaseOneRecordingFrame(frame);
685 ++mNumFramesDropped;
686 // Safeguard against the abuse of the kSkipFrame_Option.
687 if (skipTimeUs - frameTime >= 1E6) {
688 LOGE("Frame skipping requested is way too long: %lld us",
689 skipTimeUs - frameTime);
690 return UNKNOWN_ERROR;
691 }
692 } else {
693 mFramesBeingEncoded.push_back(frame);
694 *buffer = new MediaBuffer(frame->pointer(), frame->size());
695 (*buffer)->setObserver(this);
696 (*buffer)->add_ref();
697 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber56223b92010-08-11 12:34:32 -0700698
James Dong542db5d2010-07-21 14:51:35 -0700699 return OK;
700 }
701 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700702 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700703 return OK;
704}
705
James Dongc32cd792010-04-26 17:48:26 -0700706void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
707 int32_t msgType, const sp<IMemory> &data) {
708 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700709 Mutex::Autolock autoLock(mLock);
James Dongc32cd792010-04-26 17:48:26 -0700710 if (!mStarted) {
James Dongf60cafe2010-06-19 09:04:18 -0700711 releaseOneRecordingFrame(data);
James Dong13aec892010-04-21 16:14:15 -0700712 ++mNumFramesReceived;
713 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700714 return;
715 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700716
James Dongf60cafe2010-06-19 09:04:18 -0700717 if (mNumFramesReceived > 0 &&
718 timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
719 if (mNumGlitches % 10 == 0) { // Don't spam the log
James Dongba290022010-12-09 15:04:33 -0800720 LOGV("Long delay detected in video recording");
James Dongf60cafe2010-06-19 09:04:18 -0700721 }
722 ++mNumGlitches;
723 }
724
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700725 // May need to skip frame or modify timestamp. Currently implemented
726 // by the subclass CameraSourceTimeLapse.
727 if(skipCurrentFrame(timestampUs)) {
728 releaseOneRecordingFrame(data);
729 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700730 }
731
James Dong365a9632010-06-04 13:59:27 -0700732 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700733 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700734 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700735 // Initial delay
736 if (mStartTimeUs > 0) {
737 if (timestampUs < mStartTimeUs) {
738 // Frame was captured before recording was started
739 // Drop it without updating the statistical data.
740 releaseOneRecordingFrame(data);
741 return;
742 }
743 mStartTimeUs = timestampUs - mStartTimeUs;
744 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700745 }
James Dong13aec892010-04-21 16:14:15 -0700746 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700747
James Dong7278cf32010-05-27 16:05:58 -0700748 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700749 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
750 mFrameTimes.push_back(timeUs);
751 LOGV("initial delay: %lld, current time stamp: %lld",
752 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700753 mFrameAvailableCondition.signal();
754}
755
James Dong5c952312010-10-18 21:42:27 -0700756size_t CameraSource::getNumberOfVideoBuffers() const {
757 LOGV("getNumberOfVideoBuffers");
758 size_t nBuffers = 0;
759 int64_t token = IPCThreadState::self()->clearCallingIdentity();
760 if (mInitCheck == OK && mCamera != 0) {
761 nBuffers = mCamera->getNumberOfVideoBuffers();
762 }
763 IPCThreadState::self()->restoreCallingIdentity(token);
764 return nBuffers;
765}
766
767sp<IMemory> CameraSource::getVideoBuffer(size_t index) const {
768 LOGV("getVideoBuffer: %d", index);
769 sp<IMemory> buffer = 0;
770 int64_t token = IPCThreadState::self()->clearCallingIdentity();
771 if (mInitCheck == OK && mCamera != 0) {
772 buffer = mCamera->getVideoBuffer(index);
773 }
774 IPCThreadState::self()->restoreCallingIdentity(token);
775 return buffer;
776}
777
778bool CameraSource::isMetaDataStoredInVideoBuffers() const {
779 LOGV("isMetaDataStoredInVideoBuffers");
780 return mIsMetaDataStoredInVideoBuffers;
781}
782
Andreas Huber20111aa2009-07-14 16:56:47 -0700783} // namespace android