forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtpinfo.go
98 lines (76 loc) · 1.77 KB
/
rtpinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package headers
import (
"fmt"
"strconv"
"strings"
"github.com/cobalt-robotics/gortsplib/pkg/base"
)
// RTPInfoEntry is an entry of a RTP-Info header.
type RTPInfoEntry struct {
URL string
SequenceNumber *uint16
Timestamp *uint32
}
// RTPInfo is a RTP-Info header.
type RTPInfo []*RTPInfoEntry
// Unmarshal decodes a RTP-Info header.
func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
if len(v) == 0 {
return fmt.Errorf("value not provided")
}
if len(v) > 1 {
return fmt.Errorf("value provided multiple times (%v)", v)
}
for _, part := range strings.Split(v[0], ",") {
e := &RTPInfoEntry{}
// remove leading spaces
part = strings.TrimLeft(part, " ")
kvs, err := keyValParse(part, ';')
if err != nil {
return err
}
for k, v := range kvs {
switch k {
case "url":
e.URL = v
case "seq":
vi, err := strconv.ParseUint(v, 10, 16)
if err != nil {
return err
}
vi2 := uint16(vi)
e.SequenceNumber = &vi2
case "rtptime":
vi, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return err
}
vi2 := uint32(vi)
e.Timestamp = &vi2
default:
// ignore non-standard keys
}
}
if e.URL == "" {
return fmt.Errorf("URL is missing")
}
*h = append(*h, e)
}
return nil
}
// Marshal encodes a RTP-Info header.
func (h RTPInfo) Marshal() base.HeaderValue {
rets := make([]string, len(h))
for i, e := range h {
var tmp []string
tmp = append(tmp, "url="+e.URL)
if e.SequenceNumber != nil {
tmp = append(tmp, "seq="+strconv.FormatUint(uint64(*e.SequenceNumber), 10))
}
if e.Timestamp != nil {
tmp = append(tmp, "rtptime="+strconv.FormatUint(uint64(*e.Timestamp), 10))
}
rets[i] = strings.Join(tmp, ";")
}
return base.HeaderValue{strings.Join(rets, ",")}
}