summaryrefslogtreecommitdiff
path: root/chromium/third_party/libjingle/source/talk/media/base/rtputils.cc
blob: 5215c3b76f80176e47b732bf912b6d3781b2c190 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * libjingle
 * Copyright 2011 Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "talk/media/base/rtputils.h"

namespace cricket {

static const int kRtpVersion = 2;
static const size_t kRtpFlagsOffset = 0;
static const size_t kRtpPayloadTypeOffset = 1;
static const size_t kRtpSeqNumOffset = 2;
static const size_t kRtpTimestampOffset = 4;
static const size_t kRtpSsrcOffset = 8;
static const size_t kRtcpPayloadTypeOffset = 1;

bool GetUint8(const void* data, size_t offset, int* value) {
  if (!data || !value) {
    return false;
  }
  *value = *(static_cast<const uint8*>(data) + offset);
  return true;
}

bool GetUint16(const void* data, size_t offset, int* value) {
  if (!data || !value) {
    return false;
  }
  *value = static_cast<int>(
      talk_base::GetBE16(static_cast<const uint8*>(data) + offset));
  return true;
}

bool GetUint32(const void* data, size_t offset, uint32* value) {
  if (!data || !value) {
    return false;
  }
  *value = talk_base::GetBE32(static_cast<const uint8*>(data) + offset);
  return true;
}

bool SetUint8(void* data, size_t offset, int value) {
  if (!data) {
    return false;
  }
  talk_base::Set8(data, offset, value);
  return true;
}

bool SetUint16(void* data, size_t offset, int value) {
  if (!data) {
    return false;
  }
  talk_base::SetBE16(static_cast<uint8*>(data) + offset, value);
  return true;
}

bool SetUint32(void* data, size_t offset, uint32 value) {
  if (!data) {
    return false;
  }
  talk_base::SetBE32(static_cast<uint8*>(data) + offset, value);
  return true;
}

bool GetRtpFlags(const void* data, size_t len, int* value) {
  if (len < kMinRtpPacketLen) {
    return false;
  }
  return GetUint8(data, kRtpFlagsOffset, value);
}

bool GetRtpPayloadType(const void* data, size_t len, int* value) {
  if (len < kMinRtpPacketLen) {
    return false;
  }
  if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
    return false;
  }
  *value &= 0x7F;
  return true;
}

bool GetRtpSeqNum(const void* data, size_t len, int* value) {
  if (len < kMinRtpPacketLen) {
    return false;
  }
  return GetUint16(data, kRtpSeqNumOffset, value);
}

bool GetRtpTimestamp(const void* data, size_t len, uint32* value) {
  if (len < kMinRtpPacketLen) {
    return false;
  }
  return GetUint32(data, kRtpTimestampOffset, value);
}

bool GetRtpSsrc(const void* data, size_t len, uint32* value) {
  if (len < kMinRtpPacketLen) {
    return false;
  }
  return GetUint32(data, kRtpSsrcOffset, value);
}

bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
  if (!data || len < kMinRtpPacketLen || !value) return false;
  const uint8* header = static_cast<const uint8*>(data);
  // Get base header size + length of CSRCs (not counting extension yet).
  size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32);
  if (len < header_size) return false;
  // If there's an extension, read and add in the extension size.
  if (header[0] & 0x10) {
    if (len < header_size + sizeof(uint32)) return false;
    header_size += ((talk_base::GetBE16(header + header_size + 2) + 1) *
                    sizeof(uint32));
    if (len < header_size) return false;
  }
  *value = header_size;
  return true;
}

bool GetRtpVersion(const void* data, size_t len, int* version) {
  if (len == 0) {
    return false;
  }

  const uint8 first = static_cast<const uint8*>(data)[0];
  *version = static_cast<int>((first >> 6) & 0x3);
  return true;
}

bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
  return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
          GetRtpSeqNum(data, len, &(header->seq_num)) &&
          GetRtpTimestamp(data, len, &(header->timestamp)) &&
          GetRtpSsrc(data, len, &(header->ssrc)));
}

bool GetRtcpType(const void* data, size_t len, int* value) {
  if (len < kMinRtcpPacketLen) {
    return false;
  }
  return GetUint8(data, kRtcpPayloadTypeOffset, value);
}

// This method returns SSRC first of RTCP packet, except if packet is SDES.
// TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
// to send non-compound packets only to feedback messages.
bool GetRtcpSsrc(const void* data, size_t len, uint32* value) {
  // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
  if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
  int pl_type;
  if (!GetRtcpType(data, len, &pl_type)) return false;
  // SDES packet parsing is not supported.
  if (pl_type == kRtcpTypeSDES) return false;
  *value = talk_base::GetBE32(static_cast<const uint8*>(data) + 4);
  return true;
}

bool SetRtpHeaderFlags(
    void* data, size_t len,
    bool padding, bool extension, int csrc_count) {
  if (csrc_count > 0x0F) {
    return false;
  }
  int flags = 0;
  flags |= (kRtpVersion << 6);
  flags |= ((padding ? 1 : 0) << 5);
  flags |= ((extension ? 1 : 0) << 4);
  flags |= csrc_count;
  return SetUint8(data, kRtpFlagsOffset, flags);
}

// Assumes marker bit is 0.
bool SetRtpPayloadType(void* data, size_t len, int value) {
  if (value >= 0x7F) {
    return false;
  }
  return SetUint8(data, kRtpPayloadTypeOffset, value & 0x7F);
}

bool SetRtpSeqNum(void* data, size_t len, int value) {
  return SetUint16(data, kRtpSeqNumOffset, value);
}

bool SetRtpTimestamp(void* data, size_t len, uint32 value) {
  return SetUint32(data, kRtpTimestampOffset, value);
}

bool SetRtpSsrc(void* data, size_t len, uint32 value) {
  return SetUint32(data, kRtpSsrcOffset, value);
}

// Assumes version 2, no padding, no extensions, no csrcs.
bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
  return (SetRtpHeaderFlags(data, len, false, false, 0) &&
          SetRtpPayloadType(data, len, header.payload_type) &&
          SetRtpSeqNum(data, len, header.seq_num) &&
          SetRtpTimestamp(data, len, header.timestamp) &&
          SetRtpSsrc(data, len, header.ssrc));
}

}  // namespace cricket