Android Question WebRTC with self signed cert can't load the camera and microphone

deantangNYP

Active Member
Licensed User
Longtime User
Appreciate if someone can advise. I have the following code. it is able to handle SSL Error. When the URL is loaded, the WebView does not pop up the "Allow" or "Block" for the Camera and Microphone for user acceptance.

B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

Sub Globals
    Private WebView1 As WebView
    Private we As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
        
        we.Initialize(WebView1)
        Dim client As DefaultWebViewClient
        client.Initialize("client")
        we.SetWebViewClient(client)
        WebView1.LoadUrl("https://192.168.0.52:8081/join?room=test&name=test2")

        End If
    End If
End Sub

Sub client_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
    Log(SslError1)
    SslErrorHandler1.Proceed
 
End Sub

Was expecting a message like this "allow" and "block" to appear.
Screenshot_20231117_182604.jpg
 
Last edited:

deantangNYP

Active Member
Licensed User
Longtime User
Anyone can help please? Cant seem to get the Webview to have the Allow or Block permission for Camera/Microphone to appear after handling of the SSL error

I wanted the webrtc camera to appear as follow. Can it be done on WebView? Is my code wrong? Please advise.
Capture2.PNG



B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

Sub Globals
    Private WebView1 As WebView
    Private we As WebViewExtras
    Private client As DefaultWebViewClient
 
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
 
    rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
        
            'Handle SSL Error ()
            we.Initialize(WebView1)
            client.Initialize("client")
            we.SetWebViewClient(client)
        
            ' Create and set the WebChromeClient
            Dim chromeClient As JavaObject
            chromeClient.InitializeNewInstance(Application.PackageName & ".main$MyChromeClient", Null)
            Dim jo As JavaObject = WebView1
            jo.RunMethod("setWebChromeClient", Array(chromeClient))
        
            WebView1.LoadUrl("https://192.168.0.52:8081/join?room=test&name=test2")
                
        End If
    End If

End Sub

Sub client_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
    Log("SSL Error Occurred: " & SslError1)
    SslErrorHandler1.Proceed
End Sub


#if Java
import android.webkit.*;
public static class MyChromeClient extends WebChromeClient {
@Override
     public void onPermissionRequest(PermissionRequest request) {
        request.grant(request.getResources());
    }
}

#End If
 

Attachments

  • Capture.PNG
    Capture.PNG
    36.1 KB · Views: 49
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
2 comments:

you should add this to your webchromeclient:
onConsoleMessage (ConsoleMessage consoleMessage)
you can check the documentation to get it working.
it's the only way to debug a webview.

it looks like you have turned off the grant permission prompt with
onPermissionRequest

i haven't done much with webview in a long time, but i seem to remember
granting permission with onPermissionRequest (just like you did) and no
longer seeing the prompt. however, in my case the camera and audio worked
(as i recall), so there may be an error in your case, which will only appear in the
log if you add onConsoleMessage to your chrome client. b4a is designed
to put webchromeclient console messages in the log
 
Upvote 0

deantangNYP

Active Member
Licensed User
Longtime User
2 comments:

you should add this to your webchromeclient:
onConsoleMessage (ConsoleMessage consoleMessage)
you can check the documentation to get it working.
it's the only way to debug a webview.

it looks like you have turned off the grant permission prompt with
onPermissionRequest

i haven't done much with webview in a long time, but i seem to remember
granting permission with onPermissionRequest (just like you did) and no
longer seeing the prompt. however, in my case the camera and audio worked
(as i recall), so there may be an error in your case, which will only appear in the
log if you add onConsoleMessage to your chrome client. b4a is designed
to put webchromeclient console messages in the log
Thanks for the reply. i not familiar with webview. still learning.

Can advise how you add onConsoleMessage to webchromeclient?
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
You need also to allow runtime permissions for CAMERA, RECORD_AUDIO and optional MODIFY_AUDIO_SETTINGS in manifest and with runtime permissions.
UltimateWebView is deprecated and no longer available. You can try WebkitLibrarySet, but it will not work with devices with SDK25 and below.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
camera and audio permissions used to be handled in webview. they are now handled with runtime
permissions and permission requests in the manifest. in addition - and because of this - you need
to grant camera/audio permissions in the webchromeclient. you have done this, or at least, you tried.

in order to debug webview, you need to handle console messages. that means overriding
webchromeclient's normal handling. for the most part, console messages are generated for you;
you simply need to override webchromeclient's handling and redirect them to BA.Log

so, with all of this in mind, do the following

in b4a:

remove this:
B4X:
        we.Initialize(WebView1)
        Dim client As DefaultWebViewClient
        client.Initialize("client")
        we.SetWebViewClient(client)

replace with:
B4X:
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("addWCC", Array(webview))

in inline java, do this:
remove your inline java

replace with:
B4X:
#if Java
import android.webkit.*;
    public static void addWCC( WebView wv ) {
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setMediaPlaybackRequiresUserGesture(false);
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
               public boolean onConsoleMessage (ConsoleMessage consoleMessage) {
                BA.Log(consoleMessage.message() + " (" + consoleMessage.messageLevel() + ")");
                 return true;
            }


           @Override
              public void onPermissionRequest(PermissionRequest request) {
            wv.evaluateJavascript("console.log('overriding permission request');",null);
               request.grant(request.getResources());    // this alone is enough to grant all the permissions, currently only video/audio
           }
          
         });         // end of add webchromeclient

       wv.evaluateJavascript("console.log('webchrome client added');",null);
      
     }

#End If

when i run a simple webcam app in a webview, my camera opens. as far as webrtc is concerned,
i cannot comment on what you have implemented there. i can only say my camera opens when i
run a simple html document in a webview.
 
Upvote 0

deantangNYP

Active Member
Licensed User
Longtime User
You need also to allow runtime permissions for CAMERA, RECORD_AUDIO and optional MODIFY_AUDIO_SETTINGS in manifest and with runtime permissions.
UltimateWebView is deprecated and no longer available. You can try WebkitLibrarySet, but it will not work with devices with SDK25 and below.
Thanks for the advise
 
Upvote 0

deantangNYP

Active Member
Licensed User
Longtime User
camera and audio permissions used to be handled in webview. they are now handled with runtime
permissions and permission requests in the manifest. in addition - and because of this - you need
to grant camera/audio permissions in the webchromeclient. you have done this, or at least, you tried.

in order to debug webview, you need to handle console messages. that means overriding
webchromeclient's normal handling. for the most part, console messages are generated for you;
you simply need to override webchromeclient's handling and redirect them to BA.Log

so, with all of this in mind, do the following

in b4a:

remove this:
B4X:
        we.Initialize(WebView1)
        Dim client As DefaultWebViewClient
        client.Initialize("client")
        we.SetWebViewClient(client)

replace with:
B4X:
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("addWCC", Array(webview))

in inline java, do this:
remove your inline java

replace with:
B4X:
#if Java
import android.webkit.*;
    public static void addWCC( WebView wv ) {
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setMediaPlaybackRequiresUserGesture(false);
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
               public boolean onConsoleMessage (ConsoleMessage consoleMessage) {
                BA.Log(consoleMessage.message() + " (" + consoleMessage.messageLevel() + ")");
                 return true;
            }


           @Override
              public void onPermissionRequest(PermissionRequest request) {
            wv.evaluateJavascript("console.log('overriding permission request');",null);
               request.grant(request.getResources());    // this alone is enough to grant all the permissions, currently only video/audio
           }
         
         });         // end of add webchromeclient

       wv.evaluateJavascript("console.log('webchrome client added');",null);
     
     }

#End If

when i run a simple webcam app in a webview, my camera opens. as far as webrtc is concerned,
i cannot comment on what you have implemented there. i can only say my camera opens when i
run a simple html document in a webview.
Thank you so much for your advise
 
Upvote 0
Top