-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rtmp: support ingesting AV1, VP9, H265, MP3, PCM from other servers (#…
- Loading branch information
Showing
8 changed files
with
353 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package rtmp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/bluenviron/gortsplib/v4/pkg/format" | ||
"github.com/bluenviron/mediacommon/pkg/codecs/h264" | ||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg1audio" | ||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio" | ||
"github.com/bluenviron/mediamtx/internal/asyncwriter" | ||
"github.com/bluenviron/mediamtx/internal/stream" | ||
"github.com/bluenviron/mediamtx/internal/unit" | ||
) | ||
|
||
var errNoSupportedCodecs = errors.New( | ||
"the stream doesn't contain any supported codec, which are currently H264, MPEG-4 Audio, MPEG-1/2 Audio") | ||
|
||
func setupVideo( | ||
stream *stream.Stream, | ||
writer *asyncwriter.Writer, | ||
w **Writer, | ||
nconn net.Conn, | ||
writeTimeout time.Duration, | ||
) format.Format { | ||
var videoFormatH264 *format.H264 | ||
videoMedia := stream.Desc().FindFormat(&videoFormatH264) | ||
|
||
if videoFormatH264 != nil { | ||
var videoDTSExtractor *h264.DTSExtractor | ||
|
||
stream.AddReader(writer, videoMedia, videoFormatH264, func(u unit.Unit) error { | ||
tunit := u.(*unit.H264) | ||
|
||
if tunit.AU == nil { | ||
return nil | ||
} | ||
|
||
idrPresent := false | ||
nonIDRPresent := false | ||
|
||
for _, nalu := range tunit.AU { | ||
typ := h264.NALUType(nalu[0] & 0x1F) | ||
switch typ { | ||
case h264.NALUTypeIDR: | ||
idrPresent = true | ||
|
||
case h264.NALUTypeNonIDR: | ||
nonIDRPresent = true | ||
} | ||
} | ||
|
||
var dts time.Duration | ||
|
||
// wait until we receive an IDR | ||
if videoDTSExtractor == nil { | ||
if !idrPresent { | ||
return nil | ||
} | ||
|
||
videoDTSExtractor = h264.NewDTSExtractor() | ||
|
||
var err error | ||
dts, err = videoDTSExtractor.Extract(tunit.AU, tunit.PTS) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
if !idrPresent && !nonIDRPresent { | ||
return nil | ||
} | ||
|
||
var err error | ||
dts, err = videoDTSExtractor.Extract(tunit.AU, tunit.PTS) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
nconn.SetWriteDeadline(time.Now().Add(writeTimeout)) | ||
return (*w).WriteH264(tunit.PTS, dts, idrPresent, tunit.AU) | ||
}) | ||
|
||
return videoFormatH264 | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setupAudio( | ||
stream *stream.Stream, | ||
writer *asyncwriter.Writer, | ||
w **Writer, | ||
nconn net.Conn, | ||
writeTimeout time.Duration, | ||
) format.Format { | ||
var audioFormatMPEG4Audio *format.MPEG4Audio | ||
audioMedia := stream.Desc().FindFormat(&audioFormatMPEG4Audio) | ||
|
||
if audioMedia != nil { | ||
stream.AddReader(writer, audioMedia, audioFormatMPEG4Audio, func(u unit.Unit) error { | ||
tunit := u.(*unit.MPEG4Audio) | ||
|
||
if tunit.AUs == nil { | ||
return nil | ||
} | ||
|
||
for i, au := range tunit.AUs { | ||
nconn.SetWriteDeadline(time.Now().Add(writeTimeout)) | ||
err := (*w).WriteMPEG4Audio( | ||
tunit.PTS+time.Duration(i)*mpeg4audio.SamplesPerAccessUnit* | ||
time.Second/time.Duration(audioFormatMPEG4Audio.ClockRate()), | ||
au, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return audioFormatMPEG4Audio | ||
} | ||
|
||
var audioFormatMPEG1 *format.MPEG1Audio | ||
audioMedia = stream.Desc().FindFormat(&audioFormatMPEG1) | ||
|
||
if audioMedia != nil { | ||
stream.AddReader(writer, audioMedia, audioFormatMPEG1, func(u unit.Unit) error { | ||
tunit := u.(*unit.MPEG1Audio) | ||
|
||
pts := tunit.PTS | ||
|
||
for _, frame := range tunit.Frames { | ||
var h mpeg1audio.FrameHeader | ||
err := h.Unmarshal(frame) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !(!h.MPEG2 && h.Layer == 3) { | ||
return fmt.Errorf("RTMP only supports MPEG-1 layer 3 audio") | ||
} | ||
|
||
nconn.SetWriteDeadline(time.Now().Add(writeTimeout)) | ||
err = (*w).WriteMPEG1Audio(pts, &h, frame) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pts += time.Duration(h.SampleCount()) * | ||
time.Second / time.Duration(h.SampleRate) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return audioFormatMPEG1 | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// FromStream maps a MediaMTX stream to a RTMP stream. | ||
func FromStream( | ||
stream *stream.Stream, | ||
writer *asyncwriter.Writer, | ||
conn *Conn, | ||
nconn net.Conn, | ||
writeTimeout time.Duration, | ||
) error { | ||
var w *Writer | ||
|
||
videoFormat := setupVideo( | ||
stream, | ||
writer, | ||
&w, | ||
nconn, | ||
writeTimeout, | ||
) | ||
|
||
audioFormat := setupAudio( | ||
stream, | ||
writer, | ||
&w, | ||
nconn, | ||
writeTimeout, | ||
) | ||
|
||
if videoFormat == nil && audioFormat == nil { | ||
return errNoSupportedCodecs | ||
} | ||
|
||
var err error | ||
w, err = NewWriter(conn, videoFormat, audioFormat) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package rtmp | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bluenviron/gortsplib/v4/pkg/description" | ||
"github.com/bluenviron/gortsplib/v4/pkg/format" | ||
"github.com/bluenviron/mediamtx/internal/stream" | ||
"github.com/bluenviron/mediamtx/internal/unit" | ||
) | ||
|
||
// ToStream maps a RTMP stream to a MediaMTX stream. | ||
func ToStream(r *Reader, stream **stream.Stream) ([]*description.Media, error) { | ||
videoFormat, audioFormat := r.Tracks() | ||
|
||
var medias []*description.Media | ||
|
||
if videoFormat != nil { | ||
medi := &description.Media{ | ||
Type: description.MediaTypeVideo, | ||
Formats: []format.Format{videoFormat}, | ||
} | ||
medias = append(medias, medi) | ||
|
||
switch videoFormat.(type) { | ||
case *format.AV1: | ||
r.OnDataAV1(func(pts time.Duration, tu [][]byte) { | ||
(*stream).WriteUnit(medi, videoFormat, &unit.AV1{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
TU: tu, | ||
}) | ||
}) | ||
|
||
case *format.VP9: | ||
r.OnDataVP9(func(pts time.Duration, frame []byte) { | ||
(*stream).WriteUnit(medi, videoFormat, &unit.VP9{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
Frame: frame, | ||
}) | ||
}) | ||
|
||
case *format.H265: | ||
r.OnDataH265(func(pts time.Duration, au [][]byte) { | ||
(*stream).WriteUnit(medi, videoFormat, &unit.H265{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
AU: au, | ||
}) | ||
}) | ||
|
||
case *format.H264: | ||
r.OnDataH264(func(pts time.Duration, au [][]byte) { | ||
(*stream).WriteUnit(medi, videoFormat, &unit.H264{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
AU: au, | ||
}) | ||
}) | ||
|
||
default: | ||
return nil, fmt.Errorf("unsupported video codec: %T", videoFormat) | ||
} | ||
} | ||
|
||
if audioFormat != nil { | ||
medi := &description.Media{ | ||
Type: description.MediaTypeAudio, | ||
Formats: []format.Format{audioFormat}, | ||
} | ||
medias = append(medias, medi) | ||
|
||
switch audioFormat.(type) { | ||
case *format.MPEG4Audio: | ||
r.OnDataMPEG4Audio(func(pts time.Duration, au []byte) { | ||
(*stream).WriteUnit(medi, audioFormat, &unit.MPEG4Audio{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
AUs: [][]byte{au}, | ||
}) | ||
}) | ||
|
||
case *format.MPEG1Audio: | ||
r.OnDataMPEG1Audio(func(pts time.Duration, frame []byte) { | ||
(*stream).WriteUnit(medi, audioFormat, &unit.MPEG1Audio{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
Frames: [][]byte{frame}, | ||
}) | ||
}) | ||
|
||
case *format.G711: | ||
r.OnDataG711(func(pts time.Duration, samples []byte) { | ||
(*stream).WriteUnit(medi, audioFormat, &unit.G711{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
Samples: samples, | ||
}) | ||
}) | ||
|
||
case *format.LPCM: | ||
r.OnDataLPCM(func(pts time.Duration, samples []byte) { | ||
(*stream).WriteUnit(medi, audioFormat, &unit.LPCM{ | ||
Base: unit.Base{ | ||
NTP: time.Now(), | ||
PTS: pts, | ||
}, | ||
Samples: samples, | ||
}) | ||
}) | ||
|
||
default: | ||
return nil, fmt.Errorf("unsupported audio codec: %T", audioFormat) | ||
} | ||
} | ||
|
||
return medias, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.