This topic describes the basic features of DingRTC, which include initializing the SDK, joining a channel, publishing local streams, subscribing to remote streams, and leaving a channel.
Prerequisites
Download and integrate the latest version of the SDK. For more information, see How to integrate the Real-Time Communication SDK for Electron.
You can obtain a token for channel authentication. For more information, see Use a token for authentication.
Terms
When you integrate the DingRTC Electron SDK, you should be familiar with the following basic concepts:
The DingRTCClient class. An instance of this class represents a local client. Its methods provide features such as joining and leaving channels, and publishing and subscribing to audio and video streams.
The LocalTrack and RemoteTrack classes. Instances of these classes, and other classes that use them as a base class, represent an audio or video object. These objects provide control operations for audio and video streams, such as playback and pause.
Basic logic
Call the createClient() method to create a client instance.
Call join() to join a Real-Time Communication (RTC) channel.
Publish local audio and video tracks.
Create a camera track using createCameraVideoTrack() or a microphone track using createMicrophoneAudioTrack().
After you join the channel, call publish() and pass the audio and video track objects as parameters to publish them to the channel.
Subscribe to remote audio and video tracks.
Before you join the channel, listen for the "user-published" event to receive notifications when remote users publish streams. The callback parameters include the publisher's information and the audio or video track type.
Call subscribe() to subscribe to a remote user. After a successful subscription, you can retrieve the remote audio and video objects from the API response or from the properties of the RemoteUser object.
Call play() to play the remote audio and video tracks.
Procedure
The code examples in this topic are for reference only. You should develop your application based on your specific requirements.
Create a Client object.
import DingRTC from 'dingrtc-electron-sdk'; const client = DingRTC.createClient();Join a channel.
await client.join({ appId: joinInfo.appId, token: joinInfo.token, uid: joinInfo.uid, channel: joinInfo.channel, userName: joinInfo.userName, });The following information is required to join a channel:
Property
Type
Description
appId
string
The AppId of your project in DingRTC. Only uppercase letters, lowercase letters, digits, and underscores (_) are supported.
channel
string
The channel ID. It can contain only letters, digits, underscores (_), and hyphens (-). The length cannot exceed 64 characters.
token
string
The token for channel authentication.
uid
string
The user ID. It can contain only letters, digits, underscores (_), and hyphens (-). The length cannot exceed 64 characters.
NoteIf the same user ID logs on from another client, the client that joined the channel first is removed from the channel by the client that joined later.
userName
string
The username. The length cannot exceed 64 bytes in UTF-8 encoding.
Publish or unpublish local audio and video tracks.
Create local audio and video tracks
// Camera track const cameraTrack = await DingRTC.createCameraVideoTrack({ frameRate: 15, dimension: 'VD_1280x720', }); // Microphone track const micTrack = await DingRTC.createMicrophoneAudioTrack();NoteAfter the tracks are created, you can call the `play` method for a local preview, as shown in the following example.
Note:
The play method of a video track can specify a DOM element where the SDK creates a video element to play the video track. This means developers do not need to create the video element themselves. The example code specifies the DOM element by its ID value.
Typically, you do not need to play audio from your own microphone. This example is provided only to demonstrate how to use the interface in special scenarios, such as device diagnostics.
cameraTrack.play('#local'); // micTrack.play();Publish the local audio and video tracks
client.publish([cameraTrack, micTrack]);Unpublish the local audio and video tracks
client.unpublish([cameraTrack, micTrack]);
Subscribe to or unsubscribe from remote streams.
Subscribe to remote audio and video streams
Before you join the channel, listen for the Client "user-published" event on the object. This ensures that you do not miss notifications when remote users publish audio or video tracks. In the callback for this event, call subscribe() to subscribe to the remote user's audio and video tracks.
client.on('user-published', (user, mediaType, auxiliary) => { if (mediaType === 'video') { client.subscribe(user.userId, mediaType, auxiliary).then((track) => { track.play(`#uid${user.userId}`); }); // Here, mcuSubscribed is an assumed variable that indicates whether you have subscribed to the mixed MCU audio stream. } else if (!mcuSubscribed) { mcuSubscribed = true; client.subscribe('mcu', 'audio'); }); } });Alternatively, you can subscribe to the audio and video tracks published by a specific remote user from the remoteUsers property of the client instance.
for (const user of client.remoteUsers) { if (user.hasVideo) { client.subscribe(user.userId, 'video').then((track) => { track.play(`#uid${user.userId}`); }); } if (user.hasAudio && !mcuSubscribed) { mcuSubscribed = true; client.subscribe('mcu', 'audio'); } }NoteThe SDK does not yet support subscribing to the audio stream of a specific user. To subscribe to an audio stream, you must set `userId` to the string 'mcu'. This subscribes your client to the mixed audio stream of all remote users in the channel. You only need to subscribe to this mixed audio stream once. When a new remote user publishes an audio stream, it is automatically mixed into the subscribed MCU audio stream.
Unsubscribe from remote audio and video streams
client.unsubscribe(user.userId, 'video'); // Unsubscribe from the user's video track. client.unsubscribe('mcu', 'audio'); // Unsubscribe from the mixed remote audio track.NoteThe SDK does not yet support subscribing to the audio stream of a specific user. To unsubscribe from an audio stream, you must set `userId` to the string 'mcu'. This unsubscribes your client from the mixed audio stream of all remote users in the channel. You only need to perform this unsubscribe operation once.
Subscribe to video streams of different resolutions
You can subscribe to video streams in `high` and `low` resolution. The default is `high`. To change the subscribed video track of a specific user to low definition, call the following method:
client.setRemoteVideoStreamType(user.userId, 'low');Alternatively, you can change the default video resolution for all subscriptions:
client.setRemoteDefaultVideoStreamType('low');NoteThis method affects remote users who join the channel after the method is called. It does not affect remote users who have already joined the channel.
In WebSDK 3.3.0 and later, four new video resolutions are available for these two methods: FHD, HD, SD, and LD. For more information, see RemoteStreamType. The legacy `high` and `low` values are retained and correspond to FHD and LD, respectively. The default value is `high` or `FHD`, which represents the highest resolution published by the remote user. For example, if a remote user publishes a 1080p stream, setting the resolution to `FHD` or `high` subscribes you to the 1080p stream. If the remote user publishes a 480p stream, setting the resolution to `FHD` or `high` subscribes you to the 480p stream.
Leave the channel.
client.leave();
This method affects remote users who join the channel after the method is called. It does not affect remote users who have already joined the channel.
What to do next
You can download the sample code and run the demo to start a real-time audio and video call with other users in a channel. For more information, see Run the Electron Demo.