Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Plugin] Live stream canvas via webrtc #976

Merged
merged 72 commits into from
Sep 15, 2022
Merged

[Plugin] Live stream canvas via webrtc #976

merged 72 commits into from
Sep 15, 2022

Conversation

Juice10
Copy link
Contributor

@Juice10 Juice10 commented Aug 25, 2022

Adds plugin to live stream contents of canvas elements via webrtc

Example of live streaming via yarn live-stream

webrtc-1080p.mov

Instructions

Record side

// Record side

import rrweb from 'rrweb';
import { RRWebPluginCanvasWebRTCRecord } from 'rrweb-plugin-canvas-webrtc-record';

const webRTCRecordPlugin = new RRWebPluginCanvasWebRTCRecord({
  signalSendCallback: (msg) => {
    // provides webrtc sdp offer signal & connect message
    // make sure you send this to the replayer's `webRTCReplayPlugin.signalReceive(signal)`
    sendSignalToReplayer(msg); // example of function that sends the signal to the replayer
  },
});

rrweb.record({
  emit: (event) => {
    // send these events to the `replayer.addEvent(event)`, how you do that is up to you
    // you can send them to a server for example which can then send them to the replayer
    sendEventToReplayer(event); // example of function that sends the event to the replayer
  },
  plugins: [
    // add the plugin to the list of plugins, and initialize it via `.initPlugin()`
    webRTCRecordPlugin.initPlugin()
  ],
  recordCanvas: false, // we don't want canvas recording turned on, we're going to do that via the plugin
});

Replay Side

// Replay side
import rrweb from 'rrweb';
import { RRWebPluginCanvasWebRTCReplay } from 'rrweb-plugin-canvas-webrtc-replay';

const webRTCReplayPlugin = new RRWebPluginCanvasWebRTCReplay({
  canvasFoundCallback(canvas, context) {
    console.log('canvas', canvas);
    // send the canvas id to `webRTCRecordPlugin.setupStream(id)`, how you do that is up to you
    // you can send them to a server for example which can then send them to the replayer
    sendCanvasIdToRecordScript(context.id); // example of function that sends the id to the record script
  },
  signalSendCallback(signal) {
    // provides webrtc sdp offer signal & connect message
    // make sure you send this to the record script's `webRTCRecordPlugin.signalReceive(signal)`
    sendSignalToRecordScript(signal); // example of function that sends the signal to the record script
  }
});

const replayer = new rrweb.Replayer([], {
  UNSAFE_replayCanvas: true, // turn canvas replay on!
  liveMode: true, // live mode is needed to stream events to the replayer
  plugins: [
    webRTCReplayPlugin.initPlugin()
  ],
});
replayer.startLive(); // start the replayer in live mode


replayer.addEvent(event); // call this whenever an event is received from the record script

Includes the following hooks for plugins

Record/Replay plugins

.getMirror exposes the mirror to plugins

Replay plugins

.onBuild called whenever a node was added to the dom

Other things this PR touches

Extra

Diagram of how the plugin works (PDF)

Juice10 and others added 30 commits May 31, 2022 18:09
When iframes get inserted they create untracked elements, both on the dom and rrdom side.
Because they are untracked they generate negative numbers when fetching the id from mirror.
This creates a problem when comparing and fetching ids across mirrors.
This commit tries to get away from using negative ids as much as possible in rrdom's comparisons
@Juice10 Juice10 marked this pull request as ready for review August 26, 2022 15:39
Copy link
Member

@YunFeng0817 YunFeng0817 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is very innovative both in design and implementation. Actually, I've used this branch many times for quicker debugging of some issues. I'm looking forward to using it in the main branch.

packages/rrweb/src/plugins/canvas-webrtc/replay/index.ts Outdated Show resolved Hide resolved
packages/rrweb/src/plugins/canvas-webrtc/record/index.ts Outdated Show resolved Hide resolved
packages/rrweb/scripts/stream.js Outdated Show resolved Hide resolved
packages/rrweb/scripts/stream.js Outdated Show resolved Hide resolved
packages/rrweb/scripts/stream.js Show resolved Hide resolved
packages/rrweb/scripts/stream.js Outdated Show resolved Hide resolved
@Yuyz0112 Yuyz0112 merged commit a8478f1 into master Sep 15, 2022
@YunFeng0817 YunFeng0817 deleted the live-stream branch September 15, 2022 03:51
@Juice10 Juice10 added the 2.0 label Dec 1, 2022
@YunFeng0817 YunFeng0817 mentioned this pull request Jan 10, 2023
@Code-Crash
Copy link
Contributor

Hi @Juice10 ,

I'm exploring video streaming recording via this plugin, do we have any examples for the same for the video?

Thanks.

@Juice10
Copy link
Contributor Author

Juice10 commented Mar 10, 2023

@Code-Crash I've created a PR to also do this for video, check it out here: #1046
It's pretty much finished just needs updating readme and renaming the plug-in

@Code-Crash
Copy link
Contributor

Thanks @Juice10 , will have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants