blob: bdffce9c10fe9a52206370e333ad383d8ca5c867 [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 Li4ca2c7c2011-06-01 17:22:24 +0800161 mInitCheck = init(camera, proxy, cameraId,
James Dong5c952312010-10-18 21:42:27 -0700162 videoSize, frameRate,
163 storeMetaDataInVideoBuffers);
James Dong54ff19a2010-10-08 11:59:32 -0700164}
165
166status_t CameraSource::initCheck() const {
167 return mInitCheck;
168}
169
170status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800171 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
172 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700173
174 if (camera == 0) {
175 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800176 if (mCamera == 0) return -EBUSY;
177 // If proxy is not passed in by applications, still use the proxy of
178 // our own Camera to simplify the code.
179 mCameraRecordingProxy = mCamera->getRecordingProxy();
James Dong54ff19a2010-10-08 11:59:32 -0700180 mCameraFlags &= ~FLAGS_HOT_CAMERA;
181 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800182 // We get the proxy from Camera, not ICamera. We need to get the proxy
183 // to the remote Camera owned by the application. Here mCamera is a
184 // local Camera object created by us. We cannot use the proxy from
185 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700186 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800187 if (mCamera == 0) return -EBUSY;
188 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700189 mCameraFlags |= FLAGS_HOT_CAMERA;
190 }
191
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800192 mCamera->lock();
193 mDeathNotifier = new DeathNotifier();
194 // isBinderAlive needs linkToDeath to work.
195 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
196
James Dong54ff19a2010-10-08 11:59:32 -0700197 return OK;
198}
199
200
201/*
202 * Check to see whether the requested video width and height is one
203 * of the supported sizes.
204 * @param width the video frame width in pixels
205 * @param height the video frame height in pixels
206 * @param suppportedSizes the vector of sizes that we check against
207 * @return true if the dimension (width and height) is supported.
208 */
209static bool isVideoSizeSupported(
210 int32_t width, int32_t height,
211 const Vector<Size>& supportedSizes) {
212
213 LOGV("isVideoSizeSupported");
214 for (size_t i = 0; i < supportedSizes.size(); ++i) {
215 if (width == supportedSizes[i].width &&
216 height == supportedSizes[i].height) {
217 return true;
218 }
219 }
220 return false;
221}
222
223/*
224 * If the preview and video output is separate, we only set the
225 * the video size, and applications should set the preview size
226 * to some proper value, and the recording framework will not
227 * change the preview size; otherwise, if the video and preview
228 * output is the same, we need to set the preview to be the same
229 * as the requested video size.
230 *
231 */
232/*
233 * Query the camera to retrieve the supported video frame sizes
234 * and also to see whether CameraParameters::setVideoSize()
235 * is supported or not.
236 * @param params CameraParameters to retrieve the information
237 * @@param isSetVideoSizeSupported retunrs whether method
238 * CameraParameters::setVideoSize() is supported or not.
239 * @param sizes returns the vector of Size objects for the
240 * supported video frame sizes advertised by the camera.
241 */
242static void getSupportedVideoSizes(
243 const CameraParameters& params,
244 bool *isSetVideoSizeSupported,
245 Vector<Size>& sizes) {
246
247 *isSetVideoSizeSupported = true;
248 params.getSupportedVideoSizes(sizes);
249 if (sizes.size() == 0) {
250 LOGD("Camera does not support setVideoSize()");
251 params.getSupportedPreviewSizes(sizes);
252 *isSetVideoSizeSupported = false;
253 }
254}
255
256/*
257 * Check whether the camera has the supported color format
258 * @param params CameraParameters to retrieve the information
259 * @return OK if no error.
260 */
261status_t CameraSource::isCameraColorFormatSupported(
262 const CameraParameters& params) {
263 mColorFormat = getColorFormat(params.get(
264 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
265 if (mColorFormat == -1) {
266 return BAD_VALUE;
267 }
268 return OK;
269}
270
271/*
272 * Configure the camera to use the requested video size
273 * (width and height) and/or frame rate. If both width and
274 * height are -1, configuration on the video size is skipped.
275 * if frameRate is -1, configuration on the frame rate
276 * is skipped. Skipping the configuration allows one to
277 * use the current camera setting without the need to
278 * actually know the specific values (see Create() method).
279 *
280 * @param params the CameraParameters to be configured
281 * @param width the target video frame width in pixels
282 * @param height the target video frame height in pixels
283 * @param frameRate the target frame rate in frames per second.
284 * @return OK if no error.
285 */
286status_t CameraSource::configureCamera(
287 CameraParameters* params,
288 int32_t width, int32_t height,
289 int32_t frameRate) {
290
291 Vector<Size> sizes;
292 bool isSetVideoSizeSupportedByCamera = true;
293 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
294 bool isCameraParamChanged = false;
295 if (width != -1 && height != -1) {
296 if (!isVideoSizeSupported(width, height, sizes)) {
297 LOGE("Video dimension (%dx%d) is unsupported", width, height);
James Dongea7b4852010-12-05 14:25:34 -0800298 releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700299 return BAD_VALUE;
300 }
301 if (isSetVideoSizeSupportedByCamera) {
302 params->setVideoSize(width, height);
303 } else {
304 params->setPreviewSize(width, height);
305 }
306 isCameraParamChanged = true;
307 } else if ((width == -1 && height != -1) ||
308 (width != -1 && height == -1)) {
309 // If one and only one of the width and height is -1
310 // we reject such a request.
311 LOGE("Requested video size (%dx%d) is not supported", width, height);
James Dongea7b4852010-12-05 14:25:34 -0800312 releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700313 return BAD_VALUE;
314 } else { // width == -1 && height == -1
315 // Do not configure the camera.
316 // Use the current width and height value setting from the camera.
317 }
318
319 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700320 CHECK(frameRate > 0 && frameRate <= 120);
321 const char* supportedFrameRates =
322 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
323 CHECK(supportedFrameRates != NULL);
324 LOGV("Supported frame rates: %s", supportedFrameRates);
325 char buf[4];
326 snprintf(buf, 4, "%d", frameRate);
327 if (strstr(supportedFrameRates, buf) == NULL) {
328 LOGE("Requested frame rate (%d) is not supported: %s",
329 frameRate, supportedFrameRates);
James Dongea7b4852010-12-05 14:25:34 -0800330 releaseCamera();
James Dong63573082010-10-24 09:32:39 -0700331 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 Dong9d7f58a2010-06-09 15:57:48 -0700466 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong54ff19a2010-10-08 11:59:32 -0700467
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800468 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
469 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700470 return err;
471 }
472 CameraParameters params(mCamera->getParameters());
473 if ((err = isCameraColorFormatSupported(params)) != OK) {
474 return err;
475 }
476
477 // Set the camera to use the requested video frame size
478 // and/or frame rate.
479 if ((err = configureCamera(&params,
480 videoSize.width, videoSize.height,
481 frameRate))) {
482 return err;
483 }
484
485 // Check on video frame size and frame rate.
486 CameraParameters newCameraParams(mCamera->getParameters());
487 if ((err = checkVideoSize(newCameraParams,
488 videoSize.width, videoSize.height)) != OK) {
489 return err;
490 }
491 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
492 return err;
493 }
494
495 // This CHECK is good, since we just passed the lock/unlock
496 // check earlier by calling mCamera->setParameters().
497 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700498
James Dongabdd2ba2010-12-10 13:09:05 -0800499 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700500 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800501 mCamera->storeMetaDataInBuffers(false);
502 if (storeMetaDataInVideoBuffers) {
503 if (OK == mCamera->storeMetaDataInBuffers(true)) {
504 mIsMetaDataStoredInVideoBuffers = true;
505 }
James Dong5c952312010-10-18 21:42:27 -0700506 }
507
James Dong9d7f58a2010-06-09 15:57:48 -0700508 IPCThreadState::self()->restoreCallingIdentity(token);
509
James Dong54ff19a2010-10-08 11:59:32 -0700510 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700511 if (glitchDurationUs > mGlitchDurationThresholdUs) {
512 mGlitchDurationThresholdUs = glitchDurationUs;
513 }
514
James Dongddcc4a62010-06-08 11:58:53 -0700515 // XXX: query camera for the stride and slice height
516 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700517 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700518 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
519 mMeta->setInt32(kKeyColorFormat, mColorFormat);
520 mMeta->setInt32(kKeyWidth, mVideoSize.width);
521 mMeta->setInt32(kKeyHeight, mVideoSize.height);
522 mMeta->setInt32(kKeyStride, mVideoSize.width);
523 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800524 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700525 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700526}
527
528CameraSource::~CameraSource() {
529 if (mStarted) {
530 stop();
531 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700532}
533
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700534void CameraSource::startCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800535 // Reset the identity to the current thread because media server owns the
536 // camera and recording is started by the applications. The applications
537 // will connect to the camera in ICameraRecordingProxy::startRecording.
538 int64_t token = IPCThreadState::self()->clearCallingIdentity();
539 mCamera->unlock();
540 mCamera.clear();
541 IPCThreadState::self()->restoreCallingIdentity(token);
542 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700543}
544
James Dongf60cafe2010-06-19 09:04:18 -0700545status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700546 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700547 if (mInitCheck != OK) {
548 LOGE("CameraSource is not initialized yet");
549 return mInitCheck;
550 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700551
James Dong365a9632010-06-04 13:59:27 -0700552 char value[PROPERTY_VALUE_MAX];
553 if (property_get("media.stagefright.record-stats", value, NULL)
554 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
555 mCollectStats = true;
556 }
James Dong9d7f58a2010-06-09 15:57:48 -0700557
James Dongf60cafe2010-06-19 09:04:18 -0700558 mStartTimeUs = 0;
559 int64_t startTimeUs;
560 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
561 mStartTimeUs = startTimeUs;
562 }
563
James Dongc42478e2010-11-15 10:38:37 -0800564 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700565
566 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700567 return OK;
568}
569
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700570void CameraSource::stopCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800571 mCameraRecordingProxy->stopRecording();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700572}
573
James Dongea7b4852010-12-05 14:25:34 -0800574void CameraSource::releaseCamera() {
575 LOGV("releaseCamera");
576 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
577 LOGV("Camera was cold when we started, stopping preview");
578 mCamera->stopPreview();
579 }
James Dongea7b4852010-12-05 14:25:34 -0800580 mCamera.clear();
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800581 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
582 mCameraRecordingProxy.clear();
James Dongea7b4852010-12-05 14:25:34 -0800583 mCameraFlags = 0;
584}
585
Andreas Huber20111aa2009-07-14 16:56:47 -0700586status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800587 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700588 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700589 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700590 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700591
James Dongc32cd792010-04-26 17:48:26 -0700592 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700593 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800594 if (NO_ERROR !=
595 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
596 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700597 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800598 }
James Dong7278cf32010-05-27 16:05:58 -0700599 }
James Dongd69c7f62010-12-09 11:24:22 -0800600 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800601 releaseCamera();
James Dong7278cf32010-05-27 16:05:58 -0700602
James Dong365a9632010-06-04 13:59:27 -0700603 if (mCollectStats) {
604 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
605 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
606 mLastFrameTimestampUs - mFirstFrameTimeUs);
607 }
James Dong13aec892010-04-21 16:14:15 -0700608
James Dongba290022010-12-09 15:04:33 -0800609 if (mNumGlitches > 0) {
James Donga4726132011-02-16 12:28:26 -0800610 LOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800611 }
612
James Dong13aec892010-04-21 16:14:15 -0700613 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800614 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700615 return OK;
616}
617
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700618void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800619 if (mCameraRecordingProxy != NULL) {
620 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dongd69c7f62010-12-09 11:24:22 -0800621 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700622}
623
James Dongc32cd792010-04-26 17:48:26 -0700624void CameraSource::releaseQueuedFrames() {
625 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700626 while (!mFramesReceived.empty()) {
627 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700628 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700629 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700630 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700631 }
632}
633
Andreas Huber20111aa2009-07-14 16:56:47 -0700634sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700635 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700636}
637
James Dongf60cafe2010-06-19 09:04:18 -0700638void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700639 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700640}
641
James Dong7278cf32010-05-27 16:05:58 -0700642void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
643 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700644 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700645 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
646 it != mFramesBeingEncoded.end(); ++it) {
647 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700648 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700649 mFramesBeingEncoded.erase(it);
650 ++mNumFramesEncoded;
651 buffer->setObserver(0);
652 buffer->release();
653 mFrameCompleteCondition.signal();
654 return;
655 }
656 }
657 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
658}
659
Andreas Huber20111aa2009-07-14 16:56:47 -0700660status_t CameraSource::read(
661 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700662 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700663
664 *buffer = NULL;
665
666 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700667 ReadOptions::SeekMode mode;
668 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700669 return ERROR_UNSUPPORTED;
670 }
671
672 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700673 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700674
675 {
676 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800677 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800678 if (NO_ERROR !=
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800679 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) {
680 if (!mCameraRecordingProxy->asBinder()->isBinderAlive()) {
681 LOGW("camera recording proxy is gone");
682 return ERROR_END_OF_STREAM;
683 }
James Dong41152ef2010-12-20 21:29:12 -0800684 LOGW("Timed out waiting for incoming camera video frames: %lld us",
685 mLastFrameTimestampUs);
686 }
James Dong542db5d2010-07-21 14:51:35 -0700687 }
James Dong79e23b42010-12-11 10:43:41 -0800688 if (!mStarted) {
689 return OK;
690 }
691 frame = *mFramesReceived.begin();
692 mFramesReceived.erase(mFramesReceived.begin());
693
694 frameTime = *mFrameTimes.begin();
695 mFrameTimes.erase(mFrameTimes.begin());
696 mFramesBeingEncoded.push_back(frame);
697 *buffer = new MediaBuffer(frame->pointer(), frame->size());
698 (*buffer)->setObserver(this);
699 (*buffer)->add_ref();
700 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700701 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700702 return OK;
703}
704
James Dongc32cd792010-04-26 17:48:26 -0700705void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
706 int32_t msgType, const sp<IMemory> &data) {
707 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700708 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800709 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
710 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700711 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700712 return;
713 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700714
James Dong98cfde02011-06-03 17:21:16 -0700715 if (mNumFramesReceived > 0) {
716 CHECK(timestampUs > mLastFrameTimestampUs);
717 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
718 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700719 }
James Dongf60cafe2010-06-19 09:04:18 -0700720 }
721
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700722 // May need to skip frame or modify timestamp. Currently implemented
723 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800724 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700725 releaseOneRecordingFrame(data);
726 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700727 }
728
James Dong365a9632010-06-04 13:59:27 -0700729 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700730 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700731 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700732 // Initial delay
733 if (mStartTimeUs > 0) {
734 if (timestampUs < mStartTimeUs) {
735 // Frame was captured before recording was started
736 // Drop it without updating the statistical data.
737 releaseOneRecordingFrame(data);
738 return;
739 }
740 mStartTimeUs = timestampUs - mStartTimeUs;
741 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700742 }
James Dong13aec892010-04-21 16:14:15 -0700743 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700744
James Dong98cfde02011-06-03 17:21:16 -0700745 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700746 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700747 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
748 mFrameTimes.push_back(timeUs);
749 LOGV("initial delay: %lld, current time stamp: %lld",
750 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700751 mFrameAvailableCondition.signal();
752}
753
James Dong5c952312010-10-18 21:42:27 -0700754bool CameraSource::isMetaDataStoredInVideoBuffers() const {
755 LOGV("isMetaDataStoredInVideoBuffers");
756 return mIsMetaDataStoredInVideoBuffers;
757}
758
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800759CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
760 mSource = source;
761}
762
763void CameraSource::ProxyListener::dataCallbackTimestamp(
764 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
765 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
766}
767
768void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
769 LOGI("Camera recording proxy died");
770}
771
Andreas Huber20111aa2009-07-14 16:56:47 -0700772} // namespace android