This commit is contained in:
2026-05-24 14:39:45 +08:00
parent de23d47e5f
commit 81834ebe09
175 changed files with 439 additions and 56 deletions

View File

@@ -37,12 +37,12 @@ namespace Unity.RenderStreaming
IAddReceiverHandler, IParticipantJoinedHandler, IParticipantLeftHandler,
ICallRequestHandler, IMessageHandler
{
[SerializeField] private List<Component> streams = new List<Component>();
[SerializeField] private List<Component> streams = new();
/// <summary>
/// 当前连接的所有Participant的participantId列表
/// </summary>
private List<string> participantIds = new List<string>();
private List<string> participantIds = new();
/// <summary>
/// 房间的connectionIdHost创建连接时使用的ID
@@ -51,15 +51,16 @@ namespace Unity.RenderStreaming
public string RoomConnectionId
{
get { return roomConnectionId; }
set { roomConnectionId = value; }
get => roomConnectionId;
set => roomConnectionId = value;
}
/// <summary>
/// 每个Participant的独立Receiverkey=participantId
/// VideoStreamReceiver/AudioStreamReceiver内部只持有一个transceiver
/// 必须为每个Participant创建独立的实例
/// </summary>
private Dictionary<string, ParticipantStreams> m_participantStreams = new Dictionary<string, ParticipantStreams>();
private Dictionary<string, ParticipantStreams> m_participantStreams = new();
/// <summary>
/// Participant连接成功事件提供该Participant的Receiver引用
@@ -104,10 +105,7 @@ namespace Unity.RenderStreaming
// 清理所有Participant连接
// 注意:不能在这里调用 base.DeleteConnection(participantId)否则会给每个Participant发送disconnect
// 导致服务器返回多次通知。应该只发一次disconnect关闭房间Participant的清理由服务器通知触发。
foreach (var participantId in participantIds.ToList())
{
DisconnectParticipant(participantId);
}
foreach (var participantId in participantIds.ToList()) DisconnectParticipant(participantId);
participantIds.Clear();
if (connectionId == roomConnectionId)
@@ -143,10 +141,7 @@ namespace Unity.RenderStreaming
}
else
{
foreach (var sender in streams.OfType<IStreamSender>())
{
RemoveSender(connectionId, sender);
}
foreach (var sender in streams.OfType<IStreamSender>()) RemoveSender(connectionId, sender);
}
}
@@ -170,14 +165,12 @@ namespace Unity.RenderStreaming
RemoveReceiver(participantId, ps.audioReceiver);
// 移除DataChannel
foreach (var channel in streams.OfType<IDataChannel>().Where(c => c.ConnectionId == participantId))
{
RemoveChannel(participantId, channel);
}
// 销毁Receiver GameObject
if (ps.gameObject != null)
{
this.streams.Remove(ps.audioReceiver);
this.streams.Remove(ps.videoReceiver);
streams.Remove(ps.audioReceiver);
streams.Remove(ps.videoReceiver);
Destroy(ps.gameObject);
}
@@ -186,18 +179,10 @@ namespace Unity.RenderStreaming
else
{
// 回退无独立Receiver时走共享streams清理
foreach (var sender in streams.OfType<IStreamSender>())
{
RemoveSender(participantId, sender);
}
foreach (var receiver in streams.OfType<IStreamReceiver>())
{
RemoveReceiver(participantId, receiver);
}
foreach (var sender in streams.OfType<IStreamSender>()) RemoveSender(participantId, sender);
foreach (var receiver in streams.OfType<IStreamReceiver>()) RemoveReceiver(participantId, receiver);
foreach (var channel in streams.OfType<IDataChannel>().Where(c => c.ConnectionId == participantId))
{
RemoveChannel(participantId, channel);
}
}
OnParticipantDisconnected?.Invoke(participantId);
@@ -210,7 +195,7 @@ namespace Unity.RenderStreaming
/// </summary>
public void OnOffer(SignalingEventData data)
{
bool isNewParticipant = !participantIds.Contains(data.connectionId);
var isNewParticipant = !participantIds.Contains(data.connectionId);
if (isNewParticipant)
{
@@ -224,9 +209,9 @@ namespace Unity.RenderStreaming
ps.videoReceiver = go.AddComponent<VideoStreamReceiver>();
ps.videoReceiver.renderMode = VideoRenderMode.APIOnly;
this.streams.Add(ps.videoReceiver);
streams.Add(ps.videoReceiver);
ps.audioReceiver = go.AddComponent<AudioStreamReceiver>();
this.streams.Add(ps.audioReceiver);
streams.Add(ps.audioReceiver);
var audioSource = go.AddComponent<AudioSource>();
audioSource.loop = true;
ps.audioReceiver.targetAudioSource = audioSource;
@@ -237,14 +222,9 @@ namespace Unity.RenderStreaming
// 在 SetRemoteDescription 之前添加 Sender 和 Channel
// 这样 transceiver 会正确匹配 offer 中的媒体行
foreach (var source in streams.OfType<IStreamSender>())
{
AddSender(data.connectionId, source);
}
foreach (var source in streams.OfType<IStreamSender>()) AddSender(data.connectionId, source);
foreach (var channel in streams.OfType<IDataChannel>().Where(c => c.IsLocal))
{
AddChannel(data.connectionId, channel);
}
// 不再手动调用 SendAnswer
// SignalingManagerInternal.OnOffer 会在 OnGotDescription 完成后自动调用 SendAnswer
@@ -285,9 +265,30 @@ namespace Unity.RenderStreaming
/// </summary>
public void OnAddChannel(SignalingEventData data)
{
var channel = streams.OfType<IDataChannel>().
FirstOrDefault(r => !r.IsConnected && !r.IsLocal);
channel?.SetChannel(data.connectionId, data.channel);
if (data.channel == null)
{
Debug.LogWarning(
$"[HostConnection] OnAddChannel received null channel for connectionId: {data.connectionId}");
return;
}
var channelLabel = data.channel.Label;
var channel = streams.OfType<IDataChannel>()
.FirstOrDefault(r => !r.IsConnected && !r.IsLocal && r.Label == channelLabel);
if (channel != null)
{
channel.SetChannel(data.connectionId, data.channel);
Debug.Log(
$"[HostConnection] DataChannel assigned: label={channelLabel}, connectionId={data.connectionId}");
}
else
{
Debug.LogWarning(
$"[HostConnection] No matching DataChannel found for label: {channelLabel}, connectionId: {data.connectionId}");
Debug.LogWarning(
$"[HostConnection] Available channels: {string.Join(", ", streams.OfType<IDataChannel>().Select(c => $"{c.Label}(Connected={c.IsConnected}, Local={c.IsLocal})"))}");
}
}
/// <summary>
@@ -297,7 +298,8 @@ namespace Unity.RenderStreaming
/// </summary>
public void OnParticipantJoined(SignalingEventData eventData)
{
Debug.Log($"[HostConnection] Participant joined: connectionId={eventData.connectionId}, participantId={eventData.participantId}");
Debug.Log(
$"[HostConnection] Participant joined: connectionId={eventData.connectionId}, participantId={eventData.participantId}");
}
/// <summary>
@@ -306,10 +308,11 @@ namespace Unity.RenderStreaming
/// </summary>
public void OnParticipantLeft(SignalingEventData eventData)
{
string participantId = eventData.participantId;
var participantId = eventData.participantId;
if (!string.IsNullOrEmpty(participantId))
{
Debug.Log($"[HostConnection] Participant left: connectionId={eventData.connectionId}, participantId={participantId}");
Debug.Log(
$"[HostConnection] Participant left: connectionId={eventData.connectionId}, participantId={participantId}");
DisconnectParticipant(participantId);
participantIds.Remove(participantId);
}
@@ -328,17 +331,13 @@ namespace Unity.RenderStreaming
/// </summary>
public void OnMessage(SignalingEventData eventData)
{
// eventData.
Debug.Log($"[HostConnection] Message from: {eventData.connectionId}, participantId: {eventData.participantId}, message: {eventData.message}");
foreach (var channel in streams.OfType<DataChannelBase>().Where(c => c.Label=="on-message"))
{
if (channel != null)
{
channel.OnMessage(eventData.message);
}
}
// eventData.
Debug.Log(
$"[HostConnection] Message from: {eventData.connectionId}, participantId: {eventData.participantId}, message: {eventData.message}");
foreach (var channel in streams.OfType<DataChannelBase>().Where(c => c.Label == "on-message"))
if (channel != null)
channel.OnMessage(eventData.message);
}
/// <summary>
@@ -359,7 +358,7 @@ namespace Unity.RenderStreaming
return m_participantStreams.TryGetValue(participantId, out ps);
}
IStreamReceiver GetReceiver(TrackKind kind)
private IStreamReceiver GetReceiver(TrackKind kind)
{
if (kind == TrackKind.Audio)
return streams.OfType<AudioStreamReceiver>().FirstOrDefault();
@@ -372,11 +371,9 @@ namespace Unity.RenderStreaming
{
// 清理所有Participant的Receiver GameObject
foreach (var ps in m_participantStreams.Values)
{
if (ps.gameObject != null)
Destroy(ps.gameObject);
}
m_participantStreams.Clear();
}
}
}
}