mediamtx/internal/streamproc/streamproc.go

67 lines
1.6 KiB
Go
Raw Normal View History

package streamproc
import (
2021-03-23 19:37:43 +00:00
"time"
"github.com/aler9/gortsplib"
"github.com/pion/rtp"
)
2021-03-23 19:37:43 +00:00
// TrackStartingPoint is the starting point of a track.
type TrackStartingPoint struct {
Filled bool // used to avoid mutexes
RTPTime uint32
NTPTime time.Time
}
// Path is implemented by path.path.
type Path interface {
2021-03-23 19:37:43 +00:00
OnSPSetStartingPoint(SetStartingPointReq)
OnSPFrame(int, gortsplib.StreamType, []byte)
}
// SetStartingPointReq is a set starting point request.
type SetStartingPointReq struct {
SP *StreamProc
TrackID int
StartingPoint TrackStartingPoint
}
// StreamProc is a stream processor, an intermediate layer between a source and a path.
type StreamProc struct {
path Path
2021-03-23 19:37:43 +00:00
startingPoints []TrackStartingPoint
}
// New allocates a StreamProc.
2021-03-23 19:37:43 +00:00
func New(path Path, tracksLen int) *StreamProc {
return &StreamProc{
path: path,
2021-03-23 19:37:43 +00:00
startingPoints: make([]TrackStartingPoint, tracksLen),
}
}
// OnFrame processes a frame.
func (sp *StreamProc) OnFrame(trackID int, streamType gortsplib.StreamType, payload []byte) {
if streamType == gortsplib.StreamTypeRTP &&
!sp.startingPoints[trackID].Filled {
pkt := rtp.Packet{}
err := pkt.Unmarshal(payload)
if err != nil {
return
}
sp.startingPoints[trackID].Filled = true
2021-03-23 19:37:43 +00:00
sp.startingPoints[trackID].RTPTime = pkt.Timestamp
sp.startingPoints[trackID].NTPTime = time.Now()
2021-03-23 19:37:43 +00:00
sp.path.OnSPSetStartingPoint(SetStartingPointReq{
SP: sp,
TrackID: trackID,
StartingPoint: sp.startingPoints[trackID],
})
}
2021-03-23 19:37:43 +00:00
sp.path.OnSPFrame(trackID, streamType, payload)
}