2023-07-30 20:34:35 +00:00
|
|
|
// Package stream contains the Stream object.
|
|
|
|
package stream
|
2021-08-10 16:34:10 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-14 12:18:03 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/media"
|
2023-05-14 12:18:03 +00:00
|
|
|
"github.com/pion/rtp"
|
2023-01-05 11:54:00 +00:00
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2023-08-25 16:11:02 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
2021-08-10 16:34:10 +00:00
|
|
|
)
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// Stream is a media stream.
|
|
|
|
// It stores tracks, readers and allow to write data to readers.
|
|
|
|
type Stream struct {
|
2022-12-13 19:54:17 +00:00
|
|
|
bytesReceived *uint64
|
2023-05-04 18:16:41 +00:00
|
|
|
|
|
|
|
rtspStream *gortsplib.ServerStream
|
|
|
|
smedias map[*media.Media]*streamMedia
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// New allocates a Stream.
|
|
|
|
func New(
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int,
|
2022-12-13 19:54:17 +00:00
|
|
|
medias media.Medias,
|
2022-11-11 10:59:52 +00:00
|
|
|
generateRTPPackets bool,
|
|
|
|
bytesReceived *uint64,
|
2023-07-30 20:34:35 +00:00
|
|
|
source logger.Writer,
|
|
|
|
) (*Stream, error) {
|
|
|
|
s := &Stream{
|
2022-12-13 19:54:17 +00:00
|
|
|
bytesReceived: bytesReceived,
|
|
|
|
rtspStream: gortsplib.NewServerStream(medias),
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
2022-08-14 11:01:06 +00:00
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
s.smedias = make(map[*media.Media]*streamMedia)
|
2022-08-14 11:01:06 +00:00
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
for _, media := range s.rtspStream.Medias() {
|
2022-08-14 11:01:06 +00:00
|
|
|
var err error
|
2023-05-04 18:16:41 +00:00
|
|
|
s.smedias[media], err = newStreamMedia(udpMaxPayloadSize, media, generateRTPPackets, source)
|
2022-08-14 11:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// Close closes all resources of the stream.
|
|
|
|
func (s *Stream) Close() {
|
2021-08-10 16:34:10 +00:00
|
|
|
s.rtspStream.Close()
|
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// Medias returns medias of the stream.
|
|
|
|
func (s *Stream) Medias() media.Medias {
|
2022-12-13 19:54:17 +00:00
|
|
|
return s.rtspStream.Medias()
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// RTSPStream returns the RTSP stream.
|
|
|
|
func (s *Stream) RTSPStream() *gortsplib.ServerStream {
|
|
|
|
return s.rtspStream
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddReader adds a reader.
|
2023-08-25 16:11:02 +00:00
|
|
|
func (s *Stream) AddReader(r interface{}, medi *media.Media, forma formats.Format, cb func(unit.Unit)) {
|
2022-12-13 19:54:17 +00:00
|
|
|
sm := s.smedias[medi]
|
|
|
|
sf := sm.formats[forma]
|
2023-07-30 20:34:35 +00:00
|
|
|
sf.addReader(r, cb)
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// RemoveReader removes a reader.
|
|
|
|
func (s *Stream) RemoveReader(r interface{}) {
|
2022-12-13 19:54:17 +00:00
|
|
|
for _, sm := range s.smedias {
|
|
|
|
for _, sf := range sm.formats {
|
2023-07-30 20:34:35 +00:00
|
|
|
sf.removeReader(r)
|
2022-12-13 19:54:17 +00:00
|
|
|
}
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// WriteUnit writes a Unit.
|
2023-08-25 16:11:02 +00:00
|
|
|
func (s *Stream) WriteUnit(medi *media.Media, forma formats.Format, data unit.Unit) {
|
2022-12-13 19:54:17 +00:00
|
|
|
sm := s.smedias[medi]
|
|
|
|
sf := sm.formats[forma]
|
2023-05-04 18:16:41 +00:00
|
|
|
sf.writeUnit(s, medi, data)
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
2023-05-14 12:18:03 +00:00
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// WriteRTPPacket writes a RTP packet.
|
|
|
|
func (s *Stream) WriteRTPPacket(medi *media.Media, forma formats.Format, pkt *rtp.Packet, ntp time.Time) {
|
2023-05-14 12:18:03 +00:00
|
|
|
sm := s.smedias[medi]
|
|
|
|
sf := sm.formats[forma]
|
|
|
|
sf.writeRTPPacket(s, medi, pkt, ntp)
|
|
|
|
}
|