Auto Grant access to Mic and Camera with Qt WebEngine

前端 未结 1 961
醉酒成梦
醉酒成梦 2021-01-16 16:02

I am building a home security system with RPi and WebRTC. I simply need a way to trigger a browser to open at a given URL and to auto-grant access to the Microphone and Came

相关标签:
1条回答
  • 2021-01-16 16:19

    I answered this question but for PyQt5: Grant access to Cam & Mic using Python for PyQt WebEngine, I will only do a C ++ translation to Python, the base is the same.

    #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"
    
    0 讨论(0)
提交回复
热议问题