|
|
|
|
@ -73,11 +73,12 @@ type Conn struct {
@@ -73,11 +73,12 @@ type Conn struct {
|
|
|
|
|
pathMan PathMan |
|
|
|
|
parent Parent |
|
|
|
|
|
|
|
|
|
// read mode
|
|
|
|
|
ringBuffer *ringbuffer.RingBuffer |
|
|
|
|
path readpublisher.Path |
|
|
|
|
ringBuffer *ringbuffer.RingBuffer // read
|
|
|
|
|
|
|
|
|
|
// in
|
|
|
|
|
terminate chan struct{} |
|
|
|
|
terminate chan struct{} |
|
|
|
|
parentTerminate chan struct{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// New allocates a Conn.
|
|
|
|
|
@ -106,7 +107,8 @@ func New(
@@ -106,7 +107,8 @@ func New(
|
|
|
|
|
conn: rtmp.NewServerConn(nconn), |
|
|
|
|
pathMan: pathMan, |
|
|
|
|
parent: parent, |
|
|
|
|
terminate: make(chan struct{}), |
|
|
|
|
terminate: make(chan struct{}, 1), |
|
|
|
|
parentTerminate: make(chan struct{}), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.log(logger.Info, "opened") |
|
|
|
|
@ -117,15 +119,18 @@ func New(
@@ -117,15 +119,18 @@ func New(
|
|
|
|
|
return c |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Close closes a Conn.
|
|
|
|
|
func (c *Conn) Close() { |
|
|
|
|
// ParentClose closes a Conn.
|
|
|
|
|
func (c *Conn) ParentClose() { |
|
|
|
|
c.log(logger.Info, "closed") |
|
|
|
|
close(c.terminate) |
|
|
|
|
close(c.parentTerminate) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RequestClose closes a Conn.
|
|
|
|
|
func (c *Conn) RequestClose() { |
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
// Close closes a Conn.
|
|
|
|
|
func (c *Conn) Close() { |
|
|
|
|
select { |
|
|
|
|
case c.terminate <- struct{}{}: |
|
|
|
|
default: |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IsReadPublisher implements readpublisher.ReadPublisher.
|
|
|
|
|
@ -154,443 +159,352 @@ func (c *Conn) run() {
@@ -154,443 +159,352 @@ func (c *Conn) run() {
|
|
|
|
|
defer onConnectCmd.Close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err := c.conn.ServerHandshake() |
|
|
|
|
if err != nil { |
|
|
|
|
c.log(logger.Info, "ERR: %s", err) |
|
|
|
|
c.ringBuffer = ringbuffer.New(uint64(c.readBufferCount)) |
|
|
|
|
|
|
|
|
|
connErr := make(chan error) |
|
|
|
|
go func() { |
|
|
|
|
connErr <- func() error { |
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err := c.conn.ServerHandshake() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if c.conn.IsPublishing() { |
|
|
|
|
return c.runPublish() |
|
|
|
|
} |
|
|
|
|
return c.runRead() |
|
|
|
|
}() |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
case err := <-connErr: |
|
|
|
|
if err != io.EOF { |
|
|
|
|
c.log(logger.Info, "ERR: %s", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.terminate |
|
|
|
|
return |
|
|
|
|
case <-c.terminate: |
|
|
|
|
c.ringBuffer.Close() |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
<-connErr |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if c.conn.IsPublishing() { |
|
|
|
|
c.runPublish() |
|
|
|
|
} else { |
|
|
|
|
c.runRead() |
|
|
|
|
if c.path != nil { |
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
c.path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.parentTerminate |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (c *Conn) runRead() { |
|
|
|
|
var path readpublisher.Path |
|
|
|
|
func (c *Conn) runRead() error { |
|
|
|
|
pathName, query := pathNameAndQuery(c.conn.URL()) |
|
|
|
|
|
|
|
|
|
sres := make(chan readpublisher.SetupPlayRes) |
|
|
|
|
c.pathMan.OnReadPublisherSetupPlay(readpublisher.SetupPlayReq{ |
|
|
|
|
Author: c, |
|
|
|
|
PathName: pathName, |
|
|
|
|
IP: c.ip(), |
|
|
|
|
ValidateCredentials: func(authMethods []headers.AuthMethod, pathUser string, pathPass string) error { |
|
|
|
|
return c.validateCredentials(pathUser, pathPass, query) |
|
|
|
|
}, |
|
|
|
|
Res: sres}) |
|
|
|
|
res := <-sres |
|
|
|
|
|
|
|
|
|
if res.Err != nil { |
|
|
|
|
if _, ok := res.Err.(readpublisher.ErrAuthCritical); ok { |
|
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
|
<-time.After(pauseAfterAuthError) |
|
|
|
|
} |
|
|
|
|
return res.Err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.path = res.Path |
|
|
|
|
|
|
|
|
|
var videoTrack *gortsplib.Track |
|
|
|
|
var h264Decoder *rtph264.Decoder |
|
|
|
|
var audioTrack *gortsplib.Track |
|
|
|
|
var audioClockRate int |
|
|
|
|
var aacDecoder *rtpaac.Decoder |
|
|
|
|
|
|
|
|
|
err := func() error { |
|
|
|
|
pathName, query := pathNameAndQuery(c.conn.URL()) |
|
|
|
|
|
|
|
|
|
sres := make(chan readpublisher.SetupPlayRes) |
|
|
|
|
c.pathMan.OnReadPublisherSetupPlay(readpublisher.SetupPlayReq{ |
|
|
|
|
Author: c, |
|
|
|
|
PathName: pathName, |
|
|
|
|
IP: c.ip(), |
|
|
|
|
ValidateCredentials: func(authMethods []headers.AuthMethod, pathUser string, pathPass string) error { |
|
|
|
|
return c.validateCredentials(pathUser, pathPass, query) |
|
|
|
|
}, |
|
|
|
|
Res: sres}) |
|
|
|
|
res := <-sres |
|
|
|
|
|
|
|
|
|
if res.Err != nil { |
|
|
|
|
if _, ok := res.Err.(readpublisher.ErrAuthCritical); ok { |
|
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
|
select { |
|
|
|
|
case <-time.After(pauseAfterAuthError): |
|
|
|
|
case <-c.terminate: |
|
|
|
|
} |
|
|
|
|
for i, t := range res.Tracks { |
|
|
|
|
if t.IsH264() { |
|
|
|
|
if videoTrack != nil { |
|
|
|
|
return fmt.Errorf("can't read track %d with RTMP: too many tracks", i+1) |
|
|
|
|
} |
|
|
|
|
return res.Err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
path = res.Path |
|
|
|
|
|
|
|
|
|
for i, t := range res.Tracks { |
|
|
|
|
if t.IsH264() { |
|
|
|
|
if videoTrack != nil { |
|
|
|
|
return fmt.Errorf("can't read track %d with RTMP: too many tracks", i+1) |
|
|
|
|
} |
|
|
|
|
videoTrack = t |
|
|
|
|
h264Decoder = rtph264.NewDecoder() |
|
|
|
|
|
|
|
|
|
videoTrack = t |
|
|
|
|
h264Decoder = rtph264.NewDecoder() |
|
|
|
|
|
|
|
|
|
} else if t.IsAAC() { |
|
|
|
|
if audioTrack != nil { |
|
|
|
|
return fmt.Errorf("can't read track %d with RTMP: too many tracks", i+1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
audioTrack = t |
|
|
|
|
audioClockRate, _ = audioTrack.ClockRate() |
|
|
|
|
aacDecoder = rtpaac.NewDecoder(audioClockRate) |
|
|
|
|
} else if t.IsAAC() { |
|
|
|
|
if audioTrack != nil { |
|
|
|
|
return fmt.Errorf("can't read track %d with RTMP: too many tracks", i+1) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if videoTrack == nil && audioTrack == nil { |
|
|
|
|
return fmt.Errorf("unable to find a video or audio track") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
c.conn.WriteMetadata(videoTrack, audioTrack) |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
}() |
|
|
|
|
if err != nil { |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
c.log(logger.Info, "ERR: %v", err) |
|
|
|
|
|
|
|
|
|
if path != nil { |
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
audioTrack = t |
|
|
|
|
audioClockRate, _ = audioTrack.ClockRate() |
|
|
|
|
aacDecoder = rtpaac.NewDecoder(audioClockRate) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.terminate |
|
|
|
|
return |
|
|
|
|
if videoTrack == nil && audioTrack == nil { |
|
|
|
|
return fmt.Errorf("unable to find a video or audio track") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.ringBuffer = ringbuffer.New(uint64(c.readBufferCount)) |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
c.conn.WriteMetadata(videoTrack, audioTrack) |
|
|
|
|
|
|
|
|
|
pres := make(chan readpublisher.PlayRes) |
|
|
|
|
path.OnReadPublisherPlay(readpublisher.PlayReq{c, pres}) //nolint:govet
|
|
|
|
|
c.path.OnReadPublisherPlay(readpublisher.PlayReq{c, pres}) //nolint:govet
|
|
|
|
|
<-pres |
|
|
|
|
|
|
|
|
|
c.log(logger.Info, "is reading from path '%s'", path.Name()) |
|
|
|
|
c.log(logger.Info, "is reading from path '%s'", c.path.Name()) |
|
|
|
|
|
|
|
|
|
// disable read deadline
|
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Time{}) |
|
|
|
|
|
|
|
|
|
writerDone := make(chan error) |
|
|
|
|
go func() { |
|
|
|
|
writerDone <- func() error { |
|
|
|
|
var videoBuf [][]byte |
|
|
|
|
videoDTSEst := h264.NewDTSEstimator() |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
data, ok := c.ringBuffer.Pull() |
|
|
|
|
if !ok { |
|
|
|
|
return fmt.Errorf("terminated") |
|
|
|
|
var videoBuf [][]byte |
|
|
|
|
videoDTSEst := h264.NewDTSEstimator() |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
data, ok := c.ringBuffer.Pull() |
|
|
|
|
if !ok { |
|
|
|
|
return fmt.Errorf("terminated") |
|
|
|
|
} |
|
|
|
|
pair := data.(trackIDPayloadPair) |
|
|
|
|
|
|
|
|
|
if videoTrack != nil && pair.trackID == videoTrack.ID { |
|
|
|
|
nalus, pts, err := h264Decoder.Decode(pair.buf) |
|
|
|
|
if err != nil { |
|
|
|
|
if err != rtph264.ErrMorePacketsNeeded { |
|
|
|
|
c.log(logger.Warn, "unable to decode video track: %v", err) |
|
|
|
|
} |
|
|
|
|
pair := data.(trackIDPayloadPair) |
|
|
|
|
|
|
|
|
|
if videoTrack != nil && pair.trackID == videoTrack.ID { |
|
|
|
|
nalus, pts, err := h264Decoder.Decode(pair.buf) |
|
|
|
|
if err != nil { |
|
|
|
|
if err != rtph264.ErrMorePacketsNeeded { |
|
|
|
|
c.log(logger.Warn, "unable to decode video track: %v", err) |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, nalu := range nalus { |
|
|
|
|
// remove SPS, PPS and AUD, not needed by RTSP
|
|
|
|
|
typ := h264.NALUType(nalu[0] & 0x1F) |
|
|
|
|
switch typ { |
|
|
|
|
case h264.NALUTypeSPS, h264.NALUTypePPS, h264.NALUTypeAccessUnitDelimiter: |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
videoBuf = append(videoBuf, nalu) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// RTP marker means that all the NALUs with the same PTS have been received.
|
|
|
|
|
// send them together.
|
|
|
|
|
marker := (pair.buf[1] >> 7 & 0x1) > 0 |
|
|
|
|
if marker { |
|
|
|
|
data, err := h264.EncodeAVCC(videoBuf) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dts := videoDTSEst.Feed(pts + ptsOffset) |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err = c.conn.WritePacket(av.Packet{ |
|
|
|
|
Type: av.H264, |
|
|
|
|
Data: data, |
|
|
|
|
Time: dts, |
|
|
|
|
CTime: pts + ptsOffset - dts, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
videoBuf = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if audioTrack != nil && pair.trackID == audioTrack.ID { |
|
|
|
|
aus, pts, err := aacDecoder.Decode(pair.buf) |
|
|
|
|
if err != nil { |
|
|
|
|
if err != rtpaac.ErrMorePacketsNeeded { |
|
|
|
|
c.log(logger.Warn, "unable to decode audio track: %v", err) |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, au := range aus { |
|
|
|
|
auPTS := pts + ptsOffset + time.Duration(i)*1000*time.Second/time.Duration(audioClockRate) |
|
|
|
|
|
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err := c.conn.WritePacket(av.Packet{ |
|
|
|
|
Type: av.AAC, |
|
|
|
|
Data: au, |
|
|
|
|
Time: auPTS, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, nalu := range nalus { |
|
|
|
|
// remove SPS, PPS and AUD, not needed by RTSP
|
|
|
|
|
typ := h264.NALUType(nalu[0] & 0x1F) |
|
|
|
|
switch typ { |
|
|
|
|
case h264.NALUTypeSPS, h264.NALUTypePPS, h264.NALUTypeAccessUnitDelimiter: |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
videoBuf = append(videoBuf, nalu) |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
case err := <-writerDone: |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
// RTP marker means that all the NALUs with the same PTS have been received.
|
|
|
|
|
// send them together.
|
|
|
|
|
marker := (pair.buf[1] >> 7 & 0x1) > 0 |
|
|
|
|
if marker { |
|
|
|
|
data, err := h264.EncodeAVCC(videoBuf) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != io.EOF { |
|
|
|
|
c.log(logger.Info, "ERR: %s", err) |
|
|
|
|
} |
|
|
|
|
dts := videoDTSEst.Feed(pts + ptsOffset) |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err = c.conn.WritePacket(av.Packet{ |
|
|
|
|
Type: av.H264, |
|
|
|
|
Data: data, |
|
|
|
|
Time: dts, |
|
|
|
|
CTime: pts + ptsOffset - dts, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
videoBuf = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.terminate |
|
|
|
|
} else if audioTrack != nil && pair.trackID == audioTrack.ID { |
|
|
|
|
aus, pts, err := aacDecoder.Decode(pair.buf) |
|
|
|
|
if err != nil { |
|
|
|
|
if err != rtpaac.ErrMorePacketsNeeded { |
|
|
|
|
c.log(logger.Warn, "unable to decode audio track: %v", err) |
|
|
|
|
} |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case <-c.terminate: |
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
for i, au := range aus { |
|
|
|
|
auPTS := pts + ptsOffset + time.Duration(i)*1000*time.Second/time.Duration(audioClockRate) |
|
|
|
|
|
|
|
|
|
c.ringBuffer.Close() |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
<-writerDone |
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Now().Add(c.writeTimeout)) |
|
|
|
|
err := c.conn.WritePacket(av.Packet{ |
|
|
|
|
Type: av.AAC, |
|
|
|
|
Data: au, |
|
|
|
|
Time: auPTS, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (c *Conn) runPublish() { |
|
|
|
|
var videoTrack *gortsplib.Track |
|
|
|
|
var audioTrack *gortsplib.Track |
|
|
|
|
var err error |
|
|
|
|
func (c *Conn) runPublish() error { |
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
videoTrack, audioTrack, err := c.conn.ReadMetadata() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var tracks gortsplib.Tracks |
|
|
|
|
var h264Encoder *rtph264.Encoder |
|
|
|
|
var aacEncoder *rtpaac.Encoder |
|
|
|
|
var path readpublisher.Path |
|
|
|
|
|
|
|
|
|
setupDone := make(chan struct{}) |
|
|
|
|
go func() { |
|
|
|
|
defer close(setupDone) |
|
|
|
|
err = func() error { |
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
videoTrack, audioTrack, err = c.conn.ReadMetadata() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if videoTrack != nil { |
|
|
|
|
h264Encoder = rtph264.NewEncoder(96, nil, nil, nil) |
|
|
|
|
tracks = append(tracks, videoTrack) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if videoTrack != nil { |
|
|
|
|
h264Encoder = rtph264.NewEncoder(96, nil, nil, nil) |
|
|
|
|
tracks = append(tracks, videoTrack) |
|
|
|
|
} |
|
|
|
|
if audioTrack != nil { |
|
|
|
|
clockRate, _ := audioTrack.ClockRate() |
|
|
|
|
aacEncoder = rtpaac.NewEncoder(96, clockRate, nil, nil, nil) |
|
|
|
|
tracks = append(tracks, audioTrack) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if audioTrack != nil { |
|
|
|
|
clockRate, _ := audioTrack.ClockRate() |
|
|
|
|
aacEncoder = rtpaac.NewEncoder(96, clockRate, nil, nil, nil) |
|
|
|
|
tracks = append(tracks, audioTrack) |
|
|
|
|
} |
|
|
|
|
for i, t := range tracks { |
|
|
|
|
t.ID = i |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, t := range tracks { |
|
|
|
|
t.ID = i |
|
|
|
|
} |
|
|
|
|
pathName, query := pathNameAndQuery(c.conn.URL()) |
|
|
|
|
|
|
|
|
|
resc := make(chan readpublisher.AnnounceRes) |
|
|
|
|
c.pathMan.OnReadPublisherAnnounce(readpublisher.AnnounceReq{ |
|
|
|
|
Author: c, |
|
|
|
|
PathName: pathName, |
|
|
|
|
Tracks: tracks, |
|
|
|
|
IP: c.ip(), |
|
|
|
|
ValidateCredentials: func(authMethods []headers.AuthMethod, pathUser string, pathPass string) error { |
|
|
|
|
return c.validateCredentials(pathUser, pathPass, query) |
|
|
|
|
}, |
|
|
|
|
Res: resc, |
|
|
|
|
}) |
|
|
|
|
res := <-resc |
|
|
|
|
|
|
|
|
|
if res.Err != nil { |
|
|
|
|
if _, ok := res.Err.(readpublisher.ErrAuthCritical); ok { |
|
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
|
<-time.After(pauseAfterAuthError) |
|
|
|
|
} |
|
|
|
|
return res.Err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pathName, query := pathNameAndQuery(c.conn.URL()) |
|
|
|
|
|
|
|
|
|
resc := make(chan readpublisher.AnnounceRes) |
|
|
|
|
c.pathMan.OnReadPublisherAnnounce(readpublisher.AnnounceReq{ |
|
|
|
|
Author: c, |
|
|
|
|
PathName: pathName, |
|
|
|
|
Tracks: tracks, |
|
|
|
|
IP: c.ip(), |
|
|
|
|
ValidateCredentials: func(authMethods []headers.AuthMethod, pathUser string, pathPass string) error { |
|
|
|
|
return c.validateCredentials(pathUser, pathPass, query) |
|
|
|
|
}, |
|
|
|
|
Res: resc, |
|
|
|
|
}) |
|
|
|
|
res := <-resc |
|
|
|
|
|
|
|
|
|
if res.Err != nil { |
|
|
|
|
if _, ok := res.Err.(readpublisher.ErrAuthCritical); ok { |
|
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
|
select { |
|
|
|
|
case <-time.After(pauseAfterAuthError): |
|
|
|
|
case <-c.terminate: |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return res.Err |
|
|
|
|
} |
|
|
|
|
c.path = res.Path |
|
|
|
|
|
|
|
|
|
path = res.Path |
|
|
|
|
return nil |
|
|
|
|
}() |
|
|
|
|
}() |
|
|
|
|
// disable write deadline
|
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Time{}) |
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
case <-setupDone: |
|
|
|
|
case <-c.terminate: |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
<-setupDone |
|
|
|
|
rresc := make(chan readpublisher.RecordRes) |
|
|
|
|
c.path.OnReadPublisherRecord(readpublisher.RecordReq{Author: c, Res: rresc}) |
|
|
|
|
rres := <-rresc |
|
|
|
|
|
|
|
|
|
if rres.Err != nil { |
|
|
|
|
return rres.Err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
c.log(logger.Info, "ERR: %s", err) |
|
|
|
|
c.log(logger.Info, "is publishing to path '%s', %d %s", |
|
|
|
|
c.path.Name(), |
|
|
|
|
len(tracks), |
|
|
|
|
func() string { |
|
|
|
|
if len(tracks) == 1 { |
|
|
|
|
return "track" |
|
|
|
|
} |
|
|
|
|
return "tracks" |
|
|
|
|
}()) |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.terminate |
|
|
|
|
return |
|
|
|
|
var onPublishCmd *externalcmd.Cmd |
|
|
|
|
if c.path.Conf().RunOnPublish != "" { |
|
|
|
|
_, port, _ := net.SplitHostPort(c.rtspAddress) |
|
|
|
|
onPublishCmd = externalcmd.New(c.path.Conf().RunOnPublish, |
|
|
|
|
c.path.Conf().RunOnPublishRestart, externalcmd.Environment{ |
|
|
|
|
Path: c.path.Name(), |
|
|
|
|
Port: port, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// disable write deadline
|
|
|
|
|
c.conn.NetConn().SetWriteDeadline(time.Time{}) |
|
|
|
|
defer func(path readpublisher.Path) { |
|
|
|
|
if path.Conf().RunOnPublish != "" { |
|
|
|
|
onPublishCmd.Close() |
|
|
|
|
} |
|
|
|
|
}(c.path) |
|
|
|
|
|
|
|
|
|
readerDone := make(chan error) |
|
|
|
|
go func() { |
|
|
|
|
readerDone <- func() error { |
|
|
|
|
resc := make(chan readpublisher.RecordRes) |
|
|
|
|
path.OnReadPublisherRecord(readpublisher.RecordReq{Author: c, Res: resc}) |
|
|
|
|
res := <-resc |
|
|
|
|
rtcpSenders := rtcpsenderset.New(tracks, rres.SP.OnFrame) |
|
|
|
|
defer rtcpSenders.Close() |
|
|
|
|
|
|
|
|
|
if res.Err != nil { |
|
|
|
|
return res.Err |
|
|
|
|
onFrame := func(trackID int, payload []byte) { |
|
|
|
|
rtcpSenders.OnFrame(trackID, gortsplib.StreamTypeRTP, payload) |
|
|
|
|
rres.SP.OnFrame(trackID, gortsplib.StreamTypeRTP, payload) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
pkt, err := c.conn.ReadPacket() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch pkt.Type { |
|
|
|
|
case av.H264: |
|
|
|
|
if videoTrack == nil { |
|
|
|
|
return fmt.Errorf("ERR: received an H264 frame, but track is not set up") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.log(logger.Info, "is publishing to path '%s', %d %s", |
|
|
|
|
path.Name(), |
|
|
|
|
len(tracks), |
|
|
|
|
func() string { |
|
|
|
|
if len(tracks) == 1 { |
|
|
|
|
return "track" |
|
|
|
|
} |
|
|
|
|
return "tracks" |
|
|
|
|
}()) |
|
|
|
|
|
|
|
|
|
var onPublishCmd *externalcmd.Cmd |
|
|
|
|
if path.Conf().RunOnPublish != "" { |
|
|
|
|
_, port, _ := net.SplitHostPort(c.rtspAddress) |
|
|
|
|
onPublishCmd = externalcmd.New(path.Conf().RunOnPublish, |
|
|
|
|
path.Conf().RunOnPublishRestart, externalcmd.Environment{ |
|
|
|
|
Path: path.Name(), |
|
|
|
|
Port: port, |
|
|
|
|
}) |
|
|
|
|
nalus, err := h264.DecodeAVCC(pkt.Data) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
defer func(path readpublisher.Path) { |
|
|
|
|
if path.Conf().RunOnPublish != "" { |
|
|
|
|
onPublishCmd.Close() |
|
|
|
|
} |
|
|
|
|
}(path) |
|
|
|
|
var outNALUs [][]byte |
|
|
|
|
|
|
|
|
|
rtcpSenders := rtcpsenderset.New(tracks, res.SP.OnFrame) |
|
|
|
|
defer rtcpSenders.Close() |
|
|
|
|
for _, nalu := range nalus { |
|
|
|
|
// remove SPS, PPS and AUD, not needed by RTSP
|
|
|
|
|
typ := h264.NALUType(nalu[0] & 0x1F) |
|
|
|
|
switch typ { |
|
|
|
|
case h264.NALUTypeSPS, h264.NALUTypePPS, h264.NALUTypeAccessUnitDelimiter: |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onFrame := func(trackID int, payload []byte) { |
|
|
|
|
rtcpSenders.OnFrame(trackID, gortsplib.StreamTypeRTP, payload) |
|
|
|
|
res.SP.OnFrame(trackID, gortsplib.StreamTypeRTP, payload) |
|
|
|
|
outNALUs = append(outNALUs, nalu) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
c.conn.NetConn().SetReadDeadline(time.Now().Add(c.readTimeout)) |
|
|
|
|
pkt, err := c.conn.ReadPacket() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch pkt.Type { |
|
|
|
|
case av.H264: |
|
|
|
|
if videoTrack == nil { |
|
|
|
|
return fmt.Errorf("ERR: received an H264 frame, but track is not set up") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
nalus, err := h264.DecodeAVCC(pkt.Data) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var outNALUs [][]byte |
|
|
|
|
|
|
|
|
|
for _, nalu := range nalus { |
|
|
|
|
// remove SPS, PPS and AUD, not needed by RTSP
|
|
|
|
|
typ := h264.NALUType(nalu[0] & 0x1F) |
|
|
|
|
switch typ { |
|
|
|
|
case h264.NALUTypeSPS, h264.NALUTypePPS, h264.NALUTypeAccessUnitDelimiter: |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
outNALUs = append(outNALUs, nalu) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if len(outNALUs) == 0 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
frames, err := h264Encoder.Encode(outNALUs, pkt.Time+pkt.CTime) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("ERR while encoding H264: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, frame := range frames { |
|
|
|
|
onFrame(videoTrack.ID, frame) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case av.AAC: |
|
|
|
|
if audioTrack == nil { |
|
|
|
|
return fmt.Errorf("ERR: received an AAC frame, but track is not set up") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
frames, err := aacEncoder.Encode([][]byte{pkt.Data}, pkt.Time+pkt.CTime) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("ERR while encoding AAC: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, frame := range frames { |
|
|
|
|
onFrame(audioTrack.ID, frame) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
return fmt.Errorf("ERR: unexpected packet: %v", pkt.Type) |
|
|
|
|
} |
|
|
|
|
if len(outNALUs) == 0 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
case err := <-readerDone: |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
frames, err := h264Encoder.Encode(outNALUs, pkt.Time+pkt.CTime) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("ERR while encoding H264: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != io.EOF { |
|
|
|
|
c.log(logger.Info, "ERR: %s", err) |
|
|
|
|
} |
|
|
|
|
for _, frame := range frames { |
|
|
|
|
onFrame(videoTrack.ID, frame) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
path = nil |
|
|
|
|
case av.AAC: |
|
|
|
|
if audioTrack == nil { |
|
|
|
|
return fmt.Errorf("ERR: received an AAC frame, but track is not set up") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.parent.OnConnClose(c) |
|
|
|
|
<-c.terminate |
|
|
|
|
frames, err := aacEncoder.Encode([][]byte{pkt.Data}, pkt.Time+pkt.CTime) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("ERR while encoding AAC: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case <-c.terminate: |
|
|
|
|
c.conn.NetConn().Close() |
|
|
|
|
<-readerDone |
|
|
|
|
for _, frame := range frames { |
|
|
|
|
onFrame(audioTrack.ID, frame) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res := make(chan struct{}) |
|
|
|
|
path.OnReadPublisherRemove(readpublisher.RemoveReq{c, res}) //nolint:govet
|
|
|
|
|
<-res |
|
|
|
|
path = nil |
|
|
|
|
default: |
|
|
|
|
return fmt.Errorf("ERR: unexpected packet: %v", pkt.Type) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|