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. It
43 * will return ok-requested to the test. This does not mean the request was
44 * approved though. The test will then have to click past the dialog that
45 * appears in Chrome, which will run either the OK or failed callback as a
46 * a result. To see 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_);
65 returnToTest('ok-requested');
69 * Must be called after calling doGetUserMedia.
70 * @return {string} Returns not-called-yet if we have not yet been called back
71 * by WebRTC. Otherwise it returns either ok-got-stream or
72 * failed-with-error-x (where x is the error code from the error
73 * callback) depending on which callback got called by WebRTC.
75 function obtainGetUserMediaResult() {
76 returnToTest(gRequestWebcamAndMicrophoneResult);
77 var ret = gRequestWebcamAndMicrophoneResult;
78 // Reset for the next call.
79 gRequestWebcamAndMicrophoneResult = 'not-called-yet';
84 * Stops all tracks of the last acquired local stream.
86 function stopLocalStream() {
87 if (gLocalStream == null)
88 throw failTest('Tried to stop local stream, ' +
89 'but media access is not granted.');
91 gLocalStream.getVideoTracks().forEach(function(track) {
94 gLocalStream.getAudioTracks().forEach(function(track) {
98 gRequestWebcamAndMicrophoneResult = 'not-called-yet';
99 returnToTest('ok-stopped');
102 // Functions callable from other JavaScript modules.
105 * Adds the current local media stream to a peer connection.
106 * @param {RTCPeerConnection} peerConnection
108 function addLocalStreamToPeerConnection(peerConnection) {
109 if (gLocalStream == null)
110 throw failTest('Tried to add local stream to peer connection, ' +
111 'but there is no stream yet.');
113 peerConnection.addStream(gLocalStream, gAddStreamConstraints);
114 } catch (exception) {
115 throw failTest('Failed to add stream with constraints ' +
116 gAddStreamConstraints + ': ' + exception);
118 debug('Added local stream.');
122 * @return {string} Returns the current local stream - |gLocalStream|.
124 function getLocalStream() {
132 * @param {!MediaStream} stream Media stream from getUserMedia.
133 * @param {!object} constraints The getUserMedia constraints object.
135 function ensureGotAllExpectedStreams_(stream, constraints) {
136 if (constraints['video'] && stream.getVideoTracks().length == 0) {
137 gRequestWebcamAndMicrophoneResult = 'failed-to-get-video';
138 throw ('Requested video, but did not receive a video stream from ' +
139 'getUserMedia. Perhaps the machine you are running on ' +
140 'does not have a webcam.');
142 if (constraints['audio'] && stream.getAudioTracks().length == 0) {
143 gRequestWebcamAndMicrophoneResult = 'failed-to-get-audio';
144 throw ('Requested audio, but did not receive an audio stream ' +
145 'from getUserMedia. Perhaps the machine you are running ' +
146 'on does not have audio devices.');
152 * @param {MediaStream} stream Media stream.
154 function getUserMediaOkCallback_(stream) {
155 gLocalStream = stream;
156 gRequestWebcamAndMicrophoneResult = 'ok-got-stream';
158 attachMediaStream($('local-view'), stream);
163 * @param {NavigatorUserMediaError} error Error containing details.
165 function getUserMediaFailedCallback_(error) {
166 // Translate from the old error to the new. Remove when rename fully deployed.
167 var errorName = error.name;
169 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?');
170 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + errorName;
171 debug(gRequestWebcamAndMicrophoneResult);