212 lines
8.7 KiB
C#
212 lines
8.7 KiB
C#
using Agora.Rtc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Prism.Ioc;
|
|
using Prism.Events;
|
|
using Demo.Common.Events;
|
|
using Masuit.Tools;
|
|
using System.Security.Cryptography;
|
|
using System.Threading.Channels;
|
|
|
|
namespace Demo.Common.Helpers
|
|
{
|
|
public class RtcEngineEventHandler : IRtcEngineEventHandler
|
|
{
|
|
private readonly IEventAggregator aggregator;
|
|
public RtcEngineEventHandler()
|
|
{
|
|
aggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户加入频道回调
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
/// <param name="remoteUid"></param>
|
|
/// <param name="elapsed"></param>
|
|
public override void OnUserJoined(RtcConnection connection, uint remoteUid, int elapsed)
|
|
{
|
|
//base.OnUserJoined(connection, remoteUid, elapsed);
|
|
Console.WriteLine($@" {DateTime.Now.ToString("HH:mm:ss")}新用户加入: channelId:{connection.channelId} localuid: {connection.localUid} remoteuid:{remoteUid} elapsed: {elapsed}");
|
|
|
|
|
|
//var aggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
|
aggregator.GetEvent<UserJoinEvent>().Publish(new Models.User
|
|
{
|
|
Id = remoteUid,
|
|
UserName = remoteUid.ToString(),
|
|
IsManager = true,
|
|
IsLocal = false
|
|
});
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 用户离开频道回调
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
/// <param name="remoteUid"></param>
|
|
/// <param name="reason"></param>
|
|
public override void OnUserOffline(RtcConnection connection, uint remoteUid, USER_OFFLINE_REASON_TYPE reason)
|
|
{
|
|
//var aggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
|
aggregator.GetEvent<UserLeaveEvent>().Publish(new Models.User
|
|
{
|
|
Id = remoteUid,
|
|
UserName = remoteUid.ToString(),
|
|
IsManager = true,
|
|
IsLocal = false
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 音频音量提示回调
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
/// <param name="speakers"></param>
|
|
/// <param name="speakerNumber"></param>
|
|
/// <param name="totalVolume"></param>
|
|
public override void OnAudioVolumeIndication(RtcConnection connection, AudioVolumeInfo[] speakers, uint speakerNumber, int totalVolume)
|
|
{
|
|
if (speakers.Length <= 0)
|
|
{
|
|
return;
|
|
}
|
|
speakers.ForEach(sp =>
|
|
{
|
|
sp.uid = sp.uid == 0 ? AgoraHelper._localUId : sp.uid;
|
|
aggregator.GetEvent<AudioVolumeIndicationEvent>().Publish(sp);
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地用户音频发布状态改变回调。
|
|
/// </summary>
|
|
/// <param name="channel"></param>
|
|
/// <param name="oldState"></param>
|
|
/// <param name="newState"></param>
|
|
/// <param name="elapseSinceLastState"></param>
|
|
public override void OnAudioPublishStateChanged(string channel, STREAM_PUBLISH_STATE oldState, STREAM_PUBLISH_STATE newState, int elapseSinceLastState)
|
|
{
|
|
switch (newState)
|
|
{
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_IDLE:
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_NO_PUBLISHED:
|
|
aggregator.GetEvent<AudioMuteStateChangedEvent>().Publish(new AudioMuteInfo(AgoraHelper._localUId, true, channel));
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_PUBLISHING:
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_PUBLISHED:
|
|
aggregator.GetEvent<AudioMuteStateChangedEvent>().Publish(new AudioMuteInfo(AgoraHelper._localUId, false, channel));
|
|
break;
|
|
}
|
|
}
|
|
|
|
///// <summary>
|
|
///// 远端用户音频订阅状态发生改变回调。
|
|
///// </summary>
|
|
///// <param name="channel"></param>
|
|
///// <param name="uid"></param>
|
|
///// <param name="oldState"></param>
|
|
///// <param name="newState"></param>
|
|
///// <param name="elapseSinceLastState"></param>
|
|
//public override void OnAudioSubscribeStateChanged(string channel, uint uid, STREAM_SUBSCRIBE_STATE oldState, STREAM_SUBSCRIBE_STATE newState, int elapseSinceLastState)
|
|
//{
|
|
// //Console.WriteLine($@"OnAudioSubscribeStateChanged====================== channel {channel} uid {uid} {newState}");
|
|
|
|
// switch (newState)
|
|
// {
|
|
// case STREAM_SUBSCRIBE_STATE.SUB_STATE_IDLE:
|
|
// break;
|
|
// case STREAM_SUBSCRIBE_STATE.SUB_STATE_NO_SUBSCRIBED:
|
|
// aggregator.GetEvent<AudioMuteStateChangedEvent>().Publish(new AudioMuteInfo(uid, true, channel));
|
|
// break;
|
|
// case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBING:
|
|
// break;
|
|
// case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBED:
|
|
// aggregator.GetEvent<AudioMuteStateChangedEvent>().Publish(new AudioMuteInfo(uid, false, channel));
|
|
// break;
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 远端用户音频订阅状态发生改变回调。
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
/// <param name="remoteUid"></param>
|
|
/// <param name="muted"></param>
|
|
public override void OnUserMuteAudio(RtcConnection connection, uint remoteUid, bool muted)
|
|
{
|
|
aggregator.GetEvent<AudioMuteStateChangedEvent>().Publish(new AudioMuteInfo(remoteUid, muted, connection.channelId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 远端用户视频订阅状态发生改变回调。
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
/// <param name="remoteUid"></param>
|
|
/// <param name="muted"></param>
|
|
public override void OnUserMuteVideo(RtcConnection connection, uint remoteUid, bool muted)
|
|
{
|
|
aggregator.GetEvent<VideoMuteStateChangedEvent>().Publish(new VideoMuteInfo(remoteUid, muted, connection.channelId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地用户视频发布状态改变回调。
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="channel"></param>
|
|
/// <param name="oldState"></param>
|
|
/// <param name="newState"></param>
|
|
/// <param name="elapseSinceLastState"></param>
|
|
public override void OnVideoPublishStateChanged(VIDEO_SOURCE_TYPE source, string channel, STREAM_PUBLISH_STATE oldState, STREAM_PUBLISH_STATE newState, int elapseSinceLastState)
|
|
{
|
|
switch (newState)
|
|
{
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_IDLE:
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_NO_PUBLISHED:
|
|
aggregator.GetEvent<VideoMuteStateChangedEvent>().Publish(new VideoMuteInfo(AgoraHelper._localUId, true, channel));
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_PUBLISHING:
|
|
break;
|
|
case STREAM_PUBLISH_STATE.PUB_STATE_PUBLISHED:
|
|
aggregator.GetEvent<VideoMuteStateChangedEvent>().Publish(new VideoMuteInfo(AgoraHelper._localUId, false, channel));
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 远端用户视频订阅状态发生改变回调。
|
|
/// </summary>
|
|
/// <param name="channel"></param>
|
|
/// <param name="uid"></param>
|
|
/// <param name="oldState"></param>
|
|
/// <param name="newState"></param>
|
|
/// <param name="elapseSinceLastState"></param>
|
|
public override void OnVideoSubscribeStateChanged(string channel, uint uid, STREAM_SUBSCRIBE_STATE oldState, STREAM_SUBSCRIBE_STATE newState, int elapseSinceLastState)
|
|
{
|
|
switch (newState)
|
|
{
|
|
case STREAM_SUBSCRIBE_STATE.SUB_STATE_IDLE:
|
|
break;
|
|
case STREAM_SUBSCRIBE_STATE.SUB_STATE_NO_SUBSCRIBED:
|
|
aggregator.GetEvent<VideoMuteStateChangedEvent>().Publish(new VideoMuteInfo(uid, true, channel));
|
|
break;
|
|
case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBING:
|
|
break;
|
|
case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBED:
|
|
aggregator.GetEvent<VideoMuteStateChangedEvent>().Publish(new VideoMuteInfo(uid, false, channel));
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|