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