1

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 Camera. I had hoped to use the WebEngine library with PyQt but WebEngine is not supported in PyQt for RPi. So I am trying Qt itself now. Unfortunately I am not familiar with C++, so i am struggling.

The example here has 90% of what I need. The code is replicated below. I just need to tweak it to grant access to the mic and camera when it is requested. I am hoping someone can assist me with this?

#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();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lee Melbourne
  • 407
  • 5
  • 20

1 Answers1

3

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"
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • When creating signals and slots Qt creates moc files that handle those connections, when you create .ui those are created automatically, but in the case of main.cpp you must include it manually, that's why I have to do it in the example, check http: //doc.qt.io/qt-5/moc.html – eyllanesc Mar 23 '18 at 05:25
  • @LeeMelbourne If my answer helps you please mark it as correct. – eyllanesc Mar 23 '18 at 05:26
  • Thanks very much. When I run the code as posted I get "main.moc no such file or directory". If i remove that line i get "undefined reference to vtable for webenginepage". Can you help with what I might be doing wrong? – Lee Melbourne Mar 23 '18 at 05:35
  • @LeeMelbourne In Qt Creator go to the `build` tab and press the `run qmake` option. – eyllanesc Mar 23 '18 at 05:40
  • Great! Works nicely. Unfortunately when I go to sites like meet.jit.si or appear.in they still prompt to use cam & mic. But I think that is coming from their web application layer rather than from the browser. I dont plan on having a UI on the Pi so I need a work out a way around this. – Lee Melbourne Mar 23 '18 at 06:00
  • I ran qmake to create a makefile. When i run make I get "recipe for target 'main.moc' failed make: *** [main.moc] Error 127" – Lee Melbourne Apr 01 '18 at 07:00
  • @LeeMelbourne Are you using the console or Qt creator? console: `qmake && make`. If you still have a problem with that line, comment on it then. – eyllanesc Apr 01 '18 at 07:03
  • Using console. I was following a post I found somewhere that outlined the process to make an executable and it suggested qmake to create makefile then make. Running together your way also fails. Will try comment it... – Lee Melbourne Apr 01 '18 at 07:08
  • @LeeMelbourne Do you have the .pro? – eyllanesc Apr 01 '18 at 07:10
  • Yes, have project file. After comment it I get "Makefile:216: recipe for target 'main.o' failed make: *** [main.o] Error 1" – Lee Melbourne Apr 01 '18 at 07:11
  • @LeeMelbourne I'm going to share the project in a few moments, download it, with the console you are inside the project, then you execute `qmake`, and in the end `make` – eyllanesc Apr 01 '18 at 07:12
  • @LeeMelbourne In the following link is the project: https://github.com/eyllanesc/stackoverflow/tree/master/49312744, locate with the console within the project and execute: `qmake && make` – eyllanesc Apr 01 '18 at 07:18
  • Thanks very much. But I still get make: /usr/lib/x86_64-linux-gnu/qt4/bin/moc: Command not found Makefile:190: recipe for target 'main.moc' failed make: *** [main.moc] Error 127. It seems I am missing something in my environ. Will research... – Lee Melbourne Apr 01 '18 at 07:29
  • @LeeMelbourne From the error message you noticed that you are using Qt4: /usr/lib/x86_64-linux-gnu/ **qt4** /bin/moc, but the QWebEngineView code exists for versions higher than Qt 5.4, check if you have qt5 installed. Try with `qmake-qt5 && make` – eyllanesc Apr 01 '18 at 07:32
  • Thanks @eyllanesc. Rather than continue thread I created a separate issue here: https://stackoverflow.com/questions/49596099/qmake-not-working-with-qt-5-6 – Lee Melbourne Apr 01 '18 at 10:03