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