2021-01-31 16:06:34 +00:00
|
|
|
package clientrtsp
|
2019-12-31 12:48:17 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-10 20:56:46 +00:00
|
|
|
"errors"
|
2019-12-31 12:48:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-08-03 15:35:34 +00:00
|
|
|
"strconv"
|
2020-10-19 20:17:48 +00:00
|
|
|
"sync"
|
2020-09-03 14:24:39 +00:00
|
|
|
"sync/atomic"
|
2020-06-21 09:30:30 +00:00
|
|
|
"time"
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2020-01-20 09:21:05 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2020-11-15 16:56:54 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/auth"
|
|
|
|
"github.com/aler9/gortsplib/pkg/base"
|
|
|
|
"github.com/aler9/gortsplib/pkg/headers"
|
2021-03-21 10:22:49 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/liberrors"
|
2020-10-13 22:02:55 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/client"
|
2020-11-01 21:56:56 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/externalcmd"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2020-11-01 21:56:56 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/stats"
|
2021-03-22 20:40:07 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/streamproc"
|
2019-12-31 12:48:17 +00:00
|
|
|
)
|
|
|
|
|
2020-06-21 09:30:30 +00:00
|
|
|
const (
|
2021-01-06 21:31:08 +00:00
|
|
|
sessionID = "12345678"
|
|
|
|
pauseAfterAuthError = 2 * time.Second
|
2020-06-21 09:30:30 +00:00
|
|
|
)
|
|
|
|
|
2021-01-31 22:11:14 +00:00
|
|
|
func ipEqualOrInRange(ip net.IP, ips []interface{}) bool {
|
|
|
|
for _, item := range ips {
|
|
|
|
switch titem := item.(type) {
|
|
|
|
case net.IP:
|
|
|
|
if titem.Equal(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case *net.IPNet:
|
|
|
|
if titem.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:46:08 +00:00
|
|
|
// PathMan is implemented by pathman.PathMan.
|
|
|
|
type PathMan interface {
|
|
|
|
OnClientDescribe(client.DescribeReq)
|
|
|
|
OnClientSetupPlay(client.SetupPlayReq)
|
|
|
|
OnClientAnnounce(client.AnnounceReq)
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:30:25 +00:00
|
|
|
// Parent is implemented by clientman.ClientMan.
|
2020-10-19 20:17:48 +00:00
|
|
|
type Parent interface {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2021-01-31 20:42:27 +00:00
|
|
|
OnClientClose(client.Client)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
// Client is a RTSP client.
|
2020-10-19 20:17:48 +00:00
|
|
|
type Client struct {
|
2020-11-01 16:33:06 +00:00
|
|
|
rtspPort int
|
2020-10-31 15:36:09 +00:00
|
|
|
readTimeout time.Duration
|
|
|
|
runOnConnect string
|
|
|
|
runOnConnectRestart bool
|
|
|
|
protocols map[gortsplib.StreamProtocol]struct{}
|
2020-11-01 16:33:06 +00:00
|
|
|
wg *sync.WaitGroup
|
|
|
|
stats *stats.Stats
|
2020-12-06 17:01:10 +00:00
|
|
|
conn *gortsplib.ServerConn
|
2021-03-31 19:46:08 +00:00
|
|
|
pathMan PathMan
|
2020-10-31 15:36:09 +00:00
|
|
|
parent Parent
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-03-22 20:40:07 +00:00
|
|
|
path client.Path
|
|
|
|
authUser string
|
|
|
|
authPass string
|
|
|
|
authValidator *auth.Validator
|
|
|
|
authFailures int
|
2021-03-19 21:52:10 +00:00
|
|
|
|
2021-04-11 16:44:02 +00:00
|
|
|
// read
|
2021-03-23 19:37:43 +00:00
|
|
|
setuppedTracks map[int]*gortsplib.Track
|
|
|
|
onReadCmd *externalcmd.Cmd
|
2021-03-19 21:52:10 +00:00
|
|
|
|
2021-04-11 16:44:02 +00:00
|
|
|
// publish
|
2021-03-22 20:40:07 +00:00
|
|
|
sp *streamproc.StreamProc
|
2021-03-19 21:52:10 +00:00
|
|
|
onPublishCmd *externalcmd.Cmd
|
2020-07-29 21:30:42 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
// in
|
2021-01-31 11:23:54 +00:00
|
|
|
terminate chan struct{}
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 11:30:25 +00:00
|
|
|
// New allocates a Client.
|
2020-10-19 20:17:48 +00:00
|
|
|
func New(
|
2020-12-13 22:43:31 +00:00
|
|
|
isTLS bool,
|
2020-11-01 16:33:06 +00:00
|
|
|
rtspPort int,
|
2020-10-19 20:17:48 +00:00
|
|
|
readTimeout time.Duration,
|
|
|
|
runOnConnect string,
|
2020-10-31 15:36:09 +00:00
|
|
|
runOnConnectRestart bool,
|
2020-10-19 20:17:48 +00:00
|
|
|
protocols map[gortsplib.StreamProtocol]struct{},
|
2020-11-01 16:33:06 +00:00
|
|
|
wg *sync.WaitGroup,
|
|
|
|
stats *stats.Stats,
|
2020-12-06 17:01:10 +00:00
|
|
|
conn *gortsplib.ServerConn,
|
2021-03-31 19:46:08 +00:00
|
|
|
pathMan PathMan,
|
2020-10-19 20:17:48 +00:00
|
|
|
parent Parent) *Client {
|
|
|
|
|
|
|
|
c := &Client{
|
2020-11-01 16:33:06 +00:00
|
|
|
rtspPort: rtspPort,
|
2020-10-31 15:36:09 +00:00
|
|
|
readTimeout: readTimeout,
|
|
|
|
runOnConnect: runOnConnect,
|
|
|
|
runOnConnectRestart: runOnConnectRestart,
|
|
|
|
protocols: protocols,
|
2020-11-01 16:33:06 +00:00
|
|
|
wg: wg,
|
|
|
|
stats: stats,
|
2020-12-06 17:01:10 +00:00
|
|
|
conn: conn,
|
2021-03-31 19:46:08 +00:00
|
|
|
pathMan: pathMan,
|
2020-12-06 17:01:10 +00:00
|
|
|
parent: parent,
|
|
|
|
terminate: make(chan struct{}),
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
atomic.AddInt64(c.stats.CountClients, 1)
|
2020-12-13 22:43:31 +00:00
|
|
|
c.log(logger.Info, "connected (%s)", func() string {
|
|
|
|
if isTLS {
|
2021-03-10 14:06:45 +00:00
|
|
|
return "RTSP/TLS"
|
2020-12-13 22:43:31 +00:00
|
|
|
}
|
2021-03-10 14:06:45 +00:00
|
|
|
return "RTSP/TCP"
|
2020-12-13 22:43:31 +00:00
|
|
|
}())
|
2020-10-14 17:35:21 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
c.wg.Add(1)
|
2020-05-10 14:23:57 +00:00
|
|
|
go c.run()
|
2021-01-31 20:42:27 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
return c
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 11:30:25 +00:00
|
|
|
// Close closes a Client.
|
2020-10-19 20:17:48 +00:00
|
|
|
func (c *Client) Close() {
|
|
|
|
atomic.AddInt64(c.stats.CountClients, -1)
|
2020-08-31 20:29:30 +00:00
|
|
|
close(c.terminate)
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
// IsClient implements client.Client.
|
|
|
|
func (c *Client) IsClient() {}
|
|
|
|
|
2020-12-05 19:42:59 +00:00
|
|
|
// IsSource implements path.source.
|
2020-10-19 20:17:48 +00:00
|
|
|
func (c *Client) IsSource() {}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
func (c *Client) log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
c.parent.Log(level, "[client %s] "+format, append([]interface{}{c.conn.NetConn().RemoteAddr().String()}, args...)...)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
2020-07-29 21:30:42 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
func (c *Client) ip() net.IP {
|
2020-05-03 21:26:41 +00:00
|
|
|
return c.conn.NetConn().RemoteAddr().(*net.TCPAddr).IP
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:37:15 +00:00
|
|
|
var errTerminated = errors.New("terminated")
|
2020-08-31 13:31:37 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
func (c *Client) run() {
|
|
|
|
defer c.wg.Done()
|
2020-12-08 11:21:06 +00:00
|
|
|
defer c.log(logger.Info, "disconnected")
|
2020-10-14 17:35:21 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
if c.runOnConnect != "" {
|
2020-11-21 12:34:27 +00:00
|
|
|
onConnectCmd := externalcmd.New(c.runOnConnect, c.runOnConnectRestart, externalcmd.Environment{
|
2020-11-01 16:33:06 +00:00
|
|
|
Path: "",
|
|
|
|
Port: strconv.FormatInt(int64(c.rtspPort), 10),
|
|
|
|
})
|
2020-11-21 12:34:27 +00:00
|
|
|
defer onConnectCmd.Close()
|
2020-02-16 12:04:43 +00:00
|
|
|
}
|
2020-02-15 17:25:29 +00:00
|
|
|
|
2020-12-13 12:58:56 +00:00
|
|
|
onRequest := func(req *base.Request) {
|
|
|
|
c.log(logger.Debug, "[c->s] %v", req)
|
2020-11-07 21:47:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 12:58:56 +00:00
|
|
|
onResponse := func(res *base.Response) {
|
|
|
|
c.log(logger.Debug, "[s->c] %v", res)
|
2020-11-07 21:47:10 +00:00
|
|
|
}
|
2020-12-12 22:37:15 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onDescribe := func(ctx *gortsplib.ServerConnDescribeCtx) (*base.Response, []byte, error) {
|
2021-01-31 20:42:27 +00:00
|
|
|
resc := make(chan client.DescribeRes)
|
2021-03-31 19:46:08 +00:00
|
|
|
c.pathMan.OnClientDescribe(client.DescribeReq{c, ctx.Path, ctx.Req, resc}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
res := <-resc
|
|
|
|
|
|
|
|
if res.Err != nil {
|
|
|
|
switch terr := res.Err.(type) {
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthNotCritical:
|
2021-03-16 13:17:13 +00:00
|
|
|
return terr.Response, nil, nil
|
2020-06-27 19:22:50 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthCritical:
|
2020-12-13 17:59:46 +00:00
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
select {
|
2021-01-30 21:06:02 +00:00
|
|
|
case <-time.After(pauseAfterAuthError):
|
2020-12-13 17:59:46 +00:00
|
|
|
case <-c.terminate:
|
|
|
|
}
|
2021-03-16 13:17:13 +00:00
|
|
|
return terr.Response, nil, errTerminated
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-03-10 14:06:45 +00:00
|
|
|
case client.ErrNoOnePublishing:
|
2021-01-31 11:23:54 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusNotFound,
|
2021-03-16 13:17:13 +00:00
|
|
|
}, nil, res.Err
|
2021-01-31 11:23:54 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
default:
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-03-16 13:17:13 +00:00
|
|
|
}, nil, res.Err
|
2020-05-10 20:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 11:23:54 +00:00
|
|
|
if res.Redirect != "" {
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
2021-01-31 11:23:54 +00:00
|
|
|
StatusCode: base.StatusMovedPermanently,
|
2020-12-12 22:37:15 +00:00
|
|
|
Header: base.Header{
|
2021-01-31 11:23:54 +00:00
|
|
|
"Location": base.HeaderValue{res.Redirect},
|
2020-12-12 22:37:15 +00:00
|
|
|
},
|
2021-03-16 13:17:13 +00:00
|
|
|
}, nil, nil
|
2020-12-12 22:37:15 +00:00
|
|
|
}
|
2021-01-31 11:23:54 +00:00
|
|
|
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusOK,
|
2021-03-16 13:17:13 +00:00
|
|
|
}, res.SDP, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onAnnounce := func(ctx *gortsplib.ServerConnAnnounceCtx) (*base.Response, error) {
|
2021-01-31 20:42:27 +00:00
|
|
|
resc := make(chan client.AnnounceRes)
|
2021-03-31 19:46:08 +00:00
|
|
|
c.pathMan.OnClientAnnounce(client.AnnounceReq{c, ctx.Path, ctx.Tracks, ctx.Req, resc}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
res := <-resc
|
|
|
|
|
|
|
|
if res.Err != nil {
|
|
|
|
switch terr := res.Err.(type) {
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthNotCritical:
|
2020-12-12 22:37:15 +00:00
|
|
|
return terr.Response, nil
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthCritical:
|
2020-12-13 17:59:46 +00:00
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
select {
|
2021-01-30 21:06:02 +00:00
|
|
|
case <-time.After(pauseAfterAuthError):
|
2020-12-13 17:59:46 +00:00
|
|
|
case <-c.terminate:
|
|
|
|
}
|
2020-12-12 22:37:15 +00:00
|
|
|
return terr.Response, errTerminated
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
default:
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-01-30 21:06:02 +00:00
|
|
|
}, res.Err
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
2020-10-24 17:55:47 +00:00
|
|
|
|
2021-01-30 21:06:02 +00:00
|
|
|
c.path = res.Path
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
2020-10-05 19:07:34 +00:00
|
|
|
StatusCode: base.StatusOK,
|
2020-12-12 22:37:15 +00:00
|
|
|
}, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onSetup := func(ctx *gortsplib.ServerConnSetupCtx) (*base.Response, error) {
|
|
|
|
if ctx.Transport.Protocol == gortsplib.StreamProtocolUDP {
|
2021-01-31 11:23:54 +00:00
|
|
|
if _, ok := c.protocols[gortsplib.StreamProtocolUDP]; !ok {
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusUnsupportedTransport,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := c.protocols[gortsplib.StreamProtocolTCP]; !ok {
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusUnsupportedTransport,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
switch c.conn.State() {
|
|
|
|
case gortsplib.ServerConnStateInitial, gortsplib.ServerConnStatePrePlay: // play
|
2021-01-31 20:42:27 +00:00
|
|
|
resc := make(chan client.SetupPlayRes)
|
2021-03-31 19:46:08 +00:00
|
|
|
c.pathMan.OnClientSetupPlay(client.SetupPlayReq{c, ctx.Path, ctx.Req, resc}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
res := <-resc
|
|
|
|
|
|
|
|
if res.Err != nil {
|
|
|
|
switch terr := res.Err.(type) {
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthNotCritical:
|
2020-12-12 22:37:15 +00:00
|
|
|
return terr.Response, nil
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
case client.ErrAuthCritical:
|
2020-12-13 17:59:46 +00:00
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
select {
|
2021-01-30 21:06:02 +00:00
|
|
|
case <-time.After(pauseAfterAuthError):
|
2020-12-13 17:59:46 +00:00
|
|
|
case <-c.terminate:
|
|
|
|
}
|
2020-12-12 22:37:15 +00:00
|
|
|
return terr.Response, errTerminated
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-03-10 14:06:45 +00:00
|
|
|
case client.ErrNoOnePublishing:
|
2021-01-31 11:23:54 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusNotFound,
|
|
|
|
}, res.Err
|
|
|
|
|
2020-12-05 19:42:59 +00:00
|
|
|
default:
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-01-30 21:06:02 +00:00
|
|
|
}, res.Err
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
2020-12-05 19:42:59 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2021-01-30 21:06:02 +00:00
|
|
|
c.path = res.Path
|
2020-09-05 20:56:54 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
if ctx.TrackID >= len(res.Tracks) {
|
2021-03-10 14:06:45 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-03-16 21:24:24 +00:00
|
|
|
}, fmt.Errorf("track %d does not exist", ctx.TrackID)
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
2021-03-23 19:37:43 +00:00
|
|
|
|
|
|
|
if c.setuppedTracks == nil {
|
|
|
|
c.setuppedTracks = make(map[int]*gortsplib.Track)
|
|
|
|
}
|
|
|
|
c.setuppedTracks[ctx.TrackID] = res.Tracks[ctx.TrackID]
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
2021-01-31 11:23:54 +00:00
|
|
|
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusOK,
|
|
|
|
Header: base.Header{
|
|
|
|
"Session": base.HeaderValue{sessionID},
|
|
|
|
},
|
|
|
|
}, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onPlay := func(ctx *gortsplib.ServerConnPlayCtx) (*base.Response, error) {
|
2021-03-23 19:37:43 +00:00
|
|
|
h := base.Header{
|
|
|
|
"Session": base.HeaderValue{sessionID},
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
if c.conn.State() == gortsplib.ServerConnStatePrePlay {
|
2021-03-16 21:24:24 +00:00
|
|
|
if ctx.Path != c.path.Name() {
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-03-16 21:24:24 +00:00
|
|
|
}, fmt.Errorf("path has changed, was '%s', now is '%s'", c.path.Name(), ctx.Path)
|
2020-11-24 22:39:09 +00:00
|
|
|
}
|
2019-12-31 13:55:46 +00:00
|
|
|
|
2021-03-19 21:52:10 +00:00
|
|
|
res := c.playStart()
|
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
// add RTP-Info
|
|
|
|
var ri headers.RTPInfo
|
2021-03-23 20:31:43 +00:00
|
|
|
for trackID, ti := range res.TrackInfos {
|
2021-04-17 11:55:52 +00:00
|
|
|
if ti.LastTimeNTP == 0 {
|
2021-03-23 19:37:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-19 21:52:10 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
track, ok := c.setuppedTracks[trackID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-22 20:40:07 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
u := &base.URL{
|
|
|
|
Scheme: ctx.Req.URL.Scheme,
|
|
|
|
User: ctx.Req.URL.User,
|
|
|
|
Host: ctx.Req.URL.Host,
|
2021-04-03 15:15:26 +00:00
|
|
|
Path: "/" + c.path.Name() + "/trackID=" + strconv.FormatInt(int64(trackID), 10),
|
2021-03-23 19:37:43 +00:00
|
|
|
}
|
2021-03-19 21:52:10 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
clockRate, _ := track.ClockRate()
|
2021-04-17 11:55:52 +00:00
|
|
|
ts := uint32(uint64(ti.LastTimeRTP) +
|
|
|
|
uint64(time.Since(time.Unix(ti.LastTimeNTP, 0)).Seconds()*float64(clockRate)))
|
2021-03-23 20:31:43 +00:00
|
|
|
lsn := ti.LastSequenceNumber
|
2021-03-19 21:52:10 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
ri = append(ri, &headers.RTPInfoEntry{
|
2021-03-29 20:01:51 +00:00
|
|
|
URL: u.String(),
|
2021-03-23 20:31:43 +00:00
|
|
|
SequenceNumber: &lsn,
|
|
|
|
Timestamp: &ts,
|
2021-03-23 19:37:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(ri) > 0 {
|
|
|
|
h["RTP-Info"] = ri.Write()
|
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
2020-10-05 19:07:34 +00:00
|
|
|
StatusCode: base.StatusOK,
|
2021-03-19 21:52:10 +00:00
|
|
|
Header: h,
|
2020-12-12 22:37:15 +00:00
|
|
|
}, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onRecord := func(ctx *gortsplib.ServerConnRecordCtx) (*base.Response, error) {
|
|
|
|
if ctx.Path != c.path.Name() {
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
2021-03-16 21:24:24 +00:00
|
|
|
}, fmt.Errorf("path has changed, was '%s', now is '%s'", c.path.Name(), ctx.Path)
|
2019-12-31 13:55:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
err := c.recordStart()
|
|
|
|
if err != nil {
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
|
|
|
}, err
|
|
|
|
}
|
2020-12-12 22:37:15 +00:00
|
|
|
|
|
|
|
return &base.Response{
|
2020-10-05 19:07:34 +00:00
|
|
|
StatusCode: base.StatusOK,
|
|
|
|
Header: base.Header{
|
2020-12-05 19:42:59 +00:00
|
|
|
"Session": base.HeaderValue{sessionID},
|
2019-12-31 12:48:17 +00:00
|
|
|
},
|
2020-12-12 22:37:15 +00:00
|
|
|
}, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2020-01-03 22:05:06 +00:00
|
|
|
|
2021-03-16 21:24:24 +00:00
|
|
|
onPause := func(ctx *gortsplib.ServerConnPauseCtx) (*base.Response, error) {
|
2021-01-06 20:23:45 +00:00
|
|
|
switch c.conn.State() {
|
|
|
|
case gortsplib.ServerConnStatePlay:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.playStop()
|
2021-01-30 21:06:02 +00:00
|
|
|
res := make(chan struct{})
|
2021-01-31 20:42:27 +00:00
|
|
|
c.path.OnClientPause(client.PauseReq{c, res}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
<-res
|
2020-12-12 22:37:15 +00:00
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
case gortsplib.ServerConnStateRecord:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.recordStop()
|
2021-01-30 21:06:02 +00:00
|
|
|
res := make(chan struct{})
|
2021-01-31 20:42:27 +00:00
|
|
|
c.path.OnClientPause(client.PauseReq{c, res}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
<-res
|
2020-11-07 21:47:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:37:15 +00:00
|
|
|
return &base.Response{
|
2020-11-07 21:47:10 +00:00
|
|
|
StatusCode: base.StatusOK,
|
|
|
|
Header: base.Header{
|
2020-12-05 19:42:59 +00:00
|
|
|
"Session": base.HeaderValue{sessionID},
|
2020-11-07 21:47:10 +00:00
|
|
|
},
|
2020-12-12 22:37:15 +00:00
|
|
|
}, nil
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
|
2021-01-06 11:40:18 +00:00
|
|
|
onFrame := func(trackID int, streamType gortsplib.StreamType, payload []byte) {
|
2021-01-06 20:23:45 +00:00
|
|
|
if c.conn.State() != gortsplib.ServerConnStateRecord {
|
2021-01-06 11:40:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:40:07 +00:00
|
|
|
c.sp.OnFrame(trackID, streamType, payload)
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readDone := c.conn.Read(gortsplib.ServerConnReadHandlers{
|
|
|
|
OnRequest: onRequest,
|
|
|
|
OnResponse: onResponse,
|
|
|
|
OnDescribe: onDescribe,
|
|
|
|
OnAnnounce: onAnnounce,
|
|
|
|
OnSetup: onSetup,
|
|
|
|
OnPlay: onPlay,
|
|
|
|
OnRecord: onRecord,
|
|
|
|
OnPause: onPause,
|
|
|
|
OnFrame: onFrame,
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-readDone:
|
|
|
|
c.conn.Close()
|
2021-01-31 20:42:27 +00:00
|
|
|
|
2021-03-21 10:22:49 +00:00
|
|
|
if err != io.EOF && err != errTerminated {
|
|
|
|
if _, ok := err.(liberrors.ErrServerTeardown); !ok {
|
|
|
|
c.log(logger.Info, "ERR: %s", err)
|
|
|
|
}
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
switch c.conn.State() {
|
|
|
|
case gortsplib.ServerConnStatePlay:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.playStop()
|
2020-12-13 12:58:56 +00:00
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
case gortsplib.ServerConnStateRecord:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.recordStop()
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.path != nil {
|
2021-01-30 21:06:02 +00:00
|
|
|
res := make(chan struct{})
|
2021-01-31 20:42:27 +00:00
|
|
|
c.path.OnClientRemove(client.RemoveReq{c, res}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
<-res
|
2020-12-13 12:58:56 +00:00
|
|
|
c.path = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.parent.OnClientClose(c)
|
|
|
|
<-c.terminate
|
|
|
|
|
|
|
|
case <-c.terminate:
|
|
|
|
c.conn.Close()
|
|
|
|
<-readDone
|
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
switch c.conn.State() {
|
|
|
|
case gortsplib.ServerConnStatePlay:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.playStop()
|
2020-12-13 12:58:56 +00:00
|
|
|
|
2021-01-06 20:23:45 +00:00
|
|
|
case gortsplib.ServerConnStateRecord:
|
2021-01-31 20:42:27 +00:00
|
|
|
c.recordStop()
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.path != nil {
|
2021-01-30 21:06:02 +00:00
|
|
|
res := make(chan struct{})
|
2021-01-31 20:42:27 +00:00
|
|
|
c.path.OnClientRemove(client.RemoveReq{c, res}) //nolint:govet
|
2021-01-30 21:06:02 +00:00
|
|
|
<-res
|
2020-12-13 12:58:56 +00:00
|
|
|
c.path = nil
|
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 12:58:56 +00:00
|
|
|
// Authenticate performs an authentication.
|
2021-01-31 22:11:14 +00:00
|
|
|
func (c *Client) Authenticate(authMethods []headers.AuthMethod,
|
|
|
|
pathName string, ips []interface{},
|
|
|
|
user string, pass string, req interface{}) error {
|
|
|
|
|
2020-12-13 12:58:56 +00:00
|
|
|
// validate ip
|
|
|
|
if ips != nil {
|
|
|
|
ip := c.ip()
|
|
|
|
|
|
|
|
if !ipEqualOrInRange(ip, ips) {
|
|
|
|
c.log(logger.Info, "ERR: ip '%s' not allowed", ip)
|
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
return client.ErrAuthCritical{&base.Response{ //nolint:govet
|
2020-12-13 12:58:56 +00:00
|
|
|
StatusCode: base.StatusUnauthorized,
|
|
|
|
}}
|
2020-08-31 13:31:37 +00:00
|
|
|
}
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate user
|
|
|
|
if user != "" {
|
2021-01-31 22:11:14 +00:00
|
|
|
reqRTSP := req.(*base.Request)
|
|
|
|
|
2020-12-31 18:47:25 +00:00
|
|
|
// reset authValidator every time the credentials change
|
|
|
|
if c.authValidator == nil || c.authUser != user || c.authPass != pass {
|
2020-12-13 12:58:56 +00:00
|
|
|
c.authUser = user
|
|
|
|
c.authPass = pass
|
2020-12-31 18:47:25 +00:00
|
|
|
c.authValidator = auth.NewValidator(user, pass, authMethods)
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 22:11:14 +00:00
|
|
|
// VLC strips the control attribute
|
|
|
|
// provide an alternative URL without the control attribute
|
|
|
|
altURL := func() *base.URL {
|
|
|
|
if reqRTSP.Method != base.Setup {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &base.URL{
|
|
|
|
Scheme: reqRTSP.URL.Scheme,
|
|
|
|
Host: reqRTSP.URL.Host,
|
|
|
|
Path: "/" + pathName + "/",
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := c.authValidator.ValidateHeader(reqRTSP.Header["Authorization"],
|
|
|
|
reqRTSP.Method, reqRTSP.URL, altURL)
|
2020-12-13 12:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
c.authFailures++
|
|
|
|
|
|
|
|
// vlc with login prompt sends 4 requests:
|
|
|
|
// 1) without credentials
|
|
|
|
// 2) with password but without username
|
|
|
|
// 3) without credentials
|
|
|
|
// 4) with password and username
|
|
|
|
// therefore we must allow up to 3 failures
|
|
|
|
if c.authFailures > 3 {
|
|
|
|
c.log(logger.Info, "ERR: unauthorized: %s", err)
|
2020-08-31 13:31:37 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
return client.ErrAuthCritical{&base.Response{ //nolint:govet
|
2020-12-13 12:58:56 +00:00
|
|
|
StatusCode: base.StatusUnauthorized,
|
|
|
|
Header: base.Header{
|
2020-12-31 18:47:25 +00:00
|
|
|
"WWW-Authenticate": c.authValidator.GenerateHeader(),
|
2020-12-13 12:58:56 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.authFailures > 1 {
|
|
|
|
c.log(logger.Debug, "WARN: unauthorized: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
return client.ErrAuthNotCritical{&base.Response{ //nolint:govet
|
2020-12-13 12:58:56 +00:00
|
|
|
StatusCode: base.StatusUnauthorized,
|
|
|
|
Header: base.Header{
|
2020-12-31 18:47:25 +00:00
|
|
|
"WWW-Authenticate": c.authValidator.GenerateHeader(),
|
2020-12-13 12:58:56 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
}
|
2020-12-13 12:58:56 +00:00
|
|
|
|
|
|
|
// login successful, reset authFailures
|
|
|
|
c.authFailures = 0
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:52:10 +00:00
|
|
|
func (c *Client) playStart() client.PlayRes {
|
|
|
|
resc := make(chan client.PlayRes)
|
2021-01-31 20:42:27 +00:00
|
|
|
c.path.OnClientPlay(client.PlayReq{c, resc}) //nolint:govet
|
2021-03-19 21:52:10 +00:00
|
|
|
res := <-resc
|
2020-07-13 09:12:20 +00:00
|
|
|
|
2021-03-16 10:58:26 +00:00
|
|
|
tracksLen := len(c.conn.SetuppedTracks())
|
2021-03-13 20:22:15 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
c.log(logger.Info, "is reading from path '%s', %d %s with %s",
|
|
|
|
c.path.Name(),
|
2021-03-13 20:22:15 +00:00
|
|
|
tracksLen,
|
2021-01-20 21:02:09 +00:00
|
|
|
func() string {
|
2021-03-13 20:22:15 +00:00
|
|
|
if tracksLen == 1 {
|
2021-01-20 21:02:09 +00:00
|
|
|
return "track"
|
|
|
|
}
|
|
|
|
return "tracks"
|
|
|
|
}(),
|
2021-03-13 20:22:15 +00:00
|
|
|
*c.conn.StreamProtocol())
|
2020-07-12 10:34:35 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
if c.path.Conf().RunOnRead != "" {
|
2020-12-12 22:37:15 +00:00
|
|
|
c.onReadCmd = externalcmd.New(c.path.Conf().RunOnRead, c.path.Conf().RunOnReadRestart, externalcmd.Environment{
|
2020-11-01 16:33:06 +00:00
|
|
|
Path: c.path.Name(),
|
|
|
|
Port: strconv.FormatInt(int64(c.rtspPort), 10),
|
|
|
|
})
|
2020-08-31 13:31:37 +00:00
|
|
|
}
|
2021-03-19 21:52:10 +00:00
|
|
|
|
|
|
|
return res
|
2020-08-31 13:31:37 +00:00
|
|
|
}
|
2020-08-29 13:21:56 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
func (c *Client) playStop() {
|
2020-12-12 22:37:15 +00:00
|
|
|
if c.path.Conf().RunOnRead != "" {
|
|
|
|
c.onReadCmd.Close()
|
2020-07-13 09:51:17 +00:00
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
func (c *Client) recordStart() error {
|
|
|
|
resc := make(chan client.RecordRes)
|
|
|
|
c.path.OnClientRecord(client.RecordReq{Client: c, Res: resc})
|
|
|
|
res := <-resc
|
|
|
|
|
|
|
|
if res.Err != nil {
|
|
|
|
return res.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.sp = res.SP
|
2020-10-24 17:55:47 +00:00
|
|
|
|
2021-03-22 20:40:07 +00:00
|
|
|
tracksLen := len(c.conn.AnnouncedTracks())
|
2021-03-13 20:22:15 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
c.log(logger.Info, "is publishing to path '%s', %d %s with %s",
|
|
|
|
c.path.Name(),
|
2021-03-13 20:22:15 +00:00
|
|
|
tracksLen,
|
2021-01-20 21:02:09 +00:00
|
|
|
func() string {
|
2021-03-13 20:22:15 +00:00
|
|
|
if tracksLen == 1 {
|
2021-01-20 21:02:09 +00:00
|
|
|
return "track"
|
|
|
|
}
|
|
|
|
return "tracks"
|
|
|
|
}(),
|
2021-03-13 20:22:15 +00:00
|
|
|
*c.conn.StreamProtocol())
|
2020-10-24 17:55:47 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
if c.path.Conf().RunOnPublish != "" {
|
2020-12-12 22:37:15 +00:00
|
|
|
c.onPublishCmd = externalcmd.New(c.path.Conf().RunOnPublish, c.path.Conf().RunOnPublishRestart, externalcmd.Environment{
|
2020-11-01 16:33:06 +00:00
|
|
|
Path: c.path.Name(),
|
|
|
|
Port: strconv.FormatInt(int64(c.rtspPort), 10),
|
|
|
|
})
|
2020-07-13 09:51:17 +00:00
|
|
|
}
|
2021-03-22 20:40:07 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
return nil
|
2020-08-31 13:31:37 +00:00
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
|
2021-01-31 20:42:27 +00:00
|
|
|
func (c *Client) recordStop() {
|
2020-12-12 22:37:15 +00:00
|
|
|
if c.path.Conf().RunOnPublish != "" {
|
|
|
|
c.onPublishCmd.Close()
|
2020-11-21 10:57:38 +00:00
|
|
|
}
|
2020-12-12 22:37:15 +00:00
|
|
|
}
|
2020-11-07 21:47:10 +00:00
|
|
|
|
2021-03-23 19:37:43 +00:00
|
|
|
// OnFrame implements path.Reader.
|
|
|
|
func (c *Client) OnFrame(trackID int, streamType gortsplib.StreamType, payload []byte) {
|
2021-03-16 10:58:26 +00:00
|
|
|
if _, ok := c.conn.SetuppedTracks()[trackID]; !ok {
|
2020-10-19 20:17:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:52:10 +00:00
|
|
|
c.conn.WriteFrame(trackID, streamType, payload)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|