
# SDK使用
1. 创建引擎。 
   **AppId** 获取方法请参见[创建应用](https://support.huaweicloud.com/usermanual-rtc/rtc_04_0001.html)。
   ```
   HRTCEngineConfig config = new HRTCEngineConfig();
   config.setAppId(appId); // AppId需在控制台中创建应用后获取
   config.setCountryCode(countryCode); // 可以根据Grs国家码对照表传值，建议传"CN"
   config.setContext(getApplicationContext()); // 上下文，请传入Application Context
   config.setDomain(webSocketFalvor); // 该字段已废弃，不需要再传值
   config.setMuteAudioRoute(isMuteAudioRoute);
   config.setLogEnable(true);
   config.setLogSize(logSize);
   config.setLogLevel(logLevel);
   config.setLogPath(logPath); // logPath为目录，非文件
   mHwRtcEngine = HRTCEngine.create(config, mHwHandler); // mHwHandler继承自IHRTCEngineEventHandler，用于监听各种回调事件
   ```
   设置本地窗口。
   ```
   SurfaceView surface = mHwRtcEngine.createRenderer(getApplicationContext()); // 不可使用new SurfaceView(context)创建
       mHwRtcEngine.setupLocalView(surface, HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_HIDDEN);
   ```
   加入房间。
   ```
   HRTCJoinParam joinParam = new HRTCJoinParam();
   joinParam.setUserId(mUserId); // userId用于标识同一房间的不同用户
   joinParam.setUserName(mUserName); // 用户昵称，如无特殊需求，保持和userId一致即可
   joinParam.setRole( HRTCJoinParam.HRTCRoleType.HRTC_ROLE_TYPE_JOINER);
   joinParam.setRoomId(roomid);
   joinParam.setScenario(1);
   joinParam.setOptionalInfo(optionInfo);
   joinParam.setAuthorization(signature);  
   joinParam.setCtime(ctime);
   joinParam.setAutoSubscribeVideo(false);
   joinParam.setAutoSubscribeAudio(true);
   mHwRtcEngine.joinRoom(joinParam );
   ```
   joinParam：入会参数，包含用户ID、用户名、房间号、认证信息、ctime、是否自动订阅音频和视频、SFU类型、场景和用户角色，具体请参见[HRTCJoinParam](https://support.huaweicloud.com/csdk-rtc/rtc_05_0037.html#rtc_05_0037__section1565345782011)。
   
   
2. 监听远端用户加入房间，并设置远端窗口。 
   ```
   @Override
   public void onRemoteUserOnline(String roomId, final String userId, String nickname) {
       if (userId.equals(mUserId)||mRole == HRTC_ROLE_TYPE_JOINER.ordinal()) {
           return;
       }
       Log.e(TAG, "onRemoteUserOnline userId: " + userId);
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               Toast.makeText(LiveActivity.this, userId + "加入了房间", Toast.LENGTH_SHORT).show();
               SurfaceView surface = mHwRtcEngine.createRenderer(getApplicationContext());
               mHwRtcEngine.startRemoteStreamView(userId,surface,  HRTCEnums.HRTCStreamType.HRTC_STREAM_TYPE_HD);
               mHwRtcEngine.updateRemoteRenderMode(userId,HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_Fit);
               // 将userId对应surface添加到布局中
           }  
       });
   }
   ```
   
   
3. 监听远端用户离开房间，并删除远端窗口。 
   ```
   @Override
   public void onRemoteUserOffline(String roomId, final String userId, int reason) {
       Log.i(TAG, "HwRtcDemo onRemoteUserOffline roomId:" + roomId + ", userId:" + userId + ", reason:" + reason);
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               Log.e(TAG, "HwRtcDemo run removeRemoteUser! ");
               Toast.makeText(LiveActivity.this, userId + "离开了房间", Toast.LENGTH_SHORT).show();
               mHwRtcEngine.stopRemoteStreamView(userId);
               // 将userId对应的surface从布局中移除
           }
       });
   }
   ```
   
   
4. 离开房间。 
   ```
   mHwRtcEngine.leaveRoom();
   finish();
   ```
   
   
5. 销毁引擎 
   ```
   HRTCEngine.destroy();
   ```
   
   
 
