- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我为 gstreamer 创建了一个单独的类来流式传输视频。
此类通过使用 moveToThread() 在单独的线程上运行。
我正在使用 Qt5.5 进行开发。
当我在主线程上发出 startcommand 时,Qthread 启动并且 gstreamer 使用 g_main_loop_run
流式传输视频。这绝对没问题。但不知何故 g_main_loop_run
阻塞了线程,当我发出信号以停止来自主线程的视频时,它不会执行 gstreamer 类中的插槽。
有人可以告诉我如何解决这个问题吗?我可以用其他命令替换 g_main_loop_r
un 或者可以使用 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/
我想写一些必须在自己的线程中工作的类。我读过这篇文章:http://wiki.qt.io/Threads_Events_QObjects。它建议移动必须在自己的线程中工作的对象,例如: TestCla
在调用所有与 QThread::finished() 信号关联的槽后,QThread::wait() 是否返回(即解除阻塞执行)? 提前致谢。 最佳答案 不,它可能会在与信号 finished() 关
我不是在打电话 exec()在我的代码中,但 timer和 QUdpSocket工作正常。是 exec()用于等待 event接着说? 更新:timer正在工作,因为我没有调用 moveToThrea
如果我创建 QThread 作为局部变量,我发现了一个奇怪的行为。 例如,下面的代码将作为单线程工作,这意味着我需要等待 10 秒,结果才会出来。 但是如果我将线程从局部变量更改为成员变量,它将作为多
I solved my issue by moving the mySubQThread run() into the myQThread run() That said, I still would
一段时间以来,我一直在使用 Qt 开发一个应用程序,在该应用程序中我必须从相机中抓取帧。相机将在与应用程序其余部分不同的线程中运行。我遵循了以下建议: http://mayaposch.wordpre
考虑以下代码片段: class ThreadA::QThread { public: ThreadA() { } void run() { myVariable = n
所以我在构造函数中有以下代码。 m_someObject = new SomeObject(); m_someObject->moveToThread(&m_thread); m_thread.sta
我有 3 个 QThreads 相互调用(全部继承自 QThread。我知道有些人可能建议使用 moveToThread,但暂时忽略这个事实)。简化的代码如下所示: Thread1 类: void T
我想知道 new QThread(this) 和 new QThread() 之间有什么区别,以及这将如何影响我的代码在使用 QThread 时的行为. 最佳答案 QThread 的父级谁执行什么没有
我是 PySide2 的新手。我只是想启动一个示例应用程序并在应用程序启动时启动一个线程,并希望在应用程序关闭时停止该线程。当我关闭应用程序时,我收到以下错误:QThread:在线程仍在运行时销毁。s
关于如何实例化和使用 QThread 的官方文档可以在这里找到: http://doc.qt.io/qt-5/qthread.html 该文档描述了两种基本方法:(1) 工作对象方法和 (2) QTh
我有以下设置: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Create the DBM
这是 QTread 的子对象...并在主线程中将其具体化.... 运行时错误如下: ASSERT failure in QCoreApplication::sendEvent: "Cannot sen
在过去的几天里,我一直在尝试使用 QThreads 而不对 QThread 进行子类化的新的首选方法。我遇到的麻烦是当我试图关闭我创建的一组线程时。我经常收到“线程仍在运行时已被销毁”消息(如果我在
我已经创建了这个继承自QThread的类,用于向数据库服务器发送数据,你觉得怎么样?可以改进吗? 谢谢 #ifndef QUERYTHREAD_H#define QUERYTHREAD_H#inclu
我需要在一个线程中进行无限循环。 在 this article作者写道 >you should never ever block the event loop 因为它会阻塞信号槽概念。如何在 QTh
我试图在一个单独的线程中执行一些工作,并在工作完成后停止该线程。 我已经建立了这样的连接 thread.connect( workerClass, SIGNAL( finished() ), SLOT
我有一个快速的问题。我应该创建一个小的多线程程序来从多个传感器获取数据,并且我了解 pthreads 和 qthreads。我可以访问两个图书馆。我个人偏向于使用 Qt,因为它的设计和各种功能。但是,
我需要在主线程上运行 QThread::usleep() (出于各种原因)。 但是,usleep 是受静电保护的。 我想在没有包装器的情况下使用 QThread 的 usleep 函数(我目前正在使用
我是一名优秀的程序员,十分优秀!