blob: 4d9d465c65a832a4ae7aede7f7853895945c724b [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);
solenberg358057b2015-11-27 10:46:42 -080077 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 10:26:18 +010081 EXPECT_CALL(*channel_proxy_, EnableSendTransportSequenceNumber(
82 kTransportSequenceNumberId))
83 .Times(1);
84 EXPECT_CALL(*channel_proxy_,
stefanbba9dec2016-02-01 04:39:55 -080085 RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010086 congestion_controller_.pacer(),
87 congestion_controller_.GetTransportFeedbackObserver(),
88 congestion_controller_.packet_router()))
89 .Times(1);
stefanbba9dec2016-02-01 04:39:55 -080090 EXPECT_CALL(*channel_proxy_, ResetCongestionControlObjects())
Stefan Holmerb86d4e42015-12-07 10:26:18 +010091 .Times(1);
mflodman3d7db262016-04-29 00:57:13 -070092 EXPECT_CALL(*channel_proxy_, RegisterExternalTransport(nullptr))
93 .Times(1);
94 EXPECT_CALL(*channel_proxy_, DeRegisterExternalTransport())
95 .Times(1);
solenberg13725082015-11-25 08:16:52 -080096 return channel_proxy_;
97 }));
solenberg566ef242015-11-06 15:34:49 -080098 stream_config_.voe_channel_id = kChannelId;
99 stream_config_.rtp.ssrc = kSsrc;
solenberg3a941542015-11-16 07:34:50 -0800100 stream_config_.rtp.c_name = kCName;
101 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700102 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
solenberg3a941542015-11-16 07:34:50 -0800103 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700104 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100105 stream_config_.rtp.extensions.push_back(RtpExtension(
isheriff6f8d6862016-05-26 11:24:55 -0700106 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
solenberg566ef242015-11-06 15:34:49 -0800107 }
108
109 AudioSendStream::Config& config() { return stream_config_; }
110 rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100111 CongestionController* congestion_controller() {
112 return &congestion_controller_;
113 }
solenberg566ef242015-11-06 15:34:49 -0800114
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100115 void SetupMockForSendTelephoneEvent() {
116 EXPECT_TRUE(channel_proxy_);
117 EXPECT_CALL(*channel_proxy_,
118 SetSendTelephoneEventPayloadType(kTelephoneEventPayloadType))
119 .WillOnce(Return(true));
120 EXPECT_CALL(*channel_proxy_,
121 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
122 .WillOnce(Return(true));
123 }
124
solenberg566ef242015-11-06 15:34:49 -0800125 void SetupMockForGetStats() {
solenberg3a941542015-11-16 07:34:50 -0800126 using testing::DoAll;
solenberg3a941542015-11-16 07:34:50 -0800127 using testing::SetArgReferee;
128
solenberg566ef242015-11-06 15:34:49 -0800129 std::vector<ReportBlock> report_blocks;
130 webrtc::ReportBlock block = kReportBlock;
131 report_blocks.push_back(block); // Has wrong SSRC.
132 block.source_SSRC = kSsrc;
133 report_blocks.push_back(block); // Correct block.
134 block.fraction_lost = 0;
135 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
136
solenberg358057b2015-11-27 10:46:42 -0800137 EXPECT_TRUE(channel_proxy_);
138 EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
139 .WillRepeatedly(Return(kCallStats));
140 EXPECT_CALL(*channel_proxy_, GetRemoteRTCPReportBlocks())
141 .WillRepeatedly(Return(report_blocks));
142
solenberg566ef242015-11-06 15:34:49 -0800143 EXPECT_CALL(voice_engine_, GetSendCodec(kChannelId, _))
144 .WillRepeatedly(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
solenberg566ef242015-11-06 15:34:49 -0800145 EXPECT_CALL(voice_engine_, GetSpeechInputLevelFullRange(_))
146 .WillRepeatedly(DoAll(SetArgReferee<0>(kSpeechInputLevel), Return(0)));
147 EXPECT_CALL(voice_engine_, GetEcMetricsStatus(_))
148 .WillRepeatedly(DoAll(SetArgReferee<0>(true), Return(0)));
149 EXPECT_CALL(voice_engine_, GetEchoMetrics(_, _, _, _))
150 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoReturnLoss),
151 SetArgReferee<1>(kEchoReturnLossEnhancement),
152 Return(0)));
153 EXPECT_CALL(voice_engine_, GetEcDelayMetrics(_, _, _))
154 .WillRepeatedly(DoAll(SetArgReferee<0>(kEchoDelayMedian),
155 SetArgReferee<1>(kEchoDelayStdDev), Return(0)));
156 }
157
158 private:
Stefan Holmer58c664c2016-02-08 14:31:30 +0100159 SimulatedClock simulated_clock_;
solenberg3a941542015-11-16 07:34:50 -0800160 testing::StrictMock<MockVoiceEngine> voice_engine_;
solenberg566ef242015-11-06 15:34:49 -0800161 rtc::scoped_refptr<AudioState> audio_state_;
162 AudioSendStream::Config stream_config_;
solenberg13725082015-11-25 08:16:52 -0800163 testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
perkjec81bcd2016-05-11 06:01:13 -0700164 testing::NiceMock<MockCongestionObserver> bitrate_observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100165 testing::NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100166 CongestionController congestion_controller_;
solenberg566ef242015-11-06 15:34:49 -0800167};
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100168} // namespace
solenbergc7a8b082015-10-16 14:35:07 -0700169
170TEST(AudioSendStreamTest, ConfigToString) {
solenbergc7a8b082015-10-16 14:35:07 -0700171 AudioSendStream::Config config(nullptr);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100172 config.rtp.ssrc = kSsrc;
solenbergc7a8b082015-10-16 14:35:07 -0700173 config.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700174 RtpExtension(RtpExtension::kAbsSendTimeUri, kAbsSendTimeId));
solenberg3a941542015-11-16 07:34:50 -0800175 config.rtp.c_name = kCName;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100176 config.voe_channel_id = kChannelId;
solenbergc7a8b082015-10-16 14:35:07 -0700177 config.cng_payload_type = 42;
178 config.red_payload_type = 17;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100179 EXPECT_EQ(
isheriff6f8d6862016-05-26 11:24:55 -0700180 "{rtp: {ssrc: 1234, extensions: [{uri: "
solenberg3a941542015-11-16 07:34:50 -0800181 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 3}], "
182 "c_name: foo_name}, voe_channel_id: 1, cng_payload_type: 42, "
183 "red_payload_type: 17}",
solenberg85a04962015-10-27 03:35:21 -0700184 config.ToString());
solenbergc7a8b082015-10-16 14:35:07 -0700185}
186
187TEST(AudioSendStreamTest, ConstructDestruct) {
solenberg566ef242015-11-06 15:34:49 -0800188 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100189 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
190 helper.congestion_controller());
solenbergc7a8b082015-10-16 14:35:07 -0700191}
solenberg85a04962015-10-27 03:35:21 -0700192
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100193TEST(AudioSendStreamTest, SendTelephoneEvent) {
194 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100195 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
196 helper.congestion_controller());
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100197 helper.SetupMockForSendTelephoneEvent();
198 EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
199 kTelephoneEventCode, kTelephoneEventDuration));
200}
201
solenberg85a04962015-10-27 03:35:21 -0700202TEST(AudioSendStreamTest, GetStats) {
solenberg566ef242015-11-06 15:34:49 -0800203 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100204 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
205 helper.congestion_controller());
solenberg566ef242015-11-06 15:34:49 -0800206 helper.SetupMockForGetStats();
solenberg85a04962015-10-27 03:35:21 -0700207 AudioSendStream::Stats stats = send_stream.GetStats();
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100208 EXPECT_EQ(kSsrc, stats.local_ssrc);
209 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
210 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
211 EXPECT_EQ(static_cast<int32_t>(kReportBlock.cumulative_num_packets_lost),
solenberg85a04962015-10-27 03:35:21 -0700212 stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100213 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
214 EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
215 EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number),
solenberg85a04962015-10-27 03:35:21 -0700216 stats.ext_seqnum);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100217 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
218 (kCodecInst.plfreq / 1000)),
219 stats.jitter_ms);
220 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
221 EXPECT_EQ(static_cast<int32_t>(kSpeechInputLevel), stats.audio_level);
solenberg85a04962015-10-27 03:35:21 -0700222 EXPECT_EQ(-1, stats.aec_quality_min);
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100223 EXPECT_EQ(kEchoDelayMedian, stats.echo_delay_median_ms);
224 EXPECT_EQ(kEchoDelayStdDev, stats.echo_delay_std_ms);
225 EXPECT_EQ(kEchoReturnLoss, stats.echo_return_loss);
226 EXPECT_EQ(kEchoReturnLossEnhancement, stats.echo_return_loss_enhancement);
solenberg85a04962015-10-27 03:35:21 -0700227 EXPECT_FALSE(stats.typing_noise_detected);
228}
solenberg566ef242015-11-06 15:34:49 -0800229
230TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
231 ConfigHelper helper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100232 internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
233 helper.congestion_controller());
solenberg566ef242015-11-06 15:34:49 -0800234 helper.SetupMockForGetStats();
235 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
236
237 internal::AudioState* internal_audio_state =
238 static_cast<internal::AudioState*>(helper.audio_state().get());
239 VoiceEngineObserver* voe_observer =
240 static_cast<VoiceEngineObserver*>(internal_audio_state);
241 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_WARNING);
242 EXPECT_TRUE(send_stream.GetStats().typing_noise_detected);
243 voe_observer->CallbackOnError(-1, VE_TYPING_NOISE_OFF_WARNING);
244 EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);
245}
solenberg85a04962015-10-27 03:35:21 -0700246} // namespace test
solenbergc7a8b082015-10-16 14:35:07 -0700247} // namespace webrtc