CameraSource.cpp revision 54ff19ac69ace7c05ea90d225e26dab3b133f487
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 <surfaceflinger/Surface.h>
31#include <utils/String8.h>
32#include <cutils/properties.h>
33
34namespace android {
35
36struct 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());
69
70    sp<CameraSource> source = mSource.promote();
71    if (source.get() != NULL) {
72        source->dataCallback(msgType, dataPtr);
73    }
74}
75
76void CameraSourceListener::postDataTimestamp(
77        nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) {
78
79    sp<CameraSource> source = mSource.promote();
80    if (source.get() != NULL) {
81        source->dataCallbackTimestamp(timestamp/1000, msgType, dataPtr);
82    }
83}
84
85static int32_t getColorFormat(const char* colorFormat) {
86    if (!strcmp(colorFormat, CameraParameters::PIXEL_FORMAT_YUV420P)) {
87       return OMX_COLOR_FormatYUV420Planar;
88    }
89
90    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
106    LOGE("Uknown color format (%s), please add it to "
107         "CameraSource::getColorFormat", colorFormat);
108
109    CHECK_EQ(0, "Unknown color format");
110}
111
112CameraSource *CameraSource::Create() {
113    Size size;
114    size.width = -1;
115    size.height = -1;
116
117    sp<ICamera> camera;
118    return new CameraSource(camera, 0, size, -1, NULL);
119}
120
121// static
122CameraSource *CameraSource::CreateFromCamera(
123    const sp<ICamera>& camera,
124    int32_t cameraId,
125    Size videoSize,
126    int32_t frameRate,
127    const sp<Surface>& surface) {
128
129    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;
139}
140
141CameraSource::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),
151      mNumFramesReceived(0),
152      mLastFrameTimestampUs(0),
153      mStarted(false),
154      mFirstFrameTimeUs(0),
155      mNumFramesEncoded(0),
156      mNumFramesDropped(0),
157      mNumGlitches(0),
158      mGlitchDurationThresholdUs(200000),
159      mCollectStats(false) {
160
161    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;
423    int64_t token = IPCThreadState::self()->clearCallingIdentity();
424
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));
454    IPCThreadState::self()->restoreCallingIdentity(token);
455
456    int64_t glitchDurationUs = (1000000LL / mVideoFrameRate);
457    if (glitchDurationUs > mGlitchDurationThresholdUs) {
458        mGlitchDurationThresholdUs = glitchDurationUs;
459    }
460
461    // XXX: query camera for the stride and slice height
462    // when the capability becomes available.
463    mMeta = new MetaData;
464    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;
471}
472
473CameraSource::~CameraSource() {
474    if (mStarted) {
475        stop();
476    }
477}
478
479void CameraSource::startCameraRecording() {
480    CHECK_EQ(OK, mCamera->startRecording());
481}
482
483status_t CameraSource::start(MetaData *meta) {
484    CHECK(!mStarted);
485    if (mInitCheck != OK) {
486        LOGE("CameraSource is not initialized yet");
487        return mInitCheck;
488    }
489
490    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    }
495
496    mStartTimeUs = 0;
497    int64_t startTimeUs;
498    if (meta && meta->findInt64(kKeyTime, &startTimeUs)) {
499        mStartTimeUs = startTimeUs;
500    }
501
502    int64_t token = IPCThreadState::self()->clearCallingIdentity();
503    mCamera->setListener(new CameraSourceListener(this));
504    startCameraRecording();
505    IPCThreadState::self()->restoreCallingIdentity(token);
506
507    mStarted = true;
508    return OK;
509}
510
511void CameraSource::stopCameraRecording() {
512    mCamera->setListener(NULL);
513    mCamera->stopRecording();
514}
515
516status_t CameraSource::stop() {
517    LOGV("stop");
518    Mutex::Autolock autoLock(mLock);
519    mStarted = false;
520    mFrameAvailableCondition.signal();
521
522    int64_t token = IPCThreadState::self()->clearCallingIdentity();
523    stopCameraRecording();
524    releaseQueuedFrames();
525    while (!mFramesBeingEncoded.empty()) {
526        LOGI("Waiting for outstanding frames being encoded: %d",
527                mFramesBeingEncoded.size());
528        mFrameCompleteCondition.wait(mLock);
529    }
530
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;
540    IPCThreadState::self()->restoreCallingIdentity(token);
541
542    if (mCollectStats) {
543        LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
544                mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
545                mLastFrameTimestampUs - mFirstFrameTimeUs);
546    }
547
548    CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
549    return OK;
550}
551
552void CameraSource::releaseRecordingFrame(const sp<IMemory>& frame) {
553    mCamera->releaseRecordingFrame(frame);
554}
555
556void CameraSource::releaseQueuedFrames() {
557    List<sp<IMemory> >::iterator it;
558    while (!mFramesReceived.empty()) {
559        it = mFramesReceived.begin();
560        releaseRecordingFrame(*it);
561        mFramesReceived.erase(it);
562        ++mNumFramesDropped;
563    }
564}
565
566sp<MetaData> CameraSource::getFormat() {
567    return mMeta;
568}
569
570void CameraSource::releaseOneRecordingFrame(const sp<IMemory>& frame) {
571    int64_t token = IPCThreadState::self()->clearCallingIdentity();
572    releaseRecordingFrame(frame);
573    IPCThreadState::self()->restoreCallingIdentity(token);
574}
575
576void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
577    LOGV("signalBufferReturned: %p", buffer->data());
578    Mutex::Autolock autoLock(mLock);
579    for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
580         it != mFramesBeingEncoded.end(); ++it) {
581        if ((*it)->pointer() ==  buffer->data()) {
582            releaseOneRecordingFrame((*it));
583            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
594status_t CameraSource::read(
595        MediaBuffer **buffer, const ReadOptions *options) {
596    LOGV("read");
597
598    *buffer = NULL;
599
600    int64_t seekTimeUs;
601    ReadOptions::SeekMode mode;
602    if (options && options->getSeekTo(&seekTimeUs, &mode)) {
603        return ERROR_UNSUPPORTED;
604    }
605
606    sp<IMemory> frame;
607    int64_t frameTime;
608
609    {
610        Mutex::Autolock autoLock(mLock);
611        while (mStarted) {
612            while(mFramesReceived.empty()) {
613                mFrameAvailableCondition.wait(mLock);
614            }
615
616            if (!mStarted) {
617                return OK;
618            }
619
620            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);
646
647                return OK;
648            }
649        }
650    }
651    return OK;
652}
653
654void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
655        int32_t msgType, const sp<IMemory> &data) {
656    LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
657    Mutex::Autolock autoLock(mLock);
658    if (!mStarted) {
659        releaseOneRecordingFrame(data);
660        ++mNumFramesReceived;
661        ++mNumFramesDropped;
662        return;
663    }
664
665    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
673    // May need to skip frame or modify timestamp. Currently implemented
674    // by the subclass CameraSourceTimeLapse.
675    if(skipCurrentFrame(timestampUs)) {
676        releaseOneRecordingFrame(data);
677        return;
678    }
679
680    mLastFrameTimestampUs = timestampUs;
681    if (mNumFramesReceived == 0) {
682        mFirstFrameTimeUs = timestampUs;
683        // 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        }
693    }
694    ++mNumFramesReceived;
695
696    mFramesReceived.push_back(data);
697    int64_t timeUs = mStartTimeUs + (timestampUs - mFirstFrameTimeUs);
698    mFrameTimes.push_back(timeUs);
699    LOGV("initial delay: %lld, current time stamp: %lld",
700        mStartTimeUs, timeUs);
701    mFrameAvailableCondition.signal();
702}
703
704}  // namespace android
705