blob: 44392ea1c0edb6238e83564cfd51fa84329cd11b [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"
Stefan Holmer80e12072016-02-23 13:30:42 +010020#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/modules/pacing/paced_sender.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010022#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080023#include "webrtc/test/mock_voe_channel_proxy.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010024#include "webrtc/test/mock_voice_engine.h"
solenbergc7a8b082015-10-16 14:35:07 -070025
26namespace webrtc {
solenberg85a04962015-10-27 03:35:21 -070027namespace test {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010028namespace {
29
solenberg3a941542015-11-16 07:34:50 -080030using testing::_;
31using testing::Return;
32
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010033const int kChannelId = 1;
34const uint32_t kSsrc = 1234;
solenberg3a941542015-11-16 07:34:50 -080035const char* kCName = "foo_name";
36const int kAudioLevelId = 2;
37const int kAbsSendTimeId = 3;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010038const int kTransportSequenceNumberId = 4;
solenberg566ef242015-11-06 15:34:49 -080039const 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};
pkasting25702cb2016-01-08 13:50:27 -080046const CodecInst kCodecInst = {-121, "codec_name_send", 48000, -231, 0, -671};
solenberg566ef242015-11-06 15:34:49 -080047const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
Fredrik Solenbergb5727682015-12-04 15:22:19 +010048const int kTelephoneEventPayloadType = 123;
solenberg8842c3e2016-03-11 03:06:41 -080049const int kTelephoneEventCode = 45;
50const int kTelephoneEventDuration = 6789;
solenberg566ef242015-11-06 15:34:49 -080051
52struct ConfigHelper {
Stefan Holmerb86d4e42015-12-07 10:26:18 +010053 ConfigHelper()
Stefan Holmer58c664c2016-02-08 14:31:30 +010054 : simulated_clock_(123456),
55 stream_config_(nullptr),
Stefan Holmer58c664c2016-02-08 14:31:30 +010056 congestion_controller_(&simulated_clock_,
Stefan Holmer58c664c2016-02-08 14:31:30 +010057 &bitrate_observer_,
58 &remote_bitrate_observer_) {
solenberg13725082015-11-25 08:16:52 -080059 using testing::Invoke;
solenberg3a941542015-11-16 07:34:50 -080060 using testing::StrEq;
61
solenberg566ef242015-11-06 15:34:49 -080062 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080063 RegisterVoiceEngineObserver(_)).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080064 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080065 DeRegisterVoiceEngineObserver()).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080066 AudioState::Config config;
67 config.voice_engine = &voice_engine_;
68 audio_state_ = AudioState::Create(config);
solenberg3a941542015-11-16 07:34:50 -080069
solenberg13725082015-11-25 08:16:52 -080070 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);
solenberg971cab02016-06-14 10:02:41 -070077 EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 10)).Times(1);
solenberg358057b2015-11-27 10:46:42 -080078 EXPECT_CALL(*channel_proxy_,
79 SetSendAbsoluteSenderTimeStatus(true, kAbsSendTimeId)).Times(1);
80 EXPECT_CALL(*channel_proxy_,
81 SetSendAudioLevelIndicationStatus(true, kAudioLevelId)).Times(1);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010082 EXPECT_CALL(*channel_proxy_, EnableSendTransportSequenceNumber(
83 kTransportSequenceNumberId))
84 .Times(1);
85 EXPECT_CALL(*channel_proxy_,
stefanbba9dec2016-02-01 04:39:55 -080086 RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010087 congestion_controller_.pacer(),
88 congestion_controller_.GetTransportFeedbackObserver(),
89 congestion_controller_.packet_router()))
90 .Times(1);
stefanbba9dec2016-02-01 04:39:55 -080091 EXPECT_CALL(*channel_proxy_, ResetCongestionControlObjects())
Stefan Holmerb86d4e42015-12-07 10:26:18 +010092 .Times(1);
mflodman3d7db262016-04-29 00:57:13 -070093 EXPECT_CALL(*channel_proxy_, RegisterExternalTransport(nullptr))
94 .Times(1);
95 EXPECT_CALL(*channel_proxy_, DeRegisterExternalTransport())
96 .Times(1);
solenberg13725082015-11-25 08:16:52 -080097 return channel_proxy_;
98 }));
solenberg566ef242015-11-06 15:34:49 -080099 stream_config_.voe_channel_id = kChannelId;
100 stream_config_.rtp.ssrc = kSsrc;
solenberg971cab02016-06-14 10:02:41 -0700101 stream_config_.rtp.nack.rtp_history_ms = 200;
solenberg3a941542015-11-16 07:34:50 -0800102 stream_config_.rtp.c_name = kCName;
103 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700104 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
solenberg3a941542015-11-16 07:34:50 -0800105 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700106 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100107 stream_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700108 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
solenberg566ef242015-11-06 15:34:49 -0800109 }
110
111 AudioSendStream::Config& config() { return stream_config_; }
112 rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100113 CongestionController* congestion_controller() {
114 return &congestion_controller_;
115 }
solenberg566ef242015-11-06 15:34:49 -0800116
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100117 void SetupMockForSendTelephoneEvent() {
118 EXPECT_TRUE(channel_proxy_);
119 EXPECT_CALL(*channel_proxy_,
120 SetSendTelephoneEventPayloadType(kTelephoneEventPayloadType))
121 .WillOnce(Return(true));
122 EXPECT_CALL(*channel_proxy_,
123 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
124 .WillOnce(Return(true));
125 }
126
solenberg566ef242015-11-06 15:34:49 -0800127 void SetupMockForGetStats() {
solenberg3a941542015-11-16 07:34:50 -0800128 using testing::DoAll;
solenberg3a941542015-11-16 07:34:50 -0800129 using testing::SetArgReferee;
130
solenberg566ef242015-11-06 15:34:49 -0800131 std::vector<ReportBlock> report_blocks;
132 webrtc::ReportBlock block = kReportBlock;
133 report_blocks.push_back(block); // Has wrong SSRC.
134 block.source_SSRC = kSsrc;
135 report_blocks.push_back(block); // Correct block.
136 block.fraction_lost = 0;
137 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
138
solenberg358057b2015-11-27 10:46:42 -0800139 EXPECT_TRUE(channel_proxy_);
140 EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
141 .WillRepeatedly(Return(kCallStats));
142 EXPECT_CALL(*channel_proxy_, GetRemoteRTCPReportBlocks())
143 .WillRepeatedly(Return(report_blocks));
144
solenberg566ef242015-11-06 15:34:49 -0800145 EXPECT_CALL(voice_engine_, GetSendCodec(kChannelId, _))
146 .WillRepeatedly(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
solenberg566ef242015-11-06 15:34:49 -0800147 EXPECT_CALL(voice_engine_, GetSpeechInputLevelFullRange(_))
148 .WillRepeatedly(DoAll(SetArgReferee<0>(kSpeechInputLevel), Return(0)));
149 EXPECT_CALL(voice_engine_, GetEcMetricsStatus(_))
150 .WillRepeatedly(DoAll(SetArgReferee<0>(true), Return(0)));
151 EXPECT_CALL(voice_engine_, GetEchoMetrics(_, _, _, _))
152 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoReturnLoss),
153 SetArgReferee<1>(kEchoReturnLossEnhancement),
154 Return(0)));
155 EXPECT_CALL(voice_engine_, GetEcDelayMetrics(_, _, _))
156 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoDelayMedian),
157 SetArgReferee<1>(kEchoDelayStdDev), Return(0)));
158 }
159
160 private:
Stefan Holmer58c664c2016-02-08 14:31:30 +0100161 SimulatedClock simulated_clock_;
solenberg3a941542015-11-16 07:34:50 -0800162 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -0800163 rtc::scoped_refptr<AudioState> audio_state_;
164 AudioSendStream::Config stream_config_;
solenberg13725082015-11-25 08:16:52 -0800165 testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
perkjec81bcd2016-05-11 06:01:13 -0700166 testing::NiceMock<MockCongestionObserver> bitrate_observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100167 testing::NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100168 CongestionController congestion_controller_;
solenberg566ef242015-11-06 15:34:49 -0800169};
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100170} // namespace
solenbergc7a8b082015-10-16 14:35:07 -0700171
172TEST(AudioSendStreamTest, ConfigToString) {
solenbergc7a8b082015-10-16 14:35:07 -0700173 AudioSendStream::Config config(nullptr);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100174 config.rtp.ssrc = kSsrc;
solenbergc7a8b082015-10-16 14:35:07 -0700175 config.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700176 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
solenberg3a941542015-11-16 07:34:50 -0800177 config.rtp.c_name = kCName;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100178 config.voe_channel_id = kChannelId;
solenbergc7a8b082015-10-16 14:35:07 -0700179 config.cng_payload_type = 42;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100180 EXPECT_EQ(
isheriff6f8d6862016-05-26 11:24:55 -0700181 "{rtp: {ssrc: 1234, extensions: [{uri: "
solenberg3a941542015-11-16 07:34:50 -0800182 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 3}], "
solenberg971cab02016-06-14 10:02:41 -0700183 "nack: {rtp_history_ms: 0}, c_name: foo_name}, voe_channel_id: 1, "
184 "cng_payload_type: 42}",
solenberg85a04962015-10-27 03:35:21 -0700185 config.ToString());
solenbergc7a8b082015-10-16 14:35:07 -0700186}
187
188TEST(AudioSendStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 15:34:49 -0800189 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100190 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
191 helper.congestion_controller());
solenbergc7a8b082015-10-16 14:35:07 -0700192}
solenberg85a04962015-10-27 03:35:21 -0700193
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100194TEST(AudioSendStreamTest, SendTelephoneEvent) {
195 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100196 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
197 helper.congestion_controller());
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100198 helper.SetupMockForSendTelephoneEvent();
199 EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
200 kTelephoneEventCode, kTelephoneEventDuration));
201}
202
solenberg85a04962015-10-27 03:35:21 -0700203TEST(AudioSendStreamTest, GetStats) {
solenberg566ef242015-11-06 15:34:49 -0800204 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100205 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
206 helper.congestion_controller());
solenberg566ef242015-11-06 15:34:49 -0800207 helper.SetupMockForGetStats();
solenberg85a04962015-10-27 03:35:21 -0700208 AudioSendStream::Stats stats = send_stream.GetStats();
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100209 EXPECT_EQ(kSsrc, stats.local_ssrc);
210 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
211 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
212 EXPECT_EQ(static_cast<int32_t>(kReportBlock.cumulative_num_packets_lost),
solenberg85a04962015-10-27 03:35:21 -0700213 stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100214 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
215 EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
216 EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number),
solenberg85a04962015-10-27 03:35:21 -0700217 stats.ext_seqnum);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100218 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
219 (kCodecInst.plfreq / 1000)),
220 stats.jitter_ms);
221 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
222 EXPECT_EQ(static_cast<int32_t>(kSpeechInputLevel), stats.audio_level);
solenberg85a04962015-10-27 03:35:21 -0700223 EXPECT_EQ(-1, stats.aec_quality_min);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100224 EXPECT_EQ(kEchoDelayMedian, stats.echo_delay_median_ms);
225 EXPECT_EQ(kEchoDelayStdDev, stats.echo_delay_std_ms);
226 EXPECT_EQ(kEchoReturnLoss, stats.echo_return_loss);
227 EXPECT_EQ(kEchoReturnLossEnhancement, stats.echo_return_loss_enhancement);
solenberg85a04962015-10-27 03:35:21 -0700228 EXPECT_FALSE(stats.typing_noise_detected);
229}
solenberg566ef242015-11-06 15:34:49 -0800230
231TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
232 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100233 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
234 helper.congestion_controller());
solenberg566ef242015-11-06 15:34:49 -0800235 helper.SetupMockForGetStats();
236 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
237
238 internal::AudioState* internal_audio_state =
239 static_cast<internal::AudioState*>(helper.audio_state().get());
240 VoiceEngineObserver* voe_observer =
241 static_cast<VoiceEngineObserver*>(internal_audio_state);
242 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
243 EXPECT_TRUE(send_stream.GetStats().typing_noise_detected);
244 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
245 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
246}
solenberg85a04962015-10-27 03:35:21 -0700247} // namespace test
solenbergc7a8b082015-10-16 14:35:07 -0700248} // namespace webrtc