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;
///
/// Raised when Live Notification.
///
public static event EventHandler 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())
{
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())
{
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())
{
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;
}
}
}