77using System . Windows . Input ;
88using Windows . Media . Core ;
99using GalaSoft . MvvmLight . Command ;
10+ using Windows . Media . Capture . Frames ;
11+ using System . Diagnostics ;
12+ using Windows . Media . Capture ;
13+ using Windows . Media . Playback ;
1014
1115namespace 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}
0 commit comments