336 lines
12 KiB
C#
336 lines
12 KiB
C#
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using RenderStreaming;
|
|
using Script.Util;
|
|
using Stary.Evo;
|
|
using Stary.Evo.UIFarme;
|
|
using Unity.RenderStreaming;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Script
|
|
{
|
|
public class MeetingInfoListController : IController
|
|
{
|
|
private MainPanel _mainPanel;
|
|
public GameObject PanelGo;
|
|
|
|
/// <summary>
|
|
/// 联系人列表项预制体
|
|
/// </summary>
|
|
private GameObject _contactEntryPrefab;
|
|
|
|
/// <summary>
|
|
/// 全部联系人列表容器
|
|
/// </summary>
|
|
private Transform _content;
|
|
|
|
private Text _meetingNum;
|
|
|
|
private Dictionary<string, MessageRecordInfo> _meetingList = new();
|
|
|
|
private GameObjectPool _objectPool;
|
|
|
|
|
|
private Toggle _micTog;
|
|
private Toggle _videoTog;
|
|
private Toggle _recordTog;
|
|
private Button _hangUpTog;
|
|
|
|
private AudioStreamSender _audioStreamSender;
|
|
private VideoStreamSender _videoStreamSender;
|
|
|
|
public void Initialize(GameObject panelGo, MainPanel mainPanel)
|
|
{
|
|
PanelGo = panelGo;
|
|
_mainPanel = mainPanel;
|
|
_contactEntryPrefab = Resources.Load<GameObject>("ParticipantEntry");
|
|
_content = panelGo.transform.Find("MeetingGrid/Viewport/Content");
|
|
_meetingNum = panelGo.transform.Find("MeetingNum/Num").GetComponent<Text>();
|
|
_objectPool = PanelGo.transform.Find("MeetingGrid/Pool").GetComponent<GameObjectPool>();
|
|
_micTog = PanelGo.transform.Find("menuBar/mic").GetComponent<Toggle>();
|
|
_videoTog = PanelGo.transform.Find("menuBar/video").GetComponent<Toggle>();
|
|
_recordTog = PanelGo.transform.Find("menuBar/record").GetComponent<Toggle>();
|
|
_hangUpTog = PanelGo.transform.Find("menuBar/hangUp").GetComponent<Button>();
|
|
_audioStreamSender = GameObject.Find("RenderStreaming").GetComponent<AudioStreamSender>();
|
|
_videoStreamSender = GameObject.Find("RenderStreaming").GetComponent<VideoStreamSender>();
|
|
}
|
|
|
|
public void OnEnter()
|
|
{
|
|
LoadUsers();
|
|
_micTog.onValueChanged.AddListener(OnMicTogValueChanged);
|
|
_videoTog.onValueChanged.AddListener(OnVideoTogValueChanged);
|
|
_recordTog.onValueChanged.AddListener(OnRecordTogValueChanged);
|
|
_hangUpTog.onClick.AddListener(OnHangUpTogValueChanged);
|
|
|
|
GameObject.FindObjectOfType<MessageChannel>().OnMediaStateChangeReceived += OnMediaStateChangeReceived;
|
|
GameObject.FindObjectOfType<MessageChannel>().OnUserInfoMessageReceived += OnUserInfoMessageReceived;
|
|
GameObject.FindObjectOfType<HostConnection>().OnParticipantDisconnected += OnParticipantDisconnected;
|
|
}
|
|
|
|
private void OnMicTogValueChanged(bool value)
|
|
{
|
|
var message = new
|
|
{
|
|
type = "on-message",
|
|
data = new
|
|
{
|
|
connectionId = this.GetSystem<IGlobalConfigSystem>().GetConnectionId(),
|
|
message = new
|
|
{
|
|
type = "media-state-changed",
|
|
data = new
|
|
{
|
|
userId = this.GetSystem<IGlobalConfigSystem>().GetUserId(),
|
|
audio = value,
|
|
video = _videoTog.isOn
|
|
}
|
|
}
|
|
}
|
|
};
|
|
_audioStreamSender.enabled = value;
|
|
SignalingMessageHelper.SendMessage(JsonConvert.SerializeObject(message));
|
|
}
|
|
|
|
private void OnVideoTogValueChanged(bool value)
|
|
{
|
|
var message = new
|
|
{
|
|
type = "on-message",
|
|
data = new
|
|
{
|
|
connectionId = this.GetSystem<IGlobalConfigSystem>().GetConnectionId(),
|
|
message = new
|
|
{
|
|
type = "media-state-changed",
|
|
data = new
|
|
{
|
|
userId = this.GetSystem<IGlobalConfigSystem>().GetUserId(),
|
|
audio = _micTog.isOn,
|
|
video = value
|
|
}
|
|
}
|
|
}
|
|
};
|
|
_videoStreamSender.enabled = value;
|
|
SignalingMessageHelper.SendMessage(JsonConvert.SerializeObject(message));
|
|
}
|
|
|
|
private void OnRecordTogValueChanged(bool value)
|
|
{
|
|
}
|
|
|
|
private void OnHangUpTogValueChanged()
|
|
{
|
|
this.GetSystem<IRenderStreamingSystem>().HangUp();
|
|
this.GetSystem<IRenderStreamingSystem>().SetLocalVideoImage(null);
|
|
this.GetSystem<IPanelSystem>().PopQueue<MainPanel>();
|
|
this.GetSystem<IPanelSystem>().PushQueue<StartPanel>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当有用户断开连接时调用
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
private void OnParticipantDisconnected(string id)
|
|
{
|
|
var userId = "";
|
|
foreach (var value in _meetingList.Values)
|
|
if (value.item.participantId == id)
|
|
{
|
|
_objectPool.Release(value.participant);
|
|
userId = value.item.userId;
|
|
break;
|
|
}
|
|
|
|
_meetingList.Remove(userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当有用户加入会议时调用
|
|
/// </summary>
|
|
/// <param name="arg1"></param>
|
|
/// <param name="arg2"></param>
|
|
private void OnUserInfoMessageReceived(string arg1, UserInfo arg2)
|
|
{
|
|
foreach (var info in _meetingList.Values) _objectPool.Release(info.participant);
|
|
|
|
_meetingList.Clear();
|
|
|
|
LoadUsers();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当有用户媒体状态改变时调用
|
|
/// </summary>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="message"></param>
|
|
private async void OnMediaStateChangeReceived(string connectionId, MediaStateChange message)
|
|
{
|
|
Debug.Log($"OnMediaStateChangeReceived: {message}");
|
|
|
|
|
|
if (_meetingList.ContainsKey(message.userId))
|
|
{
|
|
var info = _meetingList[message.userId];
|
|
SetMediaStateMic(info.participant, message.audio);
|
|
SetMediaStateVideo(info.participant, message.video);
|
|
}
|
|
}
|
|
|
|
private async void LoadUsers()
|
|
{
|
|
var response =
|
|
await WebRequestSystem.Get<MainPanel.ResponseUsers>(this.GetSystem<IGlobalConfigSystem>().IP,
|
|
$"/signaling/users?connectionId={this.GetSystem<IGlobalConfigSystem>().GetConnectionId()}");
|
|
if (response != null && response.totalCount > 0)
|
|
{
|
|
this.GetSystem<IGlobalConfigSystem>().SetUserCount(response.totalCount);
|
|
_mainPanel.OnUsersChangedEvnent?.Invoke(response.users);
|
|
_meetingNum.text = response.totalCount.ToString();
|
|
for (var i = 0; i < response.totalCount; i++)
|
|
{
|
|
var item = response.users[i];
|
|
if (string.IsNullOrEmpty(item.name) ||
|
|
string.IsNullOrEmpty(item.userId)) continue;
|
|
var entry = _objectPool.Get();
|
|
SetData(entry, item);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetData(GameObject entry, MainPanel.UsersItem item)
|
|
{
|
|
var transform = entry.transform;
|
|
///头像赋值
|
|
var background = transform.Find("headBackground/image");
|
|
var backgroundName = transform.Find("headBackground/Name");
|
|
if (string.IsNullOrEmpty(item.avatar))
|
|
{
|
|
var randomColor = WebRTCUtil.GetRandomColor();
|
|
|
|
if (background != null)
|
|
{
|
|
var image = background.GetComponent<Image>();
|
|
if (image != null) image.color = randomColor;
|
|
}
|
|
|
|
|
|
if (backgroundName != null)
|
|
{
|
|
var textComponent = backgroundName.GetComponent<Text>();
|
|
if (textComponent != null && !string.IsNullOrEmpty(item.name))
|
|
textComponent.text = item.name.Substring(0, 1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (background != null)
|
|
{
|
|
var imageComponent = background.GetComponent<Image>();
|
|
if (imageComponent != null) WebRTCUtil.DownloadAndSetAvatar(item.avatar, imageComponent);
|
|
}
|
|
|
|
if (backgroundName != null) backgroundName.GetComponent<Text>().text = "";
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(item.name))
|
|
{
|
|
var nameText = transform.Find("Name");
|
|
if (nameText != null)
|
|
{
|
|
var textComponent = nameText.GetComponent<Text>();
|
|
if (textComponent != null) textComponent.text = item.name;
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(item.userId))
|
|
{
|
|
var messageText = transform.Find("Message");
|
|
if (messageText != null)
|
|
{
|
|
var textComponent = messageText.GetComponent<Text>();
|
|
if (textComponent != null) textComponent.text = item.userId;
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(item.role))
|
|
{
|
|
var role = item.role;
|
|
var participant = transform.Find("Name/state/participant");
|
|
var host = transform.Find("Name/state/host");
|
|
|
|
if (role.Equals("host"))
|
|
{
|
|
host.gameObject.SetActive(true);
|
|
participant.gameObject.SetActive(false);
|
|
SetMediaStateMic(entry, true);
|
|
SetMediaStateVideo(entry, true);
|
|
}
|
|
else if (role.Equals("participant"))
|
|
{
|
|
participant.gameObject.SetActive(true);
|
|
host.gameObject.SetActive(false);
|
|
SetMediaStateMic(entry, false);
|
|
SetMediaStateVideo(entry, true);
|
|
}
|
|
}
|
|
|
|
_meetingList.Add(item.userId, new MessageRecordInfo(item, entry));
|
|
}
|
|
|
|
private void SetMediaStateMic(GameObject entry, bool isAudio)
|
|
{
|
|
var mic = entry.transform.Find("mic");
|
|
var micBackground = mic.transform.Find("Background");
|
|
var micCheckmark = mic.transform.Find("Checkmark");
|
|
|
|
micCheckmark.gameObject.SetActive(isAudio);
|
|
micBackground.gameObject.SetActive(!isAudio);
|
|
}
|
|
|
|
private void SetMediaStateVideo(GameObject entry, bool isVideo)
|
|
{
|
|
var video = entry.transform.Find("video");
|
|
var videoBackground = video.transform.Find("Background");
|
|
var videoCheckmark = video.transform.Find("Checkmark");
|
|
|
|
|
|
videoCheckmark.gameObject.SetActive(isVideo);
|
|
videoBackground.gameObject.SetActive(!isVideo);
|
|
}
|
|
|
|
public async void OnExit()
|
|
{
|
|
GameObject.FindObjectOfType<MessageChannel>().OnMediaStateChangeReceived -= OnMediaStateChangeReceived;
|
|
GameObject.FindObjectOfType<MessageChannel>().OnUserInfoMessageReceived -= OnUserInfoMessageReceived;
|
|
GameObject.FindObjectOfType<HostConnection>().OnParticipantDisconnected -= OnParticipantDisconnected;
|
|
|
|
foreach (var info in _meetingList.Values) _objectPool.Release(info.participant);
|
|
_meetingList.Clear();
|
|
}
|
|
|
|
public void Destroy()
|
|
{
|
|
_objectPool.AllRelease();
|
|
}
|
|
|
|
public class MessageRecordInfo
|
|
{
|
|
public MainPanel.UsersItem item;
|
|
public GameObject participant;
|
|
|
|
public MessageRecordInfo(MainPanel.UsersItem item, GameObject participant)
|
|
{
|
|
this.item = item;
|
|
this.participant = participant;
|
|
}
|
|
}
|
|
|
|
public IArchitecture GetArchitecture()
|
|
{
|
|
return MainArchitecture.Interface;
|
|
}
|
|
}
|
|
} |