Tony Herre | e204466 | 2021-11-29 11:33:42 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | using ::testing::_; |
| 21 | using ::testing::InvokeWithoutArgs; |
| 22 | using ::testing::Mock; |
| 23 | |
| 24 | static const int kTimeOut = 100; |
| 25 | static const double kDefaultVolume = 1; |
| 26 | static const double kVolume = 3.7; |
Tommi | 6589def | 2022-02-17 23:36:47 +0100 | [diff] [blame] | 27 | static const double kVolumeMuted = 0.0; |
Tony Herre | e204466 | 2021-11-29 11:33:42 +0100 | [diff] [blame] | 28 | static const uint32_t kSsrc = 3; |
| 29 | |
| 30 | namespace webrtc { |
| 31 | class 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() { |
Tommi | 6589def | 2022-02-17 23:36:47 +0100 | [diff] [blame] | 46 | EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolumeMuted)); |
Tony Herre | e204466 | 2021-11-29 11:33:42 +0100 | [diff] [blame] | 47 | receiver_->SetMediaChannel(nullptr); |
Tony Herre | e204466 | 2021-11-29 11:33:42 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | rtc::Thread* worker_; |
| 51 | rtc::scoped_refptr<AudioRtpReceiver> receiver_; |
| 52 | cricket::MockVoiceMediaChannel media_channel_; |
| 53 | }; |
| 54 | |
| 55 | TEST_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_); |
Tommi | 20d8d91 | 2022-02-08 21:12:15 +0100 | [diff] [blame] | 67 | EXPECT_CALL(media_channel_, SetDefaultRawAudioSink(_)).Times(0); |
Tony Herre | e204466 | 2021-11-29 11:33:42 +0100 | [diff] [blame] | 68 | 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 | |
| 80 | TEST_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 |