mirror of
https://github.com/lkmio/lkm.git
synced 2025-10-04 23:02:43 +08:00
支持保存推流数据包到文件. 打印拉流url
This commit is contained in:
42
stream/stream_server.go
Normal file
42
stream/stream_server.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"github.com/yangjiechina/avformat/transport"
|
||||
"github.com/yangjiechina/lkm/log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type SessionHandler[T any] interface {
|
||||
OnNewSession(conn net.Conn) T
|
||||
|
||||
OnCloseSession(session T)
|
||||
}
|
||||
|
||||
type StreamServer[T any] struct {
|
||||
SourceType SourceType
|
||||
Handler SessionHandler[T]
|
||||
}
|
||||
|
||||
func (s *StreamServer[T]) OnConnected(conn net.Conn) []byte {
|
||||
log.Sugar.Debugf("%s连接 conn:%s", s.SourceType.ToString(), conn.RemoteAddr().String())
|
||||
conn.(*transport.Conn).Data = s.Handler.OnNewSession(conn)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StreamServer[T]) OnPacket(conn net.Conn, data []byte) []byte {
|
||||
if AppConfig.Debug {
|
||||
DumpStream2File(s.SourceType, conn, data)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StreamServer[T]) OnDisConnected(conn net.Conn, err error) {
|
||||
log.Sugar.Debugf("%s断开连接 conn:%s", s.SourceType.ToString(), conn.RemoteAddr().String())
|
||||
|
||||
t := conn.(*transport.Conn)
|
||||
if t.Data != nil {
|
||||
s.Handler.OnCloseSession(t.Data.(T))
|
||||
t.Data = nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user