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