File: RecordThread.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (213 lines) | stat: -rw-r--r-- 6,671 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*************************************************************************
       RecordThread.cpp  -  thread for lowlevel audio recording
                             -------------------
    begin                : Mon Oct 20 2003
    copyright            : (C) 2003 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.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 "config.h"

#include <errno.h>

#include <QMutexLocker>
#include <QVariant>

#include "RecordDevice.h"
#include "RecordThread.h"

//***************************************************************************
Kwave::RecordThread::RecordThread()
    :Kwave::WorkerThread(nullptr, QVariant()),
     m_lock(),
     m_device(nullptr),
     m_empty_queue(),
     m_full_queue(),
     m_buffer_count(0),
     m_buffer_size(0)
{
}

//***************************************************************************
Kwave::RecordThread::~RecordThread()
{
    stop();

    {
        QMutexLocker lock(&m_lock);
        m_full_queue.clear();
        m_empty_queue.clear();
    }
}

//***************************************************************************
void Kwave::RecordThread::setRecordDevice(Kwave::RecordDevice *device)
{
    Q_ASSERT(!isRunning());
    if (isRunning()) return;

    m_device = device;
}

//***************************************************************************
int Kwave::RecordThread::setBuffers(unsigned int count, unsigned int size)
{
    QMutexLocker lock(&m_lock);

    Q_ASSERT(!isRunning());
    if (isRunning()) return -EBUSY;

    // flush all queues
    m_full_queue.clear();
    m_empty_queue.clear();

    // fill the "empty" queue again
    QByteArray buf(size, 0x00);
    for (unsigned int i = 0; i < count; i++)
        m_empty_queue.enqueue(buf);

    // take the new settings
    m_buffer_size  = size;
    m_buffer_count = count;

    // return number of buffers or -ENOMEM if not even two allocated
    return (m_empty_queue.count() >= 2) ?
        static_cast<unsigned int>(m_empty_queue.count()) : -ENOMEM;
}

//***************************************************************************
unsigned int Kwave::RecordThread::remainingBuffers()
{
    QMutexLocker lock(&m_lock);
    return static_cast<unsigned int>(m_empty_queue.count());
}

//***************************************************************************
unsigned int Kwave::RecordThread::queuedBuffers()
{
    QMutexLocker lock(&m_lock);
    return static_cast<unsigned int>(m_full_queue.count());
}

//***************************************************************************
QByteArray Kwave::RecordThread::dequeue()
{
    QMutexLocker lock(&m_lock);

    if (m_full_queue.count()) {
        // de-queue the buffer from the full list
        QByteArray buf = m_full_queue.dequeue();

        // put the buffer back to the empty list
        m_empty_queue.enqueue(buf);

        // return the buffer
        return buf;
    } else {
        // return an empty buffer
        return QByteArray();
    }
}

//***************************************************************************
void Kwave::RecordThread::run()
{
    int  result      = 0;
    bool interrupted = false;

    // read data until we receive a close signal
    while (!isInterruptionRequested() && !interrupted) {
        // dequeue a buffer from the "empty" queue

        QByteArray buffer;
        qsizetype  len;
        {
            QMutexLocker lock(&m_lock);

            if (m_empty_queue.isEmpty()) {
                // we had a "buffer overflow"
                qWarning("RecordThread::run() -> NO EMPTY BUFFER FOUND !!!");
                result = -ENOBUFS;
                break;
            }

            buffer  = m_empty_queue.dequeue();
            len     = buffer.size();
            Q_ASSERT(buffer.size());
            if (!len) {
                result = -ENOBUFS;
                break;
            }
        }

        // read into the current buffer
        unsigned int offset = 0;
        while (len && !interrupted && !isInterruptionRequested()) {
            // read raw data from the record device
            result =  (m_device) ?
                m_device->read(buffer, offset) : -EBADF;

            if ((result < 0) && (result != -EAGAIN))
                qWarning("RecordThread: read result = %d (%s)",
                         result, strerror(-result));

            if (result == -EAGAIN) {
                continue;
            } else if (result == -EBADF) {
                // file open has failed
                interrupted = true;
                break;
            } else if (result == -EINTR) {
                // thread was interrupted, received signal?
                interrupted = true;
                break;
            } else if (result < 1) {
                // something went wrong !?
                interrupted = true;
                qWarning("RecordThread::run(): read returned %d", result);
                break;
            } else {
                offset += result;
                len = buffer.size() - offset;
                Q_ASSERT(len >= 0);
                if (len < 0) len = 0;
            }
        }

        // return buffer into the empty queue and abort on errors
        // do not use it
        if (interrupted && (result < 0)) {
            QMutexLocker lock(&m_lock);
            m_empty_queue.enqueue(buffer);
            break;
        }

        // inform the application that there is something to dequeue
        {
            QMutexLocker lock(&m_lock);
            m_full_queue.enqueue(buffer);
        }
        emit bufferFull();
    }

    // do not evaluate the result of the last operation if there
    // was the external request to stop
    if (isInterruptionRequested() || (interrupted && (result > 0)))
        result = 0;

    if (result) emit stopped(result);
}

//***************************************************************************
//***************************************************************************

#include "moc_RecordThread.cpp"