Exemplo n.º 1
0
        private void CameraReceiverMethod()
        {
            byte[] framedata = new byte[1];

            var responseCameraFrame = new ResponseCameraFrame();


            while (!RequestedStop)
            {
                try
                {
                    var responseBytes = new byte[responseCameraFrame.GetResponseLength()];

                    // Receive the first part of the frame. This part contains the informations like height, width or length
                    Receive(_socket, responseBytes, responseBytes.Length);

                    try
                    {
                        responseCameraFrame.FromByteArray(responseBytes);
                    }
                    catch (InvalidOperationException) // Error while receiving one frame. Close camera server
                    {
                        RequestedStop = true;
                        TxtCommunication.SendCommand(new CommandStopCamera(), new ResponseStopCamera());

                        DisconnectFromCameraServerMethod();
                        break;
                    }


                    // Use the existing framedata object and resize if needed
                    if (framedata.Length < responseCameraFrame.FrameSizeCompressed + 2)
                    {
                        Array.Resize(ref framedata, responseCameraFrame.FrameSizeCompressed + 2);
                    }


                    // Receive the second part of the frame. This part contains the compressed JPEG data
                    Receive(_socket, framedata, responseCameraFrame.FrameSizeCompressed);

                    // Add the missing EOI (End of image) tag
                    framedata[framedata.Length - 2] = 0xFF;
                    framedata[framedata.Length - 1] = 0xD9;

                    // Store the received frame in the responseCameraFrame object
                    responseCameraFrame.FrameData = framedata;

                    // Process the received frame in another thread queue so that we can continue receiving frames
                    ReceivedFrames.Enqueue(responseCameraFrame);
                    _frameProcessingTaskQueue.DoWorkInQueue(() =>
                    {
                        if (!ReceivedFrames.IsEmpty)
                        {
                            ResponseCameraFrame frame;
                            if (ReceivedFrames.TryDequeue(out frame) && !RequestedStop)
                            {
                                FrameReceivedEventArgs eventArgs = new FrameReceivedEventArgs(framedata, responseCameraFrame.FrameSizeCompressed + 2);
                                FrameReceived?.Invoke(this, eventArgs);
                            }
                        }
                    }, false);


                    // Send an acknowledge
                    _socket.Send(BitConverter.GetBytes(TxtInterface.AcknowledgeIdCameraOnlineFrame));
                }
                catch (Exception)
                {
                }
            }
        }