blob: 7f940fc767f85e4d6f391308d84a7e6213ce1eda [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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 11:26:34 +010011#include <string>
12#include <vector>
13
solenbergc7a8b082015-10-16 14:35:07 -070014#include "testing/gtest/include/gtest/gtest.h"
15
16#include "webrtc/audio/audio_send_stream.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 03:35:21 -070018#include "webrtc/audio/conversion.h"
perkjec81bcd2016-05-11 06:01:13 -070019#include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h"
ivoc14d5dbe2016-07-04 07:06:55 -070020#include "webrtc/call/mock/mock_rtc_event_log.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010021#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010022#include "webrtc/modules/pacing/paced_sender.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010023#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080024#include "webrtc/test/mock_voe_channel_proxy.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010025#include "webrtc/test/mock_voice_engine.h"
solenbergc7a8b082015-10-16 14:35:07 -070026
27namespace webrtc {
solenberg85a04962015-10-27 03:35:21 -070028namespace test {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010029namespace {
30
solenberg3a941542015-11-16 07:34:50 -080031using testing::_;
32using testing::Return;
33
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010034const int kChannelId = 1;
35const uint32_t kSsrc = 1234;
solenberg3a941542015-11-16 07:34:50 -080036const char* kCName = "foo_name";
37const int kAudioLevelId = 2;
38const int kAbsSendTimeId = 3;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010039const int kTransportSequenceNumberId = 4;
solenberg566ef242015-11-06 15:34:49 -080040const int kEchoDelayMedian = 254;
41const int kEchoDelayStdDev = -3;
42const int kEchoReturnLoss = -65;
43const int kEchoReturnLossEnhancement = 101;
44const unsigned int kSpeechInputLevel = 96;
45const CallStatistics kCallStats = {
46 1345, 1678, 1901, 1234, 112, 13456, 17890, 1567, -1890, -1123};
pkasting25702cb2016-01-08 13:50:27 -080047const CodecInst kCodecInst = {-121, "codec_name_send", 48000, -231, 0, -671};
solenberg566ef242015-11-06 15:34:49 -080048const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
Fredrik Solenbergb5727682015-12-04 15:22:19 +010049const int kTelephoneEventPayloadType = 123;
solenberg8842c3e2016-03-11 03:06:41 -080050const int kTelephoneEventCode = 45;
51const int kTelephoneEventDuration = 6789;
solenberg566ef242015-11-06 15:34:49 -080052
mflodman86cc6ff2016-07-26 04:44:06 -070053class MockLimitObserver : public BitrateAllocator::LimitObserver {
54 public:
55 MOCK_METHOD2(OnAllocationLimitsChanged,
56 void(uint32_t min_send_bitrate_bps,
57 uint32_t max_padding_bitrate_bps));
58};
59
solenberg566ef242015-11-06 15:34:49 -080060struct ConfigHelper {
Stefan Holmerb86d4e42015-12-07 10:26:18 +010061 ConfigHelper()
Stefan Holmer58c664c2016-02-08 14:31:30 +010062 : simulated_clock_(123456),
63 stream_config_(nullptr),
Stefan Holmer58c664c2016-02-08 14:31:30 +010064 congestion_controller_(&simulated_clock_,
Stefan Holmer58c664c2016-02-08 14:31:30 +010065 &bitrate_observer_,
ivoc14d5dbe2016-07-04 07:06:55 -070066 &remote_bitrate_observer_,
mflodman86cc6ff2016-07-26 04:44:06 -070067 &event_log_),
perkj8eb37a32016-08-16 02:40:55 -070068 bitrate_allocator_(&limit_observer_) {
solenberg13725082015-11-25 08:16:52 -080069 using testing::Invoke;
solenberg3a941542015-11-16 07:34:50 -080070 using testing::StrEq;
71
solenberg566ef242015-11-06 15:34:49 -080072 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080073 RegisterVoiceEngineObserver(_)).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080074 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080075 DeRegisterVoiceEngineObserver()).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080076 AudioState::Config config;
77 config.voice_engine = &voice_engine_;
78 audio_state_ = AudioState::Create(config);
solenberg3a941542015-11-16 07:34:50 -080079
solenberg13725082015-11-25 08:16:52 -080080 EXPECT_CALL(voice_engine_, ChannelProxyFactory(kChannelId))
81 .WillOnce(Invoke([this](int channel_id) {
82 EXPECT_FALSE(channel_proxy_);
83 channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
84 EXPECT_CALL(*channel_proxy_, SetRTCPStatus(true)).Times(1);
85 EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kSsrc)).Times(1);
86 EXPECT_CALL(*channel_proxy_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
solenberg971cab02016-06-14 10:02:41 -070087 EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 10)).Times(1);
solenberg358057b2015-11-27 10:46:42 -080088 EXPECT_CALL(*channel_proxy_,
89 SetSendAbsoluteSenderTimeStatus(true, kAbsSendTimeId)).Times(1);
90 EXPECT_CALL(*channel_proxy_,
91 SetSendAudioLevelIndicationStatus(true, kAudioLevelId)).Times(1);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010092 EXPECT_CALL(*channel_proxy_, EnableSendTransportSequenceNumber(
93 kTransportSequenceNumberId))
94 .Times(1);
95 EXPECT_CALL(*channel_proxy_,
stefanbba9dec2016-02-01 04:39:55 -080096 RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010097 congestion_controller_.pacer(),
98 congestion_controller_.GetTransportFeedbackObserver(),
99 congestion_controller_.packet_router()))
100 .Times(1);
stefanbba9dec2016-02-01 04:39:55 -0800101 EXPECT_CALL(*channel_proxy_, ResetCongestionControlObjects())
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100102 .Times(1);
mflodman3d7db262016-04-29 00:57:13 -0700103 EXPECT_CALL(*channel_proxy_, RegisterExternalTransport(nullptr))
104 .Times(1);
105 EXPECT_CALL(*channel_proxy_, DeRegisterExternalTransport())
106 .Times(1);
solenberg13725082015-11-25 08:16:52 -0800107 return channel_proxy_;
108 }));
solenberg566ef242015-11-06 15:34:49 -0800109 stream_config_.voe_channel_id = kChannelId;
110 stream_config_.rtp.ssrc = kSsrc;
solenberg971cab02016-06-14 10:02:41 -0700111 stream_config_.rtp.nack.rtp_history_ms = 200;
solenberg3a941542015-11-16 07:34:50 -0800112 stream_config_.rtp.c_name = kCName;
113 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700114 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
solenberg3a941542015-11-16 07:34:50 -0800115 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700116 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100117 stream_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700118 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
solenberg566ef242015-11-06 15:34:49 -0800119 }
120
121 AudioSendStream::Config& config() { return stream_config_; }
122 rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
solenberg94218532016-06-16 10:53:22 -0700123 MockVoEChannelProxy* channel_proxy() { return channel_proxy_; }
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100124 CongestionController* congestion_controller() {
125 return &congestion_controller_;
126 }
mflodman86cc6ff2016-07-26 04:44:06 -0700127 BitrateAllocator* bitrate_allocator() { return &bitrate_allocator_; }
solenberg566ef242015-11-06 15:34:49 -0800128
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100129 void SetupMockForSendTelephoneEvent() {
130 EXPECT_TRUE(channel_proxy_);
131 EXPECT_CALL(*channel_proxy_,
132 SetSendTelephoneEventPayloadType(kTelephoneEventPayloadType))
133 .WillOnce(Return(true));
134 EXPECT_CALL(*channel_proxy_,
135 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
136 .WillOnce(Return(true));
137 }
138
solenberg566ef242015-11-06 15:34:49 -0800139 void SetupMockForGetStats() {
solenberg3a941542015-11-16 07:34:50 -0800140 using testing::DoAll;
solenberg3a941542015-11-16 07:34:50 -0800141 using testing::SetArgReferee;
142
solenberg566ef242015-11-06 15:34:49 -0800143 std::vector<ReportBlock> report_blocks;
144 webrtc::ReportBlock block = kReportBlock;
145 report_blocks.push_back(block); // Has wrong SSRC.
146 block.source_SSRC = kSsrc;
147 report_blocks.push_back(block); // Correct block.
148 block.fraction_lost = 0;
149 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
150
solenberg358057b2015-11-27 10:46:42 -0800151 EXPECT_TRUE(channel_proxy_);
152 EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
153 .WillRepeatedly(Return(kCallStats));
154 EXPECT_CALL(*channel_proxy_, GetRemoteRTCPReportBlocks())
155 .WillRepeatedly(Return(report_blocks));
156
solenberg566ef242015-11-06 15:34:49 -0800157 EXPECT_CALL(voice_engine_, GetSendCodec(kChannelId, _))
158 .WillRepeatedly(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
solenberg566ef242015-11-06 15:34:49 -0800159 EXPECT_CALL(voice_engine_, GetSpeechInputLevelFullRange(_))
160 .WillRepeatedly(DoAll(SetArgReferee<0>(kSpeechInputLevel), Return(0)));
161 EXPECT_CALL(voice_engine_, GetEcMetricsStatus(_))
162 .WillRepeatedly(DoAll(SetArgReferee<0>(true), Return(0)));
163 EXPECT_CALL(voice_engine_, GetEchoMetrics(_, _, _, _))
164 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoReturnLoss),
165 SetArgReferee<1>(kEchoReturnLossEnhancement),
166 Return(0)));
167 EXPECT_CALL(voice_engine_, GetEcDelayMetrics(_, _, _))
168 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoDelayMedian),
169 SetArgReferee<1>(kEchoDelayStdDev), Return(0)));
170 }
171
172 private:
Stefan Holmer58c664c2016-02-08 14:31:30 +0100173 SimulatedClock simulated_clock_;
solenberg3a941542015-11-16 07:34:50 -0800174 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -0800175 rtc::scoped_refptr<AudioState> audio_state_;
176 AudioSendStream::Config stream_config_;
solenberg13725082015-11-25 08:16:52 -0800177 testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
perkjec81bcd2016-05-11 06:01:13 -0700178 testing::NiceMock<MockCongestionObserver> bitrate_observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100179 testing::NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100180 CongestionController congestion_controller_;
ivoc14d5dbe2016-07-04 07:06:55 -0700181 MockRtcEventLog event_log_;
mflodman86cc6ff2016-07-26 04:44:06 -0700182 testing::NiceMock<MockLimitObserver> limit_observer_;
183 BitrateAllocator bitrate_allocator_;
solenberg566ef242015-11-06 15:34:49 -0800184};
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100185} // namespace
solenbergc7a8b082015-10-16 14:35:07 -0700186
187TEST(AudioSendStreamTest, ConfigToString) {
solenbergc7a8b082015-10-16 14:35:07 -0700188 AudioSendStream::Config config(nullptr);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100189 config.rtp.ssrc = kSsrc;
solenbergc7a8b082015-10-16 14:35:07 -0700190 config.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700191 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
solenberg3a941542015-11-16 07:34:50 -0800192 config.rtp.c_name = kCName;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100193 config.voe_channel_id = kChannelId;
solenbergc7a8b082015-10-16 14:35:07 -0700194 config.cng_payload_type = 42;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100195 EXPECT_EQ(
isheriff6f8d6862016-05-26 11:24:55 -0700196 "{rtp: {ssrc: 1234, extensions: [{uri: "
solenberg3a941542015-11-16 07:34:50 -0800197 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 3}], "
solenberg971cab02016-06-14 10:02:41 -0700198 "nack: {rtp_history_ms: 0}, c_name: foo_name}, voe_channel_id: 1, "
199 "cng_payload_type: 42}",
solenberg85a04962015-10-27 03:35:21 -0700200 config.ToString());
solenbergc7a8b082015-10-16 14:35:07 -0700201}
202
203TEST(AudioSendStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 15:34:49 -0800204 ConfigHelper helper;
perkj8eb37a32016-08-16 02:40:55 -0700205 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
206 helper.congestion_controller(),
207 helper.bitrate_allocator());
solenbergc7a8b082015-10-16 14:35:07 -0700208}
solenberg85a04962015-10-27 03:35:21 -0700209
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100210TEST(AudioSendStreamTest, SendTelephoneEvent) {
211 ConfigHelper helper;
perkj8eb37a32016-08-16 02:40:55 -0700212 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
213 helper.congestion_controller(),
214 helper.bitrate_allocator());
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100215 helper.SetupMockForSendTelephoneEvent();
216 EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
217 kTelephoneEventCode, kTelephoneEventDuration));
218}
219
solenberg94218532016-06-16 10:53:22 -0700220TEST(AudioSendStreamTest, SetMuted) {
221 ConfigHelper helper;
perkj8eb37a32016-08-16 02:40:55 -0700222 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
223 helper.congestion_controller(),
224 helper.bitrate_allocator());
solenberg94218532016-06-16 10:53:22 -0700225 EXPECT_CALL(*helper.channel_proxy(), SetInputMute(true));
226 send_stream.SetMuted(true);
227}
228
solenberg85a04962015-10-27 03:35:21 -0700229TEST(AudioSendStreamTest, GetStats) {
solenberg566ef242015-11-06 15:34:49 -0800230 ConfigHelper helper;
perkj8eb37a32016-08-16 02:40:55 -0700231 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
232 helper.congestion_controller(),
233 helper.bitrate_allocator());
solenberg566ef242015-11-06 15:34:49 -0800234 helper.SetupMockForGetStats();
solenberg85a04962015-10-27 03:35:21 -0700235 AudioSendStream::Stats stats = send_stream.GetStats();
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100236 EXPECT_EQ(kSsrc, stats.local_ssrc);
237 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
238 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
239 EXPECT_EQ(static_cast<int32_t>(kReportBlock.cumulative_num_packets_lost),
solenberg85a04962015-10-27 03:35:21 -0700240 stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100241 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
242 EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
243 EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number),
solenberg85a04962015-10-27 03:35:21 -0700244 stats.ext_seqnum);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100245 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
246 (kCodecInst.plfreq / 1000)),
247 stats.jitter_ms);
248 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
249 EXPECT_EQ(static_cast<int32_t>(kSpeechInputLevel), stats.audio_level);
solenberg85a04962015-10-27 03:35:21 -0700250 EXPECT_EQ(-1, stats.aec_quality_min);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100251 EXPECT_EQ(kEchoDelayMedian, stats.echo_delay_median_ms);
252 EXPECT_EQ(kEchoDelayStdDev, stats.echo_delay_std_ms);
253 EXPECT_EQ(kEchoReturnLoss, stats.echo_return_loss);
254 EXPECT_EQ(kEchoReturnLossEnhancement, stats.echo_return_loss_enhancement);
solenberg85a04962015-10-27 03:35:21 -0700255 EXPECT_FALSE(stats.typing_noise_detected);
256}
solenberg566ef242015-11-06 15:34:49 -0800257
258TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
259 ConfigHelper helper;
perkj8eb37a32016-08-16 02:40:55 -0700260 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
261 helper.congestion_controller(),
262 helper.bitrate_allocator());
solenberg566ef242015-11-06 15:34:49 -0800263 helper.SetupMockForGetStats();
264 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
265
266 internal::AudioState* internal_audio_state =
267 static_cast<internal::AudioState*>(helper.audio_state().get());
268 VoiceEngineObserver* voe_observer =
269 static_cast<VoiceEngineObserver*>(internal_audio_state);
270 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
271 EXPECT_TRUE(send_stream.GetStats().typing_noise_detected);
272 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
273 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
274}
solenberg85a04962015-10-27 03:35:21 -0700275} // namespace test
solenbergc7a8b082015-10-16 14:35:07 -0700276} // namespace webrtc