1 /*
2 * Copyright (C) 2012-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 "Camera2-StreamingProcessor"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 //#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22 #ifdef LOG_NNDEBUG
23 #define ALOGVV(...) ALOGV(__VA_ARGS__)
24 #else
25 #define ALOGVV(...) ((void)0)
26 #endif
27
28 #include <cutils/properties.h>
29 #include <utils/Log.h>
30 #include <utils/Trace.h>
31 #include <gui/BufferItem.h>
32 #include <gui/Surface.h>
33 #include <media/hardware/HardwareAPI.h>
34 #include <camera/StringUtils.h>
35
36 #include "common/CameraDeviceBase.h"
37 #include "api1/Camera2Client.h"
38 #include "api1/client2/StreamingProcessor.h"
39 #include "api1/client2/Camera2Heap.h"
40
41 namespace android {
42 namespace camera2 {
43
44 using android::camera3::CAMERA_STREAM_ROTATION_0;
45 using android::camera3::CAMERA_TEMPLATE_PREVIEW;
46 using android::camera3::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
47
StreamingProcessor(sp<Camera2Client> client)48 StreamingProcessor::StreamingProcessor(sp<Camera2Client> client):
49 mClient(client),
50 mDevice(client->getCameraDevice()),
51 mId(client->getCameraId()),
52 mActiveRequest(NONE),
53 mPaused(false),
54 mPreviewRequestId(Camera2Client::kPreviewRequestIdStart),
55 mPreviewStreamId(NO_STREAM),
56 mRecordingRequestId(Camera2Client::kRecordingRequestIdStart),
57 mRecordingStreamId(NO_STREAM)
58 {
59 }
60
~StreamingProcessor()61 StreamingProcessor::~StreamingProcessor() {
62 deletePreviewStream();
63 deleteRecordingStream();
64 }
65
setPreviewWindow(const sp<Surface> & window)66 status_t StreamingProcessor::setPreviewWindow(const sp<Surface>& window) {
67 ATRACE_CALL();
68 status_t res;
69
70 res = deletePreviewStream();
71 if (res != OK) return res;
72
73 Mutex::Autolock m(mMutex);
74
75 mPreviewWindow = window;
76
77 return OK;
78 }
79
setRecordingWindow(const sp<Surface> & window)80 status_t StreamingProcessor::setRecordingWindow(const sp<Surface>& window) {
81 ATRACE_CALL();
82 status_t res;
83
84 res = deleteRecordingStream();
85 if (res != OK) return res;
86
87 Mutex::Autolock m(mMutex);
88
89 mRecordingWindow = window;
90
91 return OK;
92 }
93
haveValidPreviewWindow() const94 bool StreamingProcessor::haveValidPreviewWindow() const {
95 Mutex::Autolock m(mMutex);
96 return mPreviewWindow != 0;
97 }
98
haveValidRecordingWindow() const99 bool StreamingProcessor::haveValidRecordingWindow() const {
100 Mutex::Autolock m(mMutex);
101 return mRecordingWindow != nullptr;
102 }
103
updatePreviewRequest(const Parameters & params)104 status_t StreamingProcessor::updatePreviewRequest(const Parameters ¶ms) {
105 ATRACE_CALL();
106 status_t res;
107 sp<CameraDeviceBase> device = mDevice.promote();
108 if (device == 0) {
109 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
110 return INVALID_OPERATION;
111 }
112
113 Mutex::Autolock m(mMutex);
114 if (mPreviewRequest.entryCount() == 0) {
115 sp<Camera2Client> client = mClient.promote();
116 if (client == 0) {
117 ALOGE("%s: Camera %d: Client does not exist", __FUNCTION__, mId);
118 return INVALID_OPERATION;
119 }
120
121 // Use CAMERA_TEMPLATE_ZERO_SHUTTER_LAG for ZSL streaming case.
122 if (params.useZeroShutterLag() && !params.recordingHint) {
123 res = device->createDefaultRequest(
124 CAMERA_TEMPLATE_ZERO_SHUTTER_LAG, &mPreviewRequest);
125 } else {
126 res = device->createDefaultRequest(CAMERA_TEMPLATE_PREVIEW,
127 &mPreviewRequest);
128 }
129
130 if (res != OK) {
131 ALOGE("%s: Camera %d: Unable to create default preview request: "
132 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
133 return res;
134 }
135 }
136
137 res = params.updateRequest(&mPreviewRequest);
138 if (res != OK) {
139 ALOGE("%s: Camera %d: Unable to update common entries of preview "
140 "request: %s (%d)", __FUNCTION__, mId,
141 strerror(-res), res);
142 return res;
143 }
144
145 res = mPreviewRequest.update(ANDROID_REQUEST_ID,
146 &mPreviewRequestId, 1);
147 if (res != OK) {
148 ALOGE("%s: Camera %d: Unable to update request id for preview: %s (%d)",
149 __FUNCTION__, mId, strerror(-res), res);
150 return res;
151 }
152
153 return OK;
154 }
155
updatePreviewStream(const Parameters & params)156 status_t StreamingProcessor::updatePreviewStream(const Parameters ¶ms) {
157 ATRACE_CALL();
158 Mutex::Autolock m(mMutex);
159
160 status_t res;
161 sp<CameraDeviceBase> device = mDevice.promote();
162 if (device == 0) {
163 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
164 return INVALID_OPERATION;
165 }
166
167 if (mPreviewStreamId != NO_STREAM) {
168 // Check if stream parameters have to change
169 CameraDeviceBase::StreamInfo streamInfo;
170 res = device->getStreamInfo(mPreviewStreamId, &streamInfo);
171 if (res != OK) {
172 ALOGE("%s: Camera %d: Error querying preview stream info: "
173 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
174 return res;
175 }
176 if (streamInfo.width != (uint32_t)params.previewWidth ||
177 streamInfo.height != (uint32_t)params.previewHeight) {
178 ALOGV("%s: Camera %d: Preview size switch: %d x %d -> %d x %d",
179 __FUNCTION__, mId, streamInfo.width, streamInfo.height,
180 params.previewWidth, params.previewHeight);
181 res = device->waitUntilDrained();
182 if (res != OK) {
183 ALOGE("%s: Camera %d: Error waiting for preview to drain: "
184 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
185 return res;
186 }
187 res = device->deleteStream(mPreviewStreamId);
188 if (res != OK) {
189 ALOGE("%s: Camera %d: Unable to delete old output stream "
190 "for preview: %s (%d)", __FUNCTION__, mId,
191 strerror(-res), res);
192 return res;
193 }
194 mPreviewStreamId = NO_STREAM;
195 }
196 }
197
198 if (mPreviewStreamId == NO_STREAM) {
199 res = device->createStream(mPreviewWindow,
200 params.previewWidth, params.previewHeight,
201 CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, HAL_DATASPACE_UNKNOWN,
202 CAMERA_STREAM_ROTATION_0, &mPreviewStreamId, std::string(),
203 std::unordered_set<int32_t>{ANDROID_SENSOR_PIXEL_MODE_DEFAULT});
204 if (res != OK) {
205 ALOGE("%s: Camera %d: Unable to create preview stream: %s (%d)",
206 __FUNCTION__, mId, strerror(-res), res);
207 return res;
208 }
209 }
210
211 res = device->setStreamTransform(mPreviewStreamId,
212 params.previewTransform);
213 if (res != OK) {
214 ALOGE("%s: Camera %d: Unable to set preview stream transform: "
215 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
216 return res;
217 }
218
219 return OK;
220 }
221
deletePreviewStream()222 status_t StreamingProcessor::deletePreviewStream() {
223 ATRACE_CALL();
224 status_t res;
225
226 Mutex::Autolock m(mMutex);
227
228 if (mPreviewStreamId != NO_STREAM) {
229 sp<CameraDeviceBase> device = mDevice.promote();
230 if (device == 0) {
231 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
232 return INVALID_OPERATION;
233 }
234
235 ALOGV("%s: for cameraId %d on streamId %d",
236 __FUNCTION__, mId, mPreviewStreamId);
237
238 res = device->waitUntilDrained();
239 if (res != OK) {
240 ALOGE("%s: Error waiting for preview to drain: %s (%d)",
241 __FUNCTION__, strerror(-res), res);
242 return res;
243 }
244 res = device->deleteStream(mPreviewStreamId);
245 if (res != OK) {
246 ALOGE("%s: Unable to delete old preview stream: %s (%d)",
247 __FUNCTION__, strerror(-res), res);
248 return res;
249 }
250 mPreviewStreamId = NO_STREAM;
251 }
252 return OK;
253 }
254
getPreviewStreamId() const255 int StreamingProcessor::getPreviewStreamId() const {
256 Mutex::Autolock m(mMutex);
257 return mPreviewStreamId;
258 }
259
updateRecordingRequest(const Parameters & params)260 status_t StreamingProcessor::updateRecordingRequest(const Parameters ¶ms) {
261 ATRACE_CALL();
262 status_t res;
263 Mutex::Autolock m(mMutex);
264
265 sp<CameraDeviceBase> device = mDevice.promote();
266 if (device == 0) {
267 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
268 return INVALID_OPERATION;
269 }
270
271 if (mRecordingRequest.entryCount() == 0) {
272 res = device->createDefaultRequest(camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD,
273 &mRecordingRequest);
274 if (res != OK) {
275 ALOGE("%s: Camera %d: Unable to create default recording request:"
276 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
277 return res;
278 }
279 }
280
281 res = params.updateRequest(&mRecordingRequest);
282 if (res != OK) {
283 ALOGE("%s: Camera %d: Unable to update common entries of recording "
284 "request: %s (%d)", __FUNCTION__, mId,
285 strerror(-res), res);
286 return res;
287 }
288
289 res = mRecordingRequest.update(ANDROID_REQUEST_ID,
290 &mRecordingRequestId, 1);
291 if (res != OK) {
292 ALOGE("%s: Camera %d: Unable to update request id for request: %s (%d)",
293 __FUNCTION__, mId, strerror(-res), res);
294 return res;
295 }
296
297 return OK;
298 }
299
recordingStreamNeedsUpdate(const Parameters & params,bool * needsUpdate)300 status_t StreamingProcessor::recordingStreamNeedsUpdate(
301 const Parameters ¶ms, bool *needsUpdate) {
302 status_t res;
303
304 if (needsUpdate == 0) {
305 ALOGE("%s: Camera %d: invalid argument", __FUNCTION__, mId);
306 return INVALID_OPERATION;
307 }
308
309 if (mRecordingStreamId == NO_STREAM) {
310 *needsUpdate = true;
311 return OK;
312 }
313
314 sp<CameraDeviceBase> device = mDevice.promote();
315 if (device == 0) {
316 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
317 return INVALID_OPERATION;
318 }
319
320 CameraDeviceBase::StreamInfo streamInfo;
321 res = device->getStreamInfo(mRecordingStreamId, &streamInfo);
322 if (res != OK) {
323 ALOGE("%s: Camera %d: Error querying recording output stream info: "
324 "%s (%d)", __FUNCTION__, mId,
325 strerror(-res), res);
326 return res;
327 }
328
329 if (mRecordingWindow == nullptr ||
330 streamInfo.width != (uint32_t)params.videoWidth ||
331 streamInfo.height != (uint32_t)params.videoHeight ||
332 !streamInfo.matchFormat((uint32_t)params.videoFormat) ||
333 !streamInfo.matchDataSpace(params.videoDataSpace)) {
334 *needsUpdate = true;
335 return res;
336 }
337 *needsUpdate = false;
338 return res;
339 }
340
updateRecordingStream(const Parameters & params)341 status_t StreamingProcessor::updateRecordingStream(const Parameters ¶ms) {
342 ATRACE_CALL();
343 status_t res;
344 Mutex::Autolock m(mMutex);
345
346 sp<CameraDeviceBase> device = mDevice.promote();
347 if (device == 0) {
348 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
349 return INVALID_OPERATION;
350 }
351
352 if (mRecordingStreamId != NO_STREAM) {
353 // Check if stream parameters have to change
354 CameraDeviceBase::StreamInfo streamInfo;
355 res = device->getStreamInfo(mRecordingStreamId, &streamInfo);
356 if (res != OK) {
357 ALOGE("%s: Camera %d: Error querying recording output stream info: "
358 "%s (%d)", __FUNCTION__, mId,
359 strerror(-res), res);
360 return res;
361 }
362 if (streamInfo.width != (uint32_t)params.videoWidth ||
363 streamInfo.height != (uint32_t)params.videoHeight ||
364 !streamInfo.matchFormat((uint32_t)params.videoFormat) ||
365 !streamInfo.matchDataSpace(params.videoDataSpace)) {
366 // TODO: Should wait to be sure previous recording has finished
367 res = device->deleteStream(mRecordingStreamId);
368
369 if (res == -EBUSY) {
370 ALOGV("%s: Camera %d: Device is busy, call "
371 "updateRecordingStream after it becomes idle",
372 __FUNCTION__, mId);
373 return res;
374 } else if (res != OK) {
375 ALOGE("%s: Camera %d: Unable to delete old output stream "
376 "for recording: %s (%d)", __FUNCTION__,
377 mId, strerror(-res), res);
378 return res;
379 }
380 mRecordingStreamId = NO_STREAM;
381 }
382 }
383
384 if (mRecordingStreamId == NO_STREAM) {
385 res = device->createStream(mRecordingWindow,
386 params.videoWidth, params.videoHeight,
387 params.videoFormat, params.videoDataSpace,
388 CAMERA_STREAM_ROTATION_0, &mRecordingStreamId,
389 std::string(), std::unordered_set<int32_t>{ANDROID_SENSOR_PIXEL_MODE_DEFAULT});
390 if (res != OK) {
391 ALOGE("%s: Camera %d: Can't create output stream for recording: "
392 "%s (%d)", __FUNCTION__, mId,
393 strerror(-res), res);
394 return res;
395 }
396 }
397
398 return OK;
399 }
400
deleteRecordingStream()401 status_t StreamingProcessor::deleteRecordingStream() {
402 ATRACE_CALL();
403 status_t res;
404
405 Mutex::Autolock m(mMutex);
406
407 if (mRecordingStreamId != NO_STREAM) {
408 sp<CameraDeviceBase> device = mDevice.promote();
409 if (device == 0) {
410 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
411 return INVALID_OPERATION;
412 }
413
414 res = device->waitUntilDrained();
415 if (res != OK) {
416 ALOGE("%s: Error waiting for HAL to drain: %s (%d)",
417 __FUNCTION__, strerror(-res), res);
418 return res;
419 }
420 res = device->deleteStream(mRecordingStreamId);
421 if (res != OK) {
422 ALOGE("%s: Unable to delete recording stream: %s (%d)",
423 __FUNCTION__, strerror(-res), res);
424 return res;
425 }
426 mRecordingStreamId = NO_STREAM;
427 }
428 return OK;
429 }
430
getRecordingStreamId() const431 int StreamingProcessor::getRecordingStreamId() const {
432 return mRecordingStreamId;
433 }
434
startStream(StreamType type,const Vector<int32_t> & outputStreams)435 status_t StreamingProcessor::startStream(StreamType type,
436 const Vector<int32_t> &outputStreams) {
437 ATRACE_CALL();
438 status_t res;
439
440 if (type == NONE) return INVALID_OPERATION;
441
442 sp<CameraDeviceBase> device = mDevice.promote();
443 if (device == 0) {
444 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
445 return INVALID_OPERATION;
446 }
447
448 ALOGV("%s: Camera %d: type = %d", __FUNCTION__, mId, type);
449
450 Mutex::Autolock m(mMutex);
451
452 CameraMetadata &request = (type == PREVIEW) ?
453 mPreviewRequest : mRecordingRequest;
454
455 res = request.update(
456 ANDROID_REQUEST_OUTPUT_STREAMS,
457 outputStreams);
458 if (res != OK) {
459 ALOGE("%s: Camera %d: Unable to set up preview request: %s (%d)",
460 __FUNCTION__, mId, strerror(-res), res);
461 return res;
462 }
463
464 res = request.sort();
465 if (res != OK) {
466 ALOGE("%s: Camera %d: Error sorting preview request: %s (%d)",
467 __FUNCTION__, mId, strerror(-res), res);
468 return res;
469 }
470
471 res = device->setStreamingRequest(request);
472 if (res != OK) {
473 ALOGE("%s: Camera %d: Unable to set preview request to start preview: "
474 "%s (%d)",
475 __FUNCTION__, mId, strerror(-res), res);
476 return res;
477 }
478 mActiveRequest = type;
479 mPaused = false;
480 mActiveStreamIds = outputStreams;
481 return OK;
482 }
483
togglePauseStream(bool pause)484 status_t StreamingProcessor::togglePauseStream(bool pause) {
485 ATRACE_CALL();
486 status_t res;
487
488 sp<CameraDeviceBase> device = mDevice.promote();
489 if (device == 0) {
490 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
491 return INVALID_OPERATION;
492 }
493
494 ALOGV("%s: Camera %d: toggling pause to %d", __FUNCTION__, mId, pause);
495
496 Mutex::Autolock m(mMutex);
497
498 if (mActiveRequest == NONE) {
499 ALOGE("%s: Camera %d: Can't toggle pause, streaming was not started",
500 __FUNCTION__, mId);
501 return INVALID_OPERATION;
502 }
503
504 if (mPaused == pause) {
505 return OK;
506 }
507
508 if (pause) {
509 res = device->clearStreamingRequest();
510 if (res != OK) {
511 ALOGE("%s: Camera %d: Can't clear stream request: %s (%d)",
512 __FUNCTION__, mId, strerror(-res), res);
513 return res;
514 }
515 } else {
516 CameraMetadata &request =
517 (mActiveRequest == PREVIEW) ? mPreviewRequest
518 : mRecordingRequest;
519 res = device->setStreamingRequest(request);
520 if (res != OK) {
521 ALOGE("%s: Camera %d: Unable to set preview request to resume: "
522 "%s (%d)",
523 __FUNCTION__, mId, strerror(-res), res);
524 return res;
525 }
526 }
527
528 mPaused = pause;
529 return OK;
530 }
531
stopStream()532 status_t StreamingProcessor::stopStream() {
533 ATRACE_CALL();
534 status_t res;
535
536 Mutex::Autolock m(mMutex);
537
538 sp<CameraDeviceBase> device = mDevice.promote();
539 if (device == 0) {
540 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
541 return INVALID_OPERATION;
542 }
543
544 res = device->clearStreamingRequest();
545 if (res != OK) {
546 ALOGE("%s: Camera %d: Can't clear stream request: %s (%d)",
547 __FUNCTION__, mId, strerror(-res), res);
548 return res;
549 }
550
551 mActiveRequest = NONE;
552 mActiveStreamIds.clear();
553 mPaused = false;
554
555 return OK;
556 }
557
getActiveRequestId() const558 int32_t StreamingProcessor::getActiveRequestId() const {
559 Mutex::Autolock m(mMutex);
560 switch (mActiveRequest) {
561 case NONE:
562 return 0;
563 case PREVIEW:
564 return mPreviewRequestId;
565 case RECORD:
566 return mRecordingRequestId;
567 default:
568 ALOGE("%s: Unexpected mode %d", __FUNCTION__, mActiveRequest);
569 return 0;
570 }
571 }
572
incrementStreamingIds()573 status_t StreamingProcessor::incrementStreamingIds() {
574 ATRACE_CALL();
575 Mutex::Autolock m(mMutex);
576
577 mPreviewRequestId++;
578 if (mPreviewRequestId >= Camera2Client::kPreviewRequestIdEnd) {
579 mPreviewRequestId = Camera2Client::kPreviewRequestIdStart;
580 }
581 mRecordingRequestId++;
582 if (mRecordingRequestId >= Camera2Client::kRecordingRequestIdEnd) {
583 mRecordingRequestId = Camera2Client::kRecordingRequestIdStart;
584 }
585 return OK;
586 }
587
dump(int fd,const Vector<String16> &)588 status_t StreamingProcessor::dump(int fd, const Vector<String16>& /*args*/) {
589 std::string result;
590
591 result += " Current requests:\n";
592 if (mPreviewRequest.entryCount() != 0) {
593 result += " Preview request:\n";
594 write(fd, result.c_str(), result.size());
595 mPreviewRequest.dump(fd, 2, 6);
596 result.clear();
597 } else {
598 result += " Preview request: undefined\n";
599 }
600
601 if (mRecordingRequest.entryCount() != 0) {
602 result = " Recording request:\n";
603 write(fd, result.c_str(), result.size());
604 mRecordingRequest.dump(fd, 2, 6);
605 result.clear();
606 } else {
607 result = " Recording request: undefined\n";
608 }
609
610 const char* streamTypeString[] = {
611 "none", "preview", "record"
612 };
613 result += fmt::sprintf(" Active request: %s (paused: %s\n",
614 streamTypeString[mActiveRequest],
615 mPaused ? "yes" : "no");
616
617 write(fd, result.c_str(), result.size());
618
619 return OK;
620 }
621
622 }; // namespace camera2
623 }; // namespace android
624