美文网首页
WebRTC简介

WebRTC简介

作者: 霜之幽语 | 来源:发表于2017-12-19 15:37 被阅读19次

Three main tasks

  • Acquiring audio and video
  • Communicating audio and video
  • Communicating arbitrary data

Three main JavaScript APIs

  • MediaStream(aka getUserMedia)
  • RTCPeerConnection
  • RTCDataChannel
MediaStream
  • Represents a stream of audio and/or video
  • Can contain multiple 'tracks'
  • Obtain a MediaStream with navigator.getUserMedia()
    image.png
    aka getUserMedia
var constraints = {video: true};

function successCallback(stream) {
  var video = document.querySelector("video");
  video.src = window.URL.createObjectURL(stream);
}

function errorCallback(error) {
  console.log("navigator.getUserMedia error: ", error);
}

navigator.getUserMedia(constraints, successCallback, errorCallback);

Sample
http://www.simpl.info/gum
http://idevelop.github.io/ascii-camera/
http://www.shinydemos.com/facekat/
http://webcamtoy.com/app

constraints

  • Controls the contents of the MediaStream
  • Media type, resolution, frame rate
video: {
  mandatory: {
    minWidth: 640,
    minHeight: 360  
  },
  optional  [{
    minWidth: 1280,
    minHeight: 720
  }]
}

sample
https://simpl.info/getusermedia/constraints/

getUserMedia + Web Audio

// Success callback when requesting audio input stream
function gotStream(stream) {
  var audioContext = new webkitAudioContext();

  // Create an AudioNode from the stream
  var mediaStreamSource = audioContext.createMediaStreamSource(stream);

  // Connect it to the destination or any other node for processing!
  mediaStreamSource.connect(audioContext.destination);
}

navigator.webkitGetUserMedia({audio:true}, gotStream);

Make sure to enable Web Audio Input in about:flags!
Sample
http://www.webaudiodemos.appspot.com/AudioRecorder/index.html

gUM screencapture

var constraints = {
  video: {
    mandatory: {
      chromeMediaSource: 'screen'
    }  
  }
};

navigator.webkitGetUserMedia(constraints, gotStream);

Sample
https://html5-demos.appspot.com/static/getusermedia/screenshare.html

RTCPeerConnection
Audio and video communication between peers

RTCPeerConnection does a lot

  • Signal processing
  • Codec handling
  • Peer to peer communication
  • Security
  • Bandwidth management
    image.png
    RTCPeerConnection sample
pc = new RTCPeerConnection(null);
pc.onaddstream = gotRemoteStream;
pc.addStream(localStream);
pc.createOffer(gotOffer);

function gotOffer(desc) {
  pc.setLocalDescription(desc);
  sendOffer(desc);
}

function gotAnswer(desc) {
  pc.setRemoteDescription(desc);
}

function gotRemoteStream(e) {
  attachMediaStream(remoteVideo, e.stream);
}

Sample
http://www.simpl.info/pc

RTCDataChannel
Bidirectional communication of arbitrary data between peers

RTCDataChannel

  • Same API as WebSockets
  • Ultra-low latency
  • Unreliable or reliable
  • Secure
var pc = new webkitRTCPeerConnection(servers, {optional: [{RtpDataChannels: true}]});

pc.ondatachannel = function(event) {
  receiveChannel = event.channel;
  receiveChannel.onmessage = function(event) {
    document.querySelector("div#receive").innerHTML = event.data;
  }
};

sendChannel = pc.createDataChannel("sendDataChannel", {reliabel: false});

document.querySelector("button#send").onclick = function () {
  var data = document.querySelector("textarea#send").value;
  sendChannel.send(data);
};

Sample
http://www.simpl.info/dc
http://www.sharefest.me/

相关文章

  • WebRTC研究 (二) 实例demo

    @[TOC](WebRTC研究 (二) 实例demo) 1. WebRTC 简介 webrtc官网webrtc对i...

  • WebRTC基于浏览器的开发

    WebRTC简介 WebRTC通信原理 WebRTC需要通过长链接查找到通信双方,然后通过 peer to pee...

  • WebRTC笔记

    入门简介 https://webrtc.org.cn/20190419_tutorial3_webrtc_andr...

  • Vue + WebRTC 实现音视频直播(附自定义播放器样式)

    1. 什么是WebRTC 1.1 WebRTC简介 WebRTC,名称源自网页即时通信(英语:Web Real-T...

  • WebRTC简介

    1.WebRTC 全称 Web Real-Time Communication。 包含了媒体、加密、传输层等在内的...

  • WebRTC简介

    Three main tasks Acquiring audio and video Communicating ...

  • WebRTC简介

    什么是WebRTC WebRTC是一个由Google发起的实时通讯解决方案,其中包含视频音频采集,编解码,数据传输...

  • WebRTC简介

    对WebRTC有一个初步了解,然后根据功能和模块逐步深入

  • WebRTC回声消除技术

    1 WebRTC简介 WebRTC是谷歌公司2011年发布的一项技术。WebRTC技术主要用于在支持HTML5的浏...

  • webrtc入门简介

    最近一直在研究webrtc的相关工作,在这里也直接坐下记录。 什么是WebRtc webrtc是还未正式发布的一种...

网友评论

      本文标题:WebRTC简介

      本文链接:https://www.haomeiwen.com/subject/qtgbwxtx.html