Ejemplo n.º 1
0
        public static EnumResponse FromResponse(WebcamResponse response)
        {
            var curr = new EnumResponse();

            var packet = response.packet;
            var offset = 0;

            curr.index = BitConverter.ToInt32(packet, offset);
            offset    += 4;

            var nLen = BitConverter.ToInt32(packet, offset);

            offset += 4;

            curr.name = Encoding.Unicode.GetString(packet, offset, nLen);
            offset   += nLen;

            var nDevice = BitConverter.ToInt32(packet, offset);

            offset += 4;

            curr.device = Encoding.Unicode.GetString(packet, offset, nDevice);
            offset     += nDevice;

            return(curr);
        }
Ejemplo n.º 2
0
        public static HresultResponse FromResponse(WebcamResponse response)
        {
            var start = new HresultResponse();

            start.type = response.type;
            start.hr   = BitConverter.ToInt32(response.packet, 0);

            return(start);
        }
Ejemplo n.º 3
0
        public static FormatResponse FromResponse(WebcamResponse response)
        {
            var format = new FormatResponse();

            format.Index       = BitConverter.ToInt32(response.packet, 0);
            format.Width       = BitConverter.ToInt32(response.packet, 8);
            format.Height      = BitConverter.ToInt32(response.packet, 12);
            format.Compression = BitConverter.ToUInt32(response.packet, 20);

            return(format);
        }
Ejemplo n.º 4
0
        void ProcessFrame(WebcamResponse frame)
        {
            if (_format == null)
            {
                return;
            }

            var timeStamp = new TimeSpan(
                BitConverter.ToInt64(frame.packet, 0)
                );

            var bmp  = new Bitmap(_format.Width, _format.Height, PixelFormat.Format24bppRgb);
            var data = bmp.LockBits(
                new Rectangle(0, 0, bmp.Width, bmp.Height),
                ImageLockMode.ReadWrite,
                PixelFormat.Format24bppRgb
                );

            var dataSize = data.Stride * data.Height;

            if (frame.packetSize - 8 == dataSize)
            {
                Marshal.Copy(frame.packet, 8, data.Scan0, dataSize);
            }

            bmp.UnlockBits(data);

            if (_framesOnDemand)
            {
                OnDemandCaptureFrame(timeStamp, bmp);
            }
            else
            {
                OnCaptureFrame(timeStamp, bmp);
            }
        }
Ejemplo n.º 5
0
        IEnumerator <ITask> ProcessPipe(NamedPipeClientStream client)
        {
            Port <IAsyncResult> continuation = new Port <IAsyncResult>();

            for (; ;)
            {
                IAsyncResult ar       = null;
                var          response = new WebcamResponse();

                client.BeginRead(response.header,
                                 0,
                                 WebcamResponse.HeaderSize,
                                 continuation.Post,
                                 null
                                 );

                yield return(Arbiter.Receive(false, continuation, result => ar = result));

                var length = client.EndRead(ar);
                if (length != 8)
                {
                    if (0 < Interlocked.CompareExchange(ref _shutdown, 1, 1))
                    {
                        LogInfo("Pipe has been closed");
                        client = null;
                    }
                    else
                    {
                        LogError("Bad header length read from pipe");
                    }
                    yield break;
                }

                response.packet = new byte[response.packetSize];

                client.BeginRead(response.packet,
                                 0,
                                 response.packetSize,
                                 continuation.Post,
                                 null
                                 );

                yield return(Arbiter.Receive(false, continuation, result => ar = result));

                length = client.EndRead(ar);
                if (length != response.packetSize)
                {
                    if (0 < Interlocked.CompareExchange(ref _shutdown, 1, 1))
                    {
                        LogInfo("Pipe has been closed");
                        client = null;
                    }
                    else
                    {
                        LogError("Bad packet length read from pipe");
                    }
                    yield break;
                }

                if (response.IsKnownType)
                {
                    var item = response.KnownType;

                    if (item is EnumResponse)
                    {
                        _pipeDataPort.Post((EnumResponse)item);
                    }
                    else if (item is FormatResponse)
                    {
                        var format = (FormatResponse)item;

                        if (format.Index == -1)
                        {
                            _format = format;
                            if (_state.Selected != null)
                            {
                                if (_state.Selected.Format == null)
                                {
                                    _state.Selected.Format = new Format();
                                }
                                _state.Selected.Format.Width  = _format.Width;
                                _state.Selected.Format.Height = _format.Height;
                            }
                        }
                        else
                        {
                            _pipeDataPort.Post(format);
                        }
                    }
                    else if (item is HresultResponse)
                    {
                        _pipeDataPort.Post((HresultResponse)item);
                    }
                }
                else if (response.IsFrame)
                {
                    ProcessFrame(response);
                }
            }
        }