FrameProcessorBase.cpp revision cb0652e5a850b2fcd919e977247e87239efaf70e
1/*
2 * Copyright (C) 2013 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_TAG "Camera2-FrameProcessorBase"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include "common/FrameProcessorBase.h"
25#include "common/CameraDeviceBase.h"
26
27namespace android {
28namespace camera2 {
29
30FrameProcessorBase::FrameProcessorBase(wp<CameraDeviceBase> device) :
31    Thread(/*canCallJava*/false),
32    mDevice(device) {
33}
34
35FrameProcessorBase::~FrameProcessorBase() {
36    ALOGV("%s: Exit", __FUNCTION__);
37}
38
39status_t FrameProcessorBase::registerListener(int32_t minId,
40        int32_t maxId, wp<FilteredListener> listener, bool quirkSendPartials) {
41    Mutex::Autolock l(mInputMutex);
42    ALOGV("%s: Registering listener for frame id range %d - %d",
43            __FUNCTION__, minId, maxId);
44    RangeListener rListener = { minId, maxId, listener, quirkSendPartials };
45    mRangeListeners.push_back(rListener);
46    return OK;
47}
48
49status_t FrameProcessorBase::removeListener(int32_t minId,
50                                           int32_t maxId,
51                                           wp<FilteredListener> listener) {
52    Mutex::Autolock l(mInputMutex);
53    List<RangeListener>::iterator item = mRangeListeners.begin();
54    while (item != mRangeListeners.end()) {
55        if (item->minId == minId &&
56                item->maxId == maxId &&
57                item->listener == listener) {
58            item = mRangeListeners.erase(item);
59        } else {
60            item++;
61        }
62    }
63    return OK;
64}
65
66void FrameProcessorBase::dump(int fd, const Vector<String16>& /*args*/) {
67    String8 result("    Latest received frame:\n");
68    write(fd, result.string(), result.size());
69
70    CameraMetadata lastFrame;
71    {
72        // Don't race while dumping metadata
73        Mutex::Autolock al(mLastFrameMutex);
74        lastFrame = CameraMetadata(mLastFrame);
75    }
76    lastFrame.dump(fd, 2, 6);
77}
78
79bool FrameProcessorBase::threadLoop() {
80    status_t res;
81
82    sp<CameraDeviceBase> device;
83    {
84        device = mDevice.promote();
85        if (device == 0) return false;
86    }
87
88    res = device->waitForNextFrame(kWaitDuration);
89    if (res == OK) {
90        processNewFrames(device);
91    } else if (res != TIMED_OUT) {
92        ALOGE("FrameProcessorBase: Error waiting for new "
93                "frames: %s (%d)", strerror(-res), res);
94    }
95
96    return true;
97}
98
99void FrameProcessorBase::processNewFrames(const sp<CameraDeviceBase> &device) {
100    status_t res;
101    ATRACE_CALL();
102    CaptureResult result;
103
104    ALOGV("%s: Camera %d: Process new frames", __FUNCTION__, device->getId());
105
106    while ( (res = device->getNextResult(&result)) == OK) {
107
108        // TODO: instead of getting frame number from metadata, we should read
109        // this from result.mResultExtras when CameraDeviceBase interface is fixed.
110        camera_metadata_entry_t entry;
111
112        entry = result.mMetadata.find(ANDROID_REQUEST_FRAME_COUNT);
113        if (entry.count == 0) {
114            ALOGE("%s: Camera %d: Error reading frame number",
115                    __FUNCTION__, device->getId());
116            break;
117        }
118        ATRACE_INT("cam2_frame", entry.data.i32[0]);
119
120        if (!processSingleFrame(result, device)) {
121            break;
122        }
123
124        if (!result.mMetadata.isEmpty()) {
125            Mutex::Autolock al(mLastFrameMutex);
126            mLastFrame.acquire(result.mMetadata);
127        }
128    }
129    if (res != NOT_ENOUGH_DATA) {
130        ALOGE("%s: Camera %d: Error getting next frame: %s (%d)",
131                __FUNCTION__, device->getId(), strerror(-res), res);
132        return;
133    }
134
135    return;
136}
137
138bool FrameProcessorBase::processSingleFrame(CaptureResult &result,
139                                            const sp<CameraDeviceBase> &device) {
140    ALOGV("%s: Camera %d: Process single frame (is empty? %d)",
141          __FUNCTION__, device->getId(), result.mMetadata.isEmpty());
142    return processListeners(result, device) == OK;
143}
144
145status_t FrameProcessorBase::processListeners(const CaptureResult &result,
146        const sp<CameraDeviceBase> &device) {
147    ATRACE_CALL();
148
149    camera_metadata_ro_entry_t entry;
150
151    // Quirks: Don't deliver partial results to listeners that don't want them
152    bool quirkIsPartial = false;
153    entry = result.mMetadata.find(ANDROID_QUIRKS_PARTIAL_RESULT);
154    if (entry.count != 0 &&
155            entry.data.u8[0] == ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
156        ALOGV("%s: Camera %d: Not forwarding partial result to listeners",
157                __FUNCTION__, device->getId());
158        quirkIsPartial = true;
159    }
160
161    // TODO: instead of getting requestID from CameraMetadata, we should get it
162    // from CaptureResultExtras. This will require changing Camera2Device.
163    // Currently Camera2Device uses MetadataQueue to store results, which does not
164    // include CaptureResultExtras.
165    entry = result.mMetadata.find(ANDROID_REQUEST_ID);
166    if (entry.count == 0) {
167        ALOGE("%s: Camera %d: Error reading frame id", __FUNCTION__, device->getId());
168        return BAD_VALUE;
169    }
170    int32_t requestId = entry.data.i32[0];
171
172    List<sp<FilteredListener> > listeners;
173    {
174        Mutex::Autolock l(mInputMutex);
175
176        List<RangeListener>::iterator item = mRangeListeners.begin();
177        while (item != mRangeListeners.end()) {
178            if (requestId >= item->minId && requestId < item->maxId &&
179                    (!quirkIsPartial || item->quirkSendPartials)) {
180                sp<FilteredListener> listener = item->listener.promote();
181                if (listener == 0) {
182                    item = mRangeListeners.erase(item);
183                    continue;
184                } else {
185                    listeners.push_back(listener);
186                }
187            }
188            item++;
189        }
190    }
191    ALOGV("%s: Camera %d: Got %zu range listeners out of %zu", __FUNCTION__,
192          device->getId(), listeners.size(), mRangeListeners.size());
193
194    List<sp<FilteredListener> >::iterator item = listeners.begin();
195    for (; item != listeners.end(); item++) {
196        (*item)->onResultAvailable(result);
197    }
198    return OK;
199}
200
201}; // namespace camera2
202}; // namespace android
203