blob: e031f9035995992d9982c67167c8807ce02f41c4 [file] [log] [blame]
Tony Herree2044662021-11-29 10:33:421/*
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 Alvestrandc24a2182022-02-23 13:44:5913#include <atomic>
14
Florent Castellid797cb62023-06-27 20:07:0915#include "pc/test/mock_voice_media_receive_channel_interface.h"
Tony Herree2044662021-11-29 10:33:4216#include "rtc_base/gunit.h"
17#include "rtc_base/thread.h"
18#include "test/gmock.h"
19#include "test/gtest.h"
Tommied3832b2022-03-22 10:54:0920#include "test/run_loop.h"
Tony Herree2044662021-11-29 10:33:4221
22using ::testing::_;
23using ::testing::InvokeWithoutArgs;
24using ::testing::Mock;
25
26static const int kTimeOut = 100;
27static const double kDefaultVolume = 1;
28static const double kVolume = 3.7;
Tommi6589def2022-02-17 22:36:4729static const double kVolumeMuted = 0.0;
Tony Herree2044662021-11-29 10:33:4230static const uint32_t kSsrc = 3;
31
32namespace webrtc {
33class 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>(),
Florent Castellid797cb62023-06-27 20:07:0941 false)) {
42 EXPECT_CALL(receive_channel_, SetRawAudioSink(kSsrc, _));
43 EXPECT_CALL(receive_channel_, SetBaseMinimumPlayoutDelayMs(kSsrc, _));
Tony Herree2044662021-11-29 10:33:4244 }
45
46 ~AudioRtpReceiverTest() {
Florent Castellid797cb62023-06-27 20:07:0947 EXPECT_CALL(receive_channel_, SetOutputVolume(kSsrc, kVolumeMuted));
Tony Herree2044662021-11-29 10:33:4248 receiver_->SetMediaChannel(nullptr);
Tony Herree2044662021-11-29 10:33:4249 }
50
Niels Möller83830f32022-05-20 07:12:5751 rtc::AutoThread main_thread_;
Tony Herree2044662021-11-29 10:33:4252 rtc::Thread* worker_;
53 rtc::scoped_refptr<AudioRtpReceiver> receiver_;
Florent Castellid797cb62023-06-27 20:07:0954 cricket::MockVoiceMediaReceiveChannelInterface receive_channel_;
Tony Herree2044662021-11-29 10:33:4255};
56
57TEST_F(AudioRtpReceiverTest, SetOutputVolumeIsCalled) {
58 std::atomic_int set_volume_calls(0);
59
Florent Castellid797cb62023-06-27 20:07:0960 EXPECT_CALL(receive_channel_, SetOutputVolume(kSsrc, kDefaultVolume))
Tony Herree2044662021-11-29 10:33:4261 .WillOnce(InvokeWithoutArgs([&] {
62 set_volume_calls++;
63 return true;
64 }));
65
66 receiver_->track();
67 receiver_->track()->set_enabled(true);
Florent Castellid797cb62023-06-27 20:07:0968 receiver_->SetMediaChannel(&receive_channel_);
69 EXPECT_CALL(receive_channel_, SetDefaultRawAudioSink(_)).Times(0);
Tony Herree2044662021-11-29 10:33:4270 receiver_->SetupMediaChannel(kSsrc);
71
Florent Castellid797cb62023-06-27 20:07:0972 EXPECT_CALL(receive_channel_, SetOutputVolume(kSsrc, kVolume))
Tony Herree2044662021-11-29 10:33:4273 .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
82TEST_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);
Florent Castellid797cb62023-06-27 20:07:0988 receiver_->SetMediaChannel(&receive_channel_);
Tony Herree2044662021-11-29 10:33:4289
90 // The previosly set initial volume should be propagated to the provided
91 // media_channel_ as soon as SetupMediaChannel is called.
Florent Castellid797cb62023-06-27 20:07:0992 EXPECT_CALL(receive_channel_, SetOutputVolume(kSsrc, kVolume));
Tony Herree2044662021-11-29 10:33:4293
94 receiver_->SetupMediaChannel(kSsrc);
95}
Tommied3832b2022-03-22 10:54:0996
97// Tests that OnChanged notifications are processed correctly on the worker
98// thread when a media channel pointer is passed to the receiver via the
99// constructor.
100TEST(AudioRtpReceiver, OnChangedNotificationsAfterConstruction) {
Harald Alvestranda6544372023-11-13 09:33:56101 test::RunLoop loop;
Tommied3832b2022-03-22 10:54:09102 auto* thread = rtc::Thread::Current(); // Points to loop's thread.
Florent Castellid797cb62023-06-27 20:07:09103 cricket::MockVoiceMediaReceiveChannelInterface receive_channel;
Tommied3832b2022-03-22 10:54:09104 auto receiver = rtc::make_ref_counted<AudioRtpReceiver>(
Florent Castellid797cb62023-06-27 20:07:09105 thread, std::string(), std::vector<std::string>(), true,
106 &receive_channel);
Tommied3832b2022-03-22 10:54:09107
Florent Castellid797cb62023-06-27 20:07:09108 EXPECT_CALL(receive_channel, SetDefaultRawAudioSink(_)).Times(1);
109 EXPECT_CALL(receive_channel, SetDefaultOutputVolume(kDefaultVolume)).Times(1);
Tommied3832b2022-03-22 10:54:09110 receiver->SetupUnsignaledMediaChannel();
111 loop.Flush();
112
113 // Mark the track as disabled.
114 receiver->track()->set_enabled(false);
115
116 // When the track was marked as disabled, an async notification was queued
117 // for the worker thread. This notification should trigger the volume
118 // of the media channel to be set to kVolumeMuted.
119 // Flush the worker thread, but set the expectation first for the call.
Florent Castellid797cb62023-06-27 20:07:09120 EXPECT_CALL(receive_channel, SetDefaultOutputVolume(kVolumeMuted)).Times(1);
Tommied3832b2022-03-22 10:54:09121 loop.Flush();
122
Florent Castellid797cb62023-06-27 20:07:09123 EXPECT_CALL(receive_channel, SetDefaultOutputVolume(kVolumeMuted)).Times(1);
Tommied3832b2022-03-22 10:54:09124 receiver->SetMediaChannel(nullptr);
125}
126
Tony Herree2044662021-11-29 10:33:42127} // namespace webrtc