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/types.h> |
| 22 | #include <sys/stat.h> |
| 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 | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 30 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 31 | #include "stream_format.h" |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 32 | #include "v4l2_metadata_factory.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 33 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 34 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 35 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 36 | namespace v4l2_camera_hal { |
| 37 | |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 38 | // Helper function for managing metadata. |
| 39 | static std::vector<int32_t> getMetadataKeys( |
| 40 | const camera_metadata_t* metadata) { |
| 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 | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 70 | V4L2Camera::V4L2Camera(int id, std::shared_ptr<V4L2Wrapper> v4l2_wrapper, |
| 71 | std::unique_ptr<Metadata> metadata) |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 72 | : default_camera_hal::Camera(id), |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 73 | device_(std::move(v4l2_wrapper)), |
| 74 | metadata_(std::move(metadata)), |
| 75 | max_input_streams_(0), |
| 76 | max_output_streams_({{0, 0, 0}}) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 77 | HAL_LOG_ENTER(); |
| 78 | } |
| 79 | |
| 80 | V4L2Camera::~V4L2Camera() { |
| 81 | HAL_LOG_ENTER(); |
| 82 | } |
| 83 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 84 | int V4L2Camera::connect() { |
| 85 | HAL_LOG_ENTER(); |
| 86 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 87 | if (connection_) { |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 88 | HAL_LOGE("Already connected. Please disconnect and try again."); |
| 89 | return -EIO; |
| 90 | } |
| 91 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 92 | connection_.reset(new V4L2Wrapper::Connection(device_)); |
| 93 | if (connection_->status()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 94 | HAL_LOGE("Failed to connect to device."); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 95 | return connection_->status(); |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 98 | // TODO(b/29185945): confirm this is a supported device. |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 99 | // 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] | 100 | // not be the same one that was there when the HAL was loaded. |
| 101 | // (Alternatively, better hotplugging support may make this unecessary |
| 102 | // by disabling cameras that get disconnected and checking newly connected |
| 103 | // cameras, so connect() is never called on an unsupported camera) |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 104 | |
| 105 | // TODO(b/29158098): Inform service of any flashes that are no longer available |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 106 | // because this camera is in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | void V4L2Camera::disconnect() { |
| 111 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 112 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 113 | connection_.reset(); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 114 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 115 | // 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] | 116 | // because this camera is no longer in use. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 119 | int V4L2Camera::initStaticInfo(android::CameraMetadata* out) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 120 | HAL_LOG_ENTER(); |
| 121 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 122 | int res = metadata_->FillStaticMetadata(out); |
| 123 | if (res) { |
| 124 | HAL_LOGE("Failed to get static metadata."); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 125 | return res; |
| 126 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 127 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 128 | // Extract max streams for use in verifying stream configs. |
| 129 | res = SingleTagValue(*out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, |
| 130 | &max_input_streams_); |
| 131 | if (res) { |
| 132 | HAL_LOGE("Failed to get max num input streams from static metadata."); |
| 133 | return res; |
| 134 | } |
| 135 | res = SingleTagValue(*out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, |
| 136 | &max_output_streams_); |
| 137 | if (res) { |
| 138 | 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] | 139 | return res; |
| 140 | } |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 141 | |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 142 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 145 | int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) { |
| 146 | HAL_LOG_ENTER(); |
| 147 | |
| 148 | return metadata_->GetRequestTemplate(type, out); |
| 149 | } |
| 150 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 151 | void V4L2Camera::initDeviceInfo(camera_info_t* info) { |
| 152 | HAL_LOG_ENTER(); |
| 153 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 154 | // TODO(b/31044975): move this into device interface. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 155 | // For now, just constants. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 156 | info->resource_cost = 100; |
| 157 | info->conflicting_devices = nullptr; |
| 158 | info->conflicting_devices_length = 0; |
| 159 | } |
| 160 | |
| 161 | int V4L2Camera::initDevice() { |
| 162 | HAL_LOG_ENTER(); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 163 | // Nothing to do. |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 167 | int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) { |
| 168 | HAL_LOG_ENTER(); |
| 169 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 170 | int res = device_->EnqueueBuffer(camera_buffer); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 171 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 172 | HAL_LOGE("Device failed to enqueue buffer."); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 173 | return res; |
| 174 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 175 | |
| 176 | // Turn on the stream. |
| 177 | // TODO(b/29334616): Lock around stream on/off access, only start stream |
| 178 | // if not already on. (For now, since it's synchronous, it will always be |
| 179 | // turned off before another call to this function). |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 180 | res = device_->StreamOn(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 181 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 182 | HAL_LOGE("Device failed to turn on stream."); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 183 | return res; |
| 184 | } |
| 185 | |
| 186 | // TODO(b/29334616): Enqueueing and dequeueing should be separate worker |
| 187 | // threads, not in the same function. |
| 188 | |
| 189 | // Dequeue the buffer. |
| 190 | v4l2_buffer result_buffer; |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 191 | res = device_->DequeueBuffer(&result_buffer); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 192 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 193 | HAL_LOGE("Device failed to dequeue buffer."); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 194 | return res; |
| 195 | } |
| 196 | |
| 197 | // All done, cleanup. |
| 198 | // TODO(b/29334616): Lock around stream on/off access, only stop stream if |
| 199 | // buffer queue is empty (synchronously, there's only ever 1 buffer in the |
| 200 | // queue at a time, so this is safe). |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 201 | res = device_->StreamOff(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 202 | if (res) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 203 | HAL_LOGE("Device failed to turn off stream."); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 204 | return res; |
| 205 | } |
| 206 | |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 210 | int V4L2Camera::getResultSettings(android::CameraMetadata* metadata, |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 211 | uint64_t* timestamp) { |
| 212 | HAL_LOG_ENTER(); |
| 213 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 214 | // Get the results. |
| 215 | int res = metadata_->FillResultMetadata(metadata); |
| 216 | if (res) { |
| 217 | HAL_LOGE("Failed to fill result metadata."); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 218 | return res; |
| 219 | } |
| 220 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 221 | // Extract the timestamp. |
| 222 | int64_t frame_time = 0; |
| 223 | res = SingleTagValue(*metadata, ANDROID_SENSOR_TIMESTAMP, &frame_time); |
| 224 | if (res) { |
| 225 | HAL_LOGE("Failed to extract timestamp from result metadata"); |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 226 | return res; |
| 227 | } |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 228 | *timestamp = static_cast<uint64_t>(frame_time); |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 229 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 233 | bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams, |
| 234 | int count, uint32_t mode) { |
| 235 | HAL_LOG_ENTER(); |
| 236 | |
| 237 | if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) { |
| 238 | HAL_LOGE("Unsupported stream configuration mode: %d", mode); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // This should be checked by the caller, but put here as a sanity check. |
| 243 | if (count < 1) { |
| 244 | HAL_LOGE("Must request at least 1 stream"); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // Count the number of streams of each type. |
| 249 | int32_t num_input = 0; |
| 250 | int32_t num_raw = 0; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 251 | int32_t num_stalling = 0; |
| 252 | int32_t num_non_stalling = 0; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 253 | for (int i = 0; i < count; ++i) { |
| 254 | default_camera_hal::Stream* stream = streams[i]; |
| 255 | |
| 256 | if (stream->isInputType()) { |
| 257 | ++num_input; |
| 258 | } |
| 259 | |
| 260 | if (stream->isOutputType()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 261 | StreamFormat format(*stream); |
| 262 | switch (format.Category()) { |
| 263 | case kFormatCategoryRaw: |
| 264 | ++num_raw; |
| 265 | case kFormatCategoryStalling: |
| 266 | ++num_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 267 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 268 | case kFormatCategoryNonStalling: |
| 269 | ++num_non_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 270 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 271 | case kFormatCategoryUnknown: // Fall through. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 272 | default: |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 273 | HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat()); |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 279 | if (num_input > max_input_streams_ || |
| 280 | num_raw > max_output_streams_[0] || |
| 281 | num_non_stalling > max_output_streams_[1] || |
| 282 | num_stalling > max_output_streams_[2]) { |
| 283 | HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d non-stalling, " |
| 284 | "%d stalling (max supported: %d input, %d RAW, %d non-stalling, " |
| 285 | "%d stalling)", max_input_streams_, max_output_streams_[0], |
| 286 | max_output_streams_[1], max_output_streams_[2], num_input, |
| 287 | num_raw, num_non_stalling, num_stalling); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | // TODO(b/29939583): The above logic should be all that's necessary, |
| 292 | // but V4L2 doesn't actually support more than 1 stream at a time. So for now, |
| 293 | // if not all streams are the same format and size, error. Note that this |
| 294 | // means the HAL is not spec-compliant; the requested streams are technically |
| 295 | // valid and it is not technically allowed to error once it has reached this |
| 296 | // point. |
| 297 | int format = streams[0]->getFormat(); |
| 298 | uint32_t width = streams[0]->getWidth(); |
| 299 | uint32_t height = streams[0]->getHeight(); |
| 300 | for (int i = 1; i < count; ++i) { |
| 301 | const default_camera_hal::Stream* stream = streams[i]; |
| 302 | if (stream->getFormat() != format || stream->getWidth() != width || |
| 303 | stream->getHeight() != height) { |
| 304 | HAL_LOGE("V4L2 only supports 1 stream configuration at a time " |
| 305 | "(stream 0 is format %d, width %u, height %u, " |
| 306 | "stream %d is format %d, width %u, height %u).", |
| 307 | format, width, height, i, stream->getFormat(), |
| 308 | stream->getWidth(), stream->getHeight()); |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | int V4L2Camera::setupStream(default_camera_hal::Stream* stream, |
| 317 | uint32_t* max_buffers) { |
| 318 | HAL_LOG_ENTER(); |
| 319 | |
| 320 | if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) { |
| 321 | HAL_LOGE("Rotation %d not supported", stream->getRotation()); |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | |
| 325 | // Doesn't matter what was requested, we always use dataspace V0_JFIF. |
| 326 | // Note: according to camera3.h, this isn't allowed, but etalvala@google.com |
| 327 | // claims it's underdocumented; the implementation lets the HAL overwrite it. |
| 328 | stream->setDataSpace(HAL_DATASPACE_V0_JFIF); |
| 329 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 330 | int res = device_->SetFormat(*stream, max_buffers); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 331 | if (res) { |
| 332 | HAL_LOGE("Failed to set device to correct format for stream."); |
| 333 | return res; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 334 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 335 | // Sanity check. |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 336 | if (*max_buffers < 1) { |
| 337 | HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.", |
| 338 | *max_buffers); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 339 | return -ENODEV; |
| 340 | } |
| 341 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 345 | bool V4L2Camera::isValidCaptureSettings(const android::CameraMetadata& settings) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 346 | HAL_LOG_ENTER(); |
| 347 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 348 | return metadata_->IsValidRequest(settings); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 351 | int V4L2Camera::setSettings(const android::CameraMetadata& new_settings) { |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 352 | HAL_LOG_ENTER(); |
| 353 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame^] | 354 | return metadata_->SetRequestSettings(new_settings); |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 357 | } // namespace v4l2_camera_hal |