-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
72 lines (59 loc) · 1.46 KB
/
registry.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
package camera
import (
"fmt"
"reflect"
"sync"
)
type Registry struct {
locker sync.Mutex
platforms []Platform
alreadyRegistered map[reflect.Type]struct{}
}
var defaultRegistry = NewRegistry()
func DefaultRegistry() *Registry {
return defaultRegistry
}
func NewRegistry() *Registry {
return &Registry{
locker: sync.Mutex{},
platforms: []Platform{},
alreadyRegistered: map[reflect.Type]struct{}{},
}
}
func (r *Registry) RegisterPlatform(plat Platform) {
r.locker.Lock()
defer r.locker.Unlock()
t := reflect.TypeOf(plat)
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
if _, ok := r.alreadyRegistered[t]; ok {
panic(fmt.Errorf("type '%T' is already registered", t))
}
r.platforms = append(r.platforms, plat)
}
type DevicePathAndPlatform struct {
DevicePath DevicePath
Platform Platform
}
func (d DevicePathAndPlatform) ListFormats() (Formats, error) {
return d.Platform.ListFormats(d.DevicePath)
}
func (d DevicePathAndPlatform) OpenCamera(format Format) (Camera, error) {
return d.Platform.OpenCamera(d.DevicePath, format)
}
func (r *Registry) ListCameras() ([]DevicePathAndPlatform, error) {
r.locker.Lock()
defer r.locker.Unlock()
var result []DevicePathAndPlatform
for _, plat := range r.platforms {
cameras, _ := plat.ListCameras()
for _, devicePath := range cameras {
result = append(result, DevicePathAndPlatform{
DevicePath: devicePath,
Platform: plat,
})
}
}
return result, nil
}