Skip to content

Commit 8f6a732

Browse files
philncalvaris
authored andcommitted
[GStreamer] Misc leak fixes
https://bugs.webkit.org/show_bug.cgi?id=266188 Reviewed by Xabier Rodriguez-Calvar. The GStreamer device-related objects were leaked, along with a couple GstStream objects in the player. There are more issues, specially in the MSE code, but that's for another time... We now explicitely clean-up the capture device managers and the registry scanner before de-initializing GStreamer. The FastMalloc allocator is flagged as leaked as well, as it is static and I didn't find how to properly clean it up. * Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp: (WebCore::deinitializeGStreamer): * Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h: * Source/WebCore/platform/graphics/gstreamer/GStreamerRegistryScanner.cpp: (WebCore::teardownGStreamerRegistryScanner): (WebCore::GStreamerRegistryScanner::ElementFactories::hasElementForCaps const): (WebCore::GStreamerRegistryScanner::teardown): * Source/WebCore/platform/graphics/gstreamer/GStreamerRegistryScanner.h: * Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp: (gst_allocator_fast_malloc_init): * Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp: (WebCore::MediaPlayerPrivateGStreamer::handleMessage): * Source/WebCore/platform/mediastream/gstreamer/GStreamerCaptureDeviceManager.cpp: (WebCore::teardownGStreamerCaptureDeviceManagers): (WebCore::GStreamerCaptureDeviceManager::~GStreamerCaptureDeviceManager): (WebCore::GStreamerCaptureDeviceManager::teardown): (WebCore::GStreamerCaptureDeviceManager::captureDevices): (WebCore::GStreamerCaptureDeviceManager::refreshCaptureDevices): * Source/WebCore/platform/mediastream/gstreamer/GStreamerCaptureDeviceManager.h: * Source/WebKit/WebProcess/gtk/WebProcessMainGtk.cpp: * Source/WebKit/WebProcess/wpe/WebProcessMainWPE.cpp: Canonical link: https://commits.webkit.org/271861@main
1 parent d0b6c67 commit 8f6a732

6 files changed

Lines changed: 55 additions & 10 deletions

File tree

Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ static void gst_allocator_fast_malloc_init(GstAllocatorFastMalloc* allocator)
148148
{
149149
auto* baseAllocator = GST_ALLOCATOR_CAST(allocator);
150150

151+
GST_OBJECT_FLAG_SET(allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
152+
151153
baseAllocator->mem_type = "FastMalloc";
152154
baseAllocator->mem_map = reinterpret_cast<GstMemoryMapFunction>(gstAllocatorFastMallocMemMap);
153155
baseAllocator->mem_unmap = reinterpret_cast<GstMemoryUnmapFunction>(gstAllocatorFastMallocMemUnmap);

Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,8 +2084,8 @@ void MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
20842084
GST_DEBUG_OBJECT(m_pipeline.get(), "Received STREAMS_SELECTED message selecting the following streams:");
20852085
unsigned numStreams = gst_message_streams_selected_get_size(message);
20862086
for (unsigned i = 0; i < numStreams; i++) {
2087-
GstStream* stream = gst_message_streams_selected_get_stream(message, i);
2088-
GST_DEBUG_OBJECT(pipeline(), "#%u %s %s", i, gst_stream_type_get_name(gst_stream_get_stream_type(stream)), gst_stream_get_stream_id(stream));
2087+
auto stream = adoptGRef(gst_message_streams_selected_get_stream(message, i));
2088+
GST_DEBUG_OBJECT(pipeline(), "#%u %s %s", i, gst_stream_type_get_name(gst_stream_get_stream_type(stream.get())), gst_stream_get_stream_id(stream.get()));
20892089
}
20902090
#endif
20912091
GST_DEBUG_OBJECT(m_pipeline.get(), "Setting m_waitingForStreamsSelectedEvent to false.");

Source/WebCore/platform/mediastream/gstreamer/GStreamerCaptureDeviceManager.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,39 @@ GStreamerVideoCaptureDeviceManager& GStreamerVideoCaptureDeviceManager::singleto
6363
return manager;
6464
}
6565

66+
void teardownGStreamerCaptureDeviceManagers()
67+
{
68+
auto& audioManager = GStreamerAudioCaptureDeviceManager::singleton();
69+
audioManager.teardown();
70+
71+
auto& videoManager = GStreamerVideoCaptureDeviceManager::singleton();
72+
videoManager.teardown();
73+
}
74+
6675
GStreamerCaptureDeviceManager::~GStreamerCaptureDeviceManager()
6776
{
68-
if (m_deviceMonitor)
69-
gst_device_monitor_stop(m_deviceMonitor.get());
77+
teardown();
78+
}
79+
80+
void GStreamerCaptureDeviceManager::teardown()
81+
{
82+
GST_DEBUG_OBJECT(m_deviceMonitor.get(), "Tearing down");
83+
m_isTearingDown = true;
84+
stopMonitor();
85+
RealtimeMediaSourceCenter::singleton().removeDevicesChangedObserver(*this);
86+
m_devices.clear();
87+
m_gstreamerDevices.clear();
88+
}
89+
90+
void GStreamerCaptureDeviceManager::stopMonitor()
91+
{
92+
if (!m_deviceMonitor)
93+
return;
94+
95+
auto bus = adoptGRef(gst_device_monitor_get_bus(m_deviceMonitor.get()));
96+
gst_bus_remove_watch(bus.get());
97+
gst_device_monitor_stop(m_deviceMonitor.get());
98+
m_deviceMonitor.clear();
7099
}
71100

72101
std::optional<GStreamerCaptureDevice> GStreamerCaptureDeviceManager::gstreamerDeviceWithUID(const String& deviceID)
@@ -87,7 +116,7 @@ const Vector<CaptureDevice>& GStreamerCaptureDeviceManager::captureDevices()
87116
std::call_once(onceFlag, [] {
88117
GST_DEBUG_CATEGORY_INIT(webkitGStreamerCaptureDeviceManagerDebugCategory, "webkitcapturedevicemanager", 0, "WebKit Capture Device Manager");
89118
});
90-
if (m_devices.isEmpty())
119+
if (m_devices.isEmpty() && !m_isTearingDown)
91120
refreshCaptureDevices();
92121

93122
return m_devices;
@@ -129,7 +158,12 @@ void GStreamerCaptureDeviceManager::addDevice(GRefPtr<GstDevice>&& device)
129158

130159
void GStreamerCaptureDeviceManager::refreshCaptureDevices()
131160
{
161+
GST_DEBUG_OBJECT(m_deviceMonitor.get(), "Refreshing capture devices");
132162
m_devices.clear();
163+
m_gstreamerDevices.clear();
164+
if (m_isTearingDown)
165+
return;
166+
133167
if (!m_deviceMonitor) {
134168
m_deviceMonitor = adoptGRef(gst_device_monitor_new());
135169

Source/WebCore/platform/mediastream/gstreamer/GStreamerCaptureDeviceManager.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,34 @@
2929
#include "GStreamerVideoCapturer.h"
3030
#include "RealtimeMediaSourceFactory.h"
3131

32+
#include <wtf/Noncopyable.h>
33+
3234
namespace WebCore {
3335

3436
using NodeAndFD = GStreamerVideoCapturer::NodeAndFD;
3537

36-
class GStreamerCaptureDeviceManager : public CaptureDeviceManager {
38+
void teardownGStreamerCaptureDeviceManagers();
39+
40+
class GStreamerCaptureDeviceManager : public CaptureDeviceManager
41+
WTF_MAKE_NONCOPYABLE(GStreamerCaptureDeviceManager)
42+
{
3743
public:
3844
~GStreamerCaptureDeviceManager();
3945
std::optional<GStreamerCaptureDevice> gstreamerDeviceWithUID(const String&);
4046

4147
const Vector<CaptureDevice>& captureDevices() final;
4248
virtual CaptureDevice::DeviceType deviceType() = 0;
4349

50+
void teardown();
51+
4452
private:
4553
void addDevice(GRefPtr<GstDevice>&&);
4654
void refreshCaptureDevices();
4755

4856
GRefPtr<GstDeviceMonitor> m_deviceMonitor;
4957
Vector<GStreamerCaptureDevice> m_gstreamerDevices;
5058
Vector<CaptureDevice> m_devices;
59+
bool m_isTearingDown { false };
5160
};
5261

5362
class GStreamerAudioCaptureDeviceManager final : public GStreamerCaptureDeviceManager {

Source/WebKit/WebProcess/gtk/WebProcessMainGtk.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif
3838

3939
#if USE(GSTREAMER)
40-
#include <gst/gst.h>
40+
#include <WebCore/GStreamerCommon.h>
4141
#endif
4242

4343
#if USE(GCRYPT)
@@ -75,7 +75,7 @@ class WebProcessMainGtk final: public AuxiliaryProcessMainBase<WebProcess> {
7575
void platformFinalize() override
7676
{
7777
#if USE(GSTREAMER)
78-
gst_deinit();
78+
deinitializeGStreamer();
7979
#endif
8080
}
8181
};

Source/WebKit/WebProcess/wpe/WebProcessMainWPE.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#endif
3737

3838
#if USE(GSTREAMER)
39-
#include <gst/gst.h>
39+
#include <WebCore/GStreamerCommon.h>
4040
#endif
4141

4242
namespace WebKit {
@@ -65,7 +65,7 @@ class WebProcessMainWPE final : public AuxiliaryProcessMainBase<WebProcess> {
6565
void platformFinalize() override
6666
{
6767
#if USE(GSTREAMER)
68-
gst_deinit();
68+
deinitializeGStreamer();
6969
#endif
7070
}
7171
};

0 commit comments

Comments
 (0)