3

I am building a simple web app called from Python. I am using the below code. What is the easiest way to programatically grant access to the Cam & Mic when this page is loaded? I have only found C++ examples on the web and cannot find a way to do this within Python code.

from PyQt5.QtWidgets import QApplication
from  PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl

app = QApplication([])

view = QWebEngineView()
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lee Melbourne
  • 407
  • 5
  • 20

2 Answers2

5

To give permission you must use the setFeaturePermission method of QWebEnginePage, but you must do it when the view asks you to do so when it emits the featurePermissionRequested signal, this will indicate the url and the feature.

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl

class WebEnginePage(QWebEnginePage):
    def __init__(self, *args, **kwargs):
        QWebEnginePage.__init__(self, *args, **kwargs)
        self.featurePermissionRequested.connect(self.onFeaturePermissionRequested)

    def onFeaturePermissionRequested(self, url, feature):
        if feature in (QWebEnginePage.MediaAudioCapture, 
            QWebEnginePage.MediaVideoCapture, 
            QWebEnginePage.MediaAudioVideoCapture):
            self.setFeaturePermission(url, feature, QWebEnginePage.PermissionGrantedByUser)
        else:
            self.setFeaturePermission(url, feature, QWebEnginePage.PermissionDeniedByUser)

app = QApplication([])

view = QWebEngineView()
page = WebEnginePage()
view.setPage(page)
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hi eyllanesc. I have since found out that there is no pyqt support for webengine on RPi. So I need to use c++ which is a language I dont know at all. I am starting with the example here http://doc.qt.io/archives/qt-5.7/qtwebengine-webenginewidgets-minimal-example.html. Can you possibly assist with how I would add the auto access to cam and mic to that example? – Lee Melbourne Mar 09 '18 at 10:11
0

So I found out that PyQt on the Raspberry Pi does not include support for the WebEngine capabilities. Therefore the WebEngineView class in PyQt cannot be used on the Pi. (I dont really understand why it works fine on Ubuntu but not on Raspbian, but anyway...).

I started down the path of using Qt itself, but then learned you can use the following approach

os.system('chromium-browser --use-fake-ui-for-media-stream %s' % URL)

to start Chrome with access to the microphone and camera pre-granted.

Lee Melbourne
  • 407
  • 5
  • 20