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