blob: 9f380410c58bfcc559ac8bb3c93e96048e900c86 [file] [log] [blame]
Scott Randolph5c99d852016-11-15 17:01:23 -08001/*
2 * Copyright (C) 2016 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 "android.hardware.evs@1.0-service"
18
19#include "EvsEnumerator.h"
20#include "EvsCamera.h"
21#include "EvsDisplay.h"
22
23namespace android {
24namespace hardware {
25namespace evs {
26namespace V1_0 {
27namespace implementation {
28
29
30EvsEnumerator::EvsEnumerator() {
31 ALOGD("EvsEnumerator created");
32
33 // Add sample camera data to our list of cameras
34 // NOTE: The id strings trigger special initialization inside the EvsCamera constructor
35 mCameraList.emplace_back( new EvsCamera(EvsCamera::kCameraName_Backup), false );
36 mCameraList.emplace_back( new EvsCamera("LaneView"), false );
37 mCameraList.emplace_back( new EvsCamera(EvsCamera::kCameraName_RightTurn), false );
38}
39
40// Methods from ::android::hardware::evs::V1_0::IEvsEnumerator follow.
41Return<void> EvsEnumerator::getCameraList(getCameraList_cb _hidl_cb) {
42 ALOGD("getCameraList");
43
44 const unsigned numCameras = mCameraList.size();
45
46 // Build up a packed array of CameraDesc for return
47 // NOTE: Only has to live until the callback returns
48 std::vector<CameraDesc> descriptions;
49 descriptions.reserve(numCameras);
50 for (const auto& cam : mCameraList) {
51 descriptions.push_back( cam.pCamera->getDesc() );
52 }
53
54 // Encapsulate our camera descriptions in the HIDL vec type
55 hidl_vec<CameraDesc> hidlCameras(descriptions);
56
57 // Send back the results
58 ALOGD("reporting %zu cameras available", hidlCameras.size());
59 _hidl_cb(hidlCameras);
60
61 // HIDL convention says we return Void if we sent our result back via callback
62 return Void();
63}
64
65Return<void> EvsEnumerator::openCamera(const hidl_string& cameraId,
66 openCamera_cb callback) {
67 ALOGD("openCamera");
68
69 // Find the named camera
70 CameraRecord *pRecord = nullptr;
71 for (auto &&cam : mCameraList) {
72 if (cam.pCamera->getDesc().cameraId == cameraId) {
73 // Found a match!
74 pRecord = &cam;
75 break;
76 }
77 }
78
79 if (!pRecord) {
80 ALOGE("Requested camera %s not found", cameraId.c_str());
81 callback(nullptr);
82 }
83 else if (pRecord->inUse) {
84 ALOGE("Cannot open camera %s which is already in use", cameraId.c_str());
85 callback(nullptr);
86 }
87 else {
88 /* TODO(b/33492405): Do this, When HIDL can give us back a recognizable pointer
89 pRecord->inUse = true;
90 */
91 callback(pRecord->pCamera);
92 }
93
94 return Void();
95}
96
97Return<void> EvsEnumerator::closeCamera(const ::android::sp<IEvsCamera>& camera) {
98 ALOGD("closeCamera");
99
100 if (camera == nullptr) {
101 ALOGE("Ignoring call to closeCamera with null camera pointer");
102 }
103 else {
104 // Make sure the camera has stopped streaming
105 camera->stopVideoStream();
106
107 /* TODO(b/33492405): Do this, When HIDL can give us back a recognizable pointer
108 pRecord->inUse = false;
109 */
110 }
111
112 return Void();
113}
114
115Return<void> EvsEnumerator::openDisplay(openDisplay_cb callback) {
116 ALOGD("openDisplay");
117
118 // If we already have a display active, then this request must be denied
119 if (mActiveDisplay != nullptr) {
120 ALOGW("Rejecting openDisplay request the display is already in use.");
121 callback(nullptr);
122 }
123 else {
124 // Create a new display interface and return it
125 mActiveDisplay = new EvsDisplay();
126 ALOGD("Returning new EvsDisplay object %p", mActiveDisplay.get());
127 callback(mActiveDisplay);
128 }
129
130 return Void();
131}
132
133Return<void> EvsEnumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
134 ALOGD("closeDisplay");
135
136 if (mActiveDisplay == nullptr) {
137 ALOGE("Ignoring closeDisplay when display is not active");
138 }
139 else if (display == nullptr) {
140 ALOGE("Ignoring closeDisplay with null display pointer");
141 }
142 else {
143 // Drop the active display
144 // TODO(b/33492405): When HIDL provides recognizable pointers, add validation here.
145 mActiveDisplay = nullptr;
146 }
147
148 return Void();
149}
150
151
152// TODO(b/31632518): Need to get notification when our client dies so we can close the camera.
153// As possible work around would be to give the client a HIDL object to exclusively hold
154// and use it's destructor to perform some work in the server side.
155
156
157} // namespace implementation
158} // namespace V1_0
159} // namespace evs
160} // namespace hardware
161} // namespace android