WebRTC 使用 Qt 播放网络摄像头

发布于 2025-01-07 13:18:18 字数 3226 浏览 0 评论 0原文

我使用 WebRTC 进行网络摄像头播放(用于在视频通话之前在我的应用程序中测试网络摄像头)。我接下来执行一些 WebRTC 功能:

////////////////////////

//initialization
{
webrtc::VoiceEngine* voe = VoiceEngine::Create();
webrtc::VoEBase* voeBase = VoEBase::GetInterface( voe );
voeBase->Init();
webrtc::VideoEngine* vie = VideoEngine::Create();
webrtc::ViEBase* vieBase = ViEBase::GetInterface( vie );
vieBase->Init();
webrtc::ViECapture* vieCapture = ViECapture::GetInterface( vie );
}

////////////////////////

// start webcam playing
void MainWindow::StartWebcamTest()
{
         // creating window for playing webcam video
    videoWidget = new Phonon::VideoWidget();
    videoWidget->setFixedSize( 640, 480 );
    videoWidget->setWindowTitle( "Test Webcam" );

    char captDevName[1024], captGuidName[1024];
    VoEEngineSingleton::Instance()->GetViECapture()->GetCaptureDevice( 0, captDevName, 1024, captGuidName, 1024 );
    VoEEngineSingleton::Instance()->GetViECapture()->AllocateCaptureDevice( captGuidName, 1024, captureID );

    testVideoChannel = VoEEngineSingleton::Instance()->GetViEBase()->CreateChannel( testVideoChannel );
    if( testVideoChannel == -1 )
        LOGERR << "Creating video channel error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl; 
    else LOGDEBUG << "Video channel created, ID = " << testVideoChannel << endl;

    testAudioChannel = VoEEngineSingleton::Instance()->GetVoEBase()->CreateChannel();
    if( testAudioChannel == -1 )
        LOGERR << "Creating audio channel error: " << VoEEngineSingleton::Instance()->GetVoEBase()->LastError() << endl;
    else LOGDEBUG << "Audio channel created, ID = " << testAudioChannel << endl;

    if( VoEEngineSingleton::Instance()->GetViEBase()->ConnectAudioChannel( testVideoChannel, testAudioChannel ) != 0 )
        LOGERR << "ConnectAudioChannel() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "Audio channel connected successful " << endl;

    if( VoEEngineSingleton::Instance()->GetViECapture()->ConnectCaptureDevice( captureID, testVideoChannel ) != 0 )
        LOGERR << "ConnectCaptureDevice() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "Capture device connected successful " << endl;

    if( VoEEngineSingleton::Instance()->GetViERender()->StartRender( testVideoChannel ) != 0 )
        LOGERR << "StartRender() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO "StartRender() successed " << endl;

    if( VoEEngineSingleton::Instance()->GetViECapture()->StartCapture( captureID ) != 0 )
        LOGERR << "StartCapture() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "StartCapture() successed " << endl;

    videoWidget->show();
}

当我单击播放视频按钮时 - 网络摄像头开始播放,但 videoWidget 窗口是黑色的空的。

我做什么不真实?也许我必须使用其他 Qt 小部件?

I use WebRTC for webcam playing (it's for testing webcam at my application before video calling). I do these next some WebRTC function:

////////////////////////

//initialization
{
webrtc::VoiceEngine* voe = VoiceEngine::Create();
webrtc::VoEBase* voeBase = VoEBase::GetInterface( voe );
voeBase->Init();
webrtc::VideoEngine* vie = VideoEngine::Create();
webrtc::ViEBase* vieBase = ViEBase::GetInterface( vie );
vieBase->Init();
webrtc::ViECapture* vieCapture = ViECapture::GetInterface( vie );
}

////////////////////////

// start webcam playing
void MainWindow::StartWebcamTest()
{
         // creating window for playing webcam video
    videoWidget = new Phonon::VideoWidget();
    videoWidget->setFixedSize( 640, 480 );
    videoWidget->setWindowTitle( "Test Webcam" );

    char captDevName[1024], captGuidName[1024];
    VoEEngineSingleton::Instance()->GetViECapture()->GetCaptureDevice( 0, captDevName, 1024, captGuidName, 1024 );
    VoEEngineSingleton::Instance()->GetViECapture()->AllocateCaptureDevice( captGuidName, 1024, captureID );

    testVideoChannel = VoEEngineSingleton::Instance()->GetViEBase()->CreateChannel( testVideoChannel );
    if( testVideoChannel == -1 )
        LOGERR << "Creating video channel error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl; 
    else LOGDEBUG << "Video channel created, ID = " << testVideoChannel << endl;

    testAudioChannel = VoEEngineSingleton::Instance()->GetVoEBase()->CreateChannel();
    if( testAudioChannel == -1 )
        LOGERR << "Creating audio channel error: " << VoEEngineSingleton::Instance()->GetVoEBase()->LastError() << endl;
    else LOGDEBUG << "Audio channel created, ID = " << testAudioChannel << endl;

    if( VoEEngineSingleton::Instance()->GetViEBase()->ConnectAudioChannel( testVideoChannel, testAudioChannel ) != 0 )
        LOGERR << "ConnectAudioChannel() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "Audio channel connected successful " << endl;

    if( VoEEngineSingleton::Instance()->GetViECapture()->ConnectCaptureDevice( captureID, testVideoChannel ) != 0 )
        LOGERR << "ConnectCaptureDevice() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "Capture device connected successful " << endl;

    if( VoEEngineSingleton::Instance()->GetViERender()->StartRender( testVideoChannel ) != 0 )
        LOGERR << "StartRender() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO "StartRender() successed " << endl;

    if( VoEEngineSingleton::Instance()->GetViECapture()->StartCapture( captureID ) != 0 )
        LOGERR << "StartCapture() error: " << VoEEngineSingleton::Instance()->GetViEBase()->LastError() << endl;
    else LOGINFO << "StartCapture() successed " << endl;

    videoWidget->show();
}

When I click button for playing video - webcam starts playing but videoWidget window is black empty.

What am I doing not true? And maybe I must use other Qt widget for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2025-01-14 13:18:18

您必须在 Qt 中实现视频渲染器。子类 webrtc::ExternalRenderer 并绘制框架

You have to implement video renderer in Qt. subclass webrtc::ExternalRenderer and draw the frame

葵雨 2025-01-14 13:18:18
#include <QApplication>
#include <QUrl>
#include <QWebEngineView>

class WebEnginePage: public QWebEnginePage{
    Q_OBJECT
public:
    WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
        connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
    }
private Q_SLOTS:
    void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){

        if(feature  == QWebEnginePage::MediaAudioCapture
                || feature == QWebEnginePage::MediaVideoCapture
                || feature == QWebEnginePage::MediaAudioVideoCapture)
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
        else
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QWebEngineView view;
    view.setPage(new WebEnginePage);

    QUrl url = "https://appr.tc/r/386424467";

//    view.setUrl(commandLineUrlArgument());
    view.setUrl(url);
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

#include "main.moc"
#include <QApplication>
#include <QUrl>
#include <QWebEngineView>

class WebEnginePage: public QWebEnginePage{
    Q_OBJECT
public:
    WebEnginePage(QObject *parent = Q_NULLPTR):QWebEnginePage(parent){
        connect(this, &WebEnginePage::featurePermissionRequested, this, &WebEnginePage::onFeaturePermissionRequested);
    }
private Q_SLOTS:
    void onFeaturePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature){

        if(feature  == QWebEnginePage::MediaAudioCapture
                || feature == QWebEnginePage::MediaVideoCapture
                || feature == QWebEnginePage::MediaAudioVideoCapture)
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
        else
            setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser);
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QWebEngineView view;
    view.setPage(new WebEnginePage);

    QUrl url = "https://appr.tc/r/386424467";

//    view.setUrl(commandLineUrlArgument());
    view.setUrl(url);
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

#include "main.moc"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文