blob: 4b840fca1a2e468e72104dd5bc6ea68435f2d070 [file] [log] [blame]
Emilian Peev538c90e2018-12-17 18:03:19 +00001/*
2 * Copyright (C) 2018 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 "Camera3-CompositeStream"
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/CameraDeviceBase.h"
25#include "CameraDeviceClient.h"
26#include "CompositeStream.h"
27
28namespace android {
29namespace camera3 {
30
Shuzhen Wange8675782019-12-05 09:12:14 -080031CompositeStream::CompositeStream(sp<CameraDeviceBase> device,
Emilian Peev538c90e2018-12-17 18:03:19 +000032 wp<hardware::camera2::ICameraDeviceCallbacks> cb) :
33 mDevice(device),
34 mRemoteCallback(cb),
35 mNumPartialResults(1),
36 mErrorState(false) {
Shuzhen Wange8675782019-12-05 09:12:14 -080037 if (device != nullptr) {
38 CameraMetadata staticInfo = device->info();
Emilian Peev538c90e2018-12-17 18:03:19 +000039 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
40 if (entry.count > 0) {
41 mNumPartialResults = entry.data.i32[0];
42 }
Shuzhen Wange8675782019-12-05 09:12:14 -080043 mStatusTracker = device->getStatusTracker();
Emilian Peev538c90e2018-12-17 18:03:19 +000044 }
45}
46
47status_t CompositeStream::createStream(const std::vector<sp<Surface>>& consumers,
48 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070049 camera_stream_rotation_t rotation, int * id, const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080050 const std::unordered_set<int32_t> &sensorPixelModesUsed,
51 std::vector<int> * surfaceIds,
52 int streamSetId, bool isShared, bool isMultiResolution) {
Emilian Peev538c90e2018-12-17 18:03:19 +000053 if (hasDeferredConsumer) {
54 ALOGE("%s: Deferred consumers not supported in case of composite streams!",
55 __FUNCTION__);
56 return BAD_VALUE;
57 }
58
59 if (streamSetId != camera3::CAMERA3_STREAM_ID_INVALID) {
60 ALOGE("%s: Surface groups not supported in case of composite streams!",
61 __FUNCTION__);
62 return BAD_VALUE;
63 }
64
65 if (isShared) {
66 ALOGE("%s: Shared surfaces not supported in case of composite streams!",
67 __FUNCTION__);
68 return BAD_VALUE;
69 }
70
Shuzhen Wang83bff122020-11-20 15:51:39 -080071 if (isMultiResolution) {
72 ALOGE("%s: Multi-resolution output not supported in case of composite streams!",
73 __FUNCTION__);
74 return BAD_VALUE;
75 }
76
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080077 return createInternalStreams(consumers, hasDeferredConsumer, width, height, format, rotation,
78 id, physicalCameraId, sensorPixelModesUsed, surfaceIds, streamSetId, isShared);
Emilian Peev538c90e2018-12-17 18:03:19 +000079}
80
81status_t CompositeStream::deleteStream() {
82 {
83 Mutex::Autolock l(mMutex);
84 mPendingCaptureResults.clear();
85 mCaptureResults.clear();
86 mFrameNumberMap.clear();
87 mErrorFrameNumbers.clear();
88 }
89
90 return deleteInternalStreams();
91}
92
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080093void CompositeStream::onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId,
94 const CameraMetadata& /*settings*/) {
Emilian Peev538c90e2018-12-17 18:03:19 +000095 Mutex::Autolock l(mMutex);
96 if (!mErrorState && (streamId == getStreamId())) {
97 mPendingCaptureResults.emplace(frameNumber, CameraMetadata());
98 }
99}
100
101void CompositeStream::onBufferReleased(const BufferInfo& bufferInfo) {
102 Mutex::Autolock l(mMutex);
103 if (!mErrorState && !bufferInfo.mError) {
104 mFrameNumberMap.emplace(bufferInfo.mFrameNumber, bufferInfo.mTimestamp);
105 mInputReadyCondition.signal();
106 }
107}
108
109void CompositeStream::eraseResult(int64_t frameNumber) {
110 Mutex::Autolock l(mMutex);
111
112 auto it = mPendingCaptureResults.find(frameNumber);
113 if (it == mPendingCaptureResults.end()) {
114 return;
115 }
116
117 it = mPendingCaptureResults.erase(it);
118}
119
120void CompositeStream::onResultAvailable(const CaptureResult& result) {
121 bool resultError = false;
122 {
123 Mutex::Autolock l(mMutex);
124
125 uint64_t frameNumber = result.mResultExtras.frameNumber;
126 bool resultReady = false;
127 auto it = mPendingCaptureResults.find(frameNumber);
128 if (it != mPendingCaptureResults.end()) {
129 it->second.append(result.mMetadata);
130 if (result.mResultExtras.partialResultCount >= mNumPartialResults) {
131 auto entry = it->second.find(ANDROID_SENSOR_TIMESTAMP);
132 if (entry.count == 1) {
133 auto ts = entry.data.i64[0];
134 mCaptureResults.emplace(ts, std::make_tuple(frameNumber, it->second));
135 resultReady = true;
136 } else {
137 ALOGE("%s: Timestamp metadata entry missing for frameNumber: %" PRIu64,
138 __FUNCTION__, frameNumber);
139 resultError = true;
140 }
141 mPendingCaptureResults.erase(it);
142 }
143 }
144
145 if (resultReady) {
146 mInputReadyCondition.signal();
147 }
148 }
149
150 if (resultError) {
151 onResultError(result.mResultExtras);
152 }
153}
154
155void CompositeStream::flagAnErrorFrameNumber(int64_t frameNumber) {
156 Mutex::Autolock l(mMutex);
157 mErrorFrameNumbers.emplace(frameNumber);
158 mInputReadyCondition.signal();
159}
160
161status_t CompositeStream::registerCompositeStreamListener(int32_t streamId) {
162 sp<CameraDeviceBase> device = mDevice.promote();
163 if (device.get() == nullptr) {
164 return NO_INIT;
165 }
166
167 auto ret = device->addBufferListenerForStream(streamId, this);
168 if (ret != OK) {
169 ALOGE("%s: Failed to register composite stream listener!", __FUNCTION__);
170 }
171
172 return ret;
173}
174
175bool CompositeStream::onError(int32_t errorCode, const CaptureResultExtras& resultExtras) {
176 auto ret = false;
177 switch (errorCode) {
178 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
179 onResultError(resultExtras);
180 break;
181 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
182 ret = onStreamBufferError(resultExtras);
183 break;
184 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
Shuzhen Wange8675782019-12-05 09:12:14 -0800185 onRequestError(resultExtras);
Emilian Peev538c90e2018-12-17 18:03:19 +0000186 break;
187 default:
188 ALOGE("%s: Unrecoverable error: %d detected!", __FUNCTION__, errorCode);
189 Mutex::Autolock l(mMutex);
190 mErrorState = true;
191 break;
192 }
193
194 return ret;
195}
196
Shuzhen Wange8675782019-12-05 09:12:14 -0800197void CompositeStream::notifyError(int64_t frameNumber, int32_t requestId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000198 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb =
199 mRemoteCallback.promote();
200
201 if ((frameNumber >= 0) && (remoteCb.get() != nullptr)) {
202 CaptureResultExtras extras;
203 extras.errorStreamId = getStreamId();
204 extras.frameNumber = frameNumber;
Shuzhen Wange8675782019-12-05 09:12:14 -0800205 extras.requestId = requestId;
Emilian Peev538c90e2018-12-17 18:03:19 +0000206 remoteCb->onDeviceError(
207 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
208 extras);
209 }
210}
211
Emilian Peevc0fe54c2020-03-11 14:05:07 -0700212void CompositeStream::switchToOffline() {
213 Mutex::Autolock l(mMutex);
214 mDevice.clear();
215}
216
Emilian Peev538c90e2018-12-17 18:03:19 +0000217}; // namespace camera3
218}; // namespace android