概要
cscの作法、調べてみた。
directshow.dll使ってみた。
カメラを録画してみた。
サンプルコード
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using DirectShowLib;
using System.ComponentModel;
using System.Data;
namespace AsfFilter
{
internal class Capture: IDisposable {
private IFilterGraph2 m_FilterGraph = null;
IMediaControl m_mediaCtrl = null;
private bool m_bRunning = false;
DsROTEntry m_rot = null;
public void Dispose() {
GC.SuppressFinalize(this);
CloseInterfaces();
}
~Capture() {
Dispose();
}
public Capture(int iDeviceNum, string szOutputFileName) {
DsDevice [] capDevices;
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (iDeviceNum + 1 > capDevices.Length)
{
throw new Exception("No video capture devices found at that index!");
}
try
{
SetupGraph(capDevices[iDeviceNum], szOutputFileName);
m_bRunning = false;
}
catch
{
Dispose();
throw;
}
}
public void Start() {
if (!m_bRunning)
{
int hr = m_mediaCtrl.Run();
Marshal.ThrowExceptionForHR(hr);
m_bRunning = true;
}
}
public void Pause() {
if (m_bRunning)
{
IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
int hr = mediaCtrl.Pause();
Marshal.ThrowExceptionForHR(hr);
m_bRunning = false;
}
}
private void SetupGraph(DsDevice dev, string szOutputFileName) {
int hr;
IBaseFilter capFilter = null;
IBaseFilter asfWriter = null;
ICaptureGraphBuilder2 capGraph = null;
m_FilterGraph = (IFilterGraph2) new FilterGraph();
m_rot = new DsROTEntry(m_FilterGraph);
try
{
capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
hr = capGraph.SetFiltergraph(m_FilterGraph);
Marshal.ThrowExceptionForHR(hr);
hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
Marshal.ThrowExceptionForHR(hr);
asfWriter = ConfigAsf(capGraph, szOutputFileName);
hr = capGraph.RenderStream(null, null, capFilter, null, asfWriter);
Marshal.ThrowExceptionForHR(hr);
m_mediaCtrl = m_FilterGraph as IMediaControl;
}
finally
{
if (capFilter != null)
{
Marshal.ReleaseComObject(capFilter);
capFilter = null;
}
if (asfWriter != null)
{
Marshal.ReleaseComObject(asfWriter);
asfWriter = null;
}
if (capGraph != null)
{
Marshal.ReleaseComObject(capGraph);
capGraph = null;
}
}
}
private IBaseFilter ConfigAsf(ICaptureGraphBuilder2 capGraph, string szOutputFileName) {
IFileSinkFilter pTmpSink = null;
IBaseFilter asfWriter = null;
int hr = capGraph.SetOutputFileName(MediaSubType.Asf, szOutputFileName, out asfWriter, out pTmpSink);
Marshal.ThrowExceptionForHR(hr);
try
{
IConfigAsfWriter lConfig = asfWriter as IConfigAsfWriter;
Guid cat = new Guid(0x6E2A6955, 0x81DF, 0x4943, 0xBA, 0x50, 0x68, 0xA9, 0x86, 0xA7, 0x08, 0xF6);
hr = lConfig.ConfigureFilterUsingProfileGuid(cat);
Marshal.ThrowExceptionForHR(hr);
}
finally
{
Marshal.ReleaseComObject(pTmpSink);
}
return asfWriter;
}
private void CloseInterfaces() {
int hr;
try
{
if (m_mediaCtrl != null)
{
hr = m_mediaCtrl.Stop();
m_bRunning = false;
}
}
catch
{
}
if (m_rot != null)
{
m_rot.Dispose();
m_rot = null;
}
if (m_FilterGraph != null)
{
Marshal.ReleaseComObject(m_FilterGraph);
m_FilterGraph = null;
}
}
}
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
protected override void Dispose(bool disposing) {
if (disposing)
{
if (cam != null)
{
cam.Dispose();
cam = null;
}
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent() {
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(88, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 48);
this.button1.TabIndex = 0;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.textBox1.Location = new System.Drawing.Point(16, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "foo.wmv";
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 2;
this.label1.Text = "Output file";
this.AcceptButton = this.button1;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 162);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "AsfFilter";
this.ResumeLayout(false);
this.PerformLayout();
}
Capture cam = null;
private void button1_Click(object sender, System.EventArgs e) {
const int VIDEODEVICE = 0;
Cursor.Current = Cursors.WaitCursor;
if (cam == null)
{
cam = new Capture(VIDEODEVICE, textBox1.Text);
cam.Start();
button1.Text = "Stop";
textBox1.ReadOnly = true;
}
else
{
button1.Text = "Start";
textBox1.ReadOnly = false;
cam.Pause();
cam.Dispose();
cam = null;
}
Cursor.Current = Cursors.Default;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
以上。