Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 106 additions & 2 deletions Source/DirectShow/Controls/VideoCaptureElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Windows.Interop;
using WPFMediaKit.DirectShow.MediaPlayers;
using DirectShowLib;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;

namespace WPFMediaKit.DirectShow.Controls
{
Expand Down Expand Up @@ -33,6 +36,19 @@ public int DesiredPixelWidth

#endregion

#region MaxResolution

public static readonly DependencyProperty MaxResolutionProperty =
DependencyProperty.Register("MaxResolution", typeof(bool), typeof(VideoCaptureElement));

public bool MaxResolution
{
get { return (bool)GetValue(MaxResolutionProperty); }
set { SetValue(MaxResolutionProperty, value); }
}

#endregion

#region DesiredPixelHeight

public static readonly DependencyProperty DesiredPixelHeightProperty =
Expand Down Expand Up @@ -271,8 +287,22 @@ protected VideoCapturePlayer VideoCapturePlayer
/// </summary>
private void SetParameters()
{
int height = DesiredPixelHeight;
int width = DesiredPixelWidth;
int height = 0;
int width = 0;
if (this.MaxResolution && this.VideoCaptureDevice != null)
{
var reses = GetAllAvailableResolution(this.VideoCaptureDevice);
var maxRes = reses.FirstOrDefault(p => p.Width == reses.Max(r => r.Width));

width = maxRes.Width;
height = maxRes.Height;
}
else
{
height = DesiredPixelHeight;
width = DesiredPixelWidth;
}

int fps = FPS;
bool useYuv = UseYuv;
string filename = OutputFileName;
Expand Down Expand Up @@ -304,5 +334,79 @@ protected override MediaPlayerBase OnRequestMediaPlayer()
{
return new VideoCapturePlayer();
}

public List<Resolution> GetAllAvailableResolution(DsDevice vidDev)
{
try
{
int hr;
int max = 0;
int bitCount = 0;

IBaseFilter sourceFilter = null;

var m_FilterGraph2 = new FilterGraph() as IFilterGraph2;

hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter);

var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0);

var AvailableResolutions = new List<Resolution>();

VideoInfoHeader v = new VideoInfoHeader();
IEnumMediaTypes mediaTypeEnum;
hr = pRaw2.EnumMediaTypes(out mediaTypeEnum);

AMMediaType[] mediaTypes = new AMMediaType[1];
IntPtr fetched = IntPtr.Zero;
hr = mediaTypeEnum.Next(1, mediaTypes, fetched);

while (fetched != null && mediaTypes[0] != null)
{
Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);
if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0)
{
if (v.BmiHeader.BitCount > bitCount)
{
AvailableResolutions.Clear();
max = 0;
bitCount = v.BmiHeader.BitCount;


}
AvailableResolutions.Add(new Resolution(v.BmiHeader.Width, v.BmiHeader.Height));
if (v.BmiHeader.Width > max || v.BmiHeader.Height > max)
max = (Math.Max(v.BmiHeader.Width, v.BmiHeader.Height));
}
hr = mediaTypeEnum.Next(1, mediaTypes, fetched);
}
return AvailableResolutions;
}

catch (Exception ex)
{
//Log(ex);
return new List<Resolution>();
}
}
}

public class Resolution
{
public Resolution(int width, int height)
{
this.Width = width;
this.Height = height;
}

public int Width
{
get; set;
}

public int Height
{
get; set;
}
}
}