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
- {
-
-
-
- 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;
-
-
-
- public event EventHandler<VideoFrameData>? LocalVideoFrameArrived;
-
-
-
- public event EventHandler<RemoteVideoFrameData>? RemoteVideoFrameArrived;
-
-
-
- public event EventHandler<VideoFrameData>? LocalVideoScreenFrameArrived;
-
-
-
- public event EventHandler<RemoteVideoFrameData>? RemoteVideoScreenFrameArrived;
-
-
-
- public event Action<string, int>? RemoteUserLeaveRoomNotification;
- public event Action<string>? RemoteUserEnterRoomNotification;
-
-
-
- public event Action? TryToReconnectNotification;
-
-
-
- public event Action? ConnectionRecoveryNotification;
-
-
-
- public event Action? ConnectionLostNotification;
- private TRTCChatRoom()
- {
- _localVideoRenderCallback = new LocalVideoRenderCallback(OnLocalVideoFrameArrived);
- _remoteVideoRenderCallback = new RemoteVideoRenderCallback(OnRemoteVideoFrameArrived);
- }
-
-
-
-
- private TRTCVideoEncParam GetVideoEncParam()
- {
- TRTCVideoEncParam tRTCVideoEncParam = new TRTCVideoEncParam();
- tRTCVideoEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_640_480;
- tRTCVideoEncParam.videoFps = 15;
- tRTCVideoEncParam.videoBitrate = 600;
- return tRTCVideoEncParam;
- }
-
-
-
-
-
-
-
-
- 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);
-
- 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))
- {
-
- }
- }
- 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);
- }
- }
-
-
-
- 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);
- }
-
-
-
-
- public void Mute(bool isMute)
- {
- _tRTCCloud?.muteLocalAudio(isMute);
- }
-
-
-
- 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);
- }
-
-
-
- public void StopShareScreen()
- {
- _tRTCCloud?.stopScreenCapture();
- }
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- private void OnLocalVideoFrameArrived(VideoFrameData data, TRTCVideoStreamType streamType)
- {
- if (streamType == TRTCVideoStreamType.TRTCVideoStreamTypeBig)
- {
- LocalVideoFrameArrived?.Invoke(this, data);
- }
- else
- {
- LocalVideoScreenFrameArrived?.Invoke(this, data);
- }
- }
-
-
-
-
-
- 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()
- {
-
- ConnectionLostNotification?.Invoke();
- }
- public void onConnectionRecovery()
- {
-
- ConnectionRecoveryNotification?.Invoke();
- }
- public void onTryToReconnect()
- {
-
- 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)
- {
-
- }
- #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)
- {
-
- }
- 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)
- {
-
- }
- 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)
- {
-
- }
- public void onStartPublishing(int errCode, string errMsg)
- {
-
- }
- public void onStatistics(TRTCStatistics statis)
- {
-
- }
- public void onStopPublishCDNStream(int errCode, string errMsg)
- {
-
- }
- public void onStopPublishing(int errCode, string errMsg)
- {
-
- }
- public void onSwitchRole(TXLiteAVError errCode, string errMsg)
- {
-
- }
- public void onSwitchRoom(TXLiteAVError errCode, string errMsg)
- {
-
- }
- 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
- }
- }
|