nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "call/rtx_receive_stream.h" |
| 12 | #include "call/test/mock_rtp_packet_sink_interface.h" |
| 13 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
| 14 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 15 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 16 | #include "test/gmock.h" |
| 17 | #include "test/gtest.h" |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | using ::testing::_; |
| 24 | using ::testing::StrictMock; |
| 25 | |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 26 | constexpr int kMediaPayloadType = 100; |
| 27 | constexpr int kRtxPayloadType = 98; |
nisse | 3864499 | 2017-08-30 04:16:40 -0700 | [diff] [blame] | 28 | constexpr int kUnknownPayloadType = 90; |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 29 | constexpr uint32_t kMediaSSRC = 0x3333333; |
| 30 | constexpr uint16_t kMediaSeqno = 0x5657; |
| 31 | |
| 32 | constexpr uint8_t kRtxPacket[] = { |
| 33 | 0x80, // Version 2. |
| 34 | 98, // Payload type. |
| 35 | 0x12, 0x34, // Seqno. |
| 36 | 0x11, 0x11, 0x11, 0x11, // Timestamp. |
| 37 | 0x22, 0x22, 0x22, 0x22, // SSRC. |
| 38 | // RTX header. |
| 39 | 0x56, 0x57, // Orig seqno. |
| 40 | // Payload. |
| 41 | 0xee, |
| 42 | }; |
| 43 | |
| 44 | constexpr uint8_t kRtxPacketWithCVO[] = { |
| 45 | 0x90, // Version 2, X set. |
| 46 | 98, // Payload type. |
| 47 | 0x12, 0x34, // Seqno. |
| 48 | 0x11, 0x11, 0x11, 0x11, // Timestamp. |
| 49 | 0x22, 0x22, 0x22, 0x22, // SSRC. |
| 50 | 0xbe, 0xde, 0x00, 0x01, // Extension header. |
| 51 | 0x30, 0x01, 0x00, 0x00, // 90 degree rotation. |
| 52 | // RTX header. |
| 53 | 0x56, 0x57, // Orig seqno. |
| 54 | // Payload. |
| 55 | 0xee, |
| 56 | }; |
| 57 | |
| 58 | std::map<int, int> PayloadTypeMapping() { |
nisse | 3864499 | 2017-08-30 04:16:40 -0700 | [diff] [blame] | 59 | const std::map<int, int> m = {{kRtxPayloadType, kMediaPayloadType}}; |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 60 | return m; |
| 61 | } |
| 62 | |
| 63 | template <typename T> |
| 64 | rtc::ArrayView<T> Truncate(rtc::ArrayView<T> a, size_t drop) { |
| 65 | return a.subview(0, a.size() - drop); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | TEST(RtxReceiveStreamTest, RestoresPacketPayload) { |
| 71 | StrictMock<MockRtpPacketSink> media_sink; |
| 72 | RtxReceiveStream rtx_sink(&media_sink, PayloadTypeMapping(), kMediaSSRC); |
| 73 | RtpPacketReceived rtx_packet; |
| 74 | EXPECT_TRUE(rtx_packet.Parse(rtc::ArrayView<const uint8_t>(kRtxPacket))); |
| 75 | |
| 76 | EXPECT_CALL(media_sink, OnRtpPacket(_)).WillOnce(testing::Invoke( |
| 77 | [](const RtpPacketReceived& packet) { |
| 78 | EXPECT_EQ(packet.SequenceNumber(), kMediaSeqno); |
| 79 | EXPECT_EQ(packet.Ssrc(), kMediaSSRC); |
| 80 | EXPECT_EQ(packet.PayloadType(), kMediaPayloadType); |
| 81 | EXPECT_THAT(packet.payload(), testing::ElementsAre(0xee)); |
| 82 | })); |
| 83 | |
| 84 | rtx_sink.OnRtpPacket(rtx_packet); |
| 85 | } |
| 86 | |
nisse | 3864499 | 2017-08-30 04:16:40 -0700 | [diff] [blame] | 87 | TEST(RtxReceiveStreamTest, SetsRecoveredFlag) { |
| 88 | StrictMock<MockRtpPacketSink> media_sink; |
| 89 | RtxReceiveStream rtx_sink(&media_sink, PayloadTypeMapping(), kMediaSSRC); |
| 90 | RtpPacketReceived rtx_packet; |
| 91 | EXPECT_TRUE(rtx_packet.Parse(rtc::ArrayView<const uint8_t>(kRtxPacket))); |
| 92 | EXPECT_FALSE(rtx_packet.recovered()); |
| 93 | EXPECT_CALL(media_sink, OnRtpPacket(_)) |
| 94 | .WillOnce(testing::Invoke([](const RtpPacketReceived& packet) { |
| 95 | EXPECT_TRUE(packet.recovered()); |
| 96 | })); |
| 97 | |
| 98 | rtx_sink.OnRtpPacket(rtx_packet); |
| 99 | } |
| 100 | |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 101 | TEST(RtxReceiveStreamTest, IgnoresUnknownPayloadType) { |
| 102 | StrictMock<MockRtpPacketSink> media_sink; |
nisse | 3864499 | 2017-08-30 04:16:40 -0700 | [diff] [blame] | 103 | const std::map<int, int> payload_type_mapping = { |
| 104 | {kUnknownPayloadType, kMediaPayloadType}}; |
| 105 | |
| 106 | RtxReceiveStream rtx_sink(&media_sink, payload_type_mapping, kMediaSSRC); |
nisse | eed52bf | 2017-05-19 06:15:19 -0700 | [diff] [blame] | 107 | RtpPacketReceived rtx_packet; |
| 108 | EXPECT_TRUE(rtx_packet.Parse(rtc::ArrayView<const uint8_t>(kRtxPacket))); |
| 109 | rtx_sink.OnRtpPacket(rtx_packet); |
| 110 | } |
| 111 | |
| 112 | TEST(RtxReceiveStreamTest, IgnoresTruncatedPacket) { |
| 113 | StrictMock<MockRtpPacketSink> media_sink; |
| 114 | RtxReceiveStream rtx_sink(&media_sink, PayloadTypeMapping(), kMediaSSRC); |
| 115 | RtpPacketReceived rtx_packet; |
| 116 | EXPECT_TRUE( |
| 117 | rtx_packet.Parse(Truncate(rtc::ArrayView<const uint8_t>(kRtxPacket), 2))); |
| 118 | rtx_sink.OnRtpPacket(rtx_packet); |
| 119 | } |
| 120 | |
| 121 | TEST(RtxReceiveStreamTest, CopiesRtpHeaderExtensions) { |
| 122 | StrictMock<MockRtpPacketSink> media_sink; |
| 123 | RtxReceiveStream rtx_sink(&media_sink, PayloadTypeMapping(), kMediaSSRC); |
| 124 | RtpHeaderExtensionMap extension_map; |
| 125 | extension_map.RegisterByType(3, kRtpExtensionVideoRotation); |
| 126 | RtpPacketReceived rtx_packet(&extension_map); |
| 127 | EXPECT_TRUE(rtx_packet.Parse( |
| 128 | rtc::ArrayView<const uint8_t>(kRtxPacketWithCVO))); |
| 129 | |
| 130 | VideoRotation rotation = kVideoRotation_0; |
| 131 | EXPECT_TRUE(rtx_packet.GetExtension<VideoOrientation>(&rotation)); |
| 132 | EXPECT_EQ(kVideoRotation_90, rotation); |
| 133 | |
| 134 | EXPECT_CALL(media_sink, OnRtpPacket(_)).WillOnce(testing::Invoke( |
| 135 | [](const RtpPacketReceived& packet) { |
| 136 | EXPECT_EQ(packet.SequenceNumber(), kMediaSeqno); |
| 137 | EXPECT_EQ(packet.Ssrc(), kMediaSSRC); |
| 138 | EXPECT_EQ(packet.PayloadType(), kMediaPayloadType); |
| 139 | EXPECT_THAT(packet.payload(), testing::ElementsAre(0xee)); |
| 140 | VideoRotation rotation = kVideoRotation_0; |
| 141 | EXPECT_TRUE(packet.GetExtension<VideoOrientation>(&rotation)); |
| 142 | EXPECT_EQ(rotation, kVideoRotation_90); |
| 143 | })); |
| 144 | |
| 145 | rtx_sink.OnRtpPacket(rtx_packet); |
| 146 | } |
| 147 | |
| 148 | } // namespace webrtc |