blob: 65ab82b0dd752b92b65ecae5f6fa05b82af8d6ce [file] [log] [blame]
nisseeed52bf2017-05-19 06:15:19 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#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"
nisseeed52bf2017-05-19 06:15:19 -070018
19namespace webrtc {
20
21namespace {
22
23using ::testing::_;
24using ::testing::StrictMock;
25
nisseeed52bf2017-05-19 06:15:19 -070026constexpr int kMediaPayloadType = 100;
27constexpr int kRtxPayloadType = 98;
nisse38644992017-08-30 04:16:40 -070028constexpr int kUnknownPayloadType = 90;
nisseeed52bf2017-05-19 06:15:19 -070029constexpr uint32_t kMediaSSRC = 0x3333333;
30constexpr uint16_t kMediaSeqno = 0x5657;
31
32constexpr 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
44constexpr 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
58std::map<int, int> PayloadTypeMapping() {
nisse38644992017-08-30 04:16:40 -070059 const std::map<int, int> m = {{kRtxPayloadType, kMediaPayloadType}};
nisseeed52bf2017-05-19 06:15:19 -070060 return m;
61}
62
63template <typename T>
64rtc::ArrayView<T> Truncate(rtc::ArrayView<T> a, size_t drop) {
65 return a.subview(0, a.size() - drop);
66}
67
68} // namespace
69
70TEST(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
nisse38644992017-08-30 04:16:40 -070087TEST(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
nisseeed52bf2017-05-19 06:15:19 -0700101TEST(RtxReceiveStreamTest, IgnoresUnknownPayloadType) {
102 StrictMock<MockRtpPacketSink> media_sink;
nisse38644992017-08-30 04:16:40 -0700103 const std::map<int, int> payload_type_mapping = {
104 {kUnknownPayloadType, kMediaPayloadType}};
105
106 RtxReceiveStream rtx_sink(&media_sink, payload_type_mapping, kMediaSSRC);
nisseeed52bf2017-05-19 06:15:19 -0700107 RtpPacketReceived rtx_packet;
108 EXPECT_TRUE(rtx_packet.Parse(rtc::ArrayView<const uint8_t>(kRtxPacket)));
109 rtx_sink.OnRtpPacket(rtx_packet);
110}
111
112TEST(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
121TEST(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