CameraSource.cpp revision 9d7f58a7da8502a4174a17ac49fcba6efa35a457
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      mCollectStats(false),
127      mStarted(false) {
128
129    int64_t token = IPCThreadState::self()->clearCallingIdentity();
130    String8 s = mCamera->getParameters();
131    IPCThreadState::self()->restoreCallingIdentity(token);
132
133    printf("params: \"%s\"\n", s.string());
134
135    int32_t width, height, stride, sliceHeight;
136    CameraParameters params(s);
137    params.getPreviewSize(&width, &height);
138
139    const char *colorFormatStr = params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT);
140    CHECK(colorFormatStr != NULL);
141    int32_t colorFormat = getColorFormat(colorFormatStr);
142
143    // XXX: query camera for the stride and slice height
144    // when the capability becomes available.
145    stride = width;
146    sliceHeight = height;
147
148    mMeta = new MetaData;
149    mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RAW);
150    mMeta->setInt32(kKeyColorFormat, colorFormat);
151    mMeta->setInt32(kKeyWidth, width);
152    mMeta->setInt32(kKeyHeight, height);
153    mMeta->setInt32(kKeyStride, stride);
154    mMeta->setInt32(kKeySliceHeight, sliceHeight);
155
156}
157
158CameraSource::~CameraSource() {
159    if (mStarted) {
160        stop();
161    }
162}
163
164status_t CameraSource::start(MetaData *) {
165    LOGV("start");
166    CHECK(!mStarted);
167
168    char value[PROPERTY_VALUE_MAX];
169    if (property_get("media.stagefright.record-stats", value, NULL)
170        && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
171        mCollectStats = true;
172    }
173
174    int64_t token = IPCThreadState::self()->clearCallingIdentity();
175    mCamera->setListener(new CameraSourceListener(this));
176    CHECK_EQ(OK, mCamera->startRecording());
177    IPCThreadState::self()->restoreCallingIdentity(token);
178
179    mStarted = true;
180    return OK;
181}
182
183status_t CameraSource::stop() {
184    LOGV("stop");
185    Mutex::Autolock autoLock(mLock);
186    mStarted = false;
187    mFrameAvailableCondition.signal();
188
189    int64_t token = IPCThreadState::self()->clearCallingIdentity();
190    mCamera->setListener(NULL);
191    mCamera->stopRecording();
192    releaseQueuedFrames();
193    while (!mFramesBeingEncoded.empty()) {
194        LOGI("Waiting for outstanding frames being encoded: %d",
195                mFramesBeingEncoded.size());
196        mFrameCompleteCondition.wait(mLock);
197    }
198    mCamera = NULL;
199    IPCThreadState::self()->restoreCallingIdentity(token);
200
201    if (mCollectStats) {
202        LOGI("Frames received/encoded/dropped: %d/%d/%d in %lld us",
203                mNumFramesReceived, mNumFramesEncoded, mNumFramesDropped,
204                mLastFrameTimestampUs - mFirstFrameTimeUs);
205    }
206
207    CHECK_EQ(mNumFramesReceived, mNumFramesEncoded + mNumFramesDropped);
208    return OK;
209}
210
211void CameraSource::releaseQueuedFrames() {
212    List<sp<IMemory> >::iterator it;
213    while (!mFramesReceived.empty()) {
214        it = mFramesReceived.begin();
215        mCamera->releaseRecordingFrame(*it);
216        mFramesReceived.erase(it);
217        ++mNumFramesDropped;
218    }
219}
220
221sp<MetaData> CameraSource::getFormat() {
222    return mMeta;
223}
224
225void CameraSource::signalBufferReturned(MediaBuffer *buffer) {
226    LOGV("signalBufferReturned: %p", buffer->data());
227    for (List<sp<IMemory> >::iterator it = mFramesBeingEncoded.begin();
228         it != mFramesBeingEncoded.end(); ++it) {
229        if ((*it)->pointer() ==  buffer->data()) {
230
231            int64_t token = IPCThreadState::self()->clearCallingIdentity();
232            mCamera->releaseRecordingFrame((*it));
233            IPCThreadState::self()->restoreCallingIdentity(token);
234
235            mFramesBeingEncoded.erase(it);
236            ++mNumFramesEncoded;
237            buffer->setObserver(0);
238            buffer->release();
239            mFrameCompleteCondition.signal();
240            return;
241        }
242    }
243    CHECK_EQ(0, "signalBufferReturned: bogus buffer");
244}
245
246status_t CameraSource::read(
247        MediaBuffer **buffer, const ReadOptions *options) {
248    LOGV("read");
249
250    *buffer = NULL;
251
252    int64_t seekTimeUs;
253    if (options && options->getSeekTo(&seekTimeUs)) {
254        return ERROR_UNSUPPORTED;
255    }
256
257    sp<IMemory> frame;
258    int64_t frameTime;
259
260    {
261        Mutex::Autolock autoLock(mLock);
262        while (mStarted && mFramesReceived.empty()) {
263            mFrameAvailableCondition.wait(mLock);
264        }
265        if (!mStarted) {
266            return OK;
267        }
268        frame = *mFramesReceived.begin();
269        mFramesReceived.erase(mFramesReceived.begin());
270
271        frameTime = *mFrameTimes.begin();
272        mFrameTimes.erase(mFrameTimes.begin());
273
274        mFramesBeingEncoded.push_back(frame);
275        *buffer = new MediaBuffer(frame->pointer(), frame->size());
276        (*buffer)->setObserver(this);
277        (*buffer)->add_ref();
278        (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
279    }
280    return OK;
281}
282
283void CameraSource::dataCallbackTimestamp(int64_t timestampUs,
284        int32_t msgType, const sp<IMemory> &data) {
285    LOGV("dataCallbackTimestamp: timestamp %lld us", timestampUs);
286    Mutex::Autolock autoLock(mLock);
287    if (!mStarted) {
288        int64_t token = IPCThreadState::self()->clearCallingIdentity();
289        mCamera->releaseRecordingFrame(data);
290        IPCThreadState::self()->restoreCallingIdentity(token);
291        ++mNumFramesReceived;
292        ++mNumFramesDropped;
293        return;
294    }
295
296    mLastFrameTimestampUs = timestampUs;
297    if (mNumFramesReceived == 0) {
298        mFirstFrameTimeUs = timestampUs;
299    }
300    ++mNumFramesReceived;
301
302    mFramesReceived.push_back(data);
303    mFrameTimes.push_back(timestampUs - mFirstFrameTimeUs);
304    mFrameAvailableCondition.signal();
305}
306
307}  // namespace android
308