blob: b1c6b18d7229e7652c87b642ab969eb5e28a7407 [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 Dongabdd2ba2010-12-10 13:09:05 -0800488 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700489 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800490 mCamera->storeMetaDataInBuffers(false);
491 if (storeMetaDataInVideoBuffers) {
492 if (OK == mCamera->storeMetaDataInBuffers(true)) {
493 mIsMetaDataStoredInVideoBuffers = true;
494 }
James Dong5c952312010-10-18 21:42:27 -0700495 }
496
James Dong9d7f58a2010-06-09 15:57:48 -0700497 IPCThreadState::self()->restoreCallingIdentity(token);
498
James Dong54ff19a2010-10-08 11:59:32 -0700499 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700500 if (glitchDurationUs > mGlitchDurationThresholdUs) {
501 mGlitchDurationThresholdUs = glitchDurationUs;
502 }
503
James Dongddcc4a62010-06-08 11:58:53 -0700504 // XXX: query camera for the stride and slice height
505 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700506 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700507 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
508 mMeta->setInt32(kKeyColorFormat, mColorFormat);
509 mMeta->setInt32(kKeyWidth, mVideoSize.width);
510 mMeta->setInt32(kKeyHeight, mVideoSize.height);
511 mMeta->setInt32(kKeyStride, mVideoSize.width);
512 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800513 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700514 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700515}
516
517CameraSource::~CameraSource() {
518 if (mStarted) {
519 stop();
520 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700521}
522
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700523void CameraSource::startCameraRecording() {
524 CHECK_EQ(OK, mCamera->startRecording());
James Dong2b37ced2010-10-09 01:16:58 -0700525 CHECK(mCamera->recordingEnabled());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700526}
527
James Dongf60cafe2010-06-19 09:04:18 -0700528status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700529 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700530 if (mInitCheck != OK) {
531 LOGE("CameraSource is not initialized yet");
532 return mInitCheck;
533 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700534
James Dong365a9632010-06-04 13:59:27 -0700535 char value[PROPERTY_VALUE_MAX];
536 if (property_get("media.stagefright.record-stats", value, NULL)
537 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
538 mCollectStats = true;
539 }
James Dong9d7f58a2010-06-09 15:57:48 -0700540
James Dongf60cafe2010-06-19 09:04:18 -0700541 mStartTimeUs = 0;
542 int64_t startTimeUs;
543 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
544 mStartTimeUs = startTimeUs;
545 }
546
James Dongc42478e2010-11-15 10:38:37 -0800547 // Call setListener first before calling startCameraRecording()
548 // to avoid recording frames being dropped.
James Dong9d7f58a2010-06-09 15:57:48 -0700549 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700550 mCamera->setListener(new CameraSourceListener(this));
James Dongc42478e2010-11-15 10:38:37 -0800551 startCameraRecording();
James Dong9d7f58a2010-06-09 15:57:48 -0700552 IPCThreadState::self()->restoreCallingIdentity(token);
Andreas Huber20111aa2009-07-14 16:56:47 -0700553
554 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700555 return OK;
556}
557
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700558void CameraSource::stopCameraRecording() {
Nipun Kwatra78eff722010-09-15 15:08:49 -0700559 mCamera->setListener(NULL);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700560 mCamera->stopRecording();
561}
562
James Dongea7b4852010-12-05 14:25:34 -0800563void CameraSource::releaseCamera() {
564 LOGV("releaseCamera");
565 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
566 LOGV("Camera was cold when we started, stopping preview");
567 mCamera->stopPreview();
568 }
569 mCamera->unlock();
570 mCamera.clear();
571 mCamera = 0;
572 mCameraFlags = 0;
573}
574
Andreas Huber20111aa2009-07-14 16:56:47 -0700575status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800576 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700577 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700578 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700579 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700580
James Dong9d7f58a2010-06-09 15:57:48 -0700581 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dongc32cd792010-04-26 17:48:26 -0700582 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700583 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800584 if (NO_ERROR !=
585 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
586 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700587 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800588 }
James Dong7278cf32010-05-27 16:05:58 -0700589 }
James Dongd69c7f62010-12-09 11:24:22 -0800590 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800591 releaseCamera();
James Dong9d7f58a2010-06-09 15:57:48 -0700592 IPCThreadState::self()->restoreCallingIdentity(token);
James Dong7278cf32010-05-27 16:05:58 -0700593
James Dong365a9632010-06-04 13:59:27 -0700594 if (mCollectStats) {
595 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
596 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
597 mLastFrameTimestampUs - mFirstFrameTimeUs);
598 }
James Dong13aec892010-04-21 16:14:15 -0700599
James Dongba290022010-12-09 15:04:33 -0800600 if (mNumGlitches > 0) {
601 LOGW("%d long delays between neighboring video frames during",
602 mNumGlitches);
603 }
604
James Dong13aec892010-04-21 16:14:15 -0700605 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800606 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700607 return OK;
608}
609
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700610void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
James Dongd69c7f62010-12-09 11:24:22 -0800611 if (mCamera != NULL) {
612 mCamera->releaseRecordingFrame(frame);
613 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700614}
615
James Dongc32cd792010-04-26 17:48:26 -0700616void CameraSource::releaseQueuedFrames() {
617 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700618 while (!mFramesReceived.empty()) {
619 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700620 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700621 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700622 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700623 }
624}
625
Andreas Huber20111aa2009-07-14 16:56:47 -0700626sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700627 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700628}
629
James Dongf60cafe2010-06-19 09:04:18 -0700630void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
631 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700632 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700633 IPCThreadState::self()->restoreCallingIdentity(token);
634}
635
James Dong7278cf32010-05-27 16:05:58 -0700636void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
637 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700638 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700639 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
640 it != mFramesBeingEncoded.end(); ++it) {
641 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700642 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700643 mFramesBeingEncoded.erase(it);
644 ++mNumFramesEncoded;
645 buffer->setObserver(0);
646 buffer->release();
647 mFrameCompleteCondition.signal();
648 return;
649 }
650 }
651 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
652}
653
Andreas Huber20111aa2009-07-14 16:56:47 -0700654status_t CameraSource::read(
655 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700656 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700657
658 *buffer = NULL;
659
660 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700661 ReadOptions::SeekMode mode;
662 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700663 return ERROR_UNSUPPORTED;
664 }
665
666 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700667 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700668
669 {
670 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800671 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800672 if (NO_ERROR !=
673 mFrameAvailableCondition.waitRelative(mLock, 3000000000LL)) {
674 LOGW("Timed out waiting for incoming camera video frames: %lld us",
675 mLastFrameTimestampUs);
676 }
James Dong542db5d2010-07-21 14:51:35 -0700677 }
James Dong79e23b42010-12-11 10:43:41 -0800678 if (!mStarted) {
679 return OK;
680 }
681 frame = *mFramesReceived.begin();
682 mFramesReceived.erase(mFramesReceived.begin());
683
684 frameTime = *mFrameTimes.begin();
685 mFrameTimes.erase(mFrameTimes.begin());
686 mFramesBeingEncoded.push_back(frame);
687 *buffer = new MediaBuffer(frame->pointer(), frame->size());
688 (*buffer)->setObserver(this);
689 (*buffer)->add_ref();
690 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700691 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700692 return OK;
693}
694
James Dongc32cd792010-04-26 17:48:26 -0700695void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
696 int32_t msgType, const sp<IMemory> &data) {
697 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700698 Mutex::Autolock autoLock(mLock);
James Dongc32cd792010-04-26 17:48:26 -0700699 if (!mStarted) {
James Dongf60cafe2010-06-19 09:04:18 -0700700 releaseOneRecordingFrame(data);
James Dong13aec892010-04-21 16:14:15 -0700701 ++mNumFramesReceived;
702 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700703 return;
704 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700705
James Dongf60cafe2010-06-19 09:04:18 -0700706 if (mNumFramesReceived > 0 &&
707 timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
708 if (mNumGlitches % 10 == 0) { // Don't spam the log
James Dongba290022010-12-09 15:04:33 -0800709 LOGV("Long delay detected in video recording");
James Dongf60cafe2010-06-19 09:04:18 -0700710 }
711 ++mNumGlitches;
712 }
713
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700714 // May need to skip frame or modify timestamp. Currently implemented
715 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800716 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700717 releaseOneRecordingFrame(data);
718 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700719 }
720
James Dong365a9632010-06-04 13:59:27 -0700721 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700722 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700723 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700724 // Initial delay
725 if (mStartTimeUs > 0) {
726 if (timestampUs < mStartTimeUs) {
727 // Frame was captured before recording was started
728 // Drop it without updating the statistical data.
729 releaseOneRecordingFrame(data);
730 return;
731 }
732 mStartTimeUs = timestampUs - mStartTimeUs;
733 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700734 }
James Dong13aec892010-04-21 16:14:15 -0700735 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700736
James Dong7278cf32010-05-27 16:05:58 -0700737 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700738 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
739 mFrameTimes.push_back(timeUs);
740 LOGV("initial delay: %lld, current time stamp: %lld",
741 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700742 mFrameAvailableCondition.signal();
743}
744
James Dong5c952312010-10-18 21:42:27 -0700745size_t CameraSource::getNumberOfVideoBuffers() const {
746 LOGV("getNumberOfVideoBuffers");
747 size_t nBuffers = 0;
748 int64_t token = IPCThreadState::self()->clearCallingIdentity();
749 if (mInitCheck == OK && mCamera != 0) {
750 nBuffers = mCamera->getNumberOfVideoBuffers();
751 }
752 IPCThreadState::self()->restoreCallingIdentity(token);
753 return nBuffers;
754}
755
756sp<IMemory> CameraSource::getVideoBuffer(size_t index) const {
757 LOGV("getVideoBuffer: %d", index);
758 sp<IMemory> buffer = 0;
759 int64_t token = IPCThreadState::self()->clearCallingIdentity();
760 if (mInitCheck == OK && mCamera != 0) {
761 buffer = mCamera->getVideoBuffer(index);
762 }
763 IPCThreadState::self()->restoreCallingIdentity(token);
764 return buffer;
765}
766
767bool CameraSource::isMetaDataStoredInVideoBuffers() const {
768 LOGV("isMetaDataStoredInVideoBuffers");
769 return mIsMetaDataStoredInVideoBuffers;
770}
771
Andreas Huber20111aa2009-07-14 16:56:47 -0700772} // namespace android