Files
mediamtx/internal/stream/stream_format.go
T
Alessandro RosandGitHub a56408db19 add always available streams (#5335)
When the publisher or source of a stream is offline, the server can be
configured to fill gaps in the stream with a video that is played on
repeat until a publisher comes back online. This allows readers to stay
connected regardless of the state of the stream. The offline video and
any future online stream are concatenated without decoding or
re-encoding packets, using the original codec.
2026-01-31 14:44:58 +01:00

80 lines
1.8 KiB
Go

package stream
import (
"crypto/rand"
"time"
"github.com/bluenviron/gortsplib/v5/pkg/description"
"github.com/bluenviron/gortsplib/v5/pkg/format"
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/errordumper"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/ntpestimator"
"github.com/bluenviron/mediamtx/internal/unit"
)
func multiplyAndDivide(v, m, d int64) int64 {
secs := v / d
dec := v % d
return (secs*m + dec*m/d)
}
func unitSize(u *unit.Unit) uint64 {
n := uint64(0)
for _, pkt := range u.RTPPackets {
n += uint64(pkt.MarshalSize())
}
return n
}
func randUint32() (uint32, error) {
var b [4]byte
_, err := rand.Read(b[:])
if err != nil {
return 0, err
}
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
}
type streamFormat struct {
format format.Format
media *description.Media
alwaysAvailable bool
rtpMaxPayloadSize int
replaceNTP bool
processingErrors *errordumper.Dumper
onBytesReceived func(uint64)
onBytesSent func(uint64)
writeRTSP func(*description.Media, []*rtp.Packet, time.Time)
parent logger.Writer
firstReceived bool
lastPTS int64
lastSystemTime time.Time
ptsOffset int64
formatUpdater formatUpdater
unitRemuxer unitRemuxer
rtpEncoder rtpEncoder
rtpTimeOffset uint32
ntpEstimator *ntpestimator.Estimator
onDatas map[*Reader]OnDataFunc
}
func (sf *streamFormat) initialize() error {
sf.lastSystemTime = time.Now()
sf.formatUpdater = newFormatUpdater(sf.format)
sf.unitRemuxer = newUnitRemuxer(sf.format)
if sf.replaceNTP {
sf.ntpEstimator = &ntpestimator.Estimator{
ClockRate: sf.format.ClockRate(),
}
}
sf.onDatas = make(map[*Reader]OnDataFunc)
return nil
}