-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support publishing AV1 tracks with Enhanced RTMP
- Loading branch information
Showing
14 changed files
with
262 additions
and
39 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
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,133 @@ | ||
package formatprocessor | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bluenviron/gortsplib/v3/pkg/formats" | ||
"github.com/bluenviron/gortsplib/v3/pkg/formats/rtpav1" | ||
"github.com/bluenviron/mediacommon/pkg/codecs/av1" | ||
"github.com/pion/rtp" | ||
|
||
"github.com/aler9/mediamtx/internal/logger" | ||
) | ||
|
||
// UnitAV1 is an AV1 data unit. | ||
type UnitAV1 struct { | ||
RTPPackets []*rtp.Packet | ||
NTP time.Time | ||
PTS time.Duration | ||
OBUs [][]byte | ||
} | ||
|
||
// GetRTPPackets implements Unit. | ||
func (d *UnitAV1) GetRTPPackets() []*rtp.Packet { | ||
return d.RTPPackets | ||
} | ||
|
||
// GetNTP implements Unit. | ||
func (d *UnitAV1) GetNTP() time.Time { | ||
return d.NTP | ||
} | ||
|
||
type formatProcessorAV1 struct { | ||
udpMaxPayloadSize int | ||
format *formats.AV1 | ||
log logger.Writer | ||
|
||
encoder *rtpav1.Encoder | ||
decoder *rtpav1.Decoder | ||
lastKeyFrameReceived time.Time | ||
} | ||
|
||
func newAV1( | ||
udpMaxPayloadSize int, | ||
forma *formats.AV1, | ||
generateRTPPackets bool, | ||
log logger.Writer, | ||
) (*formatProcessorAV1, error) { | ||
t := &formatProcessorAV1{ | ||
udpMaxPayloadSize: udpMaxPayloadSize, | ||
format: forma, | ||
log: log, | ||
} | ||
|
||
if generateRTPPackets { | ||
t.encoder = &rtpav1.Encoder{ | ||
PayloadMaxSize: t.udpMaxPayloadSize - 12, | ||
} | ||
t.encoder.Init() | ||
t.lastKeyFrameReceived = time.Now() | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (t *formatProcessorAV1) checkKeyFrameInterval(containsKeyFrame bool) { | ||
if containsKeyFrame { | ||
t.lastKeyFrameReceived = time.Now() | ||
} else { | ||
now := time.Now() | ||
if now.Sub(t.lastKeyFrameReceived) >= maxKeyFrameInterval { | ||
t.lastKeyFrameReceived = now | ||
t.log.Log(logger.Warn, "no AV1 key frames received in %v, stream can't be decoded", maxKeyFrameInterval) | ||
} | ||
} | ||
} | ||
|
||
func (t *formatProcessorAV1) checkOBUs(obus [][]byte) { | ||
containsKeyFrame, _ := av1.ContainsKeyFrame(obus) | ||
t.checkKeyFrameInterval(containsKeyFrame) | ||
} | ||
|
||
func (t *formatProcessorAV1) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl | ||
tunit := unit.(*UnitAV1) | ||
|
||
if tunit.RTPPackets != nil { | ||
pkt := tunit.RTPPackets[0] | ||
|
||
// remove padding | ||
pkt.Header.Padding = false | ||
pkt.PaddingSize = 0 | ||
|
||
if pkt.MarshalSize() > t.udpMaxPayloadSize { | ||
return fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)", | ||
pkt.MarshalSize(), t.udpMaxPayloadSize) | ||
} | ||
|
||
// decode from RTP | ||
if hasNonRTSPReaders { | ||
if t.decoder == nil { | ||
t.decoder = t.format.CreateDecoder() | ||
t.lastKeyFrameReceived = time.Now() | ||
} | ||
|
||
// DecodeUntilMarker() is necessary, otherwise Encode() generates partial groups | ||
obus, pts, err := t.decoder.DecodeUntilMarker(pkt) | ||
if err != nil { | ||
if err == rtpav1.ErrNonStartingPacketAndNoPrevious || err == rtpav1.ErrMorePacketsNeeded { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
tunit.OBUs = obus | ||
t.checkOBUs(obus) | ||
tunit.PTS = pts | ||
} | ||
|
||
// route packet as is | ||
return nil | ||
} | ||
|
||
t.checkOBUs(tunit.OBUs) | ||
|
||
// encode into RTP | ||
pkts, err := t.encoder.Encode(tunit.OBUs, tunit.PTS) | ||
if err != nil { | ||
return err | ||
} | ||
tunit.RTPPackets = pkts | ||
|
||
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
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,37 @@ | ||
package tracks | ||
|
||
import ( | ||
gomp4 "github.com/abema/go-mp4" | ||
) | ||
|
||
// BoxTypeAv1C returns the box type. | ||
func BoxTypeAv1C() gomp4.BoxType { return gomp4.StrToBoxType("av1C") } | ||
|
||
func init() { //nolint:gochecknoinits | ||
gomp4.AddBoxDef(&Av1C{}) | ||
} | ||
|
||
// Av1C is a Av1C ISO-BMFF box. | ||
type Av1C struct { | ||
gomp4.Box | ||
Marker uint8 `mp4:"0,size=1,const=1"` | ||
Version uint8 `mp4:"1,size=7,const=1"` | ||
SeqProfile uint8 `mp4:"2,size=3"` | ||
SeqLevelIdx0 uint8 `mp4:"3,size=5"` | ||
SeqTier0 uint8 `mp4:"4,size=1"` | ||
HighBitdepth uint8 `mp4:"5,size=1"` | ||
TwelveBit uint8 `mp4:"6,size=1"` | ||
Monochrome uint8 `mp4:"7,size=1"` | ||
ChromaSubsamplingX uint8 `mp4:"8,size=1"` | ||
ChromaSubsamplingY uint8 `mp4:"9,size=1"` | ||
ChromaSamplePosition uint8 `mp4:"10,size=2"` | ||
Reserved uint8 `mp4:"11,size=3,const=0"` | ||
InitialPresentationDelayPresent uint8 `mp4:"12,size=1"` | ||
InitialPresentationDelayMinusOne uint8 `mp4:"13,size=4"` | ||
ConfigOBUs []uint8 `mp4:"14,size=8"` | ||
} | ||
|
||
// GetType returns the box type. | ||
func (Av1C) GetType() gomp4.BoxType { | ||
return BoxTypeAv1C() | ||
} |
Oops, something went wrong.