blob: 38d25cebc962244bf8bb2b3e6af5db00467a9482 [file] [log] [blame]
Niels Mölleraf175952018-08-13 13:23:08 +02001/*
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 Anton10542f22019-01-11 09:11:00 -080013#include "rtc_base/time_utils.h"
Niels Mölleraf175952018-08-13 13:23:08 +020014
15#include "test/gmock.h"
16#include "test/gtest.h"
17
18namespace webrtc {
19namespace {
20
21using ::testing::UnorderedElementsAre;
22
23constexpr uint32_t kCsrc1 = 111;
24constexpr uint32_t kCsrc2 = 222;
25constexpr uint32_t kCsrc3 = 333;
26
27} // namespace
28
29TEST(ContributingSourcesTest, RecordSources) {
30 ContributingSources csrcs;
31 constexpr uint32_t kCsrcs[] = {kCsrc1, kCsrc2};
32 constexpr int64_t kTime1 = 10;
Jonas Oreland967f7d52018-11-06 07:35:06 +010033 csrcs.Update(kTime1, kCsrcs, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020034 EXPECT_THAT(
35 csrcs.GetSources(kTime1),
36 UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC),
37 RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC)));
38}
39
40TEST(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 Oreland967f7d52018-11-06 07:35:06 +010048 csrcs.Update(kTime1, kCsrcs1, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020049 EXPECT_THAT(
50 csrcs.GetSources(kTime1),
51 UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC),
52 RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC)));
Jonas Oreland967f7d52018-11-06 07:35:06 +010053 csrcs.Update(kTime2, kCsrcs2, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020054 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
61TEST(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 Oreland967f7d52018-11-06 07:35:06 +010068 csrcs.Update(kTime1, kCsrcs1, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020069 EXPECT_THAT(
70 csrcs.GetSources(kTime1),
71 UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC),
72 RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC)));
Jonas Oreland967f7d52018-11-06 07:35:06 +010073 csrcs.Update(kTime2, kCsrcs2, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020074 EXPECT_THAT(
75 csrcs.GetSources(kTime3),
76 UnorderedElementsAre(RtpSource(kTime2, kCsrc2, RtpSourceType::CSRC),
77 RtpSource(kTime2, kCsrc3, RtpSourceType::CSRC)));
78}
79
80TEST(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 Oreland967f7d52018-11-06 07:35:06 +010087 csrcs.Update(kTime1, kCsrcs1, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020088 EXPECT_THAT(
89 csrcs.GetSources(kTime2),
90 UnorderedElementsAre(RtpSource(kTime1, kCsrc1, RtpSourceType::CSRC),
91 RtpSource(kTime1, kCsrc2, RtpSourceType::CSRC)));
Jonas Oreland967f7d52018-11-06 07:35:06 +010092 csrcs.Update(kTime2, kCsrcs2, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020093 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 Oreland967f7d52018-11-06 07:35:06 +010098 csrcs.Update(kTime3, kCsrcs2, absl::nullopt);
Niels Mölleraf175952018-08-13 13:23:08 +020099 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 Oreland967f7d52018-11-06 07:35:06 +0100111TEST(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ölleraf175952018-08-13 13:23:08 +0200129} // namespace webrtc