Example #1
0
        public void TestDequeue()
        {
            RingBuffer <float> r1 = new RingBuffer <float>(3);

            Assert.IsTrue(r1.Enqueue(1.5f));
            Assert.IsTrue(r1.Enqueue(2.5f));
            Assert.IsTrue(r1.Enqueue(3.5f));

            Assert.IsTrue(r1.IsFull);
            Assert.IsFalse(r1.IsEmpty);

            float[] v = new float[3];
            Assert.AreEqual(3, r1.Dequeue(ref v));

            Assert.AreEqual(0, r1.Count);
            Assert.IsFalse(r1.IsFull);
            Assert.IsTrue(r1.IsEmpty);

            Assert.AreEqual(1.5f, v[0]);
            Assert.AreEqual(2.5f, v[1]);
            Assert.AreEqual(3.5f, v[2]);

            Assert.IsTrue(r1.Enqueue(4.5f));
            Assert.IsTrue(r1.Enqueue(5.5f));
            Assert.IsTrue(r1.Enqueue(6.5f));

            Assert.IsTrue(r1.IsFull);
            Assert.IsFalse(r1.IsEmpty);

            float[] v1 = new float[1];
            Assert.AreEqual(1, r1.Dequeue(ref v1));

            Assert.AreEqual(2, r1.Count);
            Assert.AreEqual(4.5f, v1[0]);

            Assert.AreEqual(1, r1.Dequeue(ref v1));
            Assert.AreEqual(1, r1.Count);
            Assert.AreEqual(5.5f, v1[0]);

            Assert.AreEqual(1, r1.Dequeue(ref v1));
            Assert.AreEqual(0, r1.Count);
            Assert.AreEqual(6.5f, v1[0]);

            Assert.IsFalse(r1.IsFull);
            Assert.IsTrue(r1.IsEmpty);

            Assert.AreEqual(0, r1.Dequeue(ref v1));
            Assert.AreEqual(0, r1.Count);
            Assert.AreEqual(6.5f, v1[0]);

            Assert.IsTrue(r1.Enqueue(7.5f, true));
            Assert.IsTrue(r1.Enqueue(8.5f, true));
            Assert.IsTrue(r1.Enqueue(9.5f, true));
            Assert.IsTrue(r1.Enqueue(10.5f, true));

            Assert.AreEqual(3, r1.Dequeue(ref v));
            Assert.AreEqual(8.5f, v[0]);
            Assert.AreEqual(9.5f, v[1]);
            Assert.AreEqual(10.5f, v[2]);
        }
Example #2
0
        public void TestCopyFrom()
        {
            RingBuffer <float> r1 = new RingBuffer <float>(3);
            RingBuffer <float> r2 = new RingBuffer <float>(10);
            RingBuffer <float> r3 = new RingBuffer <float>(1);

            Assert.IsTrue(r1.Enqueue(1.5f));
            Assert.IsTrue(r1.Enqueue(2.5f));
            Assert.IsTrue(r1.Enqueue(3.5f));

            r2.CopyFrom(r1);
            r3.CopyFrom(r2);

            Assert.AreEqual(r1.IsFull, r2.IsFull);
            Assert.AreEqual(r1.IsEmpty, r2.IsEmpty);
            Assert.AreEqual(r1[0], r2[0]);
            Assert.AreEqual(r1[1], r2[1]);
            Assert.AreEqual(r1[2], r2[2]);

            Assert.AreEqual(r3.IsFull, r2.IsFull);
            Assert.AreEqual(r3.IsEmpty, r2.IsEmpty);
            Assert.AreEqual(r3[0], r2[0]);
            Assert.AreEqual(r3[1], r2[1]);
            Assert.AreEqual(r3[2], r2[2]);

            r2.Clear();

            Assert.AreEqual(r3.IsFull, r1.IsFull);
            Assert.AreEqual(r3.IsEmpty, r1.IsEmpty);
            Assert.AreEqual(r3[0], r1[0]);
            Assert.AreEqual(r3[1], r1[1]);
            Assert.AreEqual(r3[2], r1[2]);
        }
Example #3
0
        public void TestEnqueue()
        {
            RingBuffer <float> rbf = new RingBuffer <float>(3);

            Assert.AreEqual(0, rbf.Count);

            Assert.IsTrue(rbf.IsEmpty);
            Assert.IsFalse(rbf.IsFull);
            Assert.IsTrue(rbf.Enqueue(1.5f));
            Assert.AreEqual(1, rbf.Count);

            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsFalse(rbf.IsFull);
            Assert.IsTrue(rbf.Enqueue(2.5f));
            Assert.AreEqual(2, rbf.Count);

            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsFalse(rbf.IsFull);
            Assert.IsTrue(rbf.Enqueue(3.5f));
            Assert.AreEqual(3, rbf.Count);

            // Test Add() returns false if full.
            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsTrue(rbf.IsFull);
            Assert.IsFalse(rbf.Enqueue(4.5f));

            Assert.AreEqual(1.5, rbf[0]);
            Assert.AreEqual(2.5, rbf[1]);
            Assert.AreEqual(3.5, rbf[2]);

            // Test Add() overwirte oldest value if full.
            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsTrue(rbf.IsFull);
            Assert.IsTrue(rbf.Enqueue(4.5f, true));
            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsTrue(rbf.IsFull);

            Assert.AreEqual(2.5, rbf[0]);
            Assert.AreEqual(3.5, rbf[1]);
            Assert.AreEqual(4.5, rbf[2]);

            Assert.IsTrue(rbf.Enqueue(5.5f, true));
            Assert.AreEqual(3.5, rbf[0]);
            Assert.AreEqual(4.5, rbf[1]);
            Assert.AreEqual(5.5, rbf[2]);

            Assert.IsTrue(rbf.Enqueue(6.5f, true));
            Assert.AreEqual(4.5, rbf[0]);
            Assert.AreEqual(5.5, rbf[1]);
            Assert.AreEqual(6.5, rbf[2]);

            Assert.IsTrue(rbf.Enqueue(7.5f, true));
            Assert.AreEqual(5.5, rbf[0]);
            Assert.AreEqual(6.5, rbf[1]);
            Assert.AreEqual(7.5, rbf[2]);
        }
Example #4
0
        public void TestClear()
        {
            RingBuffer <float> rbf = new RingBuffer <float>(3);

            Assert.IsTrue(rbf.IsEmpty);
            Assert.IsFalse(rbf.IsFull);

            Assert.IsTrue(rbf.Enqueue(1.5f));
            Assert.IsTrue(rbf.Enqueue(2.5f));
            Assert.IsTrue(rbf.Enqueue(3.5f));

            Assert.IsFalse(rbf.IsEmpty);
            Assert.IsTrue(rbf.IsFull);
            Assert.AreEqual(3, rbf.Count);

            rbf.Clear();

            Assert.IsTrue(rbf.IsEmpty);
            Assert.IsFalse(rbf.IsFull);
            Assert.AreEqual(0, rbf.Count);
        }
Example #5
0
            public void Add(float left, float right)
            {
                if (m_leftChannel.IsFull)
                {
                    m_leftChannel.Dequeue(m_scratch.Length);
                    m_rightChannel.Dequeue(m_scratch.Length);
                    m_frameSamples += m_scratch.Length;
                }

                m_leftChannel.Enqueue(left);
                m_rightChannel.Enqueue(right);
            }
Example #6
0
        public void TestGetRange()
        {
            RingBuffer <float> r1 = new RingBuffer <float>(3);

            Assert.IsTrue(r1.Enqueue(4.5f));
            Assert.IsTrue(r1.Enqueue(1.5f));
            Assert.IsTrue(r1.Enqueue(2.5f));
            Assert.IsTrue(r1.Enqueue(3.5f, true));

            float[] v1 = new float[1];
            r1.GetRange(ref v1);
            Assert.AreEqual(1.5f, v1[0]);

            float[] v2 = new float[2];
            r1.GetRange(ref v2);
            Assert.AreEqual(1.5f, v2[0]);
            Assert.AreEqual(2.5f, v2[1]);

            float[] v3 = new float[3];
            r1.GetRange(ref v3);
            Assert.AreEqual(1.5f, v3[0]);
            Assert.AreEqual(2.5f, v3[1]);
            Assert.AreEqual(3.5f, v3[2]);

            r1.GetRange(ref v2, 1, 2);
            Assert.AreEqual(2.5f, v2[0]);
            Assert.AreEqual(3.5f, v2[1]);

            r1.GetRange(ref v1, 1, 1);
            Assert.AreEqual(2.5f, v1[0]);

            r1.GetRange(ref v1, 2, 1);
            Assert.AreEqual(3.5f, v1[0]);

            r1.GetRange(ref v1, 0, 1);
            Assert.AreEqual(1.5f, v1[0]);
        }
Example #7
0
        public void TestEnumerators()
        {
            RingBuffer <float> r1 = new RingBuffer <float>(3);

            Assert.IsTrue(r1.Enqueue(4.5f));
            Assert.IsTrue(r1.Enqueue(1.5f));
            Assert.IsTrue(r1.Enqueue(2.5f));
            Assert.IsTrue(r1.Enqueue(3.5f, true));

            using (var e1 = r1.GetEnumerator()) {
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }

            using (var e1 = r1.GetEnumerator(0, 3)) {
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }

            using (var e1 = r1.GetEnumerator(1, 2)) {
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsTrue(e1.MoveNext());
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }

            using (var e1 = r1.GetEnumerator(2, 1)) {
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(3.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }

            using (var e1 = r1.GetEnumerator(1, 1)) {
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(2.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }

            using (var e1 = r1.GetEnumerator(0, 1)) {
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
                e1.Reset();
                Assert.AreEqual(1.5, e1.Current);
                Assert.IsFalse(e1.MoveNext());
            }
        }
Example #8
0
        private void ReadFramesFromFfmpeg()
        {
            long PIXEL_SIZE_BYTES = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Color32));

            // TODO: Use ffprobe instead of width/height, then w/h properties could be removed.
            byte[] buf = new byte[Height * Width * PIXEL_SIZE_BYTES];
            System.IO.BinaryReader dataPipe = m_dataReader;
            // Keep a local set of buffers to avoid garbage collection.
            Bytable[] localRefs = new Bytable[m_framesOut.Capacity];

            // Init local refs.
            int lastLocalRef = 0;

            for (int i = 0; i < localRefs.Length; i++)
            {
                localRefs[i] = new Color32Bytable(null);
            }

            try
            {
                using (dataPipe)
                {
                    while (!m_shouldExit)
                    {
                        while (!m_shouldPause)
                        {
                            if (m_framesOut.IsFull)
                            {
                                // Wait for the consumer.
                                m_frameReady.WaitOne();
                            }

                            int bytesRead = dataPipe.Read(buf, 0, buf.Length);
                            while (bytesRead < buf.Length)
                            {
                                bytesRead += dataPipe.Read(buf, bytesRead, buf.Length - bytesRead);
                                if (bytesRead == 0)
                                {
                                    return;
                                }
                            }
                            if (bytesRead != buf.Length)
                            {
                                // For some reason we only read the wrong amount of data.
                                UnityEngine.Debug.LogWarningFormat("BAD READ RESULT: got {0} bytes, expected {1}",
                                                                   bytesRead, buf.Length);
                                continue;
                            }

                            // If the last buffer we had was the same size, no allocation will happen here. We
                            // will also be holding a reference to that array, so even after it's removed from the
                            // m_framesOut buffer, it should not generate garbage.
                            Bytable curFrame = localRefs[lastLocalRef];
                            lastLocalRef = (lastLocalRef + 1) % localRefs.Length;

                            curFrame.FromBytes(buf);

                            // If called with overwriteIfFull=true, this code will require a lock.
                            m_framesOut.Enqueue(curFrame);
                            m_frameCount++;
                        }

                        // Wait for the next frame
                        m_ready.WaitOne();
                    }
                }
            }
            catch (System.Threading.ThreadInterruptedException)
            {
                // This is fine, the render thread sent an interrupt.
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
            finally
            {
                Stop();
            }
        }