//Bakalarska prace

//TEMA:
//UZIVATELSKY PROGRAM PRO ZOBRAZENI
//A ZPRACOVANI KVANTITATIVNI FAZE
//Z DIGITALNIHO HOLOGRAFICKEHO MIKROSKOPU

//FAKULTA STROJNIHO INZENYRSTVI
//USTAV FYZIKALNIHO INZENYRSTVI

//Jmeno:		Hladik Lukas
//Skupina:		3E/91
//Verze:		1.0
//Datum:		27. kvetna 2010


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuartzTypeLib;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class nahled_videa : Form
    {


        private FilgraphManager m_objFilterGraph = null;
        private IBasicAudio m_objBasicAudio = null;
        private IVideoWindow m_objVideoWindow = null;
        private IMediaEvent m_objMediaEvent = null;
        private IMediaEventEx m_objMediaEventEx = null;
        private IMediaPosition m_objMediaPosition = null;
        private IMediaControl m_objMediaControl = null;
       

        enum MediaStatus { None, Stopped, Paused, Running };


        private const int WM_APP = 0x8000;
        private const int WM_GRAPHNOTIFY = WM_APP + 1;
        private const int EC_COMPLETE = 0x01;
        private const int WS_CHILD = 0x40000000;
        private const int WS_CLIPCHILDREN = 0x2000000;

        private MediaStatus m_CurrentStatus = MediaStatus.None;

        
        public nahled_videa()
        {
            InitializeComponent();
        }


        //private void CleanUp()
        //{
        //    if (m_objMediaControl != null)
        //        m_objMediaControl.Stop();

        //    m_CurrentStatus = MediaStatus.Stopped;

        //    if (m_objMediaEventEx != null)
        //        m_objMediaEventEx.SetNotifyWindow(0, 0, 0);

        //    if (m_objVideoWindow != null)
        //    {
        //        m_objVideoWindow.Visible = 0;
        //        m_objVideoWindow.Owner = 0;
        //    }

        //    if (m_objMediaControl != null) m_objMediaControl = null;
        //    if (m_objMediaPosition != null) m_objMediaPosition = null;
        //    if (m_objMediaEventEx != null) m_objMediaEventEx = null;
        //    if (m_objMediaEvent != null) m_objMediaEvent = null;
        //    if (m_objVideoWindow != null) m_objVideoWindow = null;
        //    if (m_objBasicAudio != null) m_objBasicAudio = null;
        //    if (m_objFilterGraph != null) m_objFilterGraph = null;
        //}

        private void CleanUp()
        {
            if (this.m_objMediaControl != null)
                this.m_objMediaControl.Stop();

            this.m_CurrentStatus = MediaStatus.Stopped;

            if (this.m_objMediaEventEx != null)
                this.m_objMediaEventEx.SetNotifyWindow(0, 0, 0);

            if (this.m_objVideoWindow != null)
            {
                this.m_objVideoWindow.Visible = 0;
                this.m_objVideoWindow.Owner = 0;
            }

            if (this.m_objMediaControl != null) this.m_objMediaControl = null;
            if (this.m_objMediaPosition != null) this.m_objMediaPosition = null;
            if (this.m_objMediaEventEx != null) this.m_objMediaEventEx = null;
            if (this.m_objMediaEvent != null) this.m_objMediaEvent = null;
            if (this.m_objVideoWindow != null) this.m_objVideoWindow = null;
            // if (this.m_objBasicAudio != null) this.m_objBasicAudio = null;
            if (this.m_objFilterGraph != null) this.m_objFilterGraph = null;
            this.m_objFilterGraph = null;

            if (m_objFilterGraph != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objFilterGraph);
            if (m_objMediaControl != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaControl);
            if (m_objVideoWindow != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objVideoWindow);
            if (m_objMediaEventEx != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEventEx);
            if (m_objMediaPosition != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaPosition);
            if (m_objMediaEvent != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEvent);

            //while (System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objFilterGraph) > 0);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaControl);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objVideoWindow);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEventEx);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEventEx);

            //m_objMediaPosition

        }





        private void UpdateStatusBar()
        {
            switch (m_CurrentStatus)
            {
                case MediaStatus.None: statusBarPanel1.Text = "Zastaveno"; break;
                case MediaStatus.Paused: statusBarPanel1.Text = "Pauza"; break;
                case MediaStatus.Running: statusBarPanel1.Text = "Přehrávání"; break;
                case MediaStatus.Stopped: statusBarPanel1.Text = "Zastaveno"; break;
            }

            if (m_objMediaPosition != null)
            {
                int s = (int)m_objMediaPosition.Duration;
                int h = s / 3600;
                int m = (s - (h * 3600)) / 60;
                s = s - (h * 3600 + m * 60);

                statusBarPanel2.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);

                s = (int)m_objMediaPosition.CurrentPosition;
                h = s / 3600;
                m = (s - (h * 3600)) / 60;
                s = s - (h * 3600 + m * 60);

                trackBar1.Maximum = (int)m_objMediaPosition.Duration + 1;
                trackBar1.Value = (int)m_objMediaPosition.CurrentPosition;
                statusBarPanel3.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
                

            }
            else
            {
                statusBarPanel2.Text = "00:00:00";
                statusBarPanel3.Text = "00:00:00";
            }
        }

        
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

          
        }

        private void nahled_videa_SizeChanged(object sender, System.EventArgs e)
        {
            if (m_objVideoWindow != null)
            {
                m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left,
                    panel1.ClientRectangle.Top+10,
                    panel1.ClientRectangle.Width,
                    panel1.ClientRectangle.Height);
            }
        }



        private void trackBar1_Scroll(object sender, EventArgs e)
        {

            m_objMediaPosition.CurrentPosition = trackBar1.Value;
        }


        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_GRAPHNOTIFY)
            {
                int lEventCode;
                int lParam1, lParam2;

                while (true)
                {
                    try
                    {
                        m_objMediaEventEx.GetEvent(out lEventCode,
                            out lParam1,
                            out lParam2,
                            0);

                        m_objMediaEventEx.FreeEventParams(lEventCode, lParam1, lParam2);

                        if (lEventCode == EC_COMPLETE)
                        {
                            m_objMediaControl.Stop();
                            m_objMediaPosition.CurrentPosition = 0;
                            m_CurrentStatus = MediaStatus.Stopped;
                            UpdateStatusBar();
                            
                        }
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
            }

            base.WndProc(ref m);
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            if (m_CurrentStatus == MediaStatus.Running)
            {
                UpdateStatusBar();
            }
        }
                
        
 
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
                                 
        }

        private void nahled_videa_Load(object sender, EventArgs e)
        {

            string nazev_videa = "tempVid.avi"; //vstupni data

            if (nazev_videa != null)
            {

                CleanUpVideoPreview();
                
                //CleanUp();

                m_objFilterGraph = new FilgraphManager();
                m_objFilterGraph.RenderFile(nazev_videa);

                m_objBasicAudio = m_objFilterGraph as IBasicAudio;

                try
                {
                    m_objVideoWindow = m_objFilterGraph as IVideoWindow;
                    m_objVideoWindow.Owner = (int)panel1.Handle;
                    m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
                    m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left,
                        panel1.ClientRectangle.Top + 10,
                        panel1.ClientRectangle.Width,
                        panel1.ClientRectangle.Height);
                }
                catch (Exception)
                {
                    m_objVideoWindow = null;
                }

                m_objMediaEvent = m_objFilterGraph as IMediaEvent;

                m_objMediaEventEx = m_objFilterGraph as IMediaEventEx;
                m_objMediaEventEx.SetNotifyWindow((int)this.Handle, WM_GRAPHNOTIFY, 0);

                m_objMediaPosition = m_objFilterGraph as IMediaPosition;

                m_objMediaControl = m_objFilterGraph as IMediaControl;

                m_objMediaControl.Run();
                m_CurrentStatus = MediaStatus.Running;

                UpdateStatusBar();
                //UpdateToolBar();



                //MessageBox.Show("ahoj");
            }
            
        }

        private void trackBar1_Scroll_1(object sender, EventArgs e)
        {
            m_objMediaPosition.CurrentPosition = trackBar1.Value; 
        }

        private void nahled_videa_Click(object sender, EventArgs e)
        {

            
            
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            m_objMediaControl.Pause();
        }

        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_objMediaControl.Pause();
        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {

        }

        private void nahled_videa_FormClosed(object sender, FormClosedEventArgs e)
        {
            
            Dispose();
            m_objMediaControl.Stop();
            CleanUpVideoPreview();

        }


        private void CleanUpVideoPreview()
        {
            if (m_objFilterGraph != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objFilterGraph);
                m_objFilterGraph = null;
            }

            if (m_objMediaControl != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaControl);
                m_objMediaControl = null;
            }


            if (m_objVideoWindow != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objVideoWindow);
                m_objVideoWindow = null;
            }



            if (m_objMediaEventEx != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEventEx);
                m_objMediaEventEx = null;
            }


            if (m_objMediaPosition != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaPosition);
                m_objMediaPosition = null;
            }


            if (m_objMediaEvent != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objMediaEvent);
                m_objMediaEvent = null;
            }


        }



        
        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {

        }

        private void nahled_videa_Leave(object sender, EventArgs e)
        {
            MessageBox.Show("");
        }
    }
}
