using Blue;
using GHZRtc;
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UI;
///
/// 重新写的
///
public class GHZRTCManager : SingletonMono, IController
{
///
/// 自身UID
///
private string uid;
///
/// 当前频道号
///
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;
}
}
///
/// 初始化
///
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(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);
}
}));
}
}
});
}
///
/// 进入房间
///
public void JoinChannel(string url, string token)
{
GHZRtcManager.Instance.ConnectRoom(url, token, false);
}
///
/// 退出房间
///
public void LeaveChannel()
{
GHZRtcManager.Instance.DisconnectRoom();
}
///
/// 显示用户画面
///
/// 用户的UID
/// 需要显示的RawImage
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();
// localVideoViewARF.aspectRatio = (float)videoTrack.Texture.width / videoTrack.Texture.height;
}
///
/// 显示用户画面
///
/// 用户的UID
/// 需要显示的Mesh
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;
}
}
}
///
/// 开关自身音频
///
///
public void MuteLocalAudioStream(bool isAudio)
{
GHZRtcManager.Instance.OnMicrophone(isAudio);
}
///
/// 开关自身视频
///
///
public void MuteLocalVideoStream(bool isVideo)
{
GHZRtcManager.Instance.OnWebCam(isVideo);
}
///
/// 订阅/取订 用户音频
///
///
///
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);
}
}
}
}
}
///
/// 订阅/取订 用户视频
///
///
///
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;
///
/// 用户音频状态发生变化的回调
///
///
/// 音频状态
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;
///
/// 用户视频状态发生变化的回调
///
///
/// 视频状态
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;
///
/// 有用户进入频道的回调
///
///
public void UserJoined(string uid)
{
Debug.Log("uid=UserJoined=>" + uid);
OnUserJoined?.Invoke(uid);
}
public delegate void OnUserOfflineEvent(string uid);
public static OnUserOfflineEvent OnUserOffline;
///
/// 有用户退出频道的回调
///
///
public void UserOffline(string uid)
{
Debug.Log("uid=UserOffline=>" + uid);
OnUserOffline?.Invoke(uid);
}
}