Skip to content

Getting Started

This page provides a production-oriented onboarding path: establish a minimal playback/publishing flow first, then integrate cleanly at the framework layer.

Prerequisites

Before you begin, make sure your environment meets the following requirements:

  • Modern browsers (Chrome 56+, Firefox 44+, Safari 11+, Edge 79+)
  • HTTPS in production (or localhost during development)
  • A WebRTC-capable media server (e.g., SRS, ZLMediaKit, monibuca)

Installation

bash
pnpm add @webrtc-engine/core
# or
npm install @webrtc-engine/core

Use via Script Tag

If you do not use a bundler, you can load the built iife file directly with a regular <script> tag. The package will be exposed on the global WebRTCEngine object.

html
<script src="./dist/index.iife.js"></script>
<script>
  const { RtcPlayer } = WebRTCEngine;

  const player = new RtcPlayer({
    url: 'webrtc://localhost/live/livestream',
    api: 'http://localhost:1985/rtc/v1/play/',
    target: document.getElementById('video'),
  });

  player.play();
</script>

If you also want plugins, load the corresponding local iife bundle as well:

html
<script src="./dist/plugin-performance.iife.js"></script>

Note: the paths above are local static asset paths. They assume you have already built the package and the current HTML page can access the generated dist directory.

Playback

typescript
import { RtcPlayer } from '@webrtc-engine/core';

const player = new RtcPlayer({
  url: 'webrtc://localhost/live/livestream',
  api: 'http://localhost:1985/rtc/v1/play/',
  target: document.getElementById('video') as HTMLVideoElement,
});

player.on('state', (state) => console.log('Playback state:', state));
player.on('error', (error) => console.error('Playback error:', error));

await player.play();

Publishing

typescript
import { RtcPublisher } from '@webrtc-engine/core';

const publisher = new RtcPublisher({
  url: 'webrtc://localhost/live/pushstream',
  api: 'http://localhost:1985/rtc/v1/publish/',
  source: { type: 'camera', audio: true },
  target: document.getElementById('preview') as HTMLVideoElement,
});

publisher.on('streamstart', () => console.log('Publishing started'));
publisher.on('error', (error) => console.error('Publishing error:', error));

await publisher.start();

Supported Media Sources

TypeConfigDescription
Camera{ type: 'camera', audio: true }Video + audio
Screen{ type: 'screen', audio: true }Optional system audio
Microphone{ type: 'microphone' }Audio-only capture
Custom{ type: 'custom', stream }Existing MediaStream

Framework Integration

Framework examples are now split into dedicated pages. You can access them from the left sidebar under "Framework Integration", or jump directly to:

Engineering Recommendations

  • Encapsulate lifecycle management to prevent player/publisher instance leaks
  • Build observability around state and error events
  • Validate permission flows early and provide actionable guidance

Next Steps

基于 MIT 许可证发布