使用Qt WebEngine自动授予对Mic和Camera的访问权限

时间:2018-03-16 03:33:03

标签: c++ qt qt5 webrtc qwebengineview

我正在使用RPi和WebRTC构建家庭安全系统。我只需要一种方法来触发浏览器在给定的URL处打开并自动授予对麦克风和摄像头的访问权限。我曾希望将WebEngine库与PyQt一起使用,但PyQt for RPi不支持WebEngine。所以我现在正在尝试Qt。不幸的是我不熟悉C ++,所以我很挣扎。

示例here占我所需要的90%。代码在下面复制。我只需要调整它就可以在请求时授予对麦克风和摄像头的访问权限。我希望有人可以帮助我吗?

#include <QApplication>
#include <QWebEngineView>

QUrl commandLineUrlArgument()
{
    const QStringList args = QCoreApplication::arguments();
    for (const QString &arg : args.mid(1)) {
        if (!arg.startsWith(QLatin1Char('-')))
            return QUrl::fromUserInput(arg);
    }
    return QUrl(QStringLiteral("https://www.qt.io"));
}


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QWebEngineView view;
    view.setUrl(commandLineUrlArgument());
    view.resize(1024, 750);
    view.show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:2)

我回答了这个问题,但对于PyQt5:Grant access to Cam & Mic using Python for PyQt WebEngine,我只会对Python进行C ++转换,基础是相同的。

#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);
    }
};

QUrl commandLineUrlArgument()
{
    const QStringList args = QCoreApplication::arguments();
    for (const QString &arg : args.mid(1)) {
        if (!arg.startsWith(QLatin1Char('-')))
            return QUrl::fromUserInput(arg);
    }
    return QUrl(QStringLiteral("https://www.qt.io"));
}


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

    return app.exec();
}

#include "main.moc"