1/*
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
11#include <string>
12
13#include "testing/gtest/include/gtest/gtest.h"
14
15#include "webrtc/audio/audio_receive_stream.h"
16#include "webrtc/audio/conversion.h"
17#include "webrtc/call/mock/mock_congestion_controller.h"
18#include "webrtc/modules/bitrate_controller/include/mock/mock_bitrate_controller.h"
19#include "webrtc/modules/pacing/packet_router.h"
20#include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_estimator.h"
21#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
22#include "webrtc/modules/utility/include/mock/mock_process_thread.h"
23#include "webrtc/system_wrappers/include/clock.h"
24#include "webrtc/test/mock_voe_channel_proxy.h"
25#include "webrtc/test/mock_voice_engine.h"
26#include "webrtc/video/call_stats.h"
27
28namespace webrtc {
29namespace test {
30namespace {
31
32using testing::_;
33using testing::Return;
34
35AudioDecodingCallStats MakeAudioDecodeStatsForTest() {
36  AudioDecodingCallStats audio_decode_stats;
37  audio_decode_stats.calls_to_silence_generator = 234;
38  audio_decode_stats.calls_to_neteq = 567;
39  audio_decode_stats.decoded_normal = 890;
40  audio_decode_stats.decoded_plc = 123;
41  audio_decode_stats.decoded_cng = 456;
42  audio_decode_stats.decoded_plc_cng = 789;
43  return audio_decode_stats;
44}
45
46const int kChannelId = 2;
47const uint32_t kRemoteSsrc = 1234;
48const uint32_t kLocalSsrc = 5678;
49const size_t kOneByteExtensionHeaderLength = 4;
50const size_t kOneByteExtensionLength = 4;
51const int kAbsSendTimeId = 2;
52const int kAudioLevelId = 3;
53const int kTransportSequenceNumberId = 4;
54const int kJitterBufferDelay = -7;
55const int kPlayoutBufferDelay = 302;
56const unsigned int kSpeechOutputLevel = 99;
57const CallStatistics kCallStats = {
58    345,  678,  901, 234, -12, 3456, 7890, 567, 890, 123};
59const CodecInst kCodecInst = {
60    123, "codec_name_recv", 96000, -187, 0, -103};
61const NetworkStatistics kNetworkStats = {
62    123, 456, false, 0, 0, 789, 12, 345, 678, 901, -1, -1, -1, -1, -1, 0};
63const AudioDecodingCallStats kAudioDecodeStats = MakeAudioDecodeStatsForTest();
64
65struct ConfigHelper {
66  ConfigHelper()
67      : simulated_clock_(123456),
68        call_stats_(&simulated_clock_),
69        congestion_controller_(&process_thread_,
70                               &call_stats_,
71                               &bitrate_observer_) {
72    using testing::Invoke;
73
74    EXPECT_CALL(voice_engine_,
75        RegisterVoiceEngineObserver(_)).WillOnce(Return(0));
76    EXPECT_CALL(voice_engine_,
77        DeRegisterVoiceEngineObserver()).WillOnce(Return(0));
78    AudioState::Config config;
79    config.voice_engine = &voice_engine_;
80    audio_state_ = AudioState::Create(config);
81
82    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_, SetLocalSSRC(kLocalSsrc)).Times(1);
87          EXPECT_CALL(*channel_proxy_,
88              SetReceiveAbsoluteSenderTimeStatus(true, kAbsSendTimeId))
89                  .Times(1);
90          EXPECT_CALL(*channel_proxy_,
91              SetReceiveAudioLevelIndicationStatus(true, kAudioLevelId))
92                  .Times(1);
93          EXPECT_CALL(*channel_proxy_, SetCongestionControlObjects(
94                                           nullptr, nullptr, &packet_router_))
95              .Times(1);
96          EXPECT_CALL(congestion_controller_, packet_router())
97              .WillOnce(Return(&packet_router_));
98          EXPECT_CALL(*channel_proxy_,
99                      SetCongestionControlObjects(nullptr, nullptr, nullptr))
100              .Times(1);
101          return channel_proxy_;
102        }));
103    stream_config_.voe_channel_id = kChannelId;
104    stream_config_.rtp.local_ssrc = kLocalSsrc;
105    stream_config_.rtp.remote_ssrc = kRemoteSsrc;
106    stream_config_.rtp.extensions.push_back(
107        RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeId));
108    stream_config_.rtp.extensions.push_back(
109        RtpExtension(RtpExtension::kAudioLevel, kAudioLevelId));
110  }
111
112  MockCongestionController* congestion_controller() {
113    return &congestion_controller_;
114  }
115  MockRemoteBitrateEstimator* remote_bitrate_estimator() {
116    return &remote_bitrate_estimator_;
117  }
118  AudioReceiveStream::Config& config() { return stream_config_; }
119  rtc::scoped_refptr<AudioState> audio_state() { return audio_state_; }
120  MockVoiceEngine& voice_engine() { return voice_engine_; }
121
122  void SetupMockForBweFeedback(bool send_side_bwe) {
123    EXPECT_CALL(congestion_controller_,
124                GetRemoteBitrateEstimator(send_side_bwe))
125        .WillOnce(Return(&remote_bitrate_estimator_));
126    EXPECT_CALL(remote_bitrate_estimator_,
127                RemoveStream(stream_config_.rtp.remote_ssrc));
128  }
129
130  void SetupMockForGetStats() {
131    using testing::DoAll;
132    using testing::SetArgReferee;
133
134    ASSERT_TRUE(channel_proxy_);
135    EXPECT_CALL(*channel_proxy_, GetRTCPStatistics())
136        .WillOnce(Return(kCallStats));
137    EXPECT_CALL(*channel_proxy_, GetDelayEstimate())
138        .WillOnce(Return(kJitterBufferDelay + kPlayoutBufferDelay));
139    EXPECT_CALL(*channel_proxy_, GetSpeechOutputLevelFullRange())
140        .WillOnce(Return(kSpeechOutputLevel));
141    EXPECT_CALL(*channel_proxy_, GetNetworkStatistics())
142        .WillOnce(Return(kNetworkStats));
143    EXPECT_CALL(*channel_proxy_, GetDecodingCallStatistics())
144        .WillOnce(Return(kAudioDecodeStats));
145
146    EXPECT_CALL(voice_engine_, GetRecCodec(kChannelId, _))
147        .WillOnce(DoAll(SetArgReferee<1>(kCodecInst), Return(0)));
148  }
149
150 private:
151  SimulatedClock simulated_clock_;
152  CallStats call_stats_;
153  PacketRouter packet_router_;
154  testing::NiceMock<MockBitrateObserver> bitrate_observer_;
155  testing::NiceMock<MockProcessThread> process_thread_;
156  MockCongestionController congestion_controller_;
157  MockRemoteBitrateEstimator remote_bitrate_estimator_;
158  testing::StrictMock<MockVoiceEngine> voice_engine_;
159  rtc::scoped_refptr<AudioState> audio_state_;
160  AudioReceiveStream::Config stream_config_;
161  testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
162};
163
164void BuildOneByteExtension(std::vector<uint8_t>::iterator it,
165                           int id,
166                           uint32_t extension_value,
167                           size_t value_length) {
168  const uint16_t kRtpOneByteHeaderExtensionId = 0xBEDE;
169  ByteWriter<uint16_t>::WriteBigEndian(&(*it), kRtpOneByteHeaderExtensionId);
170  it += 2;
171
172  ByteWriter<uint16_t>::WriteBigEndian(&(*it), kOneByteExtensionLength / 4);
173  it += 2;
174  const size_t kExtensionDataLength = kOneByteExtensionLength - 1;
175  uint32_t shifted_value = extension_value
176                           << (8 * (kExtensionDataLength - value_length));
177  *it = (id << 4) + (value_length - 1);
178  ++it;
179  ByteWriter<uint32_t, kExtensionDataLength>::WriteBigEndian(&(*it),
180                                                             shifted_value);
181}
182
183std::vector<uint8_t> CreateRtpHeaderWithOneByteExtension(
184    int extension_id,
185    uint32_t extension_value,
186    size_t value_length) {
187  std::vector<uint8_t> header;
188  header.resize(webrtc::kRtpHeaderSize + kOneByteExtensionHeaderLength +
189                kOneByteExtensionLength);
190  header[0] = 0x80;   // Version 2.
191  header[0] |= 0x10;  // Set extension bit.
192  header[1] = 100;    // Payload type.
193  header[1] |= 0x80;  // Marker bit is set.
194  ByteWriter<uint16_t>::WriteBigEndian(&header[2], 0x1234);  // Sequence number.
195  ByteWriter<uint32_t>::WriteBigEndian(&header[4], 0x5678);  // Timestamp.
196  ByteWriter<uint32_t>::WriteBigEndian(&header[8], 0x4321);  // SSRC.
197
198  BuildOneByteExtension(header.begin() + webrtc::kRtpHeaderSize, extension_id,
199                        extension_value, value_length);
200  return header;
201}
202}  // namespace
203
204TEST(AudioReceiveStreamTest, ConfigToString) {
205  AudioReceiveStream::Config config;
206  config.rtp.remote_ssrc = kRemoteSsrc;
207  config.rtp.local_ssrc = kLocalSsrc;
208  config.rtp.extensions.push_back(
209      RtpExtension(RtpExtension::kAbsSendTime, kAbsSendTimeId));
210  config.voe_channel_id = kChannelId;
211  config.combined_audio_video_bwe = true;
212  EXPECT_EQ(
213      "{rtp: {remote_ssrc: 1234, local_ssrc: 5678, extensions: [{name: "
214      "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time, id: 2}]}, "
215      "receive_transport: nullptr, rtcp_send_transport: nullptr, "
216      "voe_channel_id: 2, combined_audio_video_bwe: true}",
217      config.ToString());
218}
219
220TEST(AudioReceiveStreamTest, ConstructDestruct) {
221  ConfigHelper helper;
222  internal::AudioReceiveStream recv_stream(
223      helper.congestion_controller(), helper.config(), helper.audio_state());
224}
225
226MATCHER_P(VerifyHeaderExtension, expected_extension, "") {
227  return arg.extension.hasAbsoluteSendTime ==
228             expected_extension.hasAbsoluteSendTime &&
229         arg.extension.absoluteSendTime ==
230             expected_extension.absoluteSendTime &&
231         arg.extension.hasTransportSequenceNumber ==
232             expected_extension.hasTransportSequenceNumber &&
233         arg.extension.transportSequenceNumber ==
234             expected_extension.transportSequenceNumber;
235}
236
237TEST(AudioReceiveStreamTest, AudioPacketUpdatesBweWithTimestamp) {
238  ConfigHelper helper;
239  helper.config().combined_audio_video_bwe = true;
240  helper.SetupMockForBweFeedback(false);
241  internal::AudioReceiveStream recv_stream(
242      helper.congestion_controller(), helper.config(), helper.audio_state());
243  const int kAbsSendTimeValue = 1234;
244  std::vector<uint8_t> rtp_packet =
245      CreateRtpHeaderWithOneByteExtension(kAbsSendTimeId, kAbsSendTimeValue, 3);
246  PacketTime packet_time(5678000, 0);
247  const size_t kExpectedHeaderLength = 20;
248  RTPHeaderExtension expected_extension;
249  expected_extension.hasAbsoluteSendTime = true;
250  expected_extension.absoluteSendTime = kAbsSendTimeValue;
251  EXPECT_CALL(*helper.remote_bitrate_estimator(),
252              IncomingPacket(packet_time.timestamp / 1000,
253                             rtp_packet.size() - kExpectedHeaderLength,
254                             VerifyHeaderExtension(expected_extension), false))
255      .Times(1);
256  EXPECT_TRUE(
257      recv_stream.DeliverRtp(&rtp_packet[0], rtp_packet.size(), packet_time));
258}
259
260TEST(AudioReceiveStreamTest, AudioPacketUpdatesBweFeedback) {
261  ConfigHelper helper;
262  helper.config().combined_audio_video_bwe = true;
263  helper.config().rtp.transport_cc = true;
264  helper.config().rtp.extensions.push_back(RtpExtension(
265      RtpExtension::kTransportSequenceNumber, kTransportSequenceNumberId));
266  helper.SetupMockForBweFeedback(true);
267  internal::AudioReceiveStream recv_stream(
268      helper.congestion_controller(), helper.config(), helper.audio_state());
269  const int kTransportSequenceNumberValue = 1234;
270  std::vector<uint8_t> rtp_packet = CreateRtpHeaderWithOneByteExtension(
271      kTransportSequenceNumberId, kTransportSequenceNumberValue, 2);
272  PacketTime packet_time(5678000, 0);
273  const size_t kExpectedHeaderLength = 20;
274  RTPHeaderExtension expected_extension;
275  expected_extension.hasTransportSequenceNumber = true;
276  expected_extension.transportSequenceNumber = kTransportSequenceNumberValue;
277  EXPECT_CALL(*helper.remote_bitrate_estimator(),
278              IncomingPacket(packet_time.timestamp / 1000,
279                             rtp_packet.size() - kExpectedHeaderLength,
280                             VerifyHeaderExtension(expected_extension), false))
281      .Times(1);
282  EXPECT_TRUE(
283      recv_stream.DeliverRtp(&rtp_packet[0], rtp_packet.size(), packet_time));
284}
285
286TEST(AudioReceiveStreamTest, GetStats) {
287  ConfigHelper helper;
288  internal::AudioReceiveStream recv_stream(
289      helper.congestion_controller(), helper.config(), helper.audio_state());
290  helper.SetupMockForGetStats();
291  AudioReceiveStream::Stats stats = recv_stream.GetStats();
292  EXPECT_EQ(kRemoteSsrc, stats.remote_ssrc);
293  EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesReceived), stats.bytes_rcvd);
294  EXPECT_EQ(static_cast<uint32_t>(kCallStats.packetsReceived),
295            stats.packets_rcvd);
296  EXPECT_EQ(kCallStats.cumulativeLost, stats.packets_lost);
297  EXPECT_EQ(Q8ToFloat(kCallStats.fractionLost), stats.fraction_lost);
298  EXPECT_EQ(std::string(kCodecInst.plname), stats.codec_name);
299  EXPECT_EQ(kCallStats.extendedMax, stats.ext_seqnum);
300  EXPECT_EQ(kCallStats.jitterSamples / (kCodecInst.plfreq / 1000),
301            stats.jitter_ms);
302  EXPECT_EQ(kNetworkStats.currentBufferSize, stats.jitter_buffer_ms);
303  EXPECT_EQ(kNetworkStats.preferredBufferSize,
304            stats.jitter_buffer_preferred_ms);
305  EXPECT_EQ(static_cast<uint32_t>(kJitterBufferDelay + kPlayoutBufferDelay),
306            stats.delay_estimate_ms);
307  EXPECT_EQ(static_cast<int32_t>(kSpeechOutputLevel), stats.audio_level);
308  EXPECT_EQ(Q14ToFloat(kNetworkStats.currentExpandRate), stats.expand_rate);
309  EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSpeechExpandRate),
310            stats.speech_expand_rate);
311  EXPECT_EQ(Q14ToFloat(kNetworkStats.currentSecondaryDecodedRate),
312            stats.secondary_decoded_rate);
313  EXPECT_EQ(Q14ToFloat(kNetworkStats.currentAccelerateRate),
314            stats.accelerate_rate);
315  EXPECT_EQ(Q14ToFloat(kNetworkStats.currentPreemptiveRate),
316            stats.preemptive_expand_rate);
317  EXPECT_EQ(kAudioDecodeStats.calls_to_silence_generator,
318            stats.decoding_calls_to_silence_generator);
319  EXPECT_EQ(kAudioDecodeStats.calls_to_neteq, stats.decoding_calls_to_neteq);
320  EXPECT_EQ(kAudioDecodeStats.decoded_normal, stats.decoding_normal);
321  EXPECT_EQ(kAudioDecodeStats.decoded_plc, stats.decoding_plc);
322  EXPECT_EQ(kAudioDecodeStats.decoded_cng, stats.decoding_cng);
323  EXPECT_EQ(kAudioDecodeStats.decoded_plc_cng, stats.decoding_plc_cng);
324  EXPECT_EQ(kCallStats.capture_start_ntp_time_ms_,
325            stats.capture_start_ntp_time_ms);
326}
327}  // namespace test
328}  // namespace webrtc
329