mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-21 14:49:36 +08:00
rtmp: support ingesting AV1, VP9, H265, MP3, PCM from other servers (#3751)
This commit is contained in:
@@ -4,13 +4,11 @@ package rtmp
|
||||
import (
|
||||
"context"
|
||||
ctls "crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
||||
|
||||
"github.com/bluenviron/mediamtx/internal/conf"
|
||||
"github.com/bluenviron/mediamtx/internal/defs"
|
||||
@@ -18,7 +16,6 @@ import (
|
||||
"github.com/bluenviron/mediamtx/internal/protocols/rtmp"
|
||||
"github.com/bluenviron/mediamtx/internal/protocols/tls"
|
||||
"github.com/bluenviron/mediamtx/internal/stream"
|
||||
"github.com/bluenviron/mediamtx/internal/unit"
|
||||
)
|
||||
|
||||
// Source is a RTMP static source.
|
||||
@@ -97,73 +94,16 @@ func (s *Source) runReader(u *url.URL, nconn net.Conn) error {
|
||||
return err
|
||||
}
|
||||
|
||||
mc, err := rtmp.NewReader(conn)
|
||||
r, err := rtmp.NewReader(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
videoFormat, audioFormat := mc.Tracks()
|
||||
|
||||
var medias []*description.Media
|
||||
var stream *stream.Stream
|
||||
|
||||
if videoFormat != nil {
|
||||
videoMedia := &description.Media{
|
||||
Type: description.MediaTypeVideo,
|
||||
Formats: []format.Format{videoFormat},
|
||||
}
|
||||
medias = append(medias, videoMedia)
|
||||
|
||||
switch videoFormat.(type) {
|
||||
case *format.H264:
|
||||
mc.OnDataH264(func(pts time.Duration, au [][]byte) {
|
||||
stream.WriteUnit(videoMedia, videoFormat, &unit.H264{
|
||||
Base: unit.Base{
|
||||
NTP: time.Now(),
|
||||
PTS: pts,
|
||||
},
|
||||
AU: au,
|
||||
})
|
||||
})
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported video codec: %T", videoFormat)
|
||||
}
|
||||
}
|
||||
|
||||
if audioFormat != nil { //nolint:dupl
|
||||
audioMedia := &description.Media{
|
||||
Type: description.MediaTypeAudio,
|
||||
Formats: []format.Format{audioFormat},
|
||||
}
|
||||
medias = append(medias, audioMedia)
|
||||
|
||||
switch audioFormat.(type) {
|
||||
case *format.MPEG4Audio:
|
||||
mc.OnDataMPEG4Audio(func(pts time.Duration, au []byte) {
|
||||
stream.WriteUnit(audioMedia, audioFormat, &unit.MPEG4Audio{
|
||||
Base: unit.Base{
|
||||
NTP: time.Now(),
|
||||
PTS: pts,
|
||||
},
|
||||
AUs: [][]byte{au},
|
||||
})
|
||||
})
|
||||
|
||||
case *format.MPEG1Audio:
|
||||
mc.OnDataMPEG1Audio(func(pts time.Duration, frame []byte) {
|
||||
stream.WriteUnit(audioMedia, audioFormat, &unit.MPEG1Audio{
|
||||
Base: unit.Base{
|
||||
NTP: time.Now(),
|
||||
PTS: pts,
|
||||
},
|
||||
Frames: [][]byte{frame},
|
||||
})
|
||||
})
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported audio codec: %T", audioFormat)
|
||||
}
|
||||
medias, err := rtmp.ToStream(r, &stream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
|
||||
@@ -183,7 +123,7 @@ func (s *Source) runReader(u *url.URL, nconn net.Conn) error {
|
||||
|
||||
for {
|
||||
nconn.SetReadDeadline(time.Now().Add(time.Duration(s.ReadTimeout)))
|
||||
err := mc.Read()
|
||||
err := r.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user