aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Video/CaptureDevice.cs
blob: a8ff38da97b08f5a54a0391f9f1f5a60fd569744 (plain)
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tango.Video.DirectShow;
using Tango.Video.DirectShow.EventArguments;
using Tango.Video.DirectShow.Delegates;


namespace Tango.Video.DirectCapture
{
    /// <summary>
    /// Represents a capture device object. This is a dependency object.
    /// </summary>
    public class CaptureDevice : DependencyObject, INotifyPropertyChanged, IDisposable
    {
        private DirectShowCapture captureDevice;
        private WriteableBitmap blackSource;
        private int updateSourceCounter;

        #region Events

        /// <summary>
        /// Raises then a new frame has been received from the capture device.
        /// </summary>
        /// <example><code>
        ///void capture_FrameReceived(object sender, DirectShow.EventArguments.FrameReceivedEventArgs args)
        ///{
        ///    //Use this event to draw on, or manipulate the frames.
        ///    //This will be triggered only if RaiseFrameReceivedEvent is set to true.
        ///    //To show the manipulated image, unbind the img args from the VideoSource property in the xaml file.
        ///
        ///    args.BitmapSource.Lock();
        ///
        ///    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(args.BitmapSource.PixelWidth, args.BitmapSource.PixelHeight, args.BitmapSource.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, args.BitmapSource.BackBuffer);
        ///    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
        ///
        ///    //Draw on the frame using good old GDI+
        ///    g.FillEllipse(System.Drawing.Brushes.Red, new System.Drawing.Rectangle(20, 20, 100, 100));
        ///    g.DrawString("Process The Frames ;)", new System.Drawing.Font("Arial", 24), System.Drawing.Brushes.White, new System.Drawing.RectangleF(140, 50, 400, 50));
        ///
        ///    g.Dispose();
        ///    bmp.Dispose();
        ///
        ///    args.BitmapSource.AddDirtyRect(new Int32Rect(0, 0, args.BitmapSource.PixelWidth, args.BitmapSource.PixelHeight));
        ///    args.BitmapSource.Unlock();
        ///    args.BitmapSource.Freeze();
        ///
        ///    this.Dispatcher.BeginInvoke(new Action(() =>
        ///    {
        ///        postImage.Source = args.BitmapSource; //This event is raising from a seperate thread so we need to invoke the UI dispatcher.
        ///    }));
        ///}
        /// </code></example>
        public event FrameReceivedEventDelegate FrameReceived;

        /// <summary>
        /// Occurs when a video capture has started.
        /// </summary>
        public event Action CaptureStarted;

        /// <summary>
        /// Occurs when a video capture has stoped.
        /// </summary>
        public event Action CaptureStoped;

        #endregion

        #region Constructors

        public CaptureDevice()
        {
            captureDevice = new DirectShowCapture();
            captureDevice.FrameReceived += captureDevice_FrameReceived;
            captureDevice.UseFrameRate = false;
            captureDevice.UseFrameSize = false;
            captureDevice.UseQueue = false;

            updateSourceCounter = 0;
            UpdateSourceRatio = 1;

            //Initialize Black Source
            blackSource = new WriteableBitmap(Tango.Video.DirectShow.BitmapHelper.CreateEmptySource(new Size(1920, 1080), Colors.Transparent));
            blackSource.Freeze();


            //Initialize Available Sources.
            List<Device> list = Tango.Video.DirectShow.DirectShowCapture.GetAvailableDevices();
            AvailableCaptureDevices = new ObservableCollection<Device>(list);


            //Initialize Standard Resolutions.
            List<Resolution> res = new List<Resolution>();
            res.Add(new Resolution(320, 240));
            res.Add(new Resolution(640, 480));
            res.Add(new Resolution(720, 576));
            res.Add(new Resolution(800, 600));
            res.Add(new Resolution(1024, 768));
            res.Add(new Resolution(1280, 720));
            res.Add(new Resolution(1280, 1024));
            res.Add(new Resolution(1440, 900));
            res.Add(new Resolution(1366, 768));
            res.Add(new Resolution(1600, 900));
            res.Add(new Resolution(1680, 1050));
            res.Add(new Resolution(1600, 1200));
            res.Add(new Resolution(1920, 1080));
            res.Add(new Resolution(1920, 1200));
            StandardResolutions = new ObservableCollection<Resolution>(res);


            //Initialize Standard Frame Rates.
            List<FrameRate> rates = new List<FrameRate>();
            rates.Add(new FrameRate(5));
            rates.Add(new FrameRate(10));
            rates.Add(new FrameRate(15));
            rates.Add(new FrameRate(20));
            rates.Add(new FrameRate(23.98));
            rates.Add(new FrameRate(29.97));
            rates.Add(new FrameRate(50));
            rates.Add(new FrameRate(60));
            StandardFrameRates = new ObservableCollection<FrameRate>(rates);
        }

        #endregion

        #region Event Handlers

        private void captureDevice_FrameReceived(object sender, FrameReceivedEventArgs args)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                updateSourceCounter++;

                if (updateSourceCounter >= UpdateSourceRatio)
                {
                    VideoSource = args.BitmapSource;
                    updateSourceCounter = 0;
                }

                if (!_isStarted && ClearSourceOnStop)
                {
                    BindSourceToBlackSource();
                }

            }), System.Windows.Threading.DispatcherPriority.Send);

            if (_raiseFrameReceivedEvent)
            {
                OnFrameReceived(this, new FrameReceivedEventArgs(BitmapHelper.BitmapSourceToWriteableBitmap(args.BitmapSource)));
            }
        }

        #endregion

        #region Properties

        private bool _emulatedMode;
        /// <summary>
        /// Gets or sets a value indicating whether [video available].
        /// </summary>
        public bool EmulatedMode
        {
            get { return _emulatedMode; }
            set { _emulatedMode = value; RaisePropertyChanged(nameof(EmulatedMode)); }
        }


        /// <summary>
        /// The name of the device to capture.
        /// </summary>
        public Device Device
        {
            get { return (Device)GetValue(DeviceProperty); }
            set { SetValue(DeviceProperty, value); }
        }
        public static readonly DependencyProperty DeviceProperty =
            DependencyProperty.Register("Device", typeof(Device), typeof(CaptureDevice), new PropertyMetadata(null, new PropertyChangedCallback(DeviceNameChanged)));
        private static void DeviceNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CaptureDevice instance = GetInstance(d);
            instance.captureDevice.VideoDevice = instance.Device;

            if (instance.IsStarted && instance.InvalidateSourceOnPropertiesChange)
            {
                instance.InvalidateDevice();
            }
        }


        /// <summary>
        /// Sets the frame rate of the capture device.
        /// </summary>
        public FrameRate FrameRate
        {
            get { return (FrameRate)GetValue(FrameRateProperty); }
            set { SetValue(FrameRateProperty, value); }
        }
        public static readonly DependencyProperty FrameRateProperty =
            DependencyProperty.Register("FrameRate", typeof(FrameRate), typeof(CaptureDevice), new PropertyMetadata(new FrameRate(15), new PropertyChangedCallback(FrameRateChanged)));
        private static void FrameRateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);

            if (instance.FrameRate != null)
            {
                instance.captureDevice.FrameRate = instance.FrameRate.Rate;

                if (instance.IsStarted && instance.InvalidateSourceOnPropertiesChange)
                {
                    instance.InvalidateDevice();
                }
            }
        }


        /// <summary>
        /// When set to true, the capture device will use the default frame rate.
        /// </summary>
        public bool AutoFrameRate
        {
            get { return (bool)GetValue(AutoFrameRateProperty); }
            set 
            {
                SetValue(AutoFrameRateProperty, value);
                captureDevice.UseFrameRate = !AutoFrameRate;
            }
        }
        public static readonly DependencyProperty AutoFrameRateProperty =
            DependencyProperty.Register("AutoFrameRate", typeof(bool), typeof(CaptureDevice), new PropertyMetadata(true, new PropertyChangedCallback(AutoFrameRateChanged)));
        private static void AutoFrameRateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);
            instance.captureDevice.UseFrameRate = !instance.AutoFrameRate;
        }


        /// <summary>
        /// Sets the capture device frame width.
        /// </summary>
        public int FrameWidth
        {
            get { return (int)GetValue(FrameWidthProperty); }
            set 
            {
                SetValue(FrameWidthProperty, value);
                captureDevice.FrameSize = new FrameSize(FrameWidth, FrameHeight);
            }
        }
        public static readonly DependencyProperty FrameWidthProperty =
            DependencyProperty.Register("FrameWidth", typeof(int), typeof(CaptureDevice), new PropertyMetadata(1280, new PropertyChangedCallback(FrameWidthChanged)));
        private static void FrameWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);
            instance.captureDevice.FrameSize = new FrameSize(instance.FrameWidth, instance.FrameHeight);
        }


        /// <summary>
        /// Sets the capture device frame height.
        /// </summary>
        public int FrameHeight
        {
            get { return (int)GetValue(FrameHeightProperty); }
            set 
            {
                SetValue(FrameHeightProperty, value);
                captureDevice.FrameSize = new FrameSize(FrameWidth, FrameHeight);
            }
        }
        public static readonly DependencyProperty FrameHeightProperty =
            DependencyProperty.Register("FrameHeight", typeof(int), typeof(CaptureDevice), new PropertyMetadata(720, new PropertyChangedCallback(FrameHeightChanged)));
        private static void FrameHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);
            instance.captureDevice.FrameSize = new FrameSize(instance.FrameWidth, instance.FrameHeight);
        }


        /// <summary>
        /// Then set to true, the capture device will use the default frame size.
        /// </summary>
        public bool AutoFrameSize
        {
            get { return (bool)GetValue(AutoFrameSizeProperty); }
            set { SetValue(AutoFrameSizeProperty, value); }
        }
        public static readonly DependencyProperty AutoFrameSizeProperty =
            DependencyProperty.Register("AutoFrameSize", typeof(bool), typeof(CaptureDevice), new PropertyMetadata(true, new PropertyChangedCallback(AutoFrameSizeChanged)));
        private static void AutoFrameSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);
            instance.captureDevice.UseFrameSize = !instance.AutoFrameSize;
        }


        /// <summary>
        /// Use this property to bind your image args to.
        /// </summary>
        public BitmapSource VideoSource
        {
            get { return (BitmapSource)GetValue(VideoSourceProperty); }
            set { SetValue(VideoSourceProperty, value); }
        }
        public static readonly DependencyProperty VideoSourceProperty =
            DependencyProperty.Register("VideoSource", typeof(BitmapSource), typeof(CaptureDevice), new PropertyMetadata(null));


        /// <summary>
        /// Contains a list of available capture device names.
        /// </summary>
        public ObservableCollection<Device> AvailableCaptureDevices
        {
            get { return (ObservableCollection<Device>)GetValue(AvailableCaptureDevicesProperty); }
            set { SetValue(AvailableCaptureDevicesProperty, value); }
        }
        public static readonly DependencyProperty AvailableCaptureDevicesProperty =
            DependencyProperty.Register("AvailableCaptureDevices", typeof(ObservableCollection<Device>), typeof(CaptureDevice), new PropertyMetadata(null));


        /// <summary>
        /// Use this property as an alternative to set the capture device frame width and height.
        /// </summary>
        public Resolution Resolution
        {
            get { return (Resolution)GetValue(ResolutionProperty); }
            set { SetValue(ResolutionProperty, value); }
        }
        public static readonly DependencyProperty ResolutionProperty =
            DependencyProperty.Register("Resolution", typeof(Resolution), typeof(CaptureDevice), new PropertyMetadata(null, new PropertyChangedCallback(ResolutionChanged)));
        private static void ResolutionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = GetInstance(d);

            if (instance.Resolution != null)
            {
                instance.captureDevice.FrameSize = new FrameSize(instance.Resolution.Width, instance.Resolution.Height);

                if (instance.IsStarted && instance.InvalidateSourceOnPropertiesChange)
                {
                    instance.InvalidateDevice();
                }
            }
        }


        /// <summary>
        /// Contains a list of standard resolutions for any capture device.
        /// </summary>
        public ObservableCollection<Resolution> StandardResolutions
        {
            get { return (ObservableCollection<Resolution>)GetValue(StandardResolutionsProperty); }
            set { SetValue(StandardResolutionsProperty, value); }
        }
        public static readonly DependencyProperty StandardResolutionsProperty =
            DependencyProperty.Register("StandardResolutions", typeof(ObservableCollection<Resolution>), typeof(CaptureDevice), new PropertyMetadata(null));


        /// <summary>
        /// Contains a list of standard frame rates for any capture device.
        /// </summary>
        public ObservableCollection<FrameRate> StandardFrameRates
        {
            get { return (ObservableCollection<FrameRate>)GetValue(StandardFrameRatesProperty); }
            set { SetValue(StandardFrameRatesProperty, value); }
        }
        public static readonly DependencyProperty StandardFrameRatesProperty =
            DependencyProperty.Register("StandardFrameRates", typeof(ObservableCollection<FrameRate>), typeof(CaptureDevice), new PropertyMetadata(null));


        /// <summary>
        /// Gets or sets whether to reconnect the capture device if resolution, frame rate or device name were changed.
        /// </summary>
        public bool InvalidateSourceOnPropertiesChange
        {
            get { return (bool)GetValue(InvalidateSourceOnPropertiesChangeProperty); }
            set { SetValue(InvalidateSourceOnPropertiesChangeProperty, value); }
        }
        public static readonly DependencyProperty InvalidateSourceOnPropertiesChangeProperty =
            DependencyProperty.Register("InvalidateSourceOnPropertiesChange", typeof(bool), typeof(CaptureDevice), new PropertyMetadata(false));


        /// <summary>
        /// Gets or sets whether to clear the image args when the capture device stops.
        /// </summary>
        public bool ClearSourceOnStop
        {
            get { return (bool)GetValue(ClearSourceOnStopProperty); }
            set { SetValue(ClearSourceOnStopProperty, value); }
        }
        public static readonly DependencyProperty ClearSourceOnStopProperty =
            DependencyProperty.Register("ClearSourceOnStop", typeof(bool), typeof(CaptureDevice), new PropertyMetadata(false));


        private bool _raiseFrameReceivedEvent;
        /// <summary>
        /// Gets or sets whether to raise the FrameReceived event. (Set to true if you want to manipulate the args).
        /// </summary>
        public bool RaiseFrameReceivedEvent
        {
            get { return _raiseFrameReceivedEvent; }
            set
            {
                if (_raiseFrameReceivedEvent && !value && _isStarted)
                {
                    BindSourceToDirectShow();
                }

                _raiseFrameReceivedEvent = value;

                RaisePropertyChanged("RaiseFrameReceivedEvent");

                captureDevice.UseQueue = value;
            }
        }


        private bool _isStarted;
        /// <summary>
        /// Gets or sets whether the capture device is capturing.
        /// </summary>
        public bool IsStarted
        {
            get { return _isStarted; }
            set
            {
                _isStarted = value;
                RaisePropertyChanged("IsStarted");

                if (value)
                {
                    Start();
                }
                else
                {
                    Stop();
                }
            }
        }


        private int updateSourceRatio;
        /// <summary>
        /// Gets or sets the dependepncy property video source update frequency.
        /// </summary>
        public int UpdateSourceRatio
        {   
            get { return updateSourceRatio; }
            set { updateSourceRatio = value; RaisePropertyChanged("UpdateSourceRatio"); }
        }
        

        #endregion

        #region Public Methods

        /// <summary>
        /// Starts the capture device.
        /// </summary>
        public void Start()
        {
            BindSourceToDirectShow();

            _isStarted = true;
            RaisePropertyChanged("IsStarted");

            if (!IsInDesignMode())
            {
                captureDevice.Start();
            }

            if (CaptureStarted != null) CaptureStarted();
        }


        /// <summary>
        /// Stops the capture device.
        /// </summary>
        public void Stop()
        {
            _isStarted = false;
            RaisePropertyChanged("IsStarted");

            if (!IsInDesignMode())
            {
                captureDevice.Stop();
            }

            if (ClearSourceOnStop)
            {
                BindSourceToBlackSource();

                if (_raiseFrameReceivedEvent)
                {
                    OnFrameReceived(this, new FrameReceivedEventArgs(new WriteableBitmap(BitmapHelper.CreateEmptySource(new Size(1920, 1080), Colors.Transparent))));
                }
            }

            if (CaptureStoped != null) CaptureStoped();
        }


        /// <summary>
        /// Use this method to bind an image control to the video args property.
        /// </summary>
        /// <param name="image">The image control</param>
        /// <returns>The binding instance that was used to create the binding.</returns>
        public Binding BindImageSourceToVideoSource(Image image)
        {
            Binding b = new Binding();
            b.Source = this;
            b.Path = new PropertyPath(CaptureDevice.VideoSourceProperty);
            b.Mode = BindingMode.OneWay;
            b.IsAsync = true;
            b.BindsDirectlyToSource = true;
            image.SetBinding(Image.SourceProperty, b);

            return b;
        }


        /// <summary>
        /// Use this method to bind any control background to the video args property.
        /// </summary>
        /// <param name="control">Any type that is a control or inharits from control.</param>
        /// <returns>The binding instance that was used to create the binding.</returns>
        public Binding BindControlBackgroundToVideoSource(Control control)
        {
            Binding b = new Binding();
            b.Source = this;
            b.Path = new PropertyPath(CaptureDevice.VideoSourceProperty);
            b.Mode = BindingMode.OneWay;
            b.IsAsync = true;
            b.BindsDirectlyToSource = true;
            control.SetBinding(Control.BackgroundProperty, b);

            return b;
        }

        public void DisableSourceUpdate()
        {
            EmulatedMode = true;
            this.Unbind(CaptureDevice.VideoSourceProperty);
        }

        public void EnableSourceUpdate()
        {
            EmulatedMode = false;
            BindSourceToDirectShow();
        }

        #endregion

        #region Private Methods
        private void BindSourceToDirectShow()
        {
            this.BindAsync(CaptureDevice.VideoSourceProperty, captureDevice, DirectShowCapture.SourceProperty, BindingMode.OneWay);
        }

        private void BindSourceToBlackSource()
        {
            VideoSource = blackSource;
        }

        private bool IsInDesignMode()
        {
            return (DesignerProperties.GetIsInDesignMode(this));
        }

        private void InvalidateDevice()
        {
            try
            {
                captureDevice.Stop();
                captureDevice.Start();
            }
            catch { }
        }
        #endregion

        #region Static Methods

        private static CaptureDevice GetInstance(DependencyObject d)
        {
            return d as CaptureDevice;
        }

        /// <summary>
        /// Retrieves all the available capture devices supporting direct show.
        /// </summary>
        /// <returns>Device names.</returns>
        public static List<Device> GetAvailableCaptureDevices()
        {
            return DirectShowCapture.GetAvailableDevices();
        }

        #endregion

        #region Virtuals

        protected virtual void OnFrameReceived(object sender, FrameReceivedEventArgs args)
        {
            if (FrameReceived != null) FrameReceived(sender, args);
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Stops and disposes the current instance.
        /// </summary>
        public void Dispose()
        {
            _isStarted = false;
            captureDevice.Stop();
        }

        #endregion

        #region Notify Property Changed Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(String propName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        #endregion
    }
}