Skip to content

Commit

Permalink
move media-related logics into serverStreamMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 19, 2022
1 parent e1c07a1 commit b3de3cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
27 changes: 2 additions & 25 deletions serverstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewServerStream(medias media.Medias) *ServerStream {

st.streamMedias = make(map[*media.Media]*serverStreamMedia, len(medias))
for _, media := range medias {
ssm := &serverStreamMedia{}
ssm := newServerStreamMedia(media)

ssm.formats = make(map[uint8]*serverStreamFormat)
for _, forma := range media.Formats {
Expand Down Expand Up @@ -279,13 +279,6 @@ func (st *ServerStream) WritePacketRTP(medi *media.Media, pkt *rtp.Packet) {
// ntp is the absolute time of the packet, and is needed to generate RTCP sender reports
// that allows the receiver to reconstruct the absolute time of the packet.
func (st *ServerStream) WritePacketRTPWithNTP(medi *media.Media, pkt *rtp.Packet, ntp time.Time) {
byts := make([]byte, maxPacketSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return
}
byts = byts[:n]

st.mutex.RLock()
defer st.mutex.RUnlock()

Expand All @@ -294,23 +287,7 @@ func (st *ServerStream) WritePacketRTPWithNTP(medi *media.Media, pkt *rtp.Packet
}

sm := st.streamMedias[medi]

forma := sm.formats[pkt.PayloadType]

forma.rtcpSender.ProcessPacket(pkt, ntp, forma.format.PTSEqualsDTS(pkt))

// send unicast
for r := range st.activeUnicastReaders {
sm, ok := r.setuppedMedias[medi]
if ok {
sm.writePacketRTP(byts)
}
}

// send multicast
if sm.multicastHandler != nil {
sm.multicastHandler.writePacketRTP(byts)
}
sm.WritePacketRTPWithNTP(st, pkt, ntp)
}

// WritePacketRTCP writes a RTCP packet to all the readers of the stream.
Expand Down
41 changes: 41 additions & 0 deletions serverstreammedia.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package gortsplib

import (
"time"

"github.com/pion/rtp"

"github.com/aler9/gortsplib/v2/pkg/media"
)

type serverStreamMedia struct {
media *media.Media
formats map[uint8]*serverStreamFormat
multicastHandler *serverMulticastHandler
}

func newServerStreamMedia(medi *media.Media) *serverStreamMedia {
return &serverStreamMedia{
media: medi,
}
}

func (sm *serverStreamMedia) close() {
for _, tr := range sm.formats {
if tr.rtcpSender != nil {
Expand All @@ -28,3 +43,29 @@ func (sm *serverStreamMedia) allocateMulticastHandler(s *Server) error {
}
return nil
}

func (sm *serverStreamMedia) WritePacketRTPWithNTP(ss *ServerStream, pkt *rtp.Packet, ntp time.Time) {
byts := make([]byte, maxPacketSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return
}
byts = byts[:n]

forma := sm.formats[pkt.PayloadType]

forma.rtcpSender.ProcessPacket(pkt, ntp, forma.format.PTSEqualsDTS(pkt))

// send unicast
for r := range ss.activeUnicastReaders {
sm, ok := r.setuppedMedias[sm.media]
if ok {
sm.writePacketRTP(byts)
}
}

// send multicast
if sm.multicastHandler != nil {
sm.multicastHandler.writePacketRTP(byts)
}
}

0 comments on commit b3de3cf

Please sign in to comment.