LiveUsServiceTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Threading;
  3. using vCloud.Server.ServerProtocol.Live;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.IUS.Common.Network.Leaf;
  6. using Vinno.IUS.Common.Network.Transfer;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common;
  8. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  9. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  10. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  11. using Vinno.vCloud.Protocol.Infrastructures;
  12. using Vinno.vCloud.Protocol.Messages.Live;
  13. using Vinno.vCloud.Protocol.Messages.Terminal;
  14. namespace Vinno.vCloud.Common.FIS.Test
  15. {
  16. public class LiveUsServiceTest : VCloudTestItem
  17. {
  18. private readonly string _liveUsServiceTestError1 = "LiveUsServiceTestError1";
  19. private readonly string _liveUsServiceTestError2 = "LiveUsServiceTestError2";
  20. private readonly string _liveUsServiceTestError3 = "LiveUsServiceTestError3";
  21. private readonly int _screenWidth;
  22. private readonly int _screenHeight;
  23. private string _liveUsServiceUrl;
  24. private ServerNetWork _serverNetWork;
  25. private EnumLiveStates updateLiveState;
  26. private bool _isReceivedData;
  27. public bool IsEnable => VCloudServiceTest.EnableLiveUsService;
  28. /// <summary>
  29. /// Raised when Live Notification.
  30. /// </summary>
  31. public static event EventHandler<LiveNotificationArgs> LiveNotification;
  32. public LiveUsServiceTest(VCloudServiceTest vCloudServiceTest, int screenWidth, int screenHeight) : base("LiveUsService", vCloudServiceTest)
  33. {
  34. _screenHeight = screenHeight;
  35. _screenWidth = screenWidth;
  36. double screenRatio = (double)screenHeight / screenWidth;
  37. if (_screenWidth > 720)
  38. {
  39. _screenHeight = (int)(720 * screenRatio);
  40. _screenWidth = 720;
  41. if (_screenHeight > 1080)
  42. {
  43. _screenHeight = (int)(screenHeight / screenRatio);
  44. _screenWidth = 1080;
  45. }
  46. }
  47. }
  48. public override VCloudServiceTestResult Execute()
  49. {
  50. //Check if live service enabled
  51. if (!IsEnable)
  52. {
  53. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.CanSkipped, _liveUsServiceTestError1, "(301)");//ErrorCode=301
  54. }
  55. var result = TimeoutExecute(CheckIsPublicNetwork);
  56. if (result == VCloudSubItemTestStatus.Timeout)
  57. {
  58. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(302)");//ErrorCode=302
  59. }
  60. if (result == VCloudSubItemTestStatus.Failed)
  61. {
  62. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(308)");//ErrorCode=308
  63. }
  64. if (_serverNetWork == ServerNetWork.Public)
  65. {
  66. result = TimeoutExecute(TestLivePush, 15000);
  67. if (result == VCloudSubItemTestStatus.Timeout)
  68. {
  69. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(303)");
  70. }
  71. if (result == VCloudSubItemTestStatus.Failed)
  72. {
  73. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, "");
  74. }
  75. }
  76. else
  77. {
  78. //Check live server url available
  79. result = TimeoutExecute(CheckLiveServiceUrlAvailable);
  80. if (result == VCloudSubItemTestStatus.Timeout)
  81. {
  82. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(305)");
  83. }
  84. if (result == VCloudSubItemTestStatus.Failed)
  85. {
  86. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(306)");
  87. }
  88. //Check Push data
  89. result = TimeoutExecute(TestRtmpPush, 15000);
  90. if (result == VCloudSubItemTestStatus.Timeout)
  91. {
  92. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(307)");
  93. }
  94. if (result == VCloudSubItemTestStatus.Failed)
  95. {
  96. if (string.IsNullOrEmpty(_liveUsServiceUrl))
  97. {
  98. Logger.WriteLineError("No live US service URL");
  99. }
  100. Uri myUri = new Uri(_liveUsServiceUrl);
  101. int port = myUri.Port;
  102. if (port <= 0)
  103. {
  104. port = 1935;
  105. }
  106. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, port);
  107. }
  108. }
  109. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
  110. }
  111. private VCloudSubItemTestStatus TestRtmpPush()
  112. {
  113. ILiveVideoPusher _pusher = null;
  114. try
  115. {
  116. Logger.WriteLineInfo($"LiveUsServiceTest -TestRtmpPush begin");
  117. lock (CPCombineSmartPushConfiguration.Instance)
  118. {
  119. Logger.WriteLineInfo($"LiveUsServiceTest - TestRtmpPush start");
  120. if (CPCombineSmartPushConfiguration.Instance.PushStatus == EnumLivePushStatus.Pushing)
  121. {
  122. Logger.WriteLineInfo($"LiveUsServiceTest - VCloudSubItemTestStatus.Successful");
  123. return VCloudSubItemTestStatus.Successful;
  124. }
  125. var logPath = VCloudServiceTest.WorkingFolder;
  126. CPTestLivePushConfiguration.Instance.Initialize(logPath);
  127. CPTestLivePushConfiguration.Instance.PushUrl = _liveUsServiceUrl;
  128. CPTestLivePushConfiguration.Instance.Mode = EnumPushMode.Screen;
  129. CPTestLivePushConfiguration.Instance.Resolution = EnumResolutionMode.Low;
  130. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtmpPusher(Guid.NewGuid().ToString(), _screenWidth, _screenHeight, UpdateLiveState);
  131. _pusher.StatusChanged += OnStatusChanged;
  132. _pusher.LiveNotification += OnLiveNotification;
  133. _isReceivedData = false;
  134. _pusher.Start(CPTestLivePushConfiguration.Instance);
  135. int checkLiveStateNum = 0;
  136. //10次 等待 检查_isReceivedData
  137. while (!_isReceivedData && checkLiveStateNum < 10)
  138. {
  139. checkLiveStateNum++;
  140. Thread.Sleep(500);
  141. }
  142. _pusher.Stop();
  143. _pusher.StatusChanged -= OnStatusChanged;
  144. _pusher.LiveNotification -= OnLiveNotification;
  145. _pusher = null;
  146. if (_isReceivedData)
  147. {
  148. return VCloudSubItemTestStatus.Successful;
  149. }
  150. else
  151. {
  152. return VCloudSubItemTestStatus.Failed;
  153. }
  154. }
  155. }
  156. catch (Exception e)
  157. {
  158. Logger.WriteLineError($"LiveUsServiceTest: TestLivePush failed,ex:{e}");
  159. if (_pusher != null)
  160. {
  161. _pusher.Stop();
  162. _pusher.LiveNotification -= OnLiveNotification;
  163. _pusher.StatusChanged -= OnStatusChanged;
  164. }
  165. return VCloudSubItemTestStatus.Failed;
  166. }
  167. finally
  168. {
  169. if (_pusher != null)
  170. {
  171. _pusher.Stop();
  172. _pusher.LiveNotification -= OnLiveNotification;
  173. _pusher.StatusChanged -= OnStatusChanged;
  174. _pusher = null;
  175. }
  176. }
  177. }
  178. private void OnStatusChanged(object sender, EnumLiveStatus e)
  179. {
  180. if (e == EnumLiveStatus.Connected)
  181. {
  182. _isReceivedData = true;
  183. }
  184. }
  185. private VCloudSubItemTestStatus TestLivePush()
  186. {
  187. ILiveVideoPusher _pusher = null;
  188. updateLiveState = EnumLiveStates.Idle;
  189. try
  190. {
  191. Logger.WriteLineInfo($"LiveUsServiceTest -TestLivePush begin");
  192. lock (CPCombineSmartPushConfiguration.Instance)
  193. {
  194. Logger.WriteLineInfo($"LiveUsServiceTest - TestLivePush start");
  195. if (CPCombineSmartPushConfiguration.Instance.PushStatus == EnumLivePushStatus.Pushing)
  196. {
  197. Logger.WriteLineInfo($"LiveUsServiceTest - VCloudSubItemTestStatus.Successful");
  198. return VCloudSubItemTestStatus.Successful;
  199. }
  200. var logPath = VCloudServiceTest.WorkingFolder;
  201. var terminalname = VCloudServiceTest.TerminalName;
  202. var userSign = string.Empty;
  203. int appId;
  204. int integerRoomId;
  205. var leaf = VCloudServiceTest.GetvCloudLeaf();
  206. var terminalid = Guid.NewGuid().ToString();
  207. var roomId = terminalid;
  208. CPTestLivePushConfiguration.Instance.Initialize(logPath);
  209. //TODO 正在直播时候
  210. using (var request = MessagePool.GetMessage<GetvCloudLiveRoomSignRequest>())
  211. {
  212. request.RoomId = roomId;
  213. request.TerminalVersion = VCloudServiceTest.TerminalVersion;
  214. var getLiveRoomSignResult =
  215. GetvCloudLiveRoomSignResult.Convert(leaf.Send(request));
  216. if (getLiveRoomSignResult != null)
  217. {
  218. userSign = getLiveRoomSignResult.UserSign;
  219. appId = getLiveRoomSignResult.AppId;
  220. CPTestLivePushConfiguration.Instance.AppId = appId;
  221. integerRoomId = getLiveRoomSignResult.IntegerRoomId;
  222. CPTestLivePushConfiguration.Instance.RoomId = (uint)integerRoomId;
  223. CPTestLivePushConfiguration.Instance.TerminalId = terminalid;
  224. CPTestLivePushConfiguration.Instance.UserSign = userSign;
  225. CPTestLivePushConfiguration.Instance.Mode = EnumPushMode.Screen;
  226. CPTestLivePushConfiguration.Instance.Resolution = EnumResolutionMode.Low;
  227. _pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreator.CreateRtcPusher((uint)integerRoomId, terminalid, userSign, CPTestLivePushConfiguration.Instance.Resolution,
  228. CPTestLivePushConfiguration.Instance.PushDataSourceType, _screenWidth, _screenHeight, UpdateLiveState);
  229. _pusher.LiveNotification += OnLiveNotification;
  230. _pusher.Start(CPTestLivePushConfiguration.Instance);
  231. int checkLiveStateNum = 0;
  232. //10次 等待 检查UpdateLiveState
  233. while (updateLiveState == EnumLiveStates.Idle && checkLiveStateNum < 10)
  234. {
  235. checkLiveStateNum++;
  236. Thread.Sleep(500);
  237. }
  238. _pusher.Stop();
  239. _pusher.LiveNotification -= OnLiveNotification;
  240. _pusher = null;
  241. if (updateLiveState == EnumLiveStates.TerminalIsPushing)
  242. {
  243. Logger.WriteLineInfo($"LiveUsServiceTest: TestLivePush Successful, LiveState:{updateLiveState}");
  244. return VCloudSubItemTestStatus.Successful;
  245. }
  246. else
  247. {
  248. Logger.WriteLineWarn($"LiveUsServiceTest: TestLivePush failed, LiveState:{updateLiveState}");
  249. return VCloudSubItemTestStatus.Failed;
  250. }
  251. }
  252. else
  253. {
  254. Logger.WriteLineWarn($"LiveUsServiceTest: TestLivePush convert message failed");
  255. return VCloudSubItemTestStatus.Failed;
  256. }
  257. }
  258. }
  259. }
  260. catch (Exception e)
  261. {
  262. Logger.WriteLineError($"LiveUsServiceTest: TestLivePush failed,ex:{e}");
  263. return VCloudSubItemTestStatus.Failed;
  264. }
  265. finally
  266. {
  267. if (_pusher != null)
  268. {
  269. _pusher.Stop();
  270. _pusher.LiveNotification -= OnLiveNotification;
  271. _pusher = null;
  272. }
  273. updateLiveState = EnumLiveStates.Idle;
  274. }
  275. }
  276. private void OnLiveNotification(object sender, LiveNotificationArgs e)
  277. {
  278. LiveNotification?.Invoke(this, e);
  279. }
  280. private VCloudSubItemTestStatus CheckLiveServiceUrlAvailable()
  281. {
  282. _liveUsServiceUrl = string.Empty;
  283. var liveUsServiceUrl = string.Empty;
  284. var leaf = VCloudServiceTest.GetvCloudLeaf();
  285. using (var request = MessagePool.GetMessage<GetLiveServiceTestUrlRequest>())
  286. {
  287. request.TerminalName = VCloudServiceTest.TerminalName;
  288. var getLiveServiceTestUrlResult =
  289. GetLiveServiceTestUrlResult.Convert(leaf.Send(request));
  290. if (getLiveServiceTestUrlResult != null)
  291. {
  292. liveUsServiceUrl = getLiveServiceTestUrlResult.LiveServiceUrl;
  293. }
  294. if (string.IsNullOrEmpty(liveUsServiceUrl))
  295. {
  296. return VCloudSubItemTestStatus.Failed;
  297. }
  298. _liveUsServiceUrl = liveUsServiceUrl;
  299. }
  300. return VCloudSubItemTestStatus.Successful;
  301. }
  302. private VCloudSubItemTestStatus CheckIsPublicNetwork()
  303. {
  304. var leaf = VCloudServiceTest.GetvCloudLeaf();
  305. using (var request = MessagePool.GetMessage<TerminalConnectionTestRequest1>())
  306. {
  307. request.TerminalName = VCloudServiceTest.TerminalName;
  308. var terminalConnectionTestResult =
  309. TerminalConnectionTestResult1.Convert(leaf.Send(request));
  310. if (terminalConnectionTestResult != null)
  311. {
  312. _serverNetWork = terminalConnectionTestResult.ServerNetWork;
  313. }
  314. else
  315. {
  316. return VCloudSubItemTestStatus.Failed;
  317. }
  318. }
  319. return VCloudSubItemTestStatus.Successful;
  320. }
  321. private void UpdateLiveState(EnumLiveStates liveState)
  322. {
  323. updateLiveState = liveState;
  324. }
  325. }
  326. }