using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using WMEncoderLib;
using WMPREVIEWLib;

namespace vclass.broadcast
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		//WMEncoderApp EncoderApp;
		IWMEncoder Encoder;
		IWMEncSourceGroupCollection SrcGrpColl;
		IWMEncSourceGroup SrcGrp; 
		IWMEncSource SrcAud;
		IWMEncVideoSource2 SrcVid ; 
		IWMEncFile File;
		IWMEncProfile Pro;
		int lpreviewStream;
		WMEncDataView Preview;
		IWMEncDataViewCollection DVColl_preview;


		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Panel Panel_Preview;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		IWMEncProfileCollection ProColl;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.Panel_Preview = new System.Windows.Forms.Panel();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button1.Location = new System.Drawing.Point(136, 272);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(104, 56);
			this.button1.TabIndex = 0;
			this.button1.Text = "Start Broadcast";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Panel_Preview
			// 
			this.Panel_Preview.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.Panel_Preview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.Panel_Preview.Location = new System.Drawing.Point(32, 24);
			this.Panel_Preview.Name = "Panel_Preview";
			this.Panel_Preview.Size = new System.Drawing.Size(320, 240);
			this.Panel_Preview.TabIndex = 1;
			this.Panel_Preview.Paint += new System.Windows.Forms.PaintEventHandler(this.Panel_Preview_Paint);
			// 
			// button2
			// 
			this.button2.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button2.Location = new System.Drawing.Point(32, 272);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(104, 56);
			this.button2.TabIndex = 2;
			this.button2.Text = "Watch Video";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
			this.button3.Location = new System.Drawing.Point(240, 272);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(112, 56);
			this.button3.TabIndex = 3;
			this.button3.Text = "Stop Broadcast";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.InactiveCaption;
			this.ClientSize = new System.Drawing.Size(386, 352);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.Panel_Preview);
			this.Controls.Add(this.button1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Form1";
			this.Text = "Video";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		try 
		 {
			
			 // Create WMEncoderApp and WMEncoder objects.
			//EncoderApp = new WMEncoderApp();
			//Encoder = EncoderApp.Encoder;
			Encoder = new WMEncoder();

			 // Display the predefined Encoder UI.
			// EncoderApp.Visible = true;

			 // Specify the source for the input stream.
			SrcGrpColl = Encoder.SourceGroupCollection;
			SrcGrp = SrcGrpColl.Add("SG_1");
			SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
			SrcVid = (IWMEncVideoSource2)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
			
			SrcAud.SetInput("Default_Audio_Device", "Device", "");
			SrcVid.SetInput("Default_Video_Device", "Device", "");
			
			 // Specify a profile.
			ProColl = Encoder.ProfileCollection;
			Pro = ProColl.Item(3);
			SrcGrp.set_Profile(Pro);

		
			DVColl_preview = SrcVid.PreviewCollection;
			Preview = new WMEncDataView();
			lpreviewStream = DVColl_preview.Add(Preview);
			 // Create a broadcast.	
			 Encoder.Broadcast.set_PortNumber(WMENC_BROADCAST_PROTOCOL.WMENC_PROTOCOL_HTTP, 10140);

			 //Saves the encoded video and audio to a file
			 File = Encoder.File;
			 File.LocalFileName = "C:\\SavedVideo.wmv";
			
			 //Bunu ne yapacagıma karar vermedim daha
			 //Encoder.PrepareToEncode(false);

			 // Start the encoding process.
			 
			Encoder.PrepareToEncode(true);
	
			//Encoder.Start();
			Preview.SetViewProperties(lpreviewStream, (int)Panel_Preview.Handle);

			 // Use this statement so that the Windows Media Encoder window does not close until you press Enter.
			 Console.WriteLine("Press Enter to close this window.");
			 Console.ReadLine();
		 } 

		 catch (Exception f) 
		 {  
			 Console.WriteLine(f.StackTrace);
		 }
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
		Encoder.PrepareToEncode(true);
			Encoder.Start();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
		Preview.StartView(lpreviewStream);
		}

		private void Panel_Preview_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
		Encoder.Stop();
		}
	}
}
