
# SDK使用
1. 加入房间前必须有此步骤，检测浏览器是否兼容 SDK。具体接口详情请参见[checkSystemRequirements](https://support.huaweicloud.com/csdk-rtc/rtc_05_0137.html#rtc_05_0137__section1668175819279)。
   
   ```
   async isBrowserSupport() {
       let check = await HRTC.checkSystemRequirements(false, 'sendrecv')
       if (check === true) {
         console.warn('#############browser isSupport: ' + check)
       } else {
         console.error(`#############check browser isSupport error: ${check.getCode()} - ${check.getMsg()}`)
       }
    }
   ```
   
   
2. 创建客户端。具体接口详情请参见[createClient](https://support.huaweicloud.com/csdk-rtc/rtc_05_0137.html#rtc_05_0137__section16476840112615)。
   
   ```
   let config = { appId,domain,countryCode }
   let client = HRTC.createClient(config)
   ```
   - **domain**：string\[128\]类型，服务器域名。该参数在SDK 1.0+版本中必填，SDK 2.0+版本中非必填。
   
   - **appId**：string\[128\]类型，必填。应用ID，只有App ID相同的应用程序才能进入同一个房间进行互通。
   
   - **countryCode** ：string\[2\]类型，可选。国家码，如：CN表示中国大陆，US表示美国，HK表示中国香港。countryCode值的填写具体请参见[国家码对照表](https://support.huaweicloud.com/csdk-rtc/rtc_05_0352.html)。
   
   
   ![](https://support.huaweicloud.com/csdk-rtc/public_sys-resources/note_3.0-zh-cn.png)
   **domain** 和**appId** 请参考[应用管理](https://support.huaweicloud.com/usermanual-rtc/rtc_04_0001.html)进行获取**。**
   
   
3. 加入房间。具体接口详情请参见[join](https://support.huaweicloud.com/csdk-rtc/rtc_05_0138.html#rtc_05_0138__section152490173372)。
   
   ```
   let option = { userId: userId, userName: userName, signature: signature, ctime: ctime, role: role } 
   async joinRoom() {
      try{
        await client.join(roomId, option)
        console.log('join room success')
      } catch(error){
        console.log('join room fail',error)
      }
   }
   ```
   - **userId**：必选，string\[64\] 类型，本端用户唯一标识。
   
   - **userName**：可选，string\[256\]类型，用户昵称，该昵称为UTF-8编码。
   
   - **signature** ：必选，string\[512\]类型，鉴权签名字串，应用开发者需要向远端服务器获取鉴权签名。
     ![](https://support.huaweicloud.com/csdk-rtc/public_sys-resources/note_3.0-zh-cn.png)
     远端服务器需要您自行部署，具体请参见[接入鉴权](https://support.huaweicloud.com/csdk-rtc/rtc_05_0348.html)。
     
   
   - **ctime**：必选，string类型，签名UTC时间戳，单位秒。
   
   - **role** ：必选，number类型，用户角色，可以标识媒体方向，取值如下：
     - 0：joiner（发布并观看）。
     
     - 2：player（只观看不发布）。
      
   
   - **roomId**：必选，string\[64\]类型，房间ID，房间唯一标识。
   
   
   加入房间成功后，对端会收到"peer-join"事件。
   
   
4. 创建本地流并发布。具体接口详情请参见[createStream](https://support.huaweicloud.com/csdk-rtc/rtc_05_0137.html#rtc_05_0137__section4419114172210)、[initialize](https://support.huaweicloud.com/csdk-rtc/rtc_05_0141.html#rtc_05_0141__section20102957111315)、[addResolution](https://support.huaweicloud.com/csdk-rtc/rtc_05_0141.html#rtc_05_0141__section41992113817)、[publish](https://support.huaweicloud.com/csdk-rtc/rtc_05_0138.html#rtc_05_0138__section254304710387)、[play](https://support.huaweicloud.com/csdk-rtc/rtc_05_0140.html#rtc_05_0140__section19133134173816)。
   
   ```
   let stream = HRTC.createStream({ audio:true,microphoneId:xxx,video:true,cameraId:xxx })
   stream.initialize().then(() => {
     stream.addResolution('90p_1')  //可选，如果要开启双流可以添加另外一个分辨率的视频
     
     stream.play(elementId,{ muted:true })  //播放本地流
     client.publish(stream)
   })
   ```
   - **audio**：可选，boolean类型，指定是否采集主流的音频。默认值为false。
   
   - **video**：可选，boolean类型，指定是否采集主流的视频，主流即摄像头的流。默认值为false。
   
   - **microphoneId**：可选，string类型，在audio为true的时候有效，表示采集音频的源麦克风设备Id。如果不传，系统自动设置默认值。
   
   - **cameraId**：可选，string类型，在video为true的时候有效，表示采集视频的摄像头设备Id。如果不传，系统自动设置默认值。
   
   
   
   
5. 当收到服务器发送的"stream-added"事件通知时，可以订阅远端媒体。具体接口详情请参见[stream-added](https://support.huaweicloud.com/csdk-rtc/rtc_05_0139.html#rtc_05_0139__section1521214715619)、[subscribe](https://support.huaweicloud.com/csdk-rtc/rtc_05_0138.html#rtc_05_0138__section17154151119404)、[getStreamInfo](https://support.huaweicloud.com/csdk-rtc/rtc_05_0140.html#rtc_05_0140__section76841232115019)。
   
   ```
   client.on('stream-added', (event) => {
     const stream = event.stream
     client.subscribe(stream,{ video:true, audio:true })
   })
   ```
   ![](https://support.huaweicloud.com/csdk-rtc/public_sys-resources/note_3.0-zh-cn.png)
   ```
   如果双流场景：
   client.on('stream-added', (event) => {
     const stream = event.stream
     const streamInfo = stream.getStreamInfo()  //获取流的分辨率等信息
     const resolutionIds = streamInfo.videoProfiles.map((profile) => profile.resolutionId)  // App 根据自己的业务场景，选择分辨率
     client.subscribe(stream,{ video:true, audio:true, resolutionIds:resolutionIds })  // 订阅音频以及所选择的分辨率的视频
   })
   ```
   订阅完成之后，本端会收到"stream-subscribed"事件通知，可设置对端窗口，播放对端音视频。具体接口详情请参见[stream-subscribed](https://support.huaweicloud.com/csdk-rtc/rtc_05_0139.html#rtc_05_0139__section7981185544814)、[play](https://support.huaweicloud.com/csdk-rtc/rtc_05_0140.html#rtc_05_0140__section19133134173816)。
   ```
   client.on('stream-subscribed', (event) => {
      const stream = event.stream
      stream.play(elementId, { objectFit: 'contain', muted: true, resolutionId: resolutionId })
   })
   ```
   - **elementId**：HTML \<div\>标签ID。
   
   - 播放选项参数：
     - **objectFit**：可选，string类型，取值包括contain、cover和fill，默认值：主流：cover，辅流：contain。
     
     - **muted**：可选，boolean类型，true表示静音，false表示不静音，默认值为false。
     
     - **resolutionId**: 可选，string类型，指定要播放的分辨率的视频。默认为分辨率最高的视频。
      
   
   
   若是不想观看对端，则可取消订阅对端音视频。具体接口详情请参见[unsubscribe](https://support.huaweicloud.com/csdk-rtc/rtc_05_0138.html#rtc_05_0138__section184711745194010)。
   ```
   client.unsubscribe(stream)
   ```
   
   
6. 当远端离开房间，本端会收到"peer-leave"事件通知，清理远端用户的资源。具体接口详情请参见[peer-leave](https://support.huaweicloud.com/csdk-rtc/rtc_05_0139.html#rtc_05_0139__section13878950153)。
   
   ```
   client.on('peer-leave', (event) => {
      // just do something…
   })
   ```
   **event.userId** **：**对端用户标识，通过监听"peer-leave"事件获得。
   远端用户退出，本端同时会收到"stream-removed"事件通知，可在事件处理函数中，关掉视频窗口。具体接口详情请参见[stream-removed](https://support.huaweicloud.com/csdk-rtc/rtc_05_0139.html#rtc_05_0139__section1317117367612)。
   ```
   client.on('stream-removed', (event) => {
      event.stream.close()
   })
   ```
   通过stream对象调用[close](https://support.huaweicloud.com/csdk-rtc/rtc_05_0140.html#rtc_05_0140__section3329639135110)方法，该方法会移除之前用"play"创建的 video 标签元素并关闭摄像头、麦克风。
   
   
7. 本端离开房间。具体接口详情请参见[leave](https://support.huaweicloud.com/csdk-rtc/rtc_05_0138.html#rtc_05_0138__section19133134173816)。
   
   ```
   client.leave()
   ```
   当音视频通话结束时，调用此接口离开房间。
   至此，音视频通话基本流程可以成功运行。
   
   
 
