1 /*
2 * Copyright (C) 2012 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);
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 if (res != OK) {
384 ALOGE("%s: Camera %d: Can't create output stream for recording: "
385 "%s (%d)", __FUNCTION__, mId,
386 strerror(-res), res);
387 return res;
388 }
389 }
390
391 return OK;
392 }
393
deleteRecordingStream()394 status_t StreamingProcessor::deleteRecordingStream() {
395 ATRACE_CALL();
396 status_t res;
397
398 Mutex::Autolock m(mMutex);
399
400 if (mRecordingStreamId != NO_STREAM) {
401 sp<CameraDeviceBase> device = mDevice.promote();
402 if (device == 0) {
403 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
404 return INVALID_OPERATION;
405 }
406
407 res = device->waitUntilDrained();
408 if (res != OK) {
409 ALOGE("%s: Error waiting for HAL to drain: %s (%d)",
410 __FUNCTION__, strerror(-res), res);
411 return res;
412 }
413 res = device->deleteStream(mRecordingStreamId);
414 if (res != OK) {
415 ALOGE("%s: Unable to delete recording stream: %s (%d)",
416 __FUNCTION__, strerror(-res), res);
417 return res;
418 }
419 mRecordingStreamId = NO_STREAM;
420 }
421 return OK;
422 }
423
getRecordingStreamId() const424 int StreamingProcessor::getRecordingStreamId() const {
425 return mRecordingStreamId;
426 }
427
startStream(StreamType type,const Vector<int32_t> & outputStreams)428 status_t StreamingProcessor::startStream(StreamType type,
429 const Vector<int32_t> &outputStreams) {
430 ATRACE_CALL();
431 status_t res;
432
433 if (type == NONE) return INVALID_OPERATION;
434
435 sp<CameraDeviceBase> device = mDevice.promote();
436 if (device == 0) {
437 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
438 return INVALID_OPERATION;
439 }
440
441 ALOGV("%s: Camera %d: type = %d", __FUNCTION__, mId, type);
442
443 Mutex::Autolock m(mMutex);
444
445 CameraMetadata &request = (type == PREVIEW) ?
446 mPreviewRequest : mRecordingRequest;
447
448 res = request.update(
449 ANDROID_REQUEST_OUTPUT_STREAMS,
450 outputStreams);
451 if (res != OK) {
452 ALOGE("%s: Camera %d: Unable to set up preview request: %s (%d)",
453 __FUNCTION__, mId, strerror(-res), res);
454 return res;
455 }
456
457 res = request.sort();
458 if (res != OK) {
459 ALOGE("%s: Camera %d: Error sorting preview request: %s (%d)",
460 __FUNCTION__, mId, strerror(-res), res);
461 return res;
462 }
463
464 res = device->setStreamingRequest(request);
465 if (res != OK) {
466 ALOGE("%s: Camera %d: Unable to set preview request to start preview: "
467 "%s (%d)",
468 __FUNCTION__, mId, strerror(-res), res);
469 return res;
470 }
471 mActiveRequest = type;
472 mPaused = false;
473 mActiveStreamIds = outputStreams;
474 return OK;
475 }
476
togglePauseStream(bool pause)477 status_t StreamingProcessor::togglePauseStream(bool pause) {
478 ATRACE_CALL();
479 status_t res;
480
481 sp<CameraDeviceBase> device = mDevice.promote();
482 if (device == 0) {
483 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
484 return INVALID_OPERATION;
485 }
486
487 ALOGV("%s: Camera %d: toggling pause to %d", __FUNCTION__, mId, pause);
488
489 Mutex::Autolock m(mMutex);
490
491 if (mActiveRequest == NONE) {
492 ALOGE("%s: Camera %d: Can't toggle pause, streaming was not started",
493 __FUNCTION__, mId);
494 return INVALID_OPERATION;
495 }
496
497 if (mPaused == pause) {
498 return OK;
499 }
500
501 if (pause) {
502 res = device->clearStreamingRequest();
503 if (res != OK) {
504 ALOGE("%s: Camera %d: Can't clear stream request: %s (%d)",
505 __FUNCTION__, mId, strerror(-res), res);
506 return res;
507 }
508 } else {
509 CameraMetadata &request =
510 (mActiveRequest == PREVIEW) ? mPreviewRequest
511 : mRecordingRequest;
512 res = device->setStreamingRequest(request);
513 if (res != OK) {
514 ALOGE("%s: Camera %d: Unable to set preview request to resume: "
515 "%s (%d)",
516 __FUNCTION__, mId, strerror(-res), res);
517 return res;
518 }
519 }
520
521 mPaused = pause;
522 return OK;
523 }
524
stopStream()525 status_t StreamingProcessor::stopStream() {
526 ATRACE_CALL();
527 status_t res;
528
529 Mutex::Autolock m(mMutex);
530
531 sp<CameraDeviceBase> device = mDevice.promote();
532 if (device == 0) {
533 ALOGE("%s: Camera %d: Device does not exist", __FUNCTION__, mId);
534 return INVALID_OPERATION;
535 }
536
537 res = device->clearStreamingRequest();
538 if (res != OK) {
539 ALOGE("%s: Camera %d: Can't clear stream request: %s (%d)",
540 __FUNCTION__, mId, strerror(-res), res);
541 return res;
542 }
543
544 mActiveRequest = NONE;
545 mActiveStreamIds.clear();
546 mPaused = false;
547
548 return OK;
549 }
550
getActiveRequestId() const551 int32_t StreamingProcessor::getActiveRequestId() const {
552 Mutex::Autolock m(mMutex);
553 switch (mActiveRequest) {
554 case NONE:
555 return 0;
556 case PREVIEW:
557 return mPreviewRequestId;
558 case RECORD:
559 return mRecordingRequestId;
560 default:
561 ALOGE("%s: Unexpected mode %d", __FUNCTION__, mActiveRequest);
562 return 0;
563 }
564 }
565
incrementStreamingIds()566 status_t StreamingProcessor::incrementStreamingIds() {
567 ATRACE_CALL();
568 Mutex::Autolock m(mMutex);
569
570 mPreviewRequestId++;
571 if (mPreviewRequestId >= Camera2Client::kPreviewRequestIdEnd) {
572 mPreviewRequestId = Camera2Client::kPreviewRequestIdStart;
573 }
574 mRecordingRequestId++;
575 if (mRecordingRequestId >= Camera2Client::kRecordingRequestIdEnd) {
576 mRecordingRequestId = Camera2Client::kRecordingRequestIdStart;
577 }
578 return OK;
579 }
580
dump(int fd,const Vector<String16> &)581 status_t StreamingProcessor::dump(int fd, const Vector<String16>& /*args*/) {
582 String8 result;
583
584 result.append(" Current requests:\n");
585 if (mPreviewRequest.entryCount() != 0) {
586 result.append(" Preview request:\n");
587 write(fd, result.string(), result.size());
588 mPreviewRequest.dump(fd, 2, 6);
589 result.clear();
590 } else {
591 result.append(" Preview request: undefined\n");
592 }
593
594 if (mRecordingRequest.entryCount() != 0) {
595 result = " Recording request:\n";
596 write(fd, result.string(), result.size());
597 mRecordingRequest.dump(fd, 2, 6);
598 result.clear();
599 } else {
600 result = " Recording request: undefined\n";
601 }
602
603 const char* streamTypeString[] = {
604 "none", "preview", "record"
605 };
606 result.append(String8::format(" Active request: %s (paused: %s)\n",
607 streamTypeString[mActiveRequest],
608 mPaused ? "yes" : "no"));
609
610 write(fd, result.string(), result.size());
611
612 return OK;
613 }
614
615 }; // namespace camera2
616 }; // namespace android
617