Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 17 | #include "v4l2_camera.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 18 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 20 | #include <linux/videodev2.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 23 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 24 | #include <cstdlib> |
| 25 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 26 | #include <camera/CameraMetadata.h> |
| 27 | #include <hardware/camera3.h> |
| 28 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 29 | #include "common.h" |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 30 | #include "function_thread.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 31 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 32 | #include "stream_format.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 33 | #include "v4l2_metadata_factory.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 34 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 35 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 36 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 37 | namespace v4l2_camera_hal { |
| 38 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 39 | // Helper function for managing metadata. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 40 | static std::vector<int32_t> getMetadataKeys(const camera_metadata_t* metadata) { |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 41 | std::vector<int32_t> keys; |
| 42 | size_t num_entries = get_camera_metadata_entry_count(metadata); |
| 43 | for (size_t i = 0; i < num_entries; ++i) { |
| 44 | camera_metadata_ro_entry_t entry; |
| 45 | get_camera_metadata_ro_entry(metadata, i, &entry); |
| 46 | keys.push_back(entry.tag); |
| 47 | } |
| 48 | return keys; |
| 49 | } |
| 50 | |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 51 | V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) { |
| 52 | HAL_LOG_ENTER(); |
| 53 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 54 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path)); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 55 | if (!v4l2_wrapper) { |
| 56 | HAL_LOGE("Failed to initialize V4L2 wrapper."); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 57 | return nullptr; |
| 58 | } |
| 59 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 60 | std::unique_ptr<Metadata> metadata; |
| 61 | int res = GetV4L2Metadata(v4l2_wrapper, &metadata); |
| 62 | if (res) { |
| 63 | HAL_LOGE("Failed to initialize V4L2 metadata: %d", res); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata)); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 70 | V4L2Camera::V4L2Camera(int id, |
| 71 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper, |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 72 | std::unique_ptr<Metadata> metadata) |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 73 | : default_camera_hal::Camera(id), |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 74 | device_(std::move(v4l2_wrapper)), |
| 75 | metadata_(std::move(metadata)), |
| 76 | max_input_streams_(0), |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 77 | max_output_streams_({{0, 0, 0}}), |
| 78 | buffer_enqueuer_(new FunctionThread( |
| 79 | std::bind(&V4L2Camera::enqueueRequestBuffers, this))), |
| 80 | buffer_dequeuer_(new FunctionThread( |
| 81 | std::bind(&V4L2Camera::dequeueRequestBuffers, this))) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 82 | HAL_LOG_ENTER(); |
| 83 | } |
| 84 | |
| 85 | V4L2Camera::~V4L2Camera() { |
| 86 | HAL_LOG_ENTER(); |
| 87 | } |
| 88 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 89 | int V4L2Camera::connect() { |
| 90 | HAL_LOG_ENTER(); |
| 91 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 92 | if (connection_) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 93 | HAL_LOGE("Already connected. Please disconnect and try again."); |
| 94 | return -EIO; |
| 95 | } |
| 96 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 97 | connection_.reset(new V4L2Wrapper::Connection(device_)); |
| 98 | if (connection_->status()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 99 | HAL_LOGE("Failed to connect to device."); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 100 | return connection_->status(); |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 103 | // TODO(b/29185945): confirm this is a supported device. |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 104 | // This is checked by the HAL, but the device at |device_|'s path may |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 105 | // not be the same one that was there when the HAL was loaded. |
| 106 | // (Alternatively, better hotplugging support may make this unecessary |
| 107 | // by disabling cameras that get disconnected and checking newly connected |
| 108 | // cameras, so connect() is never called on an unsupported camera) |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 109 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 110 | // TODO(b/29158098): Inform service of any flashes that are no longer |
| 111 | // available because this camera is in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | void V4L2Camera::disconnect() { |
| 116 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 117 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 118 | connection_.reset(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 119 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 120 | // TODO(b/29158098): Inform service of any flashes that are available again |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 121 | // because this camera is no longer in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 124 | int V4L2Camera::flushBuffers() { |
| 125 | HAL_LOG_ENTER(); |
| 126 | |
| 127 | int res = device_->StreamOff(); |
| 128 | |
| 129 | // This is not strictly necessary, but prevents a buildup of aborted |
| 130 | // requests in the in_flight_ map. These should be cleared |
| 131 | // whenever the stream is turned off. |
| 132 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
| 133 | in_flight_.clear(); |
| 134 | |
| 135 | return res; |
| 136 | } |
| 137 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 138 | int V4L2Camera::initStaticInfo(android::CameraMetadata* out) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 139 | HAL_LOG_ENTER(); |
| 140 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 141 | int res = metadata_->FillStaticMetadata(out); |
| 142 | if (res) { |
| 143 | HAL_LOGE("Failed to get static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 144 | return res; |
| 145 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 146 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 147 | // Extract max streams for use in verifying stream configs. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 148 | res = SingleTagValue( |
| 149 | *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 150 | if (res) { |
| 151 | HAL_LOGE("Failed to get max num input streams from static metadata."); |
| 152 | return res; |
| 153 | } |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 154 | res = SingleTagValue( |
| 155 | *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 156 | if (res) { |
| 157 | HAL_LOGE("Failed to get max num output streams from static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 158 | return res; |
| 159 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 160 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 161 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 164 | int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) { |
| 165 | HAL_LOG_ENTER(); |
| 166 | |
| 167 | return metadata_->GetRequestTemplate(type, out); |
| 168 | } |
| 169 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 170 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 171 | HAL_LOG_ENTER(); |
| 172 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 173 | // TODO(b/31044975): move this into device interface. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 174 | // For now, just constants. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 175 | info->resource_cost = 100; |
| 176 | info->conflicting_devices = nullptr; |
| 177 | info->conflicting_devices_length = 0; |
| 178 | } |
| 179 | |
| 180 | int V4L2Camera::initDevice() { |
| 181 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 182 | |
| 183 | // Start the buffer enqueue/dequeue threads if they're not already running. |
| 184 | if (!buffer_enqueuer_->isRunning()) { |
| 185 | android::status_t res = buffer_enqueuer_->run("Enqueue buffers"); |
| 186 | if (res != android::OK) { |
| 187 | HAL_LOGE("Failed to start buffer enqueue thread: %d", res); |
| 188 | return -ENODEV; |
| 189 | } |
| 190 | } |
| 191 | if (!buffer_dequeuer_->isRunning()) { |
| 192 | android::status_t res = buffer_dequeuer_->run("Dequeue buffers"); |
| 193 | if (res != android::OK) { |
| 194 | HAL_LOGE("Failed to start buffer dequeue thread: %d", res); |
| 195 | return -ENODEV; |
| 196 | } |
| 197 | } |
| 198 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 202 | int V4L2Camera::enqueueRequest( |
| 203 | std::shared_ptr<default_camera_hal::CaptureRequest> request) { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 204 | HAL_LOG_ENTER(); |
| 205 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 206 | // Assume request validated before calling this function. |
| 207 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 208 | { |
| 209 | std::lock_guard<std::mutex> guard(request_queue_lock_); |
| 210 | request_queue_.push(request); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 211 | requests_available_.notify_one(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 212 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 213 | |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 217 | std::shared_ptr<default_camera_hal::CaptureRequest> |
| 218 | V4L2Camera::dequeueRequest() { |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 219 | std::unique_lock<std::mutex> lock(request_queue_lock_); |
| 220 | while (request_queue_.empty()) { |
| 221 | requests_available_.wait(lock); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 222 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 223 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 224 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 225 | request_queue_.front(); |
| 226 | request_queue_.pop(); |
| 227 | return request; |
| 228 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 229 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 230 | bool V4L2Camera::enqueueRequestBuffers() { |
| 231 | // Get a request from the queue (blocks this thread until one is available). |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 232 | std::shared_ptr<default_camera_hal::CaptureRequest> request = |
| 233 | dequeueRequest(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 234 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 235 | // Assume request validated before being added to the queue |
| 236 | // (For now, always exactly 1 output buffer, no inputs). |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 237 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 238 | // Setting and getting settings are best effort here, |
| 239 | // since there's no way to know through V4L2 exactly what |
| 240 | // settings are used for a buffer unless we were to enqueue them |
| 241 | // one at a time, which would be too slow. |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 242 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 243 | // Set the requested settings |
| 244 | int res = metadata_->SetRequestSettings(request->settings); |
| 245 | if (res) { |
| 246 | HAL_LOGE("Failed to set settings."); |
| 247 | completeRequest(request, res); |
| 248 | return true; |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 251 | // Replace the requested settings with a snapshot of |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 252 | // the used settings/state immediately before enqueue. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 253 | res = metadata_->FillResultMetadata(&request->settings); |
| 254 | if (res) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 255 | // Note: since request is a shared pointer, this may happen if another |
| 256 | // thread has already decided to complete the request (e.g. via flushing), |
| 257 | // since that locks the metadata (in that case, this failing is fine, |
| 258 | // and completeRequest will simply do nothing). |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 259 | HAL_LOGE("Failed to fill result metadata."); |
| 260 | completeRequest(request, res); |
| 261 | return true; |
| 262 | } |
| 263 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 264 | // Actually enqueue the buffer for capture. |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 265 | { |
| 266 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 267 | |
| 268 | uint32_t index; |
| 269 | res = device_->EnqueueBuffer(&request->output_buffers[0], &index); |
| 270 | if (res) { |
| 271 | HAL_LOGE("Device failed to enqueue buffer."); |
| 272 | completeRequest(request, res); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | // Make sure the stream is on (no effect if already on). |
| 277 | res = device_->StreamOn(); |
| 278 | if (res) { |
| 279 | HAL_LOGE("Device failed to turn on stream."); |
| 280 | // Don't really want to send an error for only the request here, |
| 281 | // since this is a full device error. |
| 282 | // TODO: Should trigger full flush. |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | // Note: the request should be dequeued/flushed from the device |
| 287 | // before removal from in_flight_. |
| 288 | in_flight_.emplace(index, request); |
| 289 | buffers_in_flight_.notify_one(); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 290 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 291 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 292 | return true; |
| 293 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 294 | |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 295 | bool V4L2Camera::dequeueRequestBuffers() { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 296 | // Dequeue a buffer. |
| 297 | uint32_t result_index; |
| 298 | int res = device_->DequeueBuffer(&result_index); |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 299 | if (res) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 300 | if (res == -EAGAIN) { |
| 301 | // EAGAIN just means nothing to dequeue right now. |
| 302 | // Wait until something is available before looping again. |
| 303 | std::unique_lock<std::mutex> lock(in_flight_lock_); |
| 304 | while (in_flight_.empty()) { |
| 305 | buffers_in_flight_.wait(lock); |
| 306 | } |
| 307 | } else { |
| 308 | HAL_LOGW("Device failed to dequeue buffer: %d", res); |
| 309 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 310 | return true; |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 313 | // Find the associated request and complete it. |
| 314 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
| 315 | auto index_request = in_flight_.find(result_index); |
| 316 | if (index_request != in_flight_.end()) { |
| 317 | completeRequest(index_request->second, 0); |
| 318 | in_flight_.erase(index_request); |
| 319 | } else { |
| 320 | HAL_LOGW( |
| 321 | "Dequeued non in-flight buffer index %d. " |
| 322 | "This buffer may have been flushed from the HAL but not the device.", |
| 323 | index_request->first); |
| 324 | } |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 325 | return true; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 328 | bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams, |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 329 | int count, |
| 330 | uint32_t mode) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 331 | HAL_LOG_ENTER(); |
| 332 | |
| 333 | if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) { |
| 334 | HAL_LOGE("Unsupported stream configuration mode: %d", mode); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | // This should be checked by the caller, but put here as a sanity check. |
| 339 | if (count < 1) { |
| 340 | HAL_LOGE("Must request at least 1 stream"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // Count the number of streams of each type. |
| 345 | int32_t num_input = 0; |
| 346 | int32_t num_raw = 0; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 347 | int32_t num_stalling = 0; |
| 348 | int32_t num_non_stalling = 0; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 349 | for (int i = 0; i < count; ++i) { |
| 350 | default_camera_hal::Stream* stream = streams[i]; |
| 351 | |
| 352 | if (stream->isInputType()) { |
| 353 | ++num_input; |
| 354 | } |
| 355 | |
| 356 | if (stream->isOutputType()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 357 | StreamFormat format(*stream); |
| 358 | switch (format.Category()) { |
| 359 | case kFormatCategoryRaw: |
| 360 | ++num_raw; |
| 361 | case kFormatCategoryStalling: |
| 362 | ++num_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 363 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 364 | case kFormatCategoryNonStalling: |
| 365 | ++num_non_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 366 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 367 | case kFormatCategoryUnknown: // Fall through. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 368 | default: |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 369 | HAL_LOGE( |
| 370 | "Unsupported format for stream %d: %d", i, stream->getFormat()); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 376 | if (num_input > max_input_streams_ || num_raw > max_output_streams_[0] || |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 377 | num_non_stalling > max_output_streams_[1] || |
| 378 | num_stalling > max_output_streams_[2]) { |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 379 | HAL_LOGE( |
| 380 | "Invalid stream configuration: %d input, %d RAW, %d non-stalling, " |
| 381 | "%d stalling (max supported: %d input, %d RAW, %d non-stalling, " |
| 382 | "%d stalling)", |
| 383 | max_input_streams_, |
| 384 | max_output_streams_[0], |
| 385 | max_output_streams_[1], |
| 386 | max_output_streams_[2], |
| 387 | num_input, |
| 388 | num_raw, |
| 389 | num_non_stalling, |
| 390 | num_stalling); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | |
| 394 | // TODO(b/29939583): The above logic should be all that's necessary, |
| 395 | // but V4L2 doesn't actually support more than 1 stream at a time. So for now, |
| 396 | // if not all streams are the same format and size, error. Note that this |
| 397 | // means the HAL is not spec-compliant; the requested streams are technically |
| 398 | // valid and it is not technically allowed to error once it has reached this |
| 399 | // point. |
| 400 | int format = streams[0]->getFormat(); |
| 401 | uint32_t width = streams[0]->getWidth(); |
| 402 | uint32_t height = streams[0]->getHeight(); |
| 403 | for (int i = 1; i < count; ++i) { |
| 404 | const default_camera_hal::Stream* stream = streams[i]; |
| 405 | if (stream->getFormat() != format || stream->getWidth() != width || |
| 406 | stream->getHeight() != height) { |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 407 | HAL_LOGE( |
| 408 | "V4L2 only supports 1 stream configuration at a time " |
| 409 | "(stream 0 is format %d, width %u, height %u, " |
| 410 | "stream %d is format %d, width %u, height %u).", |
| 411 | format, |
| 412 | width, |
| 413 | height, |
| 414 | i, |
| 415 | stream->getFormat(), |
| 416 | stream->getWidth(), |
| 417 | stream->getHeight()); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | int V4L2Camera::setupStream(default_camera_hal::Stream* stream, |
| 426 | uint32_t* max_buffers) { |
| 427 | HAL_LOG_ENTER(); |
| 428 | |
| 429 | if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) { |
| 430 | HAL_LOGE("Rotation %d not supported", stream->getRotation()); |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | // Doesn't matter what was requested, we always use dataspace V0_JFIF. |
| 435 | // Note: according to camera3.h, this isn't allowed, but etalvala@google.com |
| 436 | // claims it's underdocumented; the implementation lets the HAL overwrite it. |
| 437 | stream->setDataSpace(HAL_DATASPACE_V0_JFIF); |
| 438 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 439 | std::lock_guard<std::mutex> guard(in_flight_lock_); |
| 440 | // The framework should be enforcing this, but doesn't hurt to be safe. |
| 441 | if (!in_flight_.empty()) { |
| 442 | HAL_LOGE("Can't set device format while frames are in flight."); |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | // Ensure the stream is off. |
Ari Hausman-Cohen | 71cb874 | 2016-09-22 11:12:00 -0700 | [diff] [blame] | 447 | int res = device_->StreamOff(); |
| 448 | if (res) { |
| 449 | HAL_LOGE("Device failed to turn off stream for reconfiguration."); |
| 450 | return -ENODEV; |
| 451 | } |
| 452 | |
| 453 | res = device_->SetFormat(*stream, max_buffers); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 454 | if (res) { |
| 455 | HAL_LOGE("Failed to set device to correct format for stream."); |
| 456 | return res; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 457 | } |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 458 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 459 | // Sanity check. |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 460 | if (*max_buffers < 1) { |
| 461 | HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.", |
| 462 | *max_buffers); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 463 | return -ENODEV; |
| 464 | } |
| 465 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 469 | bool V4L2Camera::isValidRequest( |
| 470 | const default_camera_hal::CaptureRequest& request) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 471 | HAL_LOG_ENTER(); |
| 472 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 473 | if (request.input_buffer != nullptr) { |
| 474 | HAL_LOGE("Input buffer reprocessing not implemented."); |
| 475 | return false; |
| 476 | } else if (request.output_buffers.size() > 1) { |
| 477 | HAL_LOGE("Only 1 output buffer allowed per request."); |
| 478 | return false; |
| 479 | } else if (!metadata_->IsValidRequest(request.settings)) { |
| 480 | HAL_LOGE("Invalid request settings."); |
| 481 | return false; |
| 482 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 483 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 484 | return true; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 487 | } // namespace v4l2_camera_hal |