WebRTC Multi Devices


<!DOCTYPE html>
<!–
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
–>
<html>
<head>
<meta charset=”utf-8“>
<meta name=”descriptioncontent=”WebRTC code samples“>
<meta name=”viewportcontent=”width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1“>
<meta itemprop=”descriptioncontent=”Client-side WebRTC code samples“>
<meta itemprop=”imagecontent=”../../../images/webrtc-icon-192×192.png“>
<meta itemprop=”namecontent=”WebRTC code samples“>
<meta name=”mobile-web-app-capablecontent=”yes“>
<meta id=”theme-colorname=”theme-colorcontent=”#ffffff“>
<base target=”_blank“>
<title>getUserMedia: output device selection</title>
<link rel=”iconsizes=”192×192href=”../../../images/webrtc-icon-192×192.png“>
<link href=”//fonts.googleapis.com/css?family=Roboto:300,400,500,700rel=”stylesheettype=”text/css“>
<link rel=”stylesheethref=”../../../css/main.css“>
<link rel=”stylesheethref=”css/main.css“>
</head>
<body>
<div id=”container“>
<h1><a href=”//webrtc.github.io/samples/title=”WebRTC samples homepage“>WebRTC samples</a> <span>getUserMedia: output device selection</span>
</h1>
<div id=”sources“>
<div>
<h2>getUserMedia():</h2>
<div>
<video class=”gumtitle=”audio and video stream from getUserMedia()playsinline autoplay controls></video>
<div class=”outputSelector“>
<label>Select audio output: </label>
<select></select>
</div>
</div>
<div>
<audio class=”gumtitle=”audio stream from getUserMedia()controls></audio>
<div class=”outputSelector“>
<label>Select audio output: </label>
<select></select>
</div>
</div>
</div>
<div>
<h2>Local media files:</h2>
<div>
<video title=”local video fileplaysinline controls loop poster=”images/poster.jpgpreload=”metadata“>
<source src=”video/chrome.webmtype=”video/webm“/>
<source src=”video/chrome.mp4type=”video/mp4“/>
<p>This browser does not support the video element.</p>
</video>
<div class=”outputSelector“>
<label>Select audio output: </label>
<select></select>
</div>
</div>
<div>
<audio title=”local audio filecontrols loop>
<source src=”audio/audio.mp3type=”audio/mp3“/>
This browser does not support the audio element.
</audio>
<div class=”outputSelector“>
<label>Select audio output: </label>
<select></select>
</div>
</div>
</div>
</div>
<p>This demo must be run from localhost or over HTTPS Chrome 49 or later, Firefox is not supported yet.</p>
<a href=”https://github.com/webrtc/samples/tree/gh-pages/src/content/devices/multi
title=”View source for this page on GitHubid=”viewSource“>View source on GitHub</a>
</div>
<script src=”https://webrtc.github.io/adapter/adapter-latest.js“></script>
<script src=”js/main.jsasync></script>
<script src=”../../../js/lib/ga.js“></script>
</body>
</html>

main.js

/*
 *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree.
 */

'use strict';

const gumAudio = document.querySelector('audio.gum');
gumAudio.addEventListener('play', () => {
  gumAudio.volume = 0.1;
  console.log('Audio lowered to reduce feedback from local gUM stream');
});
const gumVideo = document.querySelector('video.gum');
gumVideo.addEventListener('play', () => {
  gumVideo.volume = 0.1;
  console.log('Audio lowered to reduce feedback from local gUM stream');
});

function gotDevices(deviceInfos) {
  const masterOutputSelector = document.createElement('select');

  for (let i = 0; i !== deviceInfos.length; ++i) {
    const deviceInfo = deviceInfos[i];
    const option = document.createElement('option');
    option.value = deviceInfo.deviceId;
    if (deviceInfo.kind === 'audiooutput') {
      console.info('Found audio output device: ', deviceInfo.label);
      option.text = deviceInfo.label || `speaker ${masterOutputSelector.length + 1}`;
      masterOutputSelector.appendChild(option);
    } else {
      console.log('Found non audio output device: ', deviceInfo.label);
    }
  }

  // Clone the master outputSelector and replace outputSelector placeholders.
  const allOutputSelectors = document.querySelectorAll('select');
  for (let selector = 0; selector < allOutputSelectors.length; selector++) {
    const newOutputSelector = masterOutputSelector.cloneNode(true);
    newOutputSelector.addEventListener('change', changeAudioDestination);
    allOutputSelectors[selector].parentNode.replaceChild(newOutputSelector,
        allOutputSelectors[selector]);
  }
}

navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);

// Attach audio output device to the provided media element using the deviceId.
function attachSinkId(element, sinkId, outputSelector) {
  if (typeof element.sinkId !== 'undefined') {
    element.setSinkId(sinkId)
        .then(() => {
          console.log(`Success, audio output device attached: ${sinkId} to element with ${element.title} as source.`);
        })
        .catch(error => {
          let errorMessage = error;
          if (error.name === 'SecurityError') {
            errorMessage = `You need to use HTTPS for selecting audio output device: ${error}`;
          }
          console.error(errorMessage);
          // Jump back to first output device in the list as it's the default.
          outputSelector.selectedIndex = 0;
        });
  } else {
    console.warn('Browser does not support output device selection.');
  }
}

function changeAudioDestination(event) {
  const deviceId = event.target.value;
  const outputSelector = event.target;
  // FIXME: Make the media element lookup dynamic.
  const element = event.path[2].childNodes[1];
  attachSinkId(element, deviceId, outputSelector);
}

function gotStream(stream) {
  window.stream = stream; // make stream available to console
  gumAudio.srcObject = stream;
  gumVideo.srcObject = stream;
}

function start() {
  if (window.stream) {
    window.stream.getTracks().forEach(track => {
      track.stop();
    });
  }
  const constraints = {
    audio: true,
    video: true
  };
  navigator.mediaDevices.getUserMedia(constraints).then(gotStream).catch(handleError);
}

start();

function handleError(error) {
  console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google

Você está comentando utilizando sua conta Google. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s