gpt4 book ai didi

c++ - g_main_loop_run 阻塞 Qthread 并且不允许停止视频

转载 作者:IT老高 更新时间:2023-10-28 23:21:40 26 4
gpt4 key购买 nike

我为 gstreamer 创建了一个单独的类来流式传输视频。
此类通过使用 moveToThread() 在单独的线程上运行。
我正在使用 Qt5.5 进行开发。
当我在主线程上发出 startcommand 时,Qthread 启动并且 gstreamer 使用 g_main_loop_run 流式传输视频。这绝对没问题。但不知何故 g_main_loop_run 阻塞了线程,当我发出信号以停止来自主线程的视频时,它不会执行 gstreamer 类中的插槽。

有人可以告诉我如何解决这个问题吗?我可以用其他命令替换 g_main_loop_run 或者可以使用 g_main_loop_quit( gloop );以另一种方式。

void StreamingVideo::slotStartStream() // this slot called on start of thread from main thread
{

if( !isElementsLinked() )
{
qDebug() << " we are emitting in dummy server ";
//emit sigFailed( "elementsFailed" ); // WILL CONNECT IT WITH MAIN GUI ONXCE CODE IS FINISHED
return;
}

gst_bus_add_watch( bus, busCall, gloop );
gst_object_unref( bus );

//proper adding to pipe
gst_bin_add_many( GST_BIN( pipeline ), source, capsFilter, conv, videoRate, capsFilterRate,
clockDisplay, videoEnc, udpSink, NULL
);

//proper linking:
gst_element_link_many( source, capsFilter, conv, videoRate, capsFilterRate, clockDisplay, videoEnc, udpSink, NULL );

g_print("Linked all the Elements together\n");
gst_element_set_state( pipeline, GST_STATE_PLAYING );
// Iterate
g_print ("Running...\n");
emit sigStartStream(); // signal to main thread to issue success command . works fine
g_main_loop_run( gloop );
g_print ("Returned, stopping playback\n");
//gst_element_set_state (pipeline, GST_STATE_NULL);
if( g_main_loop_is_running( gloop ) )
{
qDebug() << " in g_main_loop_is_runnung emiting signal ";
emit sigStartStream();
}
if( !g_main_loop_is_running( gloop) )
{
qDebug() << "in not gmain running thread id";
qDebug() << QThread::currentThreadId();
}

}



void StreamingVideo::slotStopStream() // THIS SLOT IS NOT CALLED WHEN VIDEO RUNNING
{
qDebug() << " we are planning to stop streaming stramingVideo::slotStopStream ";
g_print ("Returned, stopping playback\n");
g_main_loop_quit( gloop );
gst_element_set_state (pipeline, GST_STATE_NULL);
// g_main_loop_quit( gloop );
releaseMemory();
emit sigStopStream(); // signal to main thread to issue message saying video has stopped.
}

//主线程某处

 threadStreaming = new QThread();
streamVideo = new StreamingVideo( "127.0.0.1"); // we will automate this ip address later on

streamVideo->moveToThread( threadStreaming );

connect( threadStreaming, SIGNAL( started() ), streamVideo, SLOT( slotStartStream() ) );
connect( streamVideo, SIGNAL( sigStopStream() ), threadStreaming, SLOT( quit() ) );
connect( streamVideo, SIGNAL( sigStopStream() ), streamVideo, SLOT(deleteLater() ) );
connect( threadStreaming, SIGNAL( finished() ), threadStreaming, SLOT(deleteLater() ) );

connect( streamVideo, SIGNAL( sigStartStream() ), this, SLOT( slotTrueStreamRun() ) );
connect( streamVideo, SIGNAL( sigStopStream() ), this, SLOT( slotFalseStreamRun() ) );

connect( this, SIGNAL( sigMopsCamStopCmd() ), streamVideo, SLOT(slotStopStream() ) );
threadStreaming->start();

最佳答案

无需依赖GMainLoop。如果没有 g_main_loop_run(),管道应该可以正常运行。

您需要注意的一件事是,您的主 Qt 应用程序循环要么必须轮询管道的总线以获取消息,要么使用 gst_bus_set_sync_handler 为要调用的总线设置回调函数消息到达。对于后者,您必须注意,然后从管道的线程而不是应用程序的线程调用此函数。不过在这里发出信号应该没问题。

如果您想采用线程方式,则必须在您的应用程序中手动创建一个运行 GMainLoop 的线程。也有可能——不过,上述方法对我来说似乎更简单、更干净。

关于c++ - g_main_loop_run 阻塞 Qthread 并且不允许停止视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com