41 m_currentStartPosition(1),
42 m_segmentsLock(QReadWriteLock::Recursive),
46 m_maxSegments(MaxSegments),
49 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Allocated segmented ring buffer of size %1bytes").arg(
m_size));
116 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Cannot finish segment - nothing written"));
139 if (!Data || Size <= 0 || Size >=
m_size)
148 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Segmented buffer underrun - no space for write"));
153 QPair<int,int> segment =
m_segments.dequeue();
163 memcpy((
void*)
m_data.constData(), Data + copy, Size - copy);
175 return Write((uint8_t*)Data->constData(), Size);
187 if (!Dst || Max <= 0 || Max >
m_size || SegmentRef < 0 || Offset < 0 || Offset >
m_size)
196 if (Offset >= segment.second)
199 int size = qMin(Max, segment.second - Offset);
202 int readpos = (segment.first + Offset) % m_size;
203 int read = qMin(size, m_size - readpos);
204 memcpy(Dst,
m_data.constData() + readpos, read);
206 memcpy(Dst + read,
m_data.constData(), size - read);
216 if (!Dst || SegmentRef < 0)
225 int read = qMin(segment.second,
m_size - segment.first);
226 Dst->write((
const char*)(
m_data.constData() + segment.first), read);
227 if (read < segment.second)
228 Dst->write((
const char*)
m_data.constData(), segment.second - read);
229 return segment.second;
238 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Cannot retrieve init segment - zero or >1 segments"));
244 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"Already have init segment - overwriting"));
247 QPair<int,int> segment =
m_segments.dequeue();
249 int read = qMin(segment.second,
m_size - segment.first);
251 if (read < segment.second)
252 memcpy((
void*)(
m_initSegment.constData() + read),
m_data.constData(), segment.second - read);
255 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Init segment saved (%1 bytes) - resetting ringbuffer").arg(
m_initSegment.size()));
278 QByteArray result(segment.second,
'0');
279 int read = qMin(segment.second,
m_size - segment.first);
280 memcpy((
void*)result.constData(),
m_data.constData() + segment.first, read);
281 if (read < segment.second)
282 memcpy(result.data() + read,
m_data.constData(), segment.second - read);
void InitSegmentReady(void)
int GetHead(void)
Return the number of the segment at the head of the queue (the newest).
QQueue< QPair< int, int > > m_segments
QByteArray GetInitSegment(void)
Return a copy of the MP4 'init' segment.
int m_currentStartPosition
QQueue< int > m_segmentRefs
~TorcSegmentedRingBuffer()
int GetSize(void)
Return the size of the buffer (NOT the number of segments).
int FinishSegment(bool Init)
Finish the current segment and start a new one.
int ReadSegment(uint8_t *Data, int Max, int SegmentRef, int Offset=0)
Copy Max bytes of a segment identified by SegmentRef into the memory pointed to at Dst...
void SegmentReady(int Segment)
void SegmentRemoved(int Segment)
int GetSegmentsAvail(int &TailRef)
Return the number of available segments.
int Write(QByteArray *Data, int Size)
TorcSegmentedRingBuffer(int Size, int MaxSegments)
#define LOG(_MASK_, _LEVEL_, _STRING_)
void SaveInitSegment(void)
Save the MP4 'init' segment.
int GetBytesFree(void)
Return the number of free bytes available for writing.
QByteArray GetSegment(int SegmentRef)
Return a copy of the segment identified by SegmentRef.
QReadWriteLock m_segmentsLock