blob: 503cf231fc5449d5a08b427cda8ecd7ecf1b9c9a [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,
Emilian Peev434248e2022-10-06 14:58:54 -070052 int streamSetId, bool isShared, bool isMultiResolution, int32_t colorSpace,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000053 int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp) {
Emilian Peev538c90e2018-12-17 18:03:19 +000054 if (hasDeferredConsumer) {
55 ALOGE("%s: Deferred consumers not supported in case of composite streams!",
56 __FUNCTION__);
57 return BAD_VALUE;
58 }
59
60 if (streamSetId != camera3::CAMERA3_STREAM_ID_INVALID) {
61 ALOGE("%s: Surface groups not supported in case of composite streams!",
62 __FUNCTION__);
63 return BAD_VALUE;
64 }
65
66 if (isShared) {
67 ALOGE("%s: Shared surfaces not supported in case of composite streams!",
68 __FUNCTION__);
69 return BAD_VALUE;
70 }
71
Shuzhen Wang83bff122020-11-20 15:51:39 -080072 if (isMultiResolution) {
73 ALOGE("%s: Multi-resolution output not supported in case of composite streams!",
74 __FUNCTION__);
75 return BAD_VALUE;
76 }
77
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080078 return createInternalStreams(consumers, hasDeferredConsumer, width, height, format, rotation,
Emilian Peev434248e2022-10-06 14:58:54 -070079 id, physicalCameraId, sensorPixelModesUsed, surfaceIds, streamSetId, isShared,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000080 colorSpace, dynamicProfile, streamUseCase, useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +000081}
82
83status_t CompositeStream::deleteStream() {
84 {
85 Mutex::Autolock l(mMutex);
86 mPendingCaptureResults.clear();
87 mCaptureResults.clear();
88 mFrameNumberMap.clear();
89 mErrorFrameNumbers.clear();
90 }
91
92 return deleteInternalStreams();
93}
94
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080095void CompositeStream::onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId,
96 const CameraMetadata& /*settings*/) {
Emilian Peev538c90e2018-12-17 18:03:19 +000097 Mutex::Autolock l(mMutex);
98 if (!mErrorState && (streamId == getStreamId())) {
99 mPendingCaptureResults.emplace(frameNumber, CameraMetadata());
100 }
101}
102
103void CompositeStream::onBufferReleased(const BufferInfo& bufferInfo) {
104 Mutex::Autolock l(mMutex);
105 if (!mErrorState && !bufferInfo.mError) {
106 mFrameNumberMap.emplace(bufferInfo.mFrameNumber, bufferInfo.mTimestamp);
107 mInputReadyCondition.signal();
108 }
109}
110
111void CompositeStream::eraseResult(int64_t frameNumber) {
112 Mutex::Autolock l(mMutex);
113
114 auto it = mPendingCaptureResults.find(frameNumber);
115 if (it == mPendingCaptureResults.end()) {
116 return;
117 }
118
119 it = mPendingCaptureResults.erase(it);
120}
121
122void CompositeStream::onResultAvailable(const CaptureResult& result) {
123 bool resultError = false;
124 {
125 Mutex::Autolock l(mMutex);
126
127 uint64_t frameNumber = result.mResultExtras.frameNumber;
128 bool resultReady = false;
129 auto it = mPendingCaptureResults.find(frameNumber);
130 if (it != mPendingCaptureResults.end()) {
131 it->second.append(result.mMetadata);
132 if (result.mResultExtras.partialResultCount >= mNumPartialResults) {
133 auto entry = it->second.find(ANDROID_SENSOR_TIMESTAMP);
134 if (entry.count == 1) {
135 auto ts = entry.data.i64[0];
136 mCaptureResults.emplace(ts, std::make_tuple(frameNumber, it->second));
137 resultReady = true;
138 } else {
139 ALOGE("%s: Timestamp metadata entry missing for frameNumber: %" PRIu64,
140 __FUNCTION__, frameNumber);
141 resultError = true;
142 }
143 mPendingCaptureResults.erase(it);
144 }
145 }
146
147 if (resultReady) {
148 mInputReadyCondition.signal();
149 }
150 }
151
152 if (resultError) {
153 onResultError(result.mResultExtras);
154 }
155}
156
157void CompositeStream::flagAnErrorFrameNumber(int64_t frameNumber) {
158 Mutex::Autolock l(mMutex);
159 mErrorFrameNumbers.emplace(frameNumber);
160 mInputReadyCondition.signal();
161}
162
163status_t CompositeStream::registerCompositeStreamListener(int32_t streamId) {
164 sp<CameraDeviceBase> device = mDevice.promote();
165 if (device.get() == nullptr) {
166 return NO_INIT;
167 }
168
169 auto ret = device->addBufferListenerForStream(streamId, this);
170 if (ret != OK) {
171 ALOGE("%s: Failed to register composite stream listener!", __FUNCTION__);
172 }
173
174 return ret;
175}
176
177bool CompositeStream::onError(int32_t errorCode, const CaptureResultExtras& resultExtras) {
178 auto ret = false;
179 switch (errorCode) {
180 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
181 onResultError(resultExtras);
182 break;
183 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
184 ret = onStreamBufferError(resultExtras);
185 break;
186 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
Shuzhen Wange8675782019-12-05 09:12:14 -0800187 onRequestError(resultExtras);
Emilian Peev538c90e2018-12-17 18:03:19 +0000188 break;
189 default:
190 ALOGE("%s: Unrecoverable error: %d detected!", __FUNCTION__, errorCode);
191 Mutex::Autolock l(mMutex);
192 mErrorState = true;
193 break;
194 }
195
196 return ret;
197}
198
Shuzhen Wange8675782019-12-05 09:12:14 -0800199void CompositeStream::notifyError(int64_t frameNumber, int32_t requestId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000200 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb =
201 mRemoteCallback.promote();
202
203 if ((frameNumber >= 0) && (remoteCb.get() != nullptr)) {
204 CaptureResultExtras extras;
205 extras.errorStreamId = getStreamId();
206 extras.frameNumber = frameNumber;
Shuzhen Wange8675782019-12-05 09:12:14 -0800207 extras.requestId = requestId;
Emilian Peev538c90e2018-12-17 18:03:19 +0000208 remoteCb->onDeviceError(
209 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
210 extras);
211 }
212}
213
Emilian Peevc0fe54c2020-03-11 14:05:07 -0700214void CompositeStream::switchToOffline() {
215 Mutex::Autolock l(mMutex);
216 mDevice.clear();
217}
218
Emilian Peev538c90e2018-12-17 18:03:19 +0000219}; // namespace camera3
220}; // namespace android