This document describes the basic workflow of the TRTC SDK for Web and how to implement the real-time audio/video call feature.
You will deal with two types of objects in your use of the TRTC SDK for web.
Stream
class provides APIs for stream-related actions, including audio/video playback control.Below are the APIs used in a basic audio/video call:
Create a client object using TRTC.createClient(). Set the parameters as follows:
mode
: TRTC mode, which should be set to rtc
sdkAppId
: the sdkAppId
you obtain from Tencent ClouduserId
: user IDuserSig
: user signature. For the calculation method, please see UserSigconst client = TRTC.createClient({
mode: 'rtc',
sdkAppId,
userId,
userSig
});
Call Client.join() to enter a TRTC room.roomId
: room ID
client
.join({ roomId })
.then(() => {
console.log('Entered room successfully');
})
.catch(error => {
console.error('Failed to enter room' + error);
});
userId
: ID of the user to whom the stream belongs
audio
: whether to enable audio
video
: whether to enable video
const localStream = TRTC.createStream({ userId, audio: true, video: true });
localStream
.initialize()
.then(() => {
console.log('Local stream initialized successfully');
})
.catch(error => {
console.error('Failed to initialize local stream' + error);
});
client
.publish(localStream)
.then(() => {
console.log('Local stream published successfully');
})
.catch(error => {
console.error('Failed to publish local stream' + error);
});
Note:
- To ensure that you are notified when a remote user enters the room, please register the Client.on('stream-added') callback before you call Client.join() to enter the room.
- For other events such as the exit of a remote user, please see the API documentation.
client.on('stream-added', event => {
const remoteStream = event.stream;
console.log('New remote stream:' + remoteStream.getId());
// Subscribe to the remote stream
client.subscribe(remoteStream);
});
client.on('stream-subscribed', event => {
const remoteStream = event.stream;
console.log('Subscribed to remote stream successfully:' + remoteStream.getId());
// Play the remote stream
remoteStream.play('remote_stream-' + remoteStream.getId());
});
play
method allows a parameter that is a div element ID. The SDK will create an audio/video tag in the div element and play the stream on it.Play the local stream after successful initialization
localStream
.initialize()
.then(() => {
console.log('Local stream initialized successfully');
localStream.play('local_stream');
})
.catch(error => {
console.error('Failed to initialize local stream' + error);
});
Play a remote stream after successful subscription
client.on('stream-subscribed', event => {
const remoteStream = event.stream;
console.log('Subscribed to remote stream successfully:' + remoteStream.getId());
// Play the remote stream
remoteStream.play('remote_stream-' + remoteStream.getId());
});
Call Client.leave() to exit the room, and the call session ends.
client
.leave()
.then(() => {
// Exited room. You can call `client.join` to start a new call.
})
.catch(error => {
console.error('Failed to leave room' + error);
// The error is unrecoverable. Please refresh the page.
});
Note:The value of
appScene
must be the same on each client. InconsistentappScene
may cause unexpected problems.
Was this page helpful?