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; |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 118 | return new CameraSource(camera, NULL, 0, size, -1, NULL, false); |
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, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 124 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 125 | int32_t cameraId, |
| 126 | Size videoSize, |
| 127 | int32_t frameRate, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 128 | const sp<Surface>& surface, |
| 129 | bool storeMetaDataInVideoBuffers) { |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 130 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 131 | CameraSource *source = new CameraSource(camera, proxy, cameraId, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 132 | videoSize, frameRate, surface, |
| 133 | storeMetaDataInVideoBuffers); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 134 | return source; |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 135 | } |
| 136 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 137 | CameraSource::CameraSource( |
| 138 | const sp<ICamera>& camera, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 139 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 140 | int32_t cameraId, |
| 141 | Size videoSize, |
| 142 | int32_t frameRate, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 143 | const sp<Surface>& surface, |
| 144 | bool storeMetaDataInVideoBuffers) |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 145 | : mCameraFlags(0), |
| 146 | mVideoFrameRate(-1), |
| 147 | mCamera(0), |
| 148 | mSurface(surface), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 149 | mNumFramesReceived(0), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 150 | mLastFrameTimestampUs(0), |
| 151 | mStarted(false), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 152 | mNumFramesEncoded(0), |
James Dong | 7757f50 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 153 | mFirstFrameTimeUs(0), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 154 | mNumFramesDropped(0), |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 155 | mNumGlitches(0), |
| 156 | mGlitchDurationThresholdUs(200000), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 157 | mCollectStats(false) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 158 | mVideoSize.width = -1; |
| 159 | mVideoSize.height = -1; |
| 160 | |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame^] | 161 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 162 | mInitCheck = init(camera, proxy, cameraId, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 163 | videoSize, frameRate, |
| 164 | storeMetaDataInVideoBuffers); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame^] | 165 | if (mInitCheck != OK) releaseCamera(); |
| 166 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | status_t CameraSource::initCheck() const { |
| 170 | return mInitCheck; |
| 171 | } |
| 172 | |
| 173 | status_t CameraSource::isCameraAvailable( |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 174 | const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, |
| 175 | int32_t cameraId) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 176 | |
| 177 | if (camera == 0) { |
| 178 | mCamera = Camera::connect(cameraId); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 179 | if (mCamera == 0) return -EBUSY; |
| 180 | // If proxy is not passed in by applications, still use the proxy of |
| 181 | // our own Camera to simplify the code. |
| 182 | mCameraRecordingProxy = mCamera->getRecordingProxy(); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 183 | mCameraFlags &= ~FLAGS_HOT_CAMERA; |
| 184 | } else { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 185 | // We get the proxy from Camera, not ICamera. We need to get the proxy |
| 186 | // to the remote Camera owned by the application. Here mCamera is a |
| 187 | // local Camera object created by us. We cannot use the proxy from |
| 188 | // mCamera here. |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 189 | mCamera = Camera::create(camera); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 190 | if (mCamera == 0) return -EBUSY; |
| 191 | mCameraRecordingProxy = proxy; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 192 | mCameraFlags |= FLAGS_HOT_CAMERA; |
| 193 | } |
| 194 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 195 | mCamera->lock(); |
| 196 | mDeathNotifier = new DeathNotifier(); |
| 197 | // isBinderAlive needs linkToDeath to work. |
| 198 | mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier); |
| 199 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* |
| 205 | * Check to see whether the requested video width and height is one |
| 206 | * of the supported sizes. |
| 207 | * @param width the video frame width in pixels |
| 208 | * @param height the video frame height in pixels |
| 209 | * @param suppportedSizes the vector of sizes that we check against |
| 210 | * @return true if the dimension (width and height) is supported. |
| 211 | */ |
| 212 | static bool isVideoSizeSupported( |
| 213 | int32_t width, int32_t height, |
| 214 | const Vector<Size>& supportedSizes) { |
| 215 | |
| 216 | LOGV("isVideoSizeSupported"); |
| 217 | for (size_t i = 0; i < supportedSizes.size(); ++i) { |
| 218 | if (width == supportedSizes[i].width && |
| 219 | height == supportedSizes[i].height) { |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * If the preview and video output is separate, we only set the |
| 228 | * the video size, and applications should set the preview size |
| 229 | * to some proper value, and the recording framework will not |
| 230 | * change the preview size; otherwise, if the video and preview |
| 231 | * output is the same, we need to set the preview to be the same |
| 232 | * as the requested video size. |
| 233 | * |
| 234 | */ |
| 235 | /* |
| 236 | * Query the camera to retrieve the supported video frame sizes |
| 237 | * and also to see whether CameraParameters::setVideoSize() |
| 238 | * is supported or not. |
| 239 | * @param params CameraParameters to retrieve the information |
| 240 | * @@param isSetVideoSizeSupported retunrs whether method |
| 241 | * CameraParameters::setVideoSize() is supported or not. |
| 242 | * @param sizes returns the vector of Size objects for the |
| 243 | * supported video frame sizes advertised by the camera. |
| 244 | */ |
| 245 | static void getSupportedVideoSizes( |
| 246 | const CameraParameters& params, |
| 247 | bool *isSetVideoSizeSupported, |
| 248 | Vector<Size>& sizes) { |
| 249 | |
| 250 | *isSetVideoSizeSupported = true; |
| 251 | params.getSupportedVideoSizes(sizes); |
| 252 | if (sizes.size() == 0) { |
| 253 | LOGD("Camera does not support setVideoSize()"); |
| 254 | params.getSupportedPreviewSizes(sizes); |
| 255 | *isSetVideoSizeSupported = false; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Check whether the camera has the supported color format |
| 261 | * @param params CameraParameters to retrieve the information |
| 262 | * @return OK if no error. |
| 263 | */ |
| 264 | status_t CameraSource::isCameraColorFormatSupported( |
| 265 | const CameraParameters& params) { |
| 266 | mColorFormat = getColorFormat(params.get( |
| 267 | CameraParameters::KEY_VIDEO_FRAME_FORMAT)); |
| 268 | if (mColorFormat == -1) { |
| 269 | return BAD_VALUE; |
| 270 | } |
| 271 | return OK; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Configure the camera to use the requested video size |
| 276 | * (width and height) and/or frame rate. If both width and |
| 277 | * height are -1, configuration on the video size is skipped. |
| 278 | * if frameRate is -1, configuration on the frame rate |
| 279 | * is skipped. Skipping the configuration allows one to |
| 280 | * use the current camera setting without the need to |
| 281 | * actually know the specific values (see Create() method). |
| 282 | * |
| 283 | * @param params the CameraParameters to be configured |
| 284 | * @param width the target video frame width in pixels |
| 285 | * @param height the target video frame height in pixels |
| 286 | * @param frameRate the target frame rate in frames per second. |
| 287 | * @return OK if no error. |
| 288 | */ |
| 289 | status_t CameraSource::configureCamera( |
| 290 | CameraParameters* params, |
| 291 | int32_t width, int32_t height, |
| 292 | int32_t frameRate) { |
| 293 | |
| 294 | Vector<Size> sizes; |
| 295 | bool isSetVideoSizeSupportedByCamera = true; |
| 296 | getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes); |
| 297 | bool isCameraParamChanged = false; |
| 298 | if (width != -1 && height != -1) { |
| 299 | if (!isVideoSizeSupported(width, height, sizes)) { |
| 300 | LOGE("Video dimension (%dx%d) is unsupported", width, height); |
| 301 | return BAD_VALUE; |
| 302 | } |
| 303 | if (isSetVideoSizeSupportedByCamera) { |
| 304 | params->setVideoSize(width, height); |
| 305 | } else { |
| 306 | params->setPreviewSize(width, height); |
| 307 | } |
| 308 | isCameraParamChanged = true; |
| 309 | } else if ((width == -1 && height != -1) || |
| 310 | (width != -1 && height == -1)) { |
| 311 | // If one and only one of the width and height is -1 |
| 312 | // we reject such a request. |
| 313 | LOGE("Requested video size (%dx%d) is not supported", width, height); |
| 314 | return BAD_VALUE; |
| 315 | } else { // width == -1 && height == -1 |
| 316 | // Do not configure the camera. |
| 317 | // Use the current width and height value setting from the camera. |
| 318 | } |
| 319 | |
| 320 | if (frameRate != -1) { |
James Dong | 6357308 | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 321 | CHECK(frameRate > 0 && frameRate <= 120); |
| 322 | const char* supportedFrameRates = |
| 323 | params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES); |
| 324 | CHECK(supportedFrameRates != NULL); |
| 325 | LOGV("Supported frame rates: %s", supportedFrameRates); |
| 326 | char buf[4]; |
| 327 | snprintf(buf, 4, "%d", frameRate); |
| 328 | if (strstr(supportedFrameRates, buf) == NULL) { |
| 329 | LOGE("Requested frame rate (%d) is not supported: %s", |
| 330 | frameRate, supportedFrameRates); |
| 331 | return BAD_VALUE; |
| 332 | } |
| 333 | |
| 334 | // The frame rate is supported, set the camera to the requested value. |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 335 | 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 | */ |
| 365 | status_t CameraSource::checkVideoSize( |
| 366 | const CameraParameters& params, |
| 367 | int32_t width, int32_t height) { |
| 368 | |
James Dong | f96c9d1 | 2010-10-20 11:41:33 -0700 | [diff] [blame] | 369 | // 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 Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 373 | int32_t frameWidthActual = -1; |
| 374 | int32_t frameHeightActual = -1; |
James Dong | f96c9d1 | 2010-10-20 11:41:33 -0700 | [diff] [blame] | 375 | 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 Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 384 | 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 | */ |
| 416 | status_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 Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 450 | * @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 Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 455 | * @return OK if no error. |
| 456 | */ |
| 457 | status_t CameraSource::init( |
| 458 | const sp<ICamera>& camera, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 459 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 460 | int32_t cameraId, |
| 461 | Size videoSize, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 462 | int32_t frameRate, |
| 463 | bool storeMetaDataInVideoBuffers) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 464 | |
| 465 | status_t err = OK; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 466 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 467 | if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) { |
| 468 | LOGE("Camera connection could not be established."); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | CameraParameters params(mCamera->getParameters()); |
| 472 | if ((err = isCameraColorFormatSupported(params)) != OK) { |
| 473 | return err; |
| 474 | } |
| 475 | |
| 476 | // Set the camera to use the requested video frame size |
| 477 | // and/or frame rate. |
| 478 | if ((err = configureCamera(¶ms, |
| 479 | videoSize.width, videoSize.height, |
| 480 | frameRate))) { |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | // Check on video frame size and frame rate. |
| 485 | CameraParameters newCameraParams(mCamera->getParameters()); |
| 486 | if ((err = checkVideoSize(newCameraParams, |
| 487 | videoSize.width, videoSize.height)) != OK) { |
| 488 | return err; |
| 489 | } |
| 490 | if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) { |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | // This CHECK is good, since we just passed the lock/unlock |
| 495 | // check earlier by calling mCamera->setParameters(). |
| 496 | CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface)); |
James Dong | 2b37ced | 2010-10-09 01:16:58 -0700 | [diff] [blame] | 497 | |
James Dong | abdd2ba | 2010-12-10 13:09:05 -0800 | [diff] [blame] | 498 | // By default, do not store metadata in video buffers |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 499 | mIsMetaDataStoredInVideoBuffers = false; |
James Dong | abdd2ba | 2010-12-10 13:09:05 -0800 | [diff] [blame] | 500 | mCamera->storeMetaDataInBuffers(false); |
| 501 | if (storeMetaDataInVideoBuffers) { |
| 502 | if (OK == mCamera->storeMetaDataInBuffers(true)) { |
| 503 | mIsMetaDataStoredInVideoBuffers = true; |
| 504 | } |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 505 | } |
| 506 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 507 | int64_t glitchDurationUs = (1000000LL / mVideoFrameRate); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 508 | if (glitchDurationUs > mGlitchDurationThresholdUs) { |
| 509 | mGlitchDurationThresholdUs = glitchDurationUs; |
| 510 | } |
| 511 | |
James Dong | ddcc4a6 | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 512 | // XXX: query camera for the stride and slice height |
| 513 | // when the capability becomes available. |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 514 | mMeta = new MetaData; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 515 | mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); |
| 516 | mMeta->setInt32(kKeyColorFormat, mColorFormat); |
| 517 | mMeta->setInt32(kKeyWidth, mVideoSize.width); |
| 518 | mMeta->setInt32(kKeyHeight, mVideoSize.height); |
| 519 | mMeta->setInt32(kKeyStride, mVideoSize.width); |
| 520 | mMeta->setInt32(kKeySliceHeight, mVideoSize.height); |
James Dong | 393410a | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 521 | mMeta->setInt32(kKeyFrameRate, mVideoFrameRate); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 522 | return OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | CameraSource::~CameraSource() { |
| 526 | if (mStarted) { |
| 527 | stop(); |
| 528 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 531 | void CameraSource::startCameraRecording() { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 532 | // Reset the identity to the current thread because media server owns the |
| 533 | // camera and recording is started by the applications. The applications |
| 534 | // will connect to the camera in ICameraRecordingProxy::startRecording. |
| 535 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 536 | mCamera->unlock(); |
| 537 | mCamera.clear(); |
| 538 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 539 | CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this))); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 540 | } |
| 541 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 542 | status_t CameraSource::start(MetaData *meta) { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 543 | CHECK(!mStarted); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 544 | if (mInitCheck != OK) { |
| 545 | LOGE("CameraSource is not initialized yet"); |
| 546 | return mInitCheck; |
| 547 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 548 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 549 | char value[PROPERTY_VALUE_MAX]; |
| 550 | if (property_get("media.stagefright.record-stats", value, NULL) |
| 551 | && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { |
| 552 | mCollectStats = true; |
| 553 | } |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 554 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 555 | mStartTimeUs = 0; |
| 556 | int64_t startTimeUs; |
| 557 | if (meta && meta->findInt64(kKeyTime, &startTimeUs)) { |
| 558 | mStartTimeUs = startTimeUs; |
| 559 | } |
| 560 | |
James Dong | c42478e | 2010-11-15 10:38:37 -0800 | [diff] [blame] | 561 | startCameraRecording(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 562 | |
| 563 | mStarted = true; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 564 | return OK; |
| 565 | } |
| 566 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 567 | void CameraSource::stopCameraRecording() { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 568 | mCameraRecordingProxy->stopRecording(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 569 | } |
| 570 | |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 571 | void CameraSource::releaseCamera() { |
| 572 | LOGV("releaseCamera"); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame^] | 573 | if (mCamera != 0) { |
| 574 | if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) { |
| 575 | LOGV("Camera was cold when we started, stopping preview"); |
| 576 | mCamera->stopPreview(); |
| 577 | mCamera->disconnect(); |
| 578 | } else { |
| 579 | // Unlock the camera so the application can lock it back. |
| 580 | mCamera->unlock(); |
| 581 | } |
| 582 | mCamera.clear(); |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 583 | } |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame^] | 584 | if (mCameraRecordingProxy != 0) { |
| 585 | mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier); |
| 586 | mCameraRecordingProxy.clear(); |
| 587 | } |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 588 | mCameraFlags = 0; |
| 589 | } |
| 590 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 591 | status_t CameraSource::stop() { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 592 | LOGD("stop: E"); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 593 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 594 | mStarted = false; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 595 | mFrameAvailableCondition.signal(); |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 596 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 597 | releaseQueuedFrames(); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 598 | while (!mFramesBeingEncoded.empty()) { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 599 | if (NO_ERROR != |
| 600 | mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) { |
| 601 | LOGW("Timed out waiting for outstanding frames being encoded: %d", |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 602 | mFramesBeingEncoded.size()); |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 603 | } |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 604 | } |
James Dong | d69c7f6 | 2010-12-09 11:24:22 -0800 | [diff] [blame] | 605 | stopCameraRecording(); |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 606 | releaseCamera(); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 607 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 608 | if (mCollectStats) { |
| 609 | LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us", |
| 610 | mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped, |
| 611 | mLastFrameTimestampUs - mFirstFrameTimeUs); |
| 612 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 613 | |
James Dong | ba29002 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 614 | if (mNumGlitches > 0) { |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 615 | LOGW("%d long delays between neighboring video frames", mNumGlitches); |
James Dong | ba29002 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 616 | } |
| 617 | |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 618 | CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped); |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 619 | LOGD("stop: X"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 620 | return OK; |
| 621 | } |
| 622 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 623 | void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 624 | if (mCameraRecordingProxy != NULL) { |
| 625 | mCameraRecordingProxy->releaseRecordingFrame(frame); |
James Dong | d69c7f6 | 2010-12-09 11:24:22 -0800 | [diff] [blame] | 626 | } |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 627 | } |
| 628 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 629 | void CameraSource::releaseQueuedFrames() { |
| 630 | List<sp<IMemory> >::iterator it; |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 631 | while (!mFramesReceived.empty()) { |
| 632 | it = mFramesReceived.begin(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 633 | releaseRecordingFrame(*it); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 634 | mFramesReceived.erase(it); |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 635 | ++mNumFramesDropped; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 639 | sp<MetaData> CameraSource::getFormat() { |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 640 | return mMeta; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 641 | } |
| 642 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 643 | void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) { |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 644 | releaseRecordingFrame(frame); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 645 | } |
| 646 | |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 647 | void CameraSource::signalBufferReturned(MediaBuffer *buffer) { |
| 648 | LOGV("signalBufferReturned: %p", buffer->data()); |
Andreas Huber | 56223b9 | 2010-08-11 12:34:32 -0700 | [diff] [blame] | 649 | Mutex::Autolock autoLock(mLock); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 650 | for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); |
| 651 | it != mFramesBeingEncoded.end(); ++it) { |
| 652 | if ((*it)->pointer() == buffer->data()) { |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 653 | releaseOneRecordingFrame((*it)); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 654 | mFramesBeingEncoded.erase(it); |
| 655 | ++mNumFramesEncoded; |
| 656 | buffer->setObserver(0); |
| 657 | buffer->release(); |
| 658 | mFrameCompleteCondition.signal(); |
| 659 | return; |
| 660 | } |
| 661 | } |
| 662 | CHECK_EQ(0, "signalBufferReturned: bogus buffer"); |
| 663 | } |
| 664 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 665 | status_t CameraSource::read( |
| 666 | MediaBuffer **buffer, const ReadOptions *options) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 667 | LOGV("read"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 668 | |
| 669 | *buffer = NULL; |
| 670 | |
| 671 | int64_t seekTimeUs; |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 672 | ReadOptions::SeekMode mode; |
| 673 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 674 | return ERROR_UNSUPPORTED; |
| 675 | } |
| 676 | |
| 677 | sp<IMemory> frame; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 678 | int64_t frameTime; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 679 | |
| 680 | { |
| 681 | Mutex::Autolock autoLock(mLock); |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 682 | while (mStarted && mFramesReceived.empty()) { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 683 | if (NO_ERROR != |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 684 | mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) { |
| 685 | if (!mCameraRecordingProxy->asBinder()->isBinderAlive()) { |
| 686 | LOGW("camera recording proxy is gone"); |
| 687 | return ERROR_END_OF_STREAM; |
| 688 | } |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 689 | LOGW("Timed out waiting for incoming camera video frames: %lld us", |
| 690 | mLastFrameTimestampUs); |
| 691 | } |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 692 | } |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 693 | if (!mStarted) { |
| 694 | return OK; |
| 695 | } |
| 696 | frame = *mFramesReceived.begin(); |
| 697 | mFramesReceived.erase(mFramesReceived.begin()); |
| 698 | |
| 699 | frameTime = *mFrameTimes.begin(); |
| 700 | mFrameTimes.erase(mFrameTimes.begin()); |
| 701 | mFramesBeingEncoded.push_back(frame); |
| 702 | *buffer = new MediaBuffer(frame->pointer(), frame->size()); |
| 703 | (*buffer)->setObserver(this); |
| 704 | (*buffer)->add_ref(); |
| 705 | (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 706 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 707 | return OK; |
| 708 | } |
| 709 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 710 | void CameraSource::dataCallbackTimestamp(int64_t timestampUs, |
| 711 | int32_t msgType, const sp<IMemory> &data) { |
| 712 | LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 713 | Mutex::Autolock autoLock(mLock); |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 714 | if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) { |
| 715 | LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 716 | releaseOneRecordingFrame(data); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 717 | return; |
| 718 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 719 | |
James Dong | 98cfde0 | 2011-06-03 17:21:16 -0700 | [diff] [blame] | 720 | if (mNumFramesReceived > 0) { |
| 721 | CHECK(timestampUs > mLastFrameTimestampUs); |
| 722 | if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { |
| 723 | ++mNumGlitches; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 724 | } |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 727 | // May need to skip frame or modify timestamp. Currently implemented |
| 728 | // by the subclass CameraSourceTimeLapse. |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 729 | if (skipCurrentFrame(timestampUs)) { |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 730 | releaseOneRecordingFrame(data); |
| 731 | return; |
Nipun Kwatra | fc20aab | 2010-06-30 18:51:31 -0700 | [diff] [blame] | 732 | } |
| 733 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 734 | mLastFrameTimestampUs = timestampUs; |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 735 | if (mNumFramesReceived == 0) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 736 | mFirstFrameTimeUs = timestampUs; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 737 | // Initial delay |
| 738 | if (mStartTimeUs > 0) { |
| 739 | if (timestampUs < mStartTimeUs) { |
| 740 | // Frame was captured before recording was started |
| 741 | // Drop it without updating the statistical data. |
| 742 | releaseOneRecordingFrame(data); |
| 743 | return; |
| 744 | } |
| 745 | mStartTimeUs = timestampUs - mStartTimeUs; |
| 746 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 747 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 748 | ++mNumFramesReceived; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 749 | |
James Dong | 98cfde0 | 2011-06-03 17:21:16 -0700 | [diff] [blame] | 750 | CHECK(data != NULL && data->size() > 0); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 751 | mFramesReceived.push_back(data); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 752 | int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); |
| 753 | mFrameTimes.push_back(timeUs); |
| 754 | LOGV("initial delay: %lld, current time stamp: %lld", |
| 755 | mStartTimeUs, timeUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 756 | mFrameAvailableCondition.signal(); |
| 757 | } |
| 758 | |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 759 | bool CameraSource::isMetaDataStoredInVideoBuffers() const { |
| 760 | LOGV("isMetaDataStoredInVideoBuffers"); |
| 761 | return mIsMetaDataStoredInVideoBuffers; |
| 762 | } |
| 763 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 764 | CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) { |
| 765 | mSource = source; |
| 766 | } |
| 767 | |
| 768 | void CameraSource::ProxyListener::dataCallbackTimestamp( |
| 769 | nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { |
| 770 | mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr); |
| 771 | } |
| 772 | |
| 773 | void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) { |
| 774 | LOGI("Camera recording proxy died"); |
| 775 | } |
| 776 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 777 | } // namespace android |