Function Description
This article mainly introduces the advanced usage of custom capture and custom rendering.
Custom Capture
There are usually several ways to obtain audioTrack
and videoTrack
:
Use videoElement.captureStream
to capture the audio and video being played in the video tag.
Use canvas.captureStream
to capture the animation in the canvas.
Capture the video being played in the video tag
if (!HTMLVideoElement.prototype.captureStream) {
console.log('your browser does not support capturing stream from video element');
return
}
const video = document.getElementByID('your-video-element-ID');
const stream = video.captureStream();
const audioTrack = stream.getAudioTracks()[0];
const videoTrack = stream.getVideoTracks()[0];
trtc.startLocalVideo({ option:{ videoTrack } });
trtc.startLocalAudio({ option:{ audioTrack } });
Capture the animation in the canvas
if (!HTMLCanvasElement.prototype.captureStream) {
console.log('your browser does not support capturing stream from canvas element');
return
}
const canvas = document.getElementByID('your-canvas-element-ID');
const fps = 15;
const stream = canvas.captureStream(fps);
const videoTrack = stream.getVideoTracks()[0];
trtc.startLocalVideo({ option:{ videoTrack } });
Custom Rendering
If you need to customize the rendering, and do not need the SDK to play the video, you can refer to the following steps:
Do not fill in the view
parameter or pass in null
when calling the startLocalVideo
or startRemoteVideo
method.
Use your own player for video rendering.
After using this custom rendering method, the EVENT.VIDEO_PLAY_STATE_CHANGED
event will not be triggered. You need to listen to the mute/unmute/ended
events of the video track MediaStreamTrack
to determine the status of the current video data stream. Custom rendering of local video
await trtc.startLocalVideo();
const videoTrack = trtc.getVideoTrack();
const videoElement = document.getElementById('video-element');
videoElement.srcObject = new MediaStream([videoTrack]);
videoElement.play();
Custom rendering of remote video
trtc.on(TRTC.EVENT.REMOTE_VIDEO_AVAILABLE, async ({ userId, streamType }) => {
await trtc.startRemoteVideo({ userId, streamType })
const videoTrack = trtc.getVideoTrack({ userId, streamType });
const videoElement = document.getElementById('remote-video-element');
videoElement.srcObject = new MediaStream([videoTrack]);
videoElement.play();
});
Was this page helpful?