blob: 1865115b2ec48133adec3e83894b67b8bc83cade [file] [log] [blame]
Tony Herree2044662021-11-29 11:33:42 +01001/*
2 * Copyright 2021 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 "pc/audio_rtp_receiver.h"
12
13#include "media/base/media_channel.h"
14#include "pc/test/mock_voice_media_channel.h"
15#include "rtc_base/gunit.h"
16#include "rtc_base/thread.h"
17#include "test/gmock.h"
18#include "test/gtest.h"
19
20using ::testing::_;
21using ::testing::InvokeWithoutArgs;
22using ::testing::Mock;
23
24static const int kTimeOut = 100;
25static const double kDefaultVolume = 1;
26static const double kVolume = 3.7;
27static const uint32_t kSsrc = 3;
28
29namespace webrtc {
30class AudioRtpReceiverTest : public ::testing::Test {
31 protected:
32 AudioRtpReceiverTest()
33 : worker_(rtc::Thread::Current()),
34 receiver_(
35 rtc::make_ref_counted<AudioRtpReceiver>(worker_,
36 std::string(),
37 std::vector<std::string>(),
38 false)),
39 media_channel_(rtc::Thread::Current()) {
40 EXPECT_CALL(media_channel_, SetRawAudioSink(kSsrc, _));
41 EXPECT_CALL(media_channel_, SetBaseMinimumPlayoutDelayMs(kSsrc, _));
42 }
43
44 ~AudioRtpReceiverTest() {
45 receiver_->SetMediaChannel(nullptr);
46 receiver_->Stop();
47 }
48
49 rtc::Thread* worker_;
50 rtc::scoped_refptr<AudioRtpReceiver> receiver_;
51 cricket::MockVoiceMediaChannel media_channel_;
52};
53
54TEST_F(AudioRtpReceiverTest, SetOutputVolumeIsCalled) {
55 std::atomic_int set_volume_calls(0);
56
57 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kDefaultVolume))
58 .WillOnce(InvokeWithoutArgs([&] {
59 set_volume_calls++;
60 return true;
61 }));
62
63 receiver_->track();
64 receiver_->track()->set_enabled(true);
65 receiver_->SetMediaChannel(&media_channel_);
66 receiver_->SetupMediaChannel(kSsrc);
67
68 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume))
69 .WillOnce(InvokeWithoutArgs([&] {
70 set_volume_calls++;
71 return true;
72 }));
73
74 receiver_->OnSetVolume(kVolume);
75 EXPECT_TRUE_WAIT(set_volume_calls == 2, kTimeOut);
76}
77
78TEST_F(AudioRtpReceiverTest, VolumesSetBeforeStartingAreRespected) {
79 // Set the volume before setting the media channel. It should still be used
80 // as the initial volume.
81 receiver_->OnSetVolume(kVolume);
82
83 receiver_->track()->set_enabled(true);
84 receiver_->SetMediaChannel(&media_channel_);
85
86 // The previosly set initial volume should be propagated to the provided
87 // media_channel_ as soon as SetupMediaChannel is called.
88 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume));
89
90 receiver_->SetupMediaChannel(kSsrc);
91}
92} // namespace webrtc