public void OnAccept(IAsyncResult ar)
        {
            try
            {
                AsyncNetworkConnector connector = (AsyncNetworkConnector)ar.AsyncState;
                Socket socket = connector.listenerSocket.EndAccept(ar);
                var    client = new Client(socket, this.concurrentSends);

                socket.BeginReceive(client.buffer, 0, Client.BufferSize, 0,
                                    new AsyncCallback(OnReceive), client);
                connector.connectedClients.Add(client);
                connector.listenerSocket.BeginAccept(new AsyncCallback(OnAccept), connector);

                // TODO: put this in the event log instead.
                Console.WriteLine("Connected to: " + client.endpoint);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Error accepting client: " + e);
            }
            catch (System.ObjectDisposedException)
            {
                // Do nothing, this is probably just a shutdown race condition on socket cleanup.
            }
        }
 public void OnAccept(IAsyncResult ar)
 {
     try
     {
         AsyncNetworkConnector connector = (AsyncNetworkConnector)ar.AsyncState;
         Socket socket = connector.listenerSocket.EndAccept(ar);
         connector.connectedClients.Add(new Client(socket, 3));
         Console.WriteLine("Added socket to client list"); // TODO: put this in the event log instead.
         connector.listenerSocket.BeginAccept(new AsyncCallback(OnAccept), connector);
     }
     catch (Exception e)
     {
         Console.WriteLine("Error in startListeningCallBack: " + e.ToString());
     }
 }
        protected override void OnStart(string[] args)
        {
            // Try to open the first available Kinect sensor.
            this.kinect = KinectSensor.GetDefault();
            if (this.kinect == null)
            {
                EventLog.WriteEntry("No Kinect device was detected.");
                ExitCode = -1;
                throw new KinectException("No kinect device was detected.");
            }
            else
            {
                this.kinect.Open();
                this.kinect.IsAvailableChanged += this.OnAvailableChanged;
            }

            // Register as a handler for the image data being returned by the Kinect.
            this.reader      = this.kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
            this.audioSource = this.kinect.AudioSource;
            if (this.reader == null)
            {
                EventLog.WriteEntry("Unable to connect to Kinect data stream.");
                ExitCode = -2;
                throw new KinectException("Unable to connect to Kinect data stream.");
            }
            else
            {
                this.reader.MultiSourceFrameArrived += this.OnFrameArrived;
            }
            if (this.audioSource == null)
            {
                EventLog.WriteEntry("Unable to open audio source on kinect");
                ExitCode = -3;
                throw new KinectException("Unable to connect to kinect audio source");
            }
            else
            {
                this.audioReader = this.audioSource.OpenReader();
                if (this.audioReader == null)
                {
                    Console.WriteLine("Issues with audio reader");
                }
                else
                {
                    this.audioReader.FrameArrived += this.onAudioFrameArrived;
                }
            }


            // Allocate storage for the data from the Kinect.
            this.colorArray      = new byte[(this.kinect.ColorFrameSource.FrameDescription.Height * this.kinect.ColorFrameSource.FrameDescription.Width * BYTES_PER_COLOR_PIXEL)];
            this.depthArray      = new ushort[this.kinect.DepthFrameSource.FrameDescription.Height * this.kinect.DepthFrameSource.FrameDescription.Width];
            this.irArray         = new ushort[this.kinect.InfraredFrameSource.FrameDescription.Height * this.kinect.InfraredFrameSource.FrameDescription.Width];
            this.byteColorArray  = new byte[(this.kinect.ColorFrameSource.FrameDescription.Height * this.kinect.ColorFrameSource.FrameDescription.Width * BYTES_PER_COLOR_PIXEL) + sizeof(double)];
            this.byteDepthArray  = new byte[this.kinect.DepthFrameSource.FrameDescription.Height * this.kinect.DepthFrameSource.FrameDescription.Width * BYTES_PER_DEPTH_PIXEL + sizeof(double)];
            this.byteIRArray     = new byte[this.kinect.InfraredFrameSource.FrameDescription.Height * this.kinect.InfraredFrameSource.FrameDescription.Width * BYTES_PER_IR_PIXEL + sizeof(double)];
            this.bodyArray       = new Body[this.kinect.BodyFrameSource.BodyCount];
            this.scan2DArray     = new float[this.kinect.DepthFrameSource.FrameDescription.Width * 6 + 3];
            this.byteScan2DArray = new byte[this.kinect.DepthFrameSource.FrameDescription.Width * 6 * 4 + 12];
            this.audioContainer  = new AudioContainer();
            this.audioContainer.samplingFrequency  = 16000;
            this.audioContainer.frameLifeTime      = 0.016;
            this.audioContainer.numSamplesPerFrame = (int)(this.audioContainer.samplingFrequency * this.audioContainer.frameLifeTime);
            this.audioContainer.numBytesPerSample  = sizeof(float);
            this.audioContainer.audioStream        = new float[256];

            // Create network connectors that will send out the data when it is received.
            this.colorConnector  = new AsyncNetworkConnector(Properties.Settings.Default.RgbImagePort);
            this.depthConnector  = new AsyncNetworkConnector(Properties.Settings.Default.DepthImagePort);
            this.irConnector     = new AsyncNetworkConnector(Properties.Settings.Default.IrImagePort);
            this.bodyConnector   = new AsyncNetworkConnector(Properties.Settings.Default.BodyPort);
            this.audioConnector  = new AsyncNetworkConnector(Properties.Settings.Default.AudioPort);
            this.scan2DConnector = new AsyncNetworkConnector(Properties.Settings.Default.Scan2DPort);

            // Open the server connections.
            this.colorConnector.Listen();
            this.depthConnector.Listen();
            this.irConnector.Listen();
            this.bodyConnector.Listen();
            this.audioConnector.Listen();
            this.scan2DConnector.Listen();
            // get the depth (display)extents

            FrameDescription colorFrameDescription = this.kinect.ColorFrameSource.FrameDescription;

            // get size of joint space
            this.displayWidth  = colorFrameDescription.Width;
            this.displayHeight = colorFrameDescription.Height;

            // open the reader for the body frames


            // open the reader for the color frames

            // create the bitmap to display
            this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);

            mapper = this.kinect.CoordinateMapper;
            lower  = new CvMat(3, 1, MatrixType.U8C1);
            upper  = new CvMat(3, 1, MatrixType.U8C1);
            // Set specific color threshold for orange.
            lower[0] = 5;
            lower[1] = 170;
            lower[2] = 170;

            upper[0] = 10;
            upper[1] = 255;
            upper[2] = 255;
        }
Esempio n. 4
0
        protected override void OnStart(string[] args)
        {
            // Try to open the first available Kinect sensor.
            this.kinect = KinectSensor.GetDefault();
            if (this.kinect == null)
            {
                EventLog.WriteEntry("No Kinect device was detected.");
                ExitCode = -1;
                throw new KinectException("No kinect device was detected.");
            }
            else
            {
                this.filter = new KinectJointFilter();
                this.filter.Init(0.5f, 0.5f, 0.5f, 0.05f, 0.04f); // change params if you want
                this.kinect.Open();
                this.kinect.IsAvailableChanged += this.OnAvailableChanged;
            }

            // Register as a handler for the image data being returned by the Kinect.
            this.reader      = this.kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
            this.audioSource = this.kinect.AudioSource;
            if (this.reader == null)
            {
                EventLog.WriteEntry("Unable to connect to Kinect data stream.");
                ExitCode = -2;
                throw new KinectException("Unable to connect to Kinect data stream.");
            }
            else
            {
                this.reader.MultiSourceFrameArrived += this.OnFrameArrived;
            }
            if (this.audioSource == null)
            {
                EventLog.WriteEntry("Unable to open audio source on kinect");
                ExitCode = -3;
                throw new KinectException("Unable to connect to kinect audio source");
            }
            else
            {
                this.audioReader = this.audioSource.OpenReader();
                if (this.audioReader == null)
                {
                    Console.WriteLine("Issues with audio reader");
                }
                else
                {
                    this.audioReader.FrameArrived += this.onAudioFrameArrived;
                }
            }


            // Allocate storage for the data from the Kinect.
            this.colorArray     = new byte[(this.kinect.ColorFrameSource.FrameDescription.Height * this.kinect.ColorFrameSource.FrameDescription.Width * BYTES_PER_COLOR_PIXEL)];
            this.depthArray     = new ushort[this.kinect.DepthFrameSource.FrameDescription.Height * this.kinect.DepthFrameSource.FrameDescription.Width];
            this.irArray        = new ushort[this.kinect.InfraredFrameSource.FrameDescription.Height * this.kinect.InfraredFrameSource.FrameDescription.Width];
            this.byteColorArray = new byte[(this.kinect.ColorFrameSource.FrameDescription.Height * this.kinect.ColorFrameSource.FrameDescription.Width * BYTES_PER_COLOR_PIXEL) + sizeof(double)];
            this.byteDepthArray = new byte[this.kinect.DepthFrameSource.FrameDescription.Height * this.kinect.DepthFrameSource.FrameDescription.Width * BYTES_PER_DEPTH_PIXEL + sizeof(double)];
            this.byteIRArray    = new byte[this.kinect.InfraredFrameSource.FrameDescription.Height * this.kinect.InfraredFrameSource.FrameDescription.Width * BYTES_PER_IR_PIXEL + sizeof(double)];
            this.bodyArray      = new Body[this.kinect.BodyFrameSource.BodyCount];
            this.audioContainer = new AudioContainer();
            this.audioContainer.samplingFrequency  = 16000;
            this.audioContainer.frameLifeTime      = 0.016;
            this.audioContainer.numSamplesPerFrame = (int)(this.audioContainer.samplingFrequency * this.audioContainer.frameLifeTime);
            this.audioContainer.numBytesPerSample  = sizeof(float);
            this.audioContainer.audioStream        = new float[256];

            // Create network connectors that will send out the data when it is received.
            this.colorConnector = new AsyncNetworkConnector(Properties.Settings.Default.RgbImagePort);
            this.depthConnector = new AsyncNetworkConnector(Properties.Settings.Default.DepthImagePort);
            this.irConnector    = new AsyncNetworkConnector(Properties.Settings.Default.IrImagePort);
            this.bodyConnector  = new AsyncNetworkConnector(Properties.Settings.Default.BodyPort);
            this.audioConnector = new AsyncNetworkConnector(Properties.Settings.Default.AudioPort);

            // Open the server connections.
            this.colorConnector.Listen();
            this.depthConnector.Listen();
            this.irConnector.Listen();
            this.bodyConnector.Listen();
            this.audioConnector.Listen();
        }
        protected override void OnStart(string[] args)
        {
            // Try to open the first available Kinect sensor.
            this.kinect = KinectSensor.GetDefault();
            if (this.kinect == null)
            {
                EventLog.WriteEntry("No Kinect device was detected.");
                ExitCode = -1;
                throw new KinectException("No kinect device was detected.");
            }
            else
            {
                this.kinect.Open();
                this.kinect.IsAvailableChanged += this.OnAvailableChanged;
            }

            // Register as a handler for the image data being returned by the Kinect.
            this.reader = this.kinect.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
            if (this.reader == null)
            {
                EventLog.WriteEntry("Unable to connect to Kinect data stream.");
                ExitCode = -2;
                throw new KinectException("Unable to connect to Kinect data stream.");
            }
            else
            {
                this.reader.MultiSourceFrameArrived += this.OnFrameArrived;
            }

            // Register as a handler for the audio source data being returned by the Kinect.
            this.audioSource = this.kinect.AudioSource;
            if (this.audioSource == null)
            {
                EventLog.WriteEntry("Unable to connect to Kinect audio source.");
                ExitCode = -3;
                throw new KinectException("Unable to connect to Kinect audio source.");
            }

            // Register as a handler for the audio reader data being returned by the Kinect.
            this.audioReader = this.audioSource.OpenReader();
            if (this.audioReader == null)
            {
                EventLog.WriteEntry("Unable to create reader for Kinect audio source.");
                ExitCode = -4;
                throw new KinectException("Unable to create reader for Kinect audio source.");
            }
            else
            {
                this.audioReader.FrameArrived += this.onAudioFrameArrived;
            }

            // Create an array of face sources/readers for each possible body.
            // These will be activated on demand as the corresponding bodies are tracked.
            this.faceSources = new HighDefinitionFaceFrameSource[this.kinect.BodyFrameSource.BodyCount];
            this.faceReaders = new HighDefinitionFaceFrameReader[this.kinect.BodyFrameSource.BodyCount];

            for (var i = 0; i < faceSources.Length; ++i)
            {
                // Register as a handler for the face source data being returned by the Kinect.
                this.faceSources[i] = new HighDefinitionFaceFrameSource(this.kinect);
                if (this.faceSources[i] == null)
                {
                    EventLog.WriteEntry("Unable to create Kinect face source [" + i + "].");
                    ExitCode = -5;
                    throw new KinectException("Unable to create Kinect face source [" + i + "].");
                }

                // Register as a handler for the face reader data being returned by the Kinect.
                this.faceReaders[i] = this.faceSources[i].OpenReader();
                if (this.faceReaders[i] == null)
                {
                    EventLog.WriteEntry("Unable to create reader for Kinect face source [" + i + "].");
                    ExitCode = -6;
                    throw new KinectException("Unable to create reader for Kinect face source [" + i + "].");
                }
                else
                {
                    this.faceReaders[i].FrameArrived += this.onFaceFrameArrived;
                }
            }


            // Create network connectors that will send out the data when it is received.
            this.colorConnector = new AsyncNetworkConnector(Properties.Settings.Default.RgbImagePort);
            this.depthConnector = new AsyncNetworkConnector(Properties.Settings.Default.DepthImagePort);
            this.irConnector    = new AsyncNetworkConnector(Properties.Settings.Default.IrImagePort);
            this.bodyConnector  = new AsyncNetworkConnector(Properties.Settings.Default.BodyPort);
            this.audioConnector = new AsyncNetworkConnector(Properties.Settings.Default.AudioPort);
            this.faceConnector  = new AsyncNetworkConnector(Properties.Settings.Default.FacePort);

            // Open the server connections.
            this.colorConnector.Listen();
            this.depthConnector.Listen();
            this.irConnector.Listen();
            this.bodyConnector.Listen();
            this.audioConnector.Listen();
            this.faceConnector.Listen();
        }