// License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2017 Intel Corporation. All Rights Reserved. namespace Intel.RealSense { using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; public class FrameQueue : Base.Object { /// /// Initializes a new instance of the class. /// /// Frame queues are the simplest x-platform synchronization primitive provided by librealsense /// to help developers who are not using async APIs /// /// /// max number of frames to allow to be stored in the queue before older frames will start to get dropped public FrameQueue(int capacity = 1) : base(Create(capacity), NativeMethods.rs2_delete_frame_queue) { Capacity = capacity; } /// /// Gets the max number of frames to allow to be stored in the queue before older frames will start to get dropped /// public int Capacity { get; private set; } /// /// Poll if a new frame is available and dequeue if it is /// /// type or subclass /// dequeued frame /// true if new frame was stored to public bool PollForFrame(out T frame) where T : Frame { object error; IntPtr ptr; if (NativeMethods.rs2_poll_for_frame(Handle, out ptr, out error) > 0) { frame = Frame.Create(ptr); return true; } frame = null; return false; } /// /// Wait until new frame becomes available in the queue and dequeue it /// /// max time in milliseconds to wait until an exception will be thrown /// dequeued frame public Frame WaitForFrame(uint timeout_ms = 5000u) { return WaitForFrame(timeout_ms); } /// /// Wait until new frame becomes available in the queue and dequeue it /// /// type or subclass /// max time in milliseconds to wait until an exception will be thrown /// dequeued frame public T WaitForFrame(uint timeout_ms = 5000u) where T : Frame { object error; var ptr = NativeMethods.rs2_wait_for_frame(Handle, timeout_ms, out error); return Frame.Create(ptr); } /// /// Wait until new frame becomes available in the queue and dequeue it /// /// type or subclass /// dequeued frame /// max time in milliseconds to wait until a frame becomes available /// true if new frame was stored to public bool TryWaitForFrame(out T frame, uint timeout_ms = 5000u) where T : Frame { object error; IntPtr ptr; bool res = NativeMethods.rs2_try_wait_for_frame(Handle, timeout_ms, out ptr, out error) > 0; frame = res ? Frame.Create(ptr) : null; return res; } /// /// Wait until new frame becomes available in the queue and dequeue it /// /// max time in milliseconds to wait until a frame becomes available /// dequeued frame public FrameSet WaitForFrames(uint timeout_ms = 5000u) { object error; var ptr = NativeMethods.rs2_wait_for_frame(Handle, timeout_ms, out error); return FrameSet.Create(ptr); } /// /// Enqueue new frame into a queue /// /// frame to enqueue public void Enqueue(Frame f) { object error; NativeMethods.rs2_frame_add_ref(f.Handle, out error); NativeMethods.rs2_enqueue_frame(f.Handle, Handle); } /// /// Number of frames hold by the queue /// public int QueueSize() { object error; return NativeMethods.rs2_frame_queue_size(this.Handle, out error); } internal static IntPtr Create(int capacity = 1) { object error; return NativeMethods.rs2_create_frame_queue(capacity, out error); } } }