Home | History | Annotate | Download | only in assets
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 (function () {
     18     "use strict";
     19 
     20     navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
     21     window.URL = window.URL || window.webkitURL;
     22 
     23     window.onload = function () {
     24 
     25         var video = document.querySelector('#video'),
     26             toggle = document.querySelector('#toggle'),
     27             stream = null;
     28 
     29         if (!navigator.getUserMedia) {
     30             console.error('getUserMedia not supported');
     31         }
     32 
     33         toggle.addEventListener('click', function () {
     34             if (null === stream) {
     35                 // This call to "getUserMedia" initiates a PermissionRequest in the WebView.
     36                 navigator.getUserMedia({ video: true }, function (s) {
     37                     stream = s;
     38                     video.src = window.URL.createObjectURL(stream);
     39                     toggle.innerText = 'Stop';
     40                     console.log('Started');
     41                 }, function (error) {
     42                     console.error('Error starting camera. Denied.');
     43                 });
     44             } else {
     45                 stream.stop();
     46                 stream = null;
     47                 toggle.innerText = 'Start';
     48                 console.log('Stopped');
     49             }
     50         });
     51 
     52         console.log('Page loaded');
     53 
     54     };
     55 
     56 })();
     57