private void Process()
        {
            _processing = true;
            try {
                while (_processing)
                {
                    lock (_frameBufferLock) {
                        UInt32 remove = 0;
                        foreach (KeyValuePair <UInt32, SequencedFrame> sequencedFrame in _frameBuffer)
                        {
                            if (sequencedFrame.Key < _lastSequenceRendered)
                            {
                                remove = sequencedFrame.Key;
                                //Debug.Log("A newer frame has already been rendered: "+remove);
                                break;
                            }

                            if (sequencedFrame.Value.IsComplete())
                            {
                                sequencedFrame.Value.cameraPos = FrameSource.cameraPosition;
                                sequencedFrame.Value.cameraRot = FrameSource.cameraRotation;
                                _lastSequenceRendered          = sequencedFrame.Key;
                                remove = sequencedFrame.Key;
                                break;
                            }
                        }

                        if (remove > 0)
                        {
                            SequencedFrame removeFrame = _frameBuffer[remove];
                            _frameBuffer.Remove(remove);
                            if (remove == _lastSequenceRendered)
                            {
                                FrameSource.frameQueue.Enqueue(removeFrame);
                            }
                            else
                            {
                                lock (_unusedQueueLock) {
                                    _unusedQueue.Enqueue(removeFrame);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Debug.Log(e);
            } finally {
                _processing = false;
            }

            Debug.Log("Process Thread Closed");
        }
        public override void HandleDepthData(ushort sr, ushort er, UInt32 seq, ref byte[] data, int dataOffset)
        {
            if (seq < _lastSequenceRendered)
            {
                return;
            }

            lock (_frameBufferLock)
                lock (_unusedQueueLock) {
                    if (_frameBuffer.ContainsKey(seq))
                    {
                        _frameBuffer[seq].LoadDepthData(sr, er, ref data, dataOffset);
                        _frameBuffer[seq].MarkAsLoaded(sr, er);
                        //Debug.Log("Using old frame: "+seq);
                    }
                    else if (_unusedQueue.Count > 0)
                    {
                        _frameBuffer[seq] = _unusedQueue.Dequeue();
                        _frameBuffer[seq].Reset();
                        _frameBuffer[seq].LoadDepthData(sr, er, ref data, dataOffset);
                        _frameBuffer[seq].MarkAsLoaded(sr, er);
                        //Debug.Log("Dequeued for: "+seq);
                    }
                    else if (_frameBuffer.Count > 0)
                    {
                        UInt32         oldest = _frameBuffer.Keys.Min();
                        SequencedFrame old    = _frameBuffer[oldest];
                        _frameBuffer.Remove(oldest);
                        Debug.LogWarning("Dropping frame with seq: " + oldest + ", missing: " +
                                         old.CountMissing() + " of " + TotalHeight);
                        old.Reset();
                        _frameBuffer[seq] = old;
                        _frameBuffer[seq].LoadDepthData(sr, er, ref data, dataOffset);
                        _frameBuffer[seq].MarkAsLoaded(sr, er);
                    }
                    else
                    {
                        Debug.LogWarning("Not enough (unused) framebuffers.");
                    }
                }
        }
 public void ReturnFromRender(SequencedFrame s)
 {
     lock (_unusedQueueLock) {
         _unusedQueue.Enqueue(s);
     }
 }