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); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 40 | virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr, |
| 41 | camera_frame_metadata_t *metadata); |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 42 | |
| 43 | virtual void postDataTimestamp( |
| 44 | nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
| 45 | |
| 46 | protected: |
| 47 | virtual ~CameraSourceListener(); |
| 48 | |
| 49 | private: |
| 50 | wp<CameraSource> mSource; |
| 51 | |
| 52 | CameraSourceListener(const CameraSourceListener &); |
| 53 | CameraSourceListener &operator=(const CameraSourceListener &); |
| 54 | }; |
| 55 | |
| 56 | CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source) |
| 57 | : mSource(source) { |
| 58 | } |
| 59 | |
| 60 | CameraSourceListener::~CameraSourceListener() { |
| 61 | } |
| 62 | |
| 63 | void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) { |
| 64 | LOGV("notify(%d, %d, %d)", msgType, ext1, ext2); |
| 65 | } |
| 66 | |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 67 | void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr, |
| 68 | camera_frame_metadata_t *metadata) { |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 69 | LOGV("postData(%d, ptr:%p, size:%d)", |
| 70 | msgType, dataPtr->pointer(), dataPtr->size()); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 71 | |
| 72 | sp<CameraSource> source = mSource.promote(); |
| 73 | if (source.get() != NULL) { |
| 74 | source->dataCallback(msgType, dataPtr); |
| 75 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void CameraSourceListener::postDataTimestamp( |
| 79 | nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 80 | |
| 81 | sp<CameraSource> source = mSource.promote(); |
| 82 | if (source.get() != NULL) { |
| 83 | source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr); |
| 84 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 85 | } |
| 86 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 87 | static int32_t getColorFormat(const char* colorFormat) { |
James Dong | e2d8ba8 | 2010-09-15 16:52:51 -0700 | [diff] [blame] | 88 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) { |
| 89 | return OMX_COLOR_FormatYUV420Planar; |
| 90 | } |
| 91 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 92 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) { |
| 93 | return OMX_COLOR_FormatYUV422SemiPlanar; |
| 94 | } |
| 95 | |
| 96 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) { |
| 97 | return OMX_COLOR_FormatYUV420SemiPlanar; |
| 98 | } |
| 99 | |
| 100 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) { |
| 101 | return OMX_COLOR_FormatYCbYCr; |
| 102 | } |
| 103 | |
| 104 | if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) { |
| 105 | return OMX_COLOR_Format16bitRGB565; |
| 106 | } |
| 107 | |
Dandawate Saket | 1374edd | 2011-07-11 19:12:57 -0700 | [diff] [blame] | 108 | if (!strcmp(colorFormat, "OMX_TI_COLOR_FormatYUV420PackedSemiPlanar")) { |
| 109 | return OMX_TI_COLOR_FormatYUV420PackedSemiPlanar; |
| 110 | } |
| 111 | |
James Dong | a1abc1a | 2010-09-13 16:30:51 -0700 | [diff] [blame] | 112 | LOGE("Uknown color format (%s), please add it to " |
| 113 | "CameraSource::getColorFormat", colorFormat); |
| 114 | |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 115 | CHECK_EQ(0, "Unknown color format"); |
| 116 | } |
| 117 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 118 | CameraSource *CameraSource::Create() { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 119 | Size size; |
| 120 | size.width = -1; |
| 121 | size.height = -1; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 122 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 123 | sp<ICamera> camera; |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 124 | return new CameraSource(camera, NULL, 0, size, -1, NULL, false); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 127 | // static |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 128 | CameraSource *CameraSource::CreateFromCamera( |
| 129 | const sp<ICamera>& camera, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 130 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 131 | int32_t cameraId, |
| 132 | Size videoSize, |
| 133 | int32_t frameRate, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 134 | const sp<Surface>& surface, |
| 135 | bool storeMetaDataInVideoBuffers) { |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 136 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 137 | CameraSource *source = new CameraSource(camera, proxy, cameraId, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 138 | videoSize, frameRate, surface, |
| 139 | storeMetaDataInVideoBuffers); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 140 | return source; |
Andreas Huber | 30ab662 | 2009-11-16 15:43:38 -0800 | [diff] [blame] | 141 | } |
| 142 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 143 | CameraSource::CameraSource( |
| 144 | const sp<ICamera>& camera, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 145 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 146 | int32_t cameraId, |
| 147 | Size videoSize, |
| 148 | int32_t frameRate, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 149 | const sp<Surface>& surface, |
| 150 | bool storeMetaDataInVideoBuffers) |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 151 | : mCameraFlags(0), |
| 152 | mVideoFrameRate(-1), |
| 153 | mCamera(0), |
| 154 | mSurface(surface), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 155 | mNumFramesReceived(0), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 156 | mLastFrameTimestampUs(0), |
| 157 | mStarted(false), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 158 | mNumFramesEncoded(0), |
James Dong | 7757f50 | 2011-01-25 16:31:28 -0800 | [diff] [blame] | 159 | mFirstFrameTimeUs(0), |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 160 | mNumFramesDropped(0), |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 161 | mNumGlitches(0), |
| 162 | mGlitchDurationThresholdUs(200000), |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 163 | mCollectStats(false) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 164 | mVideoSize.width = -1; |
| 165 | mVideoSize.height = -1; |
| 166 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 167 | mInitCheck = init(camera, proxy, cameraId, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 168 | videoSize, frameRate, |
| 169 | storeMetaDataInVideoBuffers); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 170 | if (mInitCheck != OK) releaseCamera(); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | status_t CameraSource::initCheck() const { |
| 174 | return mInitCheck; |
| 175 | } |
| 176 | |
| 177 | status_t CameraSource::isCameraAvailable( |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 178 | const sp<ICamera>& camera, const sp<ICameraRecordingProxy>& proxy, |
| 179 | int32_t cameraId) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 180 | |
| 181 | if (camera == 0) { |
| 182 | mCamera = Camera::connect(cameraId); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 183 | if (mCamera == 0) return -EBUSY; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 184 | mCameraFlags &= ~FLAGS_HOT_CAMERA; |
| 185 | } else { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 186 | // We get the proxy from Camera, not ICamera. We need to get the proxy |
| 187 | // to the remote Camera owned by the application. Here mCamera is a |
| 188 | // local Camera object created by us. We cannot use the proxy from |
| 189 | // mCamera here. |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 190 | mCamera = Camera::create(camera); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 191 | if (mCamera == 0) return -EBUSY; |
| 192 | mCameraRecordingProxy = proxy; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 193 | mCameraFlags |= FLAGS_HOT_CAMERA; |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 194 | mDeathNotifier = new DeathNotifier(); |
| 195 | // isBinderAlive needs linkToDeath to work. |
| 196 | mCameraRecordingProxy->asBinder()->linkToDeath(mDeathNotifier); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 199 | mCamera->lock(); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 200 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 201 | return OK; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /* |
| 206 | * Check to see whether the requested video width and height is one |
| 207 | * of the supported sizes. |
| 208 | * @param width the video frame width in pixels |
| 209 | * @param height the video frame height in pixels |
| 210 | * @param suppportedSizes the vector of sizes that we check against |
| 211 | * @return true if the dimension (width and height) is supported. |
| 212 | */ |
| 213 | static bool isVideoSizeSupported( |
| 214 | int32_t width, int32_t height, |
| 215 | const Vector<Size>& supportedSizes) { |
| 216 | |
| 217 | LOGV("isVideoSizeSupported"); |
| 218 | for (size_t i = 0; i < supportedSizes.size(); ++i) { |
| 219 | if (width == supportedSizes[i].width && |
| 220 | height == supportedSizes[i].height) { |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * If the preview and video output is separate, we only set the |
| 229 | * the video size, and applications should set the preview size |
| 230 | * to some proper value, and the recording framework will not |
| 231 | * change the preview size; otherwise, if the video and preview |
| 232 | * output is the same, we need to set the preview to be the same |
| 233 | * as the requested video size. |
| 234 | * |
| 235 | */ |
| 236 | /* |
| 237 | * Query the camera to retrieve the supported video frame sizes |
| 238 | * and also to see whether CameraParameters::setVideoSize() |
| 239 | * is supported or not. |
| 240 | * @param params CameraParameters to retrieve the information |
| 241 | * @@param isSetVideoSizeSupported retunrs whether method |
| 242 | * CameraParameters::setVideoSize() is supported or not. |
| 243 | * @param sizes returns the vector of Size objects for the |
| 244 | * supported video frame sizes advertised by the camera. |
| 245 | */ |
| 246 | static void getSupportedVideoSizes( |
| 247 | const CameraParameters& params, |
| 248 | bool *isSetVideoSizeSupported, |
| 249 | Vector<Size>& sizes) { |
| 250 | |
| 251 | *isSetVideoSizeSupported = true; |
| 252 | params.getSupportedVideoSizes(sizes); |
| 253 | if (sizes.size() == 0) { |
| 254 | LOGD("Camera does not support setVideoSize()"); |
| 255 | params.getSupportedPreviewSizes(sizes); |
| 256 | *isSetVideoSizeSupported = false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Check whether the camera has the supported color format |
| 262 | * @param params CameraParameters to retrieve the information |
| 263 | * @return OK if no error. |
| 264 | */ |
| 265 | status_t CameraSource::isCameraColorFormatSupported( |
| 266 | const CameraParameters& params) { |
| 267 | mColorFormat = getColorFormat(params.get( |
| 268 | CameraParameters::KEY_VIDEO_FRAME_FORMAT)); |
| 269 | if (mColorFormat == -1) { |
| 270 | return BAD_VALUE; |
| 271 | } |
| 272 | return OK; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Configure the camera to use the requested video size |
| 277 | * (width and height) and/or frame rate. If both width and |
| 278 | * height are -1, configuration on the video size is skipped. |
| 279 | * if frameRate is -1, configuration on the frame rate |
| 280 | * is skipped. Skipping the configuration allows one to |
| 281 | * use the current camera setting without the need to |
| 282 | * actually know the specific values (see Create() method). |
| 283 | * |
| 284 | * @param params the CameraParameters to be configured |
| 285 | * @param width the target video frame width in pixels |
| 286 | * @param height the target video frame height in pixels |
| 287 | * @param frameRate the target frame rate in frames per second. |
| 288 | * @return OK if no error. |
| 289 | */ |
| 290 | status_t CameraSource::configureCamera( |
| 291 | CameraParameters* params, |
| 292 | int32_t width, int32_t height, |
| 293 | int32_t frameRate) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 294 | LOGV("configureCamera"); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 295 | Vector<Size> sizes; |
| 296 | bool isSetVideoSizeSupportedByCamera = true; |
| 297 | getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes); |
| 298 | bool isCameraParamChanged = false; |
| 299 | if (width != -1 && height != -1) { |
| 300 | if (!isVideoSizeSupported(width, height, sizes)) { |
| 301 | LOGE("Video dimension (%dx%d) is unsupported", width, height); |
| 302 | return BAD_VALUE; |
| 303 | } |
| 304 | if (isSetVideoSizeSupportedByCamera) { |
| 305 | params->setVideoSize(width, height); |
| 306 | } else { |
| 307 | params->setPreviewSize(width, height); |
| 308 | } |
| 309 | isCameraParamChanged = true; |
| 310 | } else if ((width == -1 && height != -1) || |
| 311 | (width != -1 && height == -1)) { |
| 312 | // If one and only one of the width and height is -1 |
| 313 | // we reject such a request. |
| 314 | LOGE("Requested video size (%dx%d) is not supported", width, height); |
| 315 | return BAD_VALUE; |
| 316 | } else { // width == -1 && height == -1 |
| 317 | // Do not configure the camera. |
| 318 | // Use the current width and height value setting from the camera. |
| 319 | } |
| 320 | |
| 321 | if (frameRate != -1) { |
James Dong | 6357308 | 2010-10-24 09:32:39 -0700 | [diff] [blame] | 322 | CHECK(frameRate > 0 && frameRate <= 120); |
| 323 | const char* supportedFrameRates = |
| 324 | params->get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES); |
| 325 | CHECK(supportedFrameRates != NULL); |
| 326 | LOGV("Supported frame rates: %s", supportedFrameRates); |
| 327 | char buf[4]; |
| 328 | snprintf(buf, 4, "%d", frameRate); |
| 329 | if (strstr(supportedFrameRates, buf) == NULL) { |
| 330 | LOGE("Requested frame rate (%d) is not supported: %s", |
| 331 | frameRate, supportedFrameRates); |
| 332 | return BAD_VALUE; |
| 333 | } |
| 334 | |
| 335 | // The frame rate is supported, set the camera to the requested value. |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 336 | params->setPreviewFrameRate(frameRate); |
| 337 | isCameraParamChanged = true; |
| 338 | } else { // frameRate == -1 |
| 339 | // Do not configure the camera. |
| 340 | // Use the current frame rate value setting from the camera |
| 341 | } |
| 342 | |
| 343 | if (isCameraParamChanged) { |
| 344 | // Either frame rate or frame size needs to be changed. |
| 345 | String8 s = params->flatten(); |
| 346 | if (OK != mCamera->setParameters(s)) { |
| 347 | LOGE("Could not change settings." |
| 348 | " Someone else is using camera %p?", mCamera.get()); |
| 349 | return -EBUSY; |
| 350 | } |
| 351 | } |
| 352 | return OK; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Check whether the requested video frame size |
| 357 | * has been successfully configured or not. If both width and height |
| 358 | * are -1, check on the current width and height value setting |
| 359 | * is performed. |
| 360 | * |
| 361 | * @param params CameraParameters to retrieve the information |
| 362 | * @param the target video frame width in pixels to check against |
| 363 | * @param the target video frame height in pixels to check against |
| 364 | * @return OK if no error |
| 365 | */ |
| 366 | status_t CameraSource::checkVideoSize( |
| 367 | const CameraParameters& params, |
| 368 | int32_t width, int32_t height) { |
| 369 | |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 370 | LOGV("checkVideoSize"); |
James Dong | f96c9d1 | 2010-10-20 11:41:33 -0700 | [diff] [blame] | 371 | // The actual video size is the same as the preview size |
| 372 | // if the camera hal does not support separate video and |
| 373 | // preview output. In this case, we retrieve the video |
| 374 | // size from preview. |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 375 | int32_t frameWidthActual = -1; |
| 376 | int32_t frameHeightActual = -1; |
James Dong | f96c9d1 | 2010-10-20 11:41:33 -0700 | [diff] [blame] | 377 | Vector<Size> sizes; |
| 378 | params.getSupportedVideoSizes(sizes); |
| 379 | if (sizes.size() == 0) { |
| 380 | // video size is the same as preview size |
| 381 | params.getPreviewSize(&frameWidthActual, &frameHeightActual); |
| 382 | } else { |
| 383 | // video size may not be the same as preview |
| 384 | params.getVideoSize(&frameWidthActual, &frameHeightActual); |
| 385 | } |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 386 | if (frameWidthActual < 0 || frameHeightActual < 0) { |
| 387 | LOGE("Failed to retrieve video frame size (%dx%d)", |
| 388 | frameWidthActual, frameHeightActual); |
| 389 | return UNKNOWN_ERROR; |
| 390 | } |
| 391 | |
| 392 | // Check the actual video frame size against the target/requested |
| 393 | // video frame size. |
| 394 | if (width != -1 && height != -1) { |
| 395 | if (frameWidthActual != width || frameHeightActual != height) { |
| 396 | LOGE("Failed to set video frame size to %dx%d. " |
| 397 | "The actual video size is %dx%d ", width, height, |
| 398 | frameWidthActual, frameHeightActual); |
| 399 | return UNKNOWN_ERROR; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Good now. |
| 404 | mVideoSize.width = frameWidthActual; |
| 405 | mVideoSize.height = frameHeightActual; |
| 406 | return OK; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Check the requested frame rate has been successfully configured or not. |
| 411 | * If the target frameRate is -1, check on the current frame rate value |
| 412 | * setting is performed. |
| 413 | * |
| 414 | * @param params CameraParameters to retrieve the information |
| 415 | * @param the target video frame rate to check against |
| 416 | * @return OK if no error. |
| 417 | */ |
| 418 | status_t CameraSource::checkFrameRate( |
| 419 | const CameraParameters& params, |
| 420 | int32_t frameRate) { |
| 421 | |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 422 | LOGV("checkFrameRate"); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 423 | int32_t frameRateActual = params.getPreviewFrameRate(); |
| 424 | if (frameRateActual < 0) { |
| 425 | LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual); |
| 426 | return UNKNOWN_ERROR; |
| 427 | } |
| 428 | |
| 429 | // Check the actual video frame rate against the target/requested |
| 430 | // video frame rate. |
| 431 | if (frameRate != -1 && (frameRateActual - frameRate) != 0) { |
| 432 | LOGE("Failed to set preview frame rate to %d fps. The actual " |
| 433 | "frame rate is %d", frameRate, frameRateActual); |
| 434 | return UNKNOWN_ERROR; |
| 435 | } |
| 436 | |
| 437 | // Good now. |
| 438 | mVideoFrameRate = frameRateActual; |
| 439 | return OK; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Initialize the CameraSource to so that it becomes |
| 444 | * ready for providing the video input streams as requested. |
| 445 | * @param camera the camera object used for the video source |
| 446 | * @param cameraId if camera == 0, use camera with this id |
| 447 | * as the video source |
| 448 | * @param videoSize the target video frame size. If both |
| 449 | * width and height in videoSize is -1, use the current |
| 450 | * width and heigth settings by the camera |
| 451 | * @param frameRate the target frame rate in frames per second. |
| 452 | * if it is -1, use the current camera frame rate setting. |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 453 | * @param storeMetaDataInVideoBuffers request to store meta |
| 454 | * data or real YUV data in video buffers. Request to |
| 455 | * store meta data in video buffers may not be honored |
| 456 | * if the source does not support this feature. |
| 457 | * |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 458 | * @return OK if no error. |
| 459 | */ |
| 460 | status_t CameraSource::init( |
| 461 | const sp<ICamera>& camera, |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 462 | const sp<ICameraRecordingProxy>& proxy, |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 463 | int32_t cameraId, |
| 464 | Size videoSize, |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 465 | int32_t frameRate, |
| 466 | bool storeMetaDataInVideoBuffers) { |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 467 | |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 468 | LOGV("init"); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 469 | status_t err = OK; |
James Dong | ae4c1ac | 2011-07-08 17:59:29 -0700 | [diff] [blame] | 470 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 471 | err = initWithCameraAccess(camera, proxy, cameraId, |
| 472 | videoSize, frameRate, |
| 473 | storeMetaDataInVideoBuffers); |
| 474 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 475 | return err; |
| 476 | } |
| 477 | |
| 478 | status_t CameraSource::initWithCameraAccess( |
| 479 | const sp<ICamera>& camera, |
| 480 | const sp<ICameraRecordingProxy>& proxy, |
| 481 | int32_t cameraId, |
| 482 | Size videoSize, |
| 483 | int32_t frameRate, |
| 484 | bool storeMetaDataInVideoBuffers) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 485 | LOGV("initWithCameraAccess"); |
James Dong | ae4c1ac | 2011-07-08 17:59:29 -0700 | [diff] [blame] | 486 | status_t err = OK; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 487 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 488 | if ((err = isCameraAvailable(camera, proxy, cameraId)) != OK) { |
| 489 | LOGE("Camera connection could not be established."); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 490 | return err; |
| 491 | } |
| 492 | CameraParameters params(mCamera->getParameters()); |
| 493 | if ((err = isCameraColorFormatSupported(params)) != OK) { |
| 494 | return err; |
| 495 | } |
| 496 | |
| 497 | // Set the camera to use the requested video frame size |
| 498 | // and/or frame rate. |
| 499 | if ((err = configureCamera(¶ms, |
| 500 | videoSize.width, videoSize.height, |
| 501 | frameRate))) { |
| 502 | return err; |
| 503 | } |
| 504 | |
| 505 | // Check on video frame size and frame rate. |
| 506 | CameraParameters newCameraParams(mCamera->getParameters()); |
| 507 | if ((err = checkVideoSize(newCameraParams, |
| 508 | videoSize.width, videoSize.height)) != OK) { |
| 509 | return err; |
| 510 | } |
| 511 | if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) { |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | // This CHECK is good, since we just passed the lock/unlock |
| 516 | // check earlier by calling mCamera->setParameters(). |
| 517 | CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface)); |
James Dong | 2b37ced | 2010-10-09 01:16:58 -0700 | [diff] [blame] | 518 | |
James Dong | abdd2ba | 2010-12-10 13:09:05 -0800 | [diff] [blame] | 519 | // By default, do not store metadata in video buffers |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 520 | mIsMetaDataStoredInVideoBuffers = false; |
James Dong | abdd2ba | 2010-12-10 13:09:05 -0800 | [diff] [blame] | 521 | mCamera->storeMetaDataInBuffers(false); |
| 522 | if (storeMetaDataInVideoBuffers) { |
| 523 | if (OK == mCamera->storeMetaDataInBuffers(true)) { |
| 524 | mIsMetaDataStoredInVideoBuffers = true; |
| 525 | } |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 526 | } |
| 527 | |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 528 | int64_t glitchDurationUs = (1000000LL / mVideoFrameRate); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 529 | if (glitchDurationUs > mGlitchDurationThresholdUs) { |
| 530 | mGlitchDurationThresholdUs = glitchDurationUs; |
| 531 | } |
| 532 | |
James Dong | ddcc4a6 | 2010-06-08 11:58:53 -0700 | [diff] [blame] | 533 | // XXX: query camera for the stride and slice height |
| 534 | // when the capability becomes available. |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 535 | mMeta = new MetaData; |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 536 | mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); |
| 537 | mMeta->setInt32(kKeyColorFormat, mColorFormat); |
| 538 | mMeta->setInt32(kKeyWidth, mVideoSize.width); |
| 539 | mMeta->setInt32(kKeyHeight, mVideoSize.height); |
| 540 | mMeta->setInt32(kKeyStride, mVideoSize.width); |
| 541 | mMeta->setInt32(kKeySliceHeight, mVideoSize.height); |
James Dong | 393410a | 2010-11-10 20:43:53 -0800 | [diff] [blame] | 542 | mMeta->setInt32(kKeyFrameRate, mVideoFrameRate); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 543 | return OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | CameraSource::~CameraSource() { |
| 547 | if (mStarted) { |
| 548 | stop(); |
James Dong | ae4c1ac | 2011-07-08 17:59:29 -0700 | [diff] [blame] | 549 | } else if (mInitCheck == OK) { |
| 550 | // Camera is initialized but because start() is never called, |
| 551 | // the lock on Camera is never released(). This makes sure |
| 552 | // Camera's lock is released in this case. |
| 553 | releaseCamera(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 554 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 557 | void CameraSource::startCameraRecording() { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 558 | LOGV("startCameraRecording"); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 559 | // Reset the identity to the current thread because media server owns the |
| 560 | // camera and recording is started by the applications. The applications |
| 561 | // will connect to the camera in ICameraRecordingProxy::startRecording. |
| 562 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 563 | if (mCameraFlags & FLAGS_HOT_CAMERA) { |
| 564 | mCamera->unlock(); |
| 565 | mCamera.clear(); |
| 566 | CHECK_EQ(OK, mCameraRecordingProxy->startRecording(new ProxyListener(this))); |
| 567 | } else { |
| 568 | mCamera->setListener(new CameraSourceListener(this)); |
| 569 | mCamera->startRecording(); |
| 570 | CHECK(mCamera->recordingEnabled()); |
| 571 | } |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 572 | IPCThreadState::self()->restoreCallingIdentity(token); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 573 | } |
| 574 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 575 | status_t CameraSource::start(MetaData *meta) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 576 | LOGV("start"); |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 577 | CHECK(!mStarted); |
James Dong | 54ff19a | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 578 | if (mInitCheck != OK) { |
| 579 | LOGE("CameraSource is not initialized yet"); |
| 580 | return mInitCheck; |
| 581 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 582 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 583 | char value[PROPERTY_VALUE_MAX]; |
| 584 | if (property_get("media.stagefright.record-stats", value, NULL) |
| 585 | && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { |
| 586 | mCollectStats = true; |
| 587 | } |
James Dong | 9d7f58a | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 588 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 589 | mStartTimeUs = 0; |
| 590 | int64_t startTimeUs; |
| 591 | if (meta && meta->findInt64(kKeyTime, &startTimeUs)) { |
| 592 | mStartTimeUs = startTimeUs; |
| 593 | } |
| 594 | |
James Dong | c42478e | 2010-11-15 10:38:37 -0800 | [diff] [blame] | 595 | startCameraRecording(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 596 | |
| 597 | mStarted = true; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 598 | return OK; |
| 599 | } |
| 600 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 601 | void CameraSource::stopCameraRecording() { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 602 | LOGV("stopCameraRecording"); |
| 603 | if (mCameraFlags & FLAGS_HOT_CAMERA) { |
| 604 | mCameraRecordingProxy->stopRecording(); |
| 605 | } else { |
| 606 | mCamera->setListener(NULL); |
| 607 | mCamera->stopRecording(); |
| 608 | } |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 609 | } |
| 610 | |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 611 | void CameraSource::releaseCamera() { |
| 612 | LOGV("releaseCamera"); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 613 | if (mCamera != 0) { |
James Dong | ae4c1ac | 2011-07-08 17:59:29 -0700 | [diff] [blame] | 614 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 615 | if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) { |
| 616 | LOGV("Camera was cold when we started, stopping preview"); |
| 617 | mCamera->stopPreview(); |
| 618 | mCamera->disconnect(); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 619 | } |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 620 | mCamera->unlock(); |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 621 | mCamera.clear(); |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 622 | mCamera = 0; |
James Dong | ae4c1ac | 2011-07-08 17:59:29 -0700 | [diff] [blame] | 623 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 624 | } |
Wu-cheng Li | 95068be | 2011-06-29 15:17:11 +0800 | [diff] [blame] | 625 | if (mCameraRecordingProxy != 0) { |
| 626 | mCameraRecordingProxy->asBinder()->unlinkToDeath(mDeathNotifier); |
| 627 | mCameraRecordingProxy.clear(); |
| 628 | } |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 629 | mCameraFlags = 0; |
| 630 | } |
| 631 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 632 | status_t CameraSource::stop() { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 633 | LOGD("stop: E"); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 634 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 635 | mStarted = false; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 636 | mFrameAvailableCondition.signal(); |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 637 | |
James Dong | 9197441 | 2011-08-24 19:50:36 -0700 | [diff] [blame^] | 638 | int64_t token; |
| 639 | bool isTokenValid = false; |
| 640 | if (mCamera != 0) { |
| 641 | token = IPCThreadState::self()->clearCallingIdentity(); |
| 642 | isTokenValid = true; |
| 643 | } |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 644 | releaseQueuedFrames(); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 645 | while (!mFramesBeingEncoded.empty()) { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 646 | if (NO_ERROR != |
| 647 | mFrameCompleteCondition.waitRelative(mLock, 3000000000LL)) { |
| 648 | LOGW("Timed out waiting for outstanding frames being encoded: %d", |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 649 | mFramesBeingEncoded.size()); |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 650 | } |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 651 | } |
James Dong | d69c7f6 | 2010-12-09 11:24:22 -0800 | [diff] [blame] | 652 | stopCameraRecording(); |
James Dong | ea7b485 | 2010-12-05 14:25:34 -0800 | [diff] [blame] | 653 | releaseCamera(); |
James Dong | 9197441 | 2011-08-24 19:50:36 -0700 | [diff] [blame^] | 654 | if (isTokenValid) { |
| 655 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 656 | } |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 657 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 658 | if (mCollectStats) { |
| 659 | LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us", |
| 660 | mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped, |
| 661 | mLastFrameTimestampUs - mFirstFrameTimeUs); |
| 662 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 663 | |
James Dong | ba29002 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 664 | if (mNumGlitches > 0) { |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 665 | LOGW("%d long delays between neighboring video frames", mNumGlitches); |
James Dong | ba29002 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 666 | } |
| 667 | |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 668 | CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped); |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 669 | LOGD("stop: X"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 670 | return OK; |
| 671 | } |
| 672 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 673 | void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 674 | LOGV("releaseRecordingFrame"); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 675 | if (mCameraRecordingProxy != NULL) { |
| 676 | mCameraRecordingProxy->releaseRecordingFrame(frame); |
James Dong | 334d097 | 2011-08-05 17:19:29 -0700 | [diff] [blame] | 677 | } else if (mCamera != NULL) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 678 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 679 | mCamera->releaseRecordingFrame(frame); |
| 680 | IPCThreadState::self()->restoreCallingIdentity(token); |
James Dong | d69c7f6 | 2010-12-09 11:24:22 -0800 | [diff] [blame] | 681 | } |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 682 | } |
| 683 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 684 | void CameraSource::releaseQueuedFrames() { |
| 685 | List<sp<IMemory> >::iterator it; |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 686 | while (!mFramesReceived.empty()) { |
| 687 | it = mFramesReceived.begin(); |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 688 | releaseRecordingFrame(*it); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 689 | mFramesReceived.erase(it); |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 690 | ++mNumFramesDropped; |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 694 | sp<MetaData> CameraSource::getFormat() { |
James Dong | 653252b | 2010-06-03 11:48:31 -0700 | [diff] [blame] | 695 | return mMeta; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 696 | } |
| 697 | |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 698 | void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) { |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 699 | releaseRecordingFrame(frame); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 700 | } |
| 701 | |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 702 | void CameraSource::signalBufferReturned(MediaBuffer *buffer) { |
| 703 | LOGV("signalBufferReturned: %p", buffer->data()); |
Andreas Huber | 56223b9 | 2010-08-11 12:34:32 -0700 | [diff] [blame] | 704 | Mutex::Autolock autoLock(mLock); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 705 | for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); |
| 706 | it != mFramesBeingEncoded.end(); ++it) { |
| 707 | if ((*it)->pointer() == buffer->data()) { |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 708 | releaseOneRecordingFrame((*it)); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 709 | mFramesBeingEncoded.erase(it); |
| 710 | ++mNumFramesEncoded; |
| 711 | buffer->setObserver(0); |
| 712 | buffer->release(); |
| 713 | mFrameCompleteCondition.signal(); |
| 714 | return; |
| 715 | } |
| 716 | } |
| 717 | CHECK_EQ(0, "signalBufferReturned: bogus buffer"); |
| 718 | } |
| 719 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 720 | status_t CameraSource::read( |
| 721 | MediaBuffer **buffer, const ReadOptions *options) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 722 | LOGV("read"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 723 | |
| 724 | *buffer = NULL; |
| 725 | |
| 726 | int64_t seekTimeUs; |
Andreas Huber | abd1f4f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 727 | ReadOptions::SeekMode mode; |
| 728 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 729 | return ERROR_UNSUPPORTED; |
| 730 | } |
| 731 | |
| 732 | sp<IMemory> frame; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 733 | int64_t frameTime; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 734 | |
| 735 | { |
| 736 | Mutex::Autolock autoLock(mLock); |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 737 | while (mStarted && mFramesReceived.empty()) { |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 738 | if (NO_ERROR != |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 739 | mFrameAvailableCondition.waitRelative(mLock, 1000000000LL)) { |
James Dong | 3bd3020 | 2011-07-19 20:24:22 -0700 | [diff] [blame] | 740 | if (mCameraRecordingProxy != 0 && |
| 741 | !mCameraRecordingProxy->asBinder()->isBinderAlive()) { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 742 | LOGW("camera recording proxy is gone"); |
| 743 | return ERROR_END_OF_STREAM; |
| 744 | } |
James Dong | 41152ef | 2010-12-20 21:29:12 -0800 | [diff] [blame] | 745 | LOGW("Timed out waiting for incoming camera video frames: %lld us", |
| 746 | mLastFrameTimestampUs); |
| 747 | } |
James Dong | 542db5d | 2010-07-21 14:51:35 -0700 | [diff] [blame] | 748 | } |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 749 | if (!mStarted) { |
| 750 | return OK; |
| 751 | } |
| 752 | frame = *mFramesReceived.begin(); |
| 753 | mFramesReceived.erase(mFramesReceived.begin()); |
| 754 | |
| 755 | frameTime = *mFrameTimes.begin(); |
| 756 | mFrameTimes.erase(mFrameTimes.begin()); |
| 757 | mFramesBeingEncoded.push_back(frame); |
| 758 | *buffer = new MediaBuffer(frame->pointer(), frame->size()); |
| 759 | (*buffer)->setObserver(this); |
| 760 | (*buffer)->add_ref(); |
| 761 | (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 762 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 763 | return OK; |
| 764 | } |
| 765 | |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 766 | void CameraSource::dataCallbackTimestamp(int64_t timestampUs, |
| 767 | int32_t msgType, const sp<IMemory> &data) { |
| 768 | LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 769 | Mutex::Autolock autoLock(mLock); |
James Dong | a472613 | 2011-02-16 12:28:26 -0800 | [diff] [blame] | 770 | if (!mStarted || (mNumFramesReceived == 0 && timestampUs < mStartTimeUs)) { |
| 771 | LOGV("Drop frame at %lld/%lld us", timestampUs, mStartTimeUs); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 772 | releaseOneRecordingFrame(data); |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 773 | return; |
| 774 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 775 | |
James Dong | 98cfde0 | 2011-06-03 17:21:16 -0700 | [diff] [blame] | 776 | if (mNumFramesReceived > 0) { |
| 777 | CHECK(timestampUs > mLastFrameTimestampUs); |
| 778 | if (timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { |
| 779 | ++mNumGlitches; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 780 | } |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 783 | // May need to skip frame or modify timestamp. Currently implemented |
| 784 | // by the subclass CameraSourceTimeLapse. |
James Dong | 79e23b4 | 2010-12-11 10:43:41 -0800 | [diff] [blame] | 785 | if (skipCurrentFrame(timestampUs)) { |
Nipun Kwatra | 65e7e6f | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 786 | releaseOneRecordingFrame(data); |
| 787 | return; |
Nipun Kwatra | fc20aab | 2010-06-30 18:51:31 -0700 | [diff] [blame] | 788 | } |
| 789 | |
James Dong | 365a963 | 2010-06-04 13:59:27 -0700 | [diff] [blame] | 790 | mLastFrameTimestampUs = timestampUs; |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 791 | if (mNumFramesReceived == 0) { |
James Dong | c32cd79 | 2010-04-26 17:48:26 -0700 | [diff] [blame] | 792 | mFirstFrameTimeUs = timestampUs; |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 793 | // Initial delay |
| 794 | if (mStartTimeUs > 0) { |
| 795 | if (timestampUs < mStartTimeUs) { |
| 796 | // Frame was captured before recording was started |
| 797 | // Drop it without updating the statistical data. |
| 798 | releaseOneRecordingFrame(data); |
| 799 | return; |
| 800 | } |
| 801 | mStartTimeUs = timestampUs - mStartTimeUs; |
| 802 | } |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 803 | } |
James Dong | 13aec89 | 2010-04-21 16:14:15 -0700 | [diff] [blame] | 804 | ++mNumFramesReceived; |
Andreas Huber | be5c74f | 2009-10-13 17:08:31 -0700 | [diff] [blame] | 805 | |
James Dong | 98cfde0 | 2011-06-03 17:21:16 -0700 | [diff] [blame] | 806 | CHECK(data != NULL && data->size() > 0); |
James Dong | 7278cf3 | 2010-05-27 16:05:58 -0700 | [diff] [blame] | 807 | mFramesReceived.push_back(data); |
James Dong | f60cafe | 2010-06-19 09:04:18 -0700 | [diff] [blame] | 808 | int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); |
| 809 | mFrameTimes.push_back(timeUs); |
| 810 | LOGV("initial delay: %lld, current time stamp: %lld", |
| 811 | mStartTimeUs, timeUs); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 812 | mFrameAvailableCondition.signal(); |
| 813 | } |
| 814 | |
James Dong | 5c95231 | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 815 | bool CameraSource::isMetaDataStoredInVideoBuffers() const { |
| 816 | LOGV("isMetaDataStoredInVideoBuffers"); |
| 817 | return mIsMetaDataStoredInVideoBuffers; |
| 818 | } |
| 819 | |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 820 | CameraSource::ProxyListener::ProxyListener(const sp<CameraSource>& source) { |
| 821 | mSource = source; |
| 822 | } |
| 823 | |
| 824 | void CameraSource::ProxyListener::dataCallbackTimestamp( |
| 825 | nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { |
| 826 | mSource->dataCallbackTimestamp(timestamp / 1000, msgType, dataPtr); |
| 827 | } |
| 828 | |
| 829 | void CameraSource::DeathNotifier::binderDied(const wp<IBinder>& who) { |
| 830 | LOGI("Camera recording proxy died"); |
| 831 | } |
| 832 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 833 | } // namespace android |