blob: c7e7ced50bb9cafab7585a5623f8b1ae07a3804b [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);
Wu-cheng Li95068be2011-06-29 15:17:11 +0800164 if (mInitCheck != OK) releaseCamera();
James Dong54ff19a2010-10-08 11:59:32 -0700165}
166
167status_t CameraSource::initCheck() const {
168 return mInitCheck;
169}
170
171status_t CameraSource::isCameraAvailable(
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800172 const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy,
173 int32_t cameraId) {
James Dong54ff19a2010-10-08 11:59:32 -0700174
175 if (camera == 0) {
176 mCamera = Camera::connect(cameraId);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800177 if (mCamera == 0) return -EBUSY;
178 // If proxy is not passed in by applications, still use the proxy of
179 // our own Camera to simplify the code.
180 mCameraRecordingProxy = mCamera->getRecordingProxy();
James Dong54ff19a2010-10-08 11:59:32 -0700181 mCameraFlags &= ~FLAGS_HOT_CAMERA;
182 } else {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800183 // We get the proxy from Camera, not ICamera. We need to get the proxy
184 // to the remote Camera owned by the application. Here mCamera is a
185 // local Camera object created by us. We cannot use the proxy from
186 // mCamera here.
James Dong54ff19a2010-10-08 11:59:32 -0700187 mCamera = Camera::create(camera);
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800188 if (mCamera == 0) return -EBUSY;
189 mCameraRecordingProxy = proxy;
James Dong54ff19a2010-10-08 11:59:32 -0700190 mCameraFlags |= FLAGS_HOT_CAMERA;
191 }
192
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800193 mCamera->lock();
194 mDeathNotifier = new DeathNotifier();
195 // isBinderAlive needs linkToDeath to work.
196 mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier);
197
James Dong54ff19a2010-10-08 11:59:32 -0700198 return OK;
199}
200
201
202/*
203 * Check to see whether the requested video width and height is one
204 * of the supported sizes.
205 * @param width the video frame width in pixels
206 * @param height the video frame height in pixels
207 * @param suppportedSizes the vector of sizes that we check against
208 * @return true if the dimension (width and height) is supported.
209 */
210static bool isVideoSizeSupported(
211 int32_t width, int32_t height,
212 const Vector<Size>& supportedSizes) {
213
214 LOGV("isVideoSizeSupported");
215 for (size_t i = 0; i < supportedSizes.size(); ++i) {
216 if (width == supportedSizes[i].width &&
217 height == supportedSizes[i].height) {
218 return true;
219 }
220 }
221 return false;
222}
223
224/*
225 * If the preview and video output is separate, we only set the
226 * the video size, and applications should set the preview size
227 * to some proper value, and the recording framework will not
228 * change the preview size; otherwise, if the video and preview
229 * output is the same, we need to set the preview to be the same
230 * as the requested video size.
231 *
232 */
233/*
234 * Query the camera to retrieve the supported video frame sizes
235 * and also to see whether CameraParameters::setVideoSize()
236 * is supported or not.
237 * @param params CameraParameters to retrieve the information
238 * @@param isSetVideoSizeSupported retunrs whether method
239 * CameraParameters::setVideoSize() is supported or not.
240 * @param sizes returns the vector of Size objects for the
241 * supported video frame sizes advertised by the camera.
242 */
243static void getSupportedVideoSizes(
244 const CameraParameters& params,
245 bool *isSetVideoSizeSupported,
246 Vector<Size>& sizes) {
247
248 *isSetVideoSizeSupported = true;
249 params.getSupportedVideoSizes(sizes);
250 if (sizes.size() == 0) {
251 LOGD("Camera does not support setVideoSize()");
252 params.getSupportedPreviewSizes(sizes);
253 *isSetVideoSizeSupported = false;
254 }
255}
256
257/*
258 * Check whether the camera has the supported color format
259 * @param params CameraParameters to retrieve the information
260 * @return OK if no error.
261 */
262status_t CameraSource::isCameraColorFormatSupported(
263 const CameraParameters& params) {
264 mColorFormat = getColorFormat(params.get(
265 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
266 if (mColorFormat == -1) {
267 return BAD_VALUE;
268 }
269 return OK;
270}
271
272/*
273 * Configure the camera to use the requested video size
274 * (width and height) and/or frame rate. If both width and
275 * height are -1, configuration on the video size is skipped.
276 * if frameRate is -1, configuration on the frame rate
277 * is skipped. Skipping the configuration allows one to
278 * use the current camera setting without the need to
279 * actually know the specific values (see Create() method).
280 *
281 * @param params the CameraParameters to be configured
282 * @param width the target video frame width in pixels
283 * @param height the target video frame height in pixels
284 * @param frameRate the target frame rate in frames per second.
285 * @return OK if no error.
286 */
287status_t CameraSource::configureCamera(
288 CameraParameters* params,
289 int32_t width, int32_t height,
290 int32_t frameRate) {
291
292 Vector<Size> sizes;
293 bool isSetVideoSizeSupportedByCamera = true;
294 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
295 bool isCameraParamChanged = false;
296 if (width != -1 && height != -1) {
297 if (!isVideoSizeSupported(width, height, sizes)) {
298 LOGE("Video dimension (%dx%d) is unsupported", width, height);
299 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);
312 return BAD_VALUE;
313 } else { // width == -1 && height == -1
314 // Do not configure the camera.
315 // Use the current width and height value setting from the camera.
316 }
317
318 if (frameRate != -1) {
James Dong63573082010-10-24 09:32:39 -0700319 CHECK(frameRate > 0 && frameRate <= 120);
320 const char* supportedFrameRates =
321 params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES);
322 CHECK(supportedFrameRates != NULL);
323 LOGV("Supported frame rates: %s", supportedFrameRates);
324 char buf[4];
325 snprintf(buf, 4, "%d", frameRate);
326 if (strstr(supportedFrameRates, buf) == NULL) {
327 LOGE("Requested frame rate (%d) is not supported: %s",
328 frameRate, supportedFrameRates);
329 return BAD_VALUE;
330 }
331
332 // The frame rate is supported, set the camera to the requested value.
James Dong54ff19a2010-10-08 11:59:32 -0700333 params->setPreviewFrameRate(frameRate);
334 isCameraParamChanged = true;
335 } else { // frameRate == -1
336 // Do not configure the camera.
337 // Use the current frame rate value setting from the camera
338 }
339
340 if (isCameraParamChanged) {
341 // Either frame rate or frame size needs to be changed.
342 String8 s = params->flatten();
343 if (OK != mCamera->setParameters(s)) {
344 LOGE("Could not change settings."
345 " Someone else is using camera %p?", mCamera.get());
346 return -EBUSY;
347 }
348 }
349 return OK;
350}
351
352/*
353 * Check whether the requested video frame size
354 * has been successfully configured or not. If both width and height
355 * are -1, check on the current width and height value setting
356 * is performed.
357 *
358 * @param params CameraParameters to retrieve the information
359 * @param the target video frame width in pixels to check against
360 * @param the target video frame height in pixels to check against
361 * @return OK if no error
362 */
363status_t CameraSource::checkVideoSize(
364 const CameraParameters& params,
365 int32_t width, int32_t height) {
366
James Dongf96c9d12010-10-20 11:41:33 -0700367 // The actual video size is the same as the preview size
368 // if the camera hal does not support separate video and
369 // preview output. In this case, we retrieve the video
370 // size from preview.
James Dong54ff19a2010-10-08 11:59:32 -0700371 int32_t frameWidthActual = -1;
372 int32_t frameHeightActual = -1;
James Dongf96c9d12010-10-20 11:41:33 -0700373 Vector<Size> sizes;
374 params.getSupportedVideoSizes(sizes);
375 if (sizes.size() == 0) {
376 // video size is the same as preview size
377 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
378 } else {
379 // video size may not be the same as preview
380 params.getVideoSize(&frameWidthActual, &frameHeightActual);
381 }
James Dong54ff19a2010-10-08 11:59:32 -0700382 if (frameWidthActual < 0 || frameHeightActual < 0) {
383 LOGE("Failed to retrieve video frame size (%dx%d)",
384 frameWidthActual, frameHeightActual);
385 return UNKNOWN_ERROR;
386 }
387
388 // Check the actual video frame size against the target/requested
389 // video frame size.
390 if (width != -1 && height != -1) {
391 if (frameWidthActual != width || frameHeightActual != height) {
392 LOGE("Failed to set video frame size to %dx%d. "
393 "The actual video size is %dx%d ", width, height,
394 frameWidthActual, frameHeightActual);
395 return UNKNOWN_ERROR;
396 }
397 }
398
399 // Good now.
400 mVideoSize.width = frameWidthActual;
401 mVideoSize.height = frameHeightActual;
402 return OK;
403}
404
405/*
406 * Check the requested frame rate has been successfully configured or not.
407 * If the target frameRate is -1, check on the current frame rate value
408 * setting is performed.
409 *
410 * @param params CameraParameters to retrieve the information
411 * @param the target video frame rate to check against
412 * @return OK if no error.
413 */
414status_t CameraSource::checkFrameRate(
415 const CameraParameters& params,
416 int32_t frameRate) {
417
418 int32_t frameRateActual = params.getPreviewFrameRate();
419 if (frameRateActual < 0) {
420 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
421 return UNKNOWN_ERROR;
422 }
423
424 // Check the actual video frame rate against the target/requested
425 // video frame rate.
426 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
427 LOGE("Failed to set preview frame rate to %d fps. The actual "
428 "frame rate is %d", frameRate, frameRateActual);
429 return UNKNOWN_ERROR;
430 }
431
432 // Good now.
433 mVideoFrameRate = frameRateActual;
434 return OK;
435}
436
437/*
438 * Initialize the CameraSource to so that it becomes
439 * ready for providing the video input streams as requested.
440 * @param camera the camera object used for the video source
441 * @param cameraId if camera == 0, use camera with this id
442 * as the video source
443 * @param videoSize the target video frame size. If both
444 * width and height in videoSize is -1, use the current
445 * width and heigth settings by the camera
446 * @param frameRate the target frame rate in frames per second.
447 * if it is -1, use the current camera frame rate setting.
James Dong5c952312010-10-18 21:42:27 -0700448 * @param storeMetaDataInVideoBuffers request to store meta
449 * data or real YUV data in video buffers. Request to
450 * store meta data in video buffers may not be honored
451 * if the source does not support this feature.
452 *
James Dong54ff19a2010-10-08 11:59:32 -0700453 * @return OK if no error.
454 */
455status_t CameraSource::init(
456 const sp<ICamera>& camera,
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800457 const sp<ICameraRecordingProxy>& proxy,
James Dong54ff19a2010-10-08 11:59:32 -0700458 int32_t cameraId,
459 Size videoSize,
James Dong5c952312010-10-18 21:42:27 -0700460 int32_t frameRate,
461 bool storeMetaDataInVideoBuffers) {
James Dong54ff19a2010-10-08 11:59:32 -0700462
463 status_t err = OK;
James Dongae4c1ac2011-07-08 17:59:29 -0700464 int64_t token = IPCThreadState::self()->clearCallingIdentity();
465 err = initWithCameraAccess(camera, proxy, cameraId,
466 videoSize, frameRate,
467 storeMetaDataInVideoBuffers);
468 IPCThreadState::self()->restoreCallingIdentity(token);
469 return err;
470}
471
472status_t CameraSource::initWithCameraAccess(
473 const sp<ICamera>& camera,
474 const sp<ICameraRecordingProxy>& proxy,
475 int32_t cameraId,
476 Size videoSize,
477 int32_t frameRate,
478 bool storeMetaDataInVideoBuffers) {
479 status_t err = OK;
James Dong54ff19a2010-10-08 11:59:32 -0700480
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800481 if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) {
482 LOGE("Camera connection could not be established.");
James Dong54ff19a2010-10-08 11:59:32 -0700483 return err;
484 }
485 CameraParameters params(mCamera->getParameters());
486 if ((err = isCameraColorFormatSupported(params)) != OK) {
487 return err;
488 }
489
490 // Set the camera to use the requested video frame size
491 // and/or frame rate.
492 if ((err = configureCamera(&params,
493 videoSize.width, videoSize.height,
494 frameRate))) {
495 return err;
496 }
497
498 // Check on video frame size and frame rate.
499 CameraParameters newCameraParams(mCamera->getParameters());
500 if ((err = checkVideoSize(newCameraParams,
501 videoSize.width, videoSize.height)) != OK) {
502 return err;
503 }
504 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
505 return err;
506 }
507
508 // This CHECK is good, since we just passed the lock/unlock
509 // check earlier by calling mCamera->setParameters().
510 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong2b37ced2010-10-09 01:16:58 -0700511
James Dongabdd2ba2010-12-10 13:09:05 -0800512 // By default, do not store metadata in video buffers
James Dong5c952312010-10-18 21:42:27 -0700513 mIsMetaDataStoredInVideoBuffers = false;
James Dongabdd2ba2010-12-10 13:09:05 -0800514 mCamera->storeMetaDataInBuffers(false);
515 if (storeMetaDataInVideoBuffers) {
516 if (OK == mCamera->storeMetaDataInBuffers(true)) {
517 mIsMetaDataStoredInVideoBuffers = true;
518 }
James Dong5c952312010-10-18 21:42:27 -0700519 }
520
James Dong54ff19a2010-10-08 11:59:32 -0700521 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700522 if (glitchDurationUs > mGlitchDurationThresholdUs) {
523 mGlitchDurationThresholdUs = glitchDurationUs;
524 }
525
James Dongddcc4a62010-06-08 11:58:53 -0700526 // XXX: query camera for the stride and slice height
527 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700528 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700529 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
530 mMeta->setInt32(kKeyColorFormat, mColorFormat);
531 mMeta->setInt32(kKeyWidth, mVideoSize.width);
532 mMeta->setInt32(kKeyHeight, mVideoSize.height);
533 mMeta->setInt32(kKeyStride, mVideoSize.width);
534 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
James Dong393410a2010-11-10 20:43:53 -0800535 mMeta->setInt32(kKeyFrameRate, mVideoFrameRate);
James Dong54ff19a2010-10-08 11:59:32 -0700536 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700537}
538
539CameraSource::~CameraSource() {
540 if (mStarted) {
541 stop();
James Dongae4c1ac2011-07-08 17:59:29 -0700542 } else if (mInitCheck == OK) {
543 // Camera is initialized but because start() is never called,
544 // the lock on Camera is never released(). This makes sure
545 // Camera's lock is released in this case.
546 releaseCamera();
Andreas Huber20111aa2009-07-14 16:56:47 -0700547 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700548}
549
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700550void CameraSource::startCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800551 // Reset the identity to the current thread because media server owns the
552 // camera and recording is started by the applications. The applications
553 // will connect to the camera in ICameraRecordingProxy::startRecording.
554 int64_t token = IPCThreadState::self()->clearCallingIdentity();
555 mCamera->unlock();
556 mCamera.clear();
557 IPCThreadState::self()->restoreCallingIdentity(token);
558 CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this)));
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700559}
560
James Dongf60cafe2010-06-19 09:04:18 -0700561status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700562 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700563 if (mInitCheck != OK) {
564 LOGE("CameraSource is not initialized yet");
565 return mInitCheck;
566 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700567
James Dong365a9632010-06-04 13:59:27 -0700568 char value[PROPERTY_VALUE_MAX];
569 if (property_get("media.stagefright.record-stats", value, NULL)
570 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
571 mCollectStats = true;
572 }
James Dong9d7f58a2010-06-09 15:57:48 -0700573
James Dongf60cafe2010-06-19 09:04:18 -0700574 mStartTimeUs = 0;
575 int64_t startTimeUs;
576 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
577 mStartTimeUs = startTimeUs;
578 }
579
James Dongc42478e2010-11-15 10:38:37 -0800580 startCameraRecording();
Andreas Huber20111aa2009-07-14 16:56:47 -0700581
582 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700583 return OK;
584}
585
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700586void CameraSource::stopCameraRecording() {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800587 mCameraRecordingProxy->stopRecording();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700588}
589
James Dongea7b4852010-12-05 14:25:34 -0800590void CameraSource::releaseCamera() {
591 LOGV("releaseCamera");
Wu-cheng Li95068be2011-06-29 15:17:11 +0800592 if (mCamera != 0) {
James Dongae4c1ac2011-07-08 17:59:29 -0700593 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Wu-cheng Li95068be2011-06-29 15:17:11 +0800594 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
595 LOGV("Camera was cold when we started, stopping preview");
596 mCamera->stopPreview();
597 mCamera->disconnect();
598 } else {
599 // Unlock the camera so the application can lock it back.
600 mCamera->unlock();
601 }
602 mCamera.clear();
James Dongae4c1ac2011-07-08 17:59:29 -0700603 IPCThreadState::self()->restoreCallingIdentity(token);
James Dongea7b4852010-12-05 14:25:34 -0800604 }
Wu-cheng Li95068be2011-06-29 15:17:11 +0800605 if (mCameraRecordingProxy != 0) {
606 mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier);
607 mCameraRecordingProxy.clear();
608 }
James Dongea7b4852010-12-05 14:25:34 -0800609 mCameraFlags = 0;
610}
611
Andreas Huber20111aa2009-07-14 16:56:47 -0700612status_t CameraSource::stop() {
James Dong41152ef2010-12-20 21:29:12 -0800613 LOGD("stop: E");
James Dongc32cd792010-04-26 17:48:26 -0700614 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700615 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700616 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700617
James Dongc32cd792010-04-26 17:48:26 -0700618 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700619 while (!mFramesBeingEncoded.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800620 if (NO_ERROR !=
621 mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) {
622 LOGW("Timed out waiting for outstanding frames being encoded: %d",
James Dong365a9632010-06-04 13:59:27 -0700623 mFramesBeingEncoded.size());
James Dong41152ef2010-12-20 21:29:12 -0800624 }
James Dong7278cf32010-05-27 16:05:58 -0700625 }
James Dongd69c7f62010-12-09 11:24:22 -0800626 stopCameraRecording();
James Dongea7b4852010-12-05 14:25:34 -0800627 releaseCamera();
James Dong7278cf32010-05-27 16:05:58 -0700628
James Dong365a9632010-06-04 13:59:27 -0700629 if (mCollectStats) {
630 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
631 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
632 mLastFrameTimestampUs - mFirstFrameTimeUs);
633 }
James Dong13aec892010-04-21 16:14:15 -0700634
James Dongba290022010-12-09 15:04:33 -0800635 if (mNumGlitches > 0) {
James Donga4726132011-02-16 12:28:26 -0800636 LOGW("%d long delays between neighboring video frames", mNumGlitches);
James Dongba290022010-12-09 15:04:33 -0800637 }
638
James Dong13aec892010-04-21 16:14:15 -0700639 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
James Dong41152ef2010-12-20 21:29:12 -0800640 LOGD("stop: X");
Andreas Huber20111aa2009-07-14 16:56:47 -0700641 return OK;
642}
643
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700644void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800645 if (mCameraRecordingProxy != NULL) {
646 mCameraRecordingProxy->releaseRecordingFrame(frame);
James Dongd69c7f62010-12-09 11:24:22 -0800647 }
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700648}
649
James Dongc32cd792010-04-26 17:48:26 -0700650void CameraSource::releaseQueuedFrames() {
651 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700652 while (!mFramesReceived.empty()) {
653 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700654 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700655 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700656 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700657 }
658}
659
Andreas Huber20111aa2009-07-14 16:56:47 -0700660sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700661 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700662}
663
James Dongf60cafe2010-06-19 09:04:18 -0700664void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700665 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700666}
667
James Dong7278cf32010-05-27 16:05:58 -0700668void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
669 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700670 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700671 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
672 it != mFramesBeingEncoded.end(); ++it) {
673 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700674 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700675 mFramesBeingEncoded.erase(it);
676 ++mNumFramesEncoded;
677 buffer->setObserver(0);
678 buffer->release();
679 mFrameCompleteCondition.signal();
680 return;
681 }
682 }
683 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
684}
685
Andreas Huber20111aa2009-07-14 16:56:47 -0700686status_t CameraSource::read(
687 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700688 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700689
690 *buffer = NULL;
691
692 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700693 ReadOptions::SeekMode mode;
694 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700695 return ERROR_UNSUPPORTED;
696 }
697
698 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700699 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700700
701 {
702 Mutex::Autolock autoLock(mLock);
James Dong79e23b42010-12-11 10:43:41 -0800703 while (mStarted && mFramesReceived.empty()) {
James Dong41152ef2010-12-20 21:29:12 -0800704 if (NO_ERROR !=
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800705 mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) {
706 if (!mCameraRecordingProxy->asBinder()->isBinderAlive()) {
707 LOGW("camera recording proxy is gone");
708 return ERROR_END_OF_STREAM;
709 }
James Dong41152ef2010-12-20 21:29:12 -0800710 LOGW("Timed out waiting for incoming camera video frames: %lld us",
711 mLastFrameTimestampUs);
712 }
James Dong542db5d2010-07-21 14:51:35 -0700713 }
James Dong79e23b42010-12-11 10:43:41 -0800714 if (!mStarted) {
715 return OK;
716 }
717 frame = *mFramesReceived.begin();
718 mFramesReceived.erase(mFramesReceived.begin());
719
720 frameTime = *mFrameTimes.begin();
721 mFrameTimes.erase(mFrameTimes.begin());
722 mFramesBeingEncoded.push_back(frame);
723 *buffer = new MediaBuffer(frame->pointer(), frame->size());
724 (*buffer)->setObserver(this);
725 (*buffer)->add_ref();
726 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber20111aa2009-07-14 16:56:47 -0700727 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700728 return OK;
729}
730
James Dongc32cd792010-04-26 17:48:26 -0700731void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
732 int32_t msgType, const sp<IMemory> &data) {
733 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700734 Mutex::Autolock autoLock(mLock);
James Donga4726132011-02-16 12:28:26 -0800735 if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) {
736 LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs);
James Dongf60cafe2010-06-19 09:04:18 -0700737 releaseOneRecordingFrame(data);
James Dongc32cd792010-04-26 17:48:26 -0700738 return;
739 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700740
James Dong98cfde02011-06-03 17:21:16 -0700741 if (mNumFramesReceived > 0) {
742 CHECK(timestampUs > mLastFrameTimestampUs);
743 if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
744 ++mNumGlitches;
James Dongf60cafe2010-06-19 09:04:18 -0700745 }
James Dongf60cafe2010-06-19 09:04:18 -0700746 }
747
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700748 // May need to skip frame or modify timestamp. Currently implemented
749 // by the subclass CameraSourceTimeLapse.
James Dong79e23b42010-12-11 10:43:41 -0800750 if (skipCurrentFrame(timestampUs)) {
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700751 releaseOneRecordingFrame(data);
752 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700753 }
754
James Dong365a9632010-06-04 13:59:27 -0700755 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700756 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700757 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700758 // Initial delay
759 if (mStartTimeUs > 0) {
760 if (timestampUs < mStartTimeUs) {
761 // Frame was captured before recording was started
762 // Drop it without updating the statistical data.
763 releaseOneRecordingFrame(data);
764 return;
765 }
766 mStartTimeUs = timestampUs - mStartTimeUs;
767 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700768 }
James Dong13aec892010-04-21 16:14:15 -0700769 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700770
James Dong98cfde02011-06-03 17:21:16 -0700771 CHECK(data != NULL && data->size() > 0);
James Dong7278cf32010-05-27 16:05:58 -0700772 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700773 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
774 mFrameTimes.push_back(timeUs);
775 LOGV("initial delay: %lld, current time stamp: %lld",
776 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700777 mFrameAvailableCondition.signal();
778}
779
James Dong5c952312010-10-18 21:42:27 -0700780bool CameraSource::isMetaDataStoredInVideoBuffers() const {
781 LOGV("isMetaDataStoredInVideoBuffers");
782 return mIsMetaDataStoredInVideoBuffers;
783}
784
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800785CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) {
786 mSource = source;
787}
788
789void CameraSource::ProxyListener::dataCallbackTimestamp(
790 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
791 mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr);
792}
793
794void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) {
795 LOGI("Camera recording proxy died");
796}
797
Andreas Huber20111aa2009-07-14 16:56:47 -0700798} // namespace android