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     document.addEventListener('DOMContentLoaded', 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                 if (stream.getTracks) {
     46                     stream.getTracks().forEach(function (track) {
     47                         track.stop();
     48                     });
     49                 } else if (stream.stop) {
     50                     stream.stop();
     51                 }
     52                 stream = null;
     53                 toggle.innerText = 'Start';
     54                 console.log('Stopped');
     55             }
     56         });
     57 
     58         console.log('Page loaded');
     59 
     60     });
     61 
     62 })();
     63