Browse Source

fix player timeout

修改了播放器超时断开的bug
pull/132/head
macbookpro 6 years ago
parent
commit
02ee38a14c
  1. 18
      rtsp/rich-conn.go
  2. 9
      rtsp/rtsp-session.go

18
rtsp/rich-conn.go

@ -11,15 +11,21 @@ type RichConn struct {
}
func (conn *RichConn) Read(b []byte) (n int, err error) {
//if conn.timeout > 0 {
// conn.Conn.SetReadDeadline(time.Now().Add(conn.timeout))
//}
if conn.timeout > 0 {
conn.Conn.SetReadDeadline(time.Now().Add(conn.timeout))
} else {
var t time.Time
conn.Conn.SetReadDeadline(t)
}
return conn.Conn.Read(b)
}
func (conn *RichConn) Write(b []byte) (n int, err error) {
//if conn.timeout > 0 {
// conn.Conn.SetWriteDeadline(time.Now().Add(conn.timeout))
//}
if conn.timeout > 0 {
conn.Conn.SetWriteDeadline(time.Now().Add(conn.timeout))
} else {
var t time.Time
conn.Conn.SetWriteDeadline(t)
}
return conn.Conn.Write(b)
}

9
rtsp/rtsp-session.go

@ -87,7 +87,7 @@ const UDP_BUF_SIZE = 1048576
type Session struct {
ID string
Server *Server
Conn net.Conn
Conn *RichConn
connRW *bufio.ReadWriter
connWLock sync.RWMutex
Type SessionType
@ -130,13 +130,13 @@ func (session *Session) String() string {
func NewSession(server *Server, conn net.Conn) *Session {
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
timeoutMillis := utils.Conf().Section("rtsp").Key("timeout").MustInt(0)
timeoutTCPConn := RichConn{conn, time.Duration(timeoutMillis) * time.Millisecond}
timeoutTCPConn := &RichConn{conn, time.Duration(timeoutMillis) * time.Millisecond}
session := &Session{
ID: shortid.MustGenerate(),
Server: server,
Conn: conn,
connRW: bufio.NewReadWriter(bufio.NewReaderSize(&timeoutTCPConn, networkBuffer), bufio.NewWriterSize(&timeoutTCPConn, networkBuffer)),
Conn: timeoutTCPConn,
connRW: bufio.NewReadWriter(bufio.NewReaderSize(timeoutTCPConn, networkBuffer), bufio.NewWriterSize(timeoutTCPConn, networkBuffer)),
StartAt: time.Now(),
Timeout: utils.Conf().Section("rtsp").Key("timeout").MustInt(0),
@ -356,6 +356,7 @@ func (session *Session) handleRequest(req *Request) {
session.VControl = pusher.VControl()
session.ACodec = pusher.ACodec()
session.VCodec = pusher.VCodec()
session.Conn.timeout = 0
res.SetBody(session.Pusher.SDPRaw())
case "SETUP":
ts := req.Header["Transport"]

Loading…
Cancel
Save