-
Notifications
You must be signed in to change notification settings - Fork 9
/
device_formatfamilies.go
136 lines (115 loc) · 3.13 KB
/
device_formatfamilies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package v4l2
import (
"github.com/reiver/go-v4l2/buftype"
"golang.org/x/sys/unix"
"unsafe"
)
// FormatFamilies returns an iterator that enables you to list out all the supported formats by the device.
//
// Example:
//
// var device v4l2.Device
//
// // ...
//
// formats, err := device.FormatFamilies() // <---- NOTE THAT THIS IS WHERE THE v4l2.Device.FormatFamilies() METHOD IS CALLED.
// if nil != err {
// return err
// }
// defer formats.Close()
//
// var formatFamily v4l2.FormatFamily
// forformats.Next() {
//
// err := formats.Decode(&formatFamily)
// if nil != err {
// fmt.Fprintf(os.Stderr, "ERROR: Problem decoding format: (%T) %v \n", err, err)
// return err
// }
//
// fmt.Printf("[format] %q (%q) {compressed=%t} {emulated=%t} \n",
// formatFamily.Description(),
// formatFamily.PixelFormat(),
// formatFamily.HasFlags(v4l2.FormatFamilyFlagCompressed),
// formatFamily.HasFlags(v4l2.FormatFamilyFlagEmulated),
// )
// }
// if err := formats.Err(); nil != err {
// return err
// }
func (receiver *Device) FormatFamilies() (FormatFamilies, error) {
if err := receiver.unfit(); nil != err {
return FormatFamilies{}, err
}
return FormatFamilies{
device: receiver,
}, nil
}
// FormatFamilies is an interator that enables you to list out all the supported formats by the device.
type FormatFamilies struct {
device *Device
err error
datum internalFormatFamily
}
// Close closes the FormatFamilies iterator.
func (receiver *FormatFamilies) Close() error {
if nil == receiver {
return nil
}
receiver.device = nil
receiver.err = nil
receiver.datum.index = 0
return nil
}
// Decode loads the next format (previously obtained by calling Next).
func (receiver FormatFamilies) Decode(x interface{}) error {
if nil != receiver.err {
return receiver.err
}
p, ok := x.(*FormatFamily)
if !ok {
return errUnsupportedType
}
p.device = receiver.device
p.internal = receiver.datum
return nil
}
// Err returns any errors that occurred when Next was called.
func (receiver *FormatFamilies) Err() error {
if nil == receiver {
return errNilReceiver
}
return receiver.err
}
// Next fetches the next format.
//
// If there is a next format, it returns true.
// And the next format get be obtained by calling Decode.
//
// If there is not next format, then it returns false.
func (receiver *FormatFamilies) Next() bool {
if nil == receiver {
return false
}
device := receiver.device
if nil == device {
receiver.err = errInternalError
return false
}
receiver.datum.typ = v4l2_buftype.Datum(v4l2_buftype.VideoCapture)
_, _, errorNumber := unix.Syscall(
unix.SYS_IOCTL,
uintptr(device.fileDescriptor),
const_VIDIOC_ENUM_FMT,
uintptr(unsafe.Pointer(&receiver.datum)),
)
if unix.EINVAL == errorNumber {
return false
}
if 0 != errorNumber {
receiver.err = errorNumber
return false
}
receiver.datum.index++
return true
}