Skip to content

Commit

Permalink
rtsp: rename encryption, protocols, serverKey, serverCert into rtspEn…
Browse files Browse the repository at this point in the history
…cryption, rtspProtocols, rtspServerKey, rtspServerCert (#4078)
  • Loading branch information
aler9 authored Dec 25, 2024
1 parent 8cb584b commit dfb792e
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 112 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The `--network=host` flag is mandatory for RTSP to work, since Docker can change

```
docker run --rm -it \
-e MTX_PROTOCOLS=tcp \
-e MTX_RTSPTRANSPORTS=tcp \
-e MTX_WEBRTCADDITIONALHOSTS=192.168.x.x \
-p 8554:8554 \
-p 1935:1935 \
Expand Down Expand Up @@ -1264,7 +1264,7 @@ There are 3 ways to change the configuration:
Parameters that have array as value can be overridden by setting a comma-separated list. For example:

```
MTX_PROTOCOLS="tcp,udp"
MTX_RTSPTRANSPORTS="tcp,udp"
```
Parameters in maps can be overridden by using underscores, in the following way:
Expand Down Expand Up @@ -2266,13 +2266,13 @@ openssl genrsa -out server.key 2048
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
```

Edit `mediamtx.yml`, and set the `protocols`, `encryption`, `serverKey` and serverCert parameters:
Edit `mediamtx.yml` and set the `rtspTransports`, `encryption`, `serverKey` and serverCert parameters:

```yml
protocols: [tcp]
encryption: optional
serverKey: server.key
serverCert: server.crt
rtspTransports: [tcp]
rtspEncryption: optional
rtspServerKey: server.key
rtspServerCert: server.crt
```
Streams can be published and read with the `rtsps` scheme and the `8322` port:
Expand All @@ -2294,7 +2294,7 @@ In some scenarios, when publishing or reading from the server with RTSP, frames
* The stream throughput is too big and the stream can't be transmitted correctly with the UDP transport protocol. UDP is more performant, faster and more efficient than TCP, but doesn't have a retransmission mechanism, that is needed in case of streams that need a large bandwidth. A solution consists in switching to TCP:

```yml
protocols: [tcp]
rtspTransports: [tcp]
```
In case the source is a camera:
Expand All @@ -2319,7 +2319,7 @@ openssl genrsa -out server.key 2048
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
```

Edit mediamtx.yml, and set the `rtmpEncryption`, `rtmpServerKey` and `rtmpServerCert` parameters:
Edit mediamtx.yml and set the `rtmpEncryption`, `rtmpServerKey` and `rtmpServerCert` parameters:

```yml
rtmpEncryption: optional
Expand Down
8 changes: 4 additions & 4 deletions apidocs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ components:
# RTSP server
rtsp:
type: boolean
protocols:
rtspTransports:
type: array
items:
type: string
encryption:
rtspEncryption:
type: string
rtspAddress:
type: string
Expand All @@ -185,9 +185,9 @@ components:
type: integer
multicastRTCPPort:
type: integer
serverKey:
rtspServerKey:
type: string
serverCert:
rtspServerCert:
type: string
rtspAuthMethods:
type: array
Expand Down
46 changes: 31 additions & 15 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,21 @@ type Conf struct {
// RTSP server
RTSP bool `json:"rtsp"`
RTSPDisable *bool `json:"rtspDisable,omitempty"` // deprecated
Protocols Protocols `json:"protocols"`
Encryption Encryption `json:"encryption"`
Protocols *RTSPTransports `json:"protocols,omitempty"` // deprecated
RTSPTransports RTSPTransports `json:"rtspTransports"`
Encryption *Encryption `json:"encryption,omitempty"` // deprecated
RTSPEncryption Encryption `json:"rtspEncryption"`
RTSPAddress string `json:"rtspAddress"`
RTSPSAddress string `json:"rtspsAddress"`
RTPAddress string `json:"rtpAddress"`
RTCPAddress string `json:"rtcpAddress"`
MulticastIPRange string `json:"multicastIPRange"`
MulticastRTPPort int `json:"multicastRTPPort"`
MulticastRTCPPort int `json:"multicastRTCPPort"`
ServerKey string `json:"serverKey"`
ServerCert string `json:"serverCert"`
ServerKey *string `json:"serverKey,omitempty"`
ServerCert *string `json:"serverCert,omitempty"`
RTSPServerKey string `json:"rtspServerKey"`
RTSPServerCert string `json:"rtspServerCert"`
AuthMethods *RTSPAuthMethods `json:"authMethods,omitempty"` // deprecated
RTSPAuthMethods RTSPAuthMethods `json:"rtspAuthMethods"`

Expand Down Expand Up @@ -352,10 +356,10 @@ func (conf *Conf) setDefaults() {

// RTSP server
conf.RTSP = true
conf.Protocols = Protocols{
Protocol(gortsplib.TransportUDP): {},
Protocol(gortsplib.TransportUDPMulticast): {},
Protocol(gortsplib.TransportTCP): {},
conf.RTSPTransports = RTSPTransports{
gortsplib.TransportUDP: {},
gortsplib.TransportUDPMulticast: {},
gortsplib.TransportTCP: {},
}
conf.RTSPAddress = ":8554"
conf.RTSPSAddress = ":8322"
Expand All @@ -364,8 +368,8 @@ func (conf *Conf) setDefaults() {
conf.MulticastIPRange = "224.1.0.0/16"
conf.MulticastRTPPort = 8002
conf.MulticastRTCPPort = 8003
conf.ServerKey = "server.key"
conf.ServerCert = "server.crt"
conf.RTSPServerKey = "server.key"
conf.RTSPServerCert = "server.crt"
conf.RTSPAuthMethods = RTSPAuthMethods{auth.ValidateMethodBasic}

// RTMP server
Expand Down Expand Up @@ -581,12 +585,18 @@ func (conf *Conf) Validate() error {
if conf.RTSPDisable != nil {
conf.RTSP = !*conf.RTSPDisable
}
if conf.Encryption == EncryptionStrict {
if _, ok := conf.Protocols[Protocol(gortsplib.TransportUDP)]; ok {
return fmt.Errorf("strict encryption can't be used with the UDP transport protocol")
if conf.Protocols != nil {
conf.RTSPTransports = *conf.Protocols
}
if conf.Encryption != nil {
conf.RTSPEncryption = *conf.Encryption
}
if conf.RTSPEncryption == EncryptionStrict {
if _, ok := conf.RTSPTransports[gortsplib.TransportUDP]; ok {
return fmt.Errorf("strict encryption cannot be used with the UDP transport protocol")
}
if _, ok := conf.Protocols[Protocol(gortsplib.TransportUDPMulticast)]; ok {
return fmt.Errorf("strict encryption can't be used with the UDP-multicast transport protocol")
if _, ok := conf.RTSPTransports[gortsplib.TransportUDPMulticast]; ok {
return fmt.Errorf("strict encryption cannot be used with the UDP-multicast transport protocol")
}
}
if conf.AuthMethods != nil {
Expand All @@ -602,6 +612,12 @@ func (conf *Conf) Validate() error {
}
}
}
if conf.ServerCert != nil {
conf.RTSPServerCert = *conf.ServerCert
}
if conf.ServerKey != nil {
conf.RTSPServerKey = *conf.ServerKey
}

// RTMP

Expand Down
14 changes: 7 additions & 7 deletions internal/conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestConfFromFileAndEnv(t *testing.T) {
require.NoError(t, err)
require.Equal(t, tmpf, confPath)

require.Equal(t, Protocols{Protocol(gortsplib.TransportTCP): {}}, conf.Protocols)
require.Equal(t, RTSPTransports{gortsplib.TransportTCP: {}}, conf.RTSPTransports)
require.Equal(t, false, conf.RTMP)

pa, ok := conf.Paths["cam1"]
Expand Down Expand Up @@ -292,15 +292,15 @@ func TestConfErrors(t *testing.T) {
},
{
"invalid strict encryption 1",
"encryption: strict\n" +
"protocols: [udp]\n",
"strict encryption can't be used with the UDP transport protocol",
"rtspEncryption: strict\n" +
"rtspTransports: [udp]\n",
"strict encryption cannot be used with the UDP transport protocol",
},
{
"invalid strict encryption 2",
"encryption: strict\n" +
"protocols: [multicast]\n",
"strict encryption can't be used with the UDP-multicast transport protocol",
"rtspEncryption: strict\n" +
"rtspTransports: [multicast]\n",
"strict encryption cannot be used with the UDP-multicast transport protocol",
},
{
"invalid ICE server",
Expand Down
2 changes: 1 addition & 1 deletion internal/conf/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
)

// Encryption is the encryption parameter.
// Encryption is the rtspEncryption / rtmpEncryption parameter.
type Encryption int

// values.
Expand Down
2 changes: 1 addition & 1 deletion internal/conf/rtsp_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *RTSPTransport) UnmarshalJSON(b []byte) error {
d.Transport = nil

default:
return fmt.Errorf("invalid protocol '%s'", in)
return fmt.Errorf("invalid transport '%s'", in)
}

return nil
Expand Down
27 changes: 12 additions & 15 deletions internal/conf/protocol.go → internal/conf/rtsp_transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ import (
"github.com/bluenviron/gortsplib/v4"
)

// Protocol is a RTSP transport.
type Protocol gortsplib.Transport

// Protocols is the protocols parameter.
type Protocols map[Protocol]struct{}
// RTSPTransports is the rtspTransports parameter.
type RTSPTransports map[gortsplib.Transport]struct{}

// MarshalJSON implements json.Marshaler.
func (d Protocols) MarshalJSON() ([]byte, error) {
func (d RTSPTransports) MarshalJSON() ([]byte, error) {
out := make([]string, len(d))
i := 0

for p := range d {
var v string

switch p {
case Protocol(gortsplib.TransportUDP):
case gortsplib.TransportUDP:
v = "udp"

case Protocol(gortsplib.TransportUDPMulticast):
case gortsplib.TransportUDPMulticast:
v = "multicast"

default:
Expand All @@ -44,35 +41,35 @@ func (d Protocols) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON implements json.Unmarshaler.
func (d *Protocols) UnmarshalJSON(b []byte) error {
func (d *RTSPTransports) UnmarshalJSON(b []byte) error {
var in []string
if err := json.Unmarshal(b, &in); err != nil {
return err
}

*d = make(Protocols)
*d = make(RTSPTransports)

for _, proto := range in {
switch proto {
case "udp":
(*d)[Protocol(gortsplib.TransportUDP)] = struct{}{}
(*d)[gortsplib.TransportUDP] = struct{}{}

case "multicast":
(*d)[Protocol(gortsplib.TransportUDPMulticast)] = struct{}{}
(*d)[gortsplib.TransportUDPMulticast] = struct{}{}

case "tcp":
(*d)[Protocol(gortsplib.TransportTCP)] = struct{}{}
(*d)[gortsplib.TransportTCP] = struct{}{}

default:
return fmt.Errorf("invalid protocol: %s", proto)
return fmt.Errorf("invalid transport: %s", proto)
}
}

return nil
}

// UnmarshalEnv implements env.Unmarshaler.
func (d *Protocols) UnmarshalEnv(_ string, v string) error {
func (d *RTSPTransports) UnmarshalEnv(_ string, v string) error {
byts, _ := json.Marshal(strings.Split(v, ","))
return d.UnmarshalJSON(byts)
}
38 changes: 19 additions & 19 deletions internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func TestAPIPathsList(t *testing.T) {
defer os.Remove(serverKeyFpath)

p, ok := newInstance("api: yes\n" +
"encryption: optional\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n" +
"rtspEncryption: optional\n" +
"rtspServerCert: " + serverCertFpath + "\n" +
"rtspServerKey: " + serverKeyFpath + "\n" +
"paths:\n" +
" mypath:\n")
require.Equal(t, true, ok)
Expand Down Expand Up @@ -367,10 +367,10 @@ func TestAPIProtocolListGet(t *testing.T) {

switch ca {
case "rtsps conns", "rtsps sessions":
conf += "protocols: [tcp]\n" +
"encryption: strict\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n"
conf += "rtspTransports: [tcp]\n" +
"rtspEncryption: strict\n" +
"rtspServerCert: " + serverCertFpath + "\n" +
"rtspServerKey: " + serverKeyFpath + "\n"

case "rtmps":
conf += "rtmpEncryption: strict\n" +
Expand Down Expand Up @@ -860,10 +860,10 @@ func TestAPIProtocolGetNotFound(t *testing.T) {

switch ca {
case "rtsps conns", "rtsps sessions":
conf += "protocols: [tcp]\n" +
"encryption: strict\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n"
conf += "rtspTransports: [tcp]\n" +
"rtspEncryption: strict\n" +
"rtspServerCert: " + serverCertFpath + "\n" +
"rtspServerKey: " + serverKeyFpath + "\n"

case "rtmps":
conf += "rtmpEncryption: strict\n" +
Expand Down Expand Up @@ -957,10 +957,10 @@ func TestAPIProtocolKick(t *testing.T) {
conf := "api: yes\n"

if ca == "rtsps" {
conf += "protocols: [tcp]\n" +
"encryption: strict\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n"
conf += "rtspTransports: [tcp]\n" +
"rtspEncryption: strict\n" +
"rtspServerCert: " + serverCertFpath + "\n" +
"rtspServerKey: " + serverKeyFpath + "\n"
}

conf += "paths:\n" +
Expand Down Expand Up @@ -1114,10 +1114,10 @@ func TestAPIProtocolKickNotFound(t *testing.T) {
conf := "api: yes\n"

if ca == "rtsps" {
conf += "protocols: [tcp]\n" +
"encryption: strict\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n"
conf += "rtspTransports: [tcp]\n" +
"rtspEncryption: strict\n" +
"rtspServerCert: " + serverCertFpath + "\n" +
"rtspServerKey: " + serverKeyFpath + "\n"
}

conf += "paths:\n" +
Expand Down
Loading

0 comments on commit dfb792e

Please sign in to comment.