• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2015 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 <queue>
12 
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/format_macros.h"
15 #include "webrtc/base/timeutils.h"
16 #include "webrtc/system_wrappers/include/sleep.h"
17 #include "webrtc/test/testsupport/fileutils.h"
18 #include "webrtc/voice_engine/test/auto_test/fakes/conference_transport.h"
19 
20 namespace {
21 const int kRttMs = 25;
22 
IsNear(int ref,int comp,int error)23 bool IsNear(int ref, int comp, int error) {
24   return (ref - comp <= error) && (comp - ref >= -error);
25 }
26 
CreateSilenceFile(const std::string & silence_file,int sample_rate_hz)27 void CreateSilenceFile(const std::string& silence_file, int sample_rate_hz) {
28   FILE* fid = fopen(silence_file.c_str(), "wb");
29   int16_t zero = 0;
30   for (int i = 0; i < sample_rate_hz; ++i) {
31     // Write 1 second, but it does not matter since the file will be looped.
32     fwrite(&zero, sizeof(int16_t), 1, fid);
33   }
34   fclose(fid);
35 }
36 
37 }  // namespace
38 
39 namespace voetest {
40 
TEST(VoeConferenceTest,RttAndStartNtpTime)41 TEST(VoeConferenceTest, RttAndStartNtpTime) {
42   struct Stats {
43     Stats(int64_t rtt_receiver_1, int64_t rtt_receiver_2, int64_t ntp_delay)
44         : rtt_receiver_1_(rtt_receiver_1),
45           rtt_receiver_2_(rtt_receiver_2),
46           ntp_delay_(ntp_delay) {
47     }
48     int64_t rtt_receiver_1_;
49     int64_t rtt_receiver_2_;
50     int64_t ntp_delay_;
51   };
52 
53   const std::string input_file =
54       webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
55   const webrtc::FileFormats kInputFormat = webrtc::kFileFormatPcm32kHzFile;
56 
57   const int kDelayMs = 987;
58   ConferenceTransport trans;
59   trans.SetRtt(kRttMs);
60 
61   unsigned int id_1 = trans.AddStream(input_file, kInputFormat);
62   unsigned int id_2 = trans.AddStream(input_file, kInputFormat);
63 
64   EXPECT_TRUE(trans.StartPlayout(id_1));
65   // Start NTP time is the time when a stream is played out, rather than
66   // when it is added.
67   webrtc::SleepMs(kDelayMs);
68   EXPECT_TRUE(trans.StartPlayout(id_2));
69 
70   const int kMaxRunTimeMs = 25000;
71   const int kNeedSuccessivePass = 3;
72   const int kStatsRequestIntervalMs = 1000;
73   const int kStatsBufferSize = 3;
74 
75   uint32_t deadline = rtc::TimeAfter(kMaxRunTimeMs);
76   // Run the following up to |kMaxRunTimeMs| milliseconds.
77   int successive_pass = 0;
78   webrtc::CallStatistics stats_1;
79   webrtc::CallStatistics stats_2;
80   std::queue<Stats> stats_buffer;
81 
82   while (rtc::TimeIsLater(rtc::Time(), deadline) &&
83       successive_pass < kNeedSuccessivePass) {
84     webrtc::SleepMs(kStatsRequestIntervalMs);
85 
86     EXPECT_TRUE(trans.GetReceiverStatistics(id_1, &stats_1));
87     EXPECT_TRUE(trans.GetReceiverStatistics(id_2, &stats_2));
88 
89     // It is not easy to verify the NTP time directly. We verify it by testing
90     // the difference of two start NTP times.
91     int64_t captured_start_ntp_delay = stats_2.capture_start_ntp_time_ms_ -
92         stats_1.capture_start_ntp_time_ms_;
93 
94     // For the checks of RTT and start NTP time, We allow 10% accuracy.
95     if (IsNear(kRttMs, stats_1.rttMs, kRttMs / 10 + 1) &&
96         IsNear(kRttMs, stats_2.rttMs, kRttMs / 10 + 1) &&
97         IsNear(kDelayMs, captured_start_ntp_delay, kDelayMs / 10 + 1)) {
98       successive_pass++;
99     } else {
100       successive_pass = 0;
101     }
102     if (stats_buffer.size() >= kStatsBufferSize) {
103       stats_buffer.pop();
104     }
105     stats_buffer.push(Stats(stats_1.rttMs, stats_2.rttMs,
106                             captured_start_ntp_delay));
107   }
108 
109   EXPECT_GE(successive_pass, kNeedSuccessivePass) << "Expected to get RTT and"
110       " start NTP time estimate within 10% of the correct value over "
111       << kStatsRequestIntervalMs * kNeedSuccessivePass / 1000
112       << " seconds.";
113   if (successive_pass < kNeedSuccessivePass) {
114     printf("The most recent values (RTT for receiver 1, RTT for receiver 2, "
115         "NTP delay between receiver 1 and 2) are (from oldest):\n");
116     while (!stats_buffer.empty()) {
117       Stats stats = stats_buffer.front();
118       printf("(%" PRId64 ", %" PRId64 ", %" PRId64 ")\n", stats.rtt_receiver_1_,
119              stats.rtt_receiver_2_, stats.ntp_delay_);
120       stats_buffer.pop();
121     }
122   }
123 }
124 
125 
TEST(VoeConferenceTest,ReceivedPackets)126 TEST(VoeConferenceTest, ReceivedPackets) {
127   const int kPackets = 50;
128   const int kPacketDurationMs = 20;  // Correspond to Opus.
129 
130   const std::string input_file =
131       webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
132   const webrtc::FileFormats kInputFormat = webrtc::kFileFormatPcm32kHzFile;
133 
134   const std::string silence_file =
135       webrtc::test::TempFilename(webrtc::test::OutputPath(), "silence");
136   CreateSilenceFile(silence_file, 32000);
137 
138   {
139     ConferenceTransport trans;
140     // Add silence to stream 0, so that it will be filtered out.
141     unsigned int id_0 = trans.AddStream(silence_file, kInputFormat);
142     unsigned int id_1 = trans.AddStream(input_file, kInputFormat);
143     unsigned int id_2 = trans.AddStream(input_file, kInputFormat);
144     unsigned int id_3 = trans.AddStream(input_file, kInputFormat);
145 
146     EXPECT_TRUE(trans.StartPlayout(id_0));
147     EXPECT_TRUE(trans.StartPlayout(id_1));
148     EXPECT_TRUE(trans.StartPlayout(id_2));
149     EXPECT_TRUE(trans.StartPlayout(id_3));
150 
151     webrtc::SleepMs(kPacketDurationMs * kPackets);
152 
153     webrtc::CallStatistics stats_0;
154     webrtc::CallStatistics stats_1;
155     webrtc::CallStatistics stats_2;
156     webrtc::CallStatistics stats_3;
157     EXPECT_TRUE(trans.GetReceiverStatistics(id_0, &stats_0));
158     EXPECT_TRUE(trans.GetReceiverStatistics(id_1, &stats_1));
159     EXPECT_TRUE(trans.GetReceiverStatistics(id_2, &stats_2));
160     EXPECT_TRUE(trans.GetReceiverStatistics(id_3, &stats_3));
161 
162     // We expect stream 0 to be filtered out totally, but since it may join the
163     // call earlier than other streams and the beginning packets might have got
164     // through. So we only expect |packetsReceived| to be close to zero.
165     EXPECT_NEAR(stats_0.packetsReceived, 0, 2);
166     // We expect |packetsReceived| to match |kPackets|, but the actual value
167     // depends on the sleep timer. So we allow a small off from |kPackets|.
168     EXPECT_NEAR(stats_1.packetsReceived, kPackets, 2);
169     EXPECT_NEAR(stats_2.packetsReceived, kPackets, 2);
170     EXPECT_NEAR(stats_3.packetsReceived, kPackets, 2);
171   }
172 
173   remove(silence_file.c_str());
174 }
175 
176 }  // namespace voetest
177