- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webrtc / manual / constraints.js
1 /**
2  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 /**
8  * See http://dev.w3.org/2011/webrtc/editor/getusermedia.html for more
9  * information on getUserMedia.
10  */
11
12 /**
13  * Asks permission to use the webcam and mic from the browser.
14  */
15 function doGetUserMedia() {
16   // Call into getUserMedia via the polyfill (adapter.js).
17   var constraints = getConstraints_();
18   var constraintsString = JSON.stringify(constraints, null, ' ');
19   $('getusermedia-constraints').innerHTML = constraintsString;
20   if (!getUserMedia) {
21     log_('Browser does not support WebRTC.');
22     return;
23   }
24   log_('Requesting getUserMedia with constraints: ' + constraintsString);
25   getUserMedia(constraints, getUserMediaOkCallback_,
26                getUserMediaFailedCallback_);
27 }
28
29 // Internals
30
31 /**
32  * Builds a Javascript constraints dictionary out of the selected options in the
33  * HTML controls on the page.
34  * @private
35  * @return {Object} A dictionary of constraints.
36  */
37 function getConstraints_() {
38   var c = {};
39   c.audio = $('audio').checked;
40   if (!$('video').checked) {
41     c.video = false;
42   } else {
43     c.video = { mandatory: {}, optional: [] };
44     // Mandatory - min
45     if ($('mandatory-min-width').value != '') {
46       c.video.mandatory.minWidth = $('mandatory-min-width').value;
47     }
48     if ($('mandatory-min-height').value != '') {
49       c.video.mandatory.minHeight = $('mandatory-min-height').value;
50     }
51     if ($('mandatory-min-fps').value != '') {
52       c.video.mandatory.minFrameRate = $('mandatory-min-fps').value;
53     }
54     if ($('mandatory-min-ar').value != '') {
55       c.video.mandatory.minAspectRatio = $('mandatory-min-ar').value;
56     }
57     // Mandatory - max
58     if ($('mandatory-max-width').value != '') {
59       c.video.mandatory.maxWidth = $('mandatory-max-width').value;
60     }
61     if ($('mandatory-max-height').value != '') {
62       c.video.mandatory.maxHeight = $('mandatory-max-height').value;
63     }
64     if ($('mandatory-max-fps').value != '') {
65       c.video.mandatory.maxFrameRate = $('mandatory-max-fps').value;
66     }
67     if ($('mandatory-max-ar').value != '') {
68       c.video.mandatory.maxAspectRatio = $('mandatory-max-ar').value;
69     }
70     // Optional - min
71     if ($('optional-min-width').value != '') {
72       c.video.optional.push({ minWidth: $('optional-min-width').value });
73     }
74     if ($('optional-min-height').value != '') {
75       c.video.optional.push({ minHeight: $('optional-min-height').value });
76     }
77     if ($('optional-min-fps').value != '') {
78       c.video.optional.push({ minFrameRate: $('optional-min-fps').value });
79     }
80     if ($('optional-min-ar').value != '') {
81       c.video.optional.push({ minAspectRatio: $('optional-min-ar').value });
82     }
83     // Optional - max
84     if ($('optional-max-width').value != '') {
85       c.video.optional.push({ maxWidth: $('optional-max-width').value });
86     }
87     if ($('optional-max-height').value != '') {
88       c.video.optional.push({ maxHeight: $('optional-max-height').value });
89     }
90     if ($('optional-max-fps').value != '') {
91       c.video.optional.push({ maxFrameRate: $('optional-max-fps').value });
92     }
93     if ($('optional-max-ar').value != '') {
94       c.video.optional.push({ maxAspectRatio: $('optional-max-ar').value });
95     }
96   }
97   return c;
98 }
99
100 /**
101  * @private
102  * @param {MediaStream} stream Media stream.
103  */
104 function getUserMediaOkCallback_(stream) {
105   gLocalStream = stream;
106   var videoTag = $('local-view');
107   attachMediaStream(videoTag, stream);
108
109   // Due to crbug.com/110938 the size is 0 when onloadedmetadata fires.
110   // videoTag.onloadedmetadata = updateVideoTagSize_(videoTag);
111   // Use setTimeout as a workaround for now.
112   setTimeout(function() {updateVideoTagSize_(videoTag)}, 500);
113   gRequestWebcamAndMicrophoneResult = 'ok-got-stream';
114 }
115
116 /**
117  * @private
118  * @param {Object} videoTag The video tag to update.
119  */
120 function updateVideoTagSize_(videoTag) {
121   // Don't update if sizes are 0 (happens for Chrome M23).
122   if (videoTag.videoWidth > 0 && videoTag.videoHeight > 0) {
123     log_('Set video tag width and height: ' + videoTag.videoWidth + 'x' +
124       videoTag.videoHeight);
125     videoTag.width = videoTag.videoWidth;
126     videoTag.height = videoTag.videoHeight;
127   }
128 }
129
130 /**
131  * @private
132  * @param {NavigatorUserMediaError} error Error containing details.
133  */
134 function getUserMediaFailedCallback_(error) {
135   log_('Failed with error: ' + error);
136 }
137
138 $ = function(id) {
139   return document.getElementById(id);
140 };
141
142 /**
143  * Simple logging function.
144  * @private
145  * @param {string} message Message to print.
146  */
147 function log_(message) {
148   console.log(message);
149   $('messages').innerHTML += message + '<br>';
150 }