WebRTC视频采集中的约束有哪些和具体的使用方法

约束

  • width : 宽度约束

  • height :高度约束

  • aspectRatio: 比率

  • frameRate: 帧率

  • facingMode : 摄像头控制

    • user:前置摄像头
    • environment :后摄像头
    • left : 前置左摄像头
    • right:前置右摄像头
  • resizeMode: 采集的画面需不需要裁剪

实战案例

vim index.html

<html>
  <head>
    <title>WebRTC 获取视频和音频</title>
  </head>
  <body>
    <div>
    <label>audio Source:</label>  
		<select id="audioSource"> </select>
    </div>
   <div>
    <label>audio Output:</label>  
		<select id="audioOutput"> </select>
    </div>
   <div>
    <label>video Source:</label>  
		<select id="videoSource"> </select>
    </div>
		
      
    <video autoplay playsinline id="player"></video>
    <script src="http://webrtc.github.io/adapter/adapter-latest.js"></script>
    <script src="./js/client.js"></script>
  </body>
</html>

vim client.js

"use strict"

// 获取设备
var audioSource = document.querySelector("select#audioSource");
var audioOutput = document.querySelector("select#audioOutput");
var videoSource = document.querySelector("select#videoSource");

// 查找视频播放的标签
var videoplay = document.querySelector("video#player");

if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia){
  console.log("获取音视频方法不存在");
}else{
  
  //---------------- 主要进行约束的地方- -----------------
  var constraints = {
    video : {
      // 宽度在300 - 640之间进行自适应
      width : {
      	min: 300,
        max: 640,
      },
      height: 480,
      frameRate: 15
      facingMode: enviroment // 设置为前置摄像头
      
    },
    audio : false
  };
  
  navigator.mediaDevices.getUserMedia(constraints)
  	.then(gotMediaStream).then(gotDevices)
  	.catch(handleError);
}

function gotMediaStream(stream){
  // 复制流到video标签
  videoplay.srcObject = stream;
  // 获取流成功之后还可以继续操作
  return navigator.mediaDevices.enumerateDevices();
  
  
}
function handleError(err){
  console.log("错误啦:", err)
}


function gotDevices(deviceInfos){
  deviceInfos.forEach(function(deviceinfo){
    var option = document.createElement("option");
    option.text = deviceinfo.label;
    option.value = deviceinfo.deviceId;
    
    
    // 音频输入
    if(deviceinfo.kind === "audioinput"){
      audioSource.appendChild(option);
      
    // 音频输出
    }else if(deviceinfo.kind === "audiooutput"){
       audioOutput.appendChild(option);
      
    // 视频输入
    }else if(deviceinfo.kind === "videoinput"){
       videoSource.appendChild(option);
    }
  })
}
原文地址:https://www.cnblogs.com/fandx/p/12142320.html