andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "device_info_linux.h" |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <unistd.h> |
| 15 | #include <sys/ioctl.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | //v4l includes |
| 22 | #include <linux/videodev2.h> |
| 23 | |
| 24 | #include "ref_count.h" |
| 25 | #include "trace.h" |
| 26 | |
| 27 | |
| 28 | namespace webrtc |
| 29 | { |
| 30 | namespace videocapturemodule |
| 31 | { |
| 32 | VideoCaptureModule::DeviceInfo* |
| 33 | VideoCaptureImpl::CreateDeviceInfo(const WebRtc_Word32 id) |
| 34 | { |
| 35 | videocapturemodule::DeviceInfoLinux *deviceInfo = |
| 36 | new videocapturemodule::DeviceInfoLinux(id); |
| 37 | if (!deviceInfo) |
| 38 | { |
| 39 | deviceInfo = NULL; |
| 40 | } |
| 41 | |
| 42 | return deviceInfo; |
| 43 | } |
| 44 | |
| 45 | DeviceInfoLinux::DeviceInfoLinux(const WebRtc_Word32 id) |
| 46 | : DeviceInfoImpl(id) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | WebRtc_Word32 DeviceInfoLinux::Init() |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | DeviceInfoLinux::~DeviceInfoLinux() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | WebRtc_UWord32 DeviceInfoLinux::NumberOfDevices() |
| 60 | { |
| 61 | WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideoCapture, _id, "%s", __FUNCTION__); |
| 62 | |
| 63 | WebRtc_UWord32 count = 0; |
| 64 | char device[20]; |
| 65 | int fd = -1; |
| 66 | |
| 67 | /* detect /dev/video [0-63]VideoCaptureModule entries */ |
| 68 | for (int n = 0; n < 64; n++) |
| 69 | { |
| 70 | sprintf(device, "/dev/video%d", n); |
| 71 | if ((fd = open(device, O_RDONLY)) != -1) |
| 72 | { |
| 73 | close(fd); |
| 74 | count++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return count; |
| 79 | } |
| 80 | |
| 81 | WebRtc_Word32 DeviceInfoLinux::GetDeviceName( |
| 82 | WebRtc_UWord32 deviceNumber, |
| 83 | char* deviceNameUTF8, |
| 84 | WebRtc_UWord32 deviceNameLength, |
| 85 | char* deviceUniqueIdUTF8, |
| 86 | WebRtc_UWord32 deviceUniqueIdUTF8Length, |
| 87 | char* /*productUniqueIdUTF8*/, |
| 88 | WebRtc_UWord32 /*productUniqueIdUTF8Length*/) |
| 89 | { |
| 90 | WEBRTC_TRACE(webrtc::kTraceApiCall, webrtc::kTraceVideoCapture, _id, "%s", __FUNCTION__); |
| 91 | |
| 92 | // Travel through /dev/video [0-63] |
| 93 | WebRtc_UWord32 count = 0; |
| 94 | char device[20]; |
| 95 | int fd = -1; |
| 96 | bool found = false; |
| 97 | for (int n = 0; n < 64; n++) |
| 98 | { |
| 99 | sprintf(device, "/dev/video%d", n); |
| 100 | if ((fd = open(device, O_RDONLY)) != -1) |
| 101 | { |
| 102 | if (count == deviceNumber) { |
| 103 | // Found the device |
| 104 | found = true; |
| 105 | break; |
| 106 | } else { |
| 107 | close(fd); |
| 108 | count++; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (!found) |
| 114 | return -1; |
| 115 | |
| 116 | // query device capabilities |
| 117 | struct v4l2_capability cap; |
| 118 | if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) |
| 119 | { |
| 120 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 121 | "error in querying the device capability for device %s. errno = %d", |
| 122 | device, errno); |
| 123 | close(fd); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | close(fd); |
| 128 | |
| 129 | char cameraName[64]; |
| 130 | memset(deviceNameUTF8, 0, deviceNameLength); |
| 131 | memcpy(cameraName, cap.card, sizeof(cap.card)); |
| 132 | |
| 133 | if (deviceNameLength >= strlen(cameraName)) |
| 134 | { |
| 135 | memcpy(deviceNameUTF8, cameraName, strlen(cameraName)); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, "buffer passed is too small"); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | if (cap.bus_info[0] != 0) // may not available in all drivers |
| 144 | { |
| 145 | // copy device id |
| 146 | if (deviceUniqueIdUTF8Length >= strlen((const char*) cap.bus_info)) |
| 147 | { |
| 148 | memset(deviceUniqueIdUTF8, 0, deviceUniqueIdUTF8Length); |
| 149 | memcpy(deviceUniqueIdUTF8, cap.bus_info, |
| 150 | strlen((const char*) cap.bus_info)); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 155 | "buffer passed is too small"); |
| 156 | return -1; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | WebRtc_Word32 DeviceInfoLinux::CreateCapabilityMap( |
| 164 | const char* deviceUniqueIdUTF8) |
| 165 | { |
| 166 | int fd; |
| 167 | char device[32]; |
| 168 | bool found = false; |
| 169 | |
| 170 | const WebRtc_Word32 deviceUniqueIdUTF8Length = |
| 171 | (WebRtc_Word32) strlen((char*) deviceUniqueIdUTF8); |
| 172 | if (deviceUniqueIdUTF8Length > kVideoCaptureUniqueNameLength) |
| 173 | { |
| 174 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, "Device name too long"); |
| 175 | return -1; |
| 176 | } |
| 177 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id, |
| 178 | "CreateCapabilityMap called for device %s", deviceUniqueIdUTF8); |
| 179 | |
| 180 | /* detect /dev/video [0-63] entries */ |
| 181 | for (int n = 0; n < 64; ++n) |
| 182 | { |
| 183 | sprintf(device, "/dev/video%d", n); |
| 184 | fd = open(device, O_RDONLY); |
| 185 | if (fd == -1) |
| 186 | continue; |
| 187 | |
| 188 | // query device capabilities |
| 189 | struct v4l2_capability cap; |
| 190 | if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) |
| 191 | { |
| 192 | if (cap.bus_info[0] != 0) |
| 193 | { |
| 194 | if (strncmp((const char*) cap.bus_info, |
| 195 | (const char*) deviceUniqueIdUTF8, |
| 196 | strlen((const char*) deviceUniqueIdUTF8)) == 0) //match with device id |
| 197 | { |
| 198 | found = true; |
| 199 | break; // fd matches with device unique id supplied |
| 200 | } |
| 201 | } |
| 202 | else //match for device name |
| 203 | { |
| 204 | if (IsDeviceNameMatches((const char*) cap.card, |
| 205 | (const char*) deviceUniqueIdUTF8)) |
| 206 | { |
| 207 | found = true; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | close(fd); // close since this is not the matching device |
| 213 | } |
| 214 | |
| 215 | if (!found) |
| 216 | { |
| 217 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, "no matching device found"); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | // now fd will point to the matching device |
| 222 | // reset old capability map |
| 223 | MapItem* item = NULL; |
| 224 | while ((item = _captureCapabilities.Last())) |
| 225 | { |
| 226 | delete static_cast<VideoCaptureCapability*> (item->GetItem()); |
| 227 | _captureCapabilities.Erase(item); |
| 228 | } |
| 229 | |
| 230 | int size = FillCapabilityMap(fd); |
| 231 | close(fd); |
| 232 | |
| 233 | // Store the new used device name |
| 234 | _lastUsedDeviceNameLength = deviceUniqueIdUTF8Length; |
| 235 | _lastUsedDeviceName = (char*) realloc(_lastUsedDeviceName, |
| 236 | _lastUsedDeviceNameLength + 1); |
| 237 | memcpy(_lastUsedDeviceName, deviceUniqueIdUTF8, _lastUsedDeviceNameLength + 1); |
| 238 | |
| 239 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id, "CreateCapabilityMap %d", |
| 240 | _captureCapabilities.Size()); |
| 241 | |
| 242 | return size; |
| 243 | } |
| 244 | |
| 245 | bool DeviceInfoLinux::IsDeviceNameMatches(const char* name, |
| 246 | const char* deviceUniqueIdUTF8) |
| 247 | { |
| 248 | if (strncmp(deviceUniqueIdUTF8, name, strlen(name)) == 0) |
| 249 | return true; |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | WebRtc_Word32 DeviceInfoLinux::FillCapabilityMap(int fd) |
| 254 | { |
| 255 | |
| 256 | // set image format |
| 257 | struct v4l2_format video_fmt; |
| 258 | memset(&video_fmt, 0, sizeof(struct v4l2_format)); |
| 259 | |
| 260 | video_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 261 | video_fmt.fmt.pix.sizeimage = 0; |
| 262 | |
| 263 | int totalFmts = 3; |
| 264 | unsigned int videoFormats[] = { |
| 265 | V4L2_PIX_FMT_MJPEG, |
| 266 | V4L2_PIX_FMT_YUV420, |
| 267 | V4L2_PIX_FMT_YUYV }; |
| 268 | |
| 269 | int sizes = 13; |
| 270 | unsigned int size[][2] = { { 128, 96 }, { 160, 120 }, { 176, 144 }, |
| 271 | { 320, 240 }, { 352, 288 }, { 640, 480 }, |
| 272 | { 704, 576 }, { 800, 600 }, { 960, 720 }, |
| 273 | { 1280, 720 }, { 1024, 768 }, { 1440, 1080 }, |
| 274 | { 1920, 1080 } }; |
| 275 | |
| 276 | int index = 0; |
| 277 | for (int fmts = 0; fmts < totalFmts; fmts++) |
| 278 | { |
| 279 | for (int i = 0; i < sizes; i++) |
| 280 | { |
| 281 | video_fmt.fmt.pix.pixelformat = videoFormats[fmts]; |
| 282 | video_fmt.fmt.pix.width = size[i][0]; |
| 283 | video_fmt.fmt.pix.height = size[i][1]; |
| 284 | |
| 285 | if (ioctl(fd, VIDIOC_TRY_FMT, &video_fmt) >= 0) |
| 286 | { |
| 287 | if ((video_fmt.fmt.pix.width == size[i][0]) |
| 288 | && (video_fmt.fmt.pix.height == size[i][1])) |
| 289 | { |
| 290 | VideoCaptureCapability *cap = new VideoCaptureCapability(); |
| 291 | cap->width = video_fmt.fmt.pix.width; |
| 292 | cap->height = video_fmt.fmt.pix.height; |
| 293 | cap->expectedCaptureDelay = 120; |
| 294 | if (videoFormats[fmts] == V4L2_PIX_FMT_YUYV) |
| 295 | { |
| 296 | cap->rawType = kVideoYUY2; |
| 297 | } |
| 298 | else if (videoFormats[fmts] == V4L2_PIX_FMT_MJPEG) |
| 299 | { |
| 300 | cap->rawType = kVideoMJPEG; |
| 301 | } |
| 302 | |
| 303 | // get fps of current camera mode |
| 304 | // V4l2 does not have a stable method of knowing so we just guess. |
| 305 | if(cap->width >= 800 && cap->rawType != kVideoMJPEG) |
| 306 | { |
| 307 | cap->maxFPS = 15; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | cap->maxFPS = 30; |
| 312 | } |
| 313 | |
| 314 | _captureCapabilities.Insert(index, cap); |
| 315 | index++; |
| 316 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id, |
| 317 | "Camera capability, width:%d height:%d type:%d fps:%d", |
| 318 | cap->width, cap->height, cap->rawType, cap->maxFPS); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id, "CreateCapabilityMap %d", |
| 325 | _captureCapabilities.Size()); |
| 326 | return _captureCapabilities.Size(); |
| 327 | } |
| 328 | |
| 329 | } // namespace videocapturemodule |
| 330 | } // namespace webrtc |