CameraSource.cpp revision 56223b96c2f6de5998496fac9d6703f06adc1dca
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 17//#define LOG_NDEBUG 0 18#define LOG_TAG "CameraSource" 19#include <utils/Log.h> 20 21#include <OMX_Component.h> 22#include <binder/IPCThreadState.h> 23#include <media/stagefright/CameraSource.h> 24#include <media/stagefright/MediaDebug.h> 25#include <media/stagefright/MediaDefs.h> 26#include <media/stagefright/MediaErrors.h> 27#include <media/stagefright/MetaData.h> 28#include <camera/Camera.h> 29#include <camera/CameraParameters.h> 30#include <utils/String8.h> 31#include <cutils/properties.h> 32 33namespace android { 34 35struct CameraSourceListener : public CameraListener { 36 CameraSourceListener(const sp<CameraSource> &source); 37 38 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2); 39 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr); 40 41 virtual void postDataTimestamp( 42 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 43 44protected: 45 virtual ~CameraSourceListener(); 46 47private: 48 wp<CameraSource> mSource; 49 50 CameraSourceListener(const CameraSourceListener &); 51 CameraSourceListener &operator=(const CameraSourceListener &); 52}; 53 54CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source) 55 : mSource(source) { 56} 57 58CameraSourceListener::~CameraSourceListener() { 59} 60 61void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) { 62 LOGV("notify(%d, %d, %d)", msgType, ext1, ext2); 63} 64 65void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr) { 66 LOGV("postData(%d, ptr:%p, size:%d)", 67 msgType, dataPtr->pointer(), dataPtr->size()); 68} 69 70void CameraSourceListener::postDataTimestamp( 71 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) { 72 73 sp<CameraSource> source = mSource.promote(); 74 if (source.get() != NULL) { 75 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr); 76 } 77} 78 79static int32_t getColorFormat(const char* colorFormat) { 80 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) { 81 return OMX_COLOR_FormatYUV422SemiPlanar; 82 } 83 84 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) { 85 return OMX_COLOR_FormatYUV420SemiPlanar; 86 } 87 88 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) { 89 return OMX_COLOR_FormatYCbYCr; 90 } 91 92 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) { 93 return OMX_COLOR_Format16bitRGB565; 94 } 95 96 CHECK_EQ(0, "Unknown color format"); 97} 98 99// static 100CameraSource *CameraSource::Create() { 101 sp<Camera> camera = Camera::connect(0); 102 103 if (camera.get() == NULL) { 104 return NULL; 105 } 106 107 return new CameraSource(camera); 108} 109 110// static 111CameraSource *CameraSource::CreateFromCamera(const sp<Camera> &camera) { 112 if (camera.get() == NULL) { 113 return NULL; 114 } 115 116 return new CameraSource(camera); 117} 118 119CameraSource::CameraSource(const sp<Camera> &camera) 120 : mCamera(camera), 121 mFirstFrameTimeUs(0), 122 mLastFrameTimestampUs(0), 123 mNumFramesReceived(0), 124 mNumFramesEncoded(0), 125 mNumFramesDropped(0), 126 mNumGlitches(0), 127 mGlitchDurationThresholdUs(200000), 128 mCollectStats(false), 129 mStarted(false) { 130 131 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 132 String8 s = mCamera->getParameters(); 133 IPCThreadState::self()->restoreCallingIdentity(token); 134 135 printf("params: \"%s\"\n", s.string()); 136 137 int32_t width, height, stride, sliceHeight; 138 CameraParameters params(s); 139 params.getPreviewSize(&width, &height); 140 141 // Calculate glitch duraton threshold based on frame rate 142 int32_t frameRate = params.getPreviewFrameRate(); 143 int64_t glitchDurationUs = (1000000LL / frameRate); 144 if (glitchDurationUs > mGlitchDurationThresholdUs) { 145 mGlitchDurationThresholdUs = glitchDurationUs; 146 } 147 148 const char *colorFormatStr = params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT); 149 CHECK(colorFormatStr != NULL); 150 int32_t colorFormat = getColorFormat(colorFormatStr); 151 152 // XXX: query camera for the stride and slice height 153 // when the capability becomes available. 154 stride = width; 155 sliceHeight = height; 156 157 mMeta = new MetaData; 158 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW); 159 mMeta->setInt32(kKeyColorFormat, colorFormat); 160 mMeta->setInt32(kKeyWidth, width); 161 mMeta->setInt32(kKeyHeight, height); 162 mMeta->setInt32(kKeyStride, stride); 163 mMeta->setInt32(kKeySliceHeight, sliceHeight); 164 165} 166 167CameraSource::~CameraSource() { 168 if (mStarted) { 169 stop(); 170 } 171} 172 173status_t CameraSource::start(MetaData *meta) { 174 CHECK(!mStarted); 175 176 char value[PROPERTY_VALUE_MAX]; 177 if (property_get("media.stagefright.record-stats", value, NULL) 178 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) { 179 mCollectStats = true; 180 } 181 182 mStartTimeUs = 0; 183 int64_t startTimeUs; 184 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) { 185 mStartTimeUs = startTimeUs; 186 } 187 188 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 189 mCamera->setListener(new CameraSourceListener(this)); 190 CHECK_EQ(OK, mCamera->startRecording()); 191 IPCThreadState::self()->restoreCallingIdentity(token); 192 193 mStarted = true; 194 return OK; 195} 196 197status_t CameraSource::stop() { 198 LOGV("stop"); 199 Mutex::Autolock autoLock(mLock); 200 mStarted = false; 201 mFrameAvailableCondition.signal(); 202 203 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 204 mCamera->setListener(NULL); 205 mCamera->stopRecording(); 206 releaseQueuedFrames(); 207 while (!mFramesBeingEncoded.empty()) { 208 LOGI("Waiting for outstanding frames being encoded: %d", 209 mFramesBeingEncoded.size()); 210 mFrameCompleteCondition.wait(mLock); 211 } 212 mCamera = NULL; 213 IPCThreadState::self()->restoreCallingIdentity(token); 214 215 if (mCollectStats) { 216 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us", 217 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped, 218 mLastFrameTimestampUs - mFirstFrameTimeUs); 219 } 220 221 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped); 222 return OK; 223} 224 225void CameraSource::releaseQueuedFrames() { 226 List<sp<IMemory> >::iterator it; 227 while (!mFramesReceived.empty()) { 228 it = mFramesReceived.begin(); 229 mCamera->releaseRecordingFrame(*it); 230 mFramesReceived.erase(it); 231 ++mNumFramesDropped; 232 } 233} 234 235sp<MetaData> CameraSource::getFormat() { 236 return mMeta; 237} 238 239void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) { 240 int64_t token = IPCThreadState::self()->clearCallingIdentity(); 241 mCamera->releaseRecordingFrame(frame); 242 IPCThreadState::self()->restoreCallingIdentity(token); 243} 244 245void CameraSource::signalBufferReturned(MediaBuffer *buffer) { 246 LOGV("signalBufferReturned: %p", buffer->data()); 247 Mutex::Autolock autoLock(mLock); 248 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); 249 it != mFramesBeingEncoded.end(); ++it) { 250 if ((*it)->pointer() == buffer->data()) { 251 252 releaseOneRecordingFrame((*it)); 253 mFramesBeingEncoded.erase(it); 254 ++mNumFramesEncoded; 255 buffer->setObserver(0); 256 buffer->release(); 257 mFrameCompleteCondition.signal(); 258 return; 259 } 260 } 261 CHECK_EQ(0, "signalBufferReturned: bogus buffer"); 262} 263 264status_t CameraSource::read( 265 MediaBuffer **buffer, const ReadOptions *options) { 266 LOGV("read"); 267 268 *buffer = NULL; 269 270 int64_t seekTimeUs; 271 ReadOptions::SeekMode mode; 272 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 273 return ERROR_UNSUPPORTED; 274 } 275 276 sp<IMemory> frame; 277 int64_t frameTime; 278 279 { 280 Mutex::Autolock autoLock(mLock); 281 while (mStarted) { 282 while(mFramesReceived.empty()) { 283 mFrameAvailableCondition.wait(mLock); 284 } 285 286 if (!mStarted) { 287 return OK; 288 } 289 290 frame = *mFramesReceived.begin(); 291 mFramesReceived.erase(mFramesReceived.begin()); 292 293 frameTime = *mFrameTimes.begin(); 294 mFrameTimes.erase(mFrameTimes.begin()); 295 int64_t skipTimeUs; 296 if (!options || !options->getSkipFrame(&skipTimeUs)) { 297 skipTimeUs = frameTime; 298 } 299 if (skipTimeUs > frameTime) { 300 LOGV("skipTimeUs: %lld us > frameTime: %lld us", 301 skipTimeUs, frameTime); 302 releaseOneRecordingFrame(frame); 303 ++mNumFramesDropped; 304 // Safeguard against the abuse of the kSkipFrame_Option. 305 if (skipTimeUs - frameTime >= 1E6) { 306 LOGE("Frame skipping requested is way too long: %lld us", 307 skipTimeUs - frameTime); 308 return UNKNOWN_ERROR; 309 } 310 } else { 311 mFramesBeingEncoded.push_back(frame); 312 *buffer = new MediaBuffer(frame->pointer(), frame->size()); 313 (*buffer)->setObserver(this); 314 (*buffer)->add_ref(); 315 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); 316 317 return OK; 318 } 319 } 320 } 321 return OK; 322} 323 324void CameraSource::dataCallbackTimestamp(int64_t timestampUs, 325 int32_t msgType, const sp<IMemory> &data) { 326 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); 327 Mutex::Autolock autoLock(mLock); 328 if (!mStarted) { 329 releaseOneRecordingFrame(data); 330 ++mNumFramesReceived; 331 ++mNumFramesDropped; 332 return; 333 } 334 335 if (mNumFramesReceived > 0 && 336 timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { 337 if (mNumGlitches % 10 == 0) { // Don't spam the log 338 LOGW("Long delay detected in video recording"); 339 } 340 ++mNumGlitches; 341 } 342 343 mLastFrameTimestampUs = timestampUs; 344 if (mNumFramesReceived == 0) { 345 mFirstFrameTimeUs = timestampUs; 346 // Initial delay 347 if (mStartTimeUs > 0) { 348 if (timestampUs < mStartTimeUs) { 349 // Frame was captured before recording was started 350 // Drop it without updating the statistical data. 351 releaseOneRecordingFrame(data); 352 return; 353 } 354 mStartTimeUs = timestampUs - mStartTimeUs; 355 } 356 } 357 ++mNumFramesReceived; 358 359 mFramesReceived.push_back(data); 360 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); 361 mFrameTimes.push_back(timeUs); 362 LOGV("initial delay: %lld, current time stamp: %lld", 363 mStartTimeUs, timeUs); 364 mFrameAvailableCondition.signal(); 365} 366 367} // namespace android 368