v0.3.5
New Features
Programmatic Video
This release of node-webrtc adds non-standard, programmatic video APIs in the form of RTCVideoSource and RTCVideoSink. With these APIs, you can
- Pass I420 frames to RTCVideoSource via the
onFrame
method. Then use RTCVideoSource'screateTrack
method to create a local video MediaStreamTrack. - Construct an RTCVideoSink from a local or remote video MediaStreamTrack. The RTCVideoSink will emit a "frame" event every time an I420 frame is received. When you're finished, stop the RTCVideoSink by calling
stop
.
Because these APIs are non-standard, they are exposed via a nonstandard
property on node-webrtc's exports object. For example,
const { RTCVideoSource, RTCVideoSink } = require('wrtc').nonstandard;
const source = new RTCVideoSource();
const track = source.createTrack();
const sink = new RTCVideoSink(track);
const width = 320;
const height = 240;
const data = new Uint8ClampedArray(width * height * 1.5);
const frame = { width, height, data };
const interval = setInterval(() => {
// Update the frame in some way before sending.
source.onFrame(frame);
});
sink.onframe = ({ frame }) => {
// Do something with the received frame.
};
setTimeout(() => {
clearInterval(interval);
track.stop();
sink.stop();
}, 10000);
This release also adds bindings to some libyuv functions for handling I420 frames. These can be useful when converting to and from RGBA.
RTCVideoSource
[constructor(optional RTCVideoSourceInit init)]
interface RTCVideoSource {
readonly attribute boolean isScreencast;
readonly attribute boolean? needsDenoising;
MediaStreamTrack createTrack();
void onFrame(RTCVideoFrame frame);
};
dictionary RTCVideoSourceInit {
boolean isScreencast = false;
boolean needsDenoising;
};
dictionary RTCVideoFrame {
required unsigned long width;
required unsigned long height;
required Uint8ClampedArray data;
};
- Calling
createTrack
will return a local video MediaStreamTrack whose source is the RTCVideoSource. - Calling
onFrame
with an RTCVideoFrame pushes a new video frame to every non-stopped local video MediaStreamTrack created withcreateTrack
. - An RTCVideoFrame represents an I420 frame.
RTCVideoSink
[constructor(MediaStreamTrack track)]
interface RTCVideoSink {
void stop();
readonly attribute boolean stopped;
attribute EventHandler onframe;
};
- RTCVideoSink's constructor accepts a local or remote video MediaStreamTrack.
- As long as neither the RTCVideoSink nor the RTCVideoSink's MediaStreamTrack are stopped, the RTCVideoSink will raise a "frame" event any time an RTCVideoFrame is received.
- The "frame" event has a property,
frame
, of type RTCVideoFrame. - RTCVideoSink must be stopped by calling
stop
.
i420ToRgba
and rgbaToI420
These two functions are bindings to libyuv that provide conversions between I420 and RGBA frames. WebRTC expects I420, whereas APIs like the Canvas API expect RGBA, so these functions are useful for converting between. For example,
const { i420ToRgba, rgbaToI420 } = require('wrtc').nonstandard;
const width = 640;
const height = 480;
const i420Data = new Uint8ClampedArray(width * height * 1.5);
const rgbaData = new Uint8ClampedArray(width * height * 4);
const i420Frame = { width, height, data: i420Data };
const rgbaFrame = { width, height, data: rgbaData };
i420ToRgba(i420Frame, rgbaFrame);
rgbaToI420(rgbaFrame, i420Frame);
MediaStreamTrack
- Added support for setting MediaStreamTrack's
enabled
property (#475).