forked from pion/interceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.go
68 lines (57 loc) · 1.72 KB
/
attributes.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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package interceptor
import (
"errors"
"github.com/pion/rtcp"
"github.com/pion/rtp"
)
type unmarshaledDataKeyType int
const (
rtpHeaderKey unmarshaledDataKeyType = iota
rtcpPacketsKey
)
var errInvalidType = errors.New("found value of invalid type in attributes map")
// Attributes are a generic key/value store used by interceptors
type Attributes map[interface{}]interface{}
// Get returns the attribute associated with key.
func (a Attributes) Get(key interface{}) interface{} {
return a[key]
}
// Set sets the attribute associated with key to the given value.
func (a Attributes) Set(key interface{}, val interface{}) {
a[key] = val
}
// GetRTPHeader gets the RTP header if present. If it is not present, it will be
// unmarshalled from the raw byte slice and stored in the attributes.
func (a Attributes) GetRTPHeader(raw []byte) (*rtp.Header, error) {
if val, ok := a[rtpHeaderKey]; ok {
if header, ok := val.(*rtp.Header); ok {
return header, nil
}
return nil, errInvalidType
}
header := &rtp.Header{}
if _, err := header.Unmarshal(raw); err != nil {
return nil, err
}
a[rtpHeaderKey] = header
return header, nil
}
// GetRTCPPackets gets the RTCP packets if present. If the packet slice is not
// present, it will be unmarshalled from the raw byte slice and stored in the
// attributes.
func (a Attributes) GetRTCPPackets(raw []byte) ([]rtcp.Packet, error) {
if val, ok := a[rtcpPacketsKey]; ok {
if packets, ok := val.([]rtcp.Packet); ok {
return packets, nil
}
return nil, errInvalidType
}
pkts, err := rtcp.Unmarshal(raw)
if err != nil {
return nil, err
}
a[rtcpPacketsKey] = pkts
return pkts, nil
}