RtmpPusher.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using NT;
  2. using System;
  3. using System.Drawing;
  4. using System.Linq;
  5. using Vinno.IUS.Common.Log;
  6. using Vinno.vCloud.Common.FIS;
  7. using Vinno.vCloud.Common.FIS.Device;
  8. using Vinno.vCloud.Common.FIS.LiveVideos;
  9. using Vinno.vCloud.FIS.Windows.RTMP;
  10. using Vinno.vCloud.Protocol.Infrastructures;
  11. using Vinno.vCloud.Push.Configuration;
  12. namespace Vinno.vCloud.FIS.Windows
  13. {
  14. public class RtmpPusher : IPusher
  15. {
  16. private SmartPublisher _pusher;
  17. private CombineSmartPushConfiguration _pushConfiguration;
  18. private Action<LiveStates> _updateState;
  19. /// <summary>
  20. /// Preview local camera capured changed, Rtmp unused
  21. /// </summary>
  22. public event EventHandler<VideoFrameData> PreviewCameraCapured;
  23. /// <summary>
  24. /// The event to notificate the US to show or hide the live video out put window
  25. /// </summary>
  26. public event EventHandler<LiveNotificationArgs> LiveNotification;
  27. /// <summary>
  28. /// The event to notificate the Live Status Changed.
  29. /// Only used for RTMP pusher.
  30. /// </summary>
  31. public event EventHandler<LiveStatus> StatusChanged;
  32. public int TerminalWidth { get; private set; }
  33. public int TerminalHeight { get; private set; }
  34. public int CameraWidth { get; private set; }
  35. public int CameraHeight { get; private set; }
  36. public RtmpPusher(Action<LiveStates> updateState)
  37. {
  38. _updateState = updateState;
  39. }
  40. public bool Start(CombineSmartPushConfiguration configuration)
  41. {
  42. try
  43. {
  44. _pushConfiguration = configuration;
  45. Logger.WriteLineInfo($"Started rtmp pusher: {configuration}");
  46. InitSetSmartPusher(configuration.PushAppLogPath);
  47. DoPush();
  48. if (_pushConfiguration.ShouldPushData)
  49. {
  50. _updateState?.Invoke(LiveStates.TerminalIsPushing);
  51. }
  52. return true;
  53. }
  54. catch (Exception ex)
  55. {
  56. Logger.WriteLineError($"RtmpPusher: start failed ex:{ex}");
  57. }
  58. return true;
  59. }
  60. public bool Stop()
  61. {
  62. try
  63. {
  64. if (_pushConfiguration != null)
  65. {
  66. if (!_pushConfiguration.ShouldPushData && _pushConfiguration.ShowPreviewImage)//本地预览
  67. {
  68. var push = _pusher as CombineSmartPublisher;
  69. if (push != null)
  70. {
  71. CloseSmartPusher();
  72. Logger.WriteLineInfo($"Restart smart pusher");
  73. }
  74. DoPush();
  75. Logger.WriteLineInfo($"RtmpPusher: stop and only show preview to restart push");
  76. return true;
  77. }
  78. else
  79. {
  80. try
  81. {
  82. CloseSmartPusher();
  83. Logger.WriteLineInfo($"RtmpPusher: Dispose");
  84. return true;
  85. }
  86. catch (Exception ex)
  87. {
  88. Logger.WriteLineError($"RtmpPusher: stop failed to kill ex:{ex}");
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. Logger.WriteLineError($"RtmpPusher: stop failed ex:{e}");
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// set push mute or ummute
  101. /// </summary>
  102. /// <param name="isMute"></param>
  103. public void SetMute(bool isMute)
  104. {
  105. if (_pusher != null)
  106. {
  107. _pusher.IsMute = isMute;
  108. }
  109. }
  110. public void SetPreviewCamera(bool isShowPreview)
  111. {
  112. if (isShowPreview)
  113. {
  114. _pusher.PreviewImageReceived += OnPreviewImageReceived;
  115. }
  116. else
  117. {
  118. _pusher.PreviewImageReceived -= OnPreviewImageReceived;
  119. }
  120. }
  121. public void ChangeCamera(string cameraId)
  122. {
  123. if (_pusher != null && !_pushConfiguration.ShouldPushData)
  124. {
  125. Stop();
  126. }
  127. }
  128. public void ChangeMic(string micId)
  129. {
  130. if (_pusher != null && !_pushConfiguration.ShouldPushData)
  131. {
  132. Stop();
  133. }
  134. }
  135. public bool StartSpeedTest(uint appId, string userid, string userSign)
  136. {
  137. // throw new NotImplementedException();
  138. return true;
  139. }
  140. public void StopSpeedTest()
  141. {
  142. // throw new NotImplementedException();
  143. }
  144. private void DoPush()
  145. {
  146. switch (_pushConfiguration.Mode)
  147. {
  148. case PushMode.Screen:
  149. StartPushScreen();
  150. break;
  151. case PushMode.Camera when !string.IsNullOrEmpty(_pushConfiguration.VideoDevicePath):
  152. StartPushCamera();
  153. break;
  154. case PushMode.CombineTerminal:
  155. case PushMode.CombineTerminalGetResolution:
  156. StartPush();
  157. break;
  158. }
  159. }
  160. private bool StartPushScreen()
  161. {
  162. Logger.WriteLineInfo($"StartPushScreen begin");
  163. var noError = true;
  164. try
  165. {
  166. _pusher = new SuperScreenPusher(_pushConfiguration.PushUrl, -1, _pushConfiguration.Resolution);
  167. _pusher.IsMute = _pushConfiguration.IsMute;
  168. _pusher.StatusChanged += OnStatusChanged;
  169. if (_pushConfiguration.ShouldPushData)
  170. {
  171. _pusher.Start();
  172. }
  173. Logger.WriteLineInfo($"StartPushScreen successfully");
  174. }
  175. catch (Exception e)
  176. {
  177. noError = false;
  178. Logger.WriteLineError($"StartPushScreen ex:{e}");
  179. }
  180. if (!noError)
  181. {
  182. return false;
  183. }
  184. Logger.WriteLineInfo("Start Push Screen Success");
  185. return true;
  186. }
  187. private void InitSetSmartPusher(string logPath)
  188. {
  189. try
  190. {
  191. var ret = Environment.Is64BitProcess ?
  192. NTSmartLog_x64.NT_SL_SetPath(logPath) : NTSmartLog_x86.NT_SL_SetPath(logPath);
  193. ret = Environment.Is64BitProcess
  194. ? NTSmartPublisherSDK_x64.NT_PB_SetSDKClientKey("03396870E2CA4E4F98593EC368EE77ED", "9ACF4FBED7EF4E56833941DE29267C9F79130CBB4C7345E2859ED4E1E2DC9EDD", 0, IntPtr.Zero) :
  195. NTSmartPublisherSDK_x86.NT_PB_SetSDKClientKey("03396870E2CA4E4F98593EC368EE77ED", "9ACF4FBED7EF4E56833941DE29267C9F79130CBB4C7345E2859ED4E1E2DC9EDD", 0, IntPtr.Zero);
  196. }
  197. catch (Exception e)
  198. {
  199. Logger.WriteLineError($"Init set smart player sdk log path error:{e}");
  200. }
  201. try
  202. {
  203. var ret = Environment.Is64BitProcess
  204. ? NTSmartPublisherSDK_x64.NT_PB_Init(0, IntPtr.Zero)
  205. : NTSmartPublisherSDK_x86.NT_PB_Init(0, IntPtr.Zero);
  206. if (NTBaseCodeDefine.NT_ERC_OK != ret)
  207. {
  208. Logger.WriteLineError("Initialize SDK failed.");
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. Logger.WriteLineError($"Initialize SDK error:{e}");
  214. }
  215. }
  216. private bool StartPushCamera()
  217. {
  218. var noError = true;
  219. try
  220. {
  221. Logger.WriteLineInfo($"StartPushCamera begin");
  222. var micIndex = GetMicIndex(_pushConfiguration.AudioDevicePath);
  223. _pusher = new SuperDevicePusher(_pushConfiguration.PushUrl, _pushConfiguration.VideoDevicePath, micIndex, _pushConfiguration.AudioMode, _pushConfiguration.Resolution);
  224. _pusher.IsMute = _pushConfiguration.IsMute;
  225. _pusher.StatusChanged += OnStatusChanged;
  226. if (_pushConfiguration.EnablePreviewImage)
  227. {
  228. _pusher.EnablePreview = true;
  229. _pusher.PreviewImageReceived += OnPreviewImageReceived;
  230. }
  231. if (_pushConfiguration.ShouldPushData)
  232. {
  233. _pusher.Start();
  234. }
  235. Logger.WriteLineInfo($"StartPushCamera successfully");
  236. }
  237. catch (Exception e)
  238. {
  239. Logger.WriteLineError($"StartPushCamera ex:{e}");
  240. noError = false;
  241. }
  242. if (!noError)
  243. {
  244. return false;
  245. }
  246. return true;
  247. }
  248. private int GetMicIndex(string micId)
  249. {
  250. var micDevices = CrossPlatformHelper.Instance.DeviceService.GetMicDevices().ToList();
  251. AudioDeviceInfo audioDeviceInfo;
  252. if (string.IsNullOrEmpty(micId))
  253. {
  254. audioDeviceInfo = micDevices.FirstOrDefault();
  255. }
  256. else
  257. {
  258. audioDeviceInfo = micDevices.FirstOrDefault(c => c.Id == micId);
  259. }
  260. Logger.WriteLineInfo($"SystemMIC is :{audioDeviceInfo?.Id}");
  261. if (audioDeviceInfo == null)
  262. {
  263. return -1;
  264. }
  265. return micDevices.IndexOf(audioDeviceInfo);
  266. }
  267. /// <summary>
  268. /// update TerminalWidth TerminalHeight CameraWidth CameraHeight
  269. /// </summary>
  270. /// <param name="configuration"></param>
  271. public void UpdateResolution(CombineSmartPushConfiguration configuration)
  272. {
  273. InitSetSmartPusher(configuration.PushAppLogPath);
  274. var pusher = new CombineSmartPublisher(configuration.PushUrl, configuration.PushDataSourceType, 0, configuration.Resolution, configuration.CameraResolution, configuration.AudioMode, configuration.VideoDevicePath);
  275. var resolutions = pusher.GetDeviceResolutions();
  276. if (resolutions != null)
  277. {
  278. if (resolutions != null)
  279. {
  280. TerminalWidth = resolutions.TerminalWidth;
  281. TerminalHeight = resolutions.TerminalHeight;
  282. CameraWidth = resolutions.CameraWidth;
  283. CameraHeight = resolutions.CameraHeight;
  284. }
  285. }
  286. pusher.Dispose();
  287. }
  288. private void StartPush()
  289. {
  290. PushDataSourceType pushDataSourceType = _pushConfiguration.PushDataSourceType;
  291. ResolutionMode resolution = _pushConfiguration.Resolution;
  292. ResolutionMode cameraResolution = _pushConfiguration.CameraResolution;
  293. var micIndex = GetMicIndex(_pushConfiguration.AudioDevicePath);
  294. AudioMode audioMode = _pushConfiguration.AudioMode;
  295. Logger.WriteLineInfo($"StartPushScreen begin");
  296. try
  297. {
  298. if (_pusher != null)
  299. {
  300. _pusher.StatusChanged -= OnStatusChanged;
  301. _pusher.PreviewImageReceived -= OnPreviewImageReceived;
  302. _pusher.Dispose();
  303. }
  304. _pusher = new CombineSmartPublisher(_pushConfiguration.PushUrl, pushDataSourceType, micIndex, resolution, cameraResolution, audioMode, _pushConfiguration.VideoDevicePath);
  305. _pusher.IsMute = _pushConfiguration.IsMute;
  306. if (_pushConfiguration.Mode == PushMode.CombineTerminalGetResolution)
  307. {
  308. }
  309. _pusher.StatusChanged += OnStatusChanged;
  310. if (_pushConfiguration.EnablePreviewImage)
  311. {
  312. _pusher.EnablePreview = true;
  313. _pusher.PreviewImageReceived += OnPreviewImageReceived;
  314. }
  315. if (_pushConfiguration.ShouldPushData)
  316. {
  317. _pusher.Start();
  318. }
  319. Logger.WriteLineInfo("Start Push Screen and Camera Success");
  320. }
  321. catch (Exception e)
  322. {
  323. Logger.WriteLineError($"StartPushScreen ex:{e}");
  324. }
  325. }
  326. private void OnStatusChanged(object sender, LiveStatusData publisherStatusData)
  327. {
  328. Logger.WriteLineInfo($"SamrtPusher status changed:{publisherStatusData.Status}");
  329. StatusChanged?.Invoke(null, publisherStatusData.Status);
  330. }
  331. private void OnPreviewImageReceived(object sender, VideoFrameData e)
  332. {
  333. if (_pushConfiguration.Mode == PushMode.CombineTerminal)
  334. {
  335. var bitmap = VideoFrameDataProcessor.VideoFrameDataToBitmap(e);
  336. var resultions = ((CombineSmartPublisher)_pusher).GetDeviceResolutions();
  337. var cameraWidth = resultions.CameraWidth;
  338. var cameraHeight = resultions.CameraHeight;
  339. if (cameraHeight == 0 || cameraWidth == 0)
  340. {
  341. return;
  342. }
  343. else
  344. {
  345. var cameraData = VideoFrameDataProcessor.GetPartBitmapDataWithoutBitmapHeader(bitmap, new Rectangle(resultions.TerminalWidth, 0, cameraWidth, cameraHeight));
  346. PreviewCameraCapured?.Invoke(this, new VideoFrameData(cameraWidth, cameraHeight, cameraData));
  347. }
  348. }
  349. else
  350. {
  351. PreviewCameraCapured?.Invoke(this, e);
  352. }
  353. }
  354. private void CloseSmartPusher()
  355. {
  356. if (_pusher != null)
  357. {
  358. _pusher.PreviewImageReceived -= OnPreviewImageReceived;
  359. _pusher.Dispose();
  360. _pusher = null;
  361. }
  362. }
  363. }
  364. }