123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #include "pch.h"
- #include <rtc_base/ref_counted_object.h>
- #include "Context.h"
- #include "GraphicsDevice/IGraphicsDevice.h"
- #include "GraphicsDevice/ITexture2D.h"
- #include "GraphicsDeviceContainer.h"
- #include "GraphicsDeviceTestBase.h"
- #include "VideoFrameUtil.h"
- namespace unity
- {
- namespace webrtc
- {
- using namespace ::webrtc;
- class ContextTest : public testing::TestWithParam<UnityGfxRenderer>
- {
- protected:
- const uint32_t kWidth = 256;
- const uint32_t kHeight = 256;
- const UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
- std::unique_ptr<GraphicsDeviceContainer> container_;
- std::unique_ptr<Context> context;
- IGraphicsDevice* device_;
- std::unique_ptr<ITexture2D> texture_;
- DelegateVideoFrameResize callback_videoframeresize;
- explicit ContextTest()
- : container_(CreateGraphicsDeviceContainer(GetParam()))
- , device_(container_->device())
- {
- callback_videoframeresize = &OnFrameSizeChange;
- }
- void SetUp() override
- {
- if (!device_)
- GTEST_SKIP() << "The graphics driver is not installed on the device.";
- texture_.reset(device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
- if (!texture_)
- GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
- ContextDependencies dependencies;
- dependencies.device = device_;
- context = std::make_unique<Context>(dependencies);
- }
- static void OnFrameSizeChange(UnityVideoRenderer* renderer, int width, int height) { }
- };
- TEST_P(ContextTest, InitializeAndFinalizeEncoder)
- {
- const auto source = context->CreateVideoSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateVideoTrack("video", source);
- EXPECT_NE(nullptr, track);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, CreateAndDeleteMediaStream)
- {
- const auto stream = context->CreateMediaStream("test");
- EXPECT_NE(nullptr, stream);
- context->RemoveRefPtr(stream);
- }
- TEST_P(ContextTest, CreateAndDeleteVideoTrack)
- {
- const auto source = context->CreateVideoSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateVideoTrack("video", source);
- EXPECT_NE(nullptr, track);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, CreateAndDeleteAudioTrack)
- {
- const auto source = context->CreateAudioSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateAudioTrack("audio", source);
- EXPECT_NE(nullptr, track);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, AddAndRemoveAudioTrackToMediaStream)
- {
- const auto stream = context->CreateMediaStream("audiostream");
- EXPECT_NE(nullptr, stream);
- const auto source = context->CreateAudioSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateAudioTrack("audio", source);
- EXPECT_NE(nullptr, track);
- const auto audioTrack = reinterpret_cast<webrtc::AudioTrackInterface*>(track);
- EXPECT_NE(nullptr, audioTrack);
- EXPECT_TRUE(stream->AddTrack(audioTrack));
- EXPECT_TRUE(stream->RemoveTrack(audioTrack));
- context->RemoveRefPtr(stream);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, AddAndRemoveVideoTrackToMediaStream)
- {
- const auto stream = context->CreateMediaStream("videostream");
- EXPECT_NE(nullptr, stream);
- const auto source = context->CreateVideoSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateVideoTrack("video", source);
- EXPECT_NE(nullptr, track);
- const auto videoTrack = reinterpret_cast<webrtc::VideoTrackInterface*>(track);
- EXPECT_NE(nullptr, videoTrack);
- EXPECT_TRUE(stream->AddTrack(videoTrack));
- EXPECT_TRUE(stream->RemoveTrack(videoTrack));
- context->RemoveRefPtr(stream);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, CreateAndDeletePeerConnection)
- {
- const webrtc::PeerConnectionInterface::RTCConfiguration config;
- const auto connection = context->CreatePeerConnection(config);
- EXPECT_NE(nullptr, connection);
- context->DeletePeerConnection(connection);
- }
- TEST_P(ContextTest, CreateAndDeleteDataChannel)
- {
- const webrtc::PeerConnectionInterface::RTCConfiguration config;
- const auto connection = context->CreatePeerConnection(config);
- EXPECT_NE(nullptr, connection);
- DataChannelInit init;
- init.protocol = "";
- const auto channel = context->CreateDataChannel(connection, "test", init);
- EXPECT_NE(nullptr, channel);
- context->DeleteDataChannel(channel);
- context->DeletePeerConnection(connection);
- }
- TEST_P(ContextTest, AddTrackAndRemoveTrack)
- {
- const webrtc::PeerConnectionInterface::RTCConfiguration config;
- const auto connection = context->CreatePeerConnection(config);
- EXPECT_NE(nullptr, connection);
- const auto source = static_cast<UnityVideoTrackSource*>(context->CreateVideoSource());
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateVideoTrack("video", source);
- EXPECT_NE(nullptr, track);
- std::vector<std::string> streamIds;
- const auto result = connection->connection->AddTrack(track, streamIds);
- EXPECT_TRUE(result.ok());
- auto frame = CreateTestFrame(device_, texture_.get(), kFormat);
- source->OnFrameCaptured(frame);
- const auto sender = result.value();
- const auto result2 = connection->connection->RemoveTrackNew(sender);
- EXPECT_TRUE(result2.ok());
- context->DeletePeerConnection(connection);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- TEST_P(ContextTest, CreateAndDeleteVideoRenderer)
- {
- const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
- EXPECT_NE(nullptr, renderer);
- context->DeleteVideoRenderer(renderer);
- }
- TEST_P(ContextTest, EqualRendererGetById)
- {
- const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
- EXPECT_NE(nullptr, renderer);
- const auto rendererId = renderer->GetId();
- const auto rendererGetById = context->GetVideoRenderer(rendererId);
- EXPECT_NE(nullptr, rendererGetById);
- context->DeleteVideoRenderer(renderer);
- }
- TEST_P(ContextTest, AddAndRemoveVideoRendererToVideoTrack)
- {
- const auto source = context->CreateVideoSource();
- EXPECT_NE(nullptr, source);
- const auto track = context->CreateVideoTrack("video", source);
- EXPECT_NE(nullptr, track);
- const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
- EXPECT_NE(nullptr, renderer);
- track->AddOrUpdateSink(renderer, rtc::VideoSinkWants());
- track->RemoveSink(renderer);
- context->DeleteVideoRenderer(renderer);
- context->RemoveRefPtr(track);
- context->RemoveRefPtr(source);
- }
- INSTANTIATE_TEST_SUITE_P(GfxDevice, ContextTest, testing::ValuesIn(supportedGfxDevices));
- } // end namespace webrtc
- } // end namespace unity
|