You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/media/base/rtputils.h"
  28. namespace cricket {
  29. static const int kRtpVersion = 2;
  30. static const size_t kRtpFlagsOffset = 0;
  31. static const size_t kRtpPayloadTypeOffset = 1;
  32. static const size_t kRtpSeqNumOffset = 2;
  33. static const size_t kRtpTimestampOffset = 4;
  34. static const size_t kRtpSsrcOffset = 8;
  35. static const size_t kRtcpPayloadTypeOffset = 1;
  36. bool GetUint8(const void* data, size_t offset, int* value) {
  37. if (!data || !value) {
  38. return false;
  39. }
  40. *value = *(static_cast<const uint8*>(data) + offset);
  41. return true;
  42. }
  43. bool GetUint16(const void* data, size_t offset, int* value) {
  44. if (!data || !value) {
  45. return false;
  46. }
  47. *value = static_cast<int>(
  48. talk_base::GetBE16(static_cast<const uint8*>(data) + offset));
  49. return true;
  50. }
  51. bool GetUint32(const void* data, size_t offset, uint32* value) {
  52. if (!data || !value) {
  53. return false;
  54. }
  55. *value = talk_base::GetBE32(static_cast<const uint8*>(data) + offset);
  56. return true;
  57. }
  58. bool SetUint8(void* data, size_t offset, int value) {
  59. if (!data) {
  60. return false;
  61. }
  62. talk_base::Set8(data, offset, value);
  63. return true;
  64. }
  65. bool SetUint16(void* data, size_t offset, int value) {
  66. if (!data) {
  67. return false;
  68. }
  69. talk_base::SetBE16(static_cast<uint8*>(data) + offset, value);
  70. return true;
  71. }
  72. bool SetUint32(void* data, size_t offset, uint32 value) {
  73. if (!data) {
  74. return false;
  75. }
  76. talk_base::SetBE32(static_cast<uint8*>(data) + offset, value);
  77. return true;
  78. }
  79. bool GetRtpFlags(const void* data, size_t len, int* value) {
  80. if (len < kMinRtpPacketLen) {
  81. return false;
  82. }
  83. return GetUint8(data, kRtpFlagsOffset, value);
  84. }
  85. bool GetRtpPayloadType(const void* data, size_t len, int* value) {
  86. if (len < kMinRtpPacketLen) {
  87. return false;
  88. }
  89. if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
  90. return false;
  91. }
  92. *value &= 0x7F;
  93. return true;
  94. }
  95. bool GetRtpSeqNum(const void* data, size_t len, int* value) {
  96. if (len < kMinRtpPacketLen) {
  97. return false;
  98. }
  99. return GetUint16(data, kRtpSeqNumOffset, value);
  100. }
  101. bool GetRtpTimestamp(const void* data, size_t len, uint32* value) {
  102. if (len < kMinRtpPacketLen) {
  103. return false;
  104. }
  105. return GetUint32(data, kRtpTimestampOffset, value);
  106. }
  107. bool GetRtpSsrc(const void* data, size_t len, uint32* value) {
  108. if (len < kMinRtpPacketLen) {
  109. return false;
  110. }
  111. return GetUint32(data, kRtpSsrcOffset, value);
  112. }
  113. bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
  114. if (!data || len < kMinRtpPacketLen || !value) return false;
  115. const uint8* header = static_cast<const uint8*>(data);
  116. // Get base header size + length of CSRCs (not counting extension yet).
  117. size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32);
  118. if (len < header_size) return false;
  119. // If there's an extension, read and add in the extension size.
  120. if (header[0] & 0x10) {
  121. if (len < header_size + sizeof(uint32)) return false;
  122. header_size += ((talk_base::GetBE16(header + header_size + 2) + 1) *
  123. sizeof(uint32));
  124. if (len < header_size) return false;
  125. }
  126. *value = header_size;
  127. return true;
  128. }
  129. bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
  130. return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
  131. GetRtpSeqNum(data, len, &(header->seq_num)) &&
  132. GetRtpTimestamp(data, len, &(header->timestamp)) &&
  133. GetRtpSsrc(data, len, &(header->ssrc)));
  134. }
  135. bool GetRtcpType(const void* data, size_t len, int* value) {
  136. if (len < kMinRtcpPacketLen) {
  137. return false;
  138. }
  139. return GetUint8(data, kRtcpPayloadTypeOffset, value);
  140. }
  141. // This method returns SSRC first of RTCP packet, except if packet is SDES.
  142. // TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
  143. // to send non-compound packets only to feedback messages.
  144. bool GetRtcpSsrc(const void* data, size_t len, uint32* value) {
  145. // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
  146. if (!data || len < kMinRtcpPacketLen + 4 || !value) return false;
  147. int pl_type;
  148. if (!GetRtcpType(data, len, &pl_type)) return false;
  149. // SDES packet parsing is not supported.
  150. if (pl_type == kRtcpTypeSDES) return false;
  151. *value = talk_base::GetBE32(static_cast<const uint8*>(data) + 4);
  152. return true;
  153. }
  154. bool SetRtpHeaderFlags(
  155. void* data, size_t len,
  156. bool padding, bool extension, int csrc_count) {
  157. if (csrc_count > 0x0F) {
  158. return false;
  159. }
  160. int flags = 0;
  161. flags |= (kRtpVersion << 6);
  162. flags |= ((padding ? 1 : 0) << 5);
  163. flags |= ((extension ? 1 : 0) << 4);
  164. flags |= csrc_count;
  165. return SetUint8(data, kRtpFlagsOffset, flags);
  166. }
  167. // Assumes marker bit is 0.
  168. bool SetRtpPayloadType(void* data, size_t len, int value) {
  169. if (value >= 0x7F) {
  170. return false;
  171. }
  172. return SetUint8(data, kRtpPayloadTypeOffset, value & 0x7F);
  173. }
  174. bool SetRtpSeqNum(void* data, size_t len, int value) {
  175. return SetUint16(data, kRtpSeqNumOffset, value);
  176. }
  177. bool SetRtpTimestamp(void* data, size_t len, uint32 value) {
  178. return SetUint32(data, kRtpTimestampOffset, value);
  179. }
  180. bool SetRtpSsrc(void* data, size_t len, uint32 value) {
  181. return SetUint32(data, kRtpSsrcOffset, value);
  182. }
  183. // Assumes version 2, no padding, no extensions, no csrcs.
  184. bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
  185. return (SetRtpHeaderFlags(data, len, false, false, 0) &&
  186. SetRtpPayloadType(data, len, header.payload_type) &&
  187. SetRtpSeqNum(data, len, header.seq_num) &&
  188. SetRtpTimestamp(data, len, header.timestamp) &&
  189. SetRtpSsrc(data, len, header.ssrc));
  190. }
  191. } // namespace cricket