| 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 |
| 21 |
|
| 22 |
import ( |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"syscall" |
| 26 |
) |
| 27 |
|
| 28 |
// FindDevices returns the DeviceInfo for every capture device found in the |
| 29 |
// system. The paths are always returned in absolute form. |
| 30 |
func FindDevices() []DeviceInfo { |
| 31 |
var ( |
| 32 |
devs = make(map[int]string) |
| 33 |
dInfos []DeviceInfo |
| 34 |
) |
| 35 |
filepath.Walk("/dev", func(path string, fInfo os.FileInfo, _ error) error { |
| 36 |
if fInfo == nil || fInfo.Mode()&os.ModeCharDevice == 0 { |
| 37 |
return nil |
| 38 |
} |
| 39 |
var stat syscall.Stat_t |
| 40 |
if err := syscall.Stat(path, &stat); err != nil { |
| 41 |
return nil |
| 42 |
} |
| 43 |
major, minor := int(stat.Rdev>>8), int(stat.Rdev&255) |
| 44 |
if major != 81 { |
| 45 |
return nil |
| 46 |
} |
| 47 |
if _, ok := devs[minor]; ok { |
| 48 |
return nil |
| 49 |
} |
| 50 |
dev, err := Open(path) |
| 51 |
if err != nil { |
| 52 |
return nil |
| 53 |
} |
| 54 |
defer dev.Close() |
| 55 |
dInfo, err := dev.DeviceInfo() |
| 56 |
if err != nil { |
| 57 |
return nil |
| 58 |
} |
| 59 |
dInfos = append(dInfos, dInfo) |
| 60 |
devs[minor] = path |
| 61 |
return nil |
| 62 |
}) |
| 63 |
return dInfos |
| 64 |
} |