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