mirror of
https://github.com/Monibuca/plugin-rtsp.git
synced 2025-09-26 19:51:14 +08:00
增加强制指定拉流协议的配置
This commit is contained in:
@@ -24,7 +24,7 @@ rtsp://localhost/live/test
|
||||
例如通过ffmpeg向m7s进行推流
|
||||
|
||||
```bash
|
||||
ffmpeg -i [视频源] -f rtsp rtsp://localhost/live/test
|
||||
ffmpeg -i [视频源] -c:v h264 -f rtsp rtsp://localhost/live/test
|
||||
```
|
||||
|
||||
会在m7s内部形成一个名为live/test的流
|
||||
@@ -63,6 +63,7 @@ rtsp:
|
||||
udpaddr: :8000
|
||||
rtcpaddr: :8001
|
||||
readbuffersize: 2048
|
||||
pullprotocol: 'auto'
|
||||
```
|
||||
:::tip 配置覆盖
|
||||
publish
|
||||
|
17
client.go
17
client.go
@@ -14,14 +14,23 @@ type RTSPPuller struct {
|
||||
}
|
||||
|
||||
func (p *RTSPPuller) Connect() error {
|
||||
if p.Transport == gortsplib.TransportTCP {
|
||||
p.Transport = gortsplib.TransportUDP
|
||||
} else {
|
||||
switch rtspConfig.PullProtocol {
|
||||
case "tcp", "TCP":
|
||||
p.Transport = gortsplib.TransportTCP
|
||||
case "udp", "UDP":
|
||||
p.Transport = gortsplib.TransportUDP
|
||||
default:
|
||||
if p.Transport == gortsplib.TransportTCP {
|
||||
p.Transport = gortsplib.TransportUDP
|
||||
} else {
|
||||
p.Transport = gortsplib.TransportTCP
|
||||
}
|
||||
}
|
||||
p.Client = &gortsplib.Client{
|
||||
OnPacketRTP: func(ctx *gortsplib.ClientOnPacketRTPCtx) {
|
||||
p.RTSPPublisher.Tracks[ctx.TrackID].WriteRTPPack(ctx.Packet)
|
||||
if p.RTSPPublisher.Tracks[ctx.TrackID] != nil {
|
||||
p.RTSPPublisher.Tracks[ctx.TrackID].WriteRTPPack(ctx.Packet)
|
||||
}
|
||||
},
|
||||
ReadBufferCount: rtspConfig.ReadBufferSize,
|
||||
Transport: &p.Transport,
|
||||
|
1
main.go
1
main.go
@@ -21,6 +21,7 @@ type RTSPConfig struct {
|
||||
UDPAddr string
|
||||
RTCPAddr string
|
||||
ReadBufferSize int
|
||||
PullProtocol string //tcp、udp、 auto(default)
|
||||
sync.Map
|
||||
}
|
||||
|
||||
|
@@ -63,8 +63,8 @@ func (p *RTSPPublisher) SetTracks() error {
|
||||
}
|
||||
p.Tracks[trackId] = vt
|
||||
t := track.(*gortsplib.TrackH264)
|
||||
vt.WriteSlice(common.NALUSlice{t.SPS()})
|
||||
vt.WriteSlice(common.NALUSlice{t.PPS()})
|
||||
vt.WriteSlice(common.NALUSlice{t.SPS})
|
||||
vt.WriteSlice(common.NALUSlice{t.PPS})
|
||||
case "h265", "hevc":
|
||||
vt := NewH265(p.Stream)
|
||||
if payloadType, err := strconv.Atoi(vals[0]); err == nil {
|
||||
|
@@ -112,7 +112,9 @@ func (conf *RTSPConfig) OnPacketRTP(ctx *gortsplib.ServerHandlerOnPacketRTPCtx)
|
||||
if p, ok := conf.Load(ctx.Session); ok {
|
||||
switch v := p.(type) {
|
||||
case *RTSPPublisher:
|
||||
v.Tracks[ctx.TrackID].WriteRTPPack(ctx.Packet)
|
||||
if v.Tracks[ctx.TrackID] != nil {
|
||||
v.Tracks[ctx.TrackID].WriteRTPPack(ctx.Packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@ package rtsp
|
||||
import (
|
||||
"github.com/aler9/gortsplib"
|
||||
"github.com/aler9/gortsplib/pkg/aac"
|
||||
"go.uber.org/zap"
|
||||
. "m7s.live/engine/v4"
|
||||
"m7s.live/engine/v4/codec"
|
||||
"m7s.live/engine/v4/track"
|
||||
@@ -20,34 +19,36 @@ func (s *RTSPSubscriber) OnEvent(event any) {
|
||||
switch v.CodecID {
|
||||
case codec.CodecID_H264:
|
||||
extra := v.DecoderConfiguration.Raw
|
||||
if vtrack, err := gortsplib.NewTrackH264(v.DecoderConfiguration.PayloadType, extra[0], extra[1], nil); err == nil {
|
||||
s.videoTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, vtrack)
|
||||
vtrack := &gortsplib.TrackH264{
|
||||
PayloadType: v.DecoderConfiguration.PayloadType, SPS: extra[0], PPS: extra[1],
|
||||
}
|
||||
s.videoTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, vtrack)
|
||||
case codec.CodecID_H265:
|
||||
if vtrack, err := NewH265Track(v.DecoderConfiguration.PayloadType, v.DecoderConfiguration.Raw); err == nil {
|
||||
s.videoTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, vtrack)
|
||||
vtrack := &gortsplib.TrackH265{
|
||||
PayloadType: v.DecoderConfiguration.PayloadType, VPS: v.DecoderConfiguration.Raw[0], SPS: v.DecoderConfiguration.Raw[1], PPS: v.DecoderConfiguration.Raw[2],
|
||||
}
|
||||
s.videoTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, vtrack)
|
||||
}
|
||||
s.AddTrack(v)
|
||||
case *track.Audio:
|
||||
switch v.CodecID {
|
||||
case codec.CodecID_AAC:
|
||||
var mpegConf aac.MPEG4AudioConfig
|
||||
mpegConf.Decode(v.DecoderConfiguration.Raw)
|
||||
if atrack, err := gortsplib.NewTrackAAC(v.DecoderConfiguration.PayloadType, int(mpegConf.Type), mpegConf.SampleRate, mpegConf.ChannelCount, mpegConf.AOTSpecificConfig, 13, 3, 3); err == nil {
|
||||
s.audioTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, atrack)
|
||||
} else {
|
||||
v.Stream.Error("error creating AAC track", zap.Error(err))
|
||||
mpegConf.Unmarshal(v.DecoderConfiguration.Raw)
|
||||
atrack := &gortsplib.TrackAAC{
|
||||
PayloadType: v.DecoderConfiguration.PayloadType, Config: &mpegConf, SizeLength: 13, IndexLength: 3, IndexDeltaLength: 3,
|
||||
}
|
||||
s.audioTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, atrack)
|
||||
case codec.CodecID_PCMA:
|
||||
s.audioTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, gortsplib.NewTrackPCMA())
|
||||
|
||||
s.tracks = append(s.tracks, &gortsplib.TrackPCMA{})
|
||||
case codec.CodecID_PCMU:
|
||||
s.audioTrackId = len(s.tracks)
|
||||
s.tracks = append(s.tracks, gortsplib.NewTrackPCMU())
|
||||
s.tracks = append(s.tracks, &gortsplib.TrackPCMU{})
|
||||
}
|
||||
s.AddTrack(v)
|
||||
case ISubscriber:
|
||||
|
12
track.go
12
track.go
@@ -1,12 +0,0 @@
|
||||
package rtsp
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"github.com/aler9/gortsplib"
|
||||
)
|
||||
|
||||
func NewH265Track(payloadType uint8, sprop [][]byte) (gortsplib.Track, error) {
|
||||
return gortsplib.NewTrackGeneric("video", []string{fmt.Sprintf("%d", payloadType)}, fmt.Sprintf("%d H265/90000", payloadType), fmt.Sprintf("%d packetization-mode=1; sprop-vps=%s; sprop-sps=%s; sprop-pps=%s;", payloadType, base64.StdEncoding.EncodeToString(sprop[0]), base64.StdEncoding.EncodeToString(sprop[1]), base64.StdEncoding.EncodeToString(sprop[2])))
|
||||
}
|
Reference in New Issue
Block a user