Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 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 Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "CameraSource" |
| 19 | #include <utils/Log.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 20 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 21 | #include <OMX_Component.h> |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 23 | #include <media/stagefright/CameraSource.h> |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 24 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 25 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 26 | #include <media/stagefright/MediaErrors.h> |
| 27 | #include <media/stagefright/MetaData.h> |
Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | #include <camera/Camera.h> |
| 29 | #include <camera/CameraParameters.h> |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 30 | #include <surfaceflinger/Surface.h> |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 31 | #include <utils/String8.h> |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 36 | struct 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 | |
| 45 | protected: |
| 46 | virtual ~CameraSourceListener(); |
| 47 | |
| 48 | private: |
| 49 | wp<CameraSource> mSource; |
| 50 | |
| 51 | CameraSourceListener(const CameraSourceListener &); |
| 52 | CameraSourceListener &operator=(const CameraSourceListener &); |
| 53 | }; |
| 54 | |
| 55 | CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source) |
| 56 | : mSource(source) { |
| 57 | } |
| 58 | |
| 59 | CameraSourceListener::~CameraSourceListener() { |
| 60 | } |
| 61 | |
| 62 | void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) { |
| 63 | LOGV("notify(%d, %d, %d)", msgType, ext1, ext2); |
| 64 | } |
| 65 | |
| 66 | void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr) { |
| 67 | LOGV("postData(%d, ptr:%p, size:%d)", |
| 68 | msgType, dataPtr->pointer(), dataPtr->size()); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 69 | |
| 70 | sp<CameraSource> source = mSource.promote(); |
| 71 | if (source.get() != NULL) { |
| 72 | source->dataCallback(msgType, dataPtr); |
| 73 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void CameraSourceListener::postDataTimestamp( |
| 77 | nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 78 | |
| 79 | sp<CameraSource> source = mSource.promote(); |
| 80 | if (source.get() != NULL) { |
| 81 | source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr); |
| 82 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 85 | static int32_t getColorFormat(const char* colorFormat) { |
James Dong | e2d8ba8 | 2010-09-15 16:52:51 -0700 | [diff] [blame] | 86 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) { |
| 87 | return OMX_COLOR_FormatYUV420Planar; |
| 88 | } |
| 89 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 90 | 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 Dong | a1abc1a | 2010-09-13 16:30:51 -0700 | [diff] [blame] | 106 | LOGE("Uknown color format (%s), please add it to " |
| 107 | "CameraSource::getColorFormat", colorFormat); |
| 108 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 109 | CHECK_EQ(0, "Unknown color format"); |
| 110 | } |
| 111 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 112 | CameraSource *CameraSource::Create() { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 113 | Size size; |
| 114 | size.width = -1; |
| 115 | size.height = -1; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 116 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 117 | sp<ICamera> camera; |
| 118 | return new CameraSource(camera, 0, size, -1, NULL); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 121 | // static |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 122 | CameraSource *CameraSource::CreateFromCamera( |
| 123 | const sp<ICamera>& camera, |
| 124 | int32_t cameraId, |
| 125 | Size videoSize, |
| 126 | int32_t frameRate, |
| 127 | const sp<Surface>& surface) { |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 128 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 129 | CameraSource *source = new CameraSource(camera, cameraId, |
| 130 | videoSize, frameRate, surface); |
| 131 | |
| 132 | if (source != NULL) { |
| 133 | if (source->initCheck() != OK) { |
| 134 | delete source; |
| 135 | return NULL; |
| 136 | } |
| 137 | } |
| 138 | return source; |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 139 | } |
| 140 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 141 | CameraSource::CameraSource( |
| 142 | const sp<ICamera>& camera, |
| 143 | int32_t cameraId, |
| 144 | Size videoSize, |
| 145 | int32_t frameRate, |
| 146 | const sp<Surface>& surface) |
| 147 | : mCameraFlags(0), |
| 148 | mVideoFrameRate(-1), |
| 149 | mCamera(0), |
| 150 | mSurface(surface), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 151 | mNumFramesReceived(0), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 152 | mLastFrameTimestampUs(0), |
| 153 | mStarted(false), |
| 154 | mFirstFrameTimeUs(0), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 155 | mNumFramesEncoded(0), |
| 156 | mNumFramesDropped(0), |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 157 | mNumGlitches(0), |
| 158 | mGlitchDurationThresholdUs(200000), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 159 | mCollectStats(false) { |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 160 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 161 | mVideoSize.width = -1; |
| 162 | mVideoSize.height = -1; |
| 163 | |
| 164 | mInitCheck = init(camera, cameraId, videoSize, frameRate); |
| 165 | } |
| 166 | |
| 167 | status_t CameraSource::initCheck() const { |
| 168 | return mInitCheck; |
| 169 | } |
| 170 | |
| 171 | status_t CameraSource::isCameraAvailable( |
| 172 | const sp<ICamera>& camera, int32_t cameraId) { |
| 173 | |
| 174 | if (camera == 0) { |
| 175 | mCamera = Camera::connect(cameraId); |
| 176 | mCameraFlags &= ~FLAGS_HOT_CAMERA; |
| 177 | } else { |
| 178 | mCamera = Camera::create(camera); |
| 179 | mCameraFlags |= FLAGS_HOT_CAMERA; |
| 180 | } |
| 181 | |
| 182 | // Is camera available? |
| 183 | if (mCamera == 0) { |
| 184 | LOGE("Camera connection could not be established."); |
| 185 | return -EBUSY; |
| 186 | } |
| 187 | if (!(mCameraFlags & FLAGS_HOT_CAMERA)) { |
| 188 | mCamera->lock(); |
| 189 | } |
| 190 | return OK; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /* |
| 195 | * Check to see whether the requested video width and height is one |
| 196 | * of the supported sizes. |
| 197 | * @param width the video frame width in pixels |
| 198 | * @param height the video frame height in pixels |
| 199 | * @param suppportedSizes the vector of sizes that we check against |
| 200 | * @return true if the dimension (width and height) is supported. |
| 201 | */ |
| 202 | static bool isVideoSizeSupported( |
| 203 | int32_t width, int32_t height, |
| 204 | const Vector<Size>& supportedSizes) { |
| 205 | |
| 206 | LOGV("isVideoSizeSupported"); |
| 207 | for (size_t i = 0; i < supportedSizes.size(); ++i) { |
| 208 | if (width == supportedSizes[i].width && |
| 209 | height == supportedSizes[i].height) { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * If the preview and video output is separate, we only set the |
| 218 | * the video size, and applications should set the preview size |
| 219 | * to some proper value, and the recording framework will not |
| 220 | * change the preview size; otherwise, if the video and preview |
| 221 | * output is the same, we need to set the preview to be the same |
| 222 | * as the requested video size. |
| 223 | * |
| 224 | */ |
| 225 | /* |
| 226 | * Query the camera to retrieve the supported video frame sizes |
| 227 | * and also to see whether CameraParameters::setVideoSize() |
| 228 | * is supported or not. |
| 229 | * @param params CameraParameters to retrieve the information |
| 230 | * @@param isSetVideoSizeSupported retunrs whether method |
| 231 | * CameraParameters::setVideoSize() is supported or not. |
| 232 | * @param sizes returns the vector of Size objects for the |
| 233 | * supported video frame sizes advertised by the camera. |
| 234 | */ |
| 235 | static void getSupportedVideoSizes( |
| 236 | const CameraParameters& params, |
| 237 | bool *isSetVideoSizeSupported, |
| 238 | Vector<Size>& sizes) { |
| 239 | |
| 240 | *isSetVideoSizeSupported = true; |
| 241 | params.getSupportedVideoSizes(sizes); |
| 242 | if (sizes.size() == 0) { |
| 243 | LOGD("Camera does not support setVideoSize()"); |
| 244 | params.getSupportedPreviewSizes(sizes); |
| 245 | *isSetVideoSizeSupported = false; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Check whether the camera has the supported color format |
| 251 | * @param params CameraParameters to retrieve the information |
| 252 | * @return OK if no error. |
| 253 | */ |
| 254 | status_t CameraSource::isCameraColorFormatSupported( |
| 255 | const CameraParameters& params) { |
| 256 | mColorFormat = getColorFormat(params.get( |
| 257 | CameraParameters::KEY_VIDEO_FRAME_FORMAT)); |
| 258 | if (mColorFormat == -1) { |
| 259 | return BAD_VALUE; |
| 260 | } |
| 261 | return OK; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Configure the camera to use the requested video size |
| 266 | * (width and height) and/or frame rate. If both width and |
| 267 | * height are -1, configuration on the video size is skipped. |
| 268 | * if frameRate is -1, configuration on the frame rate |
| 269 | * is skipped. Skipping the configuration allows one to |
| 270 | * use the current camera setting without the need to |
| 271 | * actually know the specific values (see Create() method). |
| 272 | * |
| 273 | * @param params the CameraParameters to be configured |
| 274 | * @param width the target video frame width in pixels |
| 275 | * @param height the target video frame height in pixels |
| 276 | * @param frameRate the target frame rate in frames per second. |
| 277 | * @return OK if no error. |
| 278 | */ |
| 279 | status_t CameraSource::configureCamera( |
| 280 | CameraParameters* params, |
| 281 | int32_t width, int32_t height, |
| 282 | int32_t frameRate) { |
| 283 | |
| 284 | Vector<Size> sizes; |
| 285 | bool isSetVideoSizeSupportedByCamera = true; |
| 286 | getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes); |
| 287 | bool isCameraParamChanged = false; |
| 288 | if (width != -1 && height != -1) { |
| 289 | if (!isVideoSizeSupported(width, height, sizes)) { |
| 290 | LOGE("Video dimension (%dx%d) is unsupported", width, height); |
| 291 | return BAD_VALUE; |
| 292 | } |
| 293 | if (isSetVideoSizeSupportedByCamera) { |
| 294 | params->setVideoSize(width, height); |
| 295 | } else { |
| 296 | params->setPreviewSize(width, height); |
| 297 | } |
| 298 | isCameraParamChanged = true; |
| 299 | } else if ((width == -1 && height != -1) || |
| 300 | (width != -1 && height == -1)) { |
| 301 | // If one and only one of the width and height is -1 |
| 302 | // we reject such a request. |
| 303 | LOGE("Requested video size (%dx%d) is not supported", width, height); |
| 304 | return BAD_VALUE; |
| 305 | } else { // width == -1 && height == -1 |
| 306 | // Do not configure the camera. |
| 307 | // Use the current width and height value setting from the camera. |
| 308 | } |
| 309 | |
| 310 | if (frameRate != -1) { |
| 311 | params->setPreviewFrameRate(frameRate); |
| 312 | isCameraParamChanged = true; |
| 313 | } else { // frameRate == -1 |
| 314 | // Do not configure the camera. |
| 315 | // Use the current frame rate value setting from the camera |
| 316 | } |
| 317 | |
| 318 | if (isCameraParamChanged) { |
| 319 | // Either frame rate or frame size needs to be changed. |
| 320 | String8 s = params->flatten(); |
| 321 | if (OK != mCamera->setParameters(s)) { |
| 322 | LOGE("Could not change settings." |
| 323 | " Someone else is using camera %p?", mCamera.get()); |
| 324 | return -EBUSY; |
| 325 | } |
| 326 | } |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Check whether the requested video frame size |
| 332 | * has been successfully configured or not. If both width and height |
| 333 | * are -1, check on the current width and height value setting |
| 334 | * is performed. |
| 335 | * |
| 336 | * @param params CameraParameters to retrieve the information |
| 337 | * @param the target video frame width in pixels to check against |
| 338 | * @param the target video frame height in pixels to check against |
| 339 | * @return OK if no error |
| 340 | */ |
| 341 | status_t CameraSource::checkVideoSize( |
| 342 | const CameraParameters& params, |
| 343 | int32_t width, int32_t height) { |
| 344 | |
| 345 | int32_t frameWidthActual = -1; |
| 346 | int32_t frameHeightActual = -1; |
| 347 | params.getPreviewSize(&frameWidthActual, &frameHeightActual); |
| 348 | if (frameWidthActual < 0 || frameHeightActual < 0) { |
| 349 | LOGE("Failed to retrieve video frame size (%dx%d)", |
| 350 | frameWidthActual, frameHeightActual); |
| 351 | return UNKNOWN_ERROR; |
| 352 | } |
| 353 | |
| 354 | // Check the actual video frame size against the target/requested |
| 355 | // video frame size. |
| 356 | if (width != -1 && height != -1) { |
| 357 | if (frameWidthActual != width || frameHeightActual != height) { |
| 358 | LOGE("Failed to set video frame size to %dx%d. " |
| 359 | "The actual video size is %dx%d ", width, height, |
| 360 | frameWidthActual, frameHeightActual); |
| 361 | return UNKNOWN_ERROR; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Good now. |
| 366 | mVideoSize.width = frameWidthActual; |
| 367 | mVideoSize.height = frameHeightActual; |
| 368 | return OK; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Check the requested frame rate has been successfully configured or not. |
| 373 | * If the target frameRate is -1, check on the current frame rate value |
| 374 | * setting is performed. |
| 375 | * |
| 376 | * @param params CameraParameters to retrieve the information |
| 377 | * @param the target video frame rate to check against |
| 378 | * @return OK if no error. |
| 379 | */ |
| 380 | status_t CameraSource::checkFrameRate( |
| 381 | const CameraParameters& params, |
| 382 | int32_t frameRate) { |
| 383 | |
| 384 | int32_t frameRateActual = params.getPreviewFrameRate(); |
| 385 | if (frameRateActual < 0) { |
| 386 | LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual); |
| 387 | return UNKNOWN_ERROR; |
| 388 | } |
| 389 | |
| 390 | // Check the actual video frame rate against the target/requested |
| 391 | // video frame rate. |
| 392 | if (frameRate != -1 && (frameRateActual - frameRate) != 0) { |
| 393 | LOGE("Failed to set preview frame rate to %d fps. The actual " |
| 394 | "frame rate is %d", frameRate, frameRateActual); |
| 395 | return UNKNOWN_ERROR; |
| 396 | } |
| 397 | |
| 398 | // Good now. |
| 399 | mVideoFrameRate = frameRateActual; |
| 400 | return OK; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Initialize the CameraSource to so that it becomes |
| 405 | * ready for providing the video input streams as requested. |
| 406 | * @param camera the camera object used for the video source |
| 407 | * @param cameraId if camera == 0, use camera with this id |
| 408 | * as the video source |
| 409 | * @param videoSize the target video frame size. If both |
| 410 | * width and height in videoSize is -1, use the current |
| 411 | * width and heigth settings by the camera |
| 412 | * @param frameRate the target frame rate in frames per second. |
| 413 | * if it is -1, use the current camera frame rate setting. |
| 414 | * @return OK if no error. |
| 415 | */ |
| 416 | status_t CameraSource::init( |
| 417 | const sp<ICamera>& camera, |
| 418 | int32_t cameraId, |
| 419 | Size videoSize, |
| 420 | int32_t frameRate) { |
| 421 | |
| 422 | status_t err = OK; |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 423 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 424 | |
| 425 | if ((err = isCameraAvailable(camera, cameraId)) != OK) { |
| 426 | return err; |
| 427 | } |
| 428 | CameraParameters params(mCamera->getParameters()); |
| 429 | if ((err = isCameraColorFormatSupported(params)) != OK) { |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | // Set the camera to use the requested video frame size |
| 434 | // and/or frame rate. |
| 435 | if ((err = configureCamera(¶ms, |
| 436 | videoSize.width, videoSize.height, |
| 437 | frameRate))) { |
| 438 | return err; |
| 439 | } |
| 440 | |
| 441 | // Check on video frame size and frame rate. |
| 442 | CameraParameters newCameraParams(mCamera->getParameters()); |
| 443 | if ((err = checkVideoSize(newCameraParams, |
| 444 | videoSize.width, videoSize.height)) != OK) { |
| 445 | return err; |
| 446 | } |
| 447 | if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) { |
| 448 | return err; |
| 449 | } |
| 450 | |
| 451 | // This CHECK is good, since we just passed the lock/unlock |
| 452 | // check earlier by calling mCamera->setParameters(). |
| 453 | CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface)); |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 454 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 455 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 456 | int64_t glitchDurationUs = (1000000LL / mVideoFrameRate); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 457 | if (glitchDurationUs > mGlitchDurationThresholdUs) { |
| 458 | mGlitchDurationThresholdUs = glitchDurationUs; |
| 459 | } |
| 460 | |
James Dong | ddcc4a6 | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 461 | // XXX: query camera for the stride and slice height |
| 462 | // when the capability becomes available. |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 463 | mMeta = new MetaData; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 464 | mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); |
| 465 | mMeta->setInt32(kKeyColorFormat, mColorFormat); |
| 466 | mMeta->setInt32(kKeyWidth, mVideoSize.width); |
| 467 | mMeta->setInt32(kKeyHeight, mVideoSize.height); |
| 468 | mMeta->setInt32(kKeyStride, mVideoSize.width); |
| 469 | mMeta->setInt32(kKeySliceHeight, mVideoSize.height); |
| 470 | return OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | CameraSource::~CameraSource() { |
| 474 | if (mStarted) { |
| 475 | stop(); |
| 476 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 479 | void CameraSource::startCameraRecording() { |
| 480 | CHECK_EQ(OK, mCamera->startRecording()); |
| 481 | } |
| 482 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 483 | status_t CameraSource::start(MetaData *meta) { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 484 | CHECK(!mStarted); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 485 | if (mInitCheck != OK) { |
| 486 | LOGE("CameraSource is not initialized yet"); |
| 487 | return mInitCheck; |
| 488 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 489 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 490 | char value[PROPERTY_VALUE_MAX]; |
| 491 | if (property_get("media.stagefright.record-stats", value, NULL) |
| 492 | && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { |
| 493 | mCollectStats = true; |
| 494 | } |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 495 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 496 | mStartTimeUs = 0; |
| 497 | int64_t startTimeUs; |
| 498 | if (meta && meta->findInt64(kKeyTime, &startTimeUs)) { |
| 499 | mStartTimeUs = startTimeUs; |
| 500 | } |
| 501 | |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 502 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 503 | mCamera->setListener(new CameraSourceListener(this)); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 504 | startCameraRecording(); |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 505 | IPCThreadState::self()->restoreCallingIdentity(token); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 506 | |
| 507 | mStarted = true; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 508 | return OK; |
| 509 | } |
| 510 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 511 | void CameraSource::stopCameraRecording() { |
Nipun Kwatra | 78eff72 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 512 | mCamera->setListener(NULL); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 513 | mCamera->stopRecording(); |
| 514 | } |
| 515 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 516 | status_t CameraSource::stop() { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 517 | LOGV("stop"); |
| 518 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 519 | mStarted = false; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 520 | mFrameAvailableCondition.signal(); |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 521 | |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 522 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 523 | stopCameraRecording(); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 524 | releaseQueuedFrames(); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 525 | while (!mFramesBeingEncoded.empty()) { |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 526 | LOGI("Waiting for outstanding frames being encoded: %d", |
| 527 | mFramesBeingEncoded.size()); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 528 | mFrameCompleteCondition.wait(mLock); |
| 529 | } |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame^] | 530 | |
| 531 | LOGV("Disconnect camera"); |
| 532 | if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) { |
| 533 | LOGV("Camera was cold when we started, stopping preview"); |
| 534 | mCamera->stopPreview(); |
| 535 | } |
| 536 | mCamera->unlock(); |
| 537 | mCamera.clear(); |
| 538 | mCamera = 0; |
| 539 | mCameraFlags = 0; |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 540 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 541 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 542 | if (mCollectStats) { |
| 543 | LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us", |
| 544 | mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped, |
| 545 | mLastFrameTimestampUs - mFirstFrameTimeUs); |
| 546 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 547 | |
| 548 | CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 549 | return OK; |
| 550 | } |
| 551 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 552 | void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) { |
| 553 | mCamera->releaseRecordingFrame(frame); |
| 554 | } |
| 555 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 556 | void CameraSource::releaseQueuedFrames() { |
| 557 | List<sp<IMemory> >::iterator it; |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 558 | while (!mFramesReceived.empty()) { |
| 559 | it = mFramesReceived.begin(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 560 | releaseRecordingFrame(*it); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 561 | mFramesReceived.erase(it); |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 562 | ++mNumFramesDropped; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 566 | sp<MetaData> CameraSource::getFormat() { |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 567 | return mMeta; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 568 | } |
| 569 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 570 | void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) { |
| 571 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 572 | releaseRecordingFrame(frame); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 573 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 574 | } |
| 575 | |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 576 | void CameraSource::signalBufferReturned(MediaBuffer *buffer) { |
| 577 | LOGV("signalBufferReturned: %p", buffer->data()); |
Andreas Huber | 56223b9 | 2010-08-11 12:34:32 -0700 | [diff] [blame] | 578 | Mutex::Autolock autoLock(mLock); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 579 | for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); |
| 580 | it != mFramesBeingEncoded.end(); ++it) { |
| 581 | if ((*it)->pointer() == buffer->data()) { |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 582 | releaseOneRecordingFrame((*it)); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 583 | mFramesBeingEncoded.erase(it); |
| 584 | ++mNumFramesEncoded; |
| 585 | buffer->setObserver(0); |
| 586 | buffer->release(); |
| 587 | mFrameCompleteCondition.signal(); |
| 588 | return; |
| 589 | } |
| 590 | } |
| 591 | CHECK_EQ(0, "signalBufferReturned: bogus buffer"); |
| 592 | } |
| 593 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 594 | status_t CameraSource::read( |
| 595 | MediaBuffer **buffer, const ReadOptions *options) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 596 | LOGV("read"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 597 | |
| 598 | *buffer = NULL; |
| 599 | |
| 600 | int64_t seekTimeUs; |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 601 | ReadOptions::SeekMode mode; |
| 602 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 603 | return ERROR_UNSUPPORTED; |
| 604 | } |
| 605 | |
| 606 | sp<IMemory> frame; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 607 | int64_t frameTime; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 608 | |
| 609 | { |
| 610 | Mutex::Autolock autoLock(mLock); |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 611 | while (mStarted) { |
| 612 | while(mFramesReceived.empty()) { |
| 613 | mFrameAvailableCondition.wait(mLock); |
| 614 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 615 | |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 616 | if (!mStarted) { |
| 617 | return OK; |
| 618 | } |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 619 | |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 620 | frame = *mFramesReceived.begin(); |
| 621 | mFramesReceived.erase(mFramesReceived.begin()); |
| 622 | |
| 623 | frameTime = *mFrameTimes.begin(); |
| 624 | mFrameTimes.erase(mFrameTimes.begin()); |
| 625 | int64_t skipTimeUs; |
| 626 | if (!options || !options->getSkipFrame(&skipTimeUs)) { |
| 627 | skipTimeUs = frameTime; |
| 628 | } |
| 629 | if (skipTimeUs > frameTime) { |
| 630 | LOGV("skipTimeUs: %lld us > frameTime: %lld us", |
| 631 | skipTimeUs, frameTime); |
| 632 | releaseOneRecordingFrame(frame); |
| 633 | ++mNumFramesDropped; |
| 634 | // Safeguard against the abuse of the kSkipFrame_Option. |
| 635 | if (skipTimeUs - frameTime >= 1E6) { |
| 636 | LOGE("Frame skipping requested is way too long: %lld us", |
| 637 | skipTimeUs - frameTime); |
| 638 | return UNKNOWN_ERROR; |
| 639 | } |
| 640 | } else { |
| 641 | mFramesBeingEncoded.push_back(frame); |
| 642 | *buffer = new MediaBuffer(frame->pointer(), frame->size()); |
| 643 | (*buffer)->setObserver(this); |
| 644 | (*buffer)->add_ref(); |
| 645 | (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); |
Andreas Huber | 56223b9 | 2010-08-11 12:34:32 -0700 | [diff] [blame] | 646 | |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 647 | return OK; |
| 648 | } |
| 649 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 650 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 651 | return OK; |
| 652 | } |
| 653 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 654 | void CameraSource::dataCallbackTimestamp(int64_t timestampUs, |
| 655 | int32_t msgType, const sp<IMemory> &data) { |
| 656 | LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 657 | Mutex::Autolock autoLock(mLock); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 658 | if (!mStarted) { |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 659 | releaseOneRecordingFrame(data); |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 660 | ++mNumFramesReceived; |
| 661 | ++mNumFramesDropped; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 662 | return; |
| 663 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 664 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 665 | if (mNumFramesReceived > 0 && |
| 666 | timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { |
| 667 | if (mNumGlitches % 10 == 0) { // Don't spam the log |
| 668 | LOGW("Long delay detected in video recording"); |
| 669 | } |
| 670 | ++mNumGlitches; |
| 671 | } |
| 672 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 673 | // May need to skip frame or modify timestamp. Currently implemented |
| 674 | // by the subclass CameraSourceTimeLapse. |
| 675 | if(skipCurrentFrame(timestampUs)) { |
| 676 | releaseOneRecordingFrame(data); |
| 677 | return; |
Nipun Kwatra | fc20aab | 2010-06-30 18:51:31 -0700 | [diff] [blame] | 678 | } |
| 679 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 680 | mLastFrameTimestampUs = timestampUs; |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 681 | if (mNumFramesReceived == 0) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 682 | mFirstFrameTimeUs = timestampUs; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 683 | // Initial delay |
| 684 | if (mStartTimeUs > 0) { |
| 685 | if (timestampUs < mStartTimeUs) { |
| 686 | // Frame was captured before recording was started |
| 687 | // Drop it without updating the statistical data. |
| 688 | releaseOneRecordingFrame(data); |
| 689 | return; |
| 690 | } |
| 691 | mStartTimeUs = timestampUs - mStartTimeUs; |
| 692 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 693 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 694 | ++mNumFramesReceived; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 695 | |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 696 | mFramesReceived.push_back(data); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 697 | int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); |
| 698 | mFrameTimes.push_back(timeUs); |
| 699 | LOGV("initial delay: %lld, current time stamp: %lld", |
| 700 | mStartTimeUs, timeUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 701 | mFrameAvailableCondition.signal(); |
| 702 | } |
| 703 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 704 | } // namespace android |