|
1 | 1 | #include "Device.hpp" |
2 | 2 | #include "FakeTransportListener.hpp" |
3 | 3 | #include "MediaSoupClientErrors.hpp" |
| 4 | +#include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 5 | +#include "api/audio_codecs/builtin_audio_encoder_factory.h" |
| 6 | +#include "api/create_peerconnection_factory.h" |
| 7 | +#include "api/video_codecs/video_decoder_factory_template.h" |
| 8 | +#include "api/video_codecs/video_decoder_factory_template_dav1d_adapter.h" |
| 9 | +#include "api/video_codecs/video_decoder_factory_template_libvpx_vp8_adapter.h" |
| 10 | +#include "api/video_codecs/video_decoder_factory_template_libvpx_vp9_adapter.h" |
| 11 | +#include "api/video_codecs/video_decoder_factory_template_open_h264_adapter.h" |
| 12 | +#include "api/video_codecs/video_encoder_factory_template.h" |
| 13 | +#include "api/video_codecs/video_encoder_factory_template_libaom_av1_adapter.h" |
| 14 | +#include "api/video_codecs/video_encoder_factory_template_libvpx_vp8_adapter.h" |
| 15 | +#include "api/video_codecs/video_encoder_factory_template_libvpx_vp9_adapter.h" |
| 16 | +#include "api/video_codecs/video_encoder_factory_template_open_h264_adapter.h" |
4 | 17 | #include "fakeParameters.hpp" |
5 | 18 | #include "ortc.hpp" |
| 19 | +#include "rtc_base/thread.h" |
6 | 20 | #include <catch.hpp> |
7 | 21 |
|
8 | 22 | TEST_CASE("Device", "[Device]") |
@@ -105,3 +119,69 @@ TEST_CASE("Device", "[Device]") |
105 | 119 | TransportRemoteParameters["dtlsParameters"])); |
106 | 120 | } |
107 | 121 | } |
| 122 | + |
| 123 | +/** |
| 124 | + * Regression test: device->Load() must not deadlock when called from the |
| 125 | + * signaling thread of a custom PeerConnectionFactory. |
| 126 | + * |
| 127 | + * Root cause: PeerConnection methods used future.get() which blocked the |
| 128 | + * calling thread. WebRTC observer callbacks are delivered on the signaling |
| 129 | + * thread, so calling from that thread deadlocked. |
| 130 | + * |
| 131 | + * Fix: waitForSignalingCallback() pumps the current WebRTC thread's message |
| 132 | + * queue while waiting, allowing observer callbacks to fire. |
| 133 | + * |
| 134 | + * https://github.com/versatica/libmediasoupclient/issues/187 |
| 135 | + */ |
| 136 | +TEST_CASE("Device::Load from signaling thread does not deadlock", "[Device]") |
| 137 | +{ |
| 138 | + auto networkThread = webrtc::Thread::CreateWithSocketServer(); |
| 139 | + auto workerThread = webrtc::Thread::Create(); |
| 140 | + auto signalingThread = webrtc::Thread::Create(); |
| 141 | + networkThread->SetName("deadlock_test_network", nullptr); |
| 142 | + workerThread->SetName("deadlock_test_worker", nullptr); |
| 143 | + signalingThread->SetName("deadlock_test_signaling", nullptr); |
| 144 | + networkThread->Start(); |
| 145 | + workerThread->Start(); |
| 146 | + signalingThread->Start(); |
| 147 | + |
| 148 | + auto factory = webrtc::CreatePeerConnectionFactory( |
| 149 | + networkThread.get(), |
| 150 | + workerThread.get(), |
| 151 | + signalingThread.get(), |
| 152 | + nullptr, |
| 153 | + webrtc::CreateBuiltinAudioEncoderFactory(), |
| 154 | + webrtc::CreateBuiltinAudioDecoderFactory(), |
| 155 | + std::make_unique<webrtc::VideoEncoderFactoryTemplate< |
| 156 | + webrtc::LibvpxVp8EncoderTemplateAdapter, |
| 157 | + webrtc::LibvpxVp9EncoderTemplateAdapter, |
| 158 | + webrtc::OpenH264EncoderTemplateAdapter, |
| 159 | + webrtc::LibaomAv1EncoderTemplateAdapter>>(), |
| 160 | + std::make_unique<webrtc::VideoDecoderFactoryTemplate< |
| 161 | + webrtc::LibvpxVp8DecoderTemplateAdapter, |
| 162 | + webrtc::LibvpxVp9DecoderTemplateAdapter, |
| 163 | + webrtc::OpenH264DecoderTemplateAdapter, |
| 164 | + webrtc::Dav1dDecoderTemplateAdapter>>(), |
| 165 | + nullptr, |
| 166 | + nullptr, |
| 167 | + nullptr, |
| 168 | + nullptr); |
| 169 | + |
| 170 | + REQUIRE(factory != nullptr); |
| 171 | + |
| 172 | + mediasoupclient::PeerConnection::Options pcOptions; |
| 173 | + pcOptions.factory = factory.get(); |
| 174 | + |
| 175 | + // Call device->Load() FROM the signaling thread - the exact scenario |
| 176 | + // from the bug report. |
| 177 | + bool loaded{ false }; |
| 178 | + signalingThread->BlockingCall( |
| 179 | + [&]() |
| 180 | + { |
| 181 | + mediasoupclient::Device device; |
| 182 | + REQUIRE_NOTHROW(device.Load(generateRouterRtpCapabilities(), &pcOptions)); |
| 183 | + loaded = device.IsLoaded(); |
| 184 | + }); |
| 185 | + |
| 186 | + REQUIRE(loaded); |
| 187 | +} |
0 commit comments