123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using vCloud.Server.ServerProtocol.Live;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Live;
- namespace Vinno.vCloud.Common.FIS.Test
- {
- public class LiveUsServiceTestForSonopost : VCloudTestItem
- {
- private readonly string _liveUsServiceTestError1 = "LiveUsServiceTestError1";
- private readonly string _liveUsServiceTestError2 = "LiveUsServiceTestError2";
- private readonly string _liveUsServiceTestError3 = "LiveUsServiceTestError3";
- private readonly AutoResetEvent _resetEvent = new AutoResetEvent(false);
- private LiveProtocol _liveProtocol = LiveProtocol.Rtmp;
- private IExtendedData _extendedData;
- public bool IsEnable => VCloudServiceTest.EnableLiveUsService;
- public LiveUsServiceTestForSonopost(VCloudServiceTest vCloudServiceTest) : base("LiveUsService", vCloudServiceTest)
- {
- }
- public override VCloudServiceTestResult Execute()
- {
- //Check if live service enabled
- if (!IsEnable)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.CanSkipped, _liveUsServiceTestError1, "(301)");
- }
- var result = TimeoutExecute(CheckIsPublicNetwork);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(302)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(308)");
- }
- _extendedData = null;
- if (_liveProtocol == LiveProtocol.Rtmp)
- {
- result = TimeoutExecute(CheckLiveServiceUrlAvailable);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(305)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(306)");
- }
- }
- else if (_liveProtocol == LiveProtocol.RTC)
- {
- result = TimeoutExecute(CheckRtcAvailable);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(309)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(310)");
- }
- }
- else
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(311)");
- }
- if (_extendedData == null)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(312)");
- }
- result = TimeoutExecute(CheckLiveVideoTransmission, 15000);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(313)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, "");
- }
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
- }
- private VCloudSubItemTestStatus CheckLiveVideoTransmission()
- {
- ILiveVideoPusherForSonopost pusher = null;
- try
- {
- if (LiveVideoStatusChecker.Instance.IsPushing)
- {
- return VCloudSubItemTestStatus.Successful;
- }
- var testCamera = CrossPlatformHelper.Instance.HardwareDetector.GetHardwareList(EnumHardwareType.Camera).FirstOrDefault();
- if (testCamera == null)
- {
- return VCloudSubItemTestStatus.Failed;
- }
- var deviceInfo = new CPVideoDeviceInfo();
- deviceInfo.Category = EnumLiveChannelCategory.Main;
- deviceInfo.Id = testCamera.Id;
- deviceInfo.Name = testCamera.Name;
- deviceInfo.Width = 640;
- deviceInfo.Height = 480;
- deviceInfo.FrameRate = 24;
- deviceInfo.IsAvailable = true;
- deviceInfo.IsEnable = true;
- CameraCaptureCapability finalCapability = null;
- if (_extendedData is RtmpExtendedData rtmpExtendedData)
- {
- var testCapbility = testCamera.Capabilities.FirstOrDefault(x => x.Width == rtmpExtendedData.UserInfos.FirstOrDefault().Width && x.Height == rtmpExtendedData.UserInfos.FirstOrDefault().Height);
- if (testCapbility == null)
- {
- var capblility = testCamera.Capabilities.FirstOrDefault(x => x.Width == 640 && x.Height == 480);
- if (capblility == null)
- {
- finalCapability = testCamera.Capabilities.FirstOrDefault();
- }
- else
- {
- finalCapability = capblility;
- }
- }
- else
- {
- finalCapability = testCapbility;
- }
- rtmpExtendedData.UserInfos.FirstOrDefault().Width = finalCapability.Width;
- rtmpExtendedData.UserInfos.FirstOrDefault().Height = finalCapability.Height;
- pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTMPSinglePusher();
- }
- else if (_extendedData is RtcExtendedData rtcExtendedData)
- {
- var testCapbility = testCamera.Capabilities.FirstOrDefault(x => x.Width == rtcExtendedData.UserInfos.FirstOrDefault().Width && x.Height == rtcExtendedData.UserInfos.FirstOrDefault().Height);
- if (testCapbility == null)
- {
- var capblility = testCamera.Capabilities.FirstOrDefault(x => x.Width == 640 && x.Height == 480);
- if (capblility == null)
- {
- finalCapability = testCamera.Capabilities.FirstOrDefault();
- }
- else
- {
- finalCapability = capblility;
- }
- }
- else
- {
- finalCapability = testCapbility;
- }
- rtcExtendedData.UserInfos.FirstOrDefault().Width = finalCapability.Width;
- rtcExtendedData.UserInfos.FirstOrDefault().Height = finalCapability.Height;
- pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTCSinglePusher();
- }
- if (pusher != null)
- {
- pusher.ChannelStateChanged += OnChannelStateChanged;
- pusher.StartPusher(_extendedData, new List<CPVideoDeviceInfo> { deviceInfo });
- var success = _resetEvent.WaitOne(10000);
- pusher.ChannelStateChanged -= OnChannelStateChanged;
- pusher.StopPusher();
- pusher.Dispose();
- pusher = null;
- if (success)
- {
- return VCloudSubItemTestStatus.Successful;
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"CheckLiveVideoTransmission Error:{e}");
- }
- finally
- {
- if (pusher != null)
- {
- pusher.ChannelStateChanged -= OnChannelStateChanged;
- pusher.StopPusher();
- pusher.Dispose();
- pusher = null;
- }
- }
- return VCloudSubItemTestStatus.Failed;
- }
- private void OnChannelStateChanged(object sender, ChannelStateEventArgs e)
- {
- if (e.Category == EnumLiveChannelCategory.Main && e.State == EnumLiveStates.TerminalIsPushing)
- {
- _resetEvent.Set();
- }
- }
- private VCloudSubItemTestStatus CheckIsPublicNetwork()
- {
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- using (var request = MessagePool.GetMessage<GetLiveProtocolRequest>())
- {
- var getLiveProtocolResult = GetLiveProtocolResult.Convert(leaf.Send(request));
- if (getLiveProtocolResult != null)
- {
- _liveProtocol = getLiveProtocolResult.LiveProtocol;
- Logger.WriteLineInfo($"Current Test Live Protocol:{_liveProtocol}");
- }
- else
- {
- return VCloudSubItemTestStatus.Failed;
- }
- }
- return VCloudSubItemTestStatus.Successful;
- }
- private VCloudSubItemTestStatus CheckLiveServiceUrlAvailable()
- {
- var data = new RtmpExtendedData(null, null, true);
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- using (var request = MessagePool.GetMessage<GetLiveServiceTestUrlRequest>())
- {
- request.TerminalName = VCloudServiceTest.TerminalName;
- var getLiveServiceTestUrlResult = GetLiveServiceTestUrlResult.Convert(leaf.Send(request));
- if (getLiveServiceTestUrlResult == null)
- {
- return VCloudSubItemTestStatus.Failed;
- }
- if (string.IsNullOrEmpty(getLiveServiceTestUrlResult.LiveServiceUrl))
- {
- return VCloudSubItemTestStatus.Failed;
- }
- var userInfo = new RtmpUserInfo(EnumLiveChannelCategory.Main, getLiveServiceTestUrlResult.LiveServiceUrl, 1280, 720);
- data.UserInfos.Add(userInfo);
- _extendedData = data;
- }
- return VCloudSubItemTestStatus.Successful;
- }
- private VCloudSubItemTestStatus CheckRtcAvailable()
- {
- var data = new RtcExtendedData(null, null, true);
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- var terminalId = Guid.NewGuid().ToString();
- var roomId = terminalId;
- using (var request = MessagePool.GetMessage<GetLiveRoomSignRequest>())
- {
- request.RoomId = roomId;
- var getLiveRoomSignResult = GetLiveRoomSignResult.Convert(leaf.Send(request));
- if (getLiveRoomSignResult == null)
- {
- return VCloudSubItemTestStatus.Failed;
- }
- if (getLiveRoomSignResult.AppId == 0)
- {
- return VCloudSubItemTestStatus.Failed;
- }
- data.RoomId = getLiveRoomSignResult.IntegerRoomId;
- data.AppId = getLiveRoomSignResult.AppId;
- var userInfo = new RtcUserInfo(EnumLiveChannelCategory.Main, terminalId, getLiveRoomSignResult.UserSign, 1280, 720);
- data.UserInfos.Add(userInfo);
- _extendedData = data;
- }
- return VCloudSubItemTestStatus.Successful;
- }
- }
- }
|