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.

366 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "media/base/rtp_utils.h"
  11. #include <string.h>
  12. #include <cstdint>
  13. #include <vector>
  14. #include "media/base/fake_rtp.h"
  15. #include "rtc_base/async_packet_socket.h"
  16. #include "test/gtest.h"
  17. namespace cricket {
  18. static const uint8_t kRtpPacketWithMarker[] = {
  19. 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  20. // 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD)
  21. // Extension (0xBEDE, 0x1122334455667788)
  22. static const uint8_t kRtpPacketWithMarkerAndCsrcAndExtension[] = {
  23. 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  24. 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
  25. 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  26. static const uint8_t kInvalidPacket[] = {0x80, 0x00};
  27. static const uint8_t kInvalidPacketWithCsrc[] = {
  28. 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  29. 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC};
  30. static const uint8_t kInvalidPacketWithCsrcAndExtension1[] = {
  31. 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x12, 0x34,
  33. 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, 0xBE, 0xDE, 0x00};
  34. static const uint8_t kInvalidPacketWithCsrcAndExtension2[] = {
  35. 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  36. 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD,
  37. 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
  38. // PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111
  39. // No FCI information is needed for PLI.
  40. static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = {
  41. 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11};
  42. // Packet has only mandatory fixed RTCP header
  43. // PT = 204, SSRC = 0x1111
  44. static const uint8_t kNonCompoundRtcpAppPacket[] = {0x81, 0xCC, 0x00, 0x0C,
  45. 0x00, 0x00, 0x11, 0x11};
  46. // PT = 202, Source count = 0
  47. static const uint8_t kNonCompoundRtcpSDESPacket[] = {0x80, 0xCA, 0x00, 0x00};
  48. static uint8_t kFakeTag[4] = {0xba, 0xdd, 0xba, 0xdd};
  49. static uint8_t kTestKey[] = "12345678901234567890";
  50. static uint8_t kTestAstValue[3] = {0xaa, 0xbb, 0xcc};
  51. // Valid rtp Message with 2 byte header extension.
  52. static uint8_t kRtpMsgWith2ByteExtnHeader[] = {
  53. // clang-format off
  54. // clang formatting doesn't respect inline comments.
  55. 0x90, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00,
  57. 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
  58. 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension
  59. 0x01, 0x00, 0x00, 0x00
  60. // clang-format on
  61. };
  62. // RTP packet with single byte extension header of length 4 bytes.
  63. // Extension id = 3 and length = 3
  64. static uint8_t kRtpMsgWithAbsSendTimeExtension[] = {
  65. 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0xBE, 0xDE, 0x00, 0x02, 0x22, 0x00, 0x02, 0x1c, 0x32, 0xaa, 0xbb, 0xcc,
  67. };
  68. // Index of AbsSendTimeExtn data in message |kRtpMsgWithAbsSendTimeExtension|.
  69. static const int kAstIndexInRtpMsg = 21;
  70. static const rtc::ArrayView<const char> kPcmuFrameArrayView =
  71. rtc::MakeArrayView(reinterpret_cast<const char*>(kPcmuFrame),
  72. sizeof(kPcmuFrame));
  73. static const rtc::ArrayView<const char> kRtcpReportArrayView =
  74. rtc::MakeArrayView(reinterpret_cast<const char*>(kRtcpReport),
  75. sizeof(kRtcpReport));
  76. static const rtc::ArrayView<const char> kInvalidPacketArrayView =
  77. rtc::MakeArrayView(reinterpret_cast<const char*>(kInvalidPacket),
  78. sizeof(kInvalidPacket));
  79. TEST(RtpUtilsTest, GetRtp) {
  80. EXPECT_TRUE(IsRtpPacket(kPcmuFrameArrayView));
  81. int pt;
  82. EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt));
  83. EXPECT_EQ(0, pt);
  84. EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker,
  85. sizeof(kRtpPacketWithMarker), &pt));
  86. EXPECT_EQ(0, pt);
  87. int seq_num;
  88. EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num));
  89. EXPECT_EQ(1, seq_num);
  90. uint32_t ts;
  91. EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts));
  92. EXPECT_EQ(0u, ts);
  93. uint32_t ssrc;
  94. EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc));
  95. EXPECT_EQ(1u, ssrc);
  96. RtpHeader header;
  97. EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header));
  98. EXPECT_EQ(0, header.payload_type);
  99. EXPECT_EQ(1, header.seq_num);
  100. EXPECT_EQ(0u, header.timestamp);
  101. EXPECT_EQ(1u, header.ssrc);
  102. EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
  103. EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num));
  104. EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts));
  105. EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc));
  106. }
  107. TEST(RtpUtilsTest, SetRtpHeader) {
  108. uint8_t packet[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  110. RtpHeader header = {9, 1111, 2222u, 3333u};
  111. EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
  112. // Bits: 10 0 0 0000
  113. EXPECT_EQ(128u, packet[0]);
  114. size_t len;
  115. EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len));
  116. EXPECT_EQ(12U, len);
  117. EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header));
  118. EXPECT_EQ(9, header.payload_type);
  119. EXPECT_EQ(1111, header.seq_num);
  120. EXPECT_EQ(2222u, header.timestamp);
  121. EXPECT_EQ(3333u, header.ssrc);
  122. }
  123. TEST(RtpUtilsTest, GetRtpHeaderLen) {
  124. size_t len;
  125. EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len));
  126. EXPECT_EQ(12U, len);
  127. EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension,
  128. sizeof(kRtpPacketWithMarkerAndCsrcAndExtension),
  129. &len));
  130. EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len);
  131. EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len));
  132. EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc,
  133. sizeof(kInvalidPacketWithCsrc), &len));
  134. EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1,
  135. sizeof(kInvalidPacketWithCsrcAndExtension1),
  136. &len));
  137. EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2,
  138. sizeof(kInvalidPacketWithCsrcAndExtension2),
  139. &len));
  140. }
  141. TEST(RtpUtilsTest, GetRtcp) {
  142. int pt;
  143. EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
  144. EXPECT_EQ(0xc9, pt);
  145. EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
  146. uint32_t ssrc;
  147. EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
  148. sizeof(kNonCompoundRtcpPliFeedbackPacket), &ssrc));
  149. EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
  150. sizeof(kNonCompoundRtcpAppPacket), &ssrc));
  151. EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
  152. sizeof(kNonCompoundRtcpSDESPacket), &ssrc));
  153. }
  154. // Invalid RTP packets.
  155. TEST(RtpUtilsTest, InvalidRtpHeader) {
  156. // Rtp message with invalid length.
  157. const uint8_t kRtpMsgWithInvalidLength[] = {
  158. // clang-format off
  159. // clang formatting doesn't respect inline comments.
  160. 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
  162. 0xDD, 0xCC, 0xBB, 0xAA, // Only 1 CSRC, but CC count is 4.
  163. // clang-format on
  164. };
  165. EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength,
  166. sizeof(kRtpMsgWithInvalidLength), nullptr));
  167. // Rtp message with single byte header extension, invalid extension length.
  168. const uint8_t kRtpMsgWithInvalidExtnLength[] = {
  169. 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  170. 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE, 0x0A, 0x00, // Extn length - 0x0A00
  171. };
  172. EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength,
  173. sizeof(kRtpMsgWithInvalidExtnLength),
  174. nullptr));
  175. }
  176. // Valid RTP packet with a 2byte header extension.
  177. TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) {
  178. EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader,
  179. sizeof(kRtpMsgWith2ByteExtnHeader), nullptr));
  180. }
  181. // Valid RTP packet which has 1 byte header AbsSendTime extension in it.
  182. TEST(RtpUtilsTest, ValidRtpPacketWithAbsSendTimeExtension) {
  183. EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithAbsSendTimeExtension,
  184. sizeof(kRtpMsgWithAbsSendTimeExtension),
  185. nullptr));
  186. }
  187. // Verify handling of a 2 byte extension header RTP messsage. Currently these
  188. // messages are not supported.
  189. TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionIn2ByteHeaderExtn) {
  190. std::vector<uint8_t> data(
  191. kRtpMsgWith2ByteExtnHeader,
  192. kRtpMsgWith2ByteExtnHeader + sizeof(kRtpMsgWith2ByteExtnHeader));
  193. EXPECT_FALSE(UpdateRtpAbsSendTimeExtension(&data[0], data.size(), 3, 0));
  194. }
  195. // Verify finding an extension ID in the TURN send indication message.
  196. TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) {
  197. // A valid STUN indication message with a valid RTP header in data attribute
  198. // payload field and no extension bit set.
  199. uint8_t message_without_extension[] = {
  200. // clang-format off
  201. // clang formatting doesn't respect inline comments.
  202. 0x00, 0x16, 0x00, 0x18, // length of
  203. 0x21, 0x12, 0xA4, 0x42, // magic cookie
  204. '0', '1', '2', '3', // transaction id
  205. '4', '5', '6', '7',
  206. '8', '9', 'a', 'b',
  207. 0x00, 0x20, 0x00, 0x04, // Mapped address.
  208. 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x13, 0x00, 0x0C, // Data attribute.
  210. 0x80, 0x00, 0x00, 0x00, // RTP packet.
  211. 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00,
  213. // clang-format on
  214. };
  215. EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(
  216. message_without_extension, sizeof(message_without_extension), 3, 0));
  217. // A valid STUN indication message with a valid RTP header and a extension
  218. // header.
  219. uint8_t message[] = {
  220. // clang-format off
  221. // clang formatting doesn't respect inline comments.
  222. 0x00, 0x16, 0x00, 0x24, // length of
  223. 0x21, 0x12, 0xA4, 0x42, // magic cookie
  224. '0', '1', '2', '3', // transaction id
  225. '4', '5', '6', '7',
  226. '8', '9', 'a', 'b',
  227. 0x00, 0x20, 0x00, 0x04, // Mapped address.
  228. 0x00, 0x00, 0x00, 0x00,
  229. 0x00, 0x13, 0x00, 0x18, // Data attribute.
  230. 0x90, 0x00, 0x00, 0x00, // RTP packet.
  231. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE,
  232. 0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc,
  233. // clang-format on
  234. };
  235. EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, sizeof(message), 3, 0));
  236. }
  237. // Test without any packet options variables set. This method should return
  238. // without HMAC value in the packet.
  239. TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) {
  240. rtc::PacketTimeUpdateParams packet_time_params;
  241. std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
  242. kRtpMsgWithAbsSendTimeExtension +
  243. sizeof(kRtpMsgWithAbsSendTimeExtension));
  244. rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
  245. EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
  246. packet_time_params, 0));
  247. // Making sure HMAC wasn't updated..
  248. EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
  249. kFakeTag, 4));
  250. // Verify AbsouluteSendTime extension field wasn't modified.
  251. EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
  252. sizeof(kTestAstValue)));
  253. }
  254. // Veirfy HMAC is updated when packet option parameters are set.
  255. TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) {
  256. rtc::PacketTimeUpdateParams packet_time_params;
  257. packet_time_params.srtp_auth_key.assign(kTestKey,
  258. kTestKey + sizeof(kTestKey));
  259. packet_time_params.srtp_auth_tag_len = 4;
  260. std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
  261. kRtpMsgWithAbsSendTimeExtension +
  262. sizeof(kRtpMsgWithAbsSendTimeExtension));
  263. rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
  264. EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
  265. packet_time_params, 0));
  266. uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0};
  267. EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
  268. kExpectedTag, sizeof(kExpectedTag)));
  269. // Verify AbsouluteSendTime extension field is not modified.
  270. EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue,
  271. sizeof(kTestAstValue)));
  272. }
  273. // Verify finding an extension ID in a raw rtp message.
  274. TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInRtpPacket) {
  275. std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
  276. kRtpMsgWithAbsSendTimeExtension +
  277. sizeof(kRtpMsgWithAbsSendTimeExtension));
  278. EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(),
  279. 3, 51183266));
  280. // Verify that the timestamp was updated.
  281. const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
  282. EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
  283. sizeof(kExpectedTimestamp)));
  284. }
  285. // Verify we update both AbsSendTime extension header and HMAC.
  286. TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) {
  287. rtc::PacketTimeUpdateParams packet_time_params;
  288. packet_time_params.srtp_auth_key.assign(kTestKey,
  289. kTestKey + sizeof(kTestKey));
  290. packet_time_params.srtp_auth_tag_len = 4;
  291. packet_time_params.rtp_sendtime_extension_id = 3;
  292. // 3 is also present in the test message.
  293. std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension,
  294. kRtpMsgWithAbsSendTimeExtension +
  295. sizeof(kRtpMsgWithAbsSendTimeExtension));
  296. rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
  297. EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
  298. packet_time_params, 51183266));
  299. const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e};
  300. EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)],
  301. kExpectedTag, sizeof(kExpectedTag)));
  302. // Verify that the timestamp was updated.
  303. const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
  304. EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp,
  305. sizeof(kExpectedTimestamp)));
  306. }
  307. TEST(RtpUtilsTest, InferRtpPacketType) {
  308. EXPECT_EQ(RtpPacketType::kRtp, InferRtpPacketType(kPcmuFrameArrayView));
  309. EXPECT_EQ(RtpPacketType::kRtcp, InferRtpPacketType(kRtcpReportArrayView));
  310. EXPECT_EQ(RtpPacketType::kUnknown,
  311. InferRtpPacketType(kInvalidPacketArrayView));
  312. }
  313. } // namespace cricket