mediamtx/internal/conf/protocol.go

79 lines
1.4 KiB
Go
Raw Normal View History

package conf
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/aler9/gortsplib/v2"
)
2021-10-22 16:41:10 +00:00
// 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 {
2021-10-22 16:41:10 +00:00
case Protocol(gortsplib.TransportUDP):
v = "udp"
2021-10-22 16:41:10 +00:00
case Protocol(gortsplib.TransportUDPMulticast):
v = "multicast"
default:
v = "tcp"
}
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":
2021-10-22 16:41:10 +00:00
(*d)[Protocol(gortsplib.TransportUDP)] = struct{}{}
case "multicast":
2021-10-22 16:41:10 +00:00
(*d)[Protocol(gortsplib.TransportUDPMulticast)] = struct{}{}
case "tcp":
2021-10-22 16:41:10 +00:00
(*d)[Protocol(gortsplib.TransportTCP)] = struct{}{}
default:
2021-09-27 08:43:48 +00:00
return fmt.Errorf("invalid protocol: %s", proto)
}
}
return nil
}
2022-12-15 23:50:47 +00:00
// unmarshalEnv implements envUnmarshaler.
func (d *Protocols) unmarshalEnv(s string) error {
byts, _ := json.Marshal(strings.Split(s, ","))
return d.UnmarshalJSON(byts)
}