Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML WebView Camera Permission

I'm working on a mobile app that has a video chat feature. I found a nice javascript library for webrtc, which seems to load just fine in QWebView but I need to give it permission to access the camera and microphone and I can't figure out how to do that. Is it possible? QWebEngineView has a handy signal and slot for that, but it's not supported for mobile.

Manifest permissions are not working as described here.

Corresponding Qt bug: cant access camera and mic with QML WebView

Any ideas?

like image 566
Derek Nowicki Avatar asked May 16 '17 05:05

Derek Nowicki


2 Answers

Qt has implemented Android's permission model in version 5.10.

See here: http://doc.qt.io/qt-5/qtandroid.html.

The functions you have to use are:

  • checkPermission
  • requestPermissions
  • requestPermissionsSync

If you want to access a camera and microphone you have to request the permissions before using. Example:

QStringList perms;
perms << "android.permission.CAMERA" << "android.permission.RECORD_AUDIO"; 
QtAndroid::checkPermissions(perms, [](const PermissionResultMap& resMap) {
     foreach(const QString &key, resMap.keys()) {
         qDebug() << "Result of permission" << key << ":" << resMap[key];
     }
}); 
like image 154
Hubi Avatar answered Sep 20 '22 14:09

Hubi


We resolved this QT bug by recompiling QT code and updating corresponding jars. We downloaded QT Source code for Android using Maintenance tool. While going through QT source code we found that due to some reason Qt developers are not overriding onPermissionRequest() of WebCromeClientdue to which WebView is not allowing Media access to JS functions. Please follow following steps to resolve issue.

  1. Download QT Source code.

  2. Update code in QtAndroidWebViewController.java. This class is located at ~/Qt/5.10.0/Src/qtwebview/src/jar/src/org/qtproject/qt5/android/view. Add following function to inner class QtAndroidWebChromeClient.

    @Override public void onPermissionRequest(PermissionRequest request) { request.grant(request.getResources()); }

  3. Depending on your Android sdk you may need to Comment/Change deprecated functions for older versions of Android. Also you can change few of the methods in QtAndroidWebViewClient as per new versions of Android.

  4. Then import project ~/Qt/5.10.0/Src/qtwebview In your Qt creator, You may need some Java knowledge to resolve issues(if you get some build issues)

  5. Depending on your build folder path Settings, successful build project will generate two jars in path build_folder/jar QtAndroidWebView.jar, QtAndroidWebView-bundled.jar.

  6. Replace jars in path ~/Qt/5.10.0/android_armv7/jar/.

  7. Now rebuild(Clean build) your original project, which is using WebView and issue is Resolved.

    Note: Path may change depending on QT Download Path and Operating System, but built jar can be replaced on any System(as java jars).

    If QT resolves this issue in Next release we can replace Updated jars.

like image 40
Swapnil Avatar answered Sep 17 '22 14:09

Swapnil