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.
8 * See http://dev.w3.org/2011/webrtc/editor/getusermedia.html for more
9 * information on getUserMedia.
13 * Keeps track of our local stream (e.g. what our local webcam is streaming).
16 var gLocalStream = null;
19 * The MediaConstraints to use when connecting the local stream with a peer
23 var gAddStreamConstraints = {};
26 * String which keeps track of what happened when we requested user media.
29 var gRequestWebcamAndMicrophoneResult = 'not-called-yet';
32 * Used as a shortcut. Moved to the top of the page due to race conditions.
33 * @param {string} id is a case-sensitive string representing the unique ID of
34 * the element being sought.
35 * @return {string} id returns the element object specified as a parameter
38 return document.getElementById(id);
42 * This function asks permission to use the webcam and mic from the browser.
43 * Its callbacks will return either request-callback-granted or
44 * request-callback-denied depending on the outcome. If the caller does not
45 * successfully resolve the request by granting or denying, the test will hang.
46 * To verify which callback was called, use obtainGetUserMediaResult().
48 * @param {!object} constraints Defines what to be requested, with mandatory
49 * and optional constraints defined. The contents of this parameter depends
50 * on the WebRTC version.
52 function doGetUserMedia(constraints) {
54 returnToTest('Browser does not support WebRTC.');
57 debug('Requesting doGetUserMedia: constraints: ' +
58 JSON.stringify(constraints, null, 0).replace(/[\r\n]/g, ''));
59 getUserMedia(constraints,
61 ensureGotAllExpectedStreams_(stream, constraints);
62 getUserMediaOkCallback_(stream);
64 getUserMediaFailedCallback_);
68 * Must be called after calling doGetUserMedia.
69 * @return {string} Returns not-called-yet if we have not yet been called back
70 * by WebRTC. Otherwise it returns either ok-got-stream or
71 * failed-with-error-x (where x is the error code from the error
72 * callback) depending on which callback got called by WebRTC.
74 function obtainGetUserMediaResult() {
75 returnToTest(gRequestWebcamAndMicrophoneResult);
76 var ret = gRequestWebcamAndMicrophoneResult;
77 // Reset for the next call.
78 gRequestWebcamAndMicrophoneResult = 'not-called-yet';
83 * Stops all tracks of the last acquired local stream.
85 function stopLocalStream() {
86 if (gLocalStream == null)
87 throw failTest('Tried to stop local stream, ' +
88 'but media access is not granted.');
90 gLocalStream.getVideoTracks().forEach(function(track) {
93 gLocalStream.getAudioTracks().forEach(function(track) {
97 gRequestWebcamAndMicrophoneResult = 'not-called-yet';
98 returnToTest('ok-stopped');
101 // Functions callable from other JavaScript modules.
104 * Adds the current local media stream to a peer connection.
105 * @param {RTCPeerConnection} peerConnection
107 function addLocalStreamToPeerConnection(peerConnection) {
108 if (gLocalStream == null)
109 throw failTest('Tried to add local stream to peer connection, ' +
110 'but there is no stream yet.');
112 peerConnection.addStream(gLocalStream, gAddStreamConstraints);
113 } catch (exception) {
114 throw failTest('Failed to add stream with constraints ' +
115 gAddStreamConstraints + ': ' + exception);
117 debug('Added local stream.');
121 * @return {string} Returns the current local stream - |gLocalStream|.
123 function getLocalStream() {
131 * @param {!MediaStream} stream Media stream from getUserMedia.
132 * @param {!object} constraints The getUserMedia constraints object.
134 function ensureGotAllExpectedStreams_(stream, constraints) {
135 if (constraints['video'] && stream.getVideoTracks().length == 0) {
136 gRequestWebcamAndMicrophoneResult = 'failed-to-get-video';
137 throw ('Requested video, but did not receive a video stream from ' +
138 'getUserMedia. Perhaps the machine you are running on ' +
139 'does not have a webcam.');
141 if (constraints['audio'] && stream.getAudioTracks().length == 0) {
142 gRequestWebcamAndMicrophoneResult = 'failed-to-get-audio';
143 throw ('Requested audio, but did not receive an audio stream ' +
144 'from getUserMedia. Perhaps the machine you are running ' +
145 'on does not have audio devices.');
151 * @param {MediaStream} stream Media stream.
153 function getUserMediaOkCallback_(stream) {
154 gLocalStream = stream;
155 gRequestWebcamAndMicrophoneResult = 'ok-got-stream';
157 attachMediaStream($('local-view'), stream);
159 returnToTest('request-callback-granted');
164 * @param {NavigatorUserMediaError} error Error containing details.
166 function getUserMediaFailedCallback_(error) {
167 // Translate from the old error to the new. Remove when rename fully deployed.
168 var errorName = error.name;
170 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?');
171 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + errorName;
172 debug(gRequestWebcamAndMicrophoneResult);
174 returnToTest('request-callback-denied');