コード例 #1
0
        void sock_XmlDataArrived(TSocket sock, TSocket.DataPacket data)
        {
            switch (data.Type)
            {
            case "Unity.Packets.Error":
                var err = data.GetObject <Packets.Error>();
                Trace.WriteLine("Error: " + err.Message);
                OnError(err.Message);
                break;

            case "Unity.Packets.FrameNotReady":
                Trace.WriteLine("up to date, requesting again...");
                var req = new Packets.Request();
                req.Type = Packets.Request.RequestType.Frame;
                req.UID  = UID;
                sock.Send(req);
                break;

            case "Unity.Packets.Capabilities":

                var caps = data.GetObject <Packets.Capabilities>();

                Trace.WriteLine("Got caps: " + caps.Caps);

                for (int i = 0; i < caps.Modes.Count; i++)
                {
                    if (caps.Modes[i].Text == "-")
                    {
                        Modes.Add(new CamServerCore.SeparatorMode(), false);
                    }
                    else
                    {
                        Modes.Add(caps.Modes[i], caps.ModeValues[i]);
                    }
                }
                Capabilities = caps.Caps;
                break;

            case "Unity.Packets.ModeChange":
                var mode = data.GetObject <Packets.ModeChange>();
                Trace.WriteLine("got mode state change");
                if (ModeStateChanged != null)
                {
                    ModeStateChanged(mode.Mode, mode.value);
                }
                break;

            default:
                Trace.WriteLine("Got unhandled xml " + data.Type);
                break;
            }
        }
コード例 #2
0
        void sck_XmlDataArrived(TSocket sk, TSocket.DataPacket xml)
        {
            Trace.WriteLine("Got packet " + xml.Type + " from " + sk);
            switch (xml.Type)
            {
            case "Unity.Packets.PTZ":
                Trace.WriteLine("Got ptz request");
                var req2    = xml.GetObject <Unity.Packets.PTZ>();
                var reqdev2 = Broadcaster.Inst.Devices
                              .FirstOrDefault(d => d.UID + "@" + Dns.GetHostName() == req2.UID);
                if (reqdev2 != null)
                {
                    var ptdev = reqdev2 as CamServerCore.IDeviceWithPanTilt;
                    if (ptdev != null)
                    {
                        if (req2.preset > -1)
                        {
                            if (req2.setPreset)
                            {
                                ptdev.SetPreset(req2.preset);
                            }
                            else
                            {
                                ptdev.JumpToPreset(req2.preset);
                            }
                        }
                        else
                        {
                            ptdev.Move(req2.Movement, req2.once);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("invalid ptz dev");
                    }
                }
                break;

            case "Unity.Packets.ModeChange":
                var r    = xml.GetObject <Unity.Packets.ModeChange>();
                var rdev = Broadcaster.Inst.Devices
                           .FirstOrDefault(d => d.UID + "@" + Dns.GetHostName() == r.UID);

                if (rdev != null)
                {
                    var mdev = rdev as CamServerCore.IDeviceWithModes;
                    if (mdev != null)
                    {
                        var mode = Modes.FirstOrDefault(m => m.Text == r.Mode.Text);
                        if (mode != null)
                        {
                            mdev.ToggleMode(mode);
                        }
                        else
                        {
                            Trace.WriteLine("Invalid mode");
                        }
                    }
                    else
                    {
                        Trace.WriteLine("invalid mode dev (no interface)");
                    }
                }
                else
                {
                    Trace.WriteLine("invalid mode dev");
                }

                break;

            case "Unity.Packets.Request":
                var req = xml.GetObject <Unity.Packets.Request>();
                CamServerCore.IVideoDevice vdev = null;
                var reqdev = Broadcaster.Inst.Devices
                             .FirstOrDefault(d => d.UID + "@" + Dns.GetHostName() == req.UID);
                if (reqdev != null)
                {
                    switch (req.Type)
                    {
                    case Packets.Request.RequestType.StartVideo:
                        Trace.WriteLine("Requesting start video");
                        vdev = reqdev as CamServerCore.IVideoDevice;
                        if (vdev != null)
                        {
                            DeviceInfo.Add(sk, new DevInfo());

                            vdev.OnFrame += img =>
                            {
                                DeviceInfo[sk].FrameCount++;
                                DeviceInfo[sk].LastFrame = img;
                                img.Freeze();

                                if (DeviceInfo[sk].FrameCount == 1)
                                {
                                    byte[] image = GetJpeg(img, 100);
                                    sk.Send("jpeg", image);
                                    Trace.WriteLine("Sent initial frame");
                                }
                            };
                            vdev.StartVideo();
                            Trace.WriteLine("Video started");
                        }
                        else
                        {
                            var err = new Unity.Packets.Error
                            {
                                Message = "Invalid video device: " + req.UID
                            };
                            sk.Send(err);
                        }

                        break;

                    case Packets.Request.RequestType.Frame:

                        if (DeviceInfo[sk].LastFrameTaken < DeviceInfo[sk].FrameCount)
                        {
                            DeviceInfo[sk].LastFrameTaken = DeviceInfo[sk].FrameCount;
                            byte[] image = GetJpeg(DeviceInfo[sk].LastFrame, 75);
                            sk.Send("jpeg", image);
                            Trace.WriteLine("Sent frame #" + DeviceInfo[sk].FrameCount);
                        }
                        else
                        {
                            var fnr = new Packets.FrameNotReady();
                            sk.Send(fnr);
                            Trace.WriteLine("Sent noop");
                        }
                        break;

                    case Packets.Request.RequestType.StopVideo:
                        Trace.WriteLine("Requesting stop video");
                        vdev = reqdev as CamServerCore.IVideoDevice;
                        if (vdev != null)
                        {
                            vdev.StopVideo();
                            Trace.WriteLine("Video stopped");
                        }
                        else
                        {
                            var err = new Unity.Packets.Error {
                                Message = "Invalid video device: " + req.UID
                            };
                            sk.Send(err);
                        }
                        break;

                    case Packets.Request.RequestType.Capabilities:
                        Trace.WriteLine("Caps request");
                        var cap = new Packets.Capabilities();

                        var ptzdev = reqdev as CamServerCore.IDeviceWithPanTilt;
                        if (ptzdev != null)
                        {
                            cap.Caps = "ptz";
                        }

                        var mxdev = reqdev as CamServerCore.IDeviceWithModes;
                        if (mxdev != null)
                        {
                            Trace.WriteLine("Adding modes...");
                            foreach (var kv in mxdev.GetModes())
                            {
                                cap.Modes.Add(new Packets.UMode(kv.Key));
                                Modes.Add(kv.Key);
                                cap.ModeValues.Add(kv.Value);
                            }

                            mxdev.ModeStateChanged += (mode, value) =>
                            {
                                Trace.WriteLine("mode changed, sending notify");
                                var mc = new Packets.ModeChange();
                                mc.Mode  = new Packets.UMode(mode);
                                mc.value = value;
                                sk.Send(mc);
                            };
                        }

                        Trace.WriteLine("Caps response: " + cap.Caps);
                        sk.Send(cap);
                        break;

                    default:
                        Trace.WriteLine("Invalid unity request: " + req.Type);
                        break;
                    }
                }
                else
                {
                    var err = new Unity.Packets.Error {
                        Message = "Invalid device specified: " + req.UID
                    };
                    sk.Send(err);
                }
                break;

            default:
                Trace.WriteLine("Invalid UnityServer packet: " + xml.Type);
                break;
            }
        }