blob: 294e5805252c46ef3855e72566370da68e77675a [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;
Tommi6589def2022-02-17 23:36:47 +010027static const double kVolumeMuted = 0.0;
Tony Herree2044662021-11-29 11:33:42 +010028static const uint32_t kSsrc = 3;
29
30namespace webrtc {
31class AudioRtpReceiverTest : public ::testing::Test {
32 protected:
33 AudioRtpReceiverTest()
34 : worker_(rtc::Thread::Current()),
35 receiver_(
36 rtc::make_ref_counted<AudioRtpReceiver>(worker_,
37 std::string(),
38 std::vector<std::string>(),
39 false)),
40 media_channel_(rtc::Thread::Current()) {
41 EXPECT_CALL(media_channel_, SetRawAudioSink(kSsrc, _));
42 EXPECT_CALL(media_channel_, SetBaseMinimumPlayoutDelayMs(kSsrc, _));
43 }
44
45 ~AudioRtpReceiverTest() {
Tommi6589def2022-02-17 23:36:47 +010046 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolumeMuted));
Tony Herree2044662021-11-29 11:33:42 +010047 receiver_->SetMediaChannel(nullptr);
Tony Herree2044662021-11-29 11:33:42 +010048 }
49
50 rtc::Thread* worker_;
51 rtc::scoped_refptr<AudioRtpReceiver> receiver_;
52 cricket::MockVoiceMediaChannel media_channel_;
53};
54
55TEST_F(AudioRtpReceiverTest, SetOutputVolumeIsCalled) {
56 std::atomic_int set_volume_calls(0);
57
58 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kDefaultVolume))
59 .WillOnce(InvokeWithoutArgs([&] {
60 set_volume_calls++;
61 return true;
62 }));
63
64 receiver_->track();
65 receiver_->track()->set_enabled(true);
66 receiver_->SetMediaChannel(&media_channel_);
Tommi20d8d912022-02-08 21:12:15 +010067 EXPECT_CALL(media_channel_, SetDefaultRawAudioSink(_)).Times(0);
Tony Herree2044662021-11-29 11:33:42 +010068 receiver_->SetupMediaChannel(kSsrc);
69
70 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume))
71 .WillOnce(InvokeWithoutArgs([&] {
72 set_volume_calls++;
73 return true;
74 }));
75
76 receiver_->OnSetVolume(kVolume);
77 EXPECT_TRUE_WAIT(set_volume_calls == 2, kTimeOut);
78}
79
80TEST_F(AudioRtpReceiverTest, VolumesSetBeforeStartingAreRespected) {
81 // Set the volume before setting the media channel. It should still be used
82 // as the initial volume.
83 receiver_->OnSetVolume(kVolume);
84
85 receiver_->track()->set_enabled(true);
86 receiver_->SetMediaChannel(&media_channel_);
87
88 // The previosly set initial volume should be propagated to the provided
89 // media_channel_ as soon as SetupMediaChannel is called.
90 EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume));
91
92 receiver_->SetupMediaChannel(kSsrc);
93}
94} // namespace webrtc