Commit 7665067a authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Rate-limit keyframe requests in disk writer.

We were requesting two keyframes in a row.
parent d94e5583
...@@ -259,8 +259,9 @@ type diskTrack struct { ...@@ -259,8 +259,9 @@ type diskTrack struct {
// bit 32 is a boolean indicating that the origin is valid // bit 32 is a boolean indicating that the origin is valid
origin uint64 origin uint64
lastKf uint32 kfRequested time.Time
savedKf *rtp.Packet lastKf time.Time
savedKf *rtp.Packet
} }
func newDiskConn(client *Client, directory string, up conn.Up, remoteTracks []conn.UpTrack) (*diskConn, error) { func newDiskConn(client *Client, directory string, up conn.Up, remoteTracks []conn.UpTrack) (*diskConn, error) {
...@@ -435,7 +436,6 @@ func keyframeDimensions(codec string, data []byte, packet *rtp.Packet) (uint32, ...@@ -435,7 +436,6 @@ func keyframeDimensions(codec string, data []byte, packet *rtp.Packet) (uint32,
} }
func (t *diskTrack) Write(buf []byte) (int, error) { func (t *diskTrack) Write(buf []byte) (int, error) {
// since we call initWriter, we take the connection lock for simplicity.
t.conn.mu.Lock() t.conn.mu.Lock()
defer t.conn.mu.Unlock() defer t.conn.mu.Unlock()
...@@ -471,17 +471,11 @@ func (t *diskTrack) Write(buf []byte) (int, error) { ...@@ -471,17 +471,11 @@ func (t *diskTrack) Write(buf []byte) (int, error) {
} }
} }
kfNeeded := false
t.builder.Push(p) t.builder.Push(p)
for { for {
sample, ts := t.builder.PopWithTimestamp() sample, ts := t.builder.PopWithTimestamp()
if sample == nil { if sample == nil {
if kfNeeded {
t.remote.RequestKeyframe()
return 0, nil
}
return len(buf), nil return len(buf), nil
} }
...@@ -503,13 +497,6 @@ func (t *diskTrack) Write(buf []byte) (int, error) { ...@@ -503,13 +497,6 @@ func (t *diskTrack) Write(buf []byte) (int, error) {
) )
return 0, err return 0, err
} }
t.lastKf = ts
} else if t.writer != nil {
// Request a keyframe every 4s
delta := ts - t.lastKf
if (delta&0x80000000) != 0 || delta > 4*90000 {
kfNeeded = true
}
} }
} else { } else {
if t.writer == nil { if t.writer == nil {
...@@ -526,13 +513,21 @@ func (t *diskTrack) Write(buf []byte) (int, error) { ...@@ -526,13 +513,21 @@ func (t *diskTrack) Write(buf []byte) (int, error) {
} }
} }
if t.writer == nil { now := time.Now()
if !keyframe { if keyframe {
t.lastKf = now
} else if t.writer == nil || now.Sub(t.lastKf) > 4*time.Second {
if now.Sub(t.kfRequested) > time.Second {
t.remote.RequestKeyframe() t.remote.RequestKeyframe()
t.kfRequested = now
} }
return 0, nil return 0, nil
} }
if t.writer == nil {
continue
}
if t.origin == 0 { if t.origin == 0 {
t.origin = uint64(ts) | (1 << 32) t.origin = uint64(ts) | (1 << 32)
} }
......
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