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