mediamtx/internal/conf/sourceprotocol.go

74 lines
1.3 KiB
Go
Raw Normal View History

package conf
import (
"encoding/json"
"fmt"
2023-04-01 16:39:12 +00:00
"github.com/bluenviron/gortsplib/v3"
)
// SourceProtocol is the sourceProtocol parameter.
type SourceProtocol struct {
2021-10-22 16:41:10 +00:00
*gortsplib.Transport
}
// MarshalJSON implements json.Marshaler.
func (d SourceProtocol) MarshalJSON() ([]byte, error) {
var out string
2021-10-22 16:41:10 +00:00
if d.Transport == nil {
out = "automatic"
} else {
2021-10-22 16:41:10 +00:00
switch *d.Transport {
case gortsplib.TransportUDP:
out = "udp"
2021-10-22 16:41:10 +00:00
case gortsplib.TransportUDPMulticast:
out = "multicast"
case gortsplib.TransportTCP:
out = "tcp"
default:
return nil, fmt.Errorf("invalid protocol: %v", d.Transport)
}
}
return json.Marshal(out)
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *SourceProtocol) UnmarshalJSON(b []byte) error {
var in string
if err := json.Unmarshal(b, &in); err != nil {
return err
}
switch in {
case "udp":
2021-10-22 16:41:10 +00:00
v := gortsplib.TransportUDP
d.Transport = &v
case "multicast":
2021-10-22 16:41:10 +00:00
v := gortsplib.TransportUDPMulticast
d.Transport = &v
case "tcp":
2021-10-22 16:41:10 +00:00
v := gortsplib.TransportTCP
d.Transport = &v
case "automatic":
d.Transport = nil
default:
2021-09-27 08:43:48 +00:00
return fmt.Errorf("invalid protocol '%s'", in)
}
return nil
}
2022-12-15 23:50:47 +00:00
// unmarshalEnv implements envUnmarshaler.
func (d *SourceProtocol) unmarshalEnv(s string) error {
return d.UnmarshalJSON([]byte(`"` + s + `"`))
}