Skip to content

Commit c1fea51

Browse files
dummy webcam stream from ViewModel command instead of handler
1 parent bb9e054 commit c1fea51

File tree

4 files changed

+152
-103
lines changed

4 files changed

+152
-103
lines changed

Samples/StyleTransfer/AppModel.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Windows.Media;
1111
using Windows.Media.Core;
12+
using Windows.Media.Playback;
1213

1314
namespace StyleTransfer
1415
{
@@ -17,6 +18,7 @@ class AppModel : INotifyPropertyChanged
1718
public AppModel()
1819
{
1920
this._useGPU = true;
21+
this._selectedCameraIndex = 0;
2022
}
2123

2224
private string _modelSource;
@@ -30,13 +32,13 @@ public string ModelSource
3032
}
3133
}
3234

33-
private string _inputMediaSource;
34-
public string InputMediaSource
35+
private string _inputMedia;
36+
public string InputMedia
3537
{
36-
get { return _inputMediaSource; }
38+
get { return _inputMedia; }
3739
set
3840
{
39-
_inputMediaSource = value;
41+
_inputMedia = value;
4042
OnPropertyChanged();
4143
}
4244
}
@@ -52,16 +54,35 @@ public bool UseGPU
5254
}
5355
}
5456

55-
// In AppModel or in AppViewModel?
56-
private VideoFrame _outputFrame;
57-
public VideoFrame OutputFrame
57+
private MediaSource _outputMediaSource;
58+
public MediaSource OutputMediaSource
5859
{
59-
get { return _outputFrame; }
60-
set
60+
get
6161
{
62-
_outputFrame = value;
63-
OnPropertyChanged();
62+
// TODO: Configure mediasource based on profile? or in VM
63+
return _outputMediaSource;
6464
}
65+
set { _outputMediaSource = value; OnPropertyChanged(); }
66+
}
67+
private MediaSource _inputMediaSource;
68+
public MediaSource InputMediaSource
69+
{
70+
get { return _inputMediaSource; }
71+
set { _inputMediaSource = value; OnPropertyChanged(); }
72+
}
73+
74+
private IEnumerable<string> _cameraNamesList;
75+
public IEnumerable<string> CameraNamesList
76+
{
77+
get { return _cameraNamesList; }
78+
set { _cameraNamesList = value; OnPropertyChanged(); }
79+
}
80+
81+
private int _selectedCameraIndex;
82+
public int SelectedCameraIndex
83+
{
84+
get { return _selectedCameraIndex; }
85+
set { _selectedCameraIndex = value; OnPropertyChanged(); }
6586
}
6687

6788
public event PropertyChangedEventHandler PropertyChanged;

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
using System.Windows.Input;
88
using Windows.Media.Core;
99
using GalaSoft.MvvmLight.Command;
10+
using Windows.Media.Capture.Frames;
11+
using System.Diagnostics;
12+
using Windows.Media.Capture;
13+
using Windows.Media.Playback;
1014

1115
namespace StyleTransfer
1216
{
@@ -18,8 +22,14 @@ public AppViewModel()
1822
// TODO: Add a param to check if have an image to save.
1923
SaveCommand = new RelayCommand(this.SaveOutput);
2024
MediaSourceCommand = new RelayCommand<string>(SetMediaSource);
25+
LiveStreamCommand = new RelayCommand(async () => await StartWebcamStream());
2126
}
2227

28+
Windows.Media.Capture.MediaCapture _mediaCapture;
29+
private List<MediaFrameSourceGroup> _mediaFrameSourceGroupList;
30+
private MediaFrameSourceGroup _selectedMediaFrameSourceGroup;
31+
private MediaFrameSource _selectedMediaFrameSource;
32+
2333
private AppModel _appModel;
2434
public AppModel CurrentApp
2535
{
@@ -37,6 +47,7 @@ public ICommand SaveCommand
3747
}
3848

3949
public ICommand MediaSourceCommand { get; set; }
50+
public ICommand LiveStreamCommand { get; set; }
4051

4152
public event PropertyChangedEventHandler PropertyChanged;
4253

@@ -45,16 +56,109 @@ public void SaveOutput()
4556
// TODO: Take from UIButtonSaveImage_Click
4657
return;
4758
}
59+
60+
public async Task StartWebcamStream()
61+
{
62+
try
63+
{
64+
// Find the sources
65+
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
66+
_mediaFrameSourceGroupList = allGroups.Where(group => group.SourceInfos.Any(sourceInfo => sourceInfo.SourceKind == MediaFrameSourceKind.Color
67+
&& (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview
68+
|| sourceInfo.MediaStreamType == MediaStreamType.VideoRecord))).ToList();
69+
}
70+
catch (Exception ex)
71+
{
72+
Debug.WriteLine(ex.Message);
73+
_mediaFrameSourceGroupList = null;
74+
}
75+
76+
if ((_mediaFrameSourceGroupList == null) || (_mediaFrameSourceGroupList.Count == 0))
77+
{
78+
// No camera sources found
79+
Debug.WriteLine("No Camera found");
80+
return;
81+
}
82+
83+
_appModel.CameraNamesList = _mediaFrameSourceGroupList.Select(group => group.DisplayName);
84+
85+
// UICmbCamera_SelectionChanged
86+
try
87+
{
88+
_selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList[_appModel.SelectedCameraIndex];
89+
90+
// Create MediaCapture and its settings
91+
_mediaCapture = new MediaCapture();
92+
var settings = new MediaCaptureInitializationSettings
93+
{
94+
SourceGroup = _selectedMediaFrameSourceGroup,
95+
PhotoCaptureSource = PhotoCaptureSource.Auto,
96+
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
97+
StreamingCaptureMode = StreamingCaptureMode.Video
98+
};
99+
100+
// Initialize MediaCapture
101+
await _mediaCapture.InitializeAsync(settings);
102+
StartPreview();
103+
}
104+
catch (Exception ex)
105+
{
106+
Debug.WriteLine(ex.ToString());
107+
}
108+
}
109+
110+
48111
public void SetMediaSource(object obj)
49112
{
50113
// TODO: Convert to a better value for the appModel object here.
51-
_appModel.InputMediaSource = obj.ToString();
114+
_appModel.InputMedia = obj.ToString();
52115

53116
// If video source, whole different code flow
54117

55118
// If camera/image upload, gather input image then put to eval/render
56119
// If source == upload --> HelperMethods::LoadVideoFrameFromFilePickedAsync
120+
121+
// Process everythign and then set MediaPlkayer object in AppModel for input/output
57122
return;
58123
}
124+
125+
private void StartPreview()
126+
{
127+
Debug.WriteLine("StartPreview");
128+
_selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoPreview
129+
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
130+
if (_selectedMediaFrameSource == null)
131+
{
132+
_selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoRecord
133+
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
134+
}
135+
136+
// if no preview stream are available, bail
137+
if (_selectedMediaFrameSource == null)
138+
{
139+
return;
140+
}
141+
142+
var _mediaPlayer = new MediaPlayer();
143+
_mediaPlayer.RealTimePlayback = true;
144+
_mediaPlayer.AutoPlay = true;
145+
_appModel.OutputMediaSource = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
146+
_appModel.InputMediaSource = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
147+
148+
149+
//UIInputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
150+
//UIOutputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
151+
152+
/*UITxtBlockPreviewProperties.Text = string.Format("{0}x{1}@{2}, {3}",
153+
_selectedMediaFrameSource.CurrentFormat.VideoFormat.Width,
154+
_selectedMediaFrameSource.CurrentFormat.VideoFormat.Height,
155+
_selectedMediaFrameSource.CurrentFormat.FrameRate.Numerator + "/" + _selectedMediaFrameSource.CurrentFormat.FrameRate.Denominator,
156+
_selectedMediaFrameSource.CurrentFormat.Subtype);
157+
158+
UICameraSelectionControls.Visibility = Visibility.Visible;
159+
UIInputMediaPlayerElement.Visibility = Visibility.Visible;
160+
UIOutputMediaPlayerElement.Visibility = Visibility.Visible;*/
161+
162+
}
59163
}
60164
}

Samples/StyleTransfer/MainPage.xaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131

132132
<Button
133133
Name="UIButtonLiveStream"
134-
Click="UIButtonLiveStream_Click"
134+
Command="{Binding LiveStreamCommand}"
135135
CommandParameter="LiveStream"
136136
ToolTipService.ToolTip="Camera preview">
137137
<Button.Content>
@@ -222,8 +222,11 @@
222222
MaxWidth="720"
223223
MaxHeight="720"
224224
AreTransportControlsEnabled="False"
225+
AutoPlay="True"
225226
Canvas.ZIndex="-1"
226-
Stretch="Uniform" />
227+
Source="{Binding CurrentApp.InputMediaSource}"
228+
Stretch="Uniform"
229+
Visibility="Visible" />
227230

228231
<!-- Input image preview -->
229232
<Image
@@ -246,8 +249,11 @@
246249
MaxWidth="720"
247250
MaxHeight="720"
248251
AreTransportControlsEnabled="False"
252+
AutoPlay="True"
249253
Canvas.ZIndex="-1"
250-
Stretch="Uniform" />
254+
Source="{Binding CurrentApp.OutputMediaSource}"
255+
Stretch="Uniform"
256+
Visibility="Visible" />
251257

252258
<Button
253259
Name="UIButtonSaveImage"
@@ -270,9 +276,13 @@
270276
HorizontalAlignment="Left"
271277
VerticalAlignment="Bottom"
272278
Orientation="vertical"
273-
Visibility="Collapsed">
279+
Visibility="Visible">
274280
<TextBlock Style="{StaticResource TextBlockStyling}" Text="Camera: " />
275-
<ComboBox Name="UICmbCamera" Foreground="White">
281+
<ComboBox
282+
Name="UICmbCamera"
283+
Foreground="White"
284+
ItemsSource="{Binding CurrentApp.CameraNamesList}"
285+
SelectedIndex="{Binding CurrentApp.SelectedCameraIndex, Mode=TwoWay}">
276286
<ComboBox.Background>
277287
<SolidColorBrush Opacity="0.3" Color="Black" />
278288
</ComboBox.Background>

Samples/StyleTransfer/MainPage.xaml.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -54,92 +54,6 @@ public MainPage()
5454

5555
}
5656

57-
private async void UIButtonLiveStream_Click(object sender, RoutedEventArgs e)
58-
{
59-
try
60-
{
61-
// Find the sources
62-
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
63-
_mediaFrameSourceGroupList = allGroups.Where(group => group.SourceInfos.Any(sourceInfo => sourceInfo.SourceKind == MediaFrameSourceKind.Color
64-
&& (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview
65-
|| sourceInfo.MediaStreamType == MediaStreamType.VideoRecord))).ToList();
66-
}
67-
catch (Exception ex)
68-
{
69-
Debug.WriteLine(ex.Message);
70-
_mediaFrameSourceGroupList = null;
71-
}
72-
73-
if ((_mediaFrameSourceGroupList == null) || (_mediaFrameSourceGroupList.Count == 0))
74-
{
75-
// No camera sources found
76-
Debug.WriteLine("No Camera found");
77-
return;
78-
}
79-
80-
var cameraNamesList = _mediaFrameSourceGroupList.Select(group => group.DisplayName);
81-
UICmbCamera.ItemsSource = cameraNamesList;
82-
UICmbCamera.SelectedIndex = 0;
83-
84-
// UICmbCamera_SelectionChanged
85-
try
86-
{
87-
_selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList[UICmbCamera.SelectedIndex];
88-
89-
// Create MediaCapture and its settings
90-
_mediaCapture = new MediaCapture();
91-
var settings = new MediaCaptureInitializationSettings
92-
{
93-
SourceGroup = _selectedMediaFrameSourceGroup,
94-
PhotoCaptureSource = PhotoCaptureSource.Auto,
95-
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
96-
StreamingCaptureMode = StreamingCaptureMode.Video
97-
};
98-
99-
// Initialize MediaCapture
100-
await _mediaCapture.InitializeAsync(settings);
101-
StartPreview();
102-
}
103-
catch (Exception ex)
104-
{
105-
Debug.WriteLine(ex.ToString());
106-
}
107-
}
10857

109-
private void StartPreview()
110-
{
111-
Debug.WriteLine("StartPreview");
112-
_selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoPreview
113-
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
114-
if (_selectedMediaFrameSource == null)
115-
{
116-
_selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoRecord
117-
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
118-
}
119-
120-
// if no preview stream are available, bail
121-
if (_selectedMediaFrameSource == null)
122-
{
123-
return;
124-
}
125-
126-
_mediaPlayer = new MediaPlayer();
127-
_mediaPlayer.RealTimePlayback = true;
128-
_mediaPlayer.AutoPlay = true;
129-
_mediaPlayer.Source = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
130-
UIInputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
131-
UIOutputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
132-
133-
UITxtBlockPreviewProperties.Text = string.Format("{0}x{1}@{2}, {3}",
134-
_selectedMediaFrameSource.CurrentFormat.VideoFormat.Width,
135-
_selectedMediaFrameSource.CurrentFormat.VideoFormat.Height,
136-
_selectedMediaFrameSource.CurrentFormat.FrameRate.Numerator + "/" + _selectedMediaFrameSource.CurrentFormat.FrameRate.Denominator,
137-
_selectedMediaFrameSource.CurrentFormat.Subtype);
138-
139-
UICameraSelectionControls.Visibility = Visibility.Visible;
140-
UIInputMediaPlayerElement.Visibility = Visibility.Visible;
141-
UIOutputMediaPlayerElement.Visibility = Visibility.Visible;
142-
143-
}
14458
}
14559
}

0 commit comments

Comments
 (0)