Debian Codegraph @

device.go

raw
1 // Package v4l, a facade to the Video4Linux video capture interface
2 // Copyright (C) 2016 Zoltán Korándi <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 //go:build linux
18 // +build linux
19
20 // Package v4l is a facade to the Video4Linux video capture interface.
21 package v4l
22
23 import "syscall"
24
25 // Control IDs. Devices may have other controls than these, including custom
26 // (driver specific) ones.
27 const (
28 // Integer controls
29 CtrlBrightness = 0x00980900
30 CtrlContrast = 0x00980901
31 CtrlSaturation = 0x00980902
32 CtrlHue = 0x00980903
33 CtrlGamma = 0x00980910
34 CtrlExposure = 0x00980911
35 CtrlGain = 0x00980913
36 CtrlWhiteBalance = 0x0098091a
37 CtrlSharpness = 0x0098091b
38 CtrlBacklightCompensation = 0x0098091c
39
40 // Boolean controls
41 CtrlHFlip = 0x00980914
42 CtrlVFlip = 0x00980915
43 CtrlAutoWhiteBalance = 0x0098090c
44 CtrlAutoGain = 0x00980912
45 CtrlAutoHue = 0x00980919
46 CtrlAutoBrightness = 0x00980920
47
48 // Enums
49 CtrlPowerLineFreq = 0x00980918
50 PowerLineFreqDisabled = 0
51 PowerLineFreq50Hz = 1
52 PowerLineFreq60Hz = 2
53 PowerLineFreqAuto = 3
54
55 // Buttons
56 CtrlDoWhiteBalance = 0x0098090d
57 )
58
59 // A Device represents a V4L capture device.
60 type Device struct {
61 *device
62 }
63
64 // device is the real representation of Device. The extra level of indirection
65 // is there to prevent clients from tampering with the file descriptor and the
66 // buffers.
67 type device struct {
68 path string
69 fd int
70 buffers [][]byte
71 bufIndex uint32
72 nCaptures uint64
73 }
74
75 // noBuffer is the value assinged to device.bufIndex when none of the buffers
76 // contain valid image data.
77 const noBuffer = ^uint32(0)
78
79 // A DeviceInfo provides information about a capture device.
80 type DeviceInfo struct {
81 // Path is the device path. (e.g. /dev/video0)
82 Path string
83
84 // DeviceName is the name of the device. (e.g. "Yoyodyne TV/FM")
85 DeviceName string
86
87 // BusInfo is the location of the device in the system.
88 // (e.g. "PCI:0000:05:06.0")
89 BusInfo string
90
91 // DriverName is the name of the driver. (e.g. "bttv")
92 DriverName string
93
94 // DriverVersion contains the three components of the driver's version
95 // number. (e.g. [3]int{1, 2, 3} for version 1.2.3)
96 DriverVersion [3]int
97
98 // Camera tells if the device is a camera. If false, then this is some other
99 // kind of capture device, e.g. an analog TV tuner.
100 Camera bool
101 }
102
103 // A DeviceConfig encapsulates the configuration of a capture device.
104 type DeviceConfig struct {
105 // Format is the four-character code (FourCC) of the pixel format, with the
106 // first character at the lowest byte.
107 Format uint32
108
109 // Width and Height specify the image dimensions.
110 Width int
111 Height int
112
113 // FPS specifies the frame rate.
114 FPS Frac
115 }
116
117 // A BufferInfo provides information about how image data is laid out in a
118 // buffer.
119 type BufferInfo struct {
120 // BufferSize is the number of bytes required to hold an image. For variable
121 // length compressed formats, it's the maximum size an image may take up.
122 BufferSize int
123
124 // ImageStride is the distance in bytes between the leftmost pixels of
125 // adjacent lines.
126 ImageStride int
127 }
128
129 // A ControlInfo provides information about a control.
130 type ControlInfo struct {
131 // CID is the identifier of the control. (e.g. 0x00980900)
132 CID uint32
133
134 // Name is the name of the control. (e.g. "Brightness")
135 Name string
136
137 // Type tells what kind of control this is. It's one of "int", "bool",
138 // "enum", "int-enum", or "button".
139 // - The valid values of an integer control are determined by Min, Max,
140 // and Step.
141 // - A boolean control can only have the values 0 and 1, where 0 means
142 // "disabled" and 1 means "enabled".
143 // - Enums can only take values from a predefined set. The values are
144 // identifiers which select one of a few named options. (see Options)
145 // - An "int-enum" is similar to an enum, except that, the options are
146 // 64-bit unsigned integers, rather than strings.
147 // - Buttons perform some action when pushed, and they don't have a value.
148 // Reading the value of a button fails, while setting it to any value is
149 // interpreted as a push.
150 Type string
151
152 // Min and Max specify the range of values the control can take, and Step is
153 // the smallest change actually affecting the hardware. They are only
154 // meaningful for integer type controls.
155 Min int32
156 Max int32
157 Step int32
158
159 // Default is the default value of the control.
160 Default int32
161
162 // Options is the list of valid values of an enum or int-enum type control.
163 // For other types it's nil.
164 Options []struct {
165 // Value is the identifier of this option. (e.g. 0, 1, 2, etc)
166 Value int32
167
168 // Name is the label of this option. Only valid for enum type controls.
169 // (e.g. "50 Hz", "60 Hz", "Auto")
170 Name string
171
172 // Int64 is the integer value of this option. Only valid for int-enum
173 // type controls. (e.g. 0, 333, 667)
174 Int64 int64
175 }
176 }
177
178 // errBadControl is returned by Device.controlInfo when the control is disabled
179 // or of unsupported type.
180 const errBadControl = Error("control disabled or of unsupported type")
181
182 // Open opens the capture device named by path. If the file is not a capture
183 // device, it fails with ErrWrongDevice.
184 func Open(path string) (*Device, error) {
185 // Open the file.
186 fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CLOEXEC, 0)
187 if err != nil {
188 return nil, err
189 }
190
191 // Check if it's a V4L device.
192 var stat syscall.Stat_t
193 if err := syscall.Fstat(fd, &stat); err != nil {
194 syscall.Close(fd)
195 return nil, err
196 }
197 if stat.Mode&syscall.S_IFCHR == 0 || stat.Rdev>>8 != 81 {
198 syscall.Close(fd)
199 return nil, ErrWrongDevice
200 }
201
202 // Check if it's a capture device.
203 var c v4l_capability
204 if err := ioctl_querycap(fd, &c); err != nil {
205 syscall.Close(fd)
206 return nil, err
207 }
208 caps := c.capabilities
209 if caps&v4l_capDeviceCaps != 0 {
210 caps = c.deviceCaps
211 }
212 if caps&v4l_capVideoCapture == 0 {
213 syscall.Close(fd)
214 return nil, ErrWrongDevice
215 }
216
217 d := device{path: path, fd: fd, bufIndex: noBuffer}
218 return &Device{&d}, nil
219 }
220
221 // Close closes the device, freeing all native resources associated with it. It
222 // stops any capture session in progress, and it may also render the contents of
223 // previously captured buffers unavailable.
224 func (d *Device) Close() {
225 d.TurnOff()
226 syscall.Close(d.fd)
227 d.fd = -1
228 }
229
230 // DeviceInfo returns information about the device.
231 func (d *Device) DeviceInfo() (DeviceInfo, error) {
232 // Query capabilities.
233 var c v4l_capability
234 if err := ioctl_querycap(d.fd, &c); err != nil {
235 return DeviceInfo{}, err
236 }
237
238 // Check if the device is a camera. Cameras do not enumerate any video
239 // standards.
240 s := v4l_standard{index: 0}
241 var cam bool
242 switch err := ioctl_enumstd(d.fd, &s); err {
243 case nil:
244 cam = false
245 case syscall.ENOTTY, syscall.EINVAL:
246 cam = true
247 default:
248 return DeviceInfo{}, err
249 }
250
251 info := DeviceInfo{
252 Path: d.path,
253 DeviceName: c.card,
254 BusInfo: c.busInfo,
255 DriverName: c.driver,
256 DriverVersion: [3]int{
257 int(c.version>>16) & 0xff,
258 int(c.version>>8) & 0xff,
259 int(c.version) & 0xff,
260 },
261 Camera: cam,
262 }
263 return info, nil
264 }
265
266 // TurnOn initiates a capture session with the device. It may fail with
267 // ErrUnsupported. While the device is turned on, its configuration cannot be
268 // changed.
269 func (d *Device) TurnOn() error {
270 // Switch to progressive format and reset the colorspace to device default.
271 f := v4l_format_pix{typ: v4l_bufTypeVideoCapture}
272 if err := ioctl_gFmt_pix(d.fd, &f); err != nil {
273 return err
274 }
275 f.fmt.field = v4l_fieldNone
276 f.fmt.colorspace = v4l_colorspaceDefault
277 f.fmt.priv = 0
278 if err := ioctl_sFmt_pix(d.fd, &f); err != nil {
279 return err
280 }
281
282 // Reset cropping.
283 cc := v4l_cropcap{typ: v4l_bufTypeVideoCapture}
284 switch err := ioctl_cropcap(d.fd, &cc); err {
285 case nil:
286 c := v4l_crop{
287 typ: v4l_bufTypeVideoCapture,
288 c: cc.defrect,
289 }
290 switch err := ioctl_sCrop(d.fd, &c); err {
291 case nil:
292 // Success.
293 case syscall.ENOTTY, syscall.EINVAL:
294 // VIDIOC_S_CROP unsupported.
295 default:
296 return err
297 }
298 case syscall.ENOTTY:
299 // No support for cropping. That's okay.
300 default:
301 return err
302 }
303
304 // Allocate buffers.
305 if err := d.allocBuffers(4); err != nil {
306 return err
307 }
308
309 // Start streaming I/O.
310 if err := ioctl_streamon(d.fd, v4l_bufTypeVideoCapture); err != nil {
311 d.freeBuffers()
312 return err
313 }
314 return nil
315 }
316
317 // TurnOff ends the capture session in progress. It does not close the device,
318 // so it can be reused for another session.
319 func (d *Device) TurnOff() {
320 ioctl_streamoff(d.fd, v4l_bufTypeVideoCapture)
321 d.freeBuffers()
322 }
323
324 // allocBuffers allocates n buffers in device memory, and mmaps and queues them.
325 func (d *Device) allocBuffers(n int) error {
326 // Request buffers.
327 rb := v4l_requestbuffers{
328 count: uint32(n),
329 typ: v4l_bufTypeVideoCapture,
330 memory: v4l_memoryMmap,
331 }
332 if err := ioctl_reqbufs(d.fd, &rb); err != nil {
333 if err == syscall.EINVAL {
334 // Memory-mapped I/O method unsupported.
335 err = ErrUnsupported
336 }
337 return err
338 }
339 if rb.count == 0 {
340 return Error("out of device memory")
341 }
342
343 // Map and enqueue the buffers.
344 d.buffers = make([][]byte, 0, rb.count)
345 for i := 0; i < cap(d.buffers); i++ {
346 b := v4l_buffer{
347 index: uint32(i),
348 typ: v4l_bufTypeVideoCapture,
349 memory: v4l_memoryMmap,
350 }
351 if err := ioctl_querybuf(d.fd, &b); err != nil {
352 d.freeBuffers()
353 return err
354 }
355 buf, err := syscall.Mmap(d.fd, int64(b.offset), int(b.length),
356 syscall.PROT_READ, syscall.MAP_SHARED)
357 if err != nil {
358 d.freeBuffers()
359 return err
360 }
361 d.buffers = append(d.buffers, buf)
362 if err := ioctl_qbuf(d.fd, &b); err != nil {
363 d.freeBuffers()
364 return err
365 }
366 }
367
368 return nil
369 }
370
371 // freeBuffers munmaps and frees any buffers allocated in device memory, and
372 // removes all pointers to them.
373 func (d *Device) freeBuffers() {
374 d.bufIndex = noBuffer
375 for i := range d.buffers {
376 syscall.Munmap(d.buffers[i])
377 d.buffers[i] = nil
378 }
379 d.buffers = nil
380 rb := v4l_requestbuffers{
381 count: 0,
382 typ: v4l_bufTypeVideoCapture,
383 memory: v4l_memoryMmap,
384 }
385 ioctl_reqbufs(d.fd, &rb)
386 }
387
388 // Capture grabs the next frame, and returns a new Buffer holding the raw image
389 // data. The device must be turned on for Capture to succeed. A call to Capture
390 // may render the contents of previously captured buffers unavailable.
391 func (d *Device) Capture() (*Buffer, error) {
392 d.nCaptures++
393
394 // Enqueue the old buffer (if any).
395 if d.bufIndex != noBuffer {
396 b := v4l_buffer{
397 typ: v4l_bufTypeVideoCapture,
398 memory: v4l_memoryMmap,
399 index: d.bufIndex,
400 }
401 d.bufIndex = noBuffer
402 if err := ioctl_qbuf(d.fd, &b); err != nil {
403 return nil, err
404 }
405 }
406
407 // Dequeue a new buffer.
408 b := v4l_buffer{
409 typ: v4l_bufTypeVideoCapture,
410 memory: v4l_memoryMmap,
411 }
412 if err := ioctl_dqbuf(d.fd, &b); err != nil {
413 return nil, err
414 }
415 d.buffers[b.index] = d.buffers[b.index][:b.bytesused]
416 d.bufIndex = b.index
417
418 return &Buffer{d.device, d.nCaptures, 0, b.sequence}, nil
419 }
420
421 // GetConfig returns the current configuration of the device.
422 func (d *Device) GetConfig() (DeviceConfig, error) {
423 // Get format.
424 f := v4l_format_pix{typ: v4l_bufTypeVideoCapture}
425 if err := ioctl_gFmt_pix(d.fd, &f); err != nil {
426 return DeviceConfig{}, err
427 }
428
429 // Get streaming parameters.
430 p := v4l_streamparm_capture{typ: v4l_bufTypeVideoCapture}
431 if err := ioctl_gParm_capture(d.fd, &p); err != nil {
432 return DeviceConfig{}, err
433 }
434
435 cfg := DeviceConfig{
436 Format: f.fmt.pixelformat,
437 Width: int(f.fmt.width),
438 Height: int(f.fmt.height),
439 FPS: Frac{
440 p.parm.timeperframe.denominator,
441 p.parm.timeperframe.numerator,
442 },
443 }
444 return cfg, nil
445 }
446
447 // SetConfig configures the device according to cfg. The configuration actually
448 // applied may be different from what was requested, as drivers are allowed to
449 // adjust the parameters against hardware capabilities (or even completely
450 // ignore them). The configuration cannot be changed while the device is turned
451 // on.
452 func (d *Device) SetConfig(cfg DeviceConfig) error {
453 // Set format.
454 f := v4l_format_pix{
455 typ: v4l_bufTypeVideoCapture,
456 fmt: v4l_pixFormat{
457 width: uint32(cfg.Width),
458 height: uint32(cfg.Height),
459 pixelformat: cfg.Format,
460 field: v4l_fieldNone,
461 colorspace: v4l_colorspaceDefault,
462 priv: 0,
463 },
464 }
465 if err := ioctl_sFmt_pix(d.fd, &f); err != nil {
466 return err
467 }
468
469 // Set streaming parameters.
470 cfg.FPS = cfg.FPS.Reduce()
471 p := v4l_streamparm_capture{
472 typ: v4l_bufTypeVideoCapture,
473 parm: v4l_captureparm{
474 timeperframe: v4l_fract{cfg.FPS.D, cfg.FPS.N},
475 },
476 }
477 if err := ioctl_sParm_capture(d.fd, &p); err != nil {
478 return err
479 }
480
481 return nil
482 }
483
484 // BufferInfo returns information about how image data is laid out in a buffer.
485 // For the same device configuration it always returns the same value.
486 func (d *Device) BufferInfo() (BufferInfo, error) {
487 f := v4l_format_pix{typ: v4l_bufTypeVideoCapture}
488 if err := ioctl_gFmt_pix(d.fd, &f); err != nil {
489 return BufferInfo{}, err
490 }
491 info := BufferInfo{
492 BufferSize: int(f.fmt.sizeimage),
493 ImageStride: int(f.fmt.bytesperline),
494 }
495 return info, nil
496 }
497
498 // ListConfigs returns the configurations supported by the device.
499 func (d *Device) ListConfigs() ([]DeviceConfig, error) {
500 var cfgs []DeviceConfig
501 for fmt := 0; ; fmt++ {
502 fd := v4l_fmtdesc{
503 index: uint32(fmt),
504 typ: v4l_bufTypeVideoCapture,
505 }
506 if err := ioctl_enumFmt(d.fd, &fd); err != nil {
507 if err != syscall.EINVAL {
508 return nil, err
509 }
510 break
511 }
512 sizes, err := d.enumFrameSizes(fd.pixelformat)
513 if err != nil {
514 return nil, err
515 }
516 for _, sz := range sizes {
517 ivals, err := d.enumFrameIvals(fd.pixelformat, sz.width, sz.height)
518 if err != nil {
519 return nil, err
520 }
521 for _, ival := range ivals {
522 cfg := DeviceConfig{
523 Format: fd.pixelformat,
524 Width: int(sz.width),
525 Height: int(sz.height),
526 FPS: Frac{
527 ival.denominator,
528 ival.numerator,
529 }.Reduce(),
530 }
531 cfgs = append(cfgs, cfg)
532 }
533 }
534 }
535
536 // Some devices/drivers seem to return duplicates. Fix that.
537 n := 0
538 for i := 0; i < len(cfgs); i++ {
539 dupe := false
540 for j := 0; j < n && !dupe; j++ {
541 dupe = cfgs[i] == cfgs[j]
542 }
543 if !dupe {
544 cfgs[n] = cfgs[i]
545 n++
546 }
547 }
548 cfgs = cfgs[:n]
549
550 return cfgs, nil
551 }
552
553 // enumFrameSizes returns the supported frame sizes. If the device does not
554 // enumerate a discrete set of frame sizes, then a few common ones within the
555 // supported range are returned.
556 func (d *Device) enumFrameSizes(fmt uint32) ([]v4l_frmsizeDiscrete, error) {
557 var (
558 sizes []v4l_frmsizeDiscrete
559 fs v4l_frmsizeenum
560 )
561
562 // Try to enumerate discrete frame sizes.
563 loop:
564 for index := 0; ; index++ {
565 fs = v4l_frmsizeenum{
566 index: uint32(index),
567 pixelFormat: fmt,
568 }
569 if err := ioctl_enumFramesizes(d.fd, &fs); err != nil {
570 if err != syscall.EINVAL {
571 return nil, err
572 }
573 return sizes, nil
574 }
575 switch fs.typ {
576 case v4l_frmsizeTypeDiscrete:
577 sizes = append(sizes, fs.discrete)
578 case v4l_frmsizeTypeContinuous, v4l_frmsizeTypeStepwise:
579 break loop
580 default:
581 return nil, nil
582 }
583 }
584
585 // Fall back to a default list.
586 fss := fs.stepwise
587 for _, fsd := range defaultFrameSizes {
588 if fsd.width < fss.minWidth || fsd.width > fss.maxWidth ||
589 (fsd.width-fss.minWidth)%fss.stepWidth != 0 {
590 continue
591 }
592 if fsd.height < fss.minHeight || fsd.height > fss.maxHeight ||
593 (fsd.height-fss.minHeight)%fss.stepHeight != 0 {
594 continue
595 }
596 sizes = append(sizes, fsd)
597 }
598
599 sizes = append(sizes, v4l_frmsizeDiscrete{fss.maxWidth, fss.maxHeight})
600
601 return sizes, nil
602 }
603
604 // defaultFrameSizes lists a few common resolutions.
605 var defaultFrameSizes = []v4l_frmsizeDiscrete{
606 {160, 120},
607 {176, 144},
608 {320, 180},
609 {320, 240},
610 {352, 288},
611 {640, 360},
612 {640, 480},
613 {800, 600},
614 {960, 540},
615 {1024, 768},
616 {1280, 720},
617 {1280, 960},
618 {1600, 1200},
619 {1920, 1080},
620 {3840, 2160},
621 {7680, 4320},
622 }
623
624 // enumFrameIvals returns the supported frame intervals. If the device does not
625 // enumerate a discrete set of frame intervals, then a few common ones within
626 // the supported range are returned.
627 func (d *Device) enumFrameIvals(fmt, w, h uint32) ([]v4l_fract, error) {
628 var (
629 ivals []v4l_fract
630 fi v4l_frmivalenum
631 )
632
633 // Try to enumerate discrete frame intervals.
634 loop:
635 for index := 0; ; index++ {
636 fi = v4l_frmivalenum{
637 index: uint32(index),
638 pixelFormat: fmt,
639 width: w,
640 height: h,
641 }
642 if err := ioctl_enumFrameintervals(d.fd, &fi); err != nil {
643 if err != syscall.EINVAL {
644 return nil, err
645 }
646 return ivals, nil
647 }
648 switch fi.typ {
649 case v4l_frmivalTypeDiscrete:
650 ivals = append(ivals, fi.discrete)
651 case v4l_frmivalTypeContinuous, v4l_frmivalTypeStepwise:
652 break loop
653 default:
654 return nil, nil
655 }
656 }
657
658 // Fall back to a default list.
659 var (
660 fis = fi.stepwise
661 min_n = fis.min.numerator
662 min_d = fis.min.denominator
663 max_n = fis.max.numerator
664 max_d = fis.max.denominator
665 )
666 for _, fid := range defaultFrameIntervals {
667 if fid.numerator*min_d < min_n*fid.denominator ||
668 fid.numerator*max_d > max_n*fid.denominator {
669 continue
670 }
671 ivals = append(ivals, fid)
672 }
673
674 ivals = append(ivals, fis.min)
675
676 return ivals, nil
677 }
678
679 // defaultFrameIntervals lists a few common frame intervals.
680 var defaultFrameIntervals = []v4l_fract{
681 {1, 5},
682 {1, 10},
683 {1, 15},
684 {1, 25},
685 {1, 30},
686 {1, 60},
687 }
688
689 // ControlInfo returns information about a control.
690 func (d *Device) ControlInfo(cid uint32) (ControlInfo, error) {
691 info, err := d.controlInfo(cid)
692 if err == errBadControl {
693 // Pretend the control does not exist.
694 err = syscall.EINVAL
695 }
696 return info, err
697 }
698
699 // ListControls returns the ControlInfo for every control the device has.
700 func (d *Device) ListControls() ([]ControlInfo, error) {
701 var (
702 lastCID uint32
703 infos []ControlInfo
704 )
705
706 for {
707 info, err := d.controlInfo(lastCID | v4l_ctrlFlagNextCtrl)
708 switch err {
709 case nil:
710 infos = append(infos, info)
711 lastCID = info.CID
712 case errBadControl:
713 // Pretend the control does not exist.
714 lastCID = info.CID
715 case syscall.EINVAL:
716 if lastCID == 0 {
717 // No support for v4l_ctrlFlagNextCtrl.
718 // Fall back to legacy method.
719 return d.listControlsLegacy()
720 }
721 return infos, nil
722 default:
723 return nil, err
724 }
725 }
726 }
727
728 // listControlsLegacy enumerates all controls the device has by querying them
729 // one-by-one rather than using the v4l_ctrlFlagNextCtrl flag.
730 func (d *Device) listControlsLegacy() ([]ControlInfo, error) {
731 var infos []ControlInfo
732
733 // Standard controls.
734 for cid := uint32(v4l_cidBase); cid < v4l_cidLastp1; cid++ {
735 info, err := d.controlInfo(cid)
736 if err != nil {
737 if err == syscall.EINVAL || err == errBadControl {
738 continue
739 }
740 return nil, err
741 }
742 infos = append(infos, info)
743 }
744
745 // Custom controls.
746 for cid := uint32(v4l_cidPrivateBase); ; cid++ {
747 info, err := d.controlInfo(cid)
748 if err != nil {
749 if err == syscall.EINVAL {
750 break
751 }
752 if err == errBadControl {
753 continue
754 }
755 return nil, err
756 }
757 infos = append(infos, info)
758 }
759
760 return infos, nil
761 }
762
763 // controlInfo returns information about a control. For disabled contorls and
764 // contorls of unsupported type it fails with errBadControl.
765 func (d *device) controlInfo(cid uint32) (ControlInfo, error) {
766 qc := v4l_queryctrl{id: cid}
767 if err := ioctl_queryctrl(d.fd, &qc); err != nil {
768 return ControlInfo{}, err
769 }
770
771 info := ControlInfo{
772 CID: qc.id,
773 Name: qc.name,
774 Min: qc.minimum,
775 Max: qc.maximum,
776 Step: qc.step,
777 Default: qc.defaultValue,
778 }
779
780 if qc.flags&v4l_ctrlFlagDisabled != 0 {
781 return info, errBadControl
782 }
783
784 switch qc.typ {
785 case v4l_ctrlTypeInteger:
786 info.Type = "int"
787 case v4l_ctrlTypeBoolean:
788 info.Type = "bool"
789 case v4l_ctrlTypeMenu:
790 info.Type = "enum"
791 case v4l_ctrlTypeButton:
792 info.Type = "button"
793 case v4l_ctrlTypeIntegerMenu:
794 info.Type = "int-enum"
795 default:
796 return info, errBadControl
797 }
798
799 if qc.typ == v4l_ctrlTypeMenu || qc.typ == v4l_ctrlTypeIntegerMenu {
800 for i := qc.minimum; i <= qc.maximum; i++ {
801 qm := v4l_querymenu{
802 id: qc.id,
803 index: uint32(i),
804 }
805 if err := ioctl_querymenu(d.fd, &qm); err != nil {
806 if err == syscall.EINVAL {
807 continue
808 }
809 return ControlInfo{}, err
810 }
811 opt := struct {
812 Value int32
813 Name string
814 Int64 int64
815 }{
816 Value: int32(qm.index),
817 }
818 if qc.typ == v4l_ctrlTypeMenu {
819 opt.Name = qm.name
820 } else {
821 opt.Int64 = qm.value
822 }
823 info.Options = append(info.Options, opt)
824 }
825 }
826
827 return info, nil
828 }
829
830 // GetControl returns the current value of a control.
831 func (d *Device) GetControl(cid uint32) (int32, error) {
832 c := v4l_control{id: cid}
833 if err := ioctl_gCtrl(d.fd, &c); err != nil {
834 return 0, err
835 }
836 return c.value, nil
837 }
838
839 // SetControl sets the value of a control.
840 func (d *Device) SetControl(cid uint32, value int32) error {
841 c := v4l_control{
842 id: cid,
843 value: value,
844 }
845 if err := ioctl_sCtrl(d.fd, &c); err != nil {
846 return err
847 }
848 return nil
849 }