DFVideoControl.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using Microsoft.Xaml.Behaviors.Core;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. using InABox.Clients;
  14. namespace InABox.DynamicGrid
  15. {
  16. public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[], DFLayoutEmbeddedMediaValue>
  17. {
  18. private DFLayoutEmbeddedMediaValue _value = null!;
  19. private Button? _playButton;
  20. private Button? _pauseButton;
  21. private Button? _downloadButton;
  22. private MediaElement _player = null!;
  23. private Border? _border;
  24. private Image? _thumbnail;
  25. private Grid? _contentGrid;
  26. private Grid? _grid;
  27. /*Point center;
  28. int rotation = 90;
  29. bool firstPlay = true;*/
  30. private string _tempfilename = null!;
  31. protected override FrameworkElement Create()
  32. {
  33. _tempfilename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(),".mp4");
  34. _value = new DFLayoutEmbeddedMediaValue();
  35. _grid = new Grid();
  36. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  37. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  38. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  39. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  40. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  41. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  42. _border = new Border()
  43. {
  44. BorderBrush = new System.Windows.Media.SolidColorBrush(Colors.Gray),
  45. BorderThickness = new Thickness(0.75),
  46. Background = new System.Windows.Media.SolidColorBrush(Colors.White),
  47. Padding = new Thickness(4),
  48. Margin = new Thickness(0)
  49. };
  50. _border.SetGridPosition(0,0,5,1);
  51. _grid.Children.Add(_border);
  52. _contentGrid = new Grid();
  53. _contentGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  54. _contentGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  55. _border.Child = _contentGrid;
  56. _thumbnail = new Image
  57. {
  58. StretchDirection = StretchDirection.DownOnly,
  59. Source = EmbeddedImageUtilities.CreateEmptyImage()
  60. };
  61. _thumbnail.Visibility = Visibility.Visible;
  62. _thumbnail.SetGridPosition(0, 0, 4, 1);
  63. _contentGrid.Children.Add(_thumbnail);
  64. _player = new MediaElement
  65. {
  66. Stretch = System.Windows.Media.Stretch.Fill,
  67. Visibility = Visibility.Visible,
  68. VerticalAlignment = VerticalAlignment.Center,
  69. HorizontalAlignment = HorizontalAlignment.Center,
  70. LoadedBehavior = MediaState.Manual,
  71. Source = new Uri(_tempfilename)
  72. };
  73. _player.Visibility = Visibility.Collapsed;
  74. _player.MediaOpened += (sender, args) =>
  75. {
  76. _player.Visibility = Visibility.Visible;
  77. _thumbnail.Visibility = Visibility.Collapsed;
  78. };
  79. _player.MediaEnded += (sender, args) =>
  80. {
  81. _player.Visibility = Visibility.Collapsed;
  82. _thumbnail.Visibility = Visibility.Visible;
  83. _player.Close();
  84. };
  85. _player.SetGridPosition(0, 0, 4, 1);
  86. _contentGrid.Children.Add(_player);
  87. _playButton = new Button
  88. {
  89. Margin = new Thickness(5,0,0,0),
  90. Content = new Image { Source = Wpf.Resources.play_button.AsBitmapImage(24,24) },
  91. Command = new ActionCommand(PlayButton_Click),
  92. Width = 30,
  93. Height = 30
  94. };
  95. _playButton.SetGridPosition(0, 1);
  96. _grid.Children.Add(_playButton);
  97. _pauseButton = new Button
  98. {
  99. Margin = new Thickness(5,5,0,0),
  100. Content = new Image { Source = Wpf.Resources.pause_button.AsBitmapImage(32,32) },
  101. Command = new ActionCommand(PauseButton_Click),
  102. Width = 30,
  103. Height = 30
  104. };
  105. _pauseButton.SetGridPosition(1, 1);
  106. _grid.Children.Add(_pauseButton);
  107. _downloadButton = new Button
  108. {
  109. Margin = new Thickness(5,5,0,0),
  110. Content = new Image { Source = ImageUtils.SvgToBitmapImage(Wpf.Resources.download) },
  111. Command = new ActionCommand(DownloadButton_Click),
  112. Width = 30,
  113. Height = 30
  114. };
  115. _downloadButton.SetGridPosition(3, 1);
  116. _grid.Children.Add(_downloadButton);
  117. return _grid;
  118. }
  119. private void DownloadButton_Click()
  120. {
  121. var dlg = new SaveFileDialog();
  122. dlg.AddExtension = true;
  123. dlg.DefaultExt = ".mp4";
  124. dlg.ShowDialog();
  125. if (!string.IsNullOrWhiteSpace(dlg.FileName))
  126. {
  127. System.IO.File.WriteAllBytes(dlg.FileName, GetValue());
  128. MessageBox.Show("Success - file saved to: "
  129. + System.Environment.NewLine
  130. + dlg.FileName);
  131. var dir = System.IO.Path.GetDirectoryName(dlg.FileName);
  132. ProcessStartInfo startInfo = new ProcessStartInfo
  133. {
  134. Arguments = dir,
  135. FileName = "explorer.exe"
  136. };
  137. Process.Start(startInfo);
  138. }
  139. }
  140. private void PauseButton_Click()
  141. {
  142. _player.Pause();
  143. }
  144. private async Task LoadVideo()
  145. {
  146. if (!File.Exists(_tempfilename))
  147. {
  148. _value.Data ??= await DigitalFormDocumentFactory.LoadDocumentAsync(_value.ID);
  149. File.WriteAllBytes(_tempfilename, _value.Data);
  150. }
  151. }
  152. private void PlayButton_Click()
  153. {
  154. LoadVideo().ContinueWith(task => _player.Play(), TaskScheduler.FromCurrentSynchronizationContext());
  155. }
  156. public override DFLayoutEmbeddedMediaValue GetSerializedValue()
  157. {
  158. if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
  159. _value.ID = DigitalFormDocumentFactory.SaveDocument(_value.Data);
  160. return _value;
  161. }
  162. public override void SetSerializedValue(DFLayoutEmbeddedMediaValue value)
  163. {
  164. _value = value;
  165. EmbeddedImageUtilities.LoadImageFromData(_thumbnail, _value.Thumbnail);
  166. }
  167. public override byte[] GetValue()
  168. {
  169. Task.Run(async () => await LoadVideo().ConfigureAwait(false))
  170. .Wait();
  171. return _value.Data ?? Array.Empty<byte>();
  172. }
  173. public override void SetValue(byte[]? value)
  174. {
  175. SetSerializedValue(new() { Data = value });
  176. }
  177. protected override bool IsEmpty() => _value.Data is null || _value.Data.Length == 0;
  178. }
  179. }