Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <div class="container">
    <video id="local" autoplay></video>
    <video id="remote" autoplay></video>
    <div class="action">
        <button id="getMediaBtn">Get user media</button>
        <button id="connectBtn" disabled>RTCPeerConnection</button>
        <button id="closeBtn" disabled>Close connection</button>


                .container {
    margin: 0 auto;
    max-width: 1000px;

video {
    width: 45%;
    background-color: #000;
    margin-bottom: 20px;

video#local {
    margin-right: 20px;

button {
    color: #fff;
    background-color: #52c41a;
    border: 1px solid #52c41a;
    border-radius: 4px;
    padding: 0 15px;
    height: 32px;
    cursor: pointer;
    transition: all .3s cubic-bezier(.645, .045, .355, 1);

button:hover {
    background-color: #73d13d;
    border-color: #73d13d;

button:not(:last-child) {
    margin-right: 20px;

button[disabled] {
    color: rgba(0, 0, 0, 0.25);
    background-color: #f5f5f5;
    border-color: #d9d9d9;
    cursor: not-allowed;

button[disabled]:hover {
    color: rgba(0, 0, 0, 0.25);
    background-color: #f5f5f5;


                const localVideo = document.querySelector('#local');
const remoteVideo = document.querySelector('#remote');
const getMediaBtn = document.querySelector('#getMediaBtn');
const connectBtn = document.querySelector('#connectBtn');
const closeBtn = document.querySelector('#closeBtn');

let pc1, pc2, localStream;

getMediaBtn.addEventListener('click', getLocalStream);
connectBtn.addEventListener('click', createConnection);
closeBtn.addEventListener('click', close);

function getLocalStream() {
  getMediaBtn.disabled = true;
    video: true,
  }, function(stream) {
    localStream = stream;
    localVideo.srcObject = stream;
    connectBtn.disabled = false;
  }, function(err) {
    console.error(`getUserMedia() error: ${}`);

async function createConnection() {
  connectBtn.disabled = true;
  closeBtn.disabled = false;

  pc1 = new RTCPeerConnection({});
  console.log('创建本地 pc 实例 pc1');
  pc1.addEventListener('icecandidate', e => onIceCandidate(pc2, e));

  pc2 = new RTCPeerConnection({});
  console.log('创建远程 pc 实例 pc2');
  pc2.addEventListener('icecandidate', e => onIceCandidate(pc1, e));
  pc2.addEventListener('addstream', gotRemoteStream);

  console.log('添加本地流到 pc1 中');

  console.log('pc1 创建 offer');
  const offer = await pc1.createOffer({
    offerToReceiveVideo: 1,
  console.log(`Offer \n${offer.sdp}`);
  console.log('pc1 setLocalDescription');
  await pc1.setLocalDescription(offer);

  console.log('pc2 setRemoteDescription'); // 省去了发送 offer 信令给 pc2 的过程
  await pc2.setRemoteDescription(offer);
  console.log('pc2 createAnswer');
  const answer = await pc2.createAnswer();
  console.log(`Answer :\n${answer.sdp}`);
  console.log('pc2 setLocalDescription');
  await pc2.setLocalDescription(answer);

  console.log('pc1 setRemoteDescription'); // 省去了 pc2 将 answer 发送给 pc1 的过程
  await pc1.setRemoteDescription(answer);

function close() {
  pc1 = null;
  pc2 = null;
  closeBtn.disabled = true;
  connectBtn.disabled = false;

// 远程 pc2 实例获取到本地流
function gotRemoteStream(e) {
  if (remoteVideo.srcObject !== {
    remoteVideo.srcObject =;
    console.log('pc2 收到媒体流');

function getName(pc) {
  return (pc === pc1) ? 'pc1' : 'pc2';

// ICE 候选可用
async function onIceCandidate(pc, event) {
  await pc.addIceCandidate(event.candidate);
  console.log(`${getName(pc)} 添加 ICE candidate 候选成功:\n${event.candidate ? event.candidate.candidate : '(null)'}`);