Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC - Browser doesn't ask for mic access permission for local html file

I have some simple webRTC code which uses getUserMedia to gain access to user's mic. Now when I load that html file (saved at my localhost) in my browser, the browser doesn't ask for mic access permission and thus get failed to get access.

But when I run the same html inside the w3schools.com editor, it asks for mic access permission and upon allowing it to access my mic, it works fine...

Why is this strange behaviour?

like image 916
Yatendra Avatar asked Jan 14 '13 12:01

Yatendra


2 Answers

When you open an html file right off the filesystem (file:// prefix), Chrome will auto-block getUserMedia permissions. You have to run a server locally.

I started up a sinatra server like this:

# server.rb
require 'sinatra'

get '/' do
  File.read('index.html')
end

Then give it ago.

$ gem install sinatra
$ ruby server.rb

http://localhost:4567

like image 166
Hugo Avatar answered Sep 28 '22 16:09

Hugo


Because of security Chrome won't open user media, e.g. WebCam when executing a file:/* document.

You could override however the security policy by starting chrome with the --disable-web-security command line option.

For testing check also the --use-fake-device-for-media-stream option.

N.B. When specifying command line options make sure there is no chrome/chromium process running. P.S Give it a try by creating a file test.html containing

<!DOCTYPE HTML>
<video autoplay/>
<script>
  navigator.webkitGetUserMedia({audio:true,video:true},
    function(stream){
      document.querySelector('video').src =
        URL.createObjectURL(stream);
    });
</script>

and than kill all chrome instances and start chrome like this:

 chrome.exe --use-fake-device-for-media-stream --disable-web-security test.html
like image 35
siddhadev Avatar answered Sep 28 '22 17:09

siddhadev