Merged in rtp-encoder-buffer-fix (pull request #137)

stream/rtp/encoder.go: more sensible use of encoder buffer

Approved-by: kortschak <dan@kortschak.io>
This commit is contained in:
Saxon Milton 2019-02-09 11:07:24 +00:00
commit 073678f190
2 changed files with 21 additions and 5 deletions

View File

@ -40,7 +40,7 @@ const (
timestampFreq = 90000 // Hz
mtsSize = 188
bufferSize = 1000
sendLen = 7 * 188
sendSize = 7 * 188
)
// Encoder implements io writer and provides functionality to wrap data into
@ -72,12 +72,28 @@ func NewEncoder(dst io.Writer, fps int) *Encoder {
// so that multiple layers of packetization can occur.
func (e *Encoder) Write(data []byte) (int, error) {
e.buffer = append(e.buffer, data...)
for len(e.buffer) >= sendLen {
e.Encode(e.buffer[:sendLen])
e.buffer = e.buffer[sendLen:]
}
if len(e.buffer) < sendSize {
return len(data), nil
}
buf := e.buffer
for len(buf) != 0 {
l := min(sendSize, len(buf))
err := e.Encode(buf[:l])
if err != nil {
return len(data), err
}
buf = buf[l:]
}
e.buffer = e.buffer[:0]
return len(data), nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// Encode takes a nalu unit and encodes it into an rtp packet and
// writes to the io.Writer given in NewEncoder

View File

@ -35,7 +35,7 @@ package rtp
const (
rtpVer = 2
headSize = 3 * 4 // Header size of an rtp packet.
defPayloadSize = sendLen // Default payload size for the rtp packet.
defPayloadSize = sendSize // Default payload size for the rtp packet.
defPktSize = headSize + defPayloadSize // Default packet size is header size + payload size.
)