brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 13 | #include "webrtc/base/array_view.h" |
brandtr | 7250b39 | 2016-12-19 01:13:46 -0800 | [diff] [blame] | 14 | #include "webrtc/call/flexfec_receive_stream_impl.h" |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 15 | #include "webrtc/modules/pacing/packet_router.h" |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 16 | #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h" |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/rtp_rtcp/mocks/mock_recovered_packet_receiver.h" |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 18 | #include "webrtc/modules/rtp_rtcp/mocks/mock_rtcp_rtt_stats.h" |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 19 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 20 | #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 21 | #include "webrtc/modules/utility/include/mock/mock_process_thread.h" |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 22 | #include "webrtc/test/gmock.h" |
| 23 | #include "webrtc/test/gtest.h" |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 24 | #include "webrtc/test/mock_transport.h" |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 30 | constexpr uint8_t kFlexfecPlType = 118; |
| 31 | constexpr uint8_t kFlexfecSsrc[] = {0x00, 0x00, 0x00, 0x01}; |
| 32 | constexpr uint8_t kMediaSsrc[] = {0x00, 0x00, 0x00, 0x02}; |
| 33 | |
| 34 | FlexfecReceiveStream::Config CreateDefaultConfig( |
| 35 | Transport* rtcp_send_transport) { |
| 36 | FlexfecReceiveStream::Config config(rtcp_send_transport); |
| 37 | config.payload_type = kFlexfecPlType; |
| 38 | config.remote_ssrc = ByteReader<uint32_t>::ReadBigEndian(kFlexfecSsrc); |
| 39 | config.protected_media_ssrcs = { |
| 40 | ByteReader<uint32_t>::ReadBigEndian(kMediaSsrc)}; |
| 41 | EXPECT_TRUE(config.IsCompleteAndEnabled()); |
| 42 | return config; |
| 43 | } |
| 44 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 45 | RtpPacketReceived ParsePacket(rtc::ArrayView<const uint8_t> packet) { |
| 46 | RtpPacketReceived parsed_packet(nullptr); |
| 47 | EXPECT_TRUE(parsed_packet.Parse(packet)); |
| 48 | return parsed_packet; |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 53 | TEST(FlexfecReceiveStreamConfigTest, IsCompleteAndEnabled) { |
| 54 | MockTransport rtcp_send_transport; |
| 55 | FlexfecReceiveStream::Config config(&rtcp_send_transport); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 56 | |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 57 | config.local_ssrc = 18374743; |
| 58 | config.rtcp_mode = RtcpMode::kCompound; |
| 59 | config.transport_cc = true; |
| 60 | config.rtp_header_extensions.emplace_back(TransportSequenceNumber::kUri, 7); |
| 61 | EXPECT_FALSE(config.IsCompleteAndEnabled()); |
| 62 | |
| 63 | config.payload_type = 123; |
| 64 | EXPECT_FALSE(config.IsCompleteAndEnabled()); |
| 65 | |
| 66 | config.remote_ssrc = 238423838; |
| 67 | EXPECT_FALSE(config.IsCompleteAndEnabled()); |
| 68 | |
| 69 | config.protected_media_ssrcs.push_back(138989393); |
| 70 | EXPECT_TRUE(config.IsCompleteAndEnabled()); |
| 71 | |
| 72 | config.protected_media_ssrcs.push_back(33423423); |
| 73 | EXPECT_FALSE(config.IsCompleteAndEnabled()); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 74 | } |
| 75 | |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 76 | class FlexfecReceiveStreamTest : public ::testing::Test { |
| 77 | protected: |
| 78 | FlexfecReceiveStreamTest() |
| 79 | : config_(CreateDefaultConfig(&rtcp_send_transport_)), |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 80 | receive_stream_(config_, |
| 81 | &recovered_packet_receiver_, |
| 82 | &rtt_stats_, |
| 83 | &process_thread_) {} |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 84 | |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 85 | MockTransport rtcp_send_transport_; |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 86 | FlexfecReceiveStream::Config config_; |
| 87 | MockRecoveredPacketReceiver recovered_packet_receiver_; |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 88 | MockRtcpRttStats rtt_stats_; |
| 89 | MockProcessThread process_thread_; |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 90 | |
| 91 | FlexfecReceiveStreamImpl receive_stream_; |
| 92 | }; |
| 93 | |
| 94 | TEST_F(FlexfecReceiveStreamTest, ConstructDestruct) {} |
| 95 | |
| 96 | TEST_F(FlexfecReceiveStreamTest, StartStop) { |
| 97 | receive_stream_.Start(); |
| 98 | receive_stream_.Stop(); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 99 | } |
| 100 | |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 101 | // Create a FlexFEC packet that protects a single media packet and ensure |
| 102 | // that the callback is called. Correctness of recovery is checked in the |
| 103 | // FlexfecReceiver unit tests. |
brandtr | 8313a6f | 2017-01-13 07:41:19 -0800 | [diff] [blame] | 104 | TEST_F(FlexfecReceiveStreamTest, RecoversPacketWhenStarted) { |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 105 | constexpr uint8_t kFlexfecSeqNum[] = {0x00, 0x01}; |
| 106 | constexpr uint8_t kFlexfecTs[] = {0x00, 0x11, 0x22, 0x33}; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 107 | constexpr uint8_t kMediaPlType = 107; |
| 108 | constexpr uint8_t kMediaSeqNum[] = {0x00, 0x02}; |
| 109 | constexpr uint8_t kMediaTs[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 110 | |
| 111 | // This packet mask protects a single media packet, i.e., the FlexFEC payload |
| 112 | // is a copy of that media packet. When inserted in the FlexFEC pipeline, |
| 113 | // it will thus trivially recover the lost media packet. |
| 114 | constexpr uint8_t kKBit0 = 1 << 7; |
| 115 | constexpr uint8_t kFlexfecPktMask[] = {kKBit0 | 0x00, 0x01}; |
| 116 | constexpr uint8_t kPayloadLength[] = {0x00, 0x04}; |
| 117 | constexpr uint8_t kSsrcCount = 1; |
| 118 | constexpr uint8_t kReservedBits = 0x00; |
| 119 | constexpr uint8_t kPayloadBits = 0x00; |
| 120 | // clang-format off |
| 121 | constexpr uint8_t kFlexfecPacket[] = { |
| 122 | // RTP header. |
| 123 | 0x80, kFlexfecPlType, kFlexfecSeqNum[0], kFlexfecSeqNum[1], |
| 124 | kFlexfecTs[0], kFlexfecTs[1], kFlexfecTs[2], kFlexfecTs[3], |
| 125 | kFlexfecSsrc[0], kFlexfecSsrc[1], kFlexfecSsrc[2], kFlexfecSsrc[3], |
| 126 | // FlexFEC header. |
| 127 | 0x00, kMediaPlType, kPayloadLength[0], kPayloadLength[1], |
| 128 | kMediaTs[0], kMediaTs[1], kMediaTs[2], kMediaTs[3], |
| 129 | kSsrcCount, kReservedBits, kReservedBits, kReservedBits, |
| 130 | kMediaSsrc[0], kMediaSsrc[1], kMediaSsrc[2], kMediaSsrc[3], |
| 131 | kMediaSeqNum[0], kMediaSeqNum[1], kFlexfecPktMask[0], kFlexfecPktMask[1], |
| 132 | // FEC payload. |
| 133 | kPayloadBits, kPayloadBits, kPayloadBits, kPayloadBits}; |
| 134 | // clang-format on |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 135 | |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 136 | testing::StrictMock<MockRecoveredPacketReceiver> recovered_packet_receiver; |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 137 | FlexfecReceiveStreamImpl receive_stream(config_, &recovered_packet_receiver, |
| 138 | &rtt_stats_, &process_thread_); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 139 | |
| 140 | // Do not call back before being started. |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 141 | receive_stream.AddAndProcessReceivedPacket(ParsePacket(kFlexfecPacket)); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 142 | |
| 143 | // Call back after being started. |
| 144 | receive_stream.Start(); |
| 145 | EXPECT_CALL( |
| 146 | recovered_packet_receiver, |
| 147 | OnRecoveredPacket(::testing::_, kRtpHeaderSize + kPayloadLength[1])); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 148 | receive_stream.AddAndProcessReceivedPacket(ParsePacket(kFlexfecPacket)); |
brandtr | 76648da | 2016-10-20 04:54:48 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace webrtc |