Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace evs { |
| 26 | namespace V1_0 { |
| 27 | namespace implementation { |
| 28 | |
| 29 | |
| 30 | EvsEnumerator::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. |
| 41 | Return<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 | |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 65 | Return<sp<IEvsCamera>> EvsEnumerator::openCamera(const hidl_string& cameraId) { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 66 | ALOGD("openCamera"); |
| 67 | |
| 68 | // Find the named camera |
| 69 | CameraRecord *pRecord = nullptr; |
| 70 | for (auto &&cam : mCameraList) { |
| 71 | if (cam.pCamera->getDesc().cameraId == cameraId) { |
| 72 | // Found a match! |
| 73 | pRecord = &cam; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (!pRecord) { |
| 79 | ALOGE("Requested camera %s not found", cameraId.c_str()); |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 80 | return nullptr; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 81 | } |
| 82 | else if (pRecord->inUse) { |
| 83 | ALOGE("Cannot open camera %s which is already in use", cameraId.c_str()); |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 84 | return nullptr; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 85 | } |
| 86 | else { |
| 87 | /* TODO(b/33492405): Do this, When HIDL can give us back a recognizable pointer |
| 88 | pRecord->inUse = true; |
| 89 | */ |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 90 | return(pRecord->pCamera); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 91 | } |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Return<void> EvsEnumerator::closeCamera(const ::android::sp<IEvsCamera>& camera) { |
| 95 | ALOGD("closeCamera"); |
| 96 | |
| 97 | if (camera == nullptr) { |
| 98 | ALOGE("Ignoring call to closeCamera with null camera pointer"); |
| 99 | } |
| 100 | else { |
| 101 | // Make sure the camera has stopped streaming |
| 102 | camera->stopVideoStream(); |
| 103 | |
| 104 | /* TODO(b/33492405): Do this, When HIDL can give us back a recognizable pointer |
| 105 | pRecord->inUse = false; |
| 106 | */ |
| 107 | } |
| 108 | |
| 109 | return Void(); |
| 110 | } |
| 111 | |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 112 | Return<sp<IEvsDisplay>> EvsEnumerator::openDisplay() { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 113 | ALOGD("openDisplay"); |
| 114 | |
| 115 | // If we already have a display active, then this request must be denied |
| 116 | if (mActiveDisplay != nullptr) { |
| 117 | ALOGW("Rejecting openDisplay request the display is already in use."); |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 118 | return nullptr; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 119 | } |
| 120 | else { |
| 121 | // Create a new display interface and return it |
| 122 | mActiveDisplay = new EvsDisplay(); |
| 123 | ALOGD("Returning new EvsDisplay object %p", mActiveDisplay.get()); |
Martijn Coenen | 527924a | 2017-01-04 12:59:48 +0100 | [diff] [blame] | 124 | return mActiveDisplay; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 125 | } |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Return<void> EvsEnumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) { |
| 129 | ALOGD("closeDisplay"); |
| 130 | |
| 131 | if (mActiveDisplay == nullptr) { |
| 132 | ALOGE("Ignoring closeDisplay when display is not active"); |
| 133 | } |
| 134 | else if (display == nullptr) { |
| 135 | ALOGE("Ignoring closeDisplay with null display pointer"); |
| 136 | } |
| 137 | else { |
| 138 | // Drop the active display |
| 139 | // TODO(b/33492405): When HIDL provides recognizable pointers, add validation here. |
| 140 | mActiveDisplay = nullptr; |
| 141 | } |
| 142 | |
| 143 | return Void(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | // TODO(b/31632518): Need to get notification when our client dies so we can close the camera. |
| 148 | // As possible work around would be to give the client a HIDL object to exclusively hold |
| 149 | // and use it's destructor to perform some work in the server side. |
| 150 | |
| 151 | |
| 152 | } // namespace implementation |
| 153 | } // namespace V1_0 |
| 154 | } // namespace evs |
| 155 | } // namespace hardware |
| 156 | } // namespace android |