123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using System;
- using System.Threading;
- using vCloud.Server.ServerProtocol.Live;
- 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.LiveVideo;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Live;
- using Vinno.vCloud.Protocol.Messages.Terminal;
- namespace Vinno.vCloud.Common.FIS.Test
- {
- public class LiveUsServiceTest : VCloudTestItem
- {
- private readonly string _liveUsServiceTestError1 = "LiveUsServiceTestError1";
- private readonly string _liveUsServiceTestError2 = "LiveUsServiceTestError2";
- private readonly string _liveUsServiceTestError3 = "LiveUsServiceTestError3";
- private readonly int _screenWidth;
- private readonly int _screenHeight;
- private string _liveUsServiceUrl;
- private ServerNetWork _serverNetWork;
- private EnumLiveStates updateLiveState;
- private bool _isReceivedData;
- public bool IsEnable => VCloudServiceTest.EnableLiveUsService;
- /// <summary>
- /// Raised when Live Notification.
- /// </summary>
- public static event EventHandler<LiveNotificationArgs> LiveNotification;
- public LiveUsServiceTest(VCloudServiceTest vCloudServiceTest, int screenWidth, int screenHeight) : base("LiveUsService", vCloudServiceTest)
- {
- _screenHeight = screenHeight;
- _screenWidth = screenWidth;
- double screenRatio = (double)screenHeight / screenWidth;
- if (_screenWidth > 720)
- {
- _screenHeight = (int)(720 * screenRatio);
- _screenWidth = 720;
- if (_screenHeight > 1080)
- {
- _screenHeight = (int)(screenHeight / screenRatio);
- _screenWidth = 1080;
- }
- }
- }
- public override VCloudServiceTestResult Execute()
- {
- //Check if live service enabled
- if (!IsEnable)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.CanSkipped, _liveUsServiceTestError1, "(301)");//ErrorCode=301
- }
- var result = TimeoutExecute(CheckIsPublicNetwork);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(302)");//ErrorCode=302
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(308)");//ErrorCode=308
- }
- if (_serverNetWork == ServerNetWork.Public)
- {
- result = TimeoutExecute(TestLivePush, 15000);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(303)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, "");
- }
- }
- else
- {
- //Check live server url available
- 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)");
- }
- //Check Push data
- result = TimeoutExecute(TestRtmpPush, 15000);
- if (result == VCloudSubItemTestStatus.Timeout)
- {
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(307)");
- }
- if (result == VCloudSubItemTestStatus.Failed)
- {
- if (string.IsNullOrEmpty(_liveUsServiceUrl))
- {
- Logger.WriteLineError("No live US service URL");
- }
- Uri myUri = new Uri(_liveUsServiceUrl);
- int port = myUri.Port;
- if (port <= 0)
- {
- port = 1935;
- }
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, port);
- }
- }
- return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
- }
- private VCloudSubItemTestStatus TestRtmpPush()
- {
- ILiveVideoPusher _pusher = null;
- try
- {
- Logger.WriteLineInfo($"LiveUsServiceTest -TestRtmpPush begin");
- lock (CPCombineSmartPushConfiguration.Instance)
- {
- Logger.WriteLineInfo($"LiveUsServiceTest - TestRtmpPush start");
- if (CPCombineSmartPushConfiguration.Instance.PushStatus == EnumLivePushStatus.Pushing)
- {
- Logger.WriteLineInfo($"LiveUsServiceTest - VCloudSubItemTestStatus.Successful");
- return VCloudSubItemTestStatus.Successful;
- }
- var logPath = VCloudServiceTest.WorkingFolder;
- CPTestLivePushConfiguration.Instance.Initialize(logPath);
- CPTestLivePushConfiguration.Instance.PushUrl = _liveUsServiceUrl;
- CPTestLivePushConfiguration.Instance.Mode = EnumPushMode.Screen;
- CPTestLivePushConfiguration.Instance.Resolution = EnumResolutionMode.Low;
- _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtmpPusher(Guid.NewGuid().ToString(), _screenWidth, _screenHeight, UpdateLiveState);
- _pusher.StatusChanged += OnStatusChanged;
- _pusher.LiveNotification += OnLiveNotification;
- _isReceivedData = false;
- _pusher.Start(CPTestLivePushConfiguration.Instance);
- int checkLiveStateNum = 0;
- //10次 等待 检查_isReceivedData
- while (!_isReceivedData && checkLiveStateNum < 10)
- {
- checkLiveStateNum++;
- Thread.Sleep(500);
- }
- _pusher.Stop();
- _pusher.StatusChanged -= OnStatusChanged;
- _pusher.LiveNotification -= OnLiveNotification;
- _pusher = null;
- if (_isReceivedData)
- {
- return VCloudSubItemTestStatus.Successful;
- }
- else
- {
- return VCloudSubItemTestStatus.Failed;
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"LiveUsServiceTest: TestLivePush failed,ex:{e}");
- if (_pusher != null)
- {
- _pusher.Stop();
- _pusher.LiveNotification -= OnLiveNotification;
- _pusher.StatusChanged -= OnStatusChanged;
- }
- return VCloudSubItemTestStatus.Failed;
- }
- finally
- {
- if (_pusher != null)
- {
- _pusher.Stop();
- _pusher.LiveNotification -= OnLiveNotification;
- _pusher.StatusChanged -= OnStatusChanged;
- _pusher = null;
- }
- }
- }
- private void OnStatusChanged(object sender, EnumLiveStatus e)
- {
- if (e == EnumLiveStatus.Connected)
- {
- _isReceivedData = true;
- }
- }
- private VCloudSubItemTestStatus TestLivePush()
- {
- ILiveVideoPusher _pusher = null;
- updateLiveState = EnumLiveStates.Idle;
- try
- {
- Logger.WriteLineInfo($"LiveUsServiceTest -TestLivePush begin");
- lock (CPCombineSmartPushConfiguration.Instance)
- {
- Logger.WriteLineInfo($"LiveUsServiceTest - TestLivePush start");
- if (CPCombineSmartPushConfiguration.Instance.PushStatus == EnumLivePushStatus.Pushing)
- {
- Logger.WriteLineInfo($"LiveUsServiceTest - VCloudSubItemTestStatus.Successful");
- return VCloudSubItemTestStatus.Successful;
- }
- var logPath = VCloudServiceTest.WorkingFolder;
- var terminalname = VCloudServiceTest.TerminalName;
- var userSign = string.Empty;
- int appId;
- int integerRoomId;
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- var terminalid = Guid.NewGuid().ToString();
- var roomId = terminalid;
- CPTestLivePushConfiguration.Instance.Initialize(logPath);
- //TODO 正在直播时候
- using (var request = MessagePool.GetMessage<GetvCloudLiveRoomSignRequest>())
- {
- request.RoomId = roomId;
- request.TerminalVersion = VCloudServiceTest.TerminalVersion;
- var getLiveRoomSignResult =
- GetvCloudLiveRoomSignResult.Convert(leaf.Send(request));
- if (getLiveRoomSignResult != null)
- {
- userSign = getLiveRoomSignResult.UserSign;
- appId = getLiveRoomSignResult.AppId;
- CPTestLivePushConfiguration.Instance.AppId = appId;
- integerRoomId = getLiveRoomSignResult.IntegerRoomId;
- CPTestLivePushConfiguration.Instance.RoomId = (uint)integerRoomId;
- CPTestLivePushConfiguration.Instance.TerminalId = terminalid;
- CPTestLivePushConfiguration.Instance.UserSign = userSign;
- CPTestLivePushConfiguration.Instance.Mode = EnumPushMode.Screen;
- CPTestLivePushConfiguration.Instance.Resolution = EnumResolutionMode.Low;
- _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher((uint)integerRoomId, terminalid, userSign, CPTestLivePushConfiguration.Instance.Resolution,
- CPTestLivePushConfiguration.Instance.PushDataSourceType, _screenWidth, _screenHeight, UpdateLiveState);
- _pusher.LiveNotification += OnLiveNotification;
- _pusher.Start(CPTestLivePushConfiguration.Instance);
- int checkLiveStateNum = 0;
- //10次 等待 检查UpdateLiveState
- while (updateLiveState == EnumLiveStates.Idle && checkLiveStateNum < 10)
- {
- checkLiveStateNum++;
- Thread.Sleep(500);
- }
- _pusher.Stop();
- _pusher.LiveNotification -= OnLiveNotification;
- _pusher = null;
- if (updateLiveState == EnumLiveStates.TerminalIsPushing)
- {
- Logger.WriteLineInfo($"LiveUsServiceTest: TestLivePush Successful, LiveState:{updateLiveState}");
- return VCloudSubItemTestStatus.Successful;
- }
- else
- {
- Logger.WriteLineWarn($"LiveUsServiceTest: TestLivePush failed, LiveState:{updateLiveState}");
- return VCloudSubItemTestStatus.Failed;
- }
- }
- else
- {
- Logger.WriteLineWarn($"LiveUsServiceTest: TestLivePush convert message failed");
- return VCloudSubItemTestStatus.Failed;
- }
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"LiveUsServiceTest: TestLivePush failed,ex:{e}");
- return VCloudSubItemTestStatus.Failed;
- }
- finally
- {
- if (_pusher != null)
- {
- _pusher.Stop();
- _pusher.LiveNotification -= OnLiveNotification;
- _pusher = null;
- }
- updateLiveState = EnumLiveStates.Idle;
- }
- }
- private void OnLiveNotification(object sender, LiveNotificationArgs e)
- {
- LiveNotification?.Invoke(this, e);
- }
- private VCloudSubItemTestStatus CheckLiveServiceUrlAvailable()
- {
- _liveUsServiceUrl = string.Empty;
- var liveUsServiceUrl = string.Empty;
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- using (var request = MessagePool.GetMessage<GetLiveServiceTestUrlRequest>())
- {
- request.TerminalName = VCloudServiceTest.TerminalName;
- var getLiveServiceTestUrlResult =
- GetLiveServiceTestUrlResult.Convert(leaf.Send(request));
- if (getLiveServiceTestUrlResult != null)
- {
- liveUsServiceUrl = getLiveServiceTestUrlResult.LiveServiceUrl;
- }
- if (string.IsNullOrEmpty(liveUsServiceUrl))
- {
- return VCloudSubItemTestStatus.Failed;
- }
- _liveUsServiceUrl = liveUsServiceUrl;
- }
- return VCloudSubItemTestStatus.Successful;
- }
- private VCloudSubItemTestStatus CheckIsPublicNetwork()
- {
- var leaf = VCloudServiceTest.GetvCloudLeaf();
- using (var request = MessagePool.GetMessage<TerminalConnectionTestRequest1>())
- {
- request.TerminalName = VCloudServiceTest.TerminalName;
- var terminalConnectionTestResult =
- TerminalConnectionTestResult1.Convert(leaf.Send(request));
- if (terminalConnectionTestResult != null)
- {
- _serverNetWork = terminalConnectionTestResult.ServerNetWork;
- }
- else
- {
- return VCloudSubItemTestStatus.Failed;
- }
- }
- return VCloudSubItemTestStatus.Successful;
- }
- private void UpdateLiveState(EnumLiveStates liveState)
- {
- updateLiveState = liveState;
- }
- }
- }
|