Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[e8cdcd]: plugins / internetradio / stream_input_buffer.cpp Maximize Restore History

Download this file

stream_input_buffer.cpp    152 lines (120 with data), 5.8 kB

  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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/***************************************************************************
stream_input_buffer.cpp - description
-------------------
begin : Sun Jan 22 2012
copyright : (C) 2012 by Martin Witte
email : emw-kradio@nocabal.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "stream_input_buffer.h"
#include "errorlog_interfaces.h"
#include <klocale.h>
#include <stdio.h>
StreamInputBuffer::StreamInputBuffer(int max_size)
: m_inputBufferMaxSize (max_size),
m_inputBufferSize (0),
m_readPending (0),
m_readPendingReleased(0)
{
}
StreamInputBuffer::~StreamInputBuffer()
{
resetBuffer();
}
// blocking function if buffer is empty!
QByteArray StreamInputBuffer::readInputBuffer(size_t minSize, size_t maxSize, bool consume, bool &err)
{
minSize = qMin(minSize, maxSize);
bool isfull = false;
bool resetDetected = false;
QByteArray retval;
{ QMutexLocker lock(&m_inputBufferAccessLock);
m_readPending += minSize;
}
// block until at least 1 byte is readable
// printf ("buffer start read(minsize = %zi, maxsize = %zi) - acquire\n", minSize, maxSize);
m_inputBufferSize.acquire(minSize);
{ QMutexLocker lock(&m_inputBufferAccessLock);
// printf ("buffer start read(minsize = %zi, maxsize = %zi) - locked\n", minSize, maxSize);
// new reset detection code
// we had a reset if (m_readPendingReleased != 0)
// this means that the semaphore was m_readPendingReleased higher than the buffer size when
// the acquire above succeeded.
// For us it means, that the requested minSize is completely unavailable from the buffer.
if (m_readPendingReleased > 0) {
// printf ("buffer reset detected\n");
resetDetected = true;
if (minSize > m_readPendingReleased) {
// this should never happen, but let's handle it gracefully:
m_inputBufferSize.release(minSize - m_readPendingReleased);
IErrorLogClient::staticLogError(i18n("This should never happen: inconsistency in buffer locking / buffer size"));
}
m_readPendingReleased -= qMin(m_readPendingReleased, minSize);
}
// although it should not happen, we need to consider that some other process could
// be also acquiring concurrently
size_t realSize = qMin(maxSize, (size_t)m_inputBufferSize.available() + minSize);
// realSize = qMax(minSize, realSize - realSize % 8); // alignment! Otherwise decoder might fail!
// printf ("bufferSize = %i minSize = %zi, maxSize = %zi realSize = %zi\n", m_inputBuffer.size(), minSize, maxSize, realSize);
QByteArray shared = m_inputBuffer.left(realSize);
// printf ("reading stream input buffer: shared.size = %i\n", shared.size());
if (!resetDetected && (size_t)shared.size() >= minSize) {
retval = QByteArray(shared.data(), shared.size()); // force deep copy for threading reasons
if (consume) {
m_inputBuffer.remove(0, retval.size());
}
isfull = (size_t)m_inputBuffer.size() >= m_inputBufferMaxSize;
}
// must be inside locked region in order to guarantee the resetBuffer works correctly
// keep track of the bytes read
// we got more data than minSize: thus we need to acquire more
if ((size_t)retval.size() > minSize) {
m_inputBufferSize.acquire(retval.size() - minSize);
}
// if we don't consume the data, we need to release what we acquired before
if (!consume && retval.size()) {
m_inputBufferSize.release(retval.size());
}
m_readPending -= minSize;
}
// printf ("reading stream input buffer: %i bytes (min = %zi, max = %zi)\n", retval.size(), minSize, maxSize);
if (!isfull) {
emit sigInputBufferNotFull();
}
err = resetDetected;
return retval;
}
void StreamInputBuffer::slotWriteInputBuffer(QByteArray data)
{
bool isFull = false;
// int oldSize = 0;
{ QMutexLocker lock(&m_inputBufferAccessLock);
// oldSize = m_inputBuffer.size();
m_inputBuffer.append(data.data(), data.size()); // force deep copy
isFull = (size_t)m_inputBuffer.size() >= m_inputBufferMaxSize;
m_inputBufferSize.release(data.size());
}
if (isFull) {
emit sigInputBufferFull();
}
// printf ("wrote stream input buffer: %i bytes (oldSize = %i, newSize = %i)\n", data.size(), oldSize, m_inputBuffer.size());
}
void StreamInputBuffer::resetBuffer()
{
QMutexLocker lock(&m_inputBufferAccessLock);
// printf ("reset input buffer\n");
while (m_inputBufferSize.available()) {
m_inputBufferSize.tryAcquire(m_inputBufferSize.available());
}
m_inputBuffer.clear();
m_readPendingReleased += m_readPending;
m_inputBufferSize.release(m_readPending); // ensure that a waiting read gets released (they will return 0 bytes)
emit sigInputBufferNotFull();
}