123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- using Android.App;
- using Android.Content.Res;
- using System;
- using System.Linq;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
- namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTMP
- {
- public class RtmpPusher : ILiveVideoPusher
- {
- private SmartPublisher _pusher;
- private CPCombineSmartPushConfiguration _pushConfiguration;
- private int _screenWidth;
- private int _screenHeight;
- private Action<EnumLiveStates> _updateState;
- /// <summary>
- /// Preview local camera capured changed, Rtmp unused
- /// </summary>
- public event EventHandler<CPVideoFrameData> 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<EnumLiveStatus> 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<EnumLiveStates> updateState)
- {
- _updateState = updateState;
- var orientation = Application.Context.Resources.Configuration.Orientation;
- switch (orientation)
- {
- case Orientation.Landscape:
- _screenWidth = Application.Context.Resources.DisplayMetrics.WidthPixels;
- _screenHeight = Application.Context.Resources.DisplayMetrics.HeightPixels;
- break;
- case Orientation.Portrait:
- _screenWidth = Application.Context.Resources.DisplayMetrics.HeightPixels;
- _screenHeight = Application.Context.Resources.DisplayMetrics.WidthPixels;
- break;
- default:
- var screenWidth = Application.Context.Resources.DisplayMetrics.WidthPixels;
- var screenHeight = Application.Context.Resources.DisplayMetrics.HeightPixels;
- if (screenHeight > screenWidth)
- {
- _screenWidth = screenHeight;
- _screenHeight = screenWidth;
- }
- else
- {
- _screenHeight = screenHeight;
- _screenWidth = screenWidth;
- }
- break;
- }
- }
- public bool Start(CPCombineSmartPushConfiguration configuration)
- {
- try
- {
- _pushConfiguration = configuration;
- UpdateResolution(_pushConfiguration);
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Started rtmp pusher: {configuration}");
- if (_pushConfiguration.ShouldPushData || _pushConfiguration.ShowPreviewImage)
- {
- DoPush();
- }
- if (_pushConfiguration.ShouldPushData)
- {
- _updateState?.Invoke(EnumLiveStates.TerminalIsPushing);
- }
- return true;
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpPusher: start failed ex:{ex}");
- }
- return true;
- }
- public bool Stop()
- {
- try
- {
- if (_pushConfiguration != null)
- {
- if (!_pushConfiguration.ShouldPushData && _pushConfiguration.ShowPreviewImage)//本地预览
- {
- CloseSmartPusher();
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"Restart smart pusher");
- DoPush();
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtmpPusher: stop and only show preview to restart push");
- return true;
- }
- else
- {
- try
- {
- CloseSmartPusher();
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RtmpPusher: Dispose");
- return true;
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpPusher: stop failed to kill ex:{ex}");
- }
- }
- }
- }
- catch (Exception e)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"RtmpPusher: stop failed ex:{e}");
- }
- finally
- {
- LiveNotification?.Invoke(this, new LiveNotificationArgs(false, TerminalWidth, TerminalHeight));
- }
- 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
- {
- if (_pushConfiguration.ShouldPushData)//如果正在推流
- {
- _pusher.PreviewImageReceived -= OnPreviewImageReceived;
- }
- else
- {
- Stop();
- }
- }
- }
- 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 EnumPushMode.Screen:
- StartPushScreen();
- break;
- case EnumPushMode.Camera when !string.IsNullOrEmpty(_pushConfiguration.VideoDevicePath):
- StartPushCamera();
- break;
- case EnumPushMode.CombineTerminal:
- case EnumPushMode.CombineTerminalGetResolution:
- StartPush();
- break;
- }
- }
- private bool StartPushScreen()
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"StartPushScreen begin");
- var noError = true;
- try
- {
- CloseSmartPusher();
- _pusher = new SuperScreenPublisher(_pushConfiguration.PushUrl, _updateState);
- _pusher.IsMute = _pushConfiguration.IsMute;
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- LiveNotification?.Invoke(this, new LiveNotificationArgs(true, TerminalWidth, TerminalHeight));
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"StartPushScreen successfully");
- }
- catch (Exception e)
- {
- noError = false;
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"StartPushScreen ex:{e}");
- }
- if (!noError)
- {
- return false;
- }
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo("Start Push Screen Success");
- return true;
- }
- private bool StartPushCamera()
- {
- var noError = true;
- try
- {
- CloseSmartPusher();
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"StartPushCamera begin");
- _pusher = new SuperDevicePublisher(_pushConfiguration.VideoDevicePath, _pushConfiguration.PushUrl, _updateState);
- _pusher.IsMute = _pushConfiguration.IsMute;
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.EnablePreviewImage)
- {
- _pusher.EnablePreview = true;
- _pusher.PreviewImageReceived += OnPreviewImageReceived;
- }
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- else if (_pushConfiguration.ShowPreviewImage)
- {
- _pusher.StartOnlyPreview();
- }
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"StartPushCamera successfully");
- }
- catch (Exception e)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"StartPushCamera ex:{e}");
- noError = false;
- }
- if (!noError)
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// update TerminalWidth TerminalHeight CameraWidth CameraHeight
- /// </summary>
- /// <param name="configuration"></param>
- public void UpdateResolution(CPCombineSmartPushConfiguration configuration)
- {
- var pusher = new CombineSmartPublisher(configuration.PushDataSourceType, configuration.VideoDevicePath, configuration.PushUrl, null);
- 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()
- {
- EnumPushDataSourceType pushDataSourceType = _pushConfiguration.PushDataSourceType;
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"StartPushCombine begin");
- try
- {
- CloseSmartPusher();
- _pusher = new CombineSmartPublisher(pushDataSourceType, _pushConfiguration.VideoDevicePath, _pushConfiguration.PushUrl, _updateState);
- _pusher.IsMute = _pushConfiguration.IsMute;
- _pusher.StatusChanged += OnStatusChanged;
- if (_pushConfiguration.EnablePreviewImage)
- {
- _pusher.EnablePreview = true;
- _pusher.PreviewImageReceived += OnPreviewImageReceived;
- }
- if (_pushConfiguration.ShouldPushData)
- {
- _pusher.Start();
- }
- else if (_pushConfiguration.ShowPreviewImage)
- {
- _pusher.StartOnlyPreview();
- }
- LiveNotification?.Invoke(this, new LiveNotificationArgs(true, TerminalWidth, TerminalHeight));
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo("Start Push Screen and Camera Success");
- }
- catch (Exception e)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"StartPushScreen ex:{e}");
- }
- }
- private void OnStatusChanged(object sender, CPLiveStatusData publisherStatusData)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"SamrtPusher status changed:{publisherStatusData.Status}");
- StatusChanged?.Invoke(this, publisherStatusData.Status);
- }
- private void OnPreviewImageReceived(object sender, CPVideoFrameData e)
- {
- PreviewCameraCapured?.Invoke(this, e);
- }
- private void CloseSmartPusher()
- {
- if (_pusher != null)
- {
- _pusher.PreviewImageReceived -= OnPreviewImageReceived;
- _pusher.StatusChanged -= OnStatusChanged;
- _pusher.Dispose();
- _pusher = null;
- }
- }
- }
- }
|