-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
147 lines (123 loc) · 4.33 KB
/
application.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package rtcp
import (
"encoding/binary"
"fmt"
)
// The Application packet indicates application data
type Application struct {
// subtype
SubType uint8
// SSRC or CSRC
SSRC uint32
// Namee (ASCII)
Name [4]byte
// application-dependent data
Data []byte
}
var _ Packet = (*Application)(nil) // assert is a Packet
const (
appSSRCOffset = 0
appNameOffset = appSSRCOffset + ssrcLength
appNameLength = 4
appDataOffset = appNameOffset + appNameLength
)
// Marshal encodes the Application packet in binary
func (app Application) Marshal() ([]byte, error) {
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P| subtype | PT=APP=204 | length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SSRC/CSRC |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | name (ASCII) |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* | application-dependent data ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
rawPacket := make([]byte, app.MarshalSize())
packetBody := rawPacket[headerLength:]
binary.BigEndian.PutUint32(packetBody, app.SSRC)
copy(packetBody[appNameOffset:], app.Name[:])
copy(packetBody[appDataOffset:], app.Data)
hData, err := app.Header().Marshal()
if err != nil {
return nil, err
}
copy(rawPacket, hData)
if getPadding(app.packetLen()) != 0 {
rawPacket[len(rawPacket)-1] = uint8(app.MarshalSize() - app.packetLen())
}
return rawPacket, nil
}
// Unmarshal decodes the Application packet from binary
func (app *Application) Unmarshal(rawPacket []byte) error {
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P| subtype | PT=APP=204 | length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SSRC/CSRC |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | name (ASCII) |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* | application-dependent data ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
var header Header
if err := header.Unmarshal(rawPacket); err != nil {
return err
}
if header.Type != TypeApplicationDefined {
return errWrongType
}
if len(rawPacket) < headerLength || getPadding(len(rawPacket)) != 0 {
return errPacketTooShort
}
packetBody := rawPacket[headerLength:]
if len(packetBody) < appDataOffset {
return errPacketTooShort
}
app.SubType = header.Count
app.SSRC = binary.BigEndian.Uint32(packetBody[appSSRCOffset:])
copy(app.Name[:], packetBody[appNameOffset:])
dataBody := packetBody[appDataOffset:]
if header.Padding {
len := len(dataBody) - int(dataBody[len(dataBody)-1])
dataBody = dataBody[:len]
}
if len(app.Data) < len(dataBody) {
app.Data = make([]byte, len(dataBody))
}
copy(app.Data, dataBody)
return nil
}
// Header returns the Header associated with this packet.
func (app *Application) Header() Header {
return Header{
Padding: getPadding(app.packetLen()) != 0,
Count: app.SubType,
Type: TypeApplicationDefined,
Length: uint16((app.MarshalSize() / 4) - 1),
}
}
func (app *Application) MarshalSize() int {
l := app.packetLen()
return l + getPadding(l)
}
func (app *Application) packetLen() int {
return headerLength + ssrcLength + appNameLength + len(app.Data)
}
// DestinationSSRC returns an array of SSRC values that this packet refers to.
func (app *Application) DestinationSSRC() []uint32 {
return []uint32{app.SSRC}
}
func (app *Application) String() string {
out := fmt.Sprintf("Application from %x\n", app.SSRC)
out += fmt.Sprintf("\tSubType: %x\n", app.SubType)
out += fmt.Sprintf("\tName: %c%c%c%c\n", app.Name[0], app.Name[1], app.Name[2], app.Name[3])
out += fmt.Sprintf("\tData: %v\n", app.Data)
return out
}