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