LiveUsServiceTestForSonopost.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using vCloud.Server.ServerProtocol.Live;
  6. using Vinno.FIS.TRTCClient.Common.Enum;
  7. using Vinno.IUS.Common.Log;
  8. using Vinno.IUS.Common.Network.Leaf;
  9. using Vinno.IUS.Common.Network.Transfer;
  10. using Vinno.vCloud.FIS.CrossPlatform.Common;
  11. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  12. using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware;
  13. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  14. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  15. using Vinno.vCloud.Protocol.Infrastructures;
  16. using Vinno.vCloud.Protocol.Messages.Live;
  17. namespace Vinno.vCloud.Common.FIS.Test
  18. {
  19. public class LiveUsServiceTestForSonopost : VCloudTestItem
  20. {
  21. private readonly string _liveUsServiceTestError1 = "LiveUsServiceTestError1";
  22. private readonly string _liveUsServiceTestError2 = "LiveUsServiceTestError2";
  23. private readonly string _liveUsServiceTestError3 = "LiveUsServiceTestError3";
  24. private readonly AutoResetEvent _resetEvent = new AutoResetEvent(false);
  25. private LiveProtocol _liveProtocol = LiveProtocol.Rtmp;
  26. private IExtendedData _extendedData;
  27. public bool IsEnable => VCloudServiceTest.EnableLiveUsService;
  28. public LiveUsServiceTestForSonopost(VCloudServiceTest vCloudServiceTest) : base("LiveUsService", vCloudServiceTest)
  29. {
  30. }
  31. public override VCloudServiceTestResult Execute()
  32. {
  33. //Check if live service enabled
  34. if (!IsEnable)
  35. {
  36. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.CanSkipped, _liveUsServiceTestError1, "(301)");
  37. }
  38. var result = TimeoutExecute(CheckIsPublicNetwork);
  39. if (result == VCloudSubItemTestStatus.Timeout)
  40. {
  41. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(302)");
  42. }
  43. if (result == VCloudSubItemTestStatus.Failed)
  44. {
  45. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(308)");
  46. }
  47. _extendedData = null;
  48. if (_liveProtocol == LiveProtocol.Rtmp)
  49. {
  50. result = TimeoutExecute(CheckLiveServiceUrlAvailable);
  51. if (result == VCloudSubItemTestStatus.Timeout)
  52. {
  53. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(305)");
  54. }
  55. if (result == VCloudSubItemTestStatus.Failed)
  56. {
  57. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(306)");
  58. }
  59. }
  60. else if (_liveProtocol == LiveProtocol.RTC)
  61. {
  62. result = TimeoutExecute(CheckRtcAvailable);
  63. if (result == VCloudSubItemTestStatus.Timeout)
  64. {
  65. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(309)");
  66. }
  67. if (result == VCloudSubItemTestStatus.Failed)
  68. {
  69. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(310)");
  70. }
  71. }
  72. else
  73. {
  74. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(311)");
  75. }
  76. if (_extendedData == null)
  77. {
  78. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError2, "(312)");
  79. }
  80. result = TimeoutExecute(CheckLiveVideoTransmission, 15000);
  81. if (result == VCloudSubItemTestStatus.Timeout)
  82. {
  83. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, TimeoutError, "(313)");
  84. }
  85. if (result == VCloudSubItemTestStatus.Failed)
  86. {
  87. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestFailed, _liveUsServiceTestError3, "");
  88. }
  89. return new VCloudServiceTestResult(Id, VCloudTestItemStatus.TestDone);
  90. }
  91. private VCloudSubItemTestStatus CheckLiveVideoTransmission()
  92. {
  93. ILiveVideoPusherForSonopost pusher = null;
  94. try
  95. {
  96. if (LiveVideoStatusChecker.Instance.IsPushing)
  97. {
  98. return VCloudSubItemTestStatus.Successful;
  99. }
  100. var testCamera = CrossPlatformHelper.Instance.HardwareDetector.GetHardwareList(EnumHardwareType.Camera).FirstOrDefault();
  101. if (testCamera == null)
  102. {
  103. return VCloudSubItemTestStatus.Failed;
  104. }
  105. var deviceInfo = new CPVideoDeviceInfo();
  106. deviceInfo.Category = EnumLiveChannelCategory.Main;
  107. deviceInfo.Id = testCamera.Id;
  108. deviceInfo.Name = testCamera.Name;
  109. deviceInfo.Width = 640;
  110. deviceInfo.Height = 480;
  111. deviceInfo.FrameRate = 24;
  112. deviceInfo.IsAvailable = true;
  113. deviceInfo.IsEnable = true;
  114. CameraCaptureCapability finalCapability = null;
  115. if (_extendedData is RtmpExtendedData rtmpExtendedData)
  116. {
  117. var testCapbility = testCamera.Capabilities.FirstOrDefault(x => x.Width == rtmpExtendedData.UserInfos.FirstOrDefault().Width && x.Height == rtmpExtendedData.UserInfos.FirstOrDefault().Height);
  118. if (testCapbility == null)
  119. {
  120. var capblility = testCamera.Capabilities.FirstOrDefault(x => x.Width == 640 && x.Height == 480);
  121. if (capblility == null)
  122. {
  123. finalCapability = testCamera.Capabilities.FirstOrDefault();
  124. }
  125. else
  126. {
  127. finalCapability = capblility;
  128. }
  129. }
  130. else
  131. {
  132. finalCapability = testCapbility;
  133. }
  134. rtmpExtendedData.UserInfos.FirstOrDefault().Width = finalCapability.Width;
  135. rtmpExtendedData.UserInfos.FirstOrDefault().Height = finalCapability.Height;
  136. pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTMPSinglePusher();
  137. }
  138. else if (_extendedData is RtcExtendedData rtcExtendedData)
  139. {
  140. var testCapbility = testCamera.Capabilities.FirstOrDefault(x => x.Width == rtcExtendedData.UserInfos.FirstOrDefault().Width && x.Height == rtcExtendedData.UserInfos.FirstOrDefault().Height);
  141. if (testCapbility == null)
  142. {
  143. var capblility = testCamera.Capabilities.FirstOrDefault(x => x.Width == 640 && x.Height == 480);
  144. if (capblility == null)
  145. {
  146. finalCapability = testCamera.Capabilities.FirstOrDefault();
  147. }
  148. else
  149. {
  150. finalCapability = capblility;
  151. }
  152. }
  153. else
  154. {
  155. finalCapability = testCapbility;
  156. }
  157. rtcExtendedData.UserInfos.FirstOrDefault().Width = finalCapability.Width;
  158. rtcExtendedData.UserInfos.FirstOrDefault().Height = finalCapability.Height;
  159. pusher = CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost.CreateRTCSinglePusher();
  160. }
  161. if (pusher != null)
  162. {
  163. pusher.ChannelStateChanged += OnChannelStateChanged;
  164. pusher.StartPusher(_extendedData, new List<CPVideoDeviceInfo> { deviceInfo });
  165. var success = _resetEvent.WaitOne(10000);
  166. pusher.ChannelStateChanged -= OnChannelStateChanged;
  167. pusher.StopPusher();
  168. pusher.Dispose();
  169. pusher = null;
  170. if (success)
  171. {
  172. return VCloudSubItemTestStatus.Successful;
  173. }
  174. }
  175. }
  176. catch (Exception e)
  177. {
  178. Logger.WriteLineError($"CheckLiveVideoTransmission Error:{e}");
  179. }
  180. finally
  181. {
  182. if (pusher != null)
  183. {
  184. pusher.ChannelStateChanged -= OnChannelStateChanged;
  185. pusher.StopPusher();
  186. pusher.Dispose();
  187. pusher = null;
  188. }
  189. }
  190. return VCloudSubItemTestStatus.Failed;
  191. }
  192. private void OnChannelStateChanged(object sender, ChannelStateEventArgs e)
  193. {
  194. if (e.Category == EnumLiveChannelCategory.Main && e.State == EnumLiveStates.TerminalIsPushing)
  195. {
  196. _resetEvent.Set();
  197. }
  198. }
  199. private VCloudSubItemTestStatus CheckIsPublicNetwork()
  200. {
  201. var leaf = VCloudServiceTest.GetvCloudLeaf();
  202. using (var request = MessagePool.GetMessage<GetLiveProtocolRequest>())
  203. {
  204. var getLiveProtocolResult = GetLiveProtocolResult.Convert(leaf.Send(request));
  205. if (getLiveProtocolResult != null)
  206. {
  207. _liveProtocol = getLiveProtocolResult.LiveProtocol;
  208. Logger.WriteLineInfo($"Current Test Live Protocol:{_liveProtocol}");
  209. }
  210. else
  211. {
  212. return VCloudSubItemTestStatus.Failed;
  213. }
  214. }
  215. return VCloudSubItemTestStatus.Successful;
  216. }
  217. private VCloudSubItemTestStatus CheckLiveServiceUrlAvailable()
  218. {
  219. var data = new RtmpExtendedData(null, null, true);
  220. var leaf = VCloudServiceTest.GetvCloudLeaf();
  221. using (var request = MessagePool.GetMessage<GetLiveServiceTestUrlRequest>())
  222. {
  223. request.TerminalName = VCloudServiceTest.TerminalName;
  224. var getLiveServiceTestUrlResult = GetLiveServiceTestUrlResult.Convert(leaf.Send(request));
  225. if (getLiveServiceTestUrlResult == null)
  226. {
  227. return VCloudSubItemTestStatus.Failed;
  228. }
  229. if (string.IsNullOrEmpty(getLiveServiceTestUrlResult.LiveServiceUrl))
  230. {
  231. return VCloudSubItemTestStatus.Failed;
  232. }
  233. var userInfo = new RtmpUserInfo(EnumLiveChannelCategory.Main, getLiveServiceTestUrlResult.LiveServiceUrl, 1280, 720);
  234. data.UserInfos.Add(userInfo);
  235. _extendedData = data;
  236. }
  237. return VCloudSubItemTestStatus.Successful;
  238. }
  239. private VCloudSubItemTestStatus CheckRtcAvailable()
  240. {
  241. var data = new RtcExtendedData(null, null, true);
  242. var leaf = VCloudServiceTest.GetvCloudLeaf();
  243. var terminalId = Guid.NewGuid().ToString();
  244. var roomId = terminalId;
  245. using (var request = MessagePool.GetMessage<GetLiveRoomSignRequest>())
  246. {
  247. request.RoomId = roomId;
  248. var getLiveRoomSignResult = GetLiveRoomSignResult.Convert(leaf.Send(request));
  249. if (getLiveRoomSignResult == null)
  250. {
  251. return VCloudSubItemTestStatus.Failed;
  252. }
  253. if (getLiveRoomSignResult.AppId == 0)
  254. {
  255. return VCloudSubItemTestStatus.Failed;
  256. }
  257. data.RoomId = getLiveRoomSignResult.IntegerRoomId;
  258. data.AppId = getLiveRoomSignResult.AppId;
  259. var userInfo = new RtcUserInfo(EnumLiveChannelCategory.Main, terminalId, getLiveRoomSignResult.UserSign, 1280, 720);
  260. data.UserInfos.Add(userInfo);
  261. _extendedData = data;
  262. }
  263. return VCloudSubItemTestStatus.Successful;
  264. }
  265. }
  266. }