mediamtx/internal/clientrtsp/client.go

654 lines
15 KiB
Go
Raw Normal View History

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"
"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"
"github.com/aler9/rtsp-simple-server/internal/client"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/externalcmd"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
"github.com/aler9/rtsp-simple-server/internal/streamproc"
2019-12-31 12:48:17 +00:00
)
const (
2021-01-06 21:31:08 +00:00
sessionID = "12345678"
pauseAfterAuthError = 2 * time.Second
)
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 {
Log(logger.Level, string, ...interface{})
OnClientClose(client.Client)
2020-10-19 20:17:48 +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
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
parent Parent
2020-10-19 20:17:48 +00:00
path client.Path
authUser string
authPass string
authValidator *auth.Validator
authFailures int
// read
2021-03-23 19:37:43 +00:00
setuppedTracks map[int]*gortsplib.Track
onReadCmd *externalcmd.Cmd
// publish
sp *streamproc.StreamProc
onPublishCmd *externalcmd.Cmd
2020-10-19 20:17:48 +00:00
// in
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,
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,
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-19 20:17:48 +00:00
c.wg.Add(1)
2020-05-10 14:23:57 +00:00
go c.run()
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)
close(c.terminate)
}
// 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
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-10-19 20:17:48 +00:00
func (c *Client) ip() net.IP {
return c.conn.NetConn().RemoteAddr().(*net.TCPAddr).IP
}
var errTerminated = errors.New("terminated")
2020-10-19 20:17:48 +00:00
func (c *Client) run() {
defer c.wg.Done()
defer c.log(logger.Info, "disconnected")
2020-10-19 20:17:48 +00:00
if c.runOnConnect != "" {
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),
})
defer onConnectCmd.Close()
2020-02-16 12:04:43 +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
}
2021-03-16 21:24:24 +00:00
onDescribe := func(ctx *gortsplib.ServerConnDescribeCtx) (*base.Response, []byte, error) {
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) {
case client.ErrAuthNotCritical:
2021-03-16 13:17:13 +00:00
return terr.Response, nil, nil
2020-06-27 19:22:50 +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:
return &base.Response{
StatusCode: base.StatusNotFound,
2021-03-16 13:17:13 +00:00
}, nil, res.Err
2020-10-19 20:17:48 +00:00
default:
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
}
}
if res.Redirect != "" {
return &base.Response{
StatusCode: base.StatusMovedPermanently,
Header: base.Header{
"Location": base.HeaderValue{res.Redirect},
},
2021-03-16 13:17:13 +00:00
}, nil, nil
}
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) {
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) {
case client.ErrAuthNotCritical:
return terr.Response, nil
2020-10-19 20:17:48 +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:
}
return terr.Response, errTerminated
2020-10-19 20:17:48 +00:00
default:
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
}
2021-01-30 21:06:02 +00:00
c.path = res.Path
2019-12-31 12:48:17 +00:00
return &base.Response{
2020-10-05 19:07:34 +00:00
StatusCode: base.StatusOK,
}, 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 {
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
}
}
switch c.conn.State() {
case gortsplib.ServerConnStateInitial, gortsplib.ServerConnStatePrePlay: // play
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) {
case client.ErrAuthNotCritical:
return terr.Response, nil
2020-10-19 20:17:48 +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:
}
return terr.Response, errTerminated
2020-10-19 20:17:48 +00:00
2021-03-10 14:06:45 +00:00
case client.ErrNoOnePublishing:
return &base.Response{
StatusCode: base.StatusNotFound,
}, res.Err
2020-12-05 19:42:59 +00:00
default:
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
}
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},
}
if c.conn.State() == gortsplib.ServerConnStatePrePlay {
2021-03-16 21:24:24 +00:00
if ctx.Path != c.path.Name() {
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
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 {
if ti.LastTimeNTP == 0 {
2021-03-23 19:37:43 +00:00
continue
}
2021-03-23 19:37:43 +00:00
track, ok := c.setuppedTracks[trackID]
if !ok {
continue
}
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,
Path: "/" + c.path.Name() + "/trackID=" + strconv.FormatInt(int64(trackID), 10),
2021-03-23 19:37:43 +00:00
}
2021-03-23 19:37:43 +00:00
clockRate, _ := track.ClockRate()
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-23 19:37:43 +00:00
ri = append(ri, &headers.RTPInfoEntry{
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
}
return &base.Response{
2020-10-05 19:07:34 +00:00
StatusCode: base.StatusOK,
Header: h,
}, 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() {
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
}
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
},
}, 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) {
switch c.conn.State() {
case gortsplib.ServerConnStatePlay:
c.playStop()
2021-01-30 21:06:02 +00:00
res := make(chan struct{})
c.path.OnClientPause(client.PauseReq{c, res}) //nolint:govet
2021-01-30 21:06:02 +00:00
<-res
case gortsplib.ServerConnStateRecord:
c.recordStop()
2021-01-30 21:06:02 +00:00
res := make(chan struct{})
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
}
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
},
}, 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) {
if c.conn.State() != gortsplib.ServerConnStateRecord {
2021-01-06 11:40:18 +00:00
return
}
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-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
}
switch c.conn.State() {
case gortsplib.ServerConnStatePlay:
c.playStop()
2020-12-13 12:58:56 +00:00
case gortsplib.ServerConnStateRecord:
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{})
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
switch c.conn.State() {
case gortsplib.ServerConnStatePlay:
c.playStop()
2020-12-13 12:58:56 +00:00
case gortsplib.ServerConnStateRecord:
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{})
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)
return client.ErrAuthCritical{&base.Response{ //nolint:govet
2020-12-13 12:58:56 +00:00
StatusCode: base.StatusUnauthorized,
}}
}
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)
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)
}
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
}
func (c *Client) playStart() client.PlayRes {
resc := make(chan client.PlayRes)
c.path.OnClientPlay(client.PlayReq{c, resc}) //nolint:govet
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
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,
func() string {
2021-03-13 20:22:15 +00:00
if tracksLen == 1 {
return "track"
}
return "tracks"
}(),
2021-03-13 20:22:15 +00:00
*c.conn.StreamProtocol())
2020-10-19 20:17:48 +00:00
if c.path.Conf().RunOnRead != "" {
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),
})
}
return res
}
func (c *Client) playStop() {
if c.path.Conf().RunOnRead != "" {
c.onReadCmd.Close()
}
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
tracksLen := len(c.conn.AnnouncedTracks())
2021-03-13 20:22:15 +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,
func() string {
2021-03-13 20:22:15 +00:00
if tracksLen == 1 {
return "track"
}
return "tracks"
}(),
2021-03-13 20:22:15 +00:00
*c.conn.StreamProtocol())
2020-10-19 20:17:48 +00:00
if c.path.Conf().RunOnPublish != "" {
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),
})
}
2021-03-23 19:37:43 +00:00
return nil
}
2020-07-13 09:12:20 +00:00
func (c *Client) recordStop() {
if c.path.Conf().RunOnPublish != "" {
c.onPublishCmd.Close()
}
}
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
}
c.conn.WriteFrame(trackID, streamType, payload)
2020-10-19 20:17:48 +00:00
}