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: ' + constraints);
58 getUserMedia(constraints,
60 ensureGotAllExpectedStreams_(stream, constraints);
61 getUserMediaOkCallback_(stream);
63 getUserMediaFailedCallback_);
64 returnToTest('ok-requested');
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 return gRequestWebcamAndMicrophoneResult;
80 * Stops all tracks of the last acquired local stream.
82 function stopLocalStream() {
83 if (gLocalStream == null)
84 throw failTest('Tried to stop local stream, ' +
85 'but media access is not granted.');
87 gLocalStream.getVideoTracks().forEach(function(track) {
90 gLocalStream.getAudioTracks().forEach(function(track) {
94 gRequestWebcamAndMicrophoneResult = 'not-called-yet';
95 returnToTest('ok-stopped');
98 // Functions callable from other JavaScript modules.
101 * Adds the current local media stream to a peer connection.
102 * @param {RTCPeerConnection} peerConnection
104 function addLocalStreamToPeerConnection(peerConnection) {
105 if (gLocalStream == null)
106 throw failTest('Tried to add local stream to peer connection, ' +
107 'but there is no stream yet.');
109 peerConnection.addStream(gLocalStream, gAddStreamConstraints);
110 } catch (exception) {
111 throw failTest('Failed to add stream with constraints ' +
112 gAddStreamConstraints + ': ' + exception);
114 debug('Added local stream.');
118 * @return {string} Returns the current local stream - |gLocalStream|.
120 function getLocalStream() {
128 * @param {!MediaStream} stream Media stream from getUserMedia.
129 * @param {!object} constraints The getUserMedia constraints object.
131 function ensureGotAllExpectedStreams_(stream, constraints) {
132 if (constraints['video'] && stream.getVideoTracks().length == 0) {
133 gRequestWebcamAndMicrophoneResult = 'failed-to-get-video';
134 throw ('Requested video, but did not receive a video stream from ' +
135 'getUserMedia. Perhaps the machine you are running on ' +
136 'does not have a webcam.');
138 if (constraints['audio'] && stream.getAudioTracks().length == 0) {
139 gRequestWebcamAndMicrophoneResult = 'failed-to-get-audio';
140 throw ('Requested audio, but did not receive an audio stream ' +
141 'from getUserMedia. Perhaps the machine you are running ' +
142 'on does not have audio devices.');
148 * @param {MediaStream} stream Media stream.
150 function getUserMediaOkCallback_(stream) {
151 gLocalStream = stream;
152 gRequestWebcamAndMicrophoneResult = 'ok-got-stream';
154 attachMediaStream($('local-view'), stream);
159 * @param {NavigatorUserMediaError} error Error containing details.
161 function getUserMediaFailedCallback_(error) {
162 // Translate from the old error to the new. Remove when rename fully deployed.
163 var errorName = error.name;
165 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?');
166 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + errorName;
167 debug(gRequestWebcamAndMicrophoneResult);