hbos | 8d609f6 | 2017-04-10 07:39:05 -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 | |
| 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 13 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/rtp_rtcp/include/rtp_header_parser.h" |
| 15 | #include "modules/rtp_rtcp/include/rtp_payload_registry.h" |
| 16 | #include "modules/rtp_rtcp/include/rtp_receiver.h" |
| 17 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 18 | #include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h" |
| 19 | #include "modules/rtp_rtcp/source/rtp_receiver_impl.h" |
| 20 | #include "test/gmock.h" |
| 21 | #include "test/gtest.h" |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | |
nisse | 7fcdb6d | 2017-06-01 00:30:55 -0700 | [diff] [blame] | 26 | using ::testing::NiceMock; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 27 | using ::testing::UnorderedElementsAre; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 28 | |
| 29 | const uint32_t kTestRate = 64000u; |
| 30 | const uint8_t kTestPayload[] = {'t', 'e', 's', 't'}; |
| 31 | const uint8_t kPcmuPayloadType = 96; |
| 32 | const int64_t kGetSourcesTimeoutMs = 10000; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 33 | const uint32_t kSsrc1 = 123; |
| 34 | const uint32_t kSsrc2 = 124; |
| 35 | const uint32_t kCsrc1 = 111; |
| 36 | const uint32_t kCsrc2 = 222; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 37 | |
| 38 | static uint32_t rtp_timestamp(int64_t time_ms) { |
| 39 | return static_cast<uint32_t>(time_ms * kTestRate / 1000); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 43 | |
| 44 | class RtpReceiverTest : public ::testing::Test { |
| 45 | protected: |
| 46 | RtpReceiverTest() |
| 47 | : fake_clock_(123456), |
| 48 | rtp_receiver_( |
| 49 | RtpReceiver::CreateAudioReceiver(&fake_clock_, |
nisse | 7fcdb6d | 2017-06-01 00:30:55 -0700 | [diff] [blame] | 50 | &mock_rtp_data_, |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 51 | &rtp_payload_registry_)) { |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 52 | rtp_receiver_->RegisterReceivePayload(kPcmuPayloadType, |
| 53 | SdpAudioFormat("PCMU", 8000, 1)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 54 | } |
| 55 | ~RtpReceiverTest() {} |
| 56 | |
| 57 | bool FindSourceByIdAndType(const std::vector<RtpSource>& sources, |
| 58 | uint32_t source_id, |
| 59 | RtpSourceType type, |
| 60 | RtpSource* source) { |
| 61 | for (size_t i = 0; i < sources.size(); ++i) { |
| 62 | if (sources[i].source_id() == source_id && |
| 63 | sources[i].source_type() == type) { |
| 64 | (*source) = sources[i]; |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | SimulatedClock fake_clock_; |
nisse | 7fcdb6d | 2017-06-01 00:30:55 -0700 | [diff] [blame] | 72 | NiceMock<MockRtpData> mock_rtp_data_; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 73 | RTPPayloadRegistry rtp_payload_registry_; |
| 74 | std::unique_ptr<RtpReceiver> rtp_receiver_; |
| 75 | }; |
| 76 | |
| 77 | TEST_F(RtpReceiverTest, GetSources) { |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 78 | int64_t now_ms = fake_clock_.TimeInMilliseconds(); |
| 79 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 80 | RTPHeader header; |
| 81 | header.payloadType = kPcmuPayloadType; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 82 | header.ssrc = kSsrc1; |
| 83 | header.timestamp = rtp_timestamp(now_ms); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 84 | header.numCSRCs = 2; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 85 | header.arrOfCSRCs[0] = kCsrc1; |
| 86 | header.arrOfCSRCs[1] = kCsrc2; |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 87 | const PayloadUnion payload_specific{ |
| 88 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 89 | |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 91 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 92 | auto sources = rtp_receiver_->GetSources(); |
| 93 | // One SSRC source and two CSRC sources. |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 94 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 95 | RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC), |
| 96 | RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC), |
| 97 | RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 98 | |
| 99 | // Advance the fake clock and the method is expected to return the |
| 100 | // contributing source object with same source id and updated timestamp. |
| 101 | fake_clock_.AdvanceTimeMilliseconds(1); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 103 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 104 | sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 105 | now_ms = fake_clock_.TimeInMilliseconds(); |
| 106 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 107 | RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC), |
| 108 | RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC), |
| 109 | RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 110 | |
| 111 | // Test the edge case that the sources are still there just before the |
| 112 | // timeout. |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 113 | int64_t prev_time_ms = fake_clock_.TimeInMilliseconds(); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 114 | fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs); |
| 115 | sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 116 | EXPECT_THAT(sources, |
| 117 | UnorderedElementsAre( |
| 118 | RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC), |
| 119 | RtpSource(prev_time_ms, kCsrc1, RtpSourceType::CSRC), |
| 120 | RtpSource(prev_time_ms, kCsrc2, RtpSourceType::CSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 121 | |
| 122 | // Time out. |
| 123 | fake_clock_.AdvanceTimeMilliseconds(1); |
| 124 | sources = rtp_receiver_->GetSources(); |
| 125 | // All the sources should be out of date. |
| 126 | ASSERT_EQ(0u, sources.size()); |
| 127 | } |
| 128 | |
| 129 | // Test the case that the SSRC is changed. |
| 130 | TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) { |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 131 | int64_t prev_time_ms = -1; |
| 132 | int64_t now_ms = fake_clock_.TimeInMilliseconds(); |
| 133 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 134 | RTPHeader header; |
| 135 | header.payloadType = kPcmuPayloadType; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 136 | header.ssrc = kSsrc1; |
| 137 | header.timestamp = rtp_timestamp(now_ms); |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 138 | const PayloadUnion payload_specific{ |
| 139 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 140 | |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 142 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 143 | auto sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 144 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 145 | RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 146 | |
| 147 | // The SSRC is changed and the old SSRC is expected to be returned. |
| 148 | fake_clock_.AdvanceTimeMilliseconds(100); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 149 | prev_time_ms = now_ms; |
| 150 | now_ms = fake_clock_.TimeInMilliseconds(); |
| 151 | header.ssrc = kSsrc2; |
| 152 | header.timestamp = rtp_timestamp(now_ms); |
| 153 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 154 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 155 | sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 156 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 157 | RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC), |
| 158 | RtpSource(now_ms, kSsrc2, RtpSourceType::SSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 159 | |
| 160 | // The SSRC is changed again and happen to be changed back to 1. No |
| 161 | // duplication is expected. |
| 162 | fake_clock_.AdvanceTimeMilliseconds(100); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 163 | header.ssrc = kSsrc1; |
| 164 | header.timestamp = rtp_timestamp(now_ms); |
| 165 | prev_time_ms = now_ms; |
| 166 | now_ms = fake_clock_.TimeInMilliseconds(); |
| 167 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 168 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 169 | sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 170 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 171 | RtpSource(prev_time_ms, kSsrc2, RtpSourceType::SSRC), |
| 172 | RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 173 | |
| 174 | // Old SSRC source timeout. |
| 175 | fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 176 | now_ms = fake_clock_.TimeInMilliseconds(); |
| 177 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 178 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 179 | sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 180 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 181 | RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC))); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(RtpReceiverTest, GetSourcesRemoveOutdatedSource) { |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 185 | int64_t now_ms = fake_clock_.TimeInMilliseconds(); |
| 186 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 187 | RTPHeader header; |
| 188 | header.payloadType = kPcmuPayloadType; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 189 | header.timestamp = rtp_timestamp(now_ms); |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 190 | const PayloadUnion payload_specific{ |
| 191 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 192 | header.numCSRCs = 1; |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 193 | size_t kSourceListSize = 20; |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 194 | |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 195 | for (size_t i = 0; i < kSourceListSize; ++i) { |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 196 | header.ssrc = i; |
| 197 | header.arrOfCSRCs[0] = (i + 1); |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 198 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 199 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 200 | } |
| 201 | |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 202 | RtpSource source(0, 0, RtpSourceType::SSRC); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 203 | auto sources = rtp_receiver_->GetSources(); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 204 | // Expect |kSourceListSize| SSRC sources and |kSourceListSize| CSRC sources. |
| 205 | ASSERT_EQ(2 * kSourceListSize, sources.size()); |
| 206 | for (size_t i = 0; i < kSourceListSize; ++i) { |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 207 | // The SSRC source IDs are expected to be 19, 18, 17 ... 0 |
| 208 | ASSERT_TRUE( |
| 209 | FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source)); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 210 | EXPECT_EQ(now_ms, source.timestamp_ms()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 211 | |
| 212 | // The CSRC source IDs are expected to be 20, 19, 18 ... 1 |
| 213 | ASSERT_TRUE( |
| 214 | FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source)); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 215 | EXPECT_EQ(now_ms, source.timestamp_ms()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 219 | for (size_t i = 0; i < kSourceListSize; ++i) { |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 220 | // The SSRC source IDs are expected to be 19, 18, 17 ... 0 |
| 221 | ASSERT_TRUE( |
| 222 | FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source)); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 223 | EXPECT_EQ(now_ms, source.timestamp_ms()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 224 | |
| 225 | // The CSRC source IDs are expected to be 20, 19, 18 ... 1 |
| 226 | ASSERT_TRUE( |
| 227 | FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source)); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 228 | EXPECT_EQ(now_ms, source.timestamp_ms()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Timeout. All the existing objects are out of date and are expected to be |
| 232 | // removed. |
| 233 | fake_clock_.AdvanceTimeMilliseconds(1); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 234 | header.ssrc = kSsrc1; |
| 235 | header.arrOfCSRCs[0] = kCsrc1; |
| 236 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 237 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
Danil Chapovalov | dd7e284 | 2018-03-09 15:37:03 +0000 | [diff] [blame] | 238 | auto* rtp_receiver_impl = static_cast<RtpReceiverImpl*>(rtp_receiver_.get()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 239 | auto ssrc_sources = rtp_receiver_impl->ssrc_sources_for_testing(); |
| 240 | ASSERT_EQ(1u, ssrc_sources.size()); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 241 | EXPECT_EQ(kSsrc1, ssrc_sources.begin()->source_id()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 242 | EXPECT_EQ(RtpSourceType::SSRC, ssrc_sources.begin()->source_type()); |
| 243 | EXPECT_EQ(fake_clock_.TimeInMilliseconds(), |
| 244 | ssrc_sources.begin()->timestamp_ms()); |
| 245 | |
| 246 | auto csrc_sources = rtp_receiver_impl->csrc_sources_for_testing(); |
| 247 | ASSERT_EQ(1u, csrc_sources.size()); |
zhihuang | 0426222 | 2017-04-11 11:28:10 -0700 | [diff] [blame] | 248 | EXPECT_EQ(kCsrc1, csrc_sources.begin()->source_id()); |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 249 | EXPECT_EQ(RtpSourceType::CSRC, csrc_sources.begin()->source_type()); |
| 250 | EXPECT_EQ(fake_clock_.TimeInMilliseconds(), |
| 251 | csrc_sources.begin()->timestamp_ms()); |
| 252 | } |
| 253 | |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 254 | // The audio level from the RTPHeader extension should be stored in the |
| 255 | // RtpSource with the matching SSRC. |
| 256 | TEST_F(RtpReceiverTest, GetSourcesContainsAudioLevelExtension) { |
| 257 | RTPHeader header; |
| 258 | int64_t time1_ms = fake_clock_.TimeInMilliseconds(); |
| 259 | header.payloadType = kPcmuPayloadType; |
| 260 | header.ssrc = kSsrc1; |
| 261 | header.timestamp = rtp_timestamp(time1_ms); |
| 262 | header.extension.hasAudioLevel = true; |
| 263 | header.extension.audioLevel = 10; |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 264 | const PayloadUnion payload_specific{ |
| 265 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 266 | |
| 267 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 268 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 269 | auto sources = rtp_receiver_->GetSources(); |
| 270 | EXPECT_THAT(sources, UnorderedElementsAre(RtpSource( |
| 271 | time1_ms, kSsrc1, RtpSourceType::SSRC, 10))); |
| 272 | |
| 273 | // Receive a packet from a different SSRC with a different level and check |
| 274 | // that they are both remembered. |
| 275 | fake_clock_.AdvanceTimeMilliseconds(1); |
| 276 | int64_t time2_ms = fake_clock_.TimeInMilliseconds(); |
| 277 | header.ssrc = kSsrc2; |
| 278 | header.timestamp = rtp_timestamp(time2_ms); |
| 279 | header.extension.hasAudioLevel = true; |
| 280 | header.extension.audioLevel = 20; |
| 281 | |
| 282 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 283 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 284 | sources = rtp_receiver_->GetSources(); |
| 285 | EXPECT_THAT(sources, |
| 286 | UnorderedElementsAre( |
| 287 | RtpSource(time1_ms, kSsrc1, RtpSourceType::SSRC, 10), |
| 288 | RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20))); |
| 289 | |
| 290 | // Receive a packet from the first SSRC again and check that the level is |
| 291 | // updated. |
| 292 | fake_clock_.AdvanceTimeMilliseconds(1); |
| 293 | int64_t time3_ms = fake_clock_.TimeInMilliseconds(); |
| 294 | header.ssrc = kSsrc1; |
| 295 | header.timestamp = rtp_timestamp(time3_ms); |
| 296 | header.extension.hasAudioLevel = true; |
| 297 | header.extension.audioLevel = 30; |
| 298 | |
| 299 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 300 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 301 | sources = rtp_receiver_->GetSources(); |
| 302 | EXPECT_THAT(sources, |
| 303 | UnorderedElementsAre( |
| 304 | RtpSource(time3_ms, kSsrc1, RtpSourceType::SSRC, 30), |
| 305 | RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20))); |
| 306 | } |
| 307 | |
| 308 | TEST_F(RtpReceiverTest, |
| 309 | MissingAudioLevelHeaderExtensionClearsRtpSourceAudioLevel) { |
| 310 | RTPHeader header; |
| 311 | int64_t time1_ms = fake_clock_.TimeInMilliseconds(); |
| 312 | header.payloadType = kPcmuPayloadType; |
| 313 | header.ssrc = kSsrc1; |
| 314 | header.timestamp = rtp_timestamp(time1_ms); |
| 315 | header.extension.hasAudioLevel = true; |
| 316 | header.extension.audioLevel = 10; |
Karl Wiberg | c62f6c7 | 2017-10-04 12:38:53 +0200 | [diff] [blame] | 317 | const PayloadUnion payload_specific{ |
| 318 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 319 | |
| 320 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 321 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 322 | auto sources = rtp_receiver_->GetSources(); |
| 323 | EXPECT_THAT(sources, UnorderedElementsAre(RtpSource( |
| 324 | time1_ms, kSsrc1, RtpSourceType::SSRC, 10))); |
| 325 | |
| 326 | // Receive a second packet without the audio level header extension and check |
| 327 | // that the audio level is cleared. |
| 328 | fake_clock_.AdvanceTimeMilliseconds(1); |
| 329 | int64_t time2_ms = fake_clock_.TimeInMilliseconds(); |
| 330 | header.timestamp = rtp_timestamp(time2_ms); |
| 331 | header.extension.hasAudioLevel = false; |
| 332 | |
| 333 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 334 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
zstein | 2b70634 | 2017-08-24 14:52:17 -0700 | [diff] [blame] | 335 | sources = rtp_receiver_->GetSources(); |
| 336 | EXPECT_THAT(sources, UnorderedElementsAre( |
| 337 | RtpSource(time2_ms, kSsrc1, RtpSourceType::SSRC))); |
| 338 | } |
| 339 | |
Niels Möller | 22ec952 | 2017-10-05 08:39:15 +0200 | [diff] [blame] | 340 | TEST_F(RtpReceiverTest, UpdatesTimestampsIfAndOnlyIfPacketArrivesInOrder) { |
| 341 | RTPHeader header; |
| 342 | int64_t time1_ms = fake_clock_.TimeInMilliseconds(); |
| 343 | header.payloadType = kPcmuPayloadType; |
| 344 | header.ssrc = kSsrc1; |
| 345 | header.timestamp = rtp_timestamp(time1_ms); |
| 346 | header.extension.hasAudioLevel = true; |
| 347 | header.extension.audioLevel = 10; |
| 348 | header.sequenceNumber = 0xfff0; |
| 349 | |
| 350 | const PayloadUnion payload_specific{ |
| 351 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
| 352 | uint32_t latest_timestamp; |
| 353 | int64_t latest_receive_time_ms; |
| 354 | |
| 355 | // No packet received yet. |
| 356 | EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 357 | &latest_receive_time_ms)); |
| 358 | // Initial packet |
| 359 | const uint32_t timestamp_1 = header.timestamp; |
| 360 | const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds(); |
| 361 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 362 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 363 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 364 | &latest_receive_time_ms)); |
| 365 | EXPECT_EQ(latest_timestamp, timestamp_1); |
| 366 | EXPECT_EQ(latest_receive_time_ms, receive_time_1); |
| 367 | |
| 368 | // Late packet, timestamp not recorded. |
| 369 | fake_clock_.AdvanceTimeMilliseconds(10); |
| 370 | header.timestamp -= 900; |
| 371 | header.sequenceNumber -= 2; |
| 372 | |
| 373 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 374 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 375 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 376 | &latest_receive_time_ms)); |
| 377 | EXPECT_EQ(latest_timestamp, timestamp_1); |
| 378 | EXPECT_EQ(latest_receive_time_ms, receive_time_1); |
| 379 | |
| 380 | // New packet, still late, no wraparound. |
| 381 | fake_clock_.AdvanceTimeMilliseconds(10); |
| 382 | header.timestamp += 1800; |
| 383 | header.sequenceNumber += 1; |
| 384 | |
| 385 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 386 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 387 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 388 | &latest_receive_time_ms)); |
| 389 | EXPECT_EQ(latest_timestamp, timestamp_1); |
| 390 | EXPECT_EQ(latest_receive_time_ms, receive_time_1); |
| 391 | |
| 392 | // New packet, new timestamp recorded |
| 393 | fake_clock_.AdvanceTimeMilliseconds(10); |
| 394 | header.timestamp += 900; |
| 395 | header.sequenceNumber += 2; |
| 396 | const uint32_t timestamp_2 = header.timestamp; |
| 397 | const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds(); |
| 398 | const uint16_t seqno_2 = header.sequenceNumber; |
| 399 | |
| 400 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 401 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 402 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 403 | &latest_receive_time_ms)); |
| 404 | EXPECT_EQ(latest_timestamp, timestamp_2); |
| 405 | EXPECT_EQ(latest_receive_time_ms, receive_time_2); |
| 406 | |
| 407 | // New packet, timestamp wraps around |
| 408 | fake_clock_.AdvanceTimeMilliseconds(10); |
| 409 | header.timestamp += 900; |
| 410 | header.sequenceNumber += 20; |
| 411 | const uint32_t timestamp_3 = header.timestamp; |
| 412 | const int64_t receive_time_3 = fake_clock_.TimeInMilliseconds(); |
| 413 | EXPECT_LT(header.sequenceNumber, seqno_2); // Wrap-around |
| 414 | |
| 415 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 416 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 417 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 418 | &latest_receive_time_ms)); |
| 419 | EXPECT_EQ(latest_timestamp, timestamp_3); |
| 420 | EXPECT_EQ(latest_receive_time_ms, receive_time_3); |
| 421 | } |
| 422 | |
| 423 | TEST_F(RtpReceiverTest, UpdatesTimestampsWhenStreamResets) { |
| 424 | RTPHeader header; |
| 425 | int64_t time1_ms = fake_clock_.TimeInMilliseconds(); |
| 426 | header.payloadType = kPcmuPayloadType; |
| 427 | header.ssrc = kSsrc1; |
| 428 | header.timestamp = rtp_timestamp(time1_ms); |
| 429 | header.extension.hasAudioLevel = true; |
| 430 | header.extension.audioLevel = 10; |
| 431 | header.sequenceNumber = 0xfff0; |
| 432 | |
| 433 | const PayloadUnion payload_specific{ |
| 434 | AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}}; |
| 435 | uint32_t latest_timestamp; |
| 436 | int64_t latest_receive_time_ms; |
| 437 | |
| 438 | // No packet received yet. |
| 439 | EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 440 | &latest_receive_time_ms)); |
| 441 | // Initial packet |
| 442 | const uint32_t timestamp_1 = header.timestamp; |
| 443 | const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds(); |
| 444 | const uint16_t seqno_1 = header.sequenceNumber; |
| 445 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 446 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 447 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 448 | &latest_receive_time_ms)); |
| 449 | EXPECT_EQ(latest_timestamp, timestamp_1); |
| 450 | EXPECT_EQ(latest_receive_time_ms, receive_time_1); |
| 451 | |
| 452 | // Packet with far in the past seqno, but unlikely to be a wrap-around. |
| 453 | // Treated as a seqno discontinuity, and timestamp is recorded. |
| 454 | fake_clock_.AdvanceTimeMilliseconds(10); |
| 455 | header.timestamp += 900; |
| 456 | header.sequenceNumber = 0x9000; |
| 457 | |
| 458 | const uint32_t timestamp_2 = header.timestamp; |
| 459 | const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds(); |
| 460 | const uint16_t seqno_2 = header.sequenceNumber; |
| 461 | EXPECT_LT(seqno_1 - seqno_2, 0x8000); // In the past. |
| 462 | |
| 463 | EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket( |
| 464 | header, kTestPayload, sizeof(kTestPayload), payload_specific)); |
| 465 | EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp, |
| 466 | &latest_receive_time_ms)); |
| 467 | EXPECT_EQ(latest_timestamp, timestamp_2); |
| 468 | EXPECT_EQ(latest_receive_time_ms, receive_time_2); |
| 469 | } |
| 470 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 471 | } // namespace webrtc |