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(); } /// /// 用户加入频道回调 /// /// /// /// 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(); aggregator.GetEvent().Publish(new Models.User { Id = remoteUid, UserName = remoteUid.ToString(), IsManager = true, IsLocal = false }); } /// /// 用户离开频道回调 /// /// /// /// public override void OnUserOffline(RtcConnection connection, uint remoteUid, USER_OFFLINE_REASON_TYPE reason) { //var aggregator = ContainerLocator.Container.Resolve(); aggregator.GetEvent().Publish(new Models.User { Id = remoteUid, UserName = remoteUid.ToString(), IsManager = true, IsLocal = false }); } /// /// 音频音量提示回调 /// /// /// /// /// 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().Publish(sp); }); } /// /// 本地用户音频发布状态改变回调。 /// /// /// /// /// 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().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().Publish(new AudioMuteInfo(AgoraHelper._localUId, false, channel)); break; } } ///// ///// 远端用户音频订阅状态发生改变回调。 ///// ///// ///// ///// ///// ///// //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().Publish(new AudioMuteInfo(uid, true, channel)); // break; // case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBING: // break; // case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBED: // aggregator.GetEvent().Publish(new AudioMuteInfo(uid, false, channel)); // break; // } //} /// /// 远端用户音频订阅状态发生改变回调。 /// /// /// /// public override void OnUserMuteAudio(RtcConnection connection, uint remoteUid, bool muted) { aggregator.GetEvent().Publish(new AudioMuteInfo(remoteUid, muted, connection.channelId)); } /// /// 远端用户视频订阅状态发生改变回调。 /// /// /// /// public override void OnUserMuteVideo(RtcConnection connection, uint remoteUid, bool muted) { aggregator.GetEvent().Publish(new VideoMuteInfo(remoteUid, muted, connection.channelId)); } /// /// 本地用户视频发布状态改变回调。 /// /// /// /// /// /// 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().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().Publish(new VideoMuteInfo(AgoraHelper._localUId, false, channel)); break; } } /// /// 远端用户视频订阅状态发生改变回调。 /// /// /// /// /// /// 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().Publish(new VideoMuteInfo(uid, true, channel)); break; case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBING: break; case STREAM_SUBSCRIBE_STATE.SUB_STATE_SUBSCRIBED: aggregator.GetEvent().Publish(new VideoMuteInfo(uid, false, channel)); break; } } } }