C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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( !iSELER_83_11845@entsLinked() )
    {
       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 – g_main_loop_run阻止Qthread,不允许停止视频全部内容,希望文章能够帮你解决c – g_main_loop_run阻止Qthread,不允许停止视频所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。