QtWebEngine 时允许使用 WebRTC 网络摄像头请求?

Posted

技术标签:

【中文标题】QtWebEngine 时允许使用 WebRTC 网络摄像头请求?【英文标题】:Allow WebRTC webcam request using when QtWebEngine? 【发布时间】:2015-06-16 18:36:04 【问题描述】:

当QtWebEngine(使用QML插件或不使用QML插件时)如何允许WebRTC网络摄像头请求?

webengine.qml

import QtQuick 2.1
import QtQuick.Controls 1.1
import QtWebEngine 1.0

ApplicationWindow 
    width: 800
    height: 600
    color: "lightgray"
    visible: true
    WebEngineView 
        id: webview
        url: "https://opentokrtc.com/test"
        anchors.fill: parent
    

在我的 Mac Yosemite 上,运行命令:

/usr/local/Cellar/qt5/5.4.0/bin/qmlscene webengine.qml 

但视频无法开始,因为它正在等待“允许”摄像头

在浏览器上你会有这个

有没有办法以编程方式设置 Chromium Web Engine 政策,例如VideoCaptureAllowed

【问题讨论】:

【参考方案1】:

将此添加到您的 WebEngineView 项目以从所有来源授予任何请求的功能,或选择性地将其限制为特定来源和特定功能:

    onFeaturePermissionRequested: 
        grantFeaturePermission(securityOrigin, feature, true);
    

【讨论】:

【参考方案2】:

你需要使用QtWebEngine.experimental请试试这个。

import QtQuick 2.4
import QtQuick.Window 2.2
import QtWebEngine 1.0
import QtWebEngine.experimental 1.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Private 1.0

Window 
    visible: true
    WebEngineView 
        id: webEngineView
        url: "https://test.webrtc.org/"
        anchors.fill: parent
        anchors.margins: 10
        experimental.onFeaturePermissionRequested: 
            console.log("request")
            experimental.grantFeaturePermission(securityOrigin, feature, true);
        

        readonly property string hideElementsJS: "
            function hideElement(id) 
                const el = document.getElementById(id);
                if (el) 
                    el.style.display = 'none';
                
            

            function hideElementsByClass(className) 
                const elList = document.getElementsByClassName(className);
                for (var i = 0, n = elList.length; i < n; ++i) 
                    elList[i].style.display = 'none';
                
            

            hideElement('hnArea');
            hideElement('lxSocialBarWrapper');
            hideElement('footerContent');
            hideElement('ftDisclaimers');
            hideElement('bottomNav');
            hideElement('topLinks');
            hideElement('rightMenuButtons');

            hideElementsByClass('footerText');
            hideElementsByClass('disclaimers');
        "

        onLoadingChanged: 
            if(loadRequest.status === WebEngineView.LoadSucceededStatus) 
                console.log("start")
                runjavascript(hideElementsJS);
                console.log("stop")
            
        
    

【讨论】:

【参考方案3】:

打开 fancybrowser 项目 在函数 MainWindow::MainWindow(const QUrl& url) 中添加 mainwindow.cpp

connect(view->page(), SIGNAL(featurePermissionRequested(QUrl,QWebEnginePage::Feature)),SLOT(test(QUrl,QWebEnginePage::Feature)));

还要加

void MainWindow::test(QUrl q, QWebEnginePage::Feature f) 
    view->page()->setFeaturePermission(q, f, 
    QWebEnginePage::PermissionGrantedByUser); 

在mainwindow.cpp和mainwindow.h下面

受保护的插槽:

void test(QUrl q, QWebEnginePage::Feature f);

然后一切正常!

【讨论】:

以上是关于QtWebEngine 时允许使用 WebRTC 网络摄像头请求?的主要内容,如果未能解决你的问题,请参考以下文章