CameraSource.cpp revision 542db5d438988360d491a5add1040a2df9aa90c9
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 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin(); 248 it != mFramesBeingEncoded.end(); ++it) { 249 if ((*it)->pointer() == buffer->data()) { 250 251 releaseOneRecordingFrame((*it)); 252 mFramesBeingEncoded.erase(it); 253 ++mNumFramesEncoded; 254 buffer->setObserver(0); 255 buffer->release(); 256 mFrameCompleteCondition.signal(); 257 return; 258 } 259 } 260 CHECK_EQ(0, "signalBufferReturned: bogus buffer"); 261} 262 263status_t CameraSource::read( 264 MediaBuffer **buffer, const ReadOptions *options) { 265 LOGV("read"); 266 267 *buffer = NULL; 268 269 int64_t seekTimeUs; 270 ReadOptions::SeekMode mode; 271 if (options && options->getSeekTo(&seekTimeUs, &mode)) { 272 return ERROR_UNSUPPORTED; 273 } 274 275 sp<IMemory> frame; 276 int64_t frameTime; 277 278 { 279 Mutex::Autolock autoLock(mLock); 280 while (mStarted) { 281 while(mFramesReceived.empty()) { 282 mFrameAvailableCondition.wait(mLock); 283 } 284 285 if (!mStarted) { 286 return OK; 287 } 288 289 frame = *mFramesReceived.begin(); 290 mFramesReceived.erase(mFramesReceived.begin()); 291 292 frameTime = *mFrameTimes.begin(); 293 mFrameTimes.erase(mFrameTimes.begin()); 294 int64_t skipTimeUs; 295 if (!options || !options->getSkipFrame(&skipTimeUs)) { 296 skipTimeUs = frameTime; 297 } 298 if (skipTimeUs > frameTime) { 299 LOGV("skipTimeUs: %lld us > frameTime: %lld us", 300 skipTimeUs, frameTime); 301 releaseOneRecordingFrame(frame); 302 ++mNumFramesDropped; 303 // Safeguard against the abuse of the kSkipFrame_Option. 304 if (skipTimeUs - frameTime >= 1E6) { 305 LOGE("Frame skipping requested is way too long: %lld us", 306 skipTimeUs - frameTime); 307 return UNKNOWN_ERROR; 308 } 309 } else { 310 mFramesBeingEncoded.push_back(frame); 311 *buffer = new MediaBuffer(frame->pointer(), frame->size()); 312 (*buffer)->setObserver(this); 313 (*buffer)->add_ref(); 314 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime); 315 return OK; 316 } 317 } 318 } 319 return OK; 320} 321 322void CameraSource::dataCallbackTimestamp(int64_t timestampUs, 323 int32_t msgType, const sp<IMemory> &data) { 324 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs); 325 Mutex::Autolock autoLock(mLock); 326 if (!mStarted) { 327 releaseOneRecordingFrame(data); 328 ++mNumFramesReceived; 329 ++mNumFramesDropped; 330 return; 331 } 332 333 if (mNumFramesReceived > 0 && 334 timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) { 335 if (mNumGlitches % 10 == 0) { // Don't spam the log 336 LOGW("Long delay detected in video recording"); 337 } 338 ++mNumGlitches; 339 } 340 341 mLastFrameTimestampUs = timestampUs; 342 if (mNumFramesReceived == 0) { 343 mFirstFrameTimeUs = timestampUs; 344 // Initial delay 345 if (mStartTimeUs > 0) { 346 if (timestampUs < mStartTimeUs) { 347 // Frame was captured before recording was started 348 // Drop it without updating the statistical data. 349 releaseOneRecordingFrame(data); 350 return; 351 } 352 mStartTimeUs = timestampUs - mStartTimeUs; 353 } 354 } 355 ++mNumFramesReceived; 356 357 mFramesReceived.push_back(data); 358 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs); 359 mFrameTimes.push_back(timeUs); 360 LOGV("initial delay: %lld, current time stamp: %lld", 361 mStartTimeUs, timeUs); 362 mFrameAvailableCondition.signal(); 363} 364 365} // namespace android 366