123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using NT;
- using System;
- using System.Drawing;
- using System.Linq;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS;
- using Vinno.vCloud.Common.FIS.Device;
- using Vinno.vCloud.Common.FIS.LiveVideos;
- using Vinno.vCloud.FIS.Windows.RTMP;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Push.Configuration;
- namespace Vinno.vCloud.FIS.Windows
- {
- public class RtmpPusher : IPusher
- {
- private SmartPublisher _pusher;
- private CombineSmartPushConfiguration _pushConfiguration;
- private Action<LiveStates> _updateState;
- /// <summary>
- /// Preview local camera capured changed, Rtmp unused
- /// </summary>
- public event EventHandler<VideoFrameData> PreviewCameraCapured;
- /// <summary>
- /// The event to notificate the US to show or hide the live video out put window
- /// </summary>
- public event EventHandler<LiveNotificationArgs> LiveNotification;
- /// <summary>
- /// The event to notificate the Live Status Changed.
- /// Only used for RTMP pusher.
- /// </summary>
- public event EventHandler<LiveStatus> StatusChanged;
- public int TerminalWidth { get; private set; }
- public int TerminalHeight { get; private set; }
- public int CameraWidth { get; private set; }
- public int CameraHeight { get; private set; }
- public RtmpPusher(Action<LiveStates> updateState)
- {
- _updateState = updateState;
- }
- public bool Start(CombineSmartPushConfiguration configuration)
- {
- try
- {
- _pushConfiguration = configuration;
- Logger.WriteLineInfo($"Started rtmp pusher: {configuration}");
- InitSetSmartPusher(configuration.PushAppLogPath);
- DoPush();
- if (_pushConfiguration.ShouldPushData)
- {
- _updateState?.Invoke(LiveStates.TerminalIsPushing);
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"RtmpPusher: start failed ex:{ex}");
- }
- return true;
- }
- public bool Stop()
- {
- try
- {
- if (_pushConfiguration != null)
- {
- if (!_pushConfiguration.ShouldPushData && _pushConfiguration.ShowPreviewImage)//本地预览
- {
- var push = _pusher as CombineSmartPublisher;
- if (push != null)
- {
- CloseSmartPusher();
- Logger.WriteLineInfo($"Restart smart pusher");
- }
- DoPush();
- Logger.WriteLineInfo($"RtmpPusher: stop and only show preview to restart push");
- return true;
- }
- else
- {
- try
- {
- CloseSmartPusher();
- Logger.WriteLineInfo($"RtmpPusher: Dispose");
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"RtmpPusher: stop failed to kill ex:{ex}");
- }
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"RtmpPusher: stop failed ex:{e}");
- }
- return false;
- }
- /// <summary>
- /// set push mute or ummute
- /// </summary>
- /// <param name="isMute"></param>
- public void SetMute(bool isMute)
- {
- if (_pusher != null)
- {
- _pusher.IsMute = isMute;
- }
- }
- public void SetPreviewCamera(bool isShowPreview)
- {
- if (isShowPreview)
- {
- _pusher.PreviewImageReceived += OnPreviewImageReceived;
- }
- else
- {
- _pusher.PreviewImageReceived -= OnPreviewImageReceived;
- }
- }
- public void ChangeCamera(string cameraId)
- {
- if (_pusher != null && !_pushConfiguration.ShouldPushData)
- {
- Stop();
- }
- }
- public void ChangeMic(string micId)
- {
- if (_pusher != null && !_pushConfiguration.ShouldPushData)
- {
- Stop();
- }
- }
- public bool StartSpeedTest(uint appId, string userid, string userSign)
- {
- // throw new NotImplementedException();
- return true;
- }
- public void StopSpeedTest()
- {
- // throw new NotImplementedException();
- }
- private void DoPush()
- {
- switch (_pushConfiguration.Mode)
- {
- case PushMode.Screen:
- StartPushScreen();
- break;
- case PushMode.Camera when !string.IsNullOrEmpty(_pushConfiguration.VideoDevicePath):
- StartPushCamera();
- break;
- case PushMode.CombineTerminal:
- case PushMode.CombineTerminalGetResolution:
- StartPush();
- break;
- }
- }
- private bool StartPushScreen()
- {
- Logger.WriteLineInfo($"StartPushScreen begin");
- var noError = true;
- try
- {
- _pusher = new SuperScreenPusher(_pushConfiguration.PushUrl, -1, _pushConfiguration.Resolution);
- _pusher.IsMute = _pushConfiguration.IsMute;
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- Logger.WriteLineInfo($"StartPushScreen successfully");
- }
- catch (Exception e)
- {
- noError = false;
- Logger.WriteLineError($"StartPushScreen ex:{e}");
- }
- if (!noError)
- {
- return false;
- }
- Logger.WriteLineInfo("Start Push Screen Success");
- return true;
- }
- private void InitSetSmartPusher(string logPath)
- {
- try
- {
- var ret = Environment.Is64BitProcess ?
- NTSmartLog_x64.NT_SL_SetPath(logPath) : NTSmartLog_x86.NT_SL_SetPath(logPath);
- ret = Environment.Is64BitProcess
- ? NTSmartPublisherSDK_x64.NT_PB_SetSDKClientKey("03396870E2CA4E4F98593EC368EE77ED", "9ACF4FBED7EF4E56833941DE29267C9F79130CBB4C7345E2859ED4E1E2DC9EDD", 0, IntPtr.Zero) :
- NTSmartPublisherSDK_x86.NT_PB_SetSDKClientKey("03396870E2CA4E4F98593EC368EE77ED", "9ACF4FBED7EF4E56833941DE29267C9F79130CBB4C7345E2859ED4E1E2DC9EDD", 0, IntPtr.Zero);
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Init set smart player sdk log path error:{e}");
- }
- try
- {
- var ret = Environment.Is64BitProcess
- ? NTSmartPublisherSDK_x64.NT_PB_Init(0, IntPtr.Zero)
- : NTSmartPublisherSDK_x86.NT_PB_Init(0, IntPtr.Zero);
- if (NTBaseCodeDefine.NT_ERC_OK != ret)
- {
- Logger.WriteLineError("Initialize SDK failed.");
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Initialize SDK error:{e}");
- }
- }
- private bool StartPushCamera()
- {
- var noError = true;
- try
- {
- Logger.WriteLineInfo($"StartPushCamera begin");
- var micIndex = GetMicIndex(_pushConfiguration.AudioDevicePath);
- _pusher = new SuperDevicePusher(_pushConfiguration.PushUrl, _pushConfiguration.VideoDevicePath, micIndex, _pushConfiguration.AudioMode, _pushConfiguration.Resolution);
- _pusher.IsMute = _pushConfiguration.IsMute;
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.EnablePreviewImage)
- {
- _pusher.EnablePreview = true;
- _pusher.PreviewImageReceived += OnPreviewImageReceived;
- }
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- Logger.WriteLineInfo($"StartPushCamera successfully");
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"StartPushCamera ex:{e}");
- noError = false;
- }
- if (!noError)
- {
- return false;
- }
- return true;
- }
- private int GetMicIndex(string micId)
- {
- var micDevices = CrossPlatformHelper.Instance.DeviceService.GetMicDevices().ToList();
- AudioDeviceInfo audioDeviceInfo;
- if (string.IsNullOrEmpty(micId))
- {
- audioDeviceInfo = micDevices.FirstOrDefault();
- }
- else
- {
- audioDeviceInfo = micDevices.FirstOrDefault(c => c.Id == micId);
- }
- Logger.WriteLineInfo($"SystemMIC is :{audioDeviceInfo?.Id}");
- if (audioDeviceInfo == null)
- {
- return -1;
- }
- return micDevices.IndexOf(audioDeviceInfo);
- }
- /// <summary>
- /// update TerminalWidth TerminalHeight CameraWidth CameraHeight
- /// </summary>
- /// <param name="configuration"></param>
- public void UpdateResolution(CombineSmartPushConfiguration configuration)
- {
- InitSetSmartPusher(configuration.PushAppLogPath);
- var pusher = new CombineSmartPublisher(configuration.PushUrl, configuration.PushDataSourceType, 0, configuration.Resolution, configuration.CameraResolution, configuration.AudioMode, configuration.VideoDevicePath);
- var resolutions = pusher.GetDeviceResolutions();
- if (resolutions != null)
- {
- if (resolutions != null)
- {
- TerminalWidth = resolutions.TerminalWidth;
- TerminalHeight = resolutions.TerminalHeight;
- CameraWidth = resolutions.CameraWidth;
- CameraHeight = resolutions.CameraHeight;
- }
- }
- pusher.Dispose();
- }
- private void StartPush()
- {
- PushDataSourceType pushDataSourceType = _pushConfiguration.PushDataSourceType;
- ResolutionMode resolution = _pushConfiguration.Resolution;
- ResolutionMode cameraResolution = _pushConfiguration.CameraResolution;
- var micIndex = GetMicIndex(_pushConfiguration.AudioDevicePath);
- AudioMode audioMode = _pushConfiguration.AudioMode;
- Logger.WriteLineInfo($"StartPushScreen begin");
- try
- {
- if (_pusher != null)
- {
- _pusher.StatusChanged -= OnStatusChanged;
- _pusher.PreviewImageReceived -= OnPreviewImageReceived;
- _pusher.Dispose();
- }
- _pusher = new CombineSmartPublisher(_pushConfiguration.PushUrl, pushDataSourceType, micIndex, resolution, cameraResolution, audioMode, _pushConfiguration.VideoDevicePath);
- _pusher.IsMute = _pushConfiguration.IsMute;
- if (_pushConfiguration.Mode == PushMode.CombineTerminalGetResolution)
- {
- }
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.EnablePreviewImage)
- {
- _pusher.EnablePreview = true;
- _pusher.PreviewImageReceived += OnPreviewImageReceived;
- }
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- Logger.WriteLineInfo("Start Push Screen and Camera Success");
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"StartPushScreen ex:{e}");
- }
- }
- private void OnStatusChanged(object sender, LiveStatusData publisherStatusData)
- {
- Logger.WriteLineInfo($"SamrtPusher status changed:{publisherStatusData.Status}");
- StatusChanged?.Invoke(null, publisherStatusData.Status);
- }
- private void OnPreviewImageReceived(object sender, VideoFrameData e)
- {
- if (_pushConfiguration.Mode == PushMode.CombineTerminal)
- {
- var bitmap = VideoFrameDataProcessor.VideoFrameDataToBitmap(e);
- var resultions = ((CombineSmartPublisher)_pusher).GetDeviceResolutions();
- var cameraWidth = resultions.CameraWidth;
- var cameraHeight = resultions.CameraHeight;
- if (cameraHeight == 0 || cameraWidth == 0)
- {
- return;
- }
- else
- {
- var cameraData = VideoFrameDataProcessor.GetPartBitmapDataWithoutBitmapHeader(bitmap, new Rectangle(resultions.TerminalWidth, 0, cameraWidth, cameraHeight));
- PreviewCameraCapured?.Invoke(this, new VideoFrameData(cameraWidth, cameraHeight, cameraData));
- }
- }
- else
- {
- PreviewCameraCapured?.Invoke(this, e);
- }
- }
- private void CloseSmartPusher()
- {
- if (_pusher != null)
- {
- _pusher.PreviewImageReceived -= OnPreviewImageReceived;
- _pusher.Dispose();
- _pusher = null;
- }
- }
- }
- }
|