123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
-
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using fis.media.Library.Players;
- using ManageLiteAV;
- namespace fis.media.ThirdPartLibrary.Tencent
- {
- /// <summary>
- /// TRTC SDK 实现
- /// </summary>
- public class TRTCChatRoom : ITRTCCloudCallback
- {
- private static TRTCChatRoom? _instance;
- public static TRTCChatRoom Instance => _instance ?? (_instance = new TRTCChatRoom());
- private ITRTCScreenCaptureSourceList? _mScreenList;
- private string _localId;
- private int _roomId;
- private bool _chatUserEnterRoomSuccess = false;
- private const int JoinAndLeaveRoomTimeout = 8000;
- AutoResetEvent _joinRoomWaitor = new AutoResetEvent(false);
- AutoResetEvent _leafRoomWaitor = new AutoResetEvent(false);
- ITRTCCloud? _tRTCCloud;
- private TRTCVideoFrame? _videoFrame;
- private LocalVideoRenderCallback _localVideoRenderCallback;
- private RemoteVideoRenderCallback _remoteVideoRenderCallback;
- /// <summary>
- /// 本地摄像头数据
- /// </summary>
- public event EventHandler<VideoFrameData>? LocalVideoFrameArrived;
- /// <summary>
- /// 远端摄像头数据
- /// </summary>
- public event EventHandler<RemoteVideoFrameData>? RemoteVideoFrameArrived;
- /// <summary>
- /// 本地屏幕分享数据
- /// </summary>
- public event EventHandler<VideoFrameData>? LocalVideoScreenFrameArrived;
- /// <summary>
- /// 远端屏幕分享数据
- /// </summary>
- public event EventHandler<RemoteVideoFrameData>? RemoteVideoScreenFrameArrived;
- /// <summary>
- /// 远端用户离开房间
- /// </summary>
- public event Action<string, int>? RemoteUserLeaveRoomNotification;
- public event Action<string>? RemoteUserEnterRoomNotification;
- /// <summary>
- /// SDK 跟服务器的连接恢复
- /// </summary>
- public event Action? TryToReconnectNotification;
- /// <summary>
- /// SDK 尝试重新连接到服务器
- /// </summary>
- public event Action? ConnectionRecoveryNotification;
- /// <summary>
- /// SDK 跟服务器的连接断开
- /// </summary>
- public event Action? ConnectionLostNotification;
- private TRTCChatRoom()
- {
- _localVideoRenderCallback = new LocalVideoRenderCallback(OnLocalVideoFrameArrived);
- _remoteVideoRenderCallback = new RemoteVideoRenderCallback(OnRemoteVideoFrameArrived);
- }
- /// <summary>
- /// 视频参数设置
- /// </summary>
- /// <returns></returns>
- private TRTCVideoEncParam GetVideoEncParam()
- {
- TRTCVideoEncParam tRTCVideoEncParam = new TRTCVideoEncParam();
- tRTCVideoEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_640_480;
- tRTCVideoEncParam.videoFps = 15;
- tRTCVideoEncParam.videoBitrate = 600;
- return tRTCVideoEncParam;
- }
- /// <summary>
- /// 进入房间
- /// </summary>
- /// <param name="accountId"></param>
- /// <param name="integerRoomId"></param>
- /// <param name="isVideo"></param>
- /// <param name="userSign"></param>
- /// <param name="appId"></param>
- public void Enter(string accountId, int integerRoomId, bool isVideo, string userSign, int appId)
- {
- if (_roomId <= 0 || _tRTCCloud == null)
- {
- _roomId = integerRoomId;
- _localId = accountId;
- _tRTCCloud = ITRTCCloud.getTRTCShareInstance();
- SIZE thumbSize = new SIZE() { cx = 120, cy = 70 };
- SIZE iconSize = new SIZE() { cx = 20, cy = 20 };
- _mScreenList = _tRTCCloud.getScreenCaptureSources(ref thumbSize, ref iconSize);
- //设置log路径
- var tRTCRenderParams = new TRTCRenderParams() { rotation = TRTCVideoRotation.TRTCVideoRotation270 };
- _tRTCCloud.setLocalRenderParams(ref tRTCRenderParams);
- var videoParam = GetVideoEncParam();
- _tRTCCloud.setVideoEncoderParam(ref videoParam);
- TRTCParams trtcParams = new TRTCParams();
- trtcParams.sdkAppId = (uint)appId;
- trtcParams.userSig = userSign;
- trtcParams.privateMapKey = "";
- trtcParams.businessInfo = "";
- _tRTCCloud.addCallback(this);
- _tRTCCloud.enterRoom(ref trtcParams, TRTCAppScene.TRTCAppSceneVideoCall);
- if (isVideo)
- {
- _tRTCCloud.startLocalPreview(IntPtr.Zero);
- var retValue = _tRTCCloud.setLocalVideoRenderCallback(TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, _localVideoRenderCallback);
- if (retValue == 0)
- {
- //设置本地视频自定义渲染成功
- }
- else
- {
- //设置本地视频自定义渲染失败
- }
- var localVideoRenderParams = new TRTCRenderParams()
- {
- fillMode = TRTCVideoFillMode.TRTCVideoFillMode_Fit
- };
- _tRTCCloud.setLocalRenderParams(ref localVideoRenderParams);
- }
- _tRTCCloud.startLocalAudio(TRTCAudioQuality.TRTCAudioQualitySpeech);
- if (!_joinRoomWaitor.WaitOne(JoinAndLeaveRoomTimeout))
- {
- //RtcChatRoom Joint Room timeout
- }
- }
- else
- {
- if (isVideo)
- {
- _tRTCCloud.startLocalPreview(IntPtr.Zero);
- var retValue = _tRTCCloud.setLocalVideoRenderCallback(TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, _localVideoRenderCallback);
- if (retValue == 0)
- {
- //设置本地视频自定义渲染成功
- }
- else
- {
- //设置本地视频自定义渲染失败
- }
- }
- _tRTCCloud.startLocalAudio(TRTCAudioQuality.TRTCAudioQualitySpeech);
- }
- }
- /// <summary>
- /// 退出房间
- /// </summary>
- public void Exit()
- {
- if (_tRTCCloud == null)
- {
- return;
- }
- _tRTCCloud.stopAllRemoteView();
- _tRTCCloud.stopLocalPreview();
- _tRTCCloud.stopLocalAudio();
- _tRTCCloud.muteLocalAudio(true);
- _tRTCCloud.muteLocalVideo(TRTCVideoStreamType.TRTCVideoStreamTypeBig, true);
- _tRTCCloud.stopScreenCapture();
- _roomId = 0;
- _tRTCCloud.exitRoom();
- if (_chatUserEnterRoomSuccess && !_leafRoomWaitor.WaitOne(JoinAndLeaveRoomTimeout))
- {
- }
- _chatUserEnterRoomSuccess = false;
- _tRTCCloud.removeCallback(this);
- _tRTCCloud.setLogCallback(null);
- }
- /// <summary>
- /// 静音/取消静音
- /// </summary>
- /// <param name="isMute"></param>
- public void Mute(bool isMute)
- {
- _tRTCCloud?.muteLocalAudio(isMute);
- }
- /// <summary>
- /// 开始分享屏幕|默认分享第一块屏幕
- /// </summary>
- public void StartShareScreen()
- {
- TRTCScreenCaptureSourceInfo sourceInfo = _mScreenList?.getSourceInfo(0)!;
- RECT rect = new RECT()
- {
- top = 0,
- left = 0,
- right = 0,
- bottom = 0,
- };
- TRTCScreenCaptureProperty property = new TRTCScreenCaptureProperty() { enableHighLight = false };
- _tRTCCloud?.selectScreenCaptureTarget(ref sourceInfo, ref rect, ref property);
- _tRTCCloud?.startScreenCapture(IntPtr.Zero, TRTCVideoStreamType.TRTCVideoStreamTypeSub, null);
- }
- /// <summary>
- /// 停止分享屏幕
- /// </summary>
- public void StopShareScreen()
- {
- _tRTCCloud?.stopScreenCapture();
- }
- /// <summary>
- /// 发送自己采集的视频
- /// </summary>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="frameBuffer"></param>
- private void SendCustomData(int width, int height, byte[] frameBuffer)
- {
- if (_videoFrame == null)
- {
- _videoFrame = new TRTCVideoFrame();
- _videoFrame.videoFormat = TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420;
- _videoFrame.bufferType = TRTCVideoBufferType.TRTCVideoBufferType_Buffer;
- }
- _videoFrame.width = (uint)width;
- _videoFrame.height = (uint)height;
- _videoFrame.data = frameBuffer;
- _videoFrame.length = (uint)frameBuffer.Length;
- _videoFrame.timestamp = 0;
- _tRTCCloud?.sendCustomVideoData(TRTCVideoStreamType.TRTCVideoStreamTypeBig, _videoFrame);
- }
-
- /// <summary>
- /// 本地视频数据
- /// </summary>
- /// <param name="data"></param>
- /// <param name="streamType"></param>
- private void OnLocalVideoFrameArrived(VideoFrameData data, TRTCVideoStreamType streamType)
- {
- if (streamType == TRTCVideoStreamType.TRTCVideoStreamTypeBig)
- {
- LocalVideoFrameArrived?.Invoke(this, data);
- }
- else
- {
- LocalVideoScreenFrameArrived?.Invoke(this, data);
- }
- }
- /// <summary>
- /// 远端视频数据
- /// </summary>
- /// <param name="data"></param>
- /// <param name="streamType"></param>
- private void OnRemoteVideoFrameArrived(RemoteVideoFrameData data, TRTCVideoStreamType streamType)
- {
- if (streamType == TRTCVideoStreamType.TRTCVideoStreamTypeBig)
- {
- RemoteVideoFrameArrived?.Invoke(this, data);
- }
- else
- {
- RemoteVideoScreenFrameArrived?.Invoke(this, data);
- }
- }
- public void onUserSubStreamAvailable(string userId, bool available)
- {
- //用户是否开启屏幕分享
- if (available)
- {
- var localVideoRenderParams = new TRTCRenderParams()
- {
- fillMode = TRTCVideoFillMode.TRTCVideoFillMode_Fit
- };
- _tRTCCloud?.setRemoteRenderParams(userId, TRTCVideoStreamType.TRTCVideoStreamTypeSub, ref localVideoRenderParams);
- _tRTCCloud?.startRemoteView(userId, TRTCVideoStreamType.TRTCVideoStreamTypeSub, IntPtr.Zero);
- }
- else
- {
- _tRTCCloud?.stopRemoteView(userId, TRTCVideoStreamType.TRTCVideoStreamTypeSub);
- }
- }
- //用户是否开启摄像头视频
- public void onUserVideoAvailable(string userId, bool available)
- {
- if (available)
- {
- _tRTCCloud?.startRemoteView(userId, TRTCVideoStreamType.TRTCVideoStreamTypeBig, IntPtr.Zero);
- _tRTCCloud?.setRemoteVideoRenderCallback(userId, TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, _remoteVideoRenderCallback);
- }
- else
- {
- _tRTCCloud?.stopRemoteView(userId, TRTCVideoStreamType.TRTCVideoStreamTypeBig);
- _tRTCCloud?.setRemoteVideoRenderCallback(userId, TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, null);
- }
- }
- public void onConnectionLost()
- {
- // SDK 跟服务器的连接断开
- ConnectionLostNotification?.Invoke();
- }
- public void onConnectionRecovery()
- {
- // SDK 跟服务器的连接恢复
- ConnectionRecoveryNotification?.Invoke();
- }
- public void onTryToReconnect()
- {
- //SDK尝试重新连接到服务器
- TryToReconnectNotification?.Invoke();
- }
- public void onDeviceChange(string deviceId, TXMediaDeviceType type, TRTCDeviceState state)
- {
- //本地设备通断回调
- }
- public void onEnterRoom(int result)
- {
- //已加入房间的回调
- _joinRoomWaitor.Set();
- if (result >= 0)
- {
- _chatUserEnterRoomSuccess = true;
- }
- else
- {
- //添加加入房间失败事件
- }
- }
- public void onExitRoom(int reason)
- {
- //离开房间的事件回调
- _leafRoomWaitor.Set();
- }
- public void onRemoteUserEnterRoom(string userId)
- {
- // 有用户加入当前房间
- RemoteUserEnterRoomNotification?.Invoke(userId);
- }
- public void onRemoteUserLeaveRoom(string userId, int reason)
- {
- // 有用户离开当前房间
- RemoteUserLeaveRoomNotification?.Invoke(userId, reason);
- }
- public void onWarning(TXLiteAVWarning warningCode, string warningMsg, IntPtr arg)
- {
- //警告回调:告知一些非严重性问题,卡顿或可恢复的解码失败
- }
- public void onError(TXLiteAVError errCode, string errMsg, IntPtr arg)
- {
- //错误回调,SDK不可恢复的错误,一定要监听,并分情况给用户界面适当的提示
- }
- #region 未使用事件
- public void onAudioDeviceCaptureVolumeChanged(uint volume, bool muted)
- {
- // 当前音频采集设备音量变化通知
- }
- public void onAudioDevicePlayoutVolumeChanged(uint volume, bool muted)
- {
- // 当前音频播放设备音量变化通知
- }
- public void onFirstAudioFrame(string userId)
- {
- //开始播放远程用户的首帧音频(本地声音不通知)
- }
- public void onFirstVideoFrame(string userId, TRTCVideoStreamType streamType, int width, int height)
- {
- //开始渲染本地或远程用户看到的首帧画面
- }
- public void onLocalRecordBegin(int errCode, string storagePath)
- {
- //录制任务已开始
- }
- public void onLocalRecordComplete(int errCode, string storagePath)
- {
- //录制任务已结束
- }
- public void onLocalRecording(int duration, string storagePath)
- {
- //录制任务进行中
- }
- public void onMicDidReady()
- {
- //麦克风准备就绪
- }
- public void onAudioEffectFinished(int effectId, int code)
- {
- //播放音效结束回调
- }
- public void onCameraDidReady()
- {
- // 摄像头准备就绪
- }
- public void onMissCustomCmdMsg(string userId, int cmdId, int errCode, int missed)
- {
- //自定义消息丢失回调
- }
- public void onNetworkQuality(TRTCQualityInfo localQuality, TRTCQualityInfo[] remoteQuality, uint remoteQualityCount)
- {
- //网络质量回调(统计上行与下行质量,2s调用一次)
- }
- public void onPlayBGMBegin(TXLiteAVError errCode)
- {
- //开始播放背景音乐(已被废弃)
- }
- public void onPlayBGMComplete(TXLiteAVError errCode)
- {
- //播放背景音乐结束(已被废弃)
- }
- public void onPlayBGMProgress(uint progressMS, uint durationMS)
- {
- // 播放背景音乐的进度(已被废弃)
- }
- public void onRecvCustomCmdMsg(string userId, int cmdID, uint seq, byte[] msg, uint msgSize)
- {
- //收到自定义消息回调
- }
- public void onRecvSEIMsg(string userId, byte[] message, uint msgSize)
- {
- //收到SEI消息的回调
- }
- public void onRemoteVideoStatusUpdated(string userId, TRTCVideoStreamType streamType, TRTCAVStatusType status, TRTCAVStatusChangeReason reason, IntPtr extrainfo)
- {
- //远端视频状态变化的事件回调
- }
- public void onScreenCaptureCovered()
- {
- // 屏幕分享被遮挡回调
- }
- public void onScreenCapturePaused(int reason)
- {
- // 屏幕分享暂停回调
- }
- public void onScreenCaptureResumed(int reason)
- {
- // 屏幕分享恢复回调
- }
- public void onScreenCaptureStarted()
- {
- // 屏幕开始分享回调
- }
- public void onScreenCaptureStoped(int reason)
- {
- // 屏幕停止分享回调
- }
- public void onSendFirstLocalAudioFrame()
- {
- //首帧本地音频已被发送
- }
- public void onSendFirstLocalVideoFrame(TRTCVideoStreamType streamType)
- {
- //首帧本地视频数据已被发送
- }
- public void onSetMixTranscodingConfig(int errCode, string errMsg)
- {
- // 设置云端混流转码参数的回调
- }
- public void onSnapshotComplete(string userId, TRTCVideoStreamType type, byte[] data, uint length, uint width, uint height, TRTCVideoPixelFormat format)
- {
- //截图完成时的回调
- }
- public void onSpeedTest(TRTCSpeedTestResult currentResult, uint finishedCount, uint totalCount)
- {
- //服务器测速回调
- }
- public void onStartPublishCDNStream(int errCode, string errMsg)
- {
- // 启动旁路推流到CDN完成的回调
- }
- public void onStartPublishing(int errCode, string errMsg)
- {
- //开始向腾讯云的直播CDN推流的回调,对应于TRTCCloud中的startPublishing()接口
- }
- public void onStatistics(TRTCStatistics statis)
- {
- //技术指标回调
- }
- public void onStopPublishCDNStream(int errCode, string errMsg)
- {
- //停止旁路推流到CDN完成的回调
- }
- public void onStopPublishing(int errCode, string errMsg)
- {
- //停止想腾讯云的直播CDN推流的回调,对应于TRTCCloud中的stopPublishing()接口
- }
- public void onSwitchRole(TXLiteAVError errCode, string errMsg)
- {
- // 切换角色的回调
- }
- public void onSwitchRoom(TXLiteAVError errCode, string errMsg)
- {
- //切换房间(switchRoom)结果的回调
- }
- public void onTestMicVolume(uint volume)
- {
- //麦克风测试音量回调
- }
- public void onTestSpeakerVolume(uint volume)
- {
- // 扬声器测试音量回调
- }
- public void onUserAudioAvailable(string userId, bool available)
- {
- // 用户是否开启音频上行
- }
- public void onUserEnter(string userId)
- {
- //有主播加入当前房间(废弃接口)
- }
- public void onUserExit(string userId, int reason)
- {
- //由用户离开当前房间(废弃接口)
- }
- public void onUserVoiceVolume(TRTCVolumeInfo[] userVolumes, uint userVolumesCount, uint totalVolume)
- {
- //用于提示音量大小的回调
- }
- public void onConnectOtherRoom(string userId, TXLiteAVError errCode, string errMsg)
- {
- throw new NotImplementedException();
- }
- public void onDisconnectOtherRoom(TXLiteAVError errCode, string errMsg)
- {
- throw new NotImplementedException();
- }
- #endregion
- }
- }
|