Home » Source Code » PowerPoint Controler » VideoStream.cs

VideoStream.cs ( File view )

Description:A s...
  • By vaselly 2014-09-16
  • View(s):40
  • Download(s):0
  • Point(s): 1
			// Motion Detector
//
// Copyright  Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//
namespace VideoSource
{

	using System;
	using System.Drawing;
	using System.Drawing.Imaging;
	using System.IO;
	using System.Threading;
	using System.Runtime.InteropServices;
	using System.Net;

	using dshow;
	using dshow.Core;

	/// 
	/// VideoStream - stream downloader
	/// 
	public class VideoStream : IVideoSource
	{

		private string	source;
		private object	userData = null;
		private int		framesReceived;

		private Thread	thread = null;
		private ManualResetEvent stopEvent = null;

		// new frame event
		public event CameraEventHandler NewFrame;

		// VideoSource property
		public virtual string VideoSource
		{

			get {
 return source; 
}
			set {
 source = value; 
}
		
}
		// Login property
		public string Login
		{

			get {
 return null; 
}
			set {
 
}
		
}
		// Password property
		public string Password
		{

			get {
 return null; 
}
			set {
  
}
		
}
		// FramesReceived property
		public int FramesReceived
		{

			get
			{

				int frames = framesReceived;
				framesReceived = 0;
				return frames;
			
}
		
}
		// BytesReceived property
		public int BytesReceived
		{

			get {
 return 0; 
}
		
}
		// UserData property
		public object UserData
		{

			get {
 return userData; 
}
			set {
 userData = value; 
}
		
}
		// Get state of the video source thread
		public bool Running
		{

			get
			{

				if (thread != null)
				{

					if (thread.Join(0) == false)
						return true;

					// the thread is not running, so free resources
					Free();
				
}
				return false;
			
}
		
}


		// Constructor
		public VideoStream()
		{

		
}

		// Start work
		public void Start()
		{

			if (thread == null)
			{

				framesReceived = 0;

				// create events
				stopEvent	= new ManualResetEvent(false);
				
				// create and start new thread
				thread = new Thread(new ThreadStart(WorkerThread));
				thread.Name = source;
				thread.Start();
			
}
		
}

		// Signal thread to stop work
		public void SignalToStop()
		{

			// stop thread
			if (thread != null)
			{

				// signal to stop
				stopEvent.Set();
			
}
		
}

		// Wait for thread stop
		public void WaitForStop()
		{

			if (thread != null)
			{

				// wait for thread stop
				thread.Join();

				Free();
			
}
		
}

		// Abort thread
		public void Stop()
		{

			if (this.Running)
			{

				thread.Abort();
				// WaitForStop();
			
}
		
}

		// Free resources
		private void Free()
		{

			thread = null;

			// release events
			stopEvent.Close();
			stopEvent = null;
		
}

		// Thread entry point
		public void WorkerThread()
		{

			bool	failed = false;

			// grabber
			Grabber	grabber = new Grabber(this);

			// objects
			object	graphObj = null;
			object	sourceObj = null;
			object	grabberObj = null;

			// interfaces
			IGraphBuilder	graph = null;
			IBaseFilter		sourceBase = null;
			IBaseFilter		grabberBase = null;
			ISampleGrabber	sg = null;
			IFileSourceFilter	fileSource = null;
			IMediaControl	mc = null;
			IMediaEventEx	mediaEvent = null;

			int	code, param1, param2;

			while ((!failed) && (!stopEvent.WaitOne(0, true)))
			{

				try
				{

					// Get type for filter graph
					Type srvType = Type.GetTypeFromCLSID(Clsid.FilterGraph);
					if (srvType == null)
						throw new ApplicationException("Failed creating filter graph");

					// create filter graph
					graphObj = Activator.CreateInstance(srvType);
					graph = (IGraphBuilder) graphObj;

					// Get type for windows media source filter
					srvType = Type.GetTypeFromCLSID(Clsid.WindowsMediaSource);
					if (srvType == null)
						throw new ApplicationException("Failed creating WM source");

					// create windows media source filter
					sourceObj = Activator.CreateInstance(srvType);
					sourceBase = (IBaseFilter) sourceObj;

					// Get type for sample grabber
					srvType = Type.GetTypeFromCLSID(Clsid.SampleGrabber);
					if (srvType == null)
						throw new ApplicationException("Failed creating sample grabber");

					// create sample grabber
					grabberObj = Activator.CreateInstance(srvType);
					sg = (ISampleGrabber) grabberObj;
					grabberBase = (IBaseFilter) grabberObj;

					// add source filter to graph
					graph.AddFilter(sourceBase, "source");
					graph.AddFilter(grabberBase, "grabber");

					// set media type
					AMMediaType mt = new AMMediaType();
					mt.majorType = MediaType.Video;
					mt.subType = MediaSubType.RGB24;
					sg.SetMediaType(mt);

					// load file
					fileSource = (IFileSourceFilter) sourceObj;
					fileSource.Load(this.source, null);

					// connect pins
					if (graph.Connect(DSTools.GetOutPin(sourceBase, 0), DSTools.GetInPin(grabberBase, 0)) < 0)
						throw new ApplicationException("Failed connecting filters");

					// get media type
					if (sg.GetConnectedMediaType(mt) == 0)
					{

						VideoInfoHeader vih = (VideoInfoHeader) Marshal.PtrToStructure(mt.formatPtr, typeof(VideoInfoHeader));

						grabber.Width = vih.BmiHeader.Width;
						grabber.Height = vih.BmiHeader.Height;
						mt.Dispose();
					
}

					// render
					graph.Render(DSTools.GetOutPin(grabberBase, 0));

					//
					sg.SetBufferSamples(false);
					sg.SetOneShot(false);
					sg.SetCallback(grabber, 1);

					// window
					IVideoWindow win = (IVideoWindow) graphObj;
					win.put_AutoShow(false);
					win = null;

					// get events interface
					mediaEvent = (IMediaEventEx) graphObj;

					// get media control
					mc = (IMediaControl) graphObj;

					// run
					mc.Run();

					while (!stopEvent.WaitOne(0, true))
					{

						Thread.Sleep(100);

						// get an event
						if (mediaEvent.GetEvent(out code, out param1, out param2, 0) == 0)
						{

							// release params
							mediaEvent.FreeEventParams(code, param1, param2);

							//
							if (code == (int) EventCode.Complete)
							{

								System.Diagnostics.Debug.WriteLine("completed");
								break;
							
}
						
}
					
}

					mc.StopWhenReady();
				
}
					// catch any exceptions
				catch (Exception e)
				{

					System.Diagnostics.Debug.WriteLine("----: " + e.Message);
					failed = true;
				
}
					// finalization block
				finally
				{

					// release all objects
					mediaEvent = null;
					mc = null;
					fileSource = null;
					graph = null;
					sourceBase = null;
					grabberBase = null;
					sg = null;

					if (graphObj != null)
					{

						Marshal.ReleaseComObject(graphObj);
						graphObj = null;
					
}
					if (sourceObj != null)
					{

						Marshal.ReleaseComObject(sourceObj);
						sourceObj = null;
					
}
					if (grabberObj != null)
					{

						Marshal.ReleaseComObject(grabberObj);
						grabberObj = null;
					
}
				
}
			
}
		
}

		// new frame
		protected void OnNewFrame(Bitmap image)
		{

			framesReceived++;
			if ((!stopEvent.WaitOne(0, true)) && (NewFrame != null))
				NewFrame(this, new CameraEventArgs(image));
		
}

		// Grabber
		private class Grabber : ISampleGrabberCB
		{

			private VideoStream parent;
			private int width, height;

			// Width property
			public int Width
			{

				get {
 return width; 
}
				set {
 width = value; 
}
			
}
			// Height property
			public int Height
			{

				get {
 return height; 
}
				set {
 height = value; 
}
			
}

			// Constructor
			public Grabber(VideoStream parent)
			{

				this.parent = parent;
			
}

			//
			public int SampleCB(double SampleTime, IntPtr pSample)
			{

				return 0;
			
}

			// Callback method that receives a pointer to the sample buffer
			public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
			{

				// create new image
				System.Drawing.Bitmap img = new Bitmap(width, height, PixelFormat.Format24bppRgb);

				// lock bitmap data
				BitmapData	bmData = img.LockBits(
					new Rectangle(0, 0, width, height),
					ImageLockMode.ReadWrite,
					PixelFormat.Format24bppRgb);

				// copy image data
				int srcStride = bmData.Stride;
				int dstStride = bmData.Stride;

				int dst = bmData.Scan0.ToInt32() + dstStride * (height - 1);
				int src = pBuffer.ToInt32();

				for (int y = 0; y < height; y++)
				{

					Win32.memcpy(dst, src, srcStride);
					dst -= dstStride;
					src += srcStride;
				
}

				// unlock bitmap data
				img.UnlockBits(bmData);

				// notify parent
				parent.OnNewFrame(img);

				// release the image
				img.Dispose();

				return 0;
			
}
		
}
	
}

}
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
01.96 kB
AssemblyInfo.cs2.37 kB2006-04-11 11:57
01.96 kB
01.96 kB
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
01.96 kB
dshow.dll32.00 kB2007-06-07 20:26
01.96 kB
1.txt45.00 B2006-04-11 11:57
IBaseFilter.cs2.14 kB2006-04-11 11:57
IBasicVideo.cs4.70 kB2006-04-11 11:57
ICreateDevEnum.cs592.00 B2006-04-11 11:57
IEnumPins.cs831.00 B2006-04-11 11:57
IFileSourceFilter.cs861.00 B2006-04-11 11:57
IFilterGraph.cs1.58 kB2006-04-11 11:57
IGraphBuilder.cs2.84 kB2006-04-11 11:57
IMediaControl.cs1.49 kB2006-04-11 11:57
IMediaEvent.cs3.06 kB2006-04-11 11:57
IMediaPosition.cs1.88 kB2006-04-11 11:57
IMediaSeeking.cs2.79 kB2006-04-11 11:57
IPin.cs2.36 kB2006-04-11 11:57
IPropertyBag.cs853.00 B2006-04-11 11:57
ISampleGrabber.cs2.21 kB2006-04-11 11:57
IVideoFrameStep.cs747.00 B2006-04-11 11:57
IVideoWindow.cs5.90 kB2006-04-11 11:57
Structures.cs4.37 kB2006-04-11 11:57
Uuids.cs4.87 kB2006-04-11 11:57
Win32.cs919.00 B2006-04-11 11:57
dshow.csproj5.38 kB2007-03-16 20:38
Filter.cs2.57 kB2006-04-11 11:57
FilterCollection.cs2.14 kB2006-04-11 11:57
01.96 kB
01.96 kB
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
01.96 kB
dshow.csproj.FileList.txt216.00 B2007-06-15 15:17
01.96 kB
dshow.dll32.00 kB2007-06-07 20:26
01.96 kB
dshow.dll28.00 kB2007-06-07 18:09
01.96 kB
Tools.cs1.11 kB2006-04-11 11:57
motion.sln1.79 kB2007-03-16 20:38
motion.suo35.00 kB2007-06-15 15:08
01.96 kB
AboutForm.cs11.91 kB2007-06-15 15:17
AboutForm.resx9.77 kB2007-06-15 15:17
App.ico26.07 kB2006-04-11 11:57
AssemblyInfo.cs2.47 kB2007-03-20 08:40
01.96 kB
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
App.ico26.07 kB2006-04-11 11:57
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
motion.exe132.00 kB2007-06-15 15:17
motion.exe.config147.00 B2007-03-19 09:20
motion.pdb181.50 kB2007-06-15 15:17
motion.vshost.exe5.50 kB2005-09-23 07:56
motion.vshost.exe.config147.00 B2007-03-19 09:20
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
dshow.dll32.00 kB2007-06-07 20:26
motion.exe124.00 kB2007-06-07 20:40
motion.exe.config147.00 B2007-03-19 09:20
motion.vshost.exe5.50 kB2005-09-23 07:56
motion.vshost.exe.config147.00 B2007-03-19 09:20
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
Camera.cs3.47 kB2006-04-11 11:57
CameraWindow.cs5.17 kB2006-04-11 11:57
CameraWindow.resx5.55 kB2006-04-11 11:57
CaptureDeviceForm.cs4.49 kB2007-06-15 15:17
CaptureDeviceForm.resx5.37 kB2006-04-11 11:57
ClassDiagram1.cd1.00 B2007-06-07 18:39
Copyright.txt913.00 B2007-06-15 15:17
IMotionDetector.cs785.00 B2007-06-07 20:36
MainForm.cs21.34 kB2007-06-13 21:54
MainForm.resx45.81 kB2007-06-13 21:51
motion.csproj7.33 kB2007-06-07 18:45
MotionDetector1.cs9.95 kB2007-06-07 20:37
MotionDetector2.cs4.44 kB2007-06-07 20:38
MotionDetector3.cs5.06 kB2007-06-07 20:38
MotionDetector3Optimized.cs6.57 kB2007-06-07 20:38
MotionDetector4.cs4.89 kB2007-06-07 20:38
01.96 kB
01.96 kB
motion.AboutForm.resources3.00 kB2007-06-15 15:17
motion.CameraWindow.resources581.00 B2007-06-13 21:51
motion.CaptureDeviceForm.resources279.00 B2007-06-13 21:51
motion.csproj.GenerateResource.Cache1.00 kB2007-06-15 15:17
motion.exe132.00 kB2007-06-15 15:17
motion.MainForm.resources26.62 kB2007-06-13 21:51
motion.pdb181.50 kB2007-06-15 15:17
motion.Resources.1.gif828.00 B2006-04-11 11:57
motion.Resources.2.gif822.00 B2006-04-11 11:57
motion.Resources.3.gif830.00 B2006-04-11 11:57
motion.Resources.4.gif822.00 B2006-04-11 11:57
motion.Resources.5.gif822.00 B2006-04-11 11:57
motion.Resources.6.gif830.00 B2006-04-11 11:57
motion.Resources.7.gif821.00 B2006-04-11 11:57
motion.Resources.8.gif822.00 B2006-04-11 11:57
motion.Resources.9.gif822.00 B2006-04-11 11:57
motion.URLForm.resources269.00 B2007-06-13 21:51
ResolveAssemblyReference.cache13.42 kB2007-06-15 14:58
01.96 kB
motion.csproj.FileList.txt1.64 kB2007-06-15 15:17
01.96 kB
motion.AboutForm.resources3.00 kB2007-06-07 20:26
motion.CameraWindow.resources581.00 B2007-06-07 20:26
motion.CaptureDeviceForm.resources279.00 B2007-06-07 20:26
motion.csproj.GenerateResource.Cache1.00 kB2007-06-07 20:27
motion.exe124.00 kB2007-06-07 20:40
motion.MainForm.resources26.62 kB2007-06-07 20:27
motion.Resources.1.gif828.00 B2006-04-11 11:57
motion.Resources.2.gif822.00 B2006-04-11 11:57
motion.Resources.3.gif830.00 B2006-04-11 11:57
motion.Resources.4.gif822.00 B2006-04-11 11:57
motion.Resources.5.gif822.00 B2006-04-11 11:57
motion.Resources.6.gif830.00 B2006-04-11 11:57
motion.Resources.7.gif821.00 B2006-04-11 11:57
motion.Resources.8.gif822.00 B2006-04-11 11:57
motion.Resources.9.gif822.00 B2006-04-11 11:57
motion.URLForm.resources269.00 B2007-06-07 20:26
01.96 kB
ResolveAssemblyReference.cache10.44 kB2007-06-07 20:26
01.96 kB
01.96 kB
1.gif828.00 B2006-04-11 11:57
2.gif822.00 B2006-04-11 11:57
3.gif830.00 B2006-04-11 11:57
4.gif822.00 B2006-04-11 11:57
5.gif822.00 B2006-04-11 11:57
6.gif830.00 B2006-04-11 11:57
7.gif821.00 B2006-04-11 11:57
8.gif822.00 B2006-04-11 11:57
9.gif822.00 B2006-04-11 11:57
UnsafeBitmap.cs2.76 kB2007-06-15 14:58
URLForm.cs3.98 kB2006-04-11 11:57
URLForm.resx5.36 kB2006-04-11 11:57
01.96 kB
ByteArrayUtils.cs1.25 kB2006-04-11 11:57
CameraEvents.cs626.00 B2006-04-11 11:57
CaptureDevice.cs8.15 kB2006-04-11 11:57
IVideoSource.cs1.57 kB2006-04-11 11:57
JPEGStream.cs6.89 kB2006-04-11 11:57
MJPEGStream.cs9.12 kB2006-04-11 11:57
VideoFileSource.cs3.45 kB2006-04-11 11:57
VideoStream.cs8.47 kB2006-04-11 11:57
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
01.96 kB
AssemblyInfo.cs2.37 kB2006-04-11 11:57
AVIReader.cs5.29 kB2006-04-11 11:57
AVIWriter.cs5.59 kB2006-04-11 11:57
01.96 kB
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
01.96 kB
01.96 kB
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
01.96 kB
Tiger.Video.VFW.dll20.00 kB2007-06-07 18:09
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
vfw.csproj.FileList.txt276.00 B2007-06-15 15:17
vfw.csproj3.55 kB2007-03-16 20:38
Win32.cs9.81 kB2006-04-11 11:57
...
Sponsored links

VideoStream.cs (805.82 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D