AlarmDecoder App Mplayer plugin tab

Contribute source code here. Anything not accompanied by source code will be removed.

AlarmDecoder App Mplayer plugin tab

Postby mathewss » Wed Mar 05, 2014 9:50 pm

Here is the mplayer webcam for Linux I put together for my wall touchscreen system running the AlarmDecoder App. I think I have pictures of it someplace on forums here and will link to it later on this post.
See this post for more details on how it all works.

The basic idea is you to create a window and tell the streaming software such as mplayer or on windows some webcam capture software etc to draw directly onto your window handle. This example is for linux and 'mplayer' but I am sure this could easily be done by just using a media capture plugin for c# and connecting directly to the capture device and this could be the base template for that work as it already creates the windows and does all the tab setup plugin stuff to embed into the AlarmDeocer App.


mplayer.cs
Code: Select all
///
///  Copyright (C) 2011 Nu Tech Software Solutions, Inc.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
///
///  DEVELOPED BY: Sean Mathews
///                  http://www.nutech.com/
///
///
/// Compile on linux using mono
///  gmcs mplayer.cs -t:library -r:../../ad2usbgui.exe -r:../../ModuleManager.dll \
///                  -pkg:gtk-sharp-2.0 -pkg:glade-sharp-2.0 -r:Mono.Posix \
///                  -resource:resources/glade-mplayer.glade
///
/// Compile on windows use csc
///
///  csc.exe /lib:"C:\Program Files (x86)\GtkSharp\2.12\lib\gtk-sharp-2.0"
///        /lib:"..\.."
///        /r:gtk-sharp.dll
///        /r:atk-sharp.dll
///        /r:gdk-sharp.dll
///        /r:glib-sharp.dll
///        /r:glade-sharp.dll
///        /r:Mono.Posix
///        /r:ad2usbgui.exe
///        /r:ModuleManager.dll
///        /target:library
///        /resource:resources/glade-mplayer.glade
///        mplayer.cs
///
/// </summary>
namespace ad2usbgui.dynamic.modules
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Xml;
    using System.Xml.Serialization;

    using ad2usbgui;
    using ad2usbgui.dynamic.api;

    using Gdk;

    using Glade;

    using Gtk;

    /// <summary>
    /// We will serialize our data from our config file into this class
    /// Quick and easy config file management.
    /// </summary>
    ///
    [Serializable]
    [XmlRoot("Configuration")]
    public class Configuration
    {
        #region Fields

        String _camera_1_url;
        String _camera_2_url;

        #endregion Fields

        #region Properties

        public string camera_1_url
        {
            get { return _camera_1_url; }
             set { _camera_1_url = value; }
        }

        public string camera_2_url
        {
            get { return _camera_2_url; }
             set { _camera_2_url = value; }
        }

        #endregion Properties

        #region Methods

        public static Configuration Deserialize(string file)
        {
            System.Xml.Serialization.XmlSerializer xs
                = new System.Xml.Serialization.XmlSerializer(typeof(Configuration));
            StreamReader reader = File.OpenText(file);
            Configuration c = (Configuration)xs.Deserialize(reader);
            reader.Close();
            return c;
        }

        public static void Serialize(string file, Configuration c)
        {
            System.Xml.Serialization.XmlSerializer xs
                = new System.Xml.Serialization.XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(file);
            xs.Serialize(writer, c);
            writer.Flush();
            writer.Close();
        }

        #endregion Methods
    }

    public class mplayer : ad2usbModule
    {
        #region Fields

        [Glade.Widget("fixed1")]
        Fixed fixedwindow1 = null;
        [Glade.Widget("fixed2")]
        Fixed fixedwindow2 = null;
        [Glade.Widget("MyMplayer")]
        VBox layout = null;
        Configuration myConfig;
        DateTime m_LastTime;
        System.Diagnostics.Process proc1;
        System.Diagnostics.Process proc2;
        cQuantumFoam QuantumFoam;
        private Gtk.Socket socket_cam1;
        private Gtk.Socket socket_cam2;
        private bool swapped = false;
        private int xid_cam1;
        private int xid_cam2;

        #endregion Fields

        #region Constructors

        public mplayer()
        {
        }

        #endregion Constructors

        #region Methods

        public void addnotebook()
        {
            string label = "Mplayer";
            HBox hbox = new HBox();
            Label notelabel = new Label(label);
            notelabel.Angle=45;

            notelabel.HeightRequest=60;
            notelabel.WidthRequest=80;
            hbox.PackStart(notelabel);

/*
          * Button close = new Button("X"); // Set this up with an image or whatever.
            close.Relief = ReliefStyle.None;
            close.HeightRequest=20;
            close.WidthRequest=20;
            close.FocusOnClick = false;
            close.Clicked +=delegate { layout.Destroy(); };
            hbox.PackStart(close);
*/
            hbox.ShowAll();

            Glade.XML gxml = new Glade.XML (null, "glade-mplayer.glade", "MyMplayer",null);
            gxml.Autoconnect(this);

            QuantumFoam.m_notebook.AppendPage(layout, hbox);
            gxml.Dispose();

            this.socket_cam1 = new Socket();
            this.socket_cam1.WidthRequest = 500;
            this.socket_cam1.HeightRequest = 400;
            this.socket_cam1.Visible = true;
            this.socket_cam1.Realized += new EventHandler(OnVideoWidgetRealizedCam1);
            this.fixedwindow1.Put(socket_cam1, 0, 0);
            //this.fixedwindow1.DoubleBuffered=true;
            //this.socket_cam1.DoubleBuffered=true;
         
         
            this.socket_cam2 = new Socket();
            this.socket_cam2.WidthRequest = 500;
            this.socket_cam2.HeightRequest = 400;
            this.socket_cam2.Visible = true;
            this.socket_cam2.Realized += new EventHandler(OnVideoWidgetRealizedCam2);
            this.fixedwindow2.Put(socket_cam2, 0, 0);
           
         //this.fixedwindow2.DoubleBuffered=true;
            //this.socket_cam2.DoubleBuffered=true;

            QuantumFoam.m_notebook.ShowAll();
            //-really-quiet -msglevel all=-1 scale=640:480 -really-quiet
            //-wid {0} scale=320:240 -vo xv http://192.168.3.5:8081/
                //
            var paramString1 = string.Format( myConfig.camera_1_url, xid_cam1);
            proc1 = new System.Diagnostics.Process();
            proc1.StartInfo.FileName = "/usr/bin/mplayer";
            proc1.StartInfo.Arguments = paramString1;
            proc1.Start();

            //QuantumFoam.m_MainWindow.InsertTextView(getName() + " " + getVersion() + " camera 1 '" + myConfig.camera_1_url + "'",false,true,true);
            Console.WriteLine("starting mplayer 1 " + paramString1);
            //"-wid {0} " +
            var paramString2 = string.Format(  myConfig.camera_2_url, xid_cam2);
            proc2 = new System.Diagnostics.Process();
            proc2.StartInfo.FileName = "/usr/bin/mplayer";
            proc2.StartInfo.Arguments = paramString2;
            proc2.Start();
            //QuantumFoam.m_MainWindow.InsertTextView(getName() + " " + getVersion() + " camera 2 '" + myConfig.camera_2_url + "'",false,true,true);
            Console.WriteLine("starting mplayer 2 " + paramString2);


        }

        public string getDescription()
        {
            return "Embedded mplayer plugin";
        }

        public string getName()
        {
            return "mplayer";
        }

        public string getVersion()
        {
            return "V1.0";
        }

        public int init(object qf)
        {
            QuantumFoam = qf as cQuantumFoam;

            // Read the configuration object from a file
            string configPath = System.IO.Path.Combine(
                 System.IO.Path.GetDirectoryName(QuantumFoam.m_exe.Location),
                 "modules" + System.IO.Path.DirectorySeparatorChar);
            myConfig = Configuration.Deserialize(configPath+"mplayerplugin.config");

            QuantumFoam.m_MainWindow.InsertTextView(getName() + " " + getVersion() + " initialized",false,true,true);
            addnotebook();
            QuantumFoam.IdleLoop += myIdleLoop;
         QuantumFoam.OnDestroy += destroy;
            return 1;
        }
      
      public void destroy()
      {
      
         try {
            Console.Write("Killing mplayer processes - 1 ");
            if (proc1 != null && !proc1.HasExited) {
               Mono.Unix.Native.Syscall.kill(proc1.Id, Mono.Unix.Native.Signum.SIGTERM);
            }
         } catch {
            Console.Write ("error ");
         } finally {
            Console.WriteLine("done");
         }
         
         try {
            Console.Write("Killing mplayer processes - 2 ");
            if (proc2 != null && !proc2.HasExited) {
               Mono.Unix.Native.Syscall.kill(proc2.Id, Mono.Unix.Native.Signum.SIGTERM);
            }
         } catch {
            Console.Write ("error ");
         } finally {
            Console.WriteLine("done");
         }
      }
      
      
      
        public int myIdleLoop()
        {
            DateTime now = DateTime.Now;
            TimeSpan duration = now - m_LastTime;
            if(duration.Seconds>10) {
                m_LastTime=now;
                // rotate through cameras
                /*
                this.fixedwindow1.HideAll();
                this.fixedwindow2.HideAll();

                if(swapped) {
                   Console.WriteLine("unswapped");

                  this.socket_cam1.Reparent(this.fixedwindow1);
                  this.socket_cam2.Reparent(this.fixedwindow2);

                  swapped=false;
                } else {
                  Console.WriteLine("swapped");
                  this.socket_cam1.Reparent(this.fixedwindow2);
                  this.socket_cam2.Reparent(this.fixedwindow1);
                  swapped=true;
                }

                this.fixedwindow1.ShowAll();
                this.fixedwindow2.ShowAll();
                */
            }
            return 1;
        }


        protected virtual void OnVideoWidgetRealizedCam1(object sender, EventArgs args)
        {
            this.xid_cam1 = (int)socket_cam1.Id;
        }

        protected virtual void OnVideoWidgetRealizedCam2(object sender, EventArgs args)
        {
            this.xid_cam2 = (int)socket_cam2.Id;
        }

        #endregion Methods
    }
}



glade-mplayer.glade
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.6 -->
  <!-- interface-naming-policy toplevel-contextual -->
  <widget class="GtkWindow" id="window1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">window1</property>
    <child>
      <widget class="GtkVBox" id="MyMplayer">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <widget class="GtkHBox" id="hbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <widget class="GtkFixed" id="fixed1">
                <property name="width_request">500</property>
                <property name="height_request">400</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </widget>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkFixed" id="fixed2">
                <property name="width_request">500</property>
                <property name="height_request">400</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </widget>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkHBox" id="hbox2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <widget class="GtkLabel" id="labelStatus">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="xalign">0.019999999552965164</property>
              </widget>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkProgressBar" id="progressbar">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="pulse_step">0.10000000149</property>
              </widget>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">1</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">1</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>



mplayerplugin.config
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<camera_1_url>-wid {0} -really-quiet -msglevel all=-1 -vo x11 -vf scale=500:-3 -lavfdopts analyzeduration=0 -demuxer lavf http://k1.tardus:8000/?action=stream.mjpg</camera_1_url>
<camera_2_url>-wid {0} -really-quiet -msglevel all=-1 -vo x11 -vf scale=500:-3 -lavfdopts analyzeduration=0 -demuxer lavf http://k1.tardus:8001/?action=stream.mjpg</camera_2_url>
</Configuration>
mathewss
Site Admin
Site Admin
 
Posts: 183
Joined: Fri Dec 06, 2013 11:14 am

Return to Code Contributions

Who is online

Users browsing this forum: No registered users and 2 guests

cron