From aa876bcd0db547bdd80d6f847f4beeedc03b7c4c Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 3 Sep 2020 15:29:18 +0200 Subject: [PATCH] Propagate CNAME. --- conn.go | 1 + disk.go | 3 ++ rtpconn.go | 94 +++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/conn.go b/conn.go index 393f91b..fe46c30 100644 --- a/conn.go +++ b/conn.go @@ -41,4 +41,5 @@ type downTrack interface { WriteRTP(packat *rtp.Packet) error Accumulate(bytes uint32) setTimeOffset(ntp uint64, rtp uint32) + setCname(string) } diff --git a/disk.go b/disk.go index b33f0fa..0095959 100644 --- a/disk.go +++ b/disk.go @@ -216,6 +216,9 @@ func newDiskConn(directory, label string, up upConnection, remoteTracks []upTrac func (t *diskTrack) setTimeOffset(ntp uint64, rtp uint32) { } +func (t *diskTrack) setCname(string) { +} + func clonePacket(packet *rtp.Packet) *rtp.Packet { buf, err := packet.Marshal() if err != nil { diff --git a/rtpconn.go b/rtpconn.go index b142c6d..0ab5061 100644 --- a/rtpconn.go +++ b/rtpconn.go @@ -79,6 +79,7 @@ type rtpDownTrack struct { srNTPTime uint64 remoteNTPTime uint64 remoteRTPTime uint32 + cname atomic.Value rtt uint64 } @@ -95,6 +96,10 @@ func (down *rtpDownTrack) setTimeOffset(ntp uint64, rtp uint32) { atomic.StoreUint32(&down.remoteRTPTime, rtp) } +func (down *rtpDownTrack) setCname(cname string) { + down.cname.Store(cname) +} + type rtpDownConnection struct { id string pc *webrtc.PeerConnection @@ -187,6 +192,7 @@ type rtpUpTrack struct { writerDone chan struct{} mu sync.Mutex + cname string local []downTrack srTime uint64 srNTPTime uint64 @@ -362,7 +368,7 @@ func getTrackMid(pc *webrtc.PeerConnection, track *webrtc.Track) string { // called locked func (up *rtpUpConnection) complete() bool { - for mid, _ := range up.labels { + for mid := range up.labels { found := false for _, t := range up.tracks { m := getTrackMid(up.pc, t.track) @@ -566,10 +572,14 @@ func writeLoop(conn *rtpUpConnection, track *rtpUpTrack, ch <-chan packetIndex) track.mu.Lock() ntp := track.srNTPTime rtp := track.srRTPTime + cname := track.cname track.mu.Unlock() if ntp != 0 { action.track.setTimeOffset(ntp, rtp) } + if cname != "" { + action.track.setCname(cname) + } } else { found := false for i, t := range local { @@ -691,7 +701,7 @@ func sendFIR(pc *webrtc.PeerConnection, ssrc uint32, seqno uint8) error { return pc.WriteRTCP([]rtcp.Packet{ &rtcp.FullIntraRequest{ FIR: []rtcp.FIREntry{ - rtcp.FIREntry{ + { SSRC: ssrc, SequenceNumber: seqno, }, @@ -716,7 +726,7 @@ func sendNACK(pc *webrtc.PeerConnection, ssrc uint32, first uint16, bitmap uint1 &rtcp.TransportLayerNack{ MediaSSRC: ssrc, Nacks: []rtcp.NackPair{ - rtcp.NackPair{ + { first, rtcp.PacketBitmap(bitmap), }, @@ -763,6 +773,7 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei now := rtptime.Jiffies() for _, p := range ps { + local := track.getLocal() switch p := p.(type) { case *rtcp.SenderReport: track.mu.Lock() @@ -773,11 +784,26 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei track.srNTPTime = p.NTPTime track.srRTPTime = p.RTPTime track.mu.Unlock() - local := track.getLocal() for _, l := range local { l.setTimeOffset(p.NTPTime, p.RTPTime) } case *rtcp.SourceDescription: + for _, c := range p.Chunks { + if c.Source != track.track.SSRC() { + continue + } + for _, i := range c.Items { + if i.Type != rtcp.SDESCNAME { + continue + } + track.mu.Lock() + track.cname = i.Text + track.mu.Unlock() + for _, l := range local { + l.setCname(i.Text) + } + } + } } } @@ -910,30 +936,46 @@ func sendSR(conn *rtpDownConnection) error { remoteNTP := atomic.LoadUint64(&t.remoteNTPTime) remoteRTP := atomic.LoadUint32(&t.remoteRTPTime) - if remoteNTP == 0 { - // we never got a remote SR for this track - continue - } - srTime := rtptime.NTPToTime(remoteNTP) - d := now.Sub(srTime) - if d > 0 && d < time.Hour { - delay := rtptime.FromDuration( - d, clockrate, - ) - nowRTP = remoteRTP + uint32(delay) + if remoteNTP != 0 { + srTime := rtptime.NTPToTime(remoteNTP) + d := now.Sub(srTime) + if d > 0 && d < time.Hour { + delay := rtptime.FromDuration( + d, clockrate, + ) + nowRTP = remoteRTP + uint32(delay) + } + + p, b := t.rate.Totals() + packets = append(packets, + &rtcp.SenderReport{ + SSRC: t.track.SSRC(), + NTPTime: nowNTP, + RTPTime: nowRTP, + PacketCount: p, + OctetCount: b, + }) + atomic.StoreUint64(&t.srTime, jiffies) + atomic.StoreUint64(&t.srNTPTime, nowNTP) } - p, b := t.rate.Totals() - packets = append(packets, - &rtcp.SenderReport{ - SSRC: t.track.SSRC(), - NTPTime: nowNTP, - RTPTime: nowRTP, - PacketCount: p, - OctetCount: b, - }) - atomic.StoreUint64(&t.srTime, jiffies) - atomic.StoreUint64(&t.srNTPTime, nowNTP) + cname, ok := t.cname.Load().(string) + if ok { + item := rtcp.SourceDescriptionItem{ + Type: rtcp.SDESCNAME, + Text: cname, + } + packets = append(packets, + &rtcp.SourceDescription{ + Chunks: []rtcp.SourceDescriptionChunk{ + { + Source: t.track.SSRC(), + Items: []rtcp.SourceDescriptionItem{item}, + }, + }, + }, + ) + } } if len(packets) == 0 {