blob: f7b76f8bdfc9a4c7c13cdfa9c88752e3226de678 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
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 Dongc32cd792010-04-26 17:48:26 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "CameraSource"
19#include <utils/Log.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070020
Andreas Huber20111aa2009-07-14 16:56:47 -070021#include <OMX_Component.h>
James Dong9d7f58a2010-06-09 15:57:48 -070022#include <binder/IPCThreadState.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070023#include <media/stagefright/CameraSource.h>
Andreas Huber0c891992009-08-26 14:48:20 -070024#include <media/stagefright/MediaDebug.h>
Andreas Huberbe5c74f2009-10-13 17:08:31 -070025#include <media/stagefright/MediaDefs.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070026#include <media/stagefright/MediaErrors.h>
27#include <media/stagefright/MetaData.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080028#include <camera/Camera.h>
29#include <camera/CameraParameters.h>
James Dong54ff19a2010-10-08 11:59:32 -070030#include <surfaceflinger/Surface.h>
Andreas Huberbe5c74f2009-10-13 17:08:31 -070031#include <utils/String8.h>
James Dong365a9632010-06-04 13:59:27 -070032#include <cutils/properties.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070033
34namespace android {
35
Andreas Huberbe5c74f2009-10-13 17:08:31 -070036struct CameraSourceListener : public CameraListener {
37 CameraSourceListener(const sp<CameraSource> &source);
38
39 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
40 virtual void postData(int32_t msgType, const sp<IMemory> &dataPtr);
41
42 virtual void postDataTimestamp(
43 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
44
45protected:
46 virtual ~CameraSourceListener();
47
48private:
49 wp<CameraSource> mSource;
50
51 CameraSourceListener(const CameraSourceListener &);
52 CameraSourceListener &operator=(const CameraSourceListener &);
53};
54
55CameraSourceListener::CameraSourceListener(const sp<CameraSource> &source)
56 : mSource(source) {
57}
58
59CameraSourceListener::~CameraSourceListener() {
60}
61
62void CameraSourceListener::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
63 LOGV("notify(%d, %d, %d)", msgType, ext1, ext2);
64}
65
66void CameraSourceListener::postData(int32_t msgType, const sp<IMemory> &dataPtr) {
67 LOGV("postData(%d, ptr:%p, size:%d)",
68 msgType, dataPtr->pointer(), dataPtr->size());
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -070069
70 sp<CameraSource> source = mSource.promote();
71 if (source.get() != NULL) {
72 source->dataCallback(msgType, dataPtr);
73 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070074}
75
76void CameraSourceListener::postDataTimestamp(
77 nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
James Dongc32cd792010-04-26 17:48:26 -070078
79 sp<CameraSource> source = mSource.promote();
80 if (source.get() != NULL) {
81 source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
82 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -070083}
84
James Dong653252b2010-06-03 11:48:31 -070085static int32_t getColorFormat(const char* colorFormat) {
James Donge2d8ba82010-09-15 16:52:51 -070086 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
87 return OMX_COLOR_FormatYUV420Planar;
88 }
89
James Dong653252b2010-06-03 11:48:31 -070090 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422SP)) {
91 return OMX_COLOR_FormatYUV422SemiPlanar;
92 }
93
94 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420SP)) {
95 return OMX_COLOR_FormatYUV420SemiPlanar;
96 }
97
98 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV422I)) {
99 return OMX_COLOR_FormatYCbYCr;
100 }
101
102 if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_RGB565)) {
103 return OMX_COLOR_Format16bitRGB565;
104 }
105
James Donga1abc1a2010-09-13 16:30:51 -0700106 LOGE("Uknown color format (%s), please add it to "
107 "CameraSource::getColorFormat", colorFormat);
108
James Dong653252b2010-06-03 11:48:31 -0700109 CHECK_EQ(0, "Unknown color format");
110}
111
Andreas Huber20111aa2009-07-14 16:56:47 -0700112CameraSource *CameraSource::Create() {
James Dong54ff19a2010-10-08 11:59:32 -0700113 Size size;
114 size.width = -1;
115 size.height = -1;
Andreas Huber20111aa2009-07-14 16:56:47 -0700116
James Dong54ff19a2010-10-08 11:59:32 -0700117 sp<ICamera> camera;
118 return new CameraSource(camera, 0, size, -1, NULL);
Andreas Huber20111aa2009-07-14 16:56:47 -0700119}
120
Andreas Huber30ab6622009-11-16 15:43:38 -0800121// static
James Dong54ff19a2010-10-08 11:59:32 -0700122CameraSource *CameraSource::CreateFromCamera(
123 const sp<ICamera>& camera,
124 int32_t cameraId,
125 Size videoSize,
126 int32_t frameRate,
127 const sp<Surface>& surface) {
Andreas Huber30ab6622009-11-16 15:43:38 -0800128
James Dong54ff19a2010-10-08 11:59:32 -0700129 CameraSource *source = new CameraSource(camera, cameraId,
130 videoSize, frameRate, surface);
131
132 if (source != NULL) {
133 if (source->initCheck() != OK) {
134 delete source;
135 return NULL;
136 }
137 }
138 return source;
Andreas Huber30ab6622009-11-16 15:43:38 -0800139}
140
James Dong54ff19a2010-10-08 11:59:32 -0700141CameraSource::CameraSource(
142 const sp<ICamera>& camera,
143 int32_t cameraId,
144 Size videoSize,
145 int32_t frameRate,
146 const sp<Surface>& surface)
147 : mCameraFlags(0),
148 mVideoFrameRate(-1),
149 mCamera(0),
150 mSurface(surface),
James Dong13aec892010-04-21 16:14:15 -0700151 mNumFramesReceived(0),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700152 mLastFrameTimestampUs(0),
153 mStarted(false),
154 mFirstFrameTimeUs(0),
James Dong13aec892010-04-21 16:14:15 -0700155 mNumFramesEncoded(0),
156 mNumFramesDropped(0),
James Dongf60cafe2010-06-19 09:04:18 -0700157 mNumGlitches(0),
158 mGlitchDurationThresholdUs(200000),
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700159 mCollectStats(false) {
James Dong9d7f58a2010-06-09 15:57:48 -0700160
James Dong54ff19a2010-10-08 11:59:32 -0700161 mVideoSize.width = -1;
162 mVideoSize.height = -1;
163
164 mInitCheck = init(camera, cameraId, videoSize, frameRate);
165}
166
167status_t CameraSource::initCheck() const {
168 return mInitCheck;
169}
170
171status_t CameraSource::isCameraAvailable(
172 const sp<ICamera>& camera, int32_t cameraId) {
173
174 if (camera == 0) {
175 mCamera = Camera::connect(cameraId);
176 mCameraFlags &= ~FLAGS_HOT_CAMERA;
177 } else {
178 mCamera = Camera::create(camera);
179 mCameraFlags |= FLAGS_HOT_CAMERA;
180 }
181
182 // Is camera available?
183 if (mCamera == 0) {
184 LOGE("Camera connection could not be established.");
185 return -EBUSY;
186 }
187 if (!(mCameraFlags & FLAGS_HOT_CAMERA)) {
188 mCamera->lock();
189 }
190 return OK;
191}
192
193
194/*
195 * Check to see whether the requested video width and height is one
196 * of the supported sizes.
197 * @param width the video frame width in pixels
198 * @param height the video frame height in pixels
199 * @param suppportedSizes the vector of sizes that we check against
200 * @return true if the dimension (width and height) is supported.
201 */
202static bool isVideoSizeSupported(
203 int32_t width, int32_t height,
204 const Vector<Size>& supportedSizes) {
205
206 LOGV("isVideoSizeSupported");
207 for (size_t i = 0; i < supportedSizes.size(); ++i) {
208 if (width == supportedSizes[i].width &&
209 height == supportedSizes[i].height) {
210 return true;
211 }
212 }
213 return false;
214}
215
216/*
217 * If the preview and video output is separate, we only set the
218 * the video size, and applications should set the preview size
219 * to some proper value, and the recording framework will not
220 * change the preview size; otherwise, if the video and preview
221 * output is the same, we need to set the preview to be the same
222 * as the requested video size.
223 *
224 */
225/*
226 * Query the camera to retrieve the supported video frame sizes
227 * and also to see whether CameraParameters::setVideoSize()
228 * is supported or not.
229 * @param params CameraParameters to retrieve the information
230 * @@param isSetVideoSizeSupported retunrs whether method
231 * CameraParameters::setVideoSize() is supported or not.
232 * @param sizes returns the vector of Size objects for the
233 * supported video frame sizes advertised by the camera.
234 */
235static void getSupportedVideoSizes(
236 const CameraParameters& params,
237 bool *isSetVideoSizeSupported,
238 Vector<Size>& sizes) {
239
240 *isSetVideoSizeSupported = true;
241 params.getSupportedVideoSizes(sizes);
242 if (sizes.size() == 0) {
243 LOGD("Camera does not support setVideoSize()");
244 params.getSupportedPreviewSizes(sizes);
245 *isSetVideoSizeSupported = false;
246 }
247}
248
249/*
250 * Check whether the camera has the supported color format
251 * @param params CameraParameters to retrieve the information
252 * @return OK if no error.
253 */
254status_t CameraSource::isCameraColorFormatSupported(
255 const CameraParameters& params) {
256 mColorFormat = getColorFormat(params.get(
257 CameraParameters::KEY_VIDEO_FRAME_FORMAT));
258 if (mColorFormat == -1) {
259 return BAD_VALUE;
260 }
261 return OK;
262}
263
264/*
265 * Configure the camera to use the requested video size
266 * (width and height) and/or frame rate. If both width and
267 * height are -1, configuration on the video size is skipped.
268 * if frameRate is -1, configuration on the frame rate
269 * is skipped. Skipping the configuration allows one to
270 * use the current camera setting without the need to
271 * actually know the specific values (see Create() method).
272 *
273 * @param params the CameraParameters to be configured
274 * @param width the target video frame width in pixels
275 * @param height the target video frame height in pixels
276 * @param frameRate the target frame rate in frames per second.
277 * @return OK if no error.
278 */
279status_t CameraSource::configureCamera(
280 CameraParameters* params,
281 int32_t width, int32_t height,
282 int32_t frameRate) {
283
284 Vector<Size> sizes;
285 bool isSetVideoSizeSupportedByCamera = true;
286 getSupportedVideoSizes(*params, &isSetVideoSizeSupportedByCamera, sizes);
287 bool isCameraParamChanged = false;
288 if (width != -1 && height != -1) {
289 if (!isVideoSizeSupported(width, height, sizes)) {
290 LOGE("Video dimension (%dx%d) is unsupported", width, height);
291 return BAD_VALUE;
292 }
293 if (isSetVideoSizeSupportedByCamera) {
294 params->setVideoSize(width, height);
295 } else {
296 params->setPreviewSize(width, height);
297 }
298 isCameraParamChanged = true;
299 } else if ((width == -1 && height != -1) ||
300 (width != -1 && height == -1)) {
301 // If one and only one of the width and height is -1
302 // we reject such a request.
303 LOGE("Requested video size (%dx%d) is not supported", width, height);
304 return BAD_VALUE;
305 } else { // width == -1 && height == -1
306 // Do not configure the camera.
307 // Use the current width and height value setting from the camera.
308 }
309
310 if (frameRate != -1) {
311 params->setPreviewFrameRate(frameRate);
312 isCameraParamChanged = true;
313 } else { // frameRate == -1
314 // Do not configure the camera.
315 // Use the current frame rate value setting from the camera
316 }
317
318 if (isCameraParamChanged) {
319 // Either frame rate or frame size needs to be changed.
320 String8 s = params->flatten();
321 if (OK != mCamera->setParameters(s)) {
322 LOGE("Could not change settings."
323 " Someone else is using camera %p?", mCamera.get());
324 return -EBUSY;
325 }
326 }
327 return OK;
328}
329
330/*
331 * Check whether the requested video frame size
332 * has been successfully configured or not. If both width and height
333 * are -1, check on the current width and height value setting
334 * is performed.
335 *
336 * @param params CameraParameters to retrieve the information
337 * @param the target video frame width in pixels to check against
338 * @param the target video frame height in pixels to check against
339 * @return OK if no error
340 */
341status_t CameraSource::checkVideoSize(
342 const CameraParameters& params,
343 int32_t width, int32_t height) {
344
345 int32_t frameWidthActual = -1;
346 int32_t frameHeightActual = -1;
347 params.getPreviewSize(&frameWidthActual, &frameHeightActual);
348 if (frameWidthActual < 0 || frameHeightActual < 0) {
349 LOGE("Failed to retrieve video frame size (%dx%d)",
350 frameWidthActual, frameHeightActual);
351 return UNKNOWN_ERROR;
352 }
353
354 // Check the actual video frame size against the target/requested
355 // video frame size.
356 if (width != -1 && height != -1) {
357 if (frameWidthActual != width || frameHeightActual != height) {
358 LOGE("Failed to set video frame size to %dx%d. "
359 "The actual video size is %dx%d ", width, height,
360 frameWidthActual, frameHeightActual);
361 return UNKNOWN_ERROR;
362 }
363 }
364
365 // Good now.
366 mVideoSize.width = frameWidthActual;
367 mVideoSize.height = frameHeightActual;
368 return OK;
369}
370
371/*
372 * Check the requested frame rate has been successfully configured or not.
373 * If the target frameRate is -1, check on the current frame rate value
374 * setting is performed.
375 *
376 * @param params CameraParameters to retrieve the information
377 * @param the target video frame rate to check against
378 * @return OK if no error.
379 */
380status_t CameraSource::checkFrameRate(
381 const CameraParameters& params,
382 int32_t frameRate) {
383
384 int32_t frameRateActual = params.getPreviewFrameRate();
385 if (frameRateActual < 0) {
386 LOGE("Failed to retrieve preview frame rate (%d)", frameRateActual);
387 return UNKNOWN_ERROR;
388 }
389
390 // Check the actual video frame rate against the target/requested
391 // video frame rate.
392 if (frameRate != -1 && (frameRateActual - frameRate) != 0) {
393 LOGE("Failed to set preview frame rate to %d fps. The actual "
394 "frame rate is %d", frameRate, frameRateActual);
395 return UNKNOWN_ERROR;
396 }
397
398 // Good now.
399 mVideoFrameRate = frameRateActual;
400 return OK;
401}
402
403/*
404 * Initialize the CameraSource to so that it becomes
405 * ready for providing the video input streams as requested.
406 * @param camera the camera object used for the video source
407 * @param cameraId if camera == 0, use camera with this id
408 * as the video source
409 * @param videoSize the target video frame size. If both
410 * width and height in videoSize is -1, use the current
411 * width and heigth settings by the camera
412 * @param frameRate the target frame rate in frames per second.
413 * if it is -1, use the current camera frame rate setting.
414 * @return OK if no error.
415 */
416status_t CameraSource::init(
417 const sp<ICamera>& camera,
418 int32_t cameraId,
419 Size videoSize,
420 int32_t frameRate) {
421
422 status_t err = OK;
James Dong9d7f58a2010-06-09 15:57:48 -0700423 int64_t token = IPCThreadState::self()->clearCallingIdentity();
James Dong54ff19a2010-10-08 11:59:32 -0700424
425 if ((err = isCameraAvailable(camera, cameraId)) != OK) {
426 return err;
427 }
428 CameraParameters params(mCamera->getParameters());
429 if ((err = isCameraColorFormatSupported(params)) != OK) {
430 return err;
431 }
432
433 // Set the camera to use the requested video frame size
434 // and/or frame rate.
435 if ((err = configureCamera(&params,
436 videoSize.width, videoSize.height,
437 frameRate))) {
438 return err;
439 }
440
441 // Check on video frame size and frame rate.
442 CameraParameters newCameraParams(mCamera->getParameters());
443 if ((err = checkVideoSize(newCameraParams,
444 videoSize.width, videoSize.height)) != OK) {
445 return err;
446 }
447 if ((err = checkFrameRate(newCameraParams, frameRate)) != OK) {
448 return err;
449 }
450
451 // This CHECK is good, since we just passed the lock/unlock
452 // check earlier by calling mCamera->setParameters().
453 CHECK_EQ(OK, mCamera->setPreviewDisplay(mSurface));
James Dong9d7f58a2010-06-09 15:57:48 -0700454 IPCThreadState::self()->restoreCallingIdentity(token);
455
James Dong54ff19a2010-10-08 11:59:32 -0700456 int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
James Dongf60cafe2010-06-19 09:04:18 -0700457 if (glitchDurationUs > mGlitchDurationThresholdUs) {
458 mGlitchDurationThresholdUs = glitchDurationUs;
459 }
460
James Dongddcc4a62010-06-08 11:58:53 -0700461 // XXX: query camera for the stride and slice height
462 // when the capability becomes available.
James Dong653252b2010-06-03 11:48:31 -0700463 mMeta = new MetaData;
James Dong54ff19a2010-10-08 11:59:32 -0700464 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
465 mMeta->setInt32(kKeyColorFormat, mColorFormat);
466 mMeta->setInt32(kKeyWidth, mVideoSize.width);
467 mMeta->setInt32(kKeyHeight, mVideoSize.height);
468 mMeta->setInt32(kKeyStride, mVideoSize.width);
469 mMeta->setInt32(kKeySliceHeight, mVideoSize.height);
470 return OK;
Andreas Huber20111aa2009-07-14 16:56:47 -0700471}
472
473CameraSource::~CameraSource() {
474 if (mStarted) {
475 stop();
476 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700477}
478
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700479void CameraSource::startCameraRecording() {
480 CHECK_EQ(OK, mCamera->startRecording());
481}
482
James Dongf60cafe2010-06-19 09:04:18 -0700483status_t CameraSource::start(MetaData *meta) {
Andreas Huber0c891992009-08-26 14:48:20 -0700484 CHECK(!mStarted);
James Dong54ff19a2010-10-08 11:59:32 -0700485 if (mInitCheck != OK) {
486 LOGE("CameraSource is not initialized yet");
487 return mInitCheck;
488 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700489
James Dong365a9632010-06-04 13:59:27 -0700490 char value[PROPERTY_VALUE_MAX];
491 if (property_get("media.stagefright.record-stats", value, NULL)
492 && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
493 mCollectStats = true;
494 }
James Dong9d7f58a2010-06-09 15:57:48 -0700495
James Dongf60cafe2010-06-19 09:04:18 -0700496 mStartTimeUs = 0;
497 int64_t startTimeUs;
498 if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
499 mStartTimeUs = startTimeUs;
500 }
501
James Dong9d7f58a2010-06-09 15:57:48 -0700502 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700503 mCamera->setListener(new CameraSourceListener(this));
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700504 startCameraRecording();
James Dong9d7f58a2010-06-09 15:57:48 -0700505 IPCThreadState::self()->restoreCallingIdentity(token);
Andreas Huber20111aa2009-07-14 16:56:47 -0700506
507 mStarted = true;
Andreas Huber20111aa2009-07-14 16:56:47 -0700508 return OK;
509}
510
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700511void CameraSource::stopCameraRecording() {
Nipun Kwatra78eff722010-09-15 15:08:49 -0700512 mCamera->setListener(NULL);
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700513 mCamera->stopRecording();
514}
515
Andreas Huber20111aa2009-07-14 16:56:47 -0700516status_t CameraSource::stop() {
James Dongc32cd792010-04-26 17:48:26 -0700517 LOGV("stop");
518 Mutex::Autolock autoLock(mLock);
Andreas Huber20111aa2009-07-14 16:56:47 -0700519 mStarted = false;
James Dongc32cd792010-04-26 17:48:26 -0700520 mFrameAvailableCondition.signal();
James Dong365a9632010-06-04 13:59:27 -0700521
James Dong9d7f58a2010-06-09 15:57:48 -0700522 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700523 stopCameraRecording();
James Dongc32cd792010-04-26 17:48:26 -0700524 releaseQueuedFrames();
James Dong7278cf32010-05-27 16:05:58 -0700525 while (!mFramesBeingEncoded.empty()) {
James Dong365a9632010-06-04 13:59:27 -0700526 LOGI("Waiting for outstanding frames being encoded: %d",
527 mFramesBeingEncoded.size());
James Dong7278cf32010-05-27 16:05:58 -0700528 mFrameCompleteCondition.wait(mLock);
529 }
James Dong54ff19a2010-10-08 11:59:32 -0700530
531 LOGV("Disconnect camera");
532 if ((mCameraFlags & FLAGS_HOT_CAMERA) == 0) {
533 LOGV("Camera was cold when we started, stopping preview");
534 mCamera->stopPreview();
535 }
536 mCamera->unlock();
537 mCamera.clear();
538 mCamera = 0;
539 mCameraFlags = 0;
James Dong9d7f58a2010-06-09 15:57:48 -0700540 IPCThreadState::self()->restoreCallingIdentity(token);
James Dong7278cf32010-05-27 16:05:58 -0700541
James Dong365a9632010-06-04 13:59:27 -0700542 if (mCollectStats) {
543 LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
544 mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
545 mLastFrameTimestampUs - mFirstFrameTimeUs);
546 }
James Dong13aec892010-04-21 16:14:15 -0700547
548 CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
Andreas Huber20111aa2009-07-14 16:56:47 -0700549 return OK;
550}
551
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700552void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
553 mCamera->releaseRecordingFrame(frame);
554}
555
James Dongc32cd792010-04-26 17:48:26 -0700556void CameraSource::releaseQueuedFrames() {
557 List<sp<IMemory> >::iterator it;
James Dong7278cf32010-05-27 16:05:58 -0700558 while (!mFramesReceived.empty()) {
559 it = mFramesReceived.begin();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700560 releaseRecordingFrame(*it);
James Dong7278cf32010-05-27 16:05:58 -0700561 mFramesReceived.erase(it);
James Dong13aec892010-04-21 16:14:15 -0700562 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700563 }
564}
565
Andreas Huber20111aa2009-07-14 16:56:47 -0700566sp<MetaData> CameraSource::getFormat() {
James Dong653252b2010-06-03 11:48:31 -0700567 return mMeta;
Andreas Huber20111aa2009-07-14 16:56:47 -0700568}
569
James Dongf60cafe2010-06-19 09:04:18 -0700570void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
571 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700572 releaseRecordingFrame(frame);
James Dongf60cafe2010-06-19 09:04:18 -0700573 IPCThreadState::self()->restoreCallingIdentity(token);
574}
575
James Dong7278cf32010-05-27 16:05:58 -0700576void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
577 LOGV("signalBufferReturned: %p", buffer->data());
Andreas Huber56223b92010-08-11 12:34:32 -0700578 Mutex::Autolock autoLock(mLock);
James Dong7278cf32010-05-27 16:05:58 -0700579 for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
580 it != mFramesBeingEncoded.end(); ++it) {
581 if ((*it)->pointer() == buffer->data()) {
James Dongf60cafe2010-06-19 09:04:18 -0700582 releaseOneRecordingFrame((*it));
James Dong7278cf32010-05-27 16:05:58 -0700583 mFramesBeingEncoded.erase(it);
584 ++mNumFramesEncoded;
585 buffer->setObserver(0);
586 buffer->release();
587 mFrameCompleteCondition.signal();
588 return;
589 }
590 }
591 CHECK_EQ(0, "signalBufferReturned: bogus buffer");
592}
593
Andreas Huber20111aa2009-07-14 16:56:47 -0700594status_t CameraSource::read(
595 MediaBuffer **buffer, const ReadOptions *options) {
James Dongc32cd792010-04-26 17:48:26 -0700596 LOGV("read");
Andreas Huber20111aa2009-07-14 16:56:47 -0700597
598 *buffer = NULL;
599
600 int64_t seekTimeUs;
Andreas Huberabd1f4f2010-07-20 15:04:28 -0700601 ReadOptions::SeekMode mode;
602 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700603 return ERROR_UNSUPPORTED;
604 }
605
606 sp<IMemory> frame;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700607 int64_t frameTime;
Andreas Huber20111aa2009-07-14 16:56:47 -0700608
609 {
610 Mutex::Autolock autoLock(mLock);
James Dong542db5d2010-07-21 14:51:35 -0700611 while (mStarted) {
612 while(mFramesReceived.empty()) {
613 mFrameAvailableCondition.wait(mLock);
614 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700615
James Dong542db5d2010-07-21 14:51:35 -0700616 if (!mStarted) {
617 return OK;
618 }
James Dong7278cf32010-05-27 16:05:58 -0700619
James Dong542db5d2010-07-21 14:51:35 -0700620 frame = *mFramesReceived.begin();
621 mFramesReceived.erase(mFramesReceived.begin());
622
623 frameTime = *mFrameTimes.begin();
624 mFrameTimes.erase(mFrameTimes.begin());
625 int64_t skipTimeUs;
626 if (!options || !options->getSkipFrame(&skipTimeUs)) {
627 skipTimeUs = frameTime;
628 }
629 if (skipTimeUs > frameTime) {
630 LOGV("skipTimeUs: %lld us > frameTime: %lld us",
631 skipTimeUs, frameTime);
632 releaseOneRecordingFrame(frame);
633 ++mNumFramesDropped;
634 // Safeguard against the abuse of the kSkipFrame_Option.
635 if (skipTimeUs - frameTime >= 1E6) {
636 LOGE("Frame skipping requested is way too long: %lld us",
637 skipTimeUs - frameTime);
638 return UNKNOWN_ERROR;
639 }
640 } else {
641 mFramesBeingEncoded.push_back(frame);
642 *buffer = new MediaBuffer(frame->pointer(), frame->size());
643 (*buffer)->setObserver(this);
644 (*buffer)->add_ref();
645 (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
Andreas Huber56223b92010-08-11 12:34:32 -0700646
James Dong542db5d2010-07-21 14:51:35 -0700647 return OK;
648 }
649 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700650 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700651 return OK;
652}
653
James Dongc32cd792010-04-26 17:48:26 -0700654void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
655 int32_t msgType, const sp<IMemory> &data) {
656 LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700657 Mutex::Autolock autoLock(mLock);
James Dongc32cd792010-04-26 17:48:26 -0700658 if (!mStarted) {
James Dongf60cafe2010-06-19 09:04:18 -0700659 releaseOneRecordingFrame(data);
James Dong13aec892010-04-21 16:14:15 -0700660 ++mNumFramesReceived;
661 ++mNumFramesDropped;
James Dongc32cd792010-04-26 17:48:26 -0700662 return;
663 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700664
James Dongf60cafe2010-06-19 09:04:18 -0700665 if (mNumFramesReceived > 0 &&
666 timestampUs - mLastFrameTimestampUs > mGlitchDurationThresholdUs) {
667 if (mNumGlitches % 10 == 0) { // Don't spam the log
668 LOGW("Long delay detected in video recording");
669 }
670 ++mNumGlitches;
671 }
672
Nipun Kwatra65e7e6f2010-07-12 09:17:14 -0700673 // May need to skip frame or modify timestamp. Currently implemented
674 // by the subclass CameraSourceTimeLapse.
675 if(skipCurrentFrame(timestampUs)) {
676 releaseOneRecordingFrame(data);
677 return;
Nipun Kwatrafc20aab2010-06-30 18:51:31 -0700678 }
679
James Dong365a9632010-06-04 13:59:27 -0700680 mLastFrameTimestampUs = timestampUs;
James Dong13aec892010-04-21 16:14:15 -0700681 if (mNumFramesReceived == 0) {
James Dongc32cd792010-04-26 17:48:26 -0700682 mFirstFrameTimeUs = timestampUs;
James Dongf60cafe2010-06-19 09:04:18 -0700683 // Initial delay
684 if (mStartTimeUs > 0) {
685 if (timestampUs < mStartTimeUs) {
686 // Frame was captured before recording was started
687 // Drop it without updating the statistical data.
688 releaseOneRecordingFrame(data);
689 return;
690 }
691 mStartTimeUs = timestampUs - mStartTimeUs;
692 }
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700693 }
James Dong13aec892010-04-21 16:14:15 -0700694 ++mNumFramesReceived;
Andreas Huberbe5c74f2009-10-13 17:08:31 -0700695
James Dong7278cf32010-05-27 16:05:58 -0700696 mFramesReceived.push_back(data);
James Dongf60cafe2010-06-19 09:04:18 -0700697 int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
698 mFrameTimes.push_back(timeUs);
699 LOGV("initial delay: %lld, current time stamp: %lld",
700 mStartTimeUs, timeUs);
Andreas Huber20111aa2009-07-14 16:56:47 -0700701 mFrameAvailableCondition.signal();
702}
703
Andreas Huber20111aa2009-07-14 16:56:47 -0700704} // namespace android