123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using Blue;
- using GHZRtc;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// 重新写的
- /// </summary>
- public class GHZRTCManager : SingletonMono<GHZRTCManager>, IController
- {
- /// <summary>
- /// 自身UID
- /// </summary>
- private string uid;
- /// <summary>
- /// 当前频道号
- /// </summary>
- private string channelName;
- public string UID
- {
- get => uid;
- set
- {
- if (value != null)
- this.uid = value;
- }
- }
- public string ChannelName
- {
- get => channelName;
- set
- {
- if (value != null)
- this.channelName = value;
- }
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- Debug.LogError("Init");
- GHZRtcManager.Instance.Init();
- }
- public string RoomID;
- public void JoinTest()
- {
- EndustryLoginUserInfo sendLogin = new EndustryLoginUserInfo()
- {
- account = "cy2",
- password = "1"
- };
- string jsonLogin = JsonMapper.ToJson(sendLogin);
- HttpTool.Instance.PostLogin("https://api-fat1.ghz-tech.com" + HttpActionLang.login, jsonLogin, mes =>
- {
- JObject obj = JObject.Parse(mes);
- if (obj["code"].ToString() == "200")
- {
- if (!string.IsNullOrWhiteSpace(obj["data"].ToString()) && !string.IsNullOrWhiteSpace(obj["data"]["token"].ToString()))
- {
- login.UserInfo.Instance.Token = obj["data"]["token"].ToString();
- JsonData data = new JsonData();
- data["roomId"] = RoomID;
- StartCoroutine(HttpTool.Instance.SendHttp("https://api-fat1.ghz-tech.com" + HttpActionLang.rtc_JoinRoom, data.ToJson(), message =>
- {
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
- {
- Debug.LogError($"加入房间:{jobject["data"].ToString()}");
- RTCRoomInfo mRTCRoomInfo = JsonConvert.DeserializeObject<RTCRoomInfo>(jobject["data"].ToString());
- Debug.LogError($"加入房间成功,房间ID:{mRTCRoomInfo.roomId}");
- Debug.LogError($"地址链接:https://{mRTCRoomInfo.host}");
- Debug.LogError($"Token:{mRTCRoomInfo.token}");
- GHZRtcManager.Instance.ConnectRoom(mRTCRoomInfo.host, mRTCRoomInfo.token);
- }
- }));
- }
- }
- });
- }
- /// <summary>
- /// 进入房间
- /// </summary>
- public void JoinChannel(string url, string token)
- {
- GHZRtcManager.Instance.ConnectRoom(url, token, false);
- }
- /// <summary>
- /// 退出房间
- /// </summary>
- public void LeaveChannel()
- {
- GHZRtcManager.Instance.DisconnectRoom();
- }
- /// <summary>
- /// 显示用户画面
- /// </summary>
- /// <param name="uid"> 用户的UID</param>
- /// <param name="rawImage"> 需要显示的RawImage</param>
- public void ShowViewRawImage(string uid, RawImage rawImage)
- {
- foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
- {
- if (i.sid == uid)
- {
- rawImage.texture = GHZRtcManager.Instance.remoteParticipantViews[i].VideoView;
- }
- }
- // var localVideoViewARF = (AspectRatioFitter)localVideoView.GetComponent<AspectRatioFitter>();
- // localVideoViewARF.aspectRatio = (float)videoTrack.Texture.width / videoTrack.Texture.height;
- }
- /// <summary>
- /// 显示用户画面
- /// </summary>
- /// <param name="uid"> 用户的UID</param>
- /// <param name="mesh"> 需要显示的Mesh</param>
- public void ShowViewMeshRenderer(string uid, MeshRenderer mesh)
- {
- foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
- {
- if (i.sid == uid)
- {
- mesh.material.mainTexture = GHZRtcManager.Instance.remoteParticipantViews[i].VideoView;
- }
- }
- }
- /// <summary>
- /// 开关自身音频
- /// </summary>
- /// <param name="isAudio"></param>
- public void MuteLocalAudioStream(bool isAudio)
- {
- GHZRtcManager.Instance.OnMicrophone(isAudio);
- }
- /// <summary>
- /// 开关自身视频
- /// </summary>
- /// <param name="isVideo"></param>
- public void MuteLocalVideoStream(bool isVideo)
- {
- GHZRtcManager.Instance.OnWebCam(isVideo);
- }
- /// <summary>
- /// 订阅/取订 用户音频
- /// </summary>
- /// <param name="uid"></param>
- /// <param name="isAudio"></param>
- public void MuteRemoteAudioStream(string uid, bool isAudio)
- {
- foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
- {
- if (i.sid == uid)
- {
- if (i.audioTracks.Count > 0)
- {
- for (int j = 0; j < i.audioTracks.Count; j++)
- {
- string sid = i.audioTracks[j].sid;
- i.Room.engine.signalClient.SendMuteTrack(sid, !isAudio);
- }
- }
- }
- }
- }
- /// <summary>
- /// 订阅/取订 用户视频
- /// </summary>
- /// <param name="uid"></param>
- /// <param name="isVideo"></param>
- public void MuteRemoteVideoStream(string uid, bool isVideo)
- {
- foreach (var i in GHZRtcManager.Instance.remoteParticipantViews.Keys)
- {
- if (i.sid == uid)
- {
- if (i.videoTracks.Count > 0)
- {
- for (int j = 0; j < i.videoTracks.Count; j++)
- {
- string sid = i.videoTracks[j].sid;
- i.Room.engine.signalClient.SendMuteTrack(sid, !isVideo);
- }
- }
- }
- }
- }
- public delegate void OnRemoteAudioStateChangedEvent(string uid, REMOTE_AUIDO_STATE_REASON_RTC state);
- public static OnRemoteAudioStateChangedEvent OnRemoteAudioStateChanged;
- /// <summary>
- /// 用户音频状态发生变化的回调
- /// </summary>
- /// <param name="uid"></param>
- /// <param name="state"> 音频状态 </param>
- public void RemoteAudioStateChanged(string uid, REMOTE_AUIDO_STATE_REASON_RTC state)
- {
- Debug.Log("uid=RemoteAudioStateChanged=>" + state);
- OnRemoteAudioStateChanged?.Invoke(uid, state);
- }
- public delegate void OnRemoteVideoStateChangedEvent(string uid, REMOTE_VIDEO_STATE_REASON_RTC state);
- public static OnRemoteVideoStateChangedEvent OnRemoteVideoStateChanged;
- /// <summary>
- /// 用户视频状态发生变化的回调
- /// </summary>
- /// <param name="uid"></param>
- /// <param name="state"> 视频状态 </param>
- public void RemoteVideoStateChanged(string uid, REMOTE_VIDEO_STATE_REASON_RTC state)
- {
- Debug.Log("uid=RemoteVideoStateChanged=>" + state);
- OnRemoteVideoStateChanged?.Invoke(uid, state);
- }
- public delegate void OnUserJoinedEvent(string uid);
- public static OnUserJoinedEvent OnUserJoined;
- /// <summary>
- /// 有用户进入频道的回调
- /// </summary>
- /// <param name="uid"></param>
- public void UserJoined(string uid)
- {
- Debug.Log("uid=UserJoined=>" + uid);
- OnUserJoined?.Invoke(uid);
- }
- public delegate void OnUserOfflineEvent(string uid);
- public static OnUserOfflineEvent OnUserOffline;
- /// <summary>
- /// 有用户退出频道的回调
- /// </summary>
- /// <param name="uid"></param>
- public void UserOffline(string uid)
- {
- Debug.Log("uid=UserOffline=>" + uid);
- OnUserOffline?.Invoke(uid);
- }
- }
|