mirror of
https://github.com/lkmio/lkm.git
synced 2025-10-15 19:50:54 +08:00
gb使用receivebuffer
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
package gb28181
|
||||
|
||||
import (
|
||||
"github.com/pion/rtp"
|
||||
"github.com/yangjiechina/avformat/transport"
|
||||
"github.com/yangjiechina/lkm/log"
|
||||
"github.com/yangjiechina/lkm/stream"
|
||||
"net"
|
||||
)
|
||||
|
||||
@@ -13,10 +11,46 @@ type TCPServer struct {
|
||||
filter Filter
|
||||
}
|
||||
|
||||
type TCPSession struct {
|
||||
source GBSource
|
||||
decoder *transport.LengthFieldFrameDecoder
|
||||
receiveBuffer *stream.ReceiveBuffer
|
||||
func (T *TCPServer) OnConnected(conn net.Conn) []byte {
|
||||
log.Sugar.Infof("GB28181连接 conn:%s", conn.RemoteAddr().String())
|
||||
|
||||
con := conn.(*transport.Conn)
|
||||
session := NewTCPSession(conn, T.filter)
|
||||
con.Data = session
|
||||
|
||||
//TCP使用ReceiveBuffer区别在于,多端口模式从第一包就使用ReceiveBuffer, 单端口模式先解析出ssrc, 找到source. 后续再使用ReceiveBuffer.
|
||||
if session.source != nil {
|
||||
return session.receiveBuffer.GetBlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (T *TCPServer) OnPacket(conn net.Conn, data []byte) []byte {
|
||||
session := conn.(*transport.Conn).Data.(*TCPSession)
|
||||
|
||||
//单端口收流
|
||||
if session.source == nil {
|
||||
//直接传给解码器, 先根据ssrc找到source. 后续还是会直接传给source
|
||||
session.Input(data)
|
||||
} else {
|
||||
session.source.(*PassiveSource).PublishSource.Input(data)
|
||||
}
|
||||
|
||||
if session.source != nil {
|
||||
return session.receiveBuffer.GetBlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (T *TCPServer) OnDisConnected(conn net.Conn, err error) {
|
||||
log.Sugar.Infof("GB28181断开连接 conn:%s", conn.RemoteAddr().String())
|
||||
|
||||
con := conn.(*transport.Conn)
|
||||
if con.Data != nil && con.Data.(*TCPSession).source != nil {
|
||||
con.Data.(*TCPSession).source.Close()
|
||||
}
|
||||
|
||||
con.Data = nil
|
||||
}
|
||||
|
||||
func NewTCPServer(addr net.Addr, filter Filter) (*TCPServer, error) {
|
||||
@@ -33,81 +67,3 @@ func NewTCPServer(addr net.Addr, filter Filter) (*TCPServer, error) {
|
||||
server.tcp = tcp
|
||||
return server, nil
|
||||
}
|
||||
|
||||
func (T *TCPServer) OnConnected(conn net.Conn) []byte {
|
||||
log.Sugar.Infof("GB28181连接 conn:%s", conn.RemoteAddr().String())
|
||||
|
||||
con := conn.(*transport.Conn)
|
||||
session := &TCPSession{}
|
||||
if stream.AppConfig.GB28181.IsMultiPort() {
|
||||
session.source = T.filter.(*singleFilter).source
|
||||
session.source.SetConn(con)
|
||||
session.receiveBuffer = stream.NewTCPReceiveBuffer()
|
||||
}
|
||||
|
||||
session.decoder = transport.NewLengthFieldFrameDecoder(0xFFFF, 2, func(bytes []byte) {
|
||||
packet := rtp.Packet{}
|
||||
err := packet.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
log.Sugar.Errorf("解析rtp失败 err:%s conn:%s", err.Error(), conn.RemoteAddr().String())
|
||||
return
|
||||
}
|
||||
|
||||
//单端口模式,ssrc匹配source
|
||||
if session.source == nil {
|
||||
//匹配不到直接关闭链接
|
||||
source := T.filter.FindSource(packet.SSRC)
|
||||
if source == nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
session.source = source
|
||||
session.receiveBuffer = stream.NewTCPReceiveBuffer()
|
||||
session.source.SetConn(con)
|
||||
|
||||
//直接丢给ps解析器, 虽然是非线程安全, 但是是阻塞执行的, 不会和后续走loop event的包冲突
|
||||
session.source.InputRtp(&packet)
|
||||
}
|
||||
|
||||
if stream.SessionStateHandshakeDone == session.source.State() {
|
||||
session.source.PreparePublishSource(conn, packet.SSRC, session.source)
|
||||
}
|
||||
|
||||
session.source.InputRtp(&packet)
|
||||
})
|
||||
|
||||
con.Data = session
|
||||
|
||||
if session.source != nil {
|
||||
return session.receiveBuffer.GetBlock()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (T *TCPServer) OnPacket(conn net.Conn, data []byte) []byte {
|
||||
session := conn.(*transport.Conn).Data.(*TCPSession)
|
||||
|
||||
//单端口收流
|
||||
if session.source == nil {
|
||||
//直接传给解码器, 先根据ssrc找到source. 后续还是会直接传给source
|
||||
if err := session.decoder.Input(data); err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
} else {
|
||||
session.source.Input(data)
|
||||
}
|
||||
|
||||
return session.receiveBuffer.GetBlock()
|
||||
}
|
||||
|
||||
func (T *TCPServer) OnDisConnected(conn net.Conn, err error) {
|
||||
log.Sugar.Infof("GB28181断开连接 conn:%s", conn.RemoteAddr().String())
|
||||
|
||||
con := conn.(*transport.Conn)
|
||||
if con.Data != nil && con.Data.(*TCPSession).source != nil {
|
||||
con.Data.(*TCPSession).source.Close()
|
||||
}
|
||||
con.Data = nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user