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 | 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. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 39 | static std::vector<int32_t> getMetadataKeys(const camera_metadata_t* metadata) { |
Ari Hausman-Cohen | dde8017 | 2016-07-01 16:20:36 -0700 | [diff] [blame] | 40 | std::vector<int32_t> keys; |
| 41 | size_t num_entries = get_camera_metadata_entry_count(metadata); |
| 42 | for (size_t i = 0; i < num_entries; ++i) { |
| 43 | camera_metadata_ro_entry_t entry; |
| 44 | get_camera_metadata_ro_entry(metadata, i, &entry); |
| 45 | keys.push_back(entry.tag); |
| 46 | } |
| 47 | return keys; |
| 48 | } |
| 49 | |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 50 | V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) { |
| 51 | HAL_LOG_ENTER(); |
| 52 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 53 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path)); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 54 | if (!v4l2_wrapper) { |
| 55 | HAL_LOGE("Failed to initialize V4L2 wrapper."); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 56 | return nullptr; |
| 57 | } |
| 58 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 59 | std::unique_ptr<Metadata> metadata; |
| 60 | int res = GetV4L2Metadata(v4l2_wrapper, &metadata); |
| 61 | if (res) { |
| 62 | HAL_LOGE("Failed to initialize V4L2 metadata: %d", res); |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata)); |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 69 | V4L2Camera::V4L2Camera(int id, |
| 70 | std::shared_ptr<V4L2Wrapper> v4l2_wrapper, |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 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 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 105 | // TODO(b/29158098): Inform service of any flashes that are no longer |
| 106 | // available 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. |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 129 | res = SingleTagValue( |
| 130 | *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 131 | if (res) { |
| 132 | HAL_LOGE("Failed to get max num input streams from static metadata."); |
| 133 | return res; |
| 134 | } |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 135 | res = SingleTagValue( |
| 136 | *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 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, |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 234 | int count, |
| 235 | uint32_t mode) { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 236 | HAL_LOG_ENTER(); |
| 237 | |
| 238 | if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) { |
| 239 | HAL_LOGE("Unsupported stream configuration mode: %d", mode); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // This should be checked by the caller, but put here as a sanity check. |
| 244 | if (count < 1) { |
| 245 | HAL_LOGE("Must request at least 1 stream"); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // Count the number of streams of each type. |
| 250 | int32_t num_input = 0; |
| 251 | int32_t num_raw = 0; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 252 | int32_t num_stalling = 0; |
| 253 | int32_t num_non_stalling = 0; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 254 | for (int i = 0; i < count; ++i) { |
| 255 | default_camera_hal::Stream* stream = streams[i]; |
| 256 | |
| 257 | if (stream->isInputType()) { |
| 258 | ++num_input; |
| 259 | } |
| 260 | |
| 261 | if (stream->isOutputType()) { |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 262 | StreamFormat format(*stream); |
| 263 | switch (format.Category()) { |
| 264 | case kFormatCategoryRaw: |
| 265 | ++num_raw; |
| 266 | case kFormatCategoryStalling: |
| 267 | ++num_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 268 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 269 | case kFormatCategoryNonStalling: |
| 270 | ++num_non_stalling; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 271 | break; |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 272 | case kFormatCategoryUnknown: // Fall through. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 273 | default: |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 274 | HAL_LOGE( |
| 275 | "Unsupported format for stream %d: %d", i, stream->getFormat()); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 281 | 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] | 282 | num_non_stalling > max_output_streams_[1] || |
| 283 | num_stalling > max_output_streams_[2]) { |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 284 | HAL_LOGE( |
| 285 | "Invalid stream configuration: %d input, %d RAW, %d non-stalling, " |
| 286 | "%d stalling (max supported: %d input, %d RAW, %d non-stalling, " |
| 287 | "%d stalling)", |
| 288 | max_input_streams_, |
| 289 | max_output_streams_[0], |
| 290 | max_output_streams_[1], |
| 291 | max_output_streams_[2], |
| 292 | num_input, |
| 293 | num_raw, |
| 294 | num_non_stalling, |
| 295 | num_stalling); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
| 299 | // TODO(b/29939583): The above logic should be all that's necessary, |
| 300 | // but V4L2 doesn't actually support more than 1 stream at a time. So for now, |
| 301 | // if not all streams are the same format and size, error. Note that this |
| 302 | // means the HAL is not spec-compliant; the requested streams are technically |
| 303 | // valid and it is not technically allowed to error once it has reached this |
| 304 | // point. |
| 305 | int format = streams[0]->getFormat(); |
| 306 | uint32_t width = streams[0]->getWidth(); |
| 307 | uint32_t height = streams[0]->getHeight(); |
| 308 | for (int i = 1; i < count; ++i) { |
| 309 | const default_camera_hal::Stream* stream = streams[i]; |
| 310 | if (stream->getFormat() != format || stream->getWidth() != width || |
| 311 | stream->getHeight() != height) { |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 312 | HAL_LOGE( |
| 313 | "V4L2 only supports 1 stream configuration at a time " |
| 314 | "(stream 0 is format %d, width %u, height %u, " |
| 315 | "stream %d is format %d, width %u, height %u).", |
| 316 | format, |
| 317 | width, |
| 318 | height, |
| 319 | i, |
| 320 | stream->getFormat(), |
| 321 | stream->getWidth(), |
| 322 | stream->getHeight()); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | int V4L2Camera::setupStream(default_camera_hal::Stream* stream, |
| 331 | uint32_t* max_buffers) { |
| 332 | HAL_LOG_ENTER(); |
| 333 | |
| 334 | if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) { |
| 335 | HAL_LOGE("Rotation %d not supported", stream->getRotation()); |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | // Doesn't matter what was requested, we always use dataspace V0_JFIF. |
| 340 | // Note: according to camera3.h, this isn't allowed, but etalvala@google.com |
| 341 | // claims it's underdocumented; the implementation lets the HAL overwrite it. |
| 342 | stream->setDataSpace(HAL_DATASPACE_V0_JFIF); |
| 343 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 344 | int res = device_->SetFormat(*stream, max_buffers); |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 345 | if (res) { |
| 346 | HAL_LOGE("Failed to set device to correct format for stream."); |
| 347 | return res; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 348 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 349 | // Sanity check. |
Ari Hausman-Cohen | 660f8b8 | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 350 | if (*max_buffers < 1) { |
| 351 | HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.", |
| 352 | *max_buffers); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 353 | return -ENODEV; |
| 354 | } |
| 355 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Ari Hausman-Cohen | 9430ad9 | 2016-08-24 14:00:32 -0700 | [diff] [blame] | 359 | bool V4L2Camera::isValidCaptureSettings( |
| 360 | const android::CameraMetadata& settings) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 361 | HAL_LOG_ENTER(); |
| 362 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 363 | return metadata_->IsValidRequest(settings); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 366 | int V4L2Camera::setSettings(const android::CameraMetadata& new_settings) { |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 367 | HAL_LOG_ENTER(); |
| 368 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 369 | return metadata_->SetRequestSettings(new_settings); |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 372 | } // namespace v4l2_camera_hal |