Commit f9edde65 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Maintain time offsets on the sender side.

parent 917fa33d
...@@ -41,4 +41,5 @@ type downTrack interface { ...@@ -41,4 +41,5 @@ type downTrack interface {
WriteRTP(packat *rtp.Packet) error WriteRTP(packat *rtp.Packet) error
Accumulate(bytes uint32) Accumulate(bytes uint32)
GetMaxBitrate(now uint64) uint64 GetMaxBitrate(now uint64) uint64
setTimeOffset(ntp uint64, rtp uint32)
} }
...@@ -204,6 +204,9 @@ func newDiskConn(directory, label string, up upConnection, remoteTracks []upTrac ...@@ -204,6 +204,9 @@ func newDiskConn(directory, label string, up upConnection, remoteTracks []upTrac
return &conn, nil return &conn, nil
} }
func (t *diskTrack) setTimeOffset(ntp uint64, rtp uint32) {
}
func clonePacket(packet *rtp.Packet) *rtp.Packet { func clonePacket(packet *rtp.Packet) *rtp.Packet {
buf, err := packet.Marshal() buf, err := packet.Marshal()
if err != nil { if err != nil {
......
...@@ -78,6 +78,8 @@ type rtpDownTrack struct { ...@@ -78,6 +78,8 @@ type rtpDownTrack struct {
stats *receiverStats stats *receiverStats
srTime uint64 srTime uint64
srNTPTime uint64 srNTPTime uint64
remoteNTPTime uint64
remoteRTPTime uint32
rtt uint64 rtt uint64
} }
...@@ -98,6 +100,11 @@ func (down *rtpDownTrack) GetMaxBitrate(now uint64) uint64 { ...@@ -98,6 +100,11 @@ func (down *rtpDownTrack) GetMaxBitrate(now uint64) uint64 {
return br2 return br2
} }
func (down *rtpDownTrack) setTimeOffset(ntp uint64, rtp uint32) {
atomic.StoreUint64(&down.remoteNTPTime, ntp)
atomic.StoreUint32(&down.remoteRTPTime, rtp)
}
type rtpDownConnection struct { type rtpDownConnection struct {
id string id string
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
...@@ -554,11 +561,19 @@ func writeLoop(conn *rtpUpConnection, track *rtpUpTrack, ch <-chan packetIndex) ...@@ -554,11 +561,19 @@ func writeLoop(conn *rtpUpConnection, track *rtpUpTrack, ch <-chan packetIndex)
if action.add { if action.add {
local = append(local, action.track) local = append(local, action.track)
firSent = false firSent = false
track.mu.Lock()
ntp := track.srNTPTime
rtp := track.srRTPTime
track.mu.Unlock()
if ntp != 0 {
action.track.setTimeOffset(ntp, rtp)
}
} else { } else {
found := false found := false
for i, t := range local { for i, t := range local {
if t == action.track { if t == action.track {
local = append(local[:i], local[i+1:]...) local = append(local[:i],
local[i+1:]...)
found = true found = true
break break
} }
...@@ -754,6 +769,10 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei ...@@ -754,6 +769,10 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei
track.srNTPTime = p.NTPTime track.srNTPTime = p.NTPTime
track.srRTPTime = p.RTPTime track.srRTPTime = p.RTPTime
track.mu.Unlock() track.mu.Unlock()
local := track.getLocal()
for _, l := range local {
l.setTimeOffset(p.NTPTime, p.RTPTime)
}
case *rtcp.SourceDescription: case *rtcp.SourceDescription:
} }
} }
...@@ -848,37 +867,22 @@ func sendSR(conn *rtpDownConnection) error { ...@@ -848,37 +867,22 @@ func sendSR(conn *rtpDownConnection) error {
for _, t := range conn.tracks { for _, t := range conn.tracks {
clockrate := t.track.Codec().ClockRate clockrate := t.track.Codec().ClockRate
remote := t.remote
var nowRTP uint32 var nowRTP uint32
switch r := remote.(type) { remoteNTP := atomic.LoadUint64(&t.remoteNTPTime)
case *rtpUpTrack: remoteRTP := atomic.LoadUint32(&t.remoteRTPTime)
r.mu.Lock() if remoteNTP == 0 {
lastTime := r.srTime // we never got a remote SR for this track
srNTPTime := r.srNTPTime continue
srRTPTime := r.srRTPTime }
r.mu.Unlock() srTime := rtptime.NTPToTime(remoteNTP)
if lastTime == 0 { d := now.Sub(srTime)
// we never got a remote SR, skip this track if d > 0 && d < time.Hour {
continue delay := rtptime.FromDuration(
} d, clockrate,
if srNTPTime != 0 { )
srTime := rtptime.NTPToTime(srNTPTime) nowRTP = remoteRTP + uint32(delay)
d := now.Sub(srTime)
if d > 0 && d < time.Hour {
delay := rtptime.FromDuration(
d, clockrate,
)
nowRTP = srRTPTime + uint32(delay)
}
}
default:
ts, ok := remote.getTimestamp()
if !ok {
continue
}
nowRTP = ts
} }
p, b := t.rate.Totals() p, b := t.rate.Totals()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment