123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System.Collections.Generic;
- using Blue;
- using Newtonsoft.Json;
- using UnityEngine;
- public class RTCService : IRTCService
- {
- private string mToken;
- private RTCRoomInfo mRTCRoomInfo;
- private List<RTCUserInfo> mUserList =new List<RTCUserInfo>();
- private Dictionary<int, RTCUserInfo> mUserDic = new Dictionary<int, RTCUserInfo>();
- public void OnInit()
- {
- // 获取 token
- this.RegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
- this.RegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
- // 创建房间
- this.RegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
- this.RegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
- // 连接RTC
- this.RegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- this.RegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
- // 加入房间
- this.RegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
- this.RegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
- }
- /// <summary>
- /// 关闭RTC
- /// </summary>
- public void CloseRTC()
- {
- }
- /// <summary>
- /// 登录后获取账号Token
- /// </summary>
- public void GetToken()
- {
- mToken = "...token...";
- this.SendEvent(new RTCGetTokenSuccessEvent() { token = mToken });
- //this.SendEvent<RTCGetTokenFailEvent>();
- }
- /// <summary>
- /// 根据Token创建房间
- /// </summary>
- public void CreatRoom()
- {
- var mRTCUrl = "...RTCURL...";
- RTCUserInfo RTCUserInfo = new RTCUserInfo()
- {
- UserID = 123,UserName="我是123",Head=321,Audio = false,Video = false
- };
- mUserDic.Add(RTCUserInfo.UserID,RTCUserInfo);
- mUserList.Add(RTCUserInfo);
- mRTCRoomInfo = new RTCRoomInfo()
- {
- RoomID=111111,RoomName="我是111111",UserList = mUserList
- };
- string currentRoomInfo = JsonConvert.SerializeObject(mRTCRoomInfo);
- this.SendEvent(new RTCCreatRoomSuccessEvent() { RTCUrl = mRTCUrl, roomInfo = currentRoomInfo });
- //this.SendEvent<RTCCreatRoomFailEvent>();
- }
- /// <summary>
- /// 根据url连接rtc
- /// </summary>
- public void ConnectByRTCUrl(string URL)
- {
- this.SendEvent<RTCConnectSuccessEvent>();
- //this.SendEvent<RTCConnectFailEvent>();
- }
- /// <summary>s
- /// 加入房间
- /// </summary>
- public void JoinRoom(int roomId)
- {
- this.SendEvent<JoinRoomSuccessEvent>();
- //this.SendEvent<JoinRoomFailEvent>();
- }
- /// <summary>
- /// 退出房间
- /// </summary>
- public void LeaveRoom()
- {
- mUserList.Clear();
- mUserDic.Clear();
- Debug.LogError($"离开房间");
- }
- /// <summary>
- /// 控制摄像头
- /// </summary>
- public void ActiveVideo(bool active)
- {
- Debug.LogError($"ActiveCamera:{active}");
- }
- /// <summary>
- /// 控制麦克风
- /// </summary>
- public void ActiveAudio(bool active)
- {
- Debug.LogError($"ActiveCamera:{active}");
- }
- /// <summary>
- /// 将远程视频流静音
- /// </summary>
- public void MuteRemoteVideoStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
- /// <summary>
- /// 将远程音频流静音
- /// </summary>
- public void MuteRemoteAudioStream(string userID, bool active)
- {
- Debug.LogError($"远程视频流===> 用户:{userID},开关:{active}");
- }
- /// <summary>
- /// 视频静音
- /// </summary>
- public void MuteVideo(bool active)
- {
- Debug.LogError($"视频静音:{active}");
- }
- /// <summary>
- /// 音频静音
- /// </summary>
- public void MuteAudio(bool active)
- {
- Debug.LogError($"音频静音{active}");
- }
- /// <summary>
- /// 打开本地视频
- /// </summary>
- public void LocalVideo(bool active)
- {
- Debug.LogError($"打开本地视频{active}");
- }
- /// <summary>
- /// 打开本地音频
- /// </summary>
- public void LoacalAudio(bool active)
- {
- Debug.LogError($"打开本地音频{active}");
- }
- #region 事件
- // 获取 token
- private void RTCGetTokenSuccess(RTCGetTokenSuccessEvent e)
- {
- Debug.LogError($"获取token成功:{e.token}");
- this.UnRegisterEvent<RTCGetTokenSuccessEvent>(RTCGetTokenSuccess);
- }
- private void RTCGetTokenFail(RTCGetTokenFailEvent e)
- {
- Debug.LogError($"RTC:RTCGetTokenFail");
- this.UnRegisterEvent<RTCGetTokenFailEvent>(RTCGetTokenFail);
- }
- // 创建房间
- private void RTCCreatRoomSuccess(RTCCreatRoomSuccessEvent e)
- {
- Debug.LogError($"创建房间成功:{e.roomInfo}");
- ConnectByRTCUrl(e.RTCUrl);
- this.UnRegisterEvent<RTCCreatRoomSuccessEvent>(RTCCreatRoomSuccess);
- }
- private void RTCCreatRoomFail(RTCCreatRoomFailEvent e)
- {
- Debug.LogError($"RTC:RTCGetTokenFail");
- this.UnRegisterEvent<RTCCreatRoomFailEvent>(RTCCreatRoomFail);
- }
- // 连接RTC
- private void RTCConnectSuccess(RTCConnectSuccessEvent e)
- {
- Debug.LogError($"RTC:RTCConnectSuccess");
- this.UnRegisterEvent<RTCConnectSuccessEvent>(RTCConnectSuccess);
- }
- private void RTCConnectFail(RTCConnectFailEvent e)
- {
- Debug.LogError($"RTC:RTCConnectFail");
- this.UnRegisterEvent<RTCConnectFailEvent>(RTCConnectFail);
- }
- // 加入房间
- private void JoinRoomSuccess(JoinRoomSuccessEvent e)
- {
- mUserList = JsonConvert.DeserializeObject<List<RTCUserInfo>>(e.roomInfo);
- foreach(RTCUserInfo user in mUserList) mUserDic.Add(user.UserID,user);
- Debug.LogError($"RTC:JoinRoomSuccess");
- this.UnRegisterEvent<JoinRoomSuccessEvent>(JoinRoomSuccess);
- }
- private void JoinRoomFail(JoinRoomFailEvent e)
- {
- Debug.LogError($"RTC:JoinRoomFail");
- this.UnRegisterEvent<JoinRoomFailEvent>(JoinRoomFail);
- }
- #endregion
- }
|