Android
This topic describes the basic features of DingRTC, which include initializing the software development kit (SDK), joining a channel, publishing a local stream, subscribing to a remote stream, and leaving a channel.
Prerequisites
You have downloaded and integrated the latest version of the SDK. For more information, see How to Integrate the Real-Time Communication SDK on Android.
You have obtained the token required for channel authentication. For more information, see Use a token for authentication.
Procedure
Initialize the SDK.
Create an RtcEngine instance and register callbacks. For more information about callback interfaces, see Callbacks and listeners.
import static com.ding.rtc.DingRtcEngine mRtcEngine = DingRtcEngine.create(getApplicationContext(), ""); mRtcEngine.setRtcEngineEventListener(new DingRtcEngineEventListener() { // Override the callback methods you need to listen for. });Preview the local video. After you create an RtcEngine instance, you can create a canvas layout to preview the local video.
DingRtcEngine.DingRtcVideoCanvas canvas = new DingRtcEngine.DingRtcVideoCanvas(); SurfaceView localView = mRtcEngine.createRenderSurfaceView(this); canvas.view = localView; canvas.renderMode = DingRtcEngine.DingRtcRenderMode.DingRtcRenderModeCrop; mRtcEngine.setLocalViewConfig(canvas, DingRtcVideoTrackCamera); mRtcEngine.startPreview();Optional: Stop the local preview.
mRtcEngine.stopPreview();Configure publishing and subscribing.
The SDK does not automatically publish streams when a user joins a channel. To publish streams immediately after a user joins a channel, implement the following code:
new DingRtcEngineEventListener() { @Override public void onJoinChannelResult(int result, String channel, String userId, int elapsed) { runOnUiThread(() -> { if(result == 0) { mRtcEngine.publishLocalAudioStream(true); mRtcEngine.publishLocalVideoStream(true); } }); } }By default, the SDK automatically subscribes to remote audio and video streams after a user joins a channel. If you do not want to automatically subscribe to streams, call the following methods before the user joins the channel:
// Configure automatic stream pulling and video definition. // Configure these values as needed. boolean autoSubAudio = true; boolean autoSubVideo = true; DingRtcEngine.DingRtcVideoStreamType preferVideoQuality = DingRtcEngine.DingRtcVideoStreamType.DingRtcVideoStreamTypeFHD; mRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(autoSubAudio); mRtcEngine.setDefaultSubscribeAllRemoteVideoStreams(autoSubVideo); mRtcEngine.setRemoteDefaultVideoStreamType(preferVideoQuality);
Join a channel.
DingRtcAuthInfo auth = new DingRtcAuthInfo(); auth.channelId = /* The channel ID. */; auth.appId = /* The application ID. */; auth.token = /* The authentication token. */; auth.userId = /* The user ID. */; auth.gslbServer = /* The GSLB address. */; mRtcEngine.joinChannel(auth, nick /* The display name. */);Parameter
Description
appId
The application ID. You can create and view the application ID on the Application Management page in the console.
channelId
The channel ID. It must be 1 to 64 characters in length and can contain uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).
userId
The user ID. It must be 1 to 64 characters in length and can contain uppercase letters, lowercase letters, digits, underscores (_), and hyphens (-).
NoteIf a user logs on from another client with the same user ID, the client that joined the channel first is removed from the channel.
token
The token for channel authentication.
gslbServer
The service endpoint. This parameter can be empty. The default value is
"https://gslb.dingrtc.com". We recommend that you obtain the endpoint from your business server and pass it to the client SDK instead of hardcoding it in the client.Important: We strongly recommend that you do not pass an empty value. Use the GSLB address returned from your business server (AppServer).
Publish or unpublish the local stream.
After you join a channel, you can publish or unpublish the local stream.
Publish local audio and video streams
If no stream is being published, call the following methods to publish the audio or video stream:
mRtcEngine.publishLocalVideoStream(true); mRtcEngine.publishLocalAudioStream(true);Or you can cancel the stream ingest.
mRtcEngine.publishLocalVideoStream(false); mRtcEngine.publishLocalAudioStream(false);You can control the audio and video streams independently.
Subscribe to or unsubscribe from a remote stream.
Subscribe to remote audio and video streams
By default, the SDK automatically subscribes to remote audio and video streams. If you have disabled automatic subscription, you can call the following methods to manually subscribe to streams after joining the channel:
// // To subscribe to or unsubscribe from all streams // // Audio mRtcEngine.subscribeAllRemoteAudioStreams(true); // false indicates unsubscribing. // Video mRtcEngine.subscribeAllRemoteVideoStreams(true); mRtcEngine.setRemoteDefaultVideoStreamType(preferVideoQuality); // Set the highest quality for stream pulling. // // To manage a specific stream // // Audio // The SDK does not support subscribing to or unsubscribing from a specific audio stream. // Video mRtcEngine.subscribeRemoteVideoStream(remoteUid, DingRtcEngine.DingRtcVideoTrack.DingRtcVideoTrackCamera, true); // false indicates unsubscribing. mRtcEngine.SetRemoteVideoStreamType(remoteUid, type); // type: DingRtcVideoStreamTypeFHD, DingRtcVideoStreamTypeHD, // DingRtcVideoStreamTypeSD, DingRtcVideoStreamTypeLD // Specifies the highest expected subscription quality. The actual stream is also limited by the network quality of the subscriber.To view the video content, you must also set up a window. The recommended time to set up the window is when the onRemoteTrackAvailableNotify callback is received. You can also set it up earlier or later.
SurfaceView remoteView = mRtcEngine.createRenderSurfaceView(this); DingRtcEngine.DingRtcVideoCanvas canvas = new DingRtcEngine.DingRtcVideoCanvas(); canvas.view = remoteView; canvas.renderMode = DingRtcEngine.DingRtcRenderMode.DingRtcRenderModeCrop; mRtcEngine.setRemoteViewConfig(canvas, remoteUid, DingRtcEngine.DingRtcVideoTrack.DingRtcVideoTrackCamera);
Leave the channel.
mRtcEngine.leaveChannel();You can shut down the engine.
if(mRtcEngine != null) {
mRtcEngine.destroy();
mRtcEngine = null;
}What to do next
You can download the sample code and run the demo to make real-time audio and video calls with others in a channel. For more information, see Run Android Demo.