接口不使用“I”开头, 实现类不使用"impl"结尾

This commit is contained in:
yangjiechina
2024-06-06 19:27:56 +08:00
parent 39c787fbee
commit 2ae2622945
37 changed files with 458 additions and 481 deletions

View File

@@ -7,39 +7,39 @@ import (
"net"
)
type IServer interface {
type Server interface {
Start(addr net.Addr) error
Close()
}
func NewServer(password string) IServer {
return &serverImpl{
func NewServer(password string) Server {
return &server{
handler: newHandler(password),
}
}
type serverImpl struct {
type server struct {
tcp *transport.TCPServer
handler *handler
}
func (s *serverImpl) Start(addr net.Addr) error {
func (s *server) Start(addr net.Addr) error {
utils.Assert(s.tcp == nil)
//监听TCP端口
server := &transport.TCPServer{}
server.SetHandler(s)
err := server.Bind(addr)
tcp := &transport.TCPServer{}
tcp.SetHandler(s)
err := tcp.Bind(addr)
if err != nil {
return err
}
s.tcp = server
s.tcp = tcp
return nil
}
func (s *serverImpl) closeSession(conn net.Conn) {
func (s *server) closeSession(conn net.Conn) {
t := conn.(*transport.Conn)
if t.Data != nil {
t.Data.(*session).close()
@@ -47,18 +47,18 @@ func (s *serverImpl) closeSession(conn net.Conn) {
}
}
func (s *serverImpl) Close() {
func (s *server) Close() {
}
func (s *serverImpl) OnConnected(conn net.Conn) {
func (s *server) OnConnected(conn net.Conn) {
log.Sugar.Debugf("rtsp连接 conn:%s", conn.RemoteAddr().String())
t := conn.(*transport.Conn)
t.Data = NewSession(conn)
}
func (s *serverImpl) OnPacket(conn net.Conn, data []byte) {
func (s *server) OnPacket(conn net.Conn, data []byte) {
t := conn.(*transport.Conn)
method, url, header, err := parseMessage(data)
@@ -75,7 +75,7 @@ func (s *serverImpl) OnPacket(conn net.Conn, data []byte) {
}
}
func (s *serverImpl) OnDisConnected(conn net.Conn, err error) {
func (s *server) OnDisConnected(conn net.Conn, err error) {
log.Sugar.Debugf("rtsp断开连接 conn:%s", conn.RemoteAddr().String())
s.closeSession(conn)

View File

@@ -20,7 +20,7 @@ var (
// 对于udp而言, 每个sink维护多个transport
// tcp直接单端口传输
type sink struct {
stream.SinkImpl
stream.BaseSink
senders []*librtp.RtpSender //一个rtsp源可能存在多个流, 每个流都需要拉取
sdpCb func(sdp string) //rtsp_stream生成sdp后使用该回调给rtsp_session, 响应describe
@@ -29,9 +29,9 @@ type sink struct {
playing bool //是否已经收到play请求
}
func NewSink(id stream.SinkId, sourceId string, conn net.Conn, cb func(sdp string)) stream.ISink {
func NewSink(id stream.SinkId, sourceId string, conn net.Conn, cb func(sdp string)) stream.Sink {
return &sink{
stream.SinkImpl{Id_: id, SourceId_: sourceId, Protocol_: stream.ProtocolRtsp, Conn: conn},
stream.BaseSink{Id_: id, SourceId_: sourceId, Protocol_: stream.ProtocolRtsp, Conn: conn},
nil,
cb,
false,
@@ -157,7 +157,7 @@ func (s *sink) SendHeader(data []byte) error {
}
func (s *sink) Close() {
s.SinkImpl.Close()
s.BaseSink.Close()
for _, sender := range s.senders {
if sender.Rtp != nil {

View File

@@ -21,7 +21,7 @@ const (
// rtsp传输流封装
// 低延迟是rtsp特性, 所以不考虑实现GOP缓存
type tranStream struct {
stream.TransStreamImpl
stream.BaseTransStream
addr net.IPAddr
addrType string
urlFormat string
@@ -30,7 +30,7 @@ type tranStream struct {
sdp string
}
func NewTransStream(addr net.IPAddr, urlFormat string) stream.ITransStream {
func NewTransStream(addr net.IPAddr, urlFormat string) stream.TransStream {
t := &tranStream{
addr: addr,
urlFormat: urlFormat,
@@ -46,7 +46,7 @@ func NewTransStream(addr net.IPAddr, urlFormat string) stream.ITransStream {
return t
}
func TransStreamFactory(source stream.ISource, protocol stream.Protocol, streams []utils.AVStream) (stream.ITransStream, error) {
func TransStreamFactory(source stream.Source, protocol stream.Protocol, streams []utils.AVStream) (stream.TransStream, error) {
trackFormat := source.Id() + "?track=%d"
return NewTransStream(net.IPAddr{
IP: net.ParseIP(stream.AppConfig.PublicIP),
@@ -130,7 +130,7 @@ func (t *tranStream) Input(packet utils.AVPacket) error {
}
stream_.cache = true
parameters := t.TransStreamImpl.Tracks[packet.Index()].CodecParameters()
parameters := t.BaseTransStream.Tracks[packet.Index()].CodecParameters()
if utils.AVCodecIdH265 == packet.CodecId() {
bytes := parameters.DecoderConfRecord().(*libhevc.HEVCDecoderConfRecord).VPS
@@ -145,24 +145,24 @@ func (t *tranStream) Input(packet utils.AVPacket) error {
stream_.extraDataBuffer = stream_.tmpExtraDataBuffer
}
data := libavc.RemoveStartCode(packet.AnnexBPacketData(t.TransStreamImpl.Tracks[packet.Index()]))
data := libavc.RemoveStartCode(packet.AnnexBPacketData(t.BaseTransStream.Tracks[packet.Index()]))
stream_.muxer.Input(data, uint32(packet.ConvertPts(stream_.rate)))
}
return nil
}
func (t *tranStream) AddSink(sink_ stream.ISink) error {
sink_.(*sink).setSenderCount(len(t.TransStreamImpl.Tracks))
func (t *tranStream) AddSink(sink_ stream.Sink) error {
sink_.(*sink).setSenderCount(len(t.BaseTransStream.Tracks))
if err := sink_.(*sink).SendHeader([]byte(t.sdp)); err != nil {
return err
}
return t.TransStreamImpl.AddSink(sink_)
return t.BaseTransStream.AddSink(sink_)
}
func (t *tranStream) AddTrack(stream utils.AVStream) error {
if err := t.TransStreamImpl.AddTrack(stream); err != nil {
if err := t.BaseTransStream.AddTrack(stream); err != nil {
return err
}