mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 14:52:46 +08:00

Read() / Write() are used to read / write from streams, while Unmarshal() / Marshal() are used to decode / encode from / to bytes.
55 lines
852 B
Go
55 lines
852 B
Go
package base
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
rtspMaxContentLength = 128 * 1024
|
|
)
|
|
|
|
type body []byte
|
|
|
|
func (b *body) read(header Header, rb *bufio.Reader) error {
|
|
cls, ok := header["Content-Length"]
|
|
if !ok || len(cls) != 1 {
|
|
*b = nil
|
|
return nil
|
|
}
|
|
|
|
cl, err := strconv.ParseInt(cls[0], 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid Content-Length")
|
|
}
|
|
|
|
if cl > rtspMaxContentLength {
|
|
return fmt.Errorf("Content-Length exceeds %d (it's %d)",
|
|
rtspMaxContentLength, cl)
|
|
}
|
|
|
|
*b = make([]byte, cl)
|
|
n, err := io.ReadFull(rb, *b)
|
|
if err != nil && n != len(*b) {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b body) marshalSize() int {
|
|
return len(b)
|
|
}
|
|
|
|
func (b body) marshalTo(buf []byte) int {
|
|
return copy(buf, b)
|
|
}
|
|
|
|
func (b body) marshal() []byte {
|
|
buf := make([]byte, b.marshalSize())
|
|
b.marshalTo(buf)
|
|
return buf
|
|
}
|