ContextTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "pch.h"
  2. #include <rtc_base/ref_counted_object.h>
  3. #include "Context.h"
  4. #include "GraphicsDevice/IGraphicsDevice.h"
  5. #include "GraphicsDevice/ITexture2D.h"
  6. #include "GraphicsDeviceContainer.h"
  7. #include "GraphicsDeviceTestBase.h"
  8. #include "VideoFrameUtil.h"
  9. namespace unity
  10. {
  11. namespace webrtc
  12. {
  13. using namespace ::webrtc;
  14. class ContextTest : public testing::TestWithParam<UnityGfxRenderer>
  15. {
  16. protected:
  17. const uint32_t kWidth = 256;
  18. const uint32_t kHeight = 256;
  19. const UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
  20. std::unique_ptr<GraphicsDeviceContainer> container_;
  21. std::unique_ptr<Context> context;
  22. IGraphicsDevice* device_;
  23. std::unique_ptr<ITexture2D> texture_;
  24. DelegateVideoFrameResize callback_videoframeresize;
  25. explicit ContextTest()
  26. : container_(CreateGraphicsDeviceContainer(GetParam()))
  27. , device_(container_->device())
  28. {
  29. callback_videoframeresize = &OnFrameSizeChange;
  30. }
  31. void SetUp() override
  32. {
  33. if (!device_)
  34. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  35. texture_.reset(device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  36. if (!texture_)
  37. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  38. ContextDependencies dependencies;
  39. dependencies.device = device_;
  40. context = std::make_unique<Context>(dependencies);
  41. }
  42. static void OnFrameSizeChange(UnityVideoRenderer* renderer, int width, int height) { }
  43. };
  44. TEST_P(ContextTest, InitializeAndFinalizeEncoder)
  45. {
  46. const auto source = context->CreateVideoSource();
  47. EXPECT_NE(nullptr, source);
  48. const auto track = context->CreateVideoTrack("video", source);
  49. EXPECT_NE(nullptr, track);
  50. context->RemoveRefPtr(track);
  51. context->RemoveRefPtr(source);
  52. }
  53. TEST_P(ContextTest, CreateAndDeleteMediaStream)
  54. {
  55. const auto stream = context->CreateMediaStream("test");
  56. EXPECT_NE(nullptr, stream);
  57. context->RemoveRefPtr(stream);
  58. }
  59. TEST_P(ContextTest, CreateAndDeleteVideoTrack)
  60. {
  61. const auto source = context->CreateVideoSource();
  62. EXPECT_NE(nullptr, source);
  63. const auto track = context->CreateVideoTrack("video", source);
  64. EXPECT_NE(nullptr, track);
  65. context->RemoveRefPtr(track);
  66. context->RemoveRefPtr(source);
  67. }
  68. TEST_P(ContextTest, CreateAndDeleteAudioTrack)
  69. {
  70. const auto source = context->CreateAudioSource();
  71. EXPECT_NE(nullptr, source);
  72. const auto track = context->CreateAudioTrack("audio", source);
  73. EXPECT_NE(nullptr, track);
  74. context->RemoveRefPtr(track);
  75. context->RemoveRefPtr(source);
  76. }
  77. TEST_P(ContextTest, AddAndRemoveAudioTrackToMediaStream)
  78. {
  79. const auto stream = context->CreateMediaStream("audiostream");
  80. EXPECT_NE(nullptr, stream);
  81. const auto source = context->CreateAudioSource();
  82. EXPECT_NE(nullptr, source);
  83. const auto track = context->CreateAudioTrack("audio", source);
  84. EXPECT_NE(nullptr, track);
  85. const auto audioTrack = reinterpret_cast<webrtc::AudioTrackInterface*>(track);
  86. EXPECT_NE(nullptr, audioTrack);
  87. EXPECT_TRUE(stream->AddTrack(audioTrack));
  88. EXPECT_TRUE(stream->RemoveTrack(audioTrack));
  89. context->RemoveRefPtr(stream);
  90. context->RemoveRefPtr(track);
  91. context->RemoveRefPtr(source);
  92. }
  93. TEST_P(ContextTest, AddAndRemoveVideoTrackToMediaStream)
  94. {
  95. const auto stream = context->CreateMediaStream("videostream");
  96. EXPECT_NE(nullptr, stream);
  97. const auto source = context->CreateVideoSource();
  98. EXPECT_NE(nullptr, source);
  99. const auto track = context->CreateVideoTrack("video", source);
  100. EXPECT_NE(nullptr, track);
  101. const auto videoTrack = reinterpret_cast<webrtc::VideoTrackInterface*>(track);
  102. EXPECT_NE(nullptr, videoTrack);
  103. EXPECT_TRUE(stream->AddTrack(videoTrack));
  104. EXPECT_TRUE(stream->RemoveTrack(videoTrack));
  105. context->RemoveRefPtr(stream);
  106. context->RemoveRefPtr(track);
  107. context->RemoveRefPtr(source);
  108. }
  109. TEST_P(ContextTest, CreateAndDeletePeerConnection)
  110. {
  111. const webrtc::PeerConnectionInterface::RTCConfiguration config;
  112. const auto connection = context->CreatePeerConnection(config);
  113. EXPECT_NE(nullptr, connection);
  114. context->DeletePeerConnection(connection);
  115. }
  116. TEST_P(ContextTest, CreateAndDeleteDataChannel)
  117. {
  118. const webrtc::PeerConnectionInterface::RTCConfiguration config;
  119. const auto connection = context->CreatePeerConnection(config);
  120. EXPECT_NE(nullptr, connection);
  121. DataChannelInit init;
  122. init.protocol = "";
  123. const auto channel = context->CreateDataChannel(connection, "test", init);
  124. EXPECT_NE(nullptr, channel);
  125. context->DeleteDataChannel(channel);
  126. context->DeletePeerConnection(connection);
  127. }
  128. TEST_P(ContextTest, AddTrackAndRemoveTrack)
  129. {
  130. const webrtc::PeerConnectionInterface::RTCConfiguration config;
  131. const auto connection = context->CreatePeerConnection(config);
  132. EXPECT_NE(nullptr, connection);
  133. const auto source = static_cast<UnityVideoTrackSource*>(context->CreateVideoSource());
  134. EXPECT_NE(nullptr, source);
  135. const auto track = context->CreateVideoTrack("video", source);
  136. EXPECT_NE(nullptr, track);
  137. std::vector<std::string> streamIds;
  138. const auto result = connection->connection->AddTrack(track, streamIds);
  139. EXPECT_TRUE(result.ok());
  140. auto frame = CreateTestFrame(device_, texture_.get(), kFormat);
  141. source->OnFrameCaptured(frame);
  142. const auto sender = result.value();
  143. const auto result2 = connection->connection->RemoveTrackNew(sender);
  144. EXPECT_TRUE(result2.ok());
  145. context->DeletePeerConnection(connection);
  146. context->RemoveRefPtr(track);
  147. context->RemoveRefPtr(source);
  148. }
  149. TEST_P(ContextTest, CreateAndDeleteVideoRenderer)
  150. {
  151. const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
  152. EXPECT_NE(nullptr, renderer);
  153. context->DeleteVideoRenderer(renderer);
  154. }
  155. TEST_P(ContextTest, EqualRendererGetById)
  156. {
  157. const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
  158. EXPECT_NE(nullptr, renderer);
  159. const auto rendererId = renderer->GetId();
  160. const auto rendererGetById = context->GetVideoRenderer(rendererId);
  161. EXPECT_NE(nullptr, rendererGetById);
  162. context->DeleteVideoRenderer(renderer);
  163. }
  164. TEST_P(ContextTest, AddAndRemoveVideoRendererToVideoTrack)
  165. {
  166. const auto source = context->CreateVideoSource();
  167. EXPECT_NE(nullptr, source);
  168. const auto track = context->CreateVideoTrack("video", source);
  169. EXPECT_NE(nullptr, track);
  170. const auto renderer = context->CreateVideoRenderer(callback_videoframeresize, true);
  171. EXPECT_NE(nullptr, renderer);
  172. track->AddOrUpdateSink(renderer, rtc::VideoSinkWants());
  173. track->RemoveSink(renderer);
  174. context->DeleteVideoRenderer(renderer);
  175. context->RemoveRefPtr(track);
  176. context->RemoveRefPtr(source);
  177. }
  178. INSTANTIATE_TEST_SUITE_P(GfxDevice, ContextTest, testing::ValuesIn(supportedGfxDevices));
  179. } // end namespace webrtc
  180. } // end namespace unity