webrtc-streaming
TypeScript icon, indicating that this package has built-in type declarations

6.0.35 • Public • Published

webrtc-streaming Launcher

webrtc-streaming launcher

Quick start

import { Launcher } from "webrtc-streaming";

const address = "xxxxxx";
const replaceWs = "xxxxxx";
const appKey = "xxxxxxx";
let appSecret;
let launcher;

async function bootstrap() {
  const data = await handleShareInfo();
  const { temporaryCredentials } = data;
  const baseOptionsType = {
    address,
    replaceWs,
    appKey,
    appSecret,
    startType: 1,
    temporaryCredentials
  };

  const container = document.querySelector("body");
  document.querySelector("body").style.width = "100%";
  document.querySelector("body").style.height = "100%";

  const uiOptions = {
    onPlay: () => {
      console.log('出现了有效画面')
      //web端->应用
      launcher.launcherBase.connection.emitUIInteraction('发给应用的数据').then(res => {
        console.log(res ? '发送成功' : '发送失败')
      })
    },
    onChange: (cb) => {
      const { phase,fakePercent } = cb;
      console.log('百分比:',fakePercent)
      if (phase === "signaling-connected") {
        //应用->web端
        launcher.launcherBase.connection.interaction.on((res)=>{
          console.log('收到应用的数据:',res)
        })
      }
    };
  };
  launcher = new Launcher(baseOptionsType, container, uiOptions);
}
async function handleShareInfo() {
  const params = {
    appKey,
    appSecret,
  };
  var url = new URL(`${address}/api/front/share/info`);
  Object.keys(params).forEach(
    (key) => params[key] && url.searchParams.append(key, params[key])
  );
  return fetch(url)
    .then((response) => response.json())
    .then((res) => {
    if (res.result) {
      return res.data;
    }
  });
}
window.addEventListener('DOMContentLoaded', () => {
  if (
    navigator.userAgent.includes('miniProgram') ||
    navigator.userAgent.includes('MicroMessenger')
  ) {
    new Promise((resolve, reject) => {
      document.addEventListener('WeixinJSBridgeReady', resolve)
    }).then(bootstrap)
  } else {
    bootstrap()
  }
})

Play callback

// call when video element start to play
extendOptions: {
  onPlay: () => {
    //do something
  },
}

Adjust bandwidth

launcher.changeBandwidthByRenegotiation(
  10000, // medians bitrate kbps
)

Statistics

launcher.toggleStatistics()
window.setInterval(() => {
  const { fps, bitrate, packetLossRate, latency, averageJitterBufferDelay } = launcher.report()
  console.log(`
    FPS: ${fps}
    biterate: ${bitrate}kbps
    latency: ${latency}ms
    averageJitterBufferDelay: ${averageJitterBufferDelay}ms
    packetLossRate: ${(packetLossRate * 100).toFixed(3)}%
  `)
}, 1000)

ToggleFullscreen

/* NOTE: fullscreen need user activation gesture
 * @see https://html.spec.whatwg.org/multipage/interaction.html#user-activation-processing-model
 */
someTriggerElement.addEventListener('click', () => launcher.toggleFullscreen())

Destory

launcher.destory([errMsg])

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
6.0.351latest

Version History

VersionDownloads (Last 7 Days)Published
6.0.351
6.0.341
6.0.330
6.0.321
6.0.310
6.0.3-0.51
6.0.3-0.40
6.0.3-0.30
6.0.3-0.20
6.0.3-0.10
6.0.302
6.0.290
5.9.00
5.8.21
5.8.11
5.8.00
5.7.00
5.6.01
5.5.00
5.4.00
5.3.02
5.2.01
5.1.10
5.1.01
5.0.01
4.0.00
3.6.00
3.5.01
3.4.00
3.3.01
3.2.01
3.1.00
3.0.00
2.3.02
2.2.30
2.2.20
2.2.11
2.2.02
2.1.00
2.0.01
1.2.11
1.1.21
1.1.11
1.1.01
1.0.11
1.0.00
0.1.10
0.1.00

Package Sidebar

Install

npm i webrtc-streaming

Weekly Downloads

28

Version

6.0.35

License

MIT

Unpacked Size

2.32 MB

Total Files

21

Last publish

Collaborators

  • jim1874