Windows 版 gstreamer 中的死锁

问题描述 投票:0回答:1

我在我的 C# WinForms 应用程序中使用 gstreamer 1.8.1 库,它允许我同时从视频服务器设备观看多个 RTSP 视频流。

我写了一个原生的 c++ dll,它包装了对 gstreamer 的调用。我通过 DllImport 属性从 C# 应用程序使用它。

大多数时候一切都很完美。但有时(我认为当与视频服务器的连接不稳定时)我在我的原生 dll 中的

gst_element_set_state(pipeline, GST_STATE_NULL);
中出现死锁。

所有对 gstreamer API 的调用都来自主 (GUI) 线程。死锁的发生非常罕见——每天只有一次。捕获此错误非常乏味,我不知道如何修复或解决它。

以下是死锁发生时 Visual Studio 调试器的一些屏幕截图:

gstreamer wrapper dll源码非常小:

#pragma comment(lib, "gstreamer-1.0.lib")
#pragma comment(lib, "glib-2.0.lib")
#pragma comment(lib, "gobject-2.0.lib")
#pragma comment(lib, "gstvideo-1.0.lib")

GSTLIB_API void init()
{
    gst_init(NULL, NULL);
}

GSTLIB_API GstElement* create(const char* uri, const void* hwnd)
{
    GstElement* pipeline = gst_element_factory_make("playbin", "play");
    g_object_set(G_OBJECT(pipeline), "uri", uri, NULL);
    gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(pipeline), (guintptr)hwnd);
    return pipeline;
}

GSTLIB_API void play(GstElement* pipeline)
{
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
}

GSTLIB_API void stop(GstElement* pipeline)
{
    gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(pipeline), 0);
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(GST_OBJECT(pipeline));
}

GSTLIB_API bool hasError(GstElement* pipeline)
{
    auto state = GST_STATE(pipeline);
    return state != GST_STATE_PLAYING;
}

原生 dll 的 C# 绑定:

[DllImport("GstLib.dll", EntryPoint = @"?init@@YAXXZ",
CallingConvention = CallingConvention.Cdecl)]
private static extern void init();

[DllImport("GstLib.dll", EntryPoint = @"?create@@YAPAU_GstElement@@PBDPBX@Z",
CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr create([MarshalAs(UnmanagedType.LPStr)] string uri, IntPtr hwnd);

[DllImport("GstLib.dll", EntryPoint = @"?stop@@YAXPAU_GstElement@@@Z",
CallingConvention = CallingConvention.Cdecl)]
private static extern void stop(IntPtr pipeline);

[DllImport("GstLib.dll", EntryPoint = @"?play@@YAXPAU_GstElement@@@Z",
CallingConvention = CallingConvention.Cdecl)]
private static extern void play(IntPtr pipeline);

[DllImport("GstLib.dll", EntryPoint = @"?hasError@@YA_NPAU_GstElement@@@Z",
CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U1)]
private static extern bool hasError(IntPtr pipeline);

有人知道为什么会发生死锁吗?

c# c++ debugging gstreamer deadlock
1个回答
0
投票

这可能只是我的纯粹猜测:

如果您在流线程中设置管道的状态,您实际上必须调用 gst_element_call_async。 GStreamer 文档指出,在流线程中调用 gst_element_set_state 可能会导致死锁:

gst_element_call_async

从另一个线程调用 func 并将 user_data 传递给它。这用于必须从流线程执行状态更改的情况,直接通过 gst_element_set_state 或间接地,例如通过 SEEK 事件。

直接从流线程调用这些函数在许多情况下会导致死锁,因为它们可能涉及等待流线程从这个流线程关闭。

MT 安全。

© www.soinside.com 2019 - 2024. All rights reserved.