123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937 |
- using AForge.Video.DirectShow;
- using ManageLiteAV;
- using System;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using Vinno.FIS.TRTCClient.Captures;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.FIS.TRTCClient.Common.Log;
- using Vinno.FIS.TRTCClient.Common.Pipe;
- using Vinno.FIS.TRTCClient.ImageSender;
- namespace Vinno.FIS.TRTCClient
- {
- internal class TRTCPusher : ITRTCCloudCallback, ITRTCVideoRenderCallback, IDisposable
- {
- private ITRTCCloud _cloud;
- private ITXDeviceManager _deviceManager;
- private TRTCVideoFrame _videoFrame;
- private CameraImageSenderV2 _imageSender;
- private bool _isImageSenderMode;
- private bool _isCustomCaptureMode;
- private string _deviceId;
- private bool _isPushing;
- public event EventHandler<VideoFrameArrivedArgs> VideoFrameReceived;
- public event EventHandler<TRTCImageFrameData> TRTCImageFrameDataReceived;
- private ManualResetEvent _exitResetEvent = new ManualResetEvent(false);
- public TRTCPusher(string path, EnumLiveChannelCategory category)
- {
- _cloud = ITRTCCloud.getTRTCShareInstance();
- var logPath = Path.Combine(path, category.ToString());
- if (!Directory.Exists(logPath))
- {
- Directory.CreateDirectory(logPath);
- }
- _cloud.setLogCompressEnabled(false);
- _cloud.setLogLevel(TRTCLogLevel.TRTCLogLevelError);
- _cloud.setLogDirPath(logPath);
- _cloud.addCallback(this);
- _cloud.setLocalVideoRenderCallback(TRTCVideoPixelFormat.TRTCVideoPixelFormat_BGRA32, TRTCVideoBufferType.TRTCVideoBufferType_Buffer, this);
- _deviceManager = _cloud.getDeviceManager();
- }
- public void EnterRoom(TRTCRoomInfo roomInfo)
- {
- if (roomInfo.IsUSMachineImage)
- {
- TRTCParams trtcParams = new TRTCParams();
- trtcParams.sdkAppId = roomInfo.AppId;
- trtcParams.roomId = roomInfo.RoomId;
- trtcParams.userId = roomInfo.UserId;
- trtcParams.userSig = roomInfo.UserSig;
- trtcParams.role = TRTCRoleType.TRTCRoleAnchor;
- _cloud.setDefaultStreamRecvMode(false, false);
- var resolution = GetResolution(roomInfo.OutputWidth, roomInfo.OutputHeight, true, roomInfo.DeviceId, roomInfo.IsOldServerMode, roomInfo.DeviceWidth, roomInfo.DeviceHeight);
- InitVideoParam(resolution, roomInfo.VideoFps, roomInfo.VideoBitrate, roomInfo.MinVideoBitrate);
- _cloud.enableCustomVideoCapture(true);//此情况,只有新版,超声机画面的时候满足。旧版超声机不存在分流的情况。
- _cloud.setVideoEncoderMirror(false);
- var correctMicId = GetCorrectId(HardwareType.Mic, roomInfo.MicDeviceId);
- if (!string.IsNullOrEmpty(correctMicId))
- {
- _deviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeMic, correctMicId);
- _cloud.startLocalAudio();//开启本地音频的采集和上行
- }
- _cloud.muteLocalAudio(roomInfo.IsMute);
- _cloud.muteAllRemoteAudio(true);
- _cloud.muteAllRemoteVideoStreams(true);
- _cloud.enterRoom(ref trtcParams, TRTCAppScene.TRTCAppSceneLIVE);
- _isPushing = true;
- }
- else
- {
- _deviceId = GetCorrectId(HardwareType.Camera, roomInfo.DeviceId);
- if (string.IsNullOrEmpty(_deviceId))
- {
- Logger.WriteLineError($"Can not get correct device Id:{roomInfo.DeviceId}");
- return;
- }
- TRTCParams trtcParams = new TRTCParams();
- trtcParams.sdkAppId = roomInfo.AppId;
- trtcParams.roomId = roomInfo.RoomId;
- trtcParams.userId = roomInfo.UserId;
- trtcParams.userSig = roomInfo.UserSig;
- trtcParams.role = TRTCRoleType.TRTCRoleAnchor;
- var resolution = GetResolution(roomInfo.OutputWidth, roomInfo.OutputHeight, false, roomInfo.DeviceId, roomInfo.IsOldServerMode, roomInfo.DeviceWidth, roomInfo.DeviceHeight);
- InitVideoParam(resolution, roomInfo.VideoFps, roomInfo.VideoBitrate, roomInfo.MinVideoBitrate);
- _cloud.setDefaultStreamRecvMode(false, false);
- _cloud.setVideoEncoderMirror(false);
- var correctMicId = GetCorrectId(HardwareType.Mic, roomInfo.MicDeviceId);
- if (!string.IsNullOrEmpty(correctMicId))
- {
- _deviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeMic, correctMicId);
- _cloud.startLocalAudio();//开启本地音频的采集和上行
- }
- _cloud.muteLocalAudio(roomInfo.IsMute);
- _cloud.muteAllRemoteAudio(true);
- _cloud.muteAllRemoteVideoStreams(true);
- if (_isImageSenderMode)
- {
- _cloud.enableCustomVideoCapture(true);
- _imageSender = GetCameraImageSenderV2(_deviceId, roomInfo.OutputWidth, roomInfo.OutputHeight, roomInfo.Category, roomInfo.IsFillMode, roomInfo.DeviceWidth, roomInfo.DeviceHeight);
- _imageSender.VideoFrameDataReceived += OnVideoFrameDataReceived;
- _imageSender.PreviewImageFrameDataReceived += OnPreviewImageFrameDataReceived;
- _imageSender.Start();
- }
- else
- {
- _deviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeCamera, _deviceId);
- _cloud.startLocalPreview(IntPtr.Zero);
- }
- _cloud.enterRoom(ref trtcParams, TRTCAppScene.TRTCAppSceneLIVE);
- _isPushing = true;
- }
- }
- private void OnVideoFrameDataReceived(object sender, TRTCVideoFrameData e)
- {
- SendData((uint)e.Width, (uint)e.Height, e.Data);
- }
- /// <summary>
- /// 发送要推流的数据
- /// </summary>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="frameBuffer"></param>
- public void SendData(uint width, uint height, byte[] frameBuffer)
- {
- var len = width * height * 3 / 2;
- if (_videoFrame == null)
- {
- _videoFrame = new TRTCVideoFrame();
- _videoFrame.videoFormat = TRTCVideoPixelFormat.TRTCVideoPixelFormat_I420;
- _videoFrame.bufferType = TRTCVideoBufferType.TRTCVideoBufferType_Buffer;
- _videoFrame.data = new byte[len];
- _videoFrame.width = width;
- _videoFrame.height = height;
- _videoFrame.length = len;
- _videoFrame.timestamp = 0;
- }
- _videoFrame.data = frameBuffer;
- _cloud?.sendCustomVideoData(_videoFrame);
- }
- public void ExitRoom()
- {
- Logger.WriteLineInfo("Exit Room");
- _isPushing = false;
- _cloud.stopAllRemoteView();
- _cloud.stopLocalPreview();
- _cloud.stopLocalAudio();
- _cloud.muteLocalAudio(true);
- _cloud.muteLocalVideo(true);
- _exitResetEvent.Reset();
- _cloud.exitRoom();
- _exitResetEvent.WaitOne(5000);
- }
- private void InitVideoParam(TRTCVideoResolution resolution, uint fps, uint videoBitrate, uint minVideoBitrate)
- {
- TRTCVideoEncParam videoEncParams = new TRTCVideoEncParam();
- videoEncParams.videoBitrate = videoBitrate;
- videoEncParams.minVideoBitrate = minVideoBitrate;
- videoEncParams.videoFps = fps;
- videoEncParams.videoResolution = resolution;
- videoEncParams.resMode = TRTCVideoResolutionMode.TRTCVideoResolutionModeLandscape;
- videoEncParams.enableAdjustRes = false;
- _cloud.setVideoEncoderParam(ref videoEncParams);
- var qosParams = new TRTCNetworkQosParam();
- qosParams.preference = TRTCVideoQosPreference.TRTCVideoQosPreferenceClear;
- qosParams.controlMode = TRTCQosControlMode.TRTCQosControlModeServer;
- _cloud.setNetworkQosParam(ref qosParams);
- }
- private CameraImageSenderV2 GetCameraImageSenderV2(string id, int width, int height, EnumLiveChannelCategory category, bool isFillMode, int deviceWidth, int deviceHeight)
- {
- if (width <= 320 && height <= 240)
- {
- return new Camera320X240SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 480 && height <= 360)
- {
- return new Camera480X360SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 640 && height <= 360)
- {
- return new Camera640X360SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 640 && height <= 480)
- {
- return new Camera640X480SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 960 && height <= 544)
- {
- return new Camera960X540SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 960 && height <= 720)
- {
- return new Camera960X720SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else if (width <= 1280 && height <= 720)
- {
- return new Camera1280X720SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- else
- {
- return new Camera1920X1080SenderV2(id, width, height, category, isFillMode, deviceWidth, deviceHeight);
- }
- }
- private TRTCVideoResolution GetResolution(int outputWidth, int outputHeight, bool isUSImage, string deviceId, bool isOldServerMode, int deviceWidth, int deviceHeight)
- {
- if (isOldServerMode)//老版本因为分辨率是计算好,且该分辨率是支持的。
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- if (outputWidth <= 120 && outputHeight <= 120)
- {
- return TRTCVideoResolution.TRTCVideoResolution_120_120;
- }
- if (outputWidth <= 160 && outputHeight <= 120)
- {
- return TRTCVideoResolution.TRTCVideoResolution_160_120;
- }
- if (outputWidth <= 160 && outputHeight <= 160)
- {
- return TRTCVideoResolution.TRTCVideoResolution_160_160;
- }
- if (outputWidth <= 256 && outputHeight <= 144)
- {
- return TRTCVideoResolution.TRTCVideoResolution_256_144;
- }
- if (outputWidth <= 320 && outputHeight <= 240)
- {
- return TRTCVideoResolution.TRTCVideoResolution_320_240;
- }
- if (outputWidth <= 480 && outputHeight <= 360)
- {
- return TRTCVideoResolution.TRTCVideoResolution_480_360;
- }
- if (outputWidth <= 480 && outputHeight <= 480)
- {
- return TRTCVideoResolution.TRTCVideoResolution_480_480;
- }
- if (outputWidth <= 640 && outputHeight <= 360)
- {
- return TRTCVideoResolution.TRTCVideoResolution_640_360;
- }
- if (outputWidth <= 640 && outputHeight <= 480)
- {
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- if (outputWidth <= 960 && outputHeight <= 544)
- {
- return TRTCVideoResolution.TRTCVideoResolution_960_540;
- }
- if (outputWidth <= 960 && outputHeight <= 720)
- {
- return TRTCVideoResolution.TRTCVideoResolution_960_720;
- }
- if (outputWidth <= 1280 && outputHeight <= 720)
- {
- return TRTCVideoResolution.TRTCVideoResolution_1280_720;
- }
- if (outputWidth <= 1920 && outputHeight <= 1080)
- {
- return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
- }
- }
- else//新版是Server计算的,等比缩放后有些分辨率摄像头是不支持的。
- {
- if (isUSImage)//超声机画面是由RTCMultiV2填充好传过来,只需直接传给TRTC即可。
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = true;
- if (outputWidth <= 640 && outputHeight <= 480)
- {
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- else if (outputWidth <= 960 && outputHeight <= 544)
- {
- return TRTCVideoResolution.TRTCVideoResolution_960_540;
- }
- else if (outputWidth <= 960 && outputHeight <= 720)
- {
- return TRTCVideoResolution.TRTCVideoResolution_960_720;
- }
- else if (outputWidth <= 1280 && outputHeight <= 720)
- {
- return TRTCVideoResolution.TRTCVideoResolution_1280_720;
- }
- else
- {
- return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
- }
- }
- else
- {
- if (SupportResolution(deviceId, outputWidth, outputHeight) && outputHeight == deviceHeight && outputWidth == deviceWidth)//若该分辨率摄像头支持打开
- {
- if (outputWidth == 120 && outputHeight == 120)//若符合TRTC分辨率,,则直接由TRTC直接打开
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_120_120;
- }
- else if (outputWidth == 160 && outputHeight == 120)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_160_120;
- }
- else if (outputWidth == 160 && outputHeight == 160)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_160_160;
- }
- else if (outputWidth == 256 && outputHeight == 144)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_256_144;
- }
- else if (outputWidth == 320 && outputHeight == 240)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_320_240;
- }
- else if (outputWidth == 480 && outputHeight == 360)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_480_360;
- }
- else if (outputWidth == 480 && outputHeight == 480)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_480_480;
- }
- else if (outputWidth == 640 && outputHeight == 360)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_640_360;
- }
- else if (outputWidth == 640 && outputHeight == 480)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- else if (outputWidth == 960 && outputHeight == 544)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_960_540;
- }
- else if (outputWidth == 960 && outputHeight == 720)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_960_720;
- }
- else if (outputWidth == 1280 && outputHeight == 720)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_1280_720;
- }
- else if (outputWidth == 1920 && outputHeight == 1080)
- {
- _isImageSenderMode = false;
- _isCustomCaptureMode = false;
- return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
- }
- else if (outputWidth <= 320 && outputHeight <= 240)//TRTC分辨率不支持,则通过ImageSender传图
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_320_240;
- }
- else if (outputWidth <= 480 && outputHeight <= 360)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_480_360;
- }
- else if (outputWidth <= 640 && outputHeight <= 360)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_640_360;
- }
- else if (outputWidth <= 640 && outputHeight <= 480)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- else if (outputWidth <= 960 && outputHeight <= 544)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_960_540;
- }
- else if (outputWidth <= 960 && outputHeight <= 720)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_960_720;
- }
- else if (outputWidth <= 1280 && outputHeight <= 720)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_1280_720;
- }
- else if (outputWidth <= 1920 && outputHeight <= 1080)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
- }
- }
- else
- {
- if (outputWidth <= 320 && outputHeight <= 240)//TRTC分辨率不支持,则通过ImageSender传图
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_320_240;
- }
- else if (outputWidth <= 480 && outputHeight <= 360)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_480_360;
- }
- else if (outputWidth <= 640 && outputHeight <= 360)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_640_360;
- }
- else if (outputWidth <= 640 && outputHeight <= 480)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- else if (outputWidth <= 960 && outputHeight <= 544)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_960_540;
- }
- else if (outputWidth <= 960 && outputHeight <= 720)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_960_720;
- }
- else if (outputWidth <= 1280 && outputHeight <= 720)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_1280_720;
- }
- else if (outputWidth <= 1920 && outputHeight <= 1080)
- {
- _isImageSenderMode = true;
- _isCustomCaptureMode = true;
- return TRTCVideoResolution.TRTCVideoResolution_1920_1080;
- }
- }
- }
- }
- return TRTCVideoResolution.TRTCVideoResolution_640_480;
- }
- private bool SupportResolution(string deviceId, int width, int height)
- {
- var videoFilterInfos = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType<FilterInfo>();
- var filterInfo = videoFilterInfos.FirstOrDefault(i => deviceId.Contains(i.MonikerString) || i.MonikerString.Contains(deviceId));
- if (filterInfo != null)
- {
- var videoCaptureDevice = new VideoCaptureDevice(filterInfo.MonikerString);
- var capbility = videoCaptureDevice.VideoCapabilities.FirstOrDefault(x => x.FrameSize.Width == width && x.FrameSize.Height == height);
- if (capbility != null)
- {
- Logger.WriteLineInfo($"TRTCPusher SupportResolution True,Id:{deviceId},Width:{width},Height:{height}");
- return true;
- }
- }
- Logger.WriteLineInfo($"TRTCPusher SupportResolution False,Id:{deviceId},Width:{width},Height:{height}");
- return false;
- }
- public void onRenderVideoFrame(string userId, TRTCVideoStreamType streamType, TRTCVideoFrame frame)
- {
- if (!_isImageSenderMode)
- {
- VideoFrameReceived?.Invoke(this, new VideoFrameArrivedArgs(frame, userId));
- }
- }
- private void OnPreviewImageFrameDataReceived(object sender, TRTCImageFrameData e)
- {
- if (_isImageSenderMode)
- {
- TRTCImageFrameDataReceived?.Invoke(this, e);
- }
- }
- /// <summary>
- /// Get the Correct Id
- /// </summary>
- /// <param name="type"></param>
- /// <param name="deviceId"></param>
- /// <returns></returns>
- private string GetCorrectId(HardwareType type, string deviceId)
- {
- switch (type)
- {
- case HardwareType.Camera:
- var cameraList = _deviceManager.getDevicesList(TRTCDeviceType.TXMediaDeviceTypeCamera);
- try
- {
- var count = cameraList.getCount();
- for (uint i = 0; i < count; i++)
- {
- var id = cameraList.getDevicePID(i);
- if (deviceId.Contains(id) || id.Contains(deviceId))
- {
- return id;
- }
- }
- }
- finally
- {
- cameraList.release();
- }
- break;
- case HardwareType.Mic:
- var micList = _deviceManager.getDevicesList(TRTCDeviceType.TXMediaDeviceTypeMic);
- try
- {
- var count = micList.getCount();
- for (uint i = 0; i < count; i++)
- {
- var id = micList.getDevicePID(i);
- if (deviceId.Contains(id) || id.Contains(deviceId))
- {
- return id;
- }
- }
- }
- finally
- {
- micList.release();
- }
- break;
- case HardwareType.Speaker:
- var speakerList = _deviceManager.getDevicesList(TRTCDeviceType.TXMediaDeviceTypeSpeaker);
- try
- {
- var count = speakerList.getCount();
- for (uint i = 0; i < count; i++)
- {
- var id = speakerList.getDevicePID(i);
- if (deviceId.Contains(id) || id.Contains(deviceId))
- {
- return id;
- }
- }
- }
- finally
- {
- speakerList.release();
- }
- break;
- }
- return string.Empty;
- }
- public void Dispose()
- {
- try
- {
- if (_imageSender != null)
- {
- _imageSender.VideoFrameDataReceived -= OnVideoFrameDataReceived;
- _imageSender.PreviewImageFrameDataReceived -= OnPreviewImageFrameDataReceived;
- _imageSender.Stop();
- _imageSender = null;
- }
- ExitRoom();
- _cloud.removeCallback(this);
- _cloud.setLogCallback(null);
- _cloud = null;
- ITRTCCloud.destroyTRTCShareInstance();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"TRTCPusher Dispose Error:{ex}");
- }
- }
- #region callback
- public void onAudioEffectFinished(int effectId, int code)
- {
- // throw new NotImplementedException();
- }
- public void onCameraDidReady()
- {
- Logger.WriteLineInfo($"onCameraDidReady");
- }
- public void onConnectionLost()
- {
- Logger.WriteLineInfo($"onConnectionLost");
- }
- public void onConnectionRecovery()
- {
- Logger.WriteLineInfo($"onConnectionRecovery");
- }
- public void onConnectOtherRoom(string userId, TXLiteAVError errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onDeviceChange(string deviceId, TRTCDeviceType type, TRTCDeviceState state)
- {
- try
- {
- Logger.WriteLineError($"DeviceChange:{deviceId},type:{type},state:{state}");
- if (string.IsNullOrEmpty(_deviceId))
- {
- return;
- }
- if (!_isImageSenderMode)
- {
- if (type == TRTCDeviceType.TXMediaDeviceTypeCamera && (deviceId.Contains(_deviceId) || _deviceId.Contains(deviceId)))
- {
- if (_isPushing && state == TRTCDeviceState.TRTCDeviceStateRemove)
- {
- _isPushing = false;
- _cloud.stopLocalPreview();
- }
- else if (!_isPushing && state == TRTCDeviceState.TRTCDeviceStateAdd)
- {
- _isPushing = true;
- _deviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeCamera, _deviceId);
- _cloud.startLocalPreview(IntPtr.Zero);
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"onDeviceChange Error:{ex}");
- }
- }
- public void onDisconnectOtherRoom(TXLiteAVError errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onEnterRoom(int result)
- {
- Logger.WriteLineInfo($"Enter room :{result}");
- }
- public void onError(TXLiteAVError errCode, string errMsg, IntPtr arg)
- {
- Logger.WriteLineError($"onError errCode:{errCode}, msg:{errMsg}, arg:{arg}");
- }
- public void onExitRoom(int reason)
- {
- Logger.WriteLineInfo($"Exit room success:{reason}");
- _exitResetEvent.Set();
- }
- public void onFirstAudioFrame(string userId)
- {
- Logger.WriteLineInfo($"onFirstAudioFrame userId: {userId}");
- }
- public void onFirstVideoFrame(string userId, TRTCVideoStreamType streamType, int width, int height)
- {
- Logger.WriteLineInfo($"onFirstVideoFrame userId: {userId}, streamType: {streamType.ToString()}, width:{width}, height:{height}");
- }
- public void onMicDidReady()
- {
- Logger.WriteLineInfo($"onMicDidReady");
- }
- public void onMissCustomCmdMsg(string userId, int cmdId, int errCode, int missed)
- {
- // throw new NotImplementedException();
- }
- public void onNetworkQuality(TRTCQualityInfo localQuality, TRTCQualityInfo[] remoteQuality, uint remoteQualityCount)
- {
- // throw new NotImplementedException();
- }
- public void onPlayBGMBegin(TXLiteAVError errCode)
- {
- // throw new NotImplementedException();
- }
- public void onPlayBGMComplete(TXLiteAVError errCode)
- {
- // throw new NotImplementedException();
- }
- public void onPlayBGMProgress(uint progressMS, uint durationMS)
- {
- // throw new NotImplementedException();
- }
- public void onRecvCustomCmdMsg(string userId, int cmdID, uint seq, byte[] msg, uint msgSize)
- {
- // throw new NotImplementedException();
- }
- public void onRecvSEIMsg(string userId, byte[] message, uint msgSize)
- {
- // throw new NotImplementedException();
- }
- public void onRemoteUserEnterRoom(string userId)
- {
- // throw new NotImplementedException();
- }
- public void onRemoteUserLeaveRoom(string userId, int reason)
- {
- // throw new NotImplementedException();
- }
- public void onScreenCaptureCovered()
- {
- // throw new NotImplementedException();
- }
- public void onScreenCapturePaused(int reason)
- {
- // throw new NotImplementedException();
- }
- public void onScreenCaptureResumed(int reason)
- {
- // throw new NotImplementedException();
- }
- public void onScreenCaptureStarted()
- {
- // throw new NotImplementedException();
- }
- public void onScreenCaptureStoped(int reason)
- {
- // throw new NotImplementedException();
- }
- public void onSendFirstLocalAudioFrame()
- {
- // throw new NotImplementedException();
- }
- public void onSendFirstLocalVideoFrame(TRTCVideoStreamType streamType)
- {
- // throw new NotImplementedException();
- }
- public void onSetMixTranscodingConfig(int errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onSpeedTest(TRTCSpeedTestResult currentResult, uint finishedCount, uint totalCount)
- {
- // throw new NotImplementedException();
- }
- public void onStartPublishCDNStream(int errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onStartPublishing(int errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onStatistics(TRTCStatistics statis)
- {
- // throw new NotImplementedException();
- }
- public void onStopPublishCDNStream(int errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onStopPublishing(int errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onSwitchRole(TXLiteAVError errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onTestMicVolume(uint volume)
- {
- // throw new NotImplementedException();
- }
- public void onTestSpeakerVolume(uint volume)
- {
- // throw new NotImplementedException();
- }
- public void onTryToReconnect()
- {
- // throw new NotImplementedException();
- }
- public void onUserAudioAvailable(string userId, bool available)
- {
- // throw new NotImplementedException();
- }
- public void onUserEnter(string userId)
- {
- // throw new NotImplementedException();
- }
- public void onUserExit(string userId, int reason)
- {
- // throw new NotImplementedException();
- }
- public void onUserSubStreamAvailable(string userId, bool available)
- {
- // throw new NotImplementedException();
- }
- public void onUserVideoAvailable(string userId, bool available)
- {
- // throw new NotImplementedException();
- }
- public void onUserVoiceVolume(TRTCVolumeInfo[] userVolumes, uint userVolumesCount, uint totalVolume)
- {
- // throw new NotImplementedException();
- }
- public void onWarning(TXLiteAVWarning warningCode, string warningMsg, IntPtr arg)
- {
- Logger.WriteLineWarn($"onWarning errCode: {warningCode}, msg:{warningMsg}, arg:{arg}");
- }
- public void onSwitchRoom(TXLiteAVError errCode, string errMsg)
- {
- // throw new NotImplementedException();
- }
- public void onAudioDeviceCaptureVolumeChanged(uint volume, bool muted)
- {
- // throw new NotImplementedException();
- }
- public void onAudioDevicePlayoutVolumeChanged(uint volume, bool muted)
- {
- // throw new NotImplementedException();
- }
- internal void SetMute(bool isMute)
- {
- if (_cloud != null)
- {
- _cloud.muteLocalAudio(isMute);
- }
- }
- internal void SwitchMic(string micId)
- {
- if (_cloud != null)
- {
- _cloud.stopLocalAudio();
- var correctMicId = GetCorrectId(HardwareType.Mic, micId);
- if (!string.IsNullOrEmpty(correctMicId))
- {
- var deviceManager = _cloud.getDeviceManager();
- deviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeMic, correctMicId);
- _cloud.startLocalAudio();//开启本地音频的采集和上行
- }
- }
- }
- #endregion callback
- public enum HardwareType
- {
- Mic,
- Camera,
- Speaker
- }
- }
- }
|