blob: f90757584ee69054a27f1fc77e3a07947b73b5c2 [file] [log] [blame]
solenbergc7a8b082015-10-16 21:35:071/*
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
Fredrik Solenbergea073732015-12-01 10:26:3411#include <string>
12#include <vector>
13
solenbergc7a8b082015-10-16 21:35:0714#include "testing/gtest/include/gtest/gtest.h"
15
16#include "webrtc/audio/audio_send_stream.h"
solenberg566ef242015-11-06 23:34:4917#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 10:35:2118#include "webrtc/audio/conversion.h"
Stefan Holmerb86d4e42015-12-07 09:26:1819#include "webrtc/call/congestion_controller.h"
20#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
21#include "webrtc/modules/pacing/paced_sender.h"
solenberg13725082015-11-25 16:16:5222#include "webrtc/test/mock_voe_channel_proxy.h"
Fredrik Solenberg0ccae132015-11-03 09:15:4923#include "webrtc/test/mock_voice_engine.h"
Stefan Holmerb86d4e42015-12-07 09:26:1824#include "webrtc/video_engine/call_stats.h"
solenbergc7a8b082015-10-16 21:35:0725
26namespace webrtc {
solenberg85a04962015-10-27 10:35:2127namespace test {
Fredrik Solenberg0ccae132015-11-03 09:15:4928namespace {
29
solenberg3a941542015-11-16 15:34:5030using testing::_;
31using testing::Return;
32
Fredrik Solenberg0ccae132015-11-03 09:15:4933const int kChannelId = 1;
34const uint32_t kSsrc = 1234;
solenberg3a941542015-11-16 15:34:5035const char* kCName = "foo_name";
36const int kAudioLevelId = 2;
37const int kAbsSendTimeId = 3;
Stefan Holmerb86d4e42015-12-07 09:26:1838const int kTransportSequenceNumberId = 4;
solenberg566ef242015-11-06 23:34:4939const int kEchoDelayMedian = 254;
40const int kEchoDelayStdDev = -3;
41const int kEchoReturnLoss = -65;
42const int kEchoReturnLossEnhancement = 101;
43const unsigned int kSpeechInputLevel = 96;
44const CallStatistics kCallStats = {
45 1345, 1678, 1901, 1234, 112, 13456, 17890, 1567, -1890, -1123};
46const CodecInst kCodecInst = {-121, "codec_name_send", 48000, -231, -451, -671};
47const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
Fredrik Solenbergb5727682015-12-04 14:22:1948const int kTelephoneEventPayloadType = 123;
49const uint8_t kTelephoneEventCode = 45;
50const uint32_t kTelephoneEventDuration = 6789;
solenberg566ef242015-11-06 23:34:4951
52struct ConfigHelper {
Stefan Holmerb86d4e42015-12-07 09:26:1853 ConfigHelper()
54 : stream_config_(nullptr),
55 process_thread_(ProcessThread::Create("AudioTestThread")),
56 congestion_controller_(process_thread_.get(),
57 &call_stats_,
58 &bitrate_observer_) {
solenberg13725082015-11-25 16:16:5259 using testing::Invoke;
solenberg3a941542015-11-16 15:34:5060 using testing::StrEq;
61
solenberg566ef242015-11-06 23:34:4962 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 15:34:5063 RegisterVoiceEngineObserver(_)).WillOnce(Return(0));
solenberg566ef242015-11-06 23:34:4964 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 15:34:5065 DeRegisterVoiceEngineObserver()).WillOnce(Return(0));
solenberg566ef242015-11-06 23:34:4966 AudioState::Config config;
67 config.voice_engine = &voice_engine_;
68 audio_state_ = AudioState::Create(config);
solenberg3a941542015-11-16 15:34:5069
solenberg13725082015-11-25 16:16:5270 EXPECT_CALL(voice_engine_, ChannelProxyFactory(kChannelId))
71 .WillOnce(Invoke([this](int channel_id) {
72 EXPECT_FALSE(channel_proxy_);
73 channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
74 EXPECT_CALL(*channel_proxy_, SetRTCPStatus(true)).Times(1);
75 EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kSsrc)).Times(1);
76 EXPECT_CALL(*channel_proxy_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
solenberg358057b2015-11-27 18:46:4277 EXPECT_CALL(*channel_proxy_,
78 SetSendAbsoluteSenderTimeStatus(true, kAbsSendTimeId)).Times(1);
79 EXPECT_CALL(*channel_proxy_,
80 SetSendAudioLevelIndicationStatus(true, kAudioLevelId)).Times(1);
Stefan Holmerb86d4e42015-12-07 09:26:1881 EXPECT_CALL(*channel_proxy_, EnableSendTransportSequenceNumber(
82 kTransportSequenceNumberId))
83 .Times(1);
84 EXPECT_CALL(*channel_proxy_,
85 SetCongestionControlObjects(
86 congestion_controller_.pacer(),
87 congestion_controller_.GetTransportFeedbackObserver(),
88 congestion_controller_.packet_router()))
89 .Times(1);
90 EXPECT_CALL(*channel_proxy_,
91 SetCongestionControlObjects(nullptr, nullptr, nullptr))
92 .Times(1);
solenberg13725082015-11-25 16:16:5293 return channel_proxy_;
94 }));
solenberg566ef242015-11-06 23:34:4995 stream_config_.voe_channel_id = kChannelId;
96 stream_config_.rtp.ssrc = kSsrc;
solenberg3a941542015-11-16 15:34:5097 stream_config_.rtp.c_name = kCName;
98 stream_config_.rtp.extensions.push_back(
99 RtpExtension(RtpExtension::kAudioLevel, kAudioLevelId));
100 stream_config_.rtp.extensions.push_back(
101 RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeId));
Stefan Holmerb86d4e42015-12-07 09:26:18102 stream_config_.rtp.extensions.push_back(RtpExtension(
103 RtpExtension::kTransportSequenceNumber, kTransportSequenceNumberId));
solenberg566ef242015-11-06 23:34:49104 }
105
106 AudioSendStream::Config& config() { return stream_config_; }
107 rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
Stefan Holmerb86d4e42015-12-07 09:26:18108 CongestionController* congestion_controller() {
109 return &congestion_controller_;
110 }
solenberg566ef242015-11-06 23:34:49111
Fredrik Solenbergb5727682015-12-04 14:22:19112 void SetupMockForSendTelephoneEvent() {
113 EXPECT_TRUE(channel_proxy_);
114 EXPECT_CALL(*channel_proxy_,
115 SetSendTelephoneEventPayloadType(kTelephoneEventPayloadType))
116 .WillOnce(Return(true));
117 EXPECT_CALL(*channel_proxy_,
118 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
119 .WillOnce(Return(true));
120 }
121
solenberg566ef242015-11-06 23:34:49122 void SetupMockForGetStats() {
solenberg3a941542015-11-16 15:34:50123 using testing::DoAll;
solenberg3a941542015-11-16 15:34:50124 using testing::SetArgReferee;
125
solenberg566ef242015-11-06 23:34:49126 std::vector<ReportBlock> report_blocks;
127 webrtc::ReportBlock block = kReportBlock;
128 report_blocks.push_back(block); // Has wrong SSRC.
129 block.source_SSRC = kSsrc;
130 report_blocks.push_back(block); // Correct block.
131 block.fraction_lost = 0;
132 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
133
solenberg358057b2015-11-27 18:46:42134 EXPECT_TRUE(channel_proxy_);
135 EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
136 .WillRepeatedly(Return(kCallStats));
137 EXPECT_CALL(*channel_proxy_, GetRemoteRTCPReportBlocks())
138 .WillRepeatedly(Return(report_blocks));
139
solenberg566ef242015-11-06 23:34:49140 EXPECT_CALL(voice_engine_, GetSendCodec(kChannelId, _))
141 .WillRepeatedly(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
solenberg566ef242015-11-06 23:34:49142 EXPECT_CALL(voice_engine_, GetSpeechInputLevelFullRange(_))
143 .WillRepeatedly(DoAll(SetArgReferee<0>(kSpeechInputLevel), Return(0)));
144 EXPECT_CALL(voice_engine_, GetEcMetricsStatus(_))
145 .WillRepeatedly(DoAll(SetArgReferee<0>(true), Return(0)));
146 EXPECT_CALL(voice_engine_, GetEchoMetrics(_, _, _, _))
147 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoReturnLoss),
148 SetArgReferee<1>(kEchoReturnLossEnhancement),
149 Return(0)));
150 EXPECT_CALL(voice_engine_, GetEcDelayMetrics(_, _, _))
151 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoDelayMedian),
152 SetArgReferee<1>(kEchoDelayStdDev), Return(0)));
153 }
154
155 private:
Stefan Holmerb86d4e42015-12-07 09:26:18156 class NullBitrateObserver : public BitrateObserver {
157 public:
158 virtual void OnNetworkChanged(uint32_t bitrate_bps,
159 uint8_t fraction_loss,
160 int64_t rtt_ms) {}
161 };
162
solenberg3a941542015-11-16 15:34:50163 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 23:34:49164 rtc::scoped_refptr<AudioState> audio_state_;
165 AudioSendStream::Config stream_config_;
solenberg13725082015-11-25 16:16:52166 testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
Stefan Holmerb86d4e42015-12-07 09:26:18167 CallStats call_stats_;
168 NullBitrateObserver bitrate_observer_;
169 rtc::scoped_ptr<ProcessThread> process_thread_;
170 CongestionController congestion_controller_;
solenberg566ef242015-11-06 23:34:49171};
Fredrik Solenberg0ccae132015-11-03 09:15:49172} // namespace
solenbergc7a8b082015-10-16 21:35:07173
174TEST(AudioSendStreamTest, ConfigToString) {
solenbergc7a8b082015-10-16 21:35:07175 AudioSendStream::Config config(nullptr);
Fredrik Solenberg0ccae132015-11-03 09:15:49176 config.rtp.ssrc = kSsrc;
solenbergc7a8b082015-10-16 21:35:07177 config.rtp.extensions.push_back(
178 RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeId));
solenberg3a941542015-11-16 15:34:50179 config.rtp.c_name = kCName;
Fredrik Solenberg0ccae132015-11-03 09:15:49180 config.voe_channel_id = kChannelId;
solenbergc7a8b082015-10-16 21:35:07181 config.cng_payload_type = 42;
182 config.red_payload_type = 17;
Fredrik Solenberg0ccae132015-11-03 09:15:49183 EXPECT_EQ(
184 "{rtp: {ssrc: 1234, extensions: [{name: "
solenberg3a941542015-11-16 15:34:50185 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 3}], "
186 "c_name: foo_name}, voe_channel_id: 1, cng_payload_type: 42, "
187 "red_payload_type: 17}",
solenberg85a04962015-10-27 10:35:21188 config.ToString());
solenbergc7a8b082015-10-16 21:35:07189}
190
191TEST(AudioSendStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 23:34:49192 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 09:26:18193 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
194 helper.congestion_controller());
solenbergc7a8b082015-10-16 21:35:07195}
solenberg85a04962015-10-27 10:35:21196
Fredrik Solenbergb5727682015-12-04 14:22:19197TEST(AudioSendStreamTest, SendTelephoneEvent) {
198 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 09:26:18199 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
200 helper.congestion_controller());
Fredrik Solenbergb5727682015-12-04 14:22:19201 helper.SetupMockForSendTelephoneEvent();
202 EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
203 kTelephoneEventCode, kTelephoneEventDuration));
204}
205
solenberg85a04962015-10-27 10:35:21206TEST(AudioSendStreamTest, GetStats) {
solenberg566ef242015-11-06 23:34:49207 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 09:26:18208 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
209 helper.congestion_controller());
solenberg566ef242015-11-06 23:34:49210 helper.SetupMockForGetStats();
solenberg85a04962015-10-27 10:35:21211 AudioSendStream::Stats stats = send_stream.GetStats();
Fredrik Solenberg0ccae132015-11-03 09:15:49212 EXPECT_EQ(kSsrc, stats.local_ssrc);
213 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
214 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
215 EXPECT_EQ(static_cast<int32_t>(kReportBlock.cumulative_num_packets_lost),
solenberg85a04962015-10-27 10:35:21216 stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 09:15:49217 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
218 EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
219 EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number),
solenberg85a04962015-10-27 10:35:21220 stats.ext_seqnum);
Fredrik Solenberg0ccae132015-11-03 09:15:49221 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
222 (kCodecInst.plfreq / 1000)),
223 stats.jitter_ms);
224 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
225 EXPECT_EQ(static_cast<int32_t>(kSpeechInputLevel), stats.audio_level);
solenberg85a04962015-10-27 10:35:21226 EXPECT_EQ(-1, stats.aec_quality_min);
Fredrik Solenberg0ccae132015-11-03 09:15:49227 EXPECT_EQ(kEchoDelayMedian, stats.echo_delay_median_ms);
228 EXPECT_EQ(kEchoDelayStdDev, stats.echo_delay_std_ms);
229 EXPECT_EQ(kEchoReturnLoss, stats.echo_return_loss);
230 EXPECT_EQ(kEchoReturnLossEnhancement, stats.echo_return_loss_enhancement);
solenberg85a04962015-10-27 10:35:21231 EXPECT_FALSE(stats.typing_noise_detected);
232}
solenberg566ef242015-11-06 23:34:49233
234TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
235 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 09:26:18236 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
237 helper.congestion_controller());
solenberg566ef242015-11-06 23:34:49238 helper.SetupMockForGetStats();
239 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
240
241 internal::AudioState* internal_audio_state =
242 static_cast<internal::AudioState*>(helper.audio_state().get());
243 VoiceEngineObserver* voe_observer =
244 static_cast<VoiceEngineObserver*>(internal_audio_state);
245 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
246 EXPECT_TRUE(send_stream.GetStats().typing_noise_detected);
247 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
248 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
249}
solenberg85a04962015-10-27 10:35:21250} // namespace test
solenbergc7a8b082015-10-16 21:35:07251} // namespace webrtc