示例#1
0
        public MainWindow()
        {
            InitializeComponent();

            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "ffmpeg服务端运行中...";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "ffmpeg服务端运行中...";
            //this.notifyIcon.Icon = new System.Drawing.Icon(@"favicon.ico");
            this.notifyIcon.Icon    = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            this.notifyIcon.Visible = true;
            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
            exit.Click += new EventHandler(Close);
            exit.Name   = "exit";
            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { exit };//open, minfrom,
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            this.WindowState   = System.Windows.WindowState.Maximized;
            this.Topmost       = false;
            this.ShowInTaskbar = false;
            this.Visibility    = System.Windows.Visibility.Hidden;
            this.Hide();

            string server     = Properties.Settings.Default.ServerHost;
            int    serverPort = Properties.Settings.Default.ServerPort;
            int    thisPort   = Properties.Settings.Default.ThisPort;

            udpclient = new UdpClientClass(server, serverPort, thisPort, 3000);

            CommandSendlog cmdlog = new CommandSendlog()
            {
                CmdIp   = Properties.Settings.Default.ThisHost,
                CmdMac  = Properties.Settings.Default.ThisMacAddr,
                CmdStr  = "request",
                CmdPort = Properties.Settings.Default.ThisPort,
                CmdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                CmdType = "0",          // FFmpegServer
            };

            CmdMessage msg = new CmdMessage()
            {
                CmdID    = DateTime.Now.ToString("yyyyMMdd-HHmmss"),
                CmdType  = "CommandSendlog",
                JsonText = JsonHelper.Serialize(cmdlog)
            };

            string str = JsonHelper.Serialize(msg);

            byte[] senddata = Encoding.UTF8.GetBytes(str);
            udpclient.Send(senddata, senddata.Length);
        }
示例#2
0
        private void ThreadFunc_Recv()//接收数据做服务
        {
            string dir  = System.Windows.Forms.Application.StartupPath;
            string file = "log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            string path = System.IO.Path.Combine(dir, file);

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, true))
            {
                while (IsReceiving)
                {
                    IPAddress  ipaddr          = ServerIPE.Address;
                    IPEndPoint remoteIPE       = new IPEndPoint(ipaddr, 0);
                    int        buffSizeCurrent = UdpListenClient.Client.Available;
                    if (buffSizeCurrent > 0)
                    {
                        try
                        {
                            byte[] data_recv = UdpListenClient.Receive(ref remoteIPE);                                // UDP接收数据
                            if (data_recv.Length > 0 && remoteIPE.Address.ToString() == ServerIPE.Address.ToString()) // 只处理特定的服务端的数据
                            {
                                string     cmdstr = Encoding.UTF8.GetString(data_recv);                               // 接收命令数据
                                CmdMessage msg    = JsonHelper.Deserialize <CmdMessage>(cmdstr);
                                switch (msg.CmdType)
                                {
                                case "VideoCaptureCommand":
                                    sw.WriteLine("VideoCaptureCommand:  " + msg.JsonText);
                                    VideoCaptureCommand cmd = JsonHelper.Deserialize <VideoCaptureCommand>(msg.JsonText);
                                    if (cmd.CmdType == 1)       // BeginVideo
                                    {
                                        string kk = cmd.ClassroomID + "";
                                        if (_ProcessDic.ContainsKey(kk))
                                        {
                                            Process p = _ProcessDic[kk];
                                            p.StandardInput.WriteLine("{0}", "q");
                                            p.Dispose();

                                            _ProcessDic.Remove(kk);
                                        }

                                        string exe  = @"ffmpeg.exe";
                                        string dir2 = Properties.Settings.Default.CameraFilePath;
                                        if (msg.CmdID == "Screen")
                                        {
                                            dir2 = Properties.Settings.Default.ScreenFilePath;
                                        }
                                        string file2 = cmd.VideoFile;
                                        string path2 = System.IO.Path.Combine(dir2, file2);

                                        if (!Directory.Exists(dir2))
                                        {
                                            Directory.CreateDirectory(dir2);
                                        }
                                        string ifmt       = (msg.CmdID == "Screen") ? "-f gdigrab" : "";
                                        string iduration  = (cmd.VideoDuration > 0) ? string.Format("-t {0}", cmd.VideoDuration) : "";
                                        string input      = (msg.CmdID == "Screen") ? "desktop" : string.Format("\"rtsp://{0}:{1}@{2}:{3}/cam/realmonitor?channel=1&subtype=0\"", cmd.TargetUser, cmd.TargetPass, cmd.TargetIP, cmd.TargetPort);
                                        string outputArgs = "-vcodec libx264 -pix_fmt yuv420p -acodec aac";
                                        string output     = string.Format("\"{0}\"", path2);
                                        // ffmpeg -t 120 -i "rtsp://*****:*****@192.168.0.151:554/cam/realmonitor?channel=1&subtype=0" -vcodec libx264 -pix_fmt yuv420p -acodec aac "D:\Workspace\web\nginx_v1.7.11.3_Gryphon\nginx-rtmp-module\tmp\rec\y.mp4"
                                        // ffmpeg -f gdigrab -t 120 -i desktop -vcodec libx264 -pix_fmt yuv420p -acodec aac xxxyyy.mp4
                                        string  args = string.Format(@" {0} {1} -i {2} {3} {4}", ifmt, iduration, input, outputArgs, output);
                                        Process proc = this.cmd.Execute2(exe, args);

                                        _ProcessDic.Add(kk, proc);
                                    }
                                    else if (cmd.CmdType == 2)      // StopVideo
                                    {
                                        string kk = cmd.ClassroomID + "";
                                        if (_ProcessDic.ContainsKey(kk))
                                        {
                                            Process p = _ProcessDic[kk];
                                            p.StandardInput.WriteLine("{0}", "q");
                                            p.Dispose();

                                            _ProcessDic.Remove(kk);
                                        }
                                    }
                                    break;

                                case "CommandSendlog":
                                    sw.WriteLine("CommandSendlog:  " + msg.JsonText);
                                    CommandSendlog cmdlog = JsonHelper.Deserialize <CommandSendlog>(msg.JsonText);
                                    break;

                                default:
                                    sw.WriteLine(cmdstr);
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            sw.WriteLine(ex.StackTrace);
                        }
                    }

                    Thread.Sleep(50);
                }
            }
        }