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