blob: ed8149abbbd38d0fddf1d943283d2d351ad11b70 [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;
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800118 return new CameraSource(camera, NULL, 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,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800124 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700125 int32_t cameraId,
126 Size videoSize,
127 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700128 const sp<Surface>& surface,
129 bool storeMetaDataInVideoBuffers) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800130
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800131 CameraSource *source = new CameraSource(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700132 videoSize, frameRate, surface,
133 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700134 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800135}
136
James Dong54ff19a2010-10-08 11:59:32 -0700137CameraSource::CameraSource(
138 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800139 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700140 int32_t cameraId,
141 Size videoSize,
142 int32_t frameRate,
James Dong5c952312010-10-18 21:42:27 -0700143 const sp<Surface>& surface,
144 bool storeMetaDataInVideoBuffers)
James Dong54ff19a2010-10-08 11:59:32 -0700145 : mCameraFlags(0),
146 mVideoFrameRate(-1),
147 mCamera(0),
148 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700149 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700150 mLastFrameTimestampUs(0),
151 mStarted(false),
James Dong13aec892010-04-21 16:14:15 -0700152 mNumFramesEncoded(0),
James Dong7757f502011-01-25 16:31:28 -0800153 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700154 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700155 mNumGlitches(0),
156 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700157 mCollectStats(false) {
James Dong54ff19a2010-10-08 11:59:32 -0700158 mVideoSize.width = -1;
159 mVideoSize.height = -1;
160
Wu-cheng Li95068be2011-06-29 15:17:11 +0800161 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800162 mInitCheck = init(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700163 videoSize, frameRate,
164 storeMetaDataInVideoBuffers);
Wu-cheng Li95068be2011-06-29 15:17:11 +0800165 if (mInitCheck != OK) releaseCamera();
166 IPCThreadState::self()->restoreCallingIdentity(token);
James Dong54ff19a2010-10-08 11:59:32 -0700167}
168
169status_t CameraSource::initCheck() const {
170 return mInitCheck;
171}
172
173status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800174 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
175 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700176
177 if (camera == 0) {
178 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800179 if (mCamera == 0) return -EBUSY;
180 // If proxy is not passed in by applications, still use the proxy of
181 // our own Camera to simplify the code.
182 mCameraRecordingProxy = mCamera->getRecordingProxy();
James Dong54ff19a2010-10-08 11:59:32 -0700183 mCameraFlags &= ~FLAGS_HOT_CAMERA;
184 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800185 // We get the proxy from Camera, not ICamera. We need to get the proxy
186 // to the remote Camera owned by the application. Here mCamera is a
187 // local Camera object created by us. We cannot use the proxy from
188 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700189 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800190 if (mCamera == 0) return -EBUSY;
191 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700192 mCameraFlags |= FLAGS_HOT_CAMERA;
193 }
194
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800195 mCamera->lock();
196 mDeathNotifier = new DeathNotifier();
197 // isBinderAlive needs linkToDeath to work.
198 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
199
James Dong54ff19a2010-10-08 11:59:32 -0700200 return OK;
201}
202
203
204/*
205 * Check to see whether the requested video width and height is one
206 * of the supported sizes.
207 * @param width the video frame width in pixels
208 * @param height the video frame height in pixels
209 * @param suppportedSizes the vector of sizes that we check against
210 * @return true if the dimension (width and height) is supported.
211 */
212static bool isVideoSizeSupported(
213 int32_t width, int32_t height,
214 const Vector<Size>& supportedSizes) {
215
216 LOGV("isVideoSizeSupported");
217 for (size_t i = 0; i < supportedSizes.size(); ++i) {
218 if (width == supportedSizes[i].width &&
219 height == supportedSizes[i].height) {
220 return true;
221 }
222 }
223 return false;
224}
225
226/*
227 * If the preview and video output is separate, we only set the
228 * the video size, and applications should set the preview size
229 * to some proper value, and the recording framework will not
230 * change the preview size; otherwise, if the video and preview
231 * output is the same, we need to set the preview to be the same
232 * as the requested video size.
233 *
234 */
235/*
236 * Query the camera to retrieve the supported video frame sizes
237 * and also to see whether CameraParameters::setVideoSize()
238 * is supported or not.
239 * @param params CameraParameters to retrieve the information
240 * @@param isSetVideoSizeSupported retunrs whether method
241 * CameraParameters::setVideoSize() is supported or not.
242 * @param sizes returns the vector of Size objects for the
243 * supported video frame sizes advertised by the camera.
244 */
245static void getSupportedVideoSizes(
246 const CameraParameters& params,
247 bool *isSetVideoSizeSupported,
248 Vector<Size>& sizes) {
249
250 *isSetVideoSizeSupported = true;
251 params.getSupportedVideoSizes(sizes);
252 if (sizes.size() == 0) {
253 LOGD("Camera does not support setVideoSize()");
254 params.getSupportedPreviewSizes(sizes);
255 *isSetVideoSizeSupported = false;
256 }
257}
258
259/*
260 * Check whether the camera has the supported color format
261 * @param params CameraParameters to retrieve the information
262 * @return OK if no error.
263 */
264status_t CameraSource::isCameraColorFormatSupported(
265 const CameraParameters& params) {
266 mColorFormat = getColorFormat(params.get(
267 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
268 if (mColorFormat == -1) {
269 return BAD_VALUE;
270 }
271 return OK;
272}
273
274/*
275 * Configure the camera to use the requested video size
276 * (width and height) and/or frame rate. If both width and
277 * height are -1, configuration on the video size is skipped.
278 * if frameRate is -1, configuration on the frame rate
279 * is skipped. Skipping the configuration allows one to
280 * use the current camera setting without the need to
281 * actually know the specific values (see Create() method).
282 *
283 * @param params the CameraParameters to be configured
284 * @param width the target video frame width in pixels
285 * @param height the target video frame height in pixels
286 * @param frameRate the target frame rate in frames per second.
287 * @return OK if no error.
288 */
289status_t CameraSource::configureCamera(
290 CameraParameters* params,
291 int32_t width, int32_t height,
292 int32_t frameRate) {
293
294 Vector<Size> sizes;
295 bool isSetVideoSizeSupportedByCamera = true;
296 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
297 bool isCameraParamChanged = false;
298 if (width != -1 && height != -1) {
299 if (!isVideoSizeSupported(width, height, sizes)) {
300 LOGE("Video dimension (%dx%d) is unsupported", width, height);
301 return BAD_VALUE;
302 }
303 if (isSetVideoSizeSupportedByCamera) {
304 params->setVideoSize(width, height);
305 } else {
306 params->setPreviewSize(width, height);
307 }
308 isCameraParamChanged = true;
309 } else if ((width == -1 && height != -1) ||
310 (width != -1 && height == -1)) {
311 // If one and only one of the width and height is -1
312 // we reject such a request.
313 LOGE("Requested video size (%dx%d) is not supported", width, height);
314 return BAD_VALUE;
315 } else { // width == -1 && height == -1
316 // Do not configure the camera.
317 // Use the current width and height value setting from the camera.
318 }
319
320 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700321 CHECK(frameRate > 0 && frameRate <= 120);
322 const char* supportedFrameRates =
323 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
324 CHECK(supportedFrameRates != NULL);
325 LOGV("Supported frame rates: %s", supportedFrameRates);
326 char buf[4];
327 snprintf(buf, 4, "%d", frameRate);
328 if (strstr(supportedFrameRates, buf) == NULL) {
329 LOGE("Requested frame rate (%d) is not supported: %s",
330 frameRate, supportedFrameRates);
331 return BAD_VALUE;
332 }
333
334 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700335 params->setPreviewFrameRate(frameRate);
336 isCameraParamChanged = true;
337 } else { // frameRate == -1
338 // Do not configure the camera.
339 // Use the current frame rate value setting from the camera
340 }
341
342 if (isCameraParamChanged) {
343 // Either frame rate or frame size needs to be changed.
344 String8 s = params->flatten();
345 if (OK != mCamera->setParameters(s)) {
346 LOGE("Could not change settings."
347 " Someone else is using camera %p?", mCamera.get());
348 return -EBUSY;
349 }
350 }
351 return OK;
352}
353
354/*
355 * Check whether the requested video frame size
356 * has been successfully configured or not. If both width and height
357 * are -1, check on the current width and height value setting
358 * is performed.
359 *
360 * @param params CameraParameters to retrieve the information
361 * @param the target video frame width in pixels to check against
362 * @param the target video frame height in pixels to check against
363 * @return OK if no error
364 */
365status_t CameraSource::checkVideoSize(
366 const CameraParameters& params,
367 int32_t width, int32_t height) {
368
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
420 int32_t frameRateActual = params.getPreviewFrameRate();
421 if (frameRateActual < 0) {
422 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
423 return UNKNOWN_ERROR;
424 }
425
426 // Check the actual video frame rate against the target/requested
427 // video frame rate.
428 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
429 LOGE("Failed to set preview frame rate to %d fps. The actual "
430 "frame rate is %d", frameRate, frameRateActual);
431 return UNKNOWN_ERROR;
432 }
433
434 // Good now.
435 mVideoFrameRate = frameRateActual;
436 return OK;
437}
438
439/*
440 * Initialize the CameraSource to so that it becomes
441 * ready for providing the video input streams as requested.
442 * @param camera the camera object used for the video source
443 * @param cameraId if camera == 0, use camera with this id
444 * as the video source
445 * @param videoSize the target video frame size. If both
446 * width and height in videoSize is -1, use the current
447 * width and heigth settings by the camera
448 * @param frameRate the target frame rate in frames per second.
449 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700450 * @param storeMetaDataInVideoBuffers request to store meta
451 * data or real YUV data in video buffers. Request to
452 * store meta data in video buffers may not be honored
453 * if the source does not support this feature.
454 *
James Dong54ff19a2010-10-08 11:59:32 -0700455 * @return OK if no error.
456 */
457status_t CameraSource::init(
458 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800459 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700460 int32_t cameraId,
461 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700462 int32_t frameRate,
463 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700464
465 status_t err = OK;
James Dong54ff19a2010-10-08 11:59:32 -0700466
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800467 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
468 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700469 return err;
470 }
471 CameraParameters params(mCamera->getParameters());
472 if ((err = isCameraColorFormatSupported(params)) != OK) {
473 return err;
474 }
475
476 // Set the camera to use the requested video frame size
477 // and/or frame rate.
478 if ((err = configureCamera(&params,
479 videoSize.width, videoSize.height,
480 frameRate))) {
481 return err;
482 }
483
484 // Check on video frame size and frame rate.
485 CameraParameters newCameraParams(mCamera->getParameters());
486 if ((err = checkVideoSize(newCameraParams,
487 videoSize.width, videoSize.height)) != OK) {
488 return err;
489 }
490 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
491 return err;
492 }
493
494 // This CHECK is good, since we just passed the lock/unlock
495 // check earlier by calling mCamera->setParameters().
496 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700497
James Dongabdd2ba2010-12-10 13:09:05 -0800498 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700499 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800500 mCamera->storeMetaDataInBuffers(false);
501 if (storeMetaDataInVideoBuffers) {
502 if (OK == mCamera->storeMetaDataInBuffers(true)) {
503 mIsMetaDataStoredInVideoBuffers = true;
504 }
James Dong5c952312010-10-18 21:42:27 -0700505 }
506
James Dong54ff19a2010-10-08 11:59:32 -0700507 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700508 if (glitchDurationUs > mGlitchDurationThresholdUs) {
509 mGlitchDurationThresholdUs = glitchDurationUs;
510 }
511
James Dongddcc4a62010-06-08 11:58:53 -0700512 // XXX: query camera for the stride and slice height
513 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700514 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700515 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
516 mMeta->setInt32(kKeyColorFormat, mColorFormat);
517 mMeta->setInt32(kKeyWidth, mVideoSize.width);
518 mMeta->setInt32(kKeyHeight, mVideoSize.height);
519 mMeta->setInt32(kKeyStride, mVideoSize.width);
520 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800521 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700522 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700523}
524
525CameraSource::~CameraSource() {
526 if (mStarted) {
527 stop();
528 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700529}
530
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700531void CameraSource::startCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800532 // Reset the identity to the current thread because media server owns the
533 // camera and recording is started by the applications. The applications
534 // will connect to the camera in ICameraRecordingProxy::startRecording.
535 int64_t token = IPCThreadState::self()->clearCallingIdentity();
536 mCamera->unlock();
537 mCamera.clear();
538 IPCThreadState::self()->restoreCallingIdentity(token);
539 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700540}
541
James Dongf60cafe2010-06-19 09:04:18 -0700542status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700543 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700544 if (mInitCheck != OK) {
545 LOGE("CameraSource is not initialized yet");
546 return mInitCheck;
547 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700548
James Dong365a9632010-06-04 13:59:27 -0700549 char value[PROPERTY_VALUE_MAX];
550 if (property_get("media.stagefright.record-stats", value, NULL)
551 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
552 mCollectStats = true;
553 }
James Dong9d7f58a2010-06-09 15:57:48 -0700554
James Dongf60cafe2010-06-19 09:04:18 -0700555 mStartTimeUs = 0;
556 int64_t startTimeUs;
557 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
558 mStartTimeUs = startTimeUs;
559 }
560
James Dongc42478e2010-11-15 10:38:37 -0800561 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700562
563 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700564 return OK;
565}
566
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700567void CameraSource::stopCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800568 mCameraRecordingProxy->stopRecording();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700569}
570
James Dongea7b4852010-12-05 14:25:34 -0800571void CameraSource::releaseCamera() {
572 LOGV("releaseCamera");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800573 if (mCamera != 0) {
574 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
575 LOGV("Camera was cold when we started, stopping preview");
576 mCamera->stopPreview();
577 mCamera->disconnect();
578 } else {
579 // Unlock the camera so the application can lock it back.
580 mCamera->unlock();
581 }
582 mCamera.clear();
James Dongea7b4852010-12-05 14:25:34 -0800583 }
Wu-cheng Li95068be2011-06-29 15:17:11 +0800584 if (mCameraRecordingProxy != 0) {
585 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
586 mCameraRecordingProxy.clear();
587 }
James Dongea7b4852010-12-05 14:25:34 -0800588 mCameraFlags = 0;
589}
590
Andreas Huber20111aa2009-07-14 16:56:47 -0700591status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800592 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700593 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700594 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700595 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700596
James Dongc32cd792010-04-26 17:48:26 -0700597 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700598 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800599 if (NO_ERROR !=
600 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
601 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700602 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800603 }
James Dong7278cf32010-05-27 16:05:58 -0700604 }
James Dongd69c7f62010-12-09 11:24:22 -0800605 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800606 releaseCamera();
James Dong7278cf32010-05-27 16:05:58 -0700607
James Dong365a9632010-06-04 13:59:27 -0700608 if (mCollectStats) {
609 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
610 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
611 mLastFrameTimestampUs - mFirstFrameTimeUs);
612 }
James Dong13aec892010-04-21 16:14:15 -0700613
James Dongba290022010-12-09 15:04:33 -0800614 if (mNumGlitches > 0) {
James Donga4726132011-02-16 12:28:26 -0800615 LOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800616 }
617
James Dong13aec892010-04-21 16:14:15 -0700618 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800619 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700620 return OK;
621}
622
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700623void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800624 if (mCameraRecordingProxy != NULL) {
625 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dongd69c7f62010-12-09 11:24:22 -0800626 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700627}
628
James Dongc32cd792010-04-26 17:48:26 -0700629void CameraSource::releaseQueuedFrames() {
630 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700631 while (!mFramesReceived.empty()) {
632 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700633 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700634 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700635 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700636 }
637}
638
Andreas Huber20111aa2009-07-14 16:56:47 -0700639sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700640 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700641}
642
James Dongf60cafe2010-06-19 09:04:18 -0700643void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700644 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700645}
646
James Dong7278cf32010-05-27 16:05:58 -0700647void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
648 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700649 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700650 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
651 it != mFramesBeingEncoded.end(); ++it) {
652 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700653 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700654 mFramesBeingEncoded.erase(it);
655 ++mNumFramesEncoded;
656 buffer->setObserver(0);
657 buffer->release();
658 mFrameCompleteCondition.signal();
659 return;
660 }
661 }
662 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
663}
664
Andreas Huber20111aa2009-07-14 16:56:47 -0700665status_t CameraSource::read(
666 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700667 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700668
669 *buffer = NULL;
670
671 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700672 ReadOptions::SeekMode mode;
673 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700674 return ERROR_UNSUPPORTED;
675 }
676
677 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700678 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700679
680 {
681 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800682 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800683 if (NO_ERROR !=
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800684 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) {
685 if (!mCameraRecordingProxy->asBinder()->isBinderAlive()) {
686 LOGW("camera recording proxy is gone");
687 return ERROR_END_OF_STREAM;
688 }
James Dong41152ef2010-12-20 21:29:12 -0800689 LOGW("Timed out waiting for incoming camera video frames: %lld us",
690 mLastFrameTimestampUs);
691 }
James Dong542db5d2010-07-21 14:51:35 -0700692 }
James Dong79e23b42010-12-11 10:43:41 -0800693 if (!mStarted) {
694 return OK;
695 }
696 frame = *mFramesReceived.begin();
697 mFramesReceived.erase(mFramesReceived.begin());
698
699 frameTime = *mFrameTimes.begin();
700 mFrameTimes.erase(mFrameTimes.begin());
701 mFramesBeingEncoded.push_back(frame);
702 *buffer = new MediaBuffer(frame->pointer(), frame->size());
703 (*buffer)->setObserver(this);
704 (*buffer)->add_ref();
705 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700706 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700707 return OK;
708}
709
James Dongc32cd792010-04-26 17:48:26 -0700710void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
711 int32_t msgType, const sp<IMemory> &data) {
712 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700713 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800714 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
715 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700716 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700717 return;
718 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700719
James Dong98cfde02011-06-03 17:21:16 -0700720 if (mNumFramesReceived > 0) {
721 CHECK(timestampUs > mLastFrameTimestampUs);
722 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
723 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700724 }
James Dongf60cafe2010-06-19 09:04:18 -0700725 }
726
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700727 // May need to skip frame or modify timestamp. Currently implemented
728 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800729 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700730 releaseOneRecordingFrame(data);
731 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700732 }
733
James Dong365a9632010-06-04 13:59:27 -0700734 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700735 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700736 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700737 // Initial delay
738 if (mStartTimeUs > 0) {
739 if (timestampUs < mStartTimeUs) {
740 // Frame was captured before recording was started
741 // Drop it without updating the statistical data.
742 releaseOneRecordingFrame(data);
743 return;
744 }
745 mStartTimeUs = timestampUs - mStartTimeUs;
746 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700747 }
James Dong13aec892010-04-21 16:14:15 -0700748 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700749
James Dong98cfde02011-06-03 17:21:16 -0700750 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700751 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700752 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
753 mFrameTimes.push_back(timeUs);
754 LOGV("initial delay: %lld, current time stamp: %lld",
755 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700756 mFrameAvailableCondition.signal();
757}
758
James Dong5c952312010-10-18 21:42:27 -0700759bool CameraSource::isMetaDataStoredInVideoBuffers() const {
760 LOGV("isMetaDataStoredInVideoBuffers");
761 return mIsMetaDataStoredInVideoBuffers;
762}
763
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800764CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
765 mSource = source;
766}
767
768void CameraSource::ProxyListener::dataCallbackTimestamp(
769 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
770 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
771}
772
773void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
774 LOGI("Camera recording proxy died");
775}
776
Andreas Huber20111aa2009-07-14 16:56:47 -0700777} // namespace android