Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "modules/rtp_rtcp/source/contributing_sources.h" |
| 12 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 13 | #include "rtc_base/time_utils.h" |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 14 | |
| 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | |
| 21 | using ::testing::UnorderedElementsAre; |
| 22 | |
| 23 | constexpr uint32_t kCsrc1 = 111; |
| 24 | constexpr uint32_t kCsrc2 = 222; |
| 25 | constexpr uint32_t kCsrc3 = 333; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 26 | constexpr uint32_t kRtpTimestamp1 = 314; |
| 27 | constexpr uint32_t kRtpTimestamp2 = 315; |
| 28 | constexpr uint32_t kRtpTimestamp3 = 316; |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | TEST(ContributingSourcesTest, RecordSources) { |
| 33 | ContributingSources csrcs; |
| 34 | constexpr uint32_t kCsrcs[] = {kCsrc1, kCsrc2}; |
| 35 | constexpr int64_t kTime1 = 10; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 36 | csrcs.Update(kTime1, kCsrcs, absl::nullopt, kRtpTimestamp1); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 37 | EXPECT_THAT( |
| 38 | csrcs.GetSources(kTime1), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 39 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 40 | absl::nullopt, kRtpTimestamp1), |
| 41 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, |
| 42 | absl::nullopt, kRtpTimestamp1))); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(ContributingSourcesTest, UpdateSources) { |
| 46 | ContributingSources csrcs; |
| 47 | // TODO(nisse): When migrating to absl::Span, the named constant arrays should |
| 48 | // be replaced by unnamed literals where they are passed to csrcs.Update(...). |
| 49 | constexpr uint32_t kCsrcs1[] = {kCsrc1, kCsrc2}; |
| 50 | constexpr uint32_t kCsrcs2[] = {kCsrc2, kCsrc3}; |
| 51 | constexpr int64_t kTime1 = 10; |
| 52 | constexpr int64_t kTime2 = kTime1 + 5 * rtc::kNumMillisecsPerSec; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 53 | csrcs.Update(kTime1, kCsrcs1, absl::nullopt, kRtpTimestamp1); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 54 | EXPECT_THAT( |
| 55 | csrcs.GetSources(kTime1), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 56 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 57 | absl::nullopt, kRtpTimestamp1), |
| 58 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, |
| 59 | absl::nullopt, kRtpTimestamp1))); |
| 60 | csrcs.Update(kTime2, kCsrcs2, absl::nullopt, kRtpTimestamp2); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 61 | EXPECT_THAT( |
| 62 | csrcs.GetSources(kTime2), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 63 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 64 | absl::nullopt, kRtpTimestamp1), |
| 65 | RtpSource(kTime2, kCsrc2, RtpSourceType::CSRC, |
| 66 | absl::nullopt, kRtpTimestamp2), |
| 67 | RtpSource(kTime2, kCsrc3, RtpSourceType::CSRC, |
| 68 | absl::nullopt, kRtpTimestamp2))); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST(ContributingSourcesTest, ReturnRecentOnly) { |
| 72 | ContributingSources csrcs; |
| 73 | constexpr uint32_t kCsrcs1[] = {kCsrc1, kCsrc2}; |
| 74 | constexpr uint32_t kCsrcs2[] = {kCsrc2, kCsrc3}; |
| 75 | constexpr int64_t kTime1 = 10; |
| 76 | constexpr int64_t kTime2 = kTime1 + 5 * rtc::kNumMillisecsPerSec; |
| 77 | constexpr int64_t kTime3 = kTime1 + 12 * rtc::kNumMillisecsPerSec; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 78 | csrcs.Update(kTime1, kCsrcs1, absl::nullopt, kRtpTimestamp1); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 79 | EXPECT_THAT( |
| 80 | csrcs.GetSources(kTime1), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 81 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 82 | absl::nullopt, kRtpTimestamp1), |
| 83 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, |
| 84 | absl::nullopt, kRtpTimestamp1))); |
| 85 | csrcs.Update(kTime2, kCsrcs2, absl::nullopt, kRtpTimestamp2); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 86 | EXPECT_THAT( |
| 87 | csrcs.GetSources(kTime3), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 88 | UnorderedElementsAre(RtpSource(kTime2, kCsrc2, RtpSourceType::CSRC, |
| 89 | absl::nullopt, kRtpTimestamp2), |
| 90 | RtpSource(kTime2, kCsrc3, RtpSourceType::CSRC, |
| 91 | absl::nullopt, kRtpTimestamp2))); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST(ContributingSourcesTest, PurgeOldSources) { |
| 95 | ContributingSources csrcs; |
| 96 | constexpr uint32_t kCsrcs1[] = {kCsrc1, kCsrc2}; |
| 97 | constexpr uint32_t kCsrcs2[] = {kCsrc2, kCsrc3}; |
| 98 | constexpr int64_t kTime1 = 10; |
| 99 | constexpr int64_t kTime2 = kTime1 + 10 * rtc::kNumMillisecsPerSec; |
| 100 | constexpr int64_t kTime3 = kTime1 + 20 * rtc::kNumMillisecsPerSec; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 101 | csrcs.Update(kTime1, kCsrcs1, absl::nullopt, kRtpTimestamp1); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 102 | EXPECT_THAT( |
| 103 | csrcs.GetSources(kTime2), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 104 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 105 | absl::nullopt, kRtpTimestamp1), |
| 106 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, |
| 107 | absl::nullopt, kRtpTimestamp1))); |
| 108 | csrcs.Update(kTime2, kCsrcs2, absl::nullopt, kRtpTimestamp2); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 109 | EXPECT_THAT( |
| 110 | csrcs.GetSources(kTime2), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 111 | UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, |
| 112 | absl::nullopt, kRtpTimestamp1), |
| 113 | RtpSource(kTime2, kCsrc2, RtpSourceType::CSRC, |
| 114 | absl::nullopt, kRtpTimestamp2), |
| 115 | RtpSource(kTime2, kCsrc3, RtpSourceType::CSRC, |
| 116 | absl::nullopt, kRtpTimestamp2))); |
| 117 | csrcs.Update(kTime3, kCsrcs2, absl::nullopt, kRtpTimestamp3); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 118 | EXPECT_THAT( |
| 119 | csrcs.GetSources(kTime3), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 120 | UnorderedElementsAre(RtpSource(kTime3, kCsrc2, RtpSourceType::CSRC, |
| 121 | absl::nullopt, kRtpTimestamp3), |
| 122 | RtpSource(kTime3, kCsrc3, RtpSourceType::CSRC, |
| 123 | absl::nullopt, kRtpTimestamp3))); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 124 | // Query at an earlier time; check that old sources really have been purged |
| 125 | // and don't reappear. |
| 126 | EXPECT_THAT( |
| 127 | csrcs.GetSources(kTime2), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 128 | UnorderedElementsAre(RtpSource(kTime3, kCsrc2, RtpSourceType::CSRC, |
| 129 | absl::nullopt, kRtpTimestamp3), |
| 130 | RtpSource(kTime3, kCsrc3, RtpSourceType::CSRC, |
| 131 | absl::nullopt, kRtpTimestamp3))); |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Jonas Oreland | 967f7d5 | 2018-11-06 07:35:06 +0100 | [diff] [blame] | 134 | TEST(ContributingSourcesTest, AudioLevel) { |
| 135 | ContributingSources csrcs; |
| 136 | constexpr uint32_t kCsrcs[] = {kCsrc1, kCsrc2}; |
| 137 | constexpr int64_t kTime1 = 10; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 138 | csrcs.Update(kTime1, kCsrcs, 47, kRtpTimestamp1); |
Jonas Oreland | 967f7d5 | 2018-11-06 07:35:06 +0100 | [diff] [blame] | 139 | EXPECT_THAT( |
| 140 | csrcs.GetSources(kTime1), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 141 | UnorderedElementsAre( |
| 142 | RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC, 47, kRtpTimestamp1), |
| 143 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, 47, kRtpTimestamp1))); |
Jonas Oreland | 967f7d5 | 2018-11-06 07:35:06 +0100 | [diff] [blame] | 144 | |
| 145 | constexpr uint32_t kCsrcsSubset[] = {kCsrc1}; |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 146 | csrcs.Update(kTime1 + 1, kCsrcsSubset, absl::nullopt, kRtpTimestamp2); |
Jonas Oreland | 967f7d5 | 2018-11-06 07:35:06 +0100 | [diff] [blame] | 147 | EXPECT_THAT( |
| 148 | csrcs.GetSources(kTime1 + 1), |
Johannes Kron | b5d9183 | 2019-05-21 13:19:22 +0200 | [diff] [blame^] | 149 | UnorderedElementsAre( |
| 150 | RtpSource(kTime1 + 1, kCsrc1, RtpSourceType::CSRC, absl::nullopt, |
| 151 | kRtpTimestamp2), |
| 152 | RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC, 47, kRtpTimestamp1))); |
Jonas Oreland | 967f7d5 | 2018-11-06 07:35:06 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Niels Möller | af17595 | 2018-08-13 13:23:08 +0200 | [diff] [blame] | 155 | } // namespace webrtc |