11 changed files with 441 additions and 496 deletions
@ -0,0 +1,198 @@ |
|||||||
|
package core |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v4/pkg/description" |
||||||
|
"github.com/bluenviron/gortsplib/v4/pkg/format" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/formats/mpegts" |
||||||
|
|
||||||
|
"github.com/bluenviron/mediamtx/internal/stream" |
||||||
|
"github.com/bluenviron/mediamtx/internal/unit" |
||||||
|
) |
||||||
|
|
||||||
|
func mpegtsSetupTracks(r *mpegts.Reader, stream **stream.Stream) ([]*description.Media, error) { |
||||||
|
var medias []*description.Media //nolint:prealloc
|
||||||
|
|
||||||
|
var td *mpegts.TimeDecoder |
||||||
|
decodeTime := func(t int64) time.Duration { |
||||||
|
if td == nil { |
||||||
|
td = mpegts.NewTimeDecoder(t) |
||||||
|
} |
||||||
|
return td.Decode(t) |
||||||
|
} |
||||||
|
|
||||||
|
for _, track := range r.Tracks() { //nolint:dupl
|
||||||
|
var medi *description.Media |
||||||
|
|
||||||
|
switch codec := track.Codec.(type) { |
||||||
|
case *mpegts.CodecH265: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeVideo, |
||||||
|
Formats: []format.Format{&format.H265{ |
||||||
|
PayloadTyp: 96, |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataH26x(track, func(pts int64, _ int64, au [][]byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.H265{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
AU: au, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecH264: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeVideo, |
||||||
|
Formats: []format.Format{&format.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
PacketizationMode: 1, |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataH26x(track, func(pts int64, _ int64, au [][]byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.H264{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
AU: au, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecMPEG4Video: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeVideo, |
||||||
|
Formats: []format.Format{&format.MPEG4Video{ |
||||||
|
PayloadTyp: 96, |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataMPEGxVideo(track, func(pts int64, frame []byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.MPEG4Video{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
Frame: frame, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecMPEG1Video: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeVideo, |
||||||
|
Formats: []format.Format{&format.MPEG1Video{}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataMPEGxVideo(track, func(pts int64, frame []byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.MPEG1Video{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
Frame: frame, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecOpus: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeAudio, |
||||||
|
Formats: []format.Format{&format.Opus{ |
||||||
|
PayloadTyp: 96, |
||||||
|
IsStereo: (codec.ChannelCount == 2), |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataOpus(track, func(pts int64, packets [][]byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.Opus{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
Packets: packets, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecMPEG4Audio: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeAudio, |
||||||
|
Formats: []format.Format{&format.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
Config: &codec.Config, |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataMPEG4Audio(track, func(pts int64, aus [][]byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.MPEG4AudioGeneric{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
AUs: aus, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecMPEG1Audio: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeAudio, |
||||||
|
Formats: []format.Format{&format.MPEG1Audio{}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataMPEG1Audio(track, func(pts int64, frames [][]byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.MPEG1Audio{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
Frames: frames, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
case *mpegts.CodecAC3: |
||||||
|
medi = &description.Media{ |
||||||
|
Type: description.MediaTypeAudio, |
||||||
|
Formats: []format.Format{&format.AC3{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SampleRate: codec.SampleRate, |
||||||
|
ChannelCount: codec.ChannelCount, |
||||||
|
}}, |
||||||
|
} |
||||||
|
|
||||||
|
r.OnDataAC3(track, func(pts int64, frame []byte) error { |
||||||
|
(*stream).WriteUnit(medi, medi.Formats[0], &unit.AC3{ |
||||||
|
Base: unit.Base{ |
||||||
|
NTP: time.Now(), |
||||||
|
PTS: decodeTime(pts), |
||||||
|
}, |
||||||
|
Frames: [][]byte{frame}, |
||||||
|
}) |
||||||
|
return nil |
||||||
|
}) |
||||||
|
|
||||||
|
default: |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
medias = append(medias, medi) |
||||||
|
} |
||||||
|
|
||||||
|
if len(medias) == 0 { |
||||||
|
return nil, fmt.Errorf("no supported tracks found") |
||||||
|
} |
||||||
|
|
||||||
|
return medias, nil |
||||||
|
} |
||||||
@ -0,0 +1,112 @@ |
|||||||
|
package formatprocessor |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v4/pkg/format" |
||||||
|
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpac3" |
||||||
|
"github.com/pion/rtp" |
||||||
|
|
||||||
|
"github.com/bluenviron/mediamtx/internal/unit" |
||||||
|
) |
||||||
|
|
||||||
|
type formatProcessorAC3 struct { |
||||||
|
udpMaxPayloadSize int |
||||||
|
format *format.AC3 |
||||||
|
encoder *rtpac3.Encoder |
||||||
|
decoder *rtpac3.Decoder |
||||||
|
} |
||||||
|
|
||||||
|
func newAC3( |
||||||
|
udpMaxPayloadSize int, |
||||||
|
forma *format.AC3, |
||||||
|
generateRTPPackets bool, |
||||||
|
) (*formatProcessorAC3, error) { |
||||||
|
t := &formatProcessorAC3{ |
||||||
|
udpMaxPayloadSize: udpMaxPayloadSize, |
||||||
|
format: forma, |
||||||
|
} |
||||||
|
|
||||||
|
if generateRTPPackets { |
||||||
|
err := t.createEncoder() |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return t, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAC3) createEncoder() error { |
||||||
|
t.encoder = &rtpac3.Encoder{ |
||||||
|
PayloadType: t.format.PayloadTyp, |
||||||
|
} |
||||||
|
return t.encoder.Init() |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAC3) ProcessUnit(uu unit.Unit) error { //nolint:dupl
|
||||||
|
u := uu.(*unit.AC3) |
||||||
|
|
||||||
|
pkts, err := t.encoder.Encode(u.Frames) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
ts := uint32(multiplyAndDivide(u.PTS, time.Duration(t.format.ClockRate()), time.Second)) |
||||||
|
for _, pkt := range pkts { |
||||||
|
pkt.Timestamp = ts |
||||||
|
} |
||||||
|
|
||||||
|
u.RTPPackets = pkts |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAC3) ProcessRTPPacket( //nolint:dupl
|
||||||
|
pkt *rtp.Packet, |
||||||
|
ntp time.Time, |
||||||
|
pts time.Duration, |
||||||
|
hasNonRTSPReaders bool, |
||||||
|
) (Unit, error) { |
||||||
|
u := &unit.AC3{ |
||||||
|
Base: unit.Base{ |
||||||
|
RTPPackets: []*rtp.Packet{pkt}, |
||||||
|
NTP: ntp, |
||||||
|
PTS: pts, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
// remove padding
|
||||||
|
pkt.Header.Padding = false |
||||||
|
pkt.PaddingSize = 0 |
||||||
|
|
||||||
|
if pkt.MarshalSize() > t.udpMaxPayloadSize { |
||||||
|
return nil, fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)", |
||||||
|
pkt.MarshalSize(), t.udpMaxPayloadSize) |
||||||
|
} |
||||||
|
|
||||||
|
// decode from RTP
|
||||||
|
if hasNonRTSPReaders || t.decoder != nil { |
||||||
|
if t.decoder == nil { |
||||||
|
var err error |
||||||
|
t.decoder, err = t.format.CreateDecoder() |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
frames, err := t.decoder.Decode(pkt) |
||||||
|
if err != nil { |
||||||
|
if err == rtpac3.ErrNonStartingPacketAndNoPrevious || err == rtpac3.ErrMorePacketsNeeded { |
||||||
|
return u, nil |
||||||
|
} |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
u.Frames = frames |
||||||
|
} |
||||||
|
|
||||||
|
// route packet as is
|
||||||
|
return u, nil |
||||||
|
} |
||||||
Loading…
Reference in new issue