c – g_main_loop_run阻止Qthread,不允许停止视频

前端之家收集整理的这篇文章主要介绍了c – g_main_loop_run阻止Qthread,不允许停止视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我为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,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,SLOT(deleteLater() ) );
        connect( threadStreaming,SIGNAL( finished() ),SLOT(deleteLater() ) );

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

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

解决方法

没有必要依靠aGMainLoop.在没有g_main_loop_run()的情况下,管道应该运行得很好.

您需要注意的一件事是,您的主要Qt应用程序循环将不得不轮询流水线的总线消息,或者使用gst_bus_set_sync_handler为总线在消息到达时设置回调函数.对于后来你必须意识到,这个函数然后从管道的线程调用,而不是应用程序的线程.这里发出的信号应该很好.

如果你想去线程,你必须在运行GMainLoop的应用程序中手动创建一个线程.也可能 – 上面看起来像对我更简单和更干净的方式.

猜你在找的C&C++相关文章