封装rtsp udp拉流

This commit is contained in:
yangjiechina
2024-03-17 14:18:21 +08:00
parent 14b297827e
commit d325daeb8b
12 changed files with 921 additions and 6 deletions

31
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"github.com/yangjiechina/live-server/flv"
"github.com/yangjiechina/live-server/hls"
"github.com/yangjiechina/live-server/rtsp"
"net"
"net/http"
@@ -14,6 +15,8 @@ import (
"github.com/yangjiechina/live-server/stream"
)
var rtspAddr *net.TCPAddr
func CreateTransStream(source stream.ISource, protocol stream.Protocol, streams []utils.AVStream) stream.ITransStream {
if stream.ProtocolRtmp == protocol {
return rtmp.NewTransStream(librtmp.ChunkSize)
@@ -30,6 +33,13 @@ func CreateTransStream(source stream.ISource, protocol stream.Protocol, streams
return transStream
} else if stream.ProtocolFlv == protocol {
return flv.NewHttpTransStream()
} else if stream.ProtocolRtsp == protocol {
trackFormat := source.Id() + "?track=%d"
return rtsp.NewTransStream(net.IPAddr{
IP: rtspAddr.IP,
Zone: rtspAddr.Zone,
}, trackFormat)
}
return nil
@@ -42,19 +52,32 @@ func init() {
func main() {
stream.AppConfig.GOPCache = true
stream.AppConfig.MergeWriteLatency = 350
rtmpAddr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:1935")
if err != nil {
panic(err)
}
impl := rtmp.NewServer()
addr := "0.0.0.0:1935"
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
err = impl.Start(rtmpAddr)
if err != nil {
panic(err)
}
err = impl.Start(tcpAddr)
println("启动rtmp服务成功:" + rtmpAddr.String())
rtspAddr, err = net.ResolveTCPAddr("tcp", "0.0.0.0:554")
if err != nil {
panic(rtspAddr)
}
rtspServer := rtsp.NewServer()
err = rtspServer.Start(rtspAddr)
if err != nil {
panic(err)
}
println("启动rtmp服务成功:" + addr)
println("启动rtsp服务成功:" + rtspAddr.String())
apiAddr := "0.0.0.0:8080"
go startApiServer(apiAddr)