Ohos

更新时间:
复制 MD 格式

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

Procedure

Note The implementation methods in this topic are for reference only. You must develop your application based on your specific requirements.
  1. Initialize the SDK.

    Create a DingRtcSDK instance and register callbacks. For more information about the callback interfaces, see Callbacks and listeners.

    createRtc(context: Context,level?: number, dir?: string): void {
      if(!dingRtc) {
        if(level)
          DingRtcSDK.setLogLevel(level)
        if(dir)
          DingRtcSDK.setLogDir(dir)
        dingRtc = DingRtcSDK.create(context);
        dingRtc.setEventListener(new ChatRtcEventListener());
      }
    }
    1. Preview the local video. After you create the DingRtcSDK instance, you can create a canvas layout to preview the local video.

      let canvas: DingRtcConstants.RtcEngineVideoCanvas = {xComponentId: user.uid  + '_camera'}
      if(track == DingRtcConstants.RtcEngineVideoTrack.RtcEngineVideoTrackCamera) {
        canvas =  {xComponentId: user.uid  + '_camera', renderMode:this.renderMode}
        user.cameraCanvas = canvas;
      } else if(track == DingRtcConstants.RtcEngineVideoTrack.RtcEngineVideoTrackScreen) {
        canvas =  {xComponentId: user.uid  + '_screen', renderMode:this.renderMode}
      }
      dingRtc?.setLocalViewConfig(canvas,track);
      dingRtc?.startPreview();
    2. Optional: Stop the local preview.

      dingRtc?.stopPreview();
    3. 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, add the following code to your application:

        new DingRtcEventListener() {
          onJoinChannelResult(result: number, channel: string, userId: string, elapsed: number): void {
            console.info(TAG, `onJoinChannelResult: ${result}, elapsed: ${elapsed}`)
            if(result == DingRtcConstants.ErrorCode.NoError) {
              dingRtc?.publishLocalVideoStream(true);
              dingRtc?.publishLocalAudioStream(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 the streams, call the following methods to configure the settings before the user joins the channel:

        // Set auto-subscription for streams and the video definition.
        // Configure these values as needed.
        boolean autoSubAudio = true;
        boolean autoSubVideo = true;
        DingRtcConstants.RtcEngineVideoStreamType preferVideoQuality = DingRtcConstants.RtcEngineVideoStreamType.RtcEngineVideoStreamTypeFHD;
        
        dingRtc?.subscribeAllRemoteAudioStreams(autoSubAudio);
        dingRtc?.subscribeAllRemoteVideoStreams(autoSubVideo);
        dingRtc?.setRemoteDefaultVideoStreamType(preferVideoQuality);
  2. Join a channel.

      let authInfo = new DingRtcConstants.AuthInfo();
      authInfo.channelId = login.cname; /* Channel ID */;
      authInfo.userId = login.uid; /* User ID */;
      authInfo.appId = login.appId;  /* Application ID */;
      authInfo.token = login.token; /* Channel authentication token */;
      authInfo.gslbServer = login.gslbServer; /* GSLB endpoint */;
      ret = dingRtc?.joinChannel(authInfo, login.uname /* 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 (-).

    Note

    If 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).

  3. Publish or unpublish the local stream.

    You can also change the publishing state after joining the channel.

    • Publish local audio and video streams

      If no stream is being published, you can call the following methods to publish an audio or video stream:

      dingRtc?.publishLocalVideoStream(true);
      dingRtc?.publishLocalAudioStream(true);

      or cancel stream ingest

      dingRtc?.publishLocalVideoStream(false);
      dingRtc?.publishLocalAudioStream(false);

      You can control the audio and video streams independently.

  4. Subscribe to or unsubscribe from a remote stream.

    • Subscribe to remote audio and video streams

      The SDK automatically subscribes to remote streams after you join a channel, unless you disabled this feature before joining. If you disabled automatic subscription, you must call the following methods to manually subscribe to streams after joining the channel:

      //
      // For all streams: subscribe to all or unsubscribe from all.
      //
      // Audio
      dingRtc?.subscribeAllRemoteAudioStreams(true); // Set to false to unsubscribe.
      // Video
      dingRtc?.subscribeAllRemoteVideoStreams(true);
      dingRtc?.setRemoteDefaultVideoStreamType(preferVideoQuality); // Set the highest quality for stream pulling.
      
      //
      // For a specific stream
      //
      // Audio
      // The SDK does not currently support subscribing to individual audio streams.
      // Video
      dingRtc?.subscribeRemoteVideoStream(remoteUid,
          DingRtcConstants.RtcEngineVideoTrack.RtcEngineVideoTrackCamera,
          true); // Set to false to unsubscribe.
      dingRtc?.SetRemoteVideoStreamType(remoteUid, type);
          // type: DingRtcVideoStreamTypeFHD, DingRtcVideoStreamTypeHD,
          // DingRtcVideoStreamTypeSD, DingRtcVideoStreamTypeLD
          // Specifies the highest expected subscription quality. The actual bitrate is also limited by the network quality on the subscriber side.

      To display a video stream, you must set a window for it. You can set the window when you receive the onRemoteTrackAvailableNotify message, or at an earlier or later time.

      let canvas: DingRtcConstants.RtcEngineVideoCanvas = {xComponentId: user.uid  + '_camera'}
      if(track == DingRtcConstants.RtcEngineVideoTrack.RtcEngineVideoTrackCamera) {
        canvas =  {xComponentId: user.uid  + '_camera', renderMode:this.renderMode}
        user.cameraCanvas = canvas;
      } else if(track == DingRtcConstants.RtcEngineVideoTrack.RtcEngineVideoTrackScreen) {
        canvas =  {xComponentId: user.uid  + '_screen', renderMode:this.renderMode}
      }
      let isLocal = user.uid === this.login?.uid
      
      if(isLocal) {
        let ret = dingRtc?.setLocalViewConfig(canvas,track);
        console.info(TAG, `attachRender setLocalViewConfig ret:${ret}`)
      } else {
        let ret = dingRtc?.setRemoteViewConfig(canvas,user.uid,track);
        console.info(TAG, `attachRender setRemoteViewConfig ret:${ret}`)
      }

  5. Leave the channel.

    dingRtc?.leaveChannel();

  6. When you no longer need the SDK, destroy the instance to release its resources.

  release() {
    dingRtc?.release()
    dingRtc = null
    console.info(TAG, 'Call release done.')
  }

What to do next

Download the sample code to quickly run the demo and start a real-time audio and video call with other users in a channel. For more information, see