aboutsummaryrefslogtreecommitdiffstats
path: root/src/OrderedQueue.cpp
blob: 517728206fb82642a60eda18e8652c95cb3f0e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ------------------------------------------------------------------
 * Copyright (C) 2017 AVT GmbH - Fabien Vercasson
 * Copyright (C) 2019 Matthias P. Braendli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */

#include "OrderedQueue.h"
#include <stdexcept>
#include <cstdint>
#include <cstring>
#include <cstdio>

using namespace std;

#define DEBUG(fmt, A...)   fprintf(stderr, "OrderedQueue: " fmt, ##A)
//#define DEBUG(x...)
#define ERROR(fmt, A...)   fprintf(stderr, "OrderedQueue: ERROR " fmt, ##A)

OrderedQueue::OrderedQueue(int maxIndex, size_t capacity) :
    _maxIndex(maxIndex),
    _capacity(capacity)
{
}

void OrderedQueue::push(int32_t index, const uint8_t* buf, size_t size, const timestamp_t& ts)
{
    // DEBUG("OrderedQueue::push index=%d\n", index);
    index = (index + _maxIndex) % _maxIndex;

    // First frame makes the index initialisation.
    if (_lastIndexPop == -1) {
        // Equivalent to index - 1 in modulo arithmetic:
        _lastIndexPop = (index + _maxIndex-1) % _maxIndex;
    }

    if (_stock.size() < _capacity) {
        if (_stock.find(index) != _stock.end()) {
            // index already exists, duplicated frame
            // Replace the old one by the new one.
            // the old one could a an old frame from the previous index loop
            _duplicated++;
            DEBUG("Duplicated index=%d\n", index);
        }

        OrderedQueueData oqd;
        oqd.buf.resize(size);
        oqd.capture_timestamp = ts;

        copy(buf, buf + size, oqd.buf.begin());
        _stock[index] = move(oqd);
    }
    else {
        _overruns++;
        if (_overruns < 100) {
            DEBUG("Overruns (size=%zu) index=%d not inserted\n", _stock.size(), index);
        }
        else if (_overruns == 100) {
            DEBUG("stop displaying Overruns\n");
        }
    }
}

bool OrderedQueue::availableData() const
{
    // TODO Wait for filling gaps
    return _stock.size() > 0;
}

OrderedQueueData OrderedQueue::pop(int32_t *returnedIndex)
{
    OrderedQueueData oqd;
    uint32_t gap = 0;

    if (_stock.size() > 0) {
        int32_t nextIndex = (_lastIndexPop+1) % _maxIndex;
        bool found = false;
        while (not found) {
            try {
                oqd = move(_stock.at(nextIndex));
                _stock.erase(nextIndex);
                _lastIndexPop = nextIndex;
                if (returnedIndex) *returnedIndex = _lastIndexPop;
                found = true;
            }
            catch (const std::out_of_range&) {
                if (_stock.size() < _capacity) {
                    break;
                }
                else {
                    // Search for the new index, starting from the current one
                    // This could be optimised, but the modulo makes things
                    // not easy.
                    gap++;
                    nextIndex = (nextIndex+1) % _maxIndex;
                }
            }
        }
    }

    if (gap > 0) {
        DEBUG("index jump of %d\n", gap);
    }

    return oqd;
}