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