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 uint8_t 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_t*>(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_t*>(data) + offset));
54   return true;
55 }
56 
GetUint32(const void * data,size_t offset,uint32_t * value)57 bool GetUint32(const void* data, size_t offset, uint32_t* value) {
58   if (!data || !value) {
59     return false;
60   }
61   *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset);
62   return true;
63 }
64 
SetUint8(void * data,size_t offset,uint8_t value)65 bool SetUint8(void* data, size_t offset, uint8_t 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,uint16_t value)73 bool SetUint16(void* data, size_t offset, uint16_t value) {
74   if (!data) {
75     return false;
76   }
77   rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value);
78   return true;
79 }
80 
SetUint32(void * data,size_t offset,uint32_t value)81 bool SetUint32(void* data, size_t offset, uint32_t value) {
82   if (!data) {
83     return false;
84   }
85   rtc::SetBE32(static_cast<uint8_t*>(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_t * value)114 bool GetRtpTimestamp(const void* data, size_t len, uint32_t* 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_t * value)121 bool GetRtpSsrc(const void* data, size_t len, uint32_t* 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_t* header = static_cast<const uint8_t*>(data);
131   // Get base header size + length of CSRCs (not counting extension yet).
132   size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t);
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_t))
137       return false;
138     header_size +=
139         ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t));
140     if (len < header_size) return false;
141   }
142   *value = header_size;
143   return true;
144 }
145 
GetRtpHeader(const void * data,size_t len,RtpHeader * header)146 bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
147   return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
148           GetRtpSeqNum(data, len, &(header->seq_num)) &&
149           GetRtpTimestamp(data, len, &(header->timestamp)) &&
150           GetRtpSsrc(data, len, &(header->ssrc)));
151 }
152 
GetRtcpType(const void * data,size_t len,int * value)153 bool GetRtcpType(const void* data, size_t len, int* value) {
154   if (len < kMinRtcpPacketLen) {
155     return false;
156   }
157   return GetUint8(data, kRtcpPayloadTypeOffset, value);
158 }
159 
160 // This method returns SSRC first of RTCP packet, except if packet is SDES.
161 // TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
162 // to send non-compound packets only to feedback messages.
GetRtcpSsrc(const void * data,size_t len,uint32_t * value)163 bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) {
164   // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
165   if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
166   int pl_type;
167   if (!GetRtcpType(data, len, &pl_type)) return false;
168   // SDES packet parsing is not supported.
169   if (pl_type == kRtcpTypeSDES) return false;
170   *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4);
171   return true;
172 }
173 
SetRtpSsrc(void * data,size_t len,uint32_t value)174 bool SetRtpSsrc(void* data, size_t len, uint32_t value) {
175   return SetUint32(data, kRtpSsrcOffset, value);
176 }
177 
178 // Assumes version 2, no padding, no extensions, no csrcs.
SetRtpHeader(void * data,size_t len,const RtpHeader & header)179 bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
180   if (!IsValidRtpPayloadType(header.payload_type) ||
181       header.seq_num < 0 || header.seq_num > UINT16_MAX) {
182     return false;
183   }
184   return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
185           SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
186           SetUint16(data, kRtpSeqNumOffset,
187                     static_cast<uint16_t>(header.seq_num)) &&
188           SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
189           SetRtpSsrc(data, len, header.ssrc));
190 }
191 
IsRtpPacket(const void * data,size_t len)192 bool IsRtpPacket(const void* data, size_t len) {
193   if (len < kMinRtpPacketLen)
194     return false;
195 
196   return (static_cast<const uint8_t*>(data)[0] >> 6) == kRtpVersion;
197 }
198 
IsValidRtpPayloadType(int payload_type)199 bool IsValidRtpPayloadType(int payload_type) {
200   return payload_type >= 0 && payload_type <= 127;
201 }
202 
203 }  // namespace cricket
204