-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
81 lines (60 loc) · 1.47 KB
/
protocol.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
package conf
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/bluenviron/gortsplib/v4"
)
// Protocol is a RTSP transport.
type Protocol gortsplib.Transport
// Protocols is the protocols parameter.
type Protocols map[Protocol]struct{}
// MarshalJSON implements json.Marshaler.
func (d Protocols) MarshalJSON() ([]byte, error) {
out := make([]string, len(d))
i := 0
for p := range d {
var v string
switch p {
case Protocol(gortsplib.TransportUDP):
v = "udp"
case Protocol(gortsplib.TransportUDPMulticast):
v = "multicast"
case Protocol(gortsplib.TransportTCP):
v = "tcp"
default:
return nil, fmt.Errorf("invalid protocol: %v", p)
}
out[i] = v
i++
}
sort.Strings(out)
return json.Marshal(out)
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *Protocols) UnmarshalJSON(b []byte) error {
var in []string
if err := json.Unmarshal(b, &in); err != nil {
return err
}
*d = make(Protocols)
for _, proto := range in {
switch proto {
case "udp":
(*d)[Protocol(gortsplib.TransportUDP)] = struct{}{}
case "multicast":
(*d)[Protocol(gortsplib.TransportUDPMulticast)] = struct{}{}
case "tcp":
(*d)[Protocol(gortsplib.TransportTCP)] = struct{}{}
default:
return fmt.Errorf("invalid protocol: %s", proto)
}
}
return nil
}
// UnmarshalEnv implements env.Unmarshaler.
func (d *Protocols) UnmarshalEnv(_ string, v string) error {
byts, _ := json.Marshal(strings.Split(v, ","))
return d.UnmarshalJSON(byts)
}