-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
758 lines (680 loc) · 29.5 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using GlobalStructures;
using DXGI;
using System.Threading.Tasks;
using System.ComponentModel;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUI3_MediaEngine
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
private IntPtr hWnd = IntPtr.Zero;
private Microsoft.UI.Windowing.AppWindow _apw;
private Microsoft.UI.Windowing.OverlappedPresenter _presenter;
CMediaEngine mediaEngine;
private System.Collections.ObjectModel.ObservableCollection<string> listURLs = new System.Collections.ObjectModel.ObservableCollection<string>();
private string sURLListFile = "URL.xaml";
private string sSubtitlesURL = string.Empty;
// private string sVideoURL = "E:\\big_buck_bunny.asf";
// Streams Radios FR
// "https://streaming.radio.funradio.fr/fun-1-44-128"
// "http://streamingp.shoutcast.com/NostalgiePremium-mp3"
// "http://cdn.nrjaudio.fm/audio1/fr/30001/mp3_128.mp3?origine=fluxradios"
// "http://streaming.radio.rtl.fr/rtl-1-44-128?listen=webCwsBCggNCQgLDQUGBAcGBg"
//// http://bbb3d.renderfarming.net/download.html
// "http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4"
private string sVideoURL = "http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)";
// 0xC00D36C4 MF_E_UNSUPPORTED_BYTESTREAM_TYPE
// 0x8001011F
// "https://www.youtube.com/watch?v=aqz-KE-bpKQ"
// "https://www.youtube.com/watch?v=36YnV9STBqc"
// "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"
// "https://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/multiangle-right.mp4"
// "https://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary-srt_eng-srt_por-srt_swe.mkv"
// 0xC00D36C4 MF_E_UNSUPPORTED_BYTESTREAM_TYPE
// http://47.51.131.147/-wvhttp-01-/GetOneShot?image_size=1280x720&frame_count=1000000000
// http://camera.butovo.com/mjpg/video.mjpg
// http://77.222.181.11:8080/mjpg/video.mjpg
public MainWindow()
{
this.InitializeComponent();
hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Microsoft.UI.WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
_apw = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(myWndId);
_apw.Closing += _apw_Closing;
_presenter = _apw.Presenter as Microsoft.UI.Windowing.OverlappedPresenter;
//_presenter.IsResizable = false;
//_presenter.SetBorderAndTitleBar(false, false);
Application.Current.Resources["ButtonBackgroundPressed"] = new SolidColorBrush(Microsoft.UI.Colors.RoyalBlue);
Application.Current.Resources["ToggleSwitchFillOn"] = new SolidColorBrush(Microsoft.UI.Colors.LightBlue);
Application.Current.Resources["ToggleSwitchFillOnPointerOver"] = new SolidColorBrush(Microsoft.UI.Colors.LightBlue);
Application.Current.Resources["ToggleSwitchFillOnPressed"] = new SolidColorBrush(Microsoft.UI.Colors.LightBlue);
HRESULT hr = HRESULT.S_OK;
mediaEngine = new CMediaEngine();
// hr = mediaEngine.Initialize(hWnd, CMediaEngine.ME_MODE.MODE_AUDIO, ctrlVideo, Microsoft.UI.Colors.Black);
hr = mediaEngine.Initialize(hWnd, CMediaEngine.ME_MODE.MODE_FRAME_SERVER, ctrlVideo, Microsoft.UI.Colors.Black);
//FrameworkElement fe = (FrameworkElement)this.Content;
//fe.RequestedTheme = ElementTheme.Default;
//Microsoft.UI.Xaml.Application.Current.RequestedTheme = ApplicationTheme.Light;
}
private void _apw_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
{
var popups = VisualTreeHelper.GetOpenPopupsForXamlRoot(this.Content.XamlRoot);
foreach (var popup in popups)
{
if (popup.Name == "SuggestionsPopup")
{
popup.IsOpen = false;
}
}
if (mediaEngine != null)
mediaEngine.Dispose();
}
bool bOpen = false;
private async void btnLoadURL_Click(object sender, RoutedEventArgs e)
{
if (!bOpen)
{
await LoadURLListAsync();
OpenURL();
}
}
private async void OpenURL()
{
bOpen = true;
mediaEngine.SetOpacity(10);
StackPanel sp = new StackPanel();
AutoSuggestBox asb = new AutoSuggestBox()
{
Width = 900,
MaxSuggestionListHeight = 300,
ItemsSource = listURLs,
};
ScrollViewer sv = new ScrollViewer()
{
Width = 400,
Height = 40,
Margin = new Thickness(0, 10, 0, 0),
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
};
sv.Content = asb;
asb.Loaded += Asb_Loaded;
asb.TextChanged += Asb_TextChanged;
sp.Children.Add(sv);
ContentDialog cd = new ContentDialog()
{
Title = "Network URL",
Content = sp,
PrimaryButtonText = "Load",
CloseButtonText = "Cancel"
};
cd.XamlRoot = this.Content.XamlRoot;
var cdResult = await cd.ShowAsync();
mediaEngine.SetOpacity(100);
bOpen = false;
if (cdResult == ContentDialogResult.Primary)
{
if (!listURLs.Contains(asb.Text))
{
listURLs.Add(asb.Text);
await SaveURLListAsync();
}
HRESULT hr = HRESULT.S_OK;
hr = mediaEngine.LoadURL(asb.Text);
}
}
// Copied from WinUI 3 Gallery
private void Asb_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// Since selecting an item will also change the text,
// only listen to changes caused by user entering text.
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
var suitableItems = new List<string>();
var splitText = sender.Text.ToLower().Split(" ");
foreach (var url in listURLs)
{
var found = splitText.All((key) =>
{
return url.ToLower().Contains(key);
});
if (found)
{
suitableItems.Add(url);
}
}
if (suitableItems.Count == 0)
{
//suitableItems.Add("No results found");
}
sender.ItemsSource = suitableItems;
}
}
private void Asb_Loaded(object sender, RoutedEventArgs e)
{
((AutoSuggestBox)sender).IsSuggestionListOpen = true;
}
//https://docs.microsoft.com/en-us/windows/win32/medfound/supported-media-formats-in-media-foundation
private async Task<string> OpenMediaDialog()
{
var fop = new Windows.Storage.Pickers.FileOpenPicker();
WinRT.Interop.InitializeWithWindow.Initialize(fop, hWnd);
fop.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
fop.FileTypeFilter.Add(".3gp");
fop.FileTypeFilter.Add(".3g2");
fop.FileTypeFilter.Add(".asf");
fop.FileTypeFilter.Add(".avi");
fop.FileTypeFilter.Add(".m4v");
fop.FileTypeFilter.Add(".mkv");
fop.FileTypeFilter.Add(".mov"); // 0xC00D5212 MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED
fop.FileTypeFilter.Add(".mp4");
fop.FileTypeFilter.Add(".wmv");
//fop.FileTypeFilter.Add(".mts"); // 0xC004F011 SL_E_LICENSE_FILE_NOT_INSTALLED
//fop.FileTypeFilter.Add(".flv"); // 0xC00D36C4 MF_E_UNSUPPORTED_BYTESTREAM_TYPE
//fop.FileTypeFilter.Add(".vob"); // 0xC004F011 SL_E_LICENSE_FILE_NOT_INSTALLED
//fop.FileTypeFilter.Add(".mpeg"); // 0xC004F011 SL_E_LICENSE_FILE_NOT_INSTALLED
//fop.FileTypeFilter.Add(".mpg"); // 0xC004F011 SL_E_LICENSE_FILE_NOT_INSTALLED
fop.FileTypeFilter.Add(".aac");
fop.FileTypeFilter.Add(".flac");
fop.FileTypeFilter.Add(".m4a");
fop.FileTypeFilter.Add(".mp3");
fop.FileTypeFilter.Add(".ogg");
fop.FileTypeFilter.Add(".wav");
fop.FileTypeFilter.Add(".wma");
var file = await fop.PickSingleFileAsync();
return (file != null ? file.Path : string.Empty);
}
private async void btnLoadFile_Click(object sender, RoutedEventArgs e)
{
HRESULT hr = HRESULT.S_OK;
if (!bOpen)
{
bOpen = true;
string sVideoFile = await OpenMediaDialog();
if (sVideoFile != string.Empty)
{
hr = mediaEngine.LoadURL(sVideoFile);
}
bOpen = false;
}
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
HRESULT hr = HRESULT.S_OK;
hr = mediaEngine.Stop(true);
}
private void btnOverlay_Click(object sender, RoutedEventArgs e)
{
//if (mediaEngine.HasVideo())
{
string sDirectory = AppDomain.CurrentDomain.BaseDirectory;
string sImageFile = sDirectory + @"Assets\Butterfly.png";
if (!mediaEngine.GetImageOverlay())
{
mediaEngine.SetImageOverlay(true, sImageFile);
btnOverlay.Content = "Remove Overlay";
}
else
{
mediaEngine.SetImageOverlay(false, null);
btnOverlay.Content = "Test Overlay";
}
}
//else
//{
// Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("The media must have Video", "Information");
// WinRT.Interop.InitializeWithWindow.Initialize(md, hWnd);
// _ = await md.ShowAsync();
//}
}
private async Task LoadURLListAsync()
{
string sDirectory = AppDomain.CurrentDomain.BaseDirectory;
string sFile = sDirectory + sURLListFile;
Windows.Storage.StorageFile file = null;
bool bFileExists = true;
try
{
//Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(sFile);
file = await Windows.Storage.StorageFile.GetFileFromPathAsync(sFile);
}
catch (System.IO.FileNotFoundException ex1)
{
bFileExists = false;
}
catch (Exception ex2)
{
//...
}
if (bFileExists)
{
using (Windows.Storage.Streams.IInputStream inputStream = await file.OpenSequentialReadAsync())
{
System.Runtime.Serialization.DataContractSerializer serializer =
new System.Runtime.Serialization.DataContractSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<string>));
listURLs = (System.Collections.ObjectModel.ObservableCollection<string>)serializer.ReadObject(inputStream.AsStreamForRead());
}
}
else
{
listURLs.Add(sVideoURL);
await SaveURLListAsync();
}
}
private async Task SaveURLListAsync()
{
string sDirectory = AppDomain.CurrentDomain.BaseDirectory;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Runtime.Serialization.DataContractSerializer serializer = new
System.Runtime.Serialization.DataContractSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<string>));
serializer.WriteObject(ms, listURLs);
Windows.Storage.StorageFolder folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(sDirectory);
var destFile = await folder.CreateFileAsync(sURLListFile, Windows.Storage.CreationCollisionOption.ReplaceExisting);
using (System.IO.Stream fileStream = await destFile.OpenStreamForWriteAsync())
{
ms.Seek(0, System.IO.SeekOrigin.Begin);
await ms.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
private void tsMirror_Toggled(object sender, RoutedEventArgs e)
{
ToggleSwitch ts = sender as ToggleSwitch;
if (mediaEngine != null)
mediaEngine.EnableHorizontalMirrorMode(ts.IsOn);
}
private void tsSubtitles_Toggled(object sender, RoutedEventArgs e)
{
ToggleSwitch ts = sender as ToggleSwitch;
btnSubtitles.Visibility = ts.IsOn ? Visibility.Visible : Visibility.Collapsed;
if (mediaEngine != null)
mediaEngine.SetSubtitles(ts.IsOn ? true : false, ts.IsOn ? sSubtitlesURL:"");
}
private string sSubtitlesURLOld = string.Empty;
private void btnSubtitles_Click(object sender, RoutedEventArgs e)
{
sSubtitlesURLOld = sSubtitlesURL;
SubTitlesDialog();
}
private async void SubTitlesDialog()
{
StackPanel sp = new StackPanel();
Button btnAddSubtitle = new Button()
{
Content = "Add subtitles file",
Width = 140,
Height = 40,
Margin = new Thickness(0, 10, 0, 0)
};
ScrollViewer sv = new ScrollViewer()
{
Width = 400,
Height = 30,
Margin = new Thickness(0, 10, 0, 0),
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
};
TextBlock tb = new TextBlock()
{
IsTextSelectionEnabled = true
};
tb.Text = sSubtitlesURL;
sv.Content = tb;
btnAddSubtitle.Click += async (sender, e) =>
{
string sSubtitleFile = await OpenSubtitleDialog();
if (sSubtitleFile != string.Empty)
{
tb.Text = sSubtitleFile;
}
};
sp.Children.Add(btnAddSubtitle);
sp.Children.Add(sv);
ContentDialog cd = new ContentDialog()
{
Title = "Subtitles",
Content = sp,
PrimaryButtonText = "Ok",
CloseButtonText = "Cancel"
};
cd.XamlRoot = this.Content.XamlRoot;
var cdResult = await cd.ShowAsync();
if (cdResult == ContentDialogResult.Primary)
{
if (tb.Text != string.Empty)
{
sSubtitlesURL = tb.Text;
mediaEngine.SetSubtitles(true, sSubtitlesURL);
btnSubtitles.Background = new SolidColorBrush(Microsoft.UI.Colors.ForestGreen);
if (mediaEngine.IsPlaying() && mediaEngine.HasVideo())
{
Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("You must reload the media to apply new subtitles file", "Information");
WinRT.Interop.InitializeWithWindow.Initialize(md, hWnd);
_ = await md.ShowAsync();
}
}
else
{
btnSubtitles.Background = new SolidColorBrush(Microsoft.UI.Colors.IndianRed);
}
}
else
{
sSubtitlesURL = sSubtitlesURLOld;
}
}
//private async void SubTitlesDialog()
//{
// StackPanel sp = new StackPanel();
// ListBox lbSubtitles = new ListBox()
// {
// Name = "lbSubtitles",
// Width = 500,
// Height = 400,
// HorizontalContentAlignment = HorizontalAlignment.Stretch,
// ItemsSource = listSubtitles
// };
// ScrollViewer.SetHorizontalScrollMode(lbSubtitles, ScrollMode.Enabled);
// ScrollViewer.SetVerticalScrollMode(lbSubtitles, ScrollMode.Enabled);
// ScrollViewer.SetHorizontalScrollBarVisibility(lbSubtitles, ScrollBarVisibility.Auto);
// ScrollViewer.SetVerticalScrollBarVisibility(lbSubtitles, ScrollBarVisibility.Auto);
// Button btnAddSubtitle = new Button()
// {
// Content = "Add subtitle file",
// Width = 140,
// Height = 40,
// Margin = new Thickness(0, 10, 0, 0)
// };
// StackPanel sp2 = new StackPanel();
// sp2.Orientation = Orientation.Horizontal;
// btnAddSubtitle.Click += async (sender, e) =>
// {
// string sSubtitleFile = await OpenSubtitleDialog();
// if (sSubtitleFile != string.Empty)
// {
// if (!listSubtitles.Contains(sSubtitleFile))
// {
// listSubtitles.Add(sSubtitleFile);
// }
// else
// {
// Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("Subtitle file already in the list !", "Information");
// WinRT.Interop.InitializeWithWindow.Initialize(md, hWnd);
// _ = await md.ShowAsync();
// }
// }
// };
// Button btnRemoveSubtitle = new Button()
// {
// Content = "Remove subtitle",
// Width = 140,
// Height = 40,
// Margin = new Thickness(30, 10, 0, 0)
// };
// lbSubtitles.SelectionChanged += (sender, e) =>
// {
// if (lbSubtitles.SelectedItems.Count > 0)
// btnRemoveSubtitle.IsEnabled = true;
// else
// btnRemoveSubtitle.IsEnabled = false;
// };
// lbSubtitles.Loaded += (sender, e) =>
// {
// if (lbSubtitles.SelectedItems.Count > 0)
// btnRemoveSubtitle.IsEnabled = true;
// else
// btnRemoveSubtitle.IsEnabled = false;
// };
// btnRemoveSubtitle.Click += (sender, e) =>
// {
// if (lbSubtitles.SelectedItem != null)
// {
// listSubtitles.Remove(lbSubtitles.SelectedItem.ToString());
// }
// };
// // IsEnabled = "{Binding ElementName=lbSubtitles, Path=SelectedItems.Count}"
// //Binding bind = new Binding();
// //bind.Mode = BindingMode.OneWay;
// //bind.ElementName = "lbSubtitles";
// ////bind.Source = lbSubtitles;
// //bind.Path = new PropertyPath("SelectedItems.Count");
// //bind.Converter = new IntToBoolConverter();
// //btnRemoveSubtitle.SetBinding(Button.IsEnabledProperty, bind);
// sp.Children.Add(lbSubtitles);
// sp2.Children.Add(btnAddSubtitle);
// sp2.Children.Add(btnRemoveSubtitle);
// sp.Children.Add(sp2);
// ContentDialog cd = new ContentDialog()
// {
// Title = "Subtitles",
// Content = sp,
// PrimaryButtonText = "Ok",
// CloseButtonText = "Cancel"
// };
// cd.XamlRoot = this.Content.XamlRoot;
// var cdResult = await cd.ShowAsync();
// if (cdResult == ContentDialogResult.Primary)
// {
// HRESULT hr = HRESULT.S_OK;
// foreach (var item in lbSubtitles.Items)
// {
// int nTrackId = mediaEngine.FindTrackFromLabel(item.ToString());
// if (nTrackId == -1)
// {
// int nNewTrackId = mediaEngine.AddSubtitle(item.ToString());
// }
// }
// mediaEngine.UpdateTracksFromCollection(listSubtitles);
// }
// else
// {
// listSubtitles = new System.Collections.ObjectModel.ObservableCollection<string>(listSubtitlesOld);
// }
//}
//public class IntToBoolConverter : IValueConverter
//{
// public object Convert(object value, Type targetType, object parameter, string language)
// {
// if ((int)value > 0)
// return true;
// else
// return false;
// }
// public object ConvertBack(object value, Type targetType, object parameter, string language)
// {
// throw new NotImplementedException();
// }
//}
private async Task<string> OpenSubtitleDialog()
{
var fop = new Windows.Storage.Pickers.FileOpenPicker();
WinRT.Interop.InitializeWithWindow.Initialize(fop, hWnd);
fop.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
fop.FileTypeFilter.Add(".srt");
fop.FileTypeFilter.Add(".vtt");
var file = await fop.PickSingleFileAsync();
return (file != null ? file.Path : string.Empty);
}
private void tsControls_Toggled(object sender, RoutedEventArgs e)
{
ToggleSwitch ts = sender as ToggleSwitch;
if (mediaEngine != null)
mediaEngine.ShowControls(ts.IsOn);
}
private void tsEffects_Toggled(object sender, RoutedEventArgs e)
{
ToggleSwitch ts = sender as ToggleSwitch;
if (mediaEngine != null)
{
mediaEngine.SetEffects(ts.IsOn);
}
}
private void cbInvert_Checked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect |= CMediaEngine.EFFECT.INVERT);
}
}
private void cbInvert_Unchecked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect & ~CMediaEngine.EFFECT.INVERT);
}
}
private void cbGrayscale_Checked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect |= CMediaEngine.EFFECT.GRAYSCALE);
}
}
private void cbGrayscale_Unchecked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect & ~CMediaEngine.EFFECT.GRAYSCALE);
}
}
private void cbRGB_Checked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect |= CMediaEngine.EFFECT.RGB);
}
}
private void cbRGB_Unchecked(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
CMediaEngine.EFFECT nEffect = mediaEngine.GetEffects();
mediaEngine.UpdateEffects(nEffect & ~CMediaEngine.EFFECT.RGB);
}
}
private void sliderR_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (mediaEngine != null)
{
Slider s = sender as Slider;
mediaEngine.EFFECT_RGB_INTENSITY_RED = (byte)s.Value;
}
}
private void sliderG_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (mediaEngine != null)
{
Slider s = sender as Slider;
mediaEngine.EFFECT_RGB_INTENSITY_GREEN = (byte)s.Value;
}
}
private void sliderB_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (mediaEngine != null)
{
Slider s = sender as Slider;
mediaEngine.EFFECT_RGB_INTENSITY_BLUE = (byte)s.Value;
}
}
private async void btnCapture_Click(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
if (mediaEngine.CanCapture())
mediaEngine.Capture();
else
{
Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("A video stream must be running !", "Information");
WinRT.Interop.InitializeWithWindow.Initialize(md, hWnd);
_ = await md.ShowAsync();
}
}
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
if (mediaEngine != null)
{
var wb = mediaEngine.GetCaptureWriteableBitmapImage();
if (wb != null)
{
SaveFrameDialog(wb);
}
else
{
Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("No captured frame found !", "Information");
WinRT.Interop.InitializeWithWindow.Initialize(md, hWnd);
_ = await md.ShowAsync();
}
}
}
private async void SaveFrameDialog(Microsoft.UI.Xaml.Media.Imaging.WriteableBitmap wb)
{
mediaEngine.SetOpacity(10);
StackPanel sp = new StackPanel();
Image img = new Image();
img.Source = wb;
sp.Children.Add(img);
ContentDialog cd = new ContentDialog()
{
Title = "Save Frame",
Content = sp,
PrimaryButtonText = "Save as",
CloseButtonText = "Cancel"
};
cd.PrimaryButtonClick += async (sender, args) =>
{
var clickDeferral = args.GetDeferral();
bool bRet = await SaveFrameDialogPicker();
args.Cancel = !bRet;
clickDeferral.Complete();
};
cd.XamlRoot = this.Content.XamlRoot;
var cdResult = await cd.ShowAsync();
mediaEngine.SetOpacity(100);
if (cdResult == ContentDialogResult.Primary)
{
}
}
private async Task<bool> SaveFrameDialogPicker()
{
var fsp = new Windows.Storage.Pickers.FileSavePicker();
WinRT.Interop.InitializeWithWindow.Initialize(fsp, hWnd);
fsp.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
fsp.SuggestedFileName = "New Document";
fsp.FileTypeChoices.Add("JPG (*.jpg)", new List<string>() { ".jpg" });
fsp.FileTypeChoices.Add("PNG Portable Network Graphics (*.png)", new List<string>() { ".png" });
fsp.FileTypeChoices.Add("GIF Graphics Interchange Format (*.gif)", new List<string>() { ".gif" });
fsp.FileTypeChoices.Add("BMP Windows Bitmap (*.bmp)", new List<string>() { ".bmp" });
fsp.FileTypeChoices.Add("TIF Tagged Image File Format (*.tif)", new List<string>() { ".tif" });
Windows.Storage.StorageFile file = await fsp.PickSaveFileAsync();
if (file != null)
{
mediaEngine.SaveCapture(file);
return true;
}
else
return false;
}
}
}