1 /*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "talk/media/base/rtputils.h"
29
30 namespace cricket {
31
32 static const int kRtpVersion = 2;
33 static const size_t kRtpFlagsOffset = 0;
34 static const size_t kRtpPayloadTypeOffset = 1;
35 static const size_t kRtpSeqNumOffset = 2;
36 static const size_t kRtpTimestampOffset = 4;
37 static const size_t kRtpSsrcOffset = 8;
38 static const size_t kRtcpPayloadTypeOffset = 1;
39
GetUint8(const void * data,size_t offset,int * value)40 bool GetUint8(const void* data, size_t offset, int* value) {
41 if (!data || !value) {
42 return false;
43 }
44 *value = *(static_cast<const uint8*>(data) + offset);
45 return true;
46 }
47
GetUint16(const void * data,size_t offset,int * value)48 bool GetUint16(const void* data, size_t offset, int* value) {
49 if (!data || !value) {
50 return false;
51 }
52 *value = static_cast<int>(
53 rtc::GetBE16(static_cast<const uint8*>(data) + offset));
54 return true;
55 }
56
GetUint32(const void * data,size_t offset,uint32 * value)57 bool GetUint32(const void* data, size_t offset, uint32* value) {
58 if (!data || !value) {
59 return false;
60 }
61 *value = rtc::GetBE32(static_cast<const uint8*>(data) + offset);
62 return true;
63 }
64
SetUint8(void * data,size_t offset,int value)65 bool SetUint8(void* data, size_t offset, int value) {
66 if (!data) {
67 return false;
68 }
69 rtc::Set8(data, offset, value);
70 return true;
71 }
72
SetUint16(void * data,size_t offset,int value)73 bool SetUint16(void* data, size_t offset, int value) {
74 if (!data) {
75 return false;
76 }
77 rtc::SetBE16(static_cast<uint8*>(data) + offset, value);
78 return true;
79 }
80
SetUint32(void * data,size_t offset,uint32 value)81 bool SetUint32(void* data, size_t offset, uint32 value) {
82 if (!data) {
83 return false;
84 }
85 rtc::SetBE32(static_cast<uint8*>(data) + offset, value);
86 return true;
87 }
88
GetRtpFlags(const void * data,size_t len,int * value)89 bool GetRtpFlags(const void* data, size_t len, int* value) {
90 if (len < kMinRtpPacketLen) {
91 return false;
92 }
93 return GetUint8(data, kRtpFlagsOffset, value);
94 }
95
GetRtpPayloadType(const void * data,size_t len,int * value)96 bool GetRtpPayloadType(const void* data, size_t len, int* value) {
97 if (len < kMinRtpPacketLen) {
98 return false;
99 }
100 if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
101 return false;
102 }
103 *value &= 0x7F;
104 return true;
105 }
106
GetRtpSeqNum(const void * data,size_t len,int * value)107 bool GetRtpSeqNum(const void* data, size_t len, int* value) {
108 if (len < kMinRtpPacketLen) {
109 return false;
110 }
111 return GetUint16(data, kRtpSeqNumOffset, value);
112 }
113
GetRtpTimestamp(const void * data,size_t len,uint32 * value)114 bool GetRtpTimestamp(const void* data, size_t len, uint32* value) {
115 if (len < kMinRtpPacketLen) {
116 return false;
117 }
118 return GetUint32(data, kRtpTimestampOffset, value);
119 }
120
GetRtpSsrc(const void * data,size_t len,uint32 * value)121 bool GetRtpSsrc(const void* data, size_t len, uint32* value) {
122 if (len < kMinRtpPacketLen) {
123 return false;
124 }
125 return GetUint32(data, kRtpSsrcOffset, value);
126 }
127
GetRtpHeaderLen(const void * data,size_t len,size_t * value)128 bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
129 if (!data || len < kMinRtpPacketLen || !value) return false;
130 const uint8* header = static_cast<const uint8*>(data);
131 // Get base header size + length of CSRCs (not counting extension yet).
132 size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32);
133 if (len < header_size) return false;
134 // If there's an extension, read and add in the extension size.
135 if (header[0] & 0x10) {
136 if (len < header_size + sizeof(uint32)) return false;
137 header_size += ((rtc::GetBE16(header + header_size + 2) + 1) *
138 sizeof(uint32));
139 if (len < header_size) return false;
140 }
141 *value = header_size;
142 return true;
143 }
144
GetRtpVersion(const void * data,size_t len,int * version)145 bool GetRtpVersion(const void* data, size_t len, int* version) {
146 if (len == 0) {
147 return false;
148 }
149
150 const uint8 first = static_cast<const uint8*>(data)[0];
151 *version = static_cast<int>((first >> 6) & 0x3);
152 return true;
153 }
154
GetRtpHeader(const void * data,size_t len,RtpHeader * header)155 bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
156 return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
157 GetRtpSeqNum(data, len, &(header->seq_num)) &&
158 GetRtpTimestamp(data, len, &(header->timestamp)) &&
159 GetRtpSsrc(data, len, &(header->ssrc)));
160 }
161
GetRtcpType(const void * data,size_t len,int * value)162 bool GetRtcpType(const void* data, size_t len, int* value) {
163 if (len < kMinRtcpPacketLen) {
164 return false;
165 }
166 return GetUint8(data, kRtcpPayloadTypeOffset, value);
167 }
168
169 // This method returns SSRC first of RTCP packet, except if packet is SDES.
170 // TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
171 // to send non-compound packets only to feedback messages.
GetRtcpSsrc(const void * data,size_t len,uint32 * value)172 bool GetRtcpSsrc(const void* data, size_t len, uint32* value) {
173 // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
174 if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
175 int pl_type;
176 if (!GetRtcpType(data, len, &pl_type)) return false;
177 // SDES packet parsing is not supported.
178 if (pl_type == kRtcpTypeSDES) return false;
179 *value = rtc::GetBE32(static_cast<const uint8*>(data) + 4);
180 return true;
181 }
182
SetRtpHeaderFlags(void * data,size_t len,bool padding,bool extension,int csrc_count)183 bool SetRtpHeaderFlags(
184 void* data, size_t len,
185 bool padding, bool extension, int csrc_count) {
186 if (csrc_count > 0x0F) {
187 return false;
188 }
189 int flags = 0;
190 flags |= (kRtpVersion << 6);
191 flags |= ((padding ? 1 : 0) << 5);
192 flags |= ((extension ? 1 : 0) << 4);
193 flags |= csrc_count;
194 return SetUint8(data, kRtpFlagsOffset, flags);
195 }
196
197 // Assumes marker bit is 0.
SetRtpPayloadType(void * data,size_t len,int value)198 bool SetRtpPayloadType(void* data, size_t len, int value) {
199 if (value >= 0x7F) {
200 return false;
201 }
202 return SetUint8(data, kRtpPayloadTypeOffset, value & 0x7F);
203 }
204
SetRtpSeqNum(void * data,size_t len,int value)205 bool SetRtpSeqNum(void* data, size_t len, int value) {
206 return SetUint16(data, kRtpSeqNumOffset, value);
207 }
208
SetRtpTimestamp(void * data,size_t len,uint32 value)209 bool SetRtpTimestamp(void* data, size_t len, uint32 value) {
210 return SetUint32(data, kRtpTimestampOffset, value);
211 }
212
SetRtpSsrc(void * data,size_t len,uint32 value)213 bool SetRtpSsrc(void* data, size_t len, uint32 value) {
214 return SetUint32(data, kRtpSsrcOffset, value);
215 }
216
217 // Assumes version 2, no padding, no extensions, no csrcs.
SetRtpHeader(void * data,size_t len,const RtpHeader & header)218 bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
219 return (SetRtpHeaderFlags(data, len, false, false, 0) &&
220 SetRtpPayloadType(data, len, header.payload_type) &&
221 SetRtpSeqNum(data, len, header.seq_num) &&
222 SetRtpTimestamp(data, len, header.timestamp) &&
223 SetRtpSsrc(data, len, header.ssrc));
224 }
225
IsRtpPacket(const void * data,size_t len)226 bool IsRtpPacket(const void* data, size_t len) {
227 if (len < kMinRtpPacketLen)
228 return false;
229
230 int version = 0;
231 if (!GetRtpVersion(data, len, &version))
232 return false;
233
234 return version == kRtpVersion;
235 }
236
237 } // namespace cricket
238