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-08-26 16:54:28 +00:00
|
|
|
"sync"
|
2023-05-14 12:18:03 +00:00
|
|
|
"time"
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
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 {
|
2023-08-26 16:54:28 +00:00
|
|
|
desc *description.Session
|
2022-12-13 19:54:17 +00:00
|
|
|
bytesReceived *uint64
|
2023-05-04 18:16:41 +00:00
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
smedias map[*description.Media]*streamMedia
|
|
|
|
mutex sync.RWMutex
|
|
|
|
rtspStream *gortsplib.ServerStream
|
|
|
|
rtspsStream *gortsplib.ServerStream
|
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,
|
2023-08-26 16:54:28 +00:00
|
|
|
desc *description.Session,
|
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,
|
2023-08-26 16:54:28 +00:00
|
|
|
desc: desc,
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
2022-08-14 11:01:06 +00:00
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
s.smedias = make(map[*description.Media]*streamMedia)
|
2022-08-14 11:01:06 +00:00
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
for _, media := range desc.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() {
|
2023-08-26 16:54:28 +00:00
|
|
|
if s.rtspStream != nil {
|
|
|
|
s.rtspStream.Close()
|
|
|
|
}
|
|
|
|
if s.rtspsStream != nil {
|
|
|
|
s.rtspsStream.Close()
|
|
|
|
}
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
// Desc returns description of the stream.
|
|
|
|
func (s *Stream) Desc() *description.Session {
|
|
|
|
return s.desc
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// RTSPStream returns the RTSP stream.
|
2023-08-26 16:54:28 +00:00
|
|
|
func (s *Stream) RTSPStream(server *gortsplib.Server) *gortsplib.ServerStream {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
if s.rtspStream == nil {
|
|
|
|
s.rtspStream = gortsplib.NewServerStream(server, s.desc)
|
|
|
|
}
|
2023-07-30 20:34:35 +00:00
|
|
|
return s.rtspStream
|
|
|
|
}
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
// RTSPSStream returns the RTSPS stream.
|
|
|
|
func (s *Stream) RTSPSStream(server *gortsplib.Server) *gortsplib.ServerStream {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
if s.rtspsStream == nil {
|
|
|
|
s.rtspsStream = gortsplib.NewServerStream(server, s.desc)
|
|
|
|
}
|
|
|
|
return s.rtspsStream
|
|
|
|
}
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
// AddReader adds a reader.
|
2023-08-26 16:54:28 +00:00
|
|
|
func (s *Stream) AddReader(r interface{}, medi *description.Media, forma format.Format, cb func(unit.Unit)) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
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{}) {
|
2023-08-26 16:54:28 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
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-26 16:54:28 +00:00
|
|
|
func (s *Stream) WriteUnit(medi *description.Media, forma format.Format, data unit.Unit) {
|
2022-12-13 19:54:17 +00:00
|
|
|
sm := s.smedias[medi]
|
|
|
|
sf := sm.formats[forma]
|
2023-08-26 16:54:28 +00:00
|
|
|
|
|
|
|
s.mutex.RLock()
|
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
|
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.
|
2023-08-26 16:54:28 +00:00
|
|
|
func (s *Stream) WriteRTPPacket(
|
|
|
|
medi *description.Media,
|
|
|
|
forma format.Format,
|
|
|
|
pkt *rtp.Packet,
|
|
|
|
ntp time.Time,
|
|
|
|
pts time.Duration,
|
|
|
|
) {
|
2023-05-14 12:18:03 +00:00
|
|
|
sm := s.smedias[medi]
|
|
|
|
sf := sm.formats[forma]
|
2023-08-26 16:54:28 +00:00
|
|
|
|
|
|
|
s.mutex.RLock()
|
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
|
|
|
|
sf.writeRTPPacket(s, medi, pkt, ntp, pts)
|
2023-05-14 12:18:03 +00:00
|
|
|
}
|