webrtc_m130/pc/audio_rtp_receiver_unittest.cc
Harald Alvestrand 2f55370634 Reland "Use two MediaChannels for 2 directions."
This reverts commit 18c869bc36b342cd4a79947067e52a93a04a7808.

Reason for revert: Added a field trial that allows landing the code without affecting performance in prod.

This CL also incorporates subsequent CLs that also had to be reverted.

Original change's description:
> Revert "Use two MediaChannels for 2 directions."
>
> This reverts commit 8981a6fac3d665beac4a58b9453e6c39988a024f.
>
> Reason for revert: Quality regression detected.
>
> Original change's description:
> > Use two MediaChannels for 2 directions.
> >
> > This CL separates the two directions of MediaChannel into two separate objects that do not couple with each other.
> >
> > The notable API change is that receiver local SSRC now has to be set explicitly - before, it was done implicitly when the send-side MediaChannel had a stream added to it.
> >
> > Bug: webrtc:13931
> > Change-Id: I83c2e3c8e79f89872d5adda1bc2899f7049748b3
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/288400
> > Commit-Queue: Harald Alvestrand <hta@webrtc.org>
> > Reviewed-by: Henrik Boström <hbos@webrtc.org>
> > Cr-Commit-Position: refs/heads/main@{#39340}
>
> No-Try: true
> Bug: webrtc:13931
> Change-Id: I791997ad9eff75c3ac9cd2e4bbacf5bc6c3a3a79
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/295663
> Commit-Queue: Harald Alvestrand <hta@webrtc.org>
> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
> Cr-Commit-Position: refs/heads/main@{#39445}

Bug: webrtc:13931
Change-Id: I1318910a685188e2b846c9040e1efc04c2c894ac
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/296080
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39494}
2023-03-07 12:57:35 +00:00

130 lines
4.5 KiB
C++

/*
* Copyright 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/audio_rtp_receiver.h"
#include <atomic>
#include "pc/test/mock_voice_media_channel.h"
#include "rtc_base/gunit.h"
#include "rtc_base/thread.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/run_loop.h"
using ::testing::_;
using ::testing::InvokeWithoutArgs;
using ::testing::Mock;
static const int kTimeOut = 100;
static const double kDefaultVolume = 1;
static const double kVolume = 3.7;
static const double kVolumeMuted = 0.0;
static const uint32_t kSsrc = 3;
namespace webrtc {
class AudioRtpReceiverTest : public ::testing::Test {
protected:
AudioRtpReceiverTest()
: worker_(rtc::Thread::Current()),
receiver_(
rtc::make_ref_counted<AudioRtpReceiver>(worker_,
std::string(),
std::vector<std::string>(),
false)),
media_channel_(cricket::MediaChannel::Role::kReceive,
rtc::Thread::Current()) {
EXPECT_CALL(media_channel_, SetRawAudioSink(kSsrc, _));
EXPECT_CALL(media_channel_, SetBaseMinimumPlayoutDelayMs(kSsrc, _));
}
~AudioRtpReceiverTest() {
EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolumeMuted));
receiver_->SetMediaChannel(nullptr);
}
rtc::AutoThread main_thread_;
rtc::Thread* worker_;
rtc::scoped_refptr<AudioRtpReceiver> receiver_;
cricket::MockVoiceMediaChannel media_channel_;
};
TEST_F(AudioRtpReceiverTest, SetOutputVolumeIsCalled) {
std::atomic_int set_volume_calls(0);
EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kDefaultVolume))
.WillOnce(InvokeWithoutArgs([&] {
set_volume_calls++;
return true;
}));
receiver_->track();
receiver_->track()->set_enabled(true);
receiver_->SetMediaChannel(media_channel_.AsVoiceReceiveChannel());
EXPECT_CALL(media_channel_, SetDefaultRawAudioSink(_)).Times(0);
receiver_->SetupMediaChannel(kSsrc);
EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume))
.WillOnce(InvokeWithoutArgs([&] {
set_volume_calls++;
return true;
}));
receiver_->OnSetVolume(kVolume);
EXPECT_TRUE_WAIT(set_volume_calls == 2, kTimeOut);
}
TEST_F(AudioRtpReceiverTest, VolumesSetBeforeStartingAreRespected) {
// Set the volume before setting the media channel. It should still be used
// as the initial volume.
receiver_->OnSetVolume(kVolume);
receiver_->track()->set_enabled(true);
receiver_->SetMediaChannel(media_channel_.AsVoiceReceiveChannel());
// The previosly set initial volume should be propagated to the provided
// media_channel_ as soon as SetupMediaChannel is called.
EXPECT_CALL(media_channel_, SetOutputVolume(kSsrc, kVolume));
receiver_->SetupMediaChannel(kSsrc);
}
// Tests that OnChanged notifications are processed correctly on the worker
// thread when a media channel pointer is passed to the receiver via the
// constructor.
TEST(AudioRtpReceiver, OnChangedNotificationsAfterConstruction) {
webrtc::test::RunLoop loop;
auto* thread = rtc::Thread::Current(); // Points to loop's thread.
cricket::MockVoiceMediaChannel media_channel(
cricket::MediaChannel::Role::kReceive, thread);
auto receiver = rtc::make_ref_counted<AudioRtpReceiver>(
thread, std::string(), std::vector<std::string>(), true, &media_channel);
EXPECT_CALL(media_channel, SetDefaultRawAudioSink(_)).Times(1);
EXPECT_CALL(media_channel, SetDefaultOutputVolume(kDefaultVolume)).Times(1);
receiver->SetupUnsignaledMediaChannel();
loop.Flush();
// Mark the track as disabled.
receiver->track()->set_enabled(false);
// When the track was marked as disabled, an async notification was queued
// for the worker thread. This notification should trigger the volume
// of the media channel to be set to kVolumeMuted.
// Flush the worker thread, but set the expectation first for the call.
EXPECT_CALL(media_channel, SetDefaultOutputVolume(kVolumeMuted)).Times(1);
loop.Flush();
EXPECT_CALL(media_channel, SetDefaultOutputVolume(kVolumeMuted)).Times(1);
receiver->SetMediaChannel(nullptr);
}
} // namespace webrtc