blob: 917206470530031a2c397b4c0f2a87699b1cf274 [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"
perkj26091b12016-09-01 01:17:40 -070019#include "webrtc/base/task_queue.h"
perkjec81bcd2016-05-11 06:01:13 -070020#include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h"
ivoc14d5dbe2016-07-04 07:06:55 -070021#include "webrtc/call/mock/mock_rtc_event_log.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010022#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/modules/pacing/paced_sender.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010024#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080025#include "webrtc/test/mock_voe_channel_proxy.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010026#include "webrtc/test/mock_voice_engine.h"
solenbergc7a8b082015-10-16 14:35:07 -070027
28namespace webrtc {
solenberg85a04962015-10-27 03:35:21 -070029namespace test {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010030namespace {
31
solenberg3a941542015-11-16 07:34:50 -080032using testing::_;
33using testing::Return;
34
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010035const int kChannelId = 1;
36const uint32_t kSsrc = 1234;
solenberg3a941542015-11-16 07:34:50 -080037const char* kCName = "foo_name";
38const int kAudioLevelId = 2;
39const int kAbsSendTimeId = 3;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010040const int kTransportSequenceNumberId = 4;
solenberg566ef242015-11-06 15:34:49 -080041const int kEchoDelayMedian = 254;
42const int kEchoDelayStdDev = -3;
43const int kEchoReturnLoss = -65;
44const int kEchoReturnLossEnhancement = 101;
45const unsigned int kSpeechInputLevel = 96;
46const CallStatistics kCallStats = {
47 1345, 1678, 1901, 1234, 112, 13456, 17890, 1567, -1890, -1123};
pkasting25702cb2016-01-08 13:50:27 -080048const CodecInst kCodecInst = {-121, "codec_name_send", 48000, -231, 0, -671};
solenberg566ef242015-11-06 15:34:49 -080049const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
Fredrik Solenbergb5727682015-12-04 15:22:19 +010050const int kTelephoneEventPayloadType = 123;
solenberg8842c3e2016-03-11 03:06:41 -080051const int kTelephoneEventCode = 45;
52const int kTelephoneEventDuration = 6789;
solenberg566ef242015-11-06 15:34:49 -080053
mflodman86cc6ff2016-07-26 04:44:06 -070054class MockLimitObserver : public BitrateAllocator::LimitObserver {
55 public:
56 MOCK_METHOD2(OnAllocationLimitsChanged,
57 void(uint32_t min_send_bitrate_bps,
58 uint32_t max_padding_bitrate_bps));
59};
60
solenberg566ef242015-11-06 15:34:49 -080061struct ConfigHelper {
Stefan Holmerb86d4e42015-12-07 10:26:18 +010062 ConfigHelper()
Stefan Holmer58c664c2016-02-08 14:31:30 +010063 : simulated_clock_(123456),
64 stream_config_(nullptr),
Stefan Holmer58c664c2016-02-08 14:31:30 +010065 congestion_controller_(&simulated_clock_,
Stefan Holmer58c664c2016-02-08 14:31:30 +010066 &bitrate_observer_,
ivoc14d5dbe2016-07-04 07:06:55 -070067 &remote_bitrate_observer_,
mflodman86cc6ff2016-07-26 04:44:06 -070068 &event_log_),
perkj26091b12016-09-01 01:17:40 -070069 bitrate_allocator_(&limit_observer_),
70 worker_queue_("ConfigHelper_worker_queue") {
solenberg13725082015-11-25 08:16:52 -080071 using testing::Invoke;
solenberg3a941542015-11-16 07:34:50 -080072 using testing::StrEq;
73
solenberg566ef242015-11-06 15:34:49 -080074 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080075 RegisterVoiceEngineObserver(_)).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080076 EXPECT_CALL(voice_engine_,
solenberg3a941542015-11-16 07:34:50 -080077 DeRegisterVoiceEngineObserver()).WillOnce(Return(0));
solenberg566ef242015-11-06 15:34:49 -080078 AudioState::Config config;
79 config.voice_engine = &voice_engine_;
80 audio_state_ = AudioState::Create(config);
solenberg3a941542015-11-16 07:34:50 -080081
solenberg13725082015-11-25 08:16:52 -080082 EXPECT_CALL(voice_engine_, ChannelProxyFactory(kChannelId))
83 .WillOnce(Invoke([this](int channel_id) {
84 EXPECT_FALSE(channel_proxy_);
85 channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
86 EXPECT_CALL(*channel_proxy_, SetRTCPStatus(true)).Times(1);
87 EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kSsrc)).Times(1);
88 EXPECT_CALL(*channel_proxy_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
solenberg971cab02016-06-14 10:02:41 -070089 EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 10)).Times(1);
solenberg358057b2015-11-27 10:46:42 -080090 EXPECT_CALL(*channel_proxy_,
91 SetSendAbsoluteSenderTimeStatus(true, kAbsSendTimeId)).Times(1);
92 EXPECT_CALL(*channel_proxy_,
93 SetSendAudioLevelIndicationStatus(true, kAudioLevelId)).Times(1);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010094 EXPECT_CALL(*channel_proxy_, EnableSendTransportSequenceNumber(
95 kTransportSequenceNumberId))
96 .Times(1);
97 EXPECT_CALL(*channel_proxy_,
stefanbba9dec2016-02-01 04:39:55 -080098 RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010099 congestion_controller_.pacer(),
100 congestion_controller_.GetTransportFeedbackObserver(),
101 congestion_controller_.packet_router()))
102 .Times(1);
stefanbba9dec2016-02-01 04:39:55 -0800103 EXPECT_CALL(*channel_proxy_, ResetCongestionControlObjects())
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100104 .Times(1);
mflodman3d7db262016-04-29 00:57:13 -0700105 EXPECT_CALL(*channel_proxy_, RegisterExternalTransport(nullptr))
106 .Times(1);
107 EXPECT_CALL(*channel_proxy_, DeRegisterExternalTransport())
108 .Times(1);
solenberg13725082015-11-25 08:16:52 -0800109 return channel_proxy_;
110 }));
solenberg566ef242015-11-06 15:34:49 -0800111 stream_config_.voe_channel_id = kChannelId;
112 stream_config_.rtp.ssrc = kSsrc;
solenberg971cab02016-06-14 10:02:41 -0700113 stream_config_.rtp.nack.rtp_history_ms = 200;
solenberg3a941542015-11-16 07:34:50 -0800114 stream_config_.rtp.c_name = kCName;
115 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700116 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
solenberg3a941542015-11-16 07:34:50 -0800117 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700118 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100119 stream_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700120 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
solenberg566ef242015-11-06 15:34:49 -0800121 }
122
123 AudioSendStream::Config& config() { return stream_config_; }
124 rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
solenberg94218532016-06-16 10:53:22 -0700125 MockVoEChannelProxy* channel_proxy() { return channel_proxy_; }
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100126 CongestionController* congestion_controller() {
127 return &congestion_controller_;
128 }
mflodman86cc6ff2016-07-26 04:44:06 -0700129 BitrateAllocator* bitrate_allocator() { return &bitrate_allocator_; }
perkj26091b12016-09-01 01:17:40 -0700130 rtc::TaskQueue* worker_queue() { return &worker_queue_; }
solenberg566ef242015-11-06 15:34:49 -0800131
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100132 void SetupMockForSendTelephoneEvent() {
133 EXPECT_TRUE(channel_proxy_);
134 EXPECT_CALL(*channel_proxy_,
135 SetSendTelephoneEventPayloadType(kTelephoneEventPayloadType))
136 .WillOnce(Return(true));
137 EXPECT_CALL(*channel_proxy_,
138 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
139 .WillOnce(Return(true));
140 }
141
solenberg566ef242015-11-06 15:34:49 -0800142 void SetupMockForGetStats() {
solenberg3a941542015-11-16 07:34:50 -0800143 using testing::DoAll;
solenberg3a941542015-11-16 07:34:50 -0800144 using testing::SetArgReferee;
145
solenberg566ef242015-11-06 15:34:49 -0800146 std::vector<ReportBlock> report_blocks;
147 webrtc::ReportBlock block = kReportBlock;
148 report_blocks.push_back(block); // Has wrong SSRC.
149 block.source_SSRC = kSsrc;
150 report_blocks.push_back(block); // Correct block.
151 block.fraction_lost = 0;
152 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
153
solenberg358057b2015-11-27 10:46:42 -0800154 EXPECT_TRUE(channel_proxy_);
155 EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
156 .WillRepeatedly(Return(kCallStats));
157 EXPECT_CALL(*channel_proxy_, GetRemoteRTCPReportBlocks())
158 .WillRepeatedly(Return(report_blocks));
159
solenberg566ef242015-11-06 15:34:49 -0800160 EXPECT_CALL(voice_engine_, GetSendCodec(kChannelId, _))
161 .WillRepeatedly(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
solenberg566ef242015-11-06 15:34:49 -0800162 EXPECT_CALL(voice_engine_, GetSpeechInputLevelFullRange(_))
163 .WillRepeatedly(DoAll(SetArgReferee<0>(kSpeechInputLevel), Return(0)));
164 EXPECT_CALL(voice_engine_, GetEcMetricsStatus(_))
165 .WillRepeatedly(DoAll(SetArgReferee<0>(true), Return(0)));
166 EXPECT_CALL(voice_engine_, GetEchoMetrics(_, _, _, _))
167 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoReturnLoss),
168 SetArgReferee<1>(kEchoReturnLossEnhancement),
169 Return(0)));
170 EXPECT_CALL(voice_engine_, GetEcDelayMetrics(_, _, _))
171 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoDelayMedian),
172 SetArgReferee<1>(kEchoDelayStdDev), Return(0)));
173 }
174
175 private:
Stefan Holmer58c664c2016-02-08 14:31:30 +0100176 SimulatedClock simulated_clock_;
solenberg3a941542015-11-16 07:34:50 -0800177 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -0800178 rtc::scoped_refptr<AudioState> audio_state_;
179 AudioSendStream::Config stream_config_;
solenberg13725082015-11-25 08:16:52 -0800180 testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
perkjec81bcd2016-05-11 06:01:13 -0700181 testing::NiceMock<MockCongestionObserver> bitrate_observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100182 testing::NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100183 CongestionController congestion_controller_;
ivoc14d5dbe2016-07-04 07:06:55 -0700184 MockRtcEventLog event_log_;
mflodman86cc6ff2016-07-26 04:44:06 -0700185 testing::NiceMock<MockLimitObserver> limit_observer_;
186 BitrateAllocator bitrate_allocator_;
perkj26091b12016-09-01 01:17:40 -0700187 // |worker_queue| is defined last to ensure all pending tasks are cancelled
188 // and deleted before any other members.
189 rtc::TaskQueue worker_queue_;
solenberg566ef242015-11-06 15:34:49 -0800190};
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100191} // namespace
solenbergc7a8b082015-10-16 14:35:07 -0700192
193TEST(AudioSendStreamTest, ConfigToString) {
solenbergc7a8b082015-10-16 14:35:07 -0700194 AudioSendStream::Config config(nullptr);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100195 config.rtp.ssrc = kSsrc;
solenbergc7a8b082015-10-16 14:35:07 -0700196 config.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700197 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
solenberg3a941542015-11-16 07:34:50 -0800198 config.rtp.c_name = kCName;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100199 config.voe_channel_id = kChannelId;
solenbergc7a8b082015-10-16 14:35:07 -0700200 config.cng_payload_type = 42;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100201 EXPECT_EQ(
isheriff6f8d6862016-05-26 11:24:55 -0700202 "{rtp: {ssrc: 1234, extensions: [{uri: "
solenberg3a941542015-11-16 07:34:50 -0800203 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 3}], "
solenberg971cab02016-06-14 10:02:41 -0700204 "nack: {rtp_history_ms: 0}, c_name: foo_name}, voe_channel_id: 1, "
205 "cng_payload_type: 42}",
solenberg85a04962015-10-27 03:35:21 -0700206 config.ToString());
solenbergc7a8b082015-10-16 14:35:07 -0700207}
208
209TEST(AudioSendStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 15:34:49 -0800210 ConfigHelper helper;
perkj26091b12016-09-01 01:17:40 -0700211 internal::AudioSendStream send_stream(
212 helper.config(), helper.audio_state(), helper.worker_queue(),
213 helper.congestion_controller(), helper.bitrate_allocator());
solenbergc7a8b082015-10-16 14:35:07 -0700214}
solenberg85a04962015-10-27 03:35:21 -0700215
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100216TEST(AudioSendStreamTest, SendTelephoneEvent) {
217 ConfigHelper helper;
perkj26091b12016-09-01 01:17:40 -0700218 internal::AudioSendStream send_stream(
219 helper.config(), helper.audio_state(), helper.worker_queue(),
220 helper.congestion_controller(), helper.bitrate_allocator());
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100221 helper.SetupMockForSendTelephoneEvent();
222 EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
223 kTelephoneEventCode, kTelephoneEventDuration));
224}
225
solenberg94218532016-06-16 10:53:22 -0700226TEST(AudioSendStreamTest, SetMuted) {
227 ConfigHelper helper;
perkj26091b12016-09-01 01:17:40 -0700228 internal::AudioSendStream send_stream(
229 helper.config(), helper.audio_state(), helper.worker_queue(),
230 helper.congestion_controller(), helper.bitrate_allocator());
solenberg94218532016-06-16 10:53:22 -0700231 EXPECT_CALL(*helper.channel_proxy(), SetInputMute(true));
232 send_stream.SetMuted(true);
233}
234
solenberg85a04962015-10-27 03:35:21 -0700235TEST(AudioSendStreamTest, GetStats) {
solenberg566ef242015-11-06 15:34:49 -0800236 ConfigHelper helper;
perkj26091b12016-09-01 01:17:40 -0700237 internal::AudioSendStream send_stream(
238 helper.config(), helper.audio_state(), helper.worker_queue(),
239 helper.congestion_controller(), helper.bitrate_allocator());
solenberg566ef242015-11-06 15:34:49 -0800240 helper.SetupMockForGetStats();
solenberg85a04962015-10-27 03:35:21 -0700241 AudioSendStream::Stats stats = send_stream.GetStats();
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100242 EXPECT_EQ(kSsrc, stats.local_ssrc);
243 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
244 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
245 EXPECT_EQ(static_cast<int32_t>(kReportBlock.cumulative_num_packets_lost),
solenberg85a04962015-10-27 03:35:21 -0700246 stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100247 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
248 EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
249 EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number),
solenberg85a04962015-10-27 03:35:21 -0700250 stats.ext_seqnum);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100251 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
252 (kCodecInst.plfreq / 1000)),
253 stats.jitter_ms);
254 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
255 EXPECT_EQ(static_cast<int32_t>(kSpeechInputLevel), stats.audio_level);
solenberg85a04962015-10-27 03:35:21 -0700256 EXPECT_EQ(-1, stats.aec_quality_min);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100257 EXPECT_EQ(kEchoDelayMedian, stats.echo_delay_median_ms);
258 EXPECT_EQ(kEchoDelayStdDev, stats.echo_delay_std_ms);
259 EXPECT_EQ(kEchoReturnLoss, stats.echo_return_loss);
260 EXPECT_EQ(kEchoReturnLossEnhancement, stats.echo_return_loss_enhancement);
solenberg85a04962015-10-27 03:35:21 -0700261 EXPECT_FALSE(stats.typing_noise_detected);
262}
solenberg566ef242015-11-06 15:34:49 -0800263
264TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
265 ConfigHelper helper;
perkj26091b12016-09-01 01:17:40 -0700266 internal::AudioSendStream send_stream(
267 helper.config(), helper.audio_state(), helper.worker_queue(),
268 helper.congestion_controller(), helper.bitrate_allocator());
solenberg566ef242015-11-06 15:34:49 -0800269 helper.SetupMockForGetStats();
270 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
271
272 internal::AudioState* internal_audio_state =
273 static_cast<internal::AudioState*>(helper.audio_state().get());
274 VoiceEngineObserver* voe_observer =
275 static_cast<VoiceEngineObserver*>(internal_audio_state);
276 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
277 EXPECT_TRUE(send_stream.GetStats().typing_noise_detected);
278 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
279 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
280}
solenberg85a04962015-10-27 03:35:21 -0700281} // namespace test
solenbergc7a8b082015-10-16 14:35:07 -0700282} // namespace webrtc