OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. |
| 7 */ |
| 8 |
| 9 'use strict'; |
| 10 |
| 11 var dimensions = document.querySelector('#dimensions'); |
| 12 var video = document.querySelector('video'); |
| 13 var stream; |
| 14 |
| 15 var vgaButton = document.querySelector('#vga'); |
| 16 var qvgaButton = document.querySelector('#qvga'); |
| 17 var hdButton = document.querySelector('#hd'); |
| 18 var fullHdButton = document.querySelector('#full-hd'); |
| 19 |
| 20 vgaButton.onclick = function() { |
| 21 getMedia(vgaConstraints); |
| 22 }; |
| 23 |
| 24 qvgaButton.onclick = function() { |
| 25 getMedia(qvgaConstraints); |
| 26 }; |
| 27 |
| 28 hdButton.onclick = function() { |
| 29 getMedia(hdConstraints); |
| 30 }; |
| 31 |
| 32 fullHdButton.onclick = function() { |
| 33 getMedia(fullHdConstraints); |
| 34 }; |
| 35 |
| 36 var qvgaConstraints = { |
| 37 video: {width: {exact: 320}, height: {exact: 240}} |
| 38 }; |
| 39 |
| 40 var vgaConstraints = { |
| 41 video: {width: {exact: 640}, height: {exact: 480}} |
| 42 }; |
| 43 |
| 44 var hdConstraints = { |
| 45 video: {width: {exact: 1280}, height: {exact: 720}} |
| 46 }; |
| 47 |
| 48 var fullHdConstraints = { |
| 49 video: {width: {exact: 1920}, height: {exact: 1080}} |
| 50 }; |
| 51 |
| 52 function gotStream(mediaStream) { |
| 53 window.stream = mediaStream; // stream available to console |
| 54 video.srcObject = mediaStream; |
| 55 } |
| 56 |
| 57 function displayVideoDimensions() { |
| 58 if (!video.videoWidth) { |
| 59 setTimeout(displayVideoDimensions, 500); |
| 60 } |
| 61 dimensions.innerHTML = 'Actual video dimensions: ' + video.videoWidth + |
| 62 'x' + video.videoHeight + 'px.'; |
| 63 } |
| 64 |
| 65 video.onloadedmetadata = displayVideoDimensions; |
| 66 |
| 67 function getMedia(constraints) { |
| 68 if (stream) { |
| 69 stream.getTracks().forEach(function(track) { |
| 70 track.stop(); |
| 71 }); |
| 72 } |
| 73 |
| 74 navigator.mediaDevices.getUserMedia(constraints) |
| 75 .then(gotStream) |
| 76 .catch(function(e) { |
| 77 var message = 'getUserMedia error: ' + e.name; |
| 78 alert(message); |
| 79 console.log(message); |
| 80 }); |
| 81 } |
OLD | NEW |