Misc improvements to RtpTransceiver unit tests and test utils.

In order to reduce the size and scope of a follow-up CL, this CL makes
some cleaning up and improvements to existing tests and adds some minor
test utility methods that will be used in the follow-up.

No change in behavior, this CL...
- Makes use of NiceMock in RtpTransceiver tests to avoid wall of text
  spam for various "uninteresting" method calls in all tests in this
  file.
- Refactors creating senders, receivers and transceivers to allow the
  follow-up CL to create such objects for kind "video" as well.
- Exposes cricket::FakeVideoEngine* to RtpTranscieverTest and allows
  adding unidirectional video codecs in the fake engine, to be used by
  the follow-up CL's tests.
- Allows creating fake video engine codecs from SdpVideoFormat in the
  fake decoder factory (already possible in the fake encoder factory).

Bug: chromium:381407888
Change-Id: Ie07eff79d832dd21800b95fd584891ebf4520798
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/374900
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43776}
This commit is contained in:
Henrik Boström 2025-01-20 14:31:12 +01:00 committed by WebRTC LUCI CQ
parent ee7371f1f8
commit 860a13c6fd
5 changed files with 79 additions and 66 deletions

View File

@ -688,5 +688,11 @@ void FakeMediaEngine::SetVideoCodecs(const std::vector<Codec>& codecs) {
video_->SetSendCodecs(codecs); video_->SetSendCodecs(codecs);
video_->SetRecvCodecs(codecs); video_->SetRecvCodecs(codecs);
} }
void FakeMediaEngine::SetVideoRecvCodecs(const std::vector<Codec>& codecs) {
video_->SetRecvCodecs(codecs);
}
void FakeMediaEngine::SetVideoSendCodecs(const std::vector<Codec>& codecs) {
video_->SetSendCodecs(codecs);
}
} // namespace cricket } // namespace cricket

View File

@ -861,6 +861,8 @@ class FakeMediaEngine : public CompositeMediaEngine {
void SetAudioRecvCodecs(const std::vector<Codec>& codecs); void SetAudioRecvCodecs(const std::vector<Codec>& codecs);
void SetAudioSendCodecs(const std::vector<Codec>& codecs); void SetAudioSendCodecs(const std::vector<Codec>& codecs);
void SetVideoCodecs(const std::vector<Codec>& codecs); void SetVideoCodecs(const std::vector<Codec>& codecs);
void SetVideoRecvCodecs(const std::vector<Codec>& codecs);
void SetVideoSendCodecs(const std::vector<Codec>& codecs);
FakeVoiceEngine* fake_voice_engine() { return voice_; } FakeVoiceEngine* fake_voice_engine() { return voice_; }
FakeVideoEngine* fake_video_engine() { return video_; } FakeVideoEngine* fake_video_engine() { return video_; }

View File

@ -117,6 +117,11 @@ void FakeWebRtcVideoDecoderFactory::DecoderDestroyed(
decoders_.end()); decoders_.end());
} }
void FakeWebRtcVideoDecoderFactory::AddSupportedVideoCodec(
const webrtc::SdpVideoFormat& format) {
supported_codec_formats_.push_back(format);
}
void FakeWebRtcVideoDecoderFactory::AddSupportedVideoCodecType( void FakeWebRtcVideoDecoderFactory::AddSupportedVideoCodecType(
const std::string& name) { const std::string& name) {
// This is to match the default H264 params of cricket::Codec. // This is to match the default H264 params of cricket::Codec.

View File

@ -69,6 +69,7 @@ class FakeWebRtcVideoDecoderFactory : public webrtc::VideoDecoderFactory {
const webrtc::SdpVideoFormat& format) override; const webrtc::SdpVideoFormat& format) override;
void DecoderDestroyed(FakeWebRtcVideoDecoder* decoder); void DecoderDestroyed(FakeWebRtcVideoDecoder* decoder);
void AddSupportedVideoCodec(const webrtc::SdpVideoFormat& format);
void AddSupportedVideoCodecType(const std::string& name); void AddSupportedVideoCodecType(const std::string& name);
int GetNumCreatedDecoders(); int GetNumCreatedDecoders();
const std::vector<FakeWebRtcVideoDecoder*>& decoders(); const std::vector<FakeWebRtcVideoDecoder*>& decoders();

View File

@ -20,7 +20,7 @@
#include "api/environment/environment_factory.h" #include "api/environment/environment_factory.h"
#include "api/peer_connection_interface.h" #include "api/peer_connection_interface.h"
#include "api/rtp_parameters.h" #include "api/rtp_parameters.h"
#include "media/base/media_engine.h" #include "media/base/fake_media_engine.h"
#include "pc/test/enable_fake_media.h" #include "pc/test/enable_fake_media.h"
#include "pc/test/mock_channel_interface.h" #include "pc/test/mock_channel_interface.h"
#include "pc/test/mock_rtp_receiver_internal.h" #include "pc/test/mock_rtp_receiver_internal.h"
@ -32,6 +32,7 @@
using ::testing::_; using ::testing::_;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::Field; using ::testing::Field;
using ::testing::NiceMock;
using ::testing::Optional; using ::testing::Optional;
using ::testing::Property; using ::testing::Property;
using ::testing::Return; using ::testing::Return;
@ -49,8 +50,10 @@ class RtpTransceiverTest : public testing::Test {
ConnectionContext::Create(CreateEnvironment(), &dependencies_)) {} ConnectionContext::Create(CreateEnvironment(), &dependencies_)) {}
protected: protected:
cricket::MediaEngineInterface* media_engine() { cricket::FakeMediaEngine* media_engine() {
return context_->media_engine(); // We know this cast is safe because we supplied the fake implementation
// in MakeDependencies().
return static_cast<cricket::FakeMediaEngine*>(context_->media_engine());
} }
ConnectionContext* context() { return context_.get(); } ConnectionContext* context() { return context_.get(); }
@ -62,7 +65,7 @@ class RtpTransceiverTest : public testing::Test {
d.network_thread = rtc::Thread::Current(); d.network_thread = rtc::Thread::Current();
d.worker_thread = rtc::Thread::Current(); d.worker_thread = rtc::Thread::Current();
d.signaling_thread = rtc::Thread::Current(); d.signaling_thread = rtc::Thread::Current();
EnableFakeMedia(d); EnableFakeMedia(d, std::make_unique<cricket::FakeMediaEngine>());
return d; return d;
} }
@ -75,7 +78,7 @@ TEST_F(RtpTransceiverTest, CannotSetChannelOnStoppedTransceiver) {
const std::string content_name("my_mid"); const std::string content_name("my_mid");
auto transceiver = rtc::make_ref_counted<RtpTransceiver>( auto transceiver = rtc::make_ref_counted<RtpTransceiver>(
cricket::MediaType::MEDIA_TYPE_AUDIO, context()); cricket::MediaType::MEDIA_TYPE_AUDIO, context());
auto channel1 = std::make_unique<cricket::MockChannelInterface>(); auto channel1 = std::make_unique<NiceMock<cricket::MockChannelInterface>>();
EXPECT_CALL(*channel1, media_type()) EXPECT_CALL(*channel1, media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO)); .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
EXPECT_CALL(*channel1, mid()).WillRepeatedly(ReturnRef(content_name)); EXPECT_CALL(*channel1, mid()).WillRepeatedly(ReturnRef(content_name));
@ -92,7 +95,7 @@ TEST_F(RtpTransceiverTest, CannotSetChannelOnStoppedTransceiver) {
transceiver->StopInternal(); transceiver->StopInternal();
EXPECT_EQ(channel1_ptr, transceiver->channel()); EXPECT_EQ(channel1_ptr, transceiver->channel());
auto channel2 = std::make_unique<cricket::MockChannelInterface>(); auto channel2 = std::make_unique<NiceMock<cricket::MockChannelInterface>>();
EXPECT_CALL(*channel2, media_type()) EXPECT_CALL(*channel2, media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO)); .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
@ -111,7 +114,7 @@ TEST_F(RtpTransceiverTest, CanUnsetChannelOnStoppedTransceiver) {
const std::string content_name("my_mid"); const std::string content_name("my_mid");
auto transceiver = rtc::make_ref_counted<RtpTransceiver>( auto transceiver = rtc::make_ref_counted<RtpTransceiver>(
cricket::MediaType::MEDIA_TYPE_VIDEO, context()); cricket::MediaType::MEDIA_TYPE_VIDEO, context());
auto channel = std::make_unique<cricket::MockChannelInterface>(); auto channel = std::make_unique<NiceMock<cricket::MockChannelInterface>>();
EXPECT_CALL(*channel, media_type()) EXPECT_CALL(*channel, media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_VIDEO)); .WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_VIDEO));
EXPECT_CALL(*channel, mid()).WillRepeatedly(ReturnRef(content_name)); EXPECT_CALL(*channel, mid()).WillRepeatedly(ReturnRef(content_name));
@ -137,59 +140,65 @@ TEST_F(RtpTransceiverTest, CanUnsetChannelOnStoppedTransceiver) {
class RtpTransceiverUnifiedPlanTest : public RtpTransceiverTest { class RtpTransceiverUnifiedPlanTest : public RtpTransceiverTest {
public: public:
RtpTransceiverUnifiedPlanTest() static rtc::scoped_refptr<MockRtpReceiverInternal> MockReceiver(
: transceiver_(rtc::make_ref_counted<RtpTransceiver>( cricket::MediaType media_type) {
RtpSenderProxyWithInternal<RtpSenderInternal>::Create( auto receiver = rtc::make_ref_counted<NiceMock<MockRtpReceiverInternal>>();
rtc::Thread::Current(),
sender_),
RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
rtc::Thread::Current(),
rtc::Thread::Current(),
receiver_),
context(),
media_engine()->voice().GetRtpHeaderExtensions(),
/* on_negotiation_needed= */ [] {})) {}
static rtc::scoped_refptr<MockRtpReceiverInternal> MockReceiver() {
auto receiver = rtc::make_ref_counted<MockRtpReceiverInternal>();
EXPECT_CALL(*receiver.get(), media_type()) EXPECT_CALL(*receiver.get(), media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO)); .WillRepeatedly(Return(media_type));
return receiver; return receiver;
} }
static rtc::scoped_refptr<MockRtpSenderInternal> MockSender() { static rtc::scoped_refptr<MockRtpSenderInternal> MockSender(
auto sender = rtc::make_ref_counted<MockRtpSenderInternal>(); cricket::MediaType media_type) {
EXPECT_CALL(*sender.get(), media_type()) auto sender = rtc::make_ref_counted<NiceMock<MockRtpSenderInternal>>();
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO)); EXPECT_CALL(*sender.get(), media_type()).WillRepeatedly(Return(media_type));
return sender; return sender;
} }
rtc::scoped_refptr<RtpTransceiver> CreateTransceiver(
rtc::scoped_refptr<RtpSenderInternal> sender,
rtc::scoped_refptr<RtpReceiverInternal> receiver) {
return rtc::make_ref_counted<RtpTransceiver>(
RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
rtc::Thread::Current(), std::move(sender)),
RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
rtc::Thread::Current(), rtc::Thread::Current(),
std::move(receiver)),
context(), media_engine()->voice().GetRtpHeaderExtensions(),
/* on_negotiation_needed= */ [] {});
}
rtc::AutoThread main_thread_; rtc::AutoThread main_thread_;
rtc::scoped_refptr<MockRtpReceiverInternal> receiver_ = MockReceiver();
rtc::scoped_refptr<MockRtpSenderInternal> sender_ = MockSender();
rtc::scoped_refptr<RtpTransceiver> transceiver_;
}; };
// Basic tests for Stop() // Basic tests for Stop()
TEST_F(RtpTransceiverUnifiedPlanTest, StopSetsDirection) { TEST_F(RtpTransceiverUnifiedPlanTest, StopSetsDirection) {
EXPECT_CALL(*receiver_.get(), Stop()); rtc::scoped_refptr<MockRtpReceiverInternal> receiver =
EXPECT_CALL(*receiver_.get(), SetMediaChannel(_)); MockReceiver(cricket::MediaType::MEDIA_TYPE_AUDIO);
EXPECT_CALL(*sender_.get(), SetTransceiverAsStopped()); rtc::scoped_refptr<MockRtpSenderInternal> sender =
EXPECT_CALL(*sender_.get(), Stop()); MockSender(cricket::MediaType::MEDIA_TYPE_AUDIO);
rtc::scoped_refptr<RtpTransceiver> transceiver =
CreateTransceiver(sender, receiver);
EXPECT_EQ(RtpTransceiverDirection::kInactive, transceiver_->direction()); EXPECT_CALL(*receiver.get(), Stop());
EXPECT_FALSE(transceiver_->current_direction()); EXPECT_CALL(*receiver.get(), SetMediaChannel(_));
transceiver_->StopStandard(); EXPECT_CALL(*sender.get(), SetTransceiverAsStopped());
EXPECT_EQ(RtpTransceiverDirection::kStopped, transceiver_->direction()); EXPECT_CALL(*sender.get(), Stop());
EXPECT_FALSE(transceiver_->current_direction());
transceiver_->StopTransceiverProcedure(); EXPECT_EQ(RtpTransceiverDirection::kInactive, transceiver->direction());
EXPECT_TRUE(transceiver_->current_direction()); EXPECT_FALSE(transceiver->current_direction());
EXPECT_EQ(RtpTransceiverDirection::kStopped, transceiver_->direction()); transceiver->StopStandard();
EXPECT_EQ(RtpTransceiverDirection::kStopped, transceiver->direction());
EXPECT_FALSE(transceiver->current_direction());
transceiver->StopTransceiverProcedure();
EXPECT_TRUE(transceiver->current_direction());
EXPECT_EQ(RtpTransceiverDirection::kStopped, transceiver->direction());
EXPECT_EQ(RtpTransceiverDirection::kStopped, EXPECT_EQ(RtpTransceiverDirection::kStopped,
*transceiver_->current_direction()); *transceiver->current_direction());
} }
class RtpTransceiverTestForHeaderExtensions : public RtpTransceiverTest { class RtpTransceiverTestForHeaderExtensions
: public RtpTransceiverUnifiedPlanTest {
public: public:
RtpTransceiverTestForHeaderExtensions() RtpTransceiverTestForHeaderExtensions()
: extensions_( : extensions_(
@ -217,28 +226,15 @@ class RtpTransceiverTestForHeaderExtensions : public RtpTransceiverTest {
extensions_, extensions_,
/* on_negotiation_needed= */ [] {})) {} /* on_negotiation_needed= */ [] {})) {}
static rtc::scoped_refptr<MockRtpReceiverInternal> MockReceiver() {
auto receiver = rtc::make_ref_counted<MockRtpReceiverInternal>();
EXPECT_CALL(*receiver.get(), media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
return receiver;
}
static rtc::scoped_refptr<MockRtpSenderInternal> MockSender() {
auto sender = rtc::make_ref_counted<MockRtpSenderInternal>();
EXPECT_CALL(*sender.get(), media_type())
.WillRepeatedly(Return(cricket::MediaType::MEDIA_TYPE_AUDIO));
return sender;
}
void ClearChannel() { void ClearChannel() {
EXPECT_CALL(*sender_.get(), SetMediaChannel(_)); EXPECT_CALL(*sender_.get(), SetMediaChannel(_));
transceiver_->ClearChannel(); transceiver_->ClearChannel();
} }
rtc::AutoThread main_thread_; rtc::scoped_refptr<MockRtpReceiverInternal> receiver_ =
rtc::scoped_refptr<MockRtpReceiverInternal> receiver_ = MockReceiver(); MockReceiver(cricket::MediaType::MEDIA_TYPE_AUDIO);
rtc::scoped_refptr<MockRtpSenderInternal> sender_ = MockSender(); rtc::scoped_refptr<MockRtpSenderInternal> sender_ =
MockSender(cricket::MediaType::MEDIA_TYPE_AUDIO);
std::vector<RtpHeaderExtensionCapability> extensions_; std::vector<RtpHeaderExtensionCapability> extensions_;
rtc::scoped_refptr<RtpTransceiver> transceiver_; rtc::scoped_refptr<RtpTransceiver> transceiver_;
@ -380,7 +376,8 @@ TEST_F(RtpTransceiverTestForHeaderExtensions,
EXPECT_CALL(*sender_.get(), SetMediaChannel(_)); EXPECT_CALL(*sender_.get(), SetMediaChannel(_));
EXPECT_CALL(*sender_.get(), SetTransceiverAsStopped()); EXPECT_CALL(*sender_.get(), SetTransceiverAsStopped());
EXPECT_CALL(*sender_.get(), Stop()); EXPECT_CALL(*sender_.get(), Stop());
auto mock_channel = std::make_unique<cricket::MockChannelInterface>(); auto mock_channel =
std::make_unique<NiceMock<cricket::MockChannelInterface>>();
auto mock_channel_ptr = mock_channel.get(); auto mock_channel_ptr = mock_channel.get();
EXPECT_CALL(*mock_channel, SetFirstPacketReceivedCallback(_)); EXPECT_CALL(*mock_channel, SetFirstPacketReceivedCallback(_));
EXPECT_CALL(*mock_channel, media_type()) EXPECT_CALL(*mock_channel, media_type())
@ -413,7 +410,8 @@ TEST_F(RtpTransceiverTestForHeaderExtensions, ReturnsNegotiatedHdrExts) {
EXPECT_CALL(*sender_.get(), SetTransceiverAsStopped()); EXPECT_CALL(*sender_.get(), SetTransceiverAsStopped());
EXPECT_CALL(*sender_.get(), Stop()); EXPECT_CALL(*sender_.get(), Stop());
auto mock_channel = std::make_unique<cricket::MockChannelInterface>(); auto mock_channel =
std::make_unique<NiceMock<cricket::MockChannelInterface>>();
auto mock_channel_ptr = mock_channel.get(); auto mock_channel_ptr = mock_channel.get();
EXPECT_CALL(*mock_channel, SetFirstPacketReceivedCallback(_)); EXPECT_CALL(*mock_channel, SetFirstPacketReceivedCallback(_));
EXPECT_CALL(*mock_channel, media_type()) EXPECT_CALL(*mock_channel, media_type())
@ -492,7 +490,7 @@ TEST_F(RtpTransceiverTestForHeaderExtensions,
}; };
// Default is stopped. // Default is stopped.
auto sender = rtc::make_ref_counted<MockRtpSenderInternal>(); auto sender = rtc::make_ref_counted<NiceMock<MockRtpSenderInternal>>();
auto transceiver = rtc::make_ref_counted<RtpTransceiver>( auto transceiver = rtc::make_ref_counted<RtpTransceiver>(
RtpSenderProxyWithInternal<RtpSenderInternal>::Create( RtpSenderProxyWithInternal<RtpSenderInternal>::Create(
rtc::Thread::Current(), sender), rtc::Thread::Current(), sender),
@ -511,7 +509,8 @@ TEST_F(RtpTransceiverTestForHeaderExtensions,
// Simulcast, i.e. more than one encoding. // Simulcast, i.e. more than one encoding.
RtpParameters simulcast_parameters; RtpParameters simulcast_parameters;
simulcast_parameters.encodings.resize(2); simulcast_parameters.encodings.resize(2);
auto simulcast_sender = rtc::make_ref_counted<MockRtpSenderInternal>(); auto simulcast_sender =
rtc::make_ref_counted<NiceMock<MockRtpSenderInternal>>();
EXPECT_CALL(*simulcast_sender, GetParametersInternal()) EXPECT_CALL(*simulcast_sender, GetParametersInternal())
.WillRepeatedly(Return(simulcast_parameters)); .WillRepeatedly(Return(simulcast_parameters));
auto simulcast_transceiver = rtc::make_ref_counted<RtpTransceiver>( auto simulcast_transceiver = rtc::make_ref_counted<RtpTransceiver>(
@ -538,7 +537,7 @@ TEST_F(RtpTransceiverTestForHeaderExtensions,
svc_parameters.encodings.resize(1); svc_parameters.encodings.resize(1);
svc_parameters.encodings[0].scalability_mode = "L3T3"; svc_parameters.encodings[0].scalability_mode = "L3T3";
auto svc_sender = rtc::make_ref_counted<MockRtpSenderInternal>(); auto svc_sender = rtc::make_ref_counted<NiceMock<MockRtpSenderInternal>>();
EXPECT_CALL(*svc_sender, GetParametersInternal()) EXPECT_CALL(*svc_sender, GetParametersInternal())
.WillRepeatedly(Return(svc_parameters)); .WillRepeatedly(Return(svc_parameters));
auto svc_transceiver = rtc::make_ref_counted<RtpTransceiver>( auto svc_transceiver = rtc::make_ref_counted<RtpTransceiver>(