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