summaryrefslogtreecommitdiffstats
path: root/OutputBuffer.h
blob: d1773aeae9b0edbfa2febcdf7b0d6b845225680f (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
/*
 * Copyright (C) 2017 Konsulko Group
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Simple buffer device to act as input device for QAudioInput.
 *
 * It basically wraps QByteArray with some additional locking that
 * the QBuffer class does not provide.  It also ensures that reads
 * return in units of the given format's sample size.  This is
 * required to handle the RTL-SDR library output sometimes arriving
 * with half a sample at the end.  QAudioOutput will blindly pass
 * that half sample along and trigger a PulseAudio error if we do
 * not handle it here.
 */

#ifndef OUTPUTBUFFER_H
#define OUTPUTBUFFER_H

#include <QtCore/QIODevice>
#include <QtCore/QMutex>
#include <QtMultimedia/QAudioFormat>

class OutputBuffer : public QIODevice
{
    Q_OBJECT

public:
    OutputBuffer(const QAudioFormat &format, QObject *parent);
    ~OutputBuffer();

    void start();
    void stop();

    qint64 readData(char *data, qint64 maxlen);
    qint64 writeData(const char *data, qint64 len);
    qint64 bytesAvailable();

private:
    QMutex m_mutex;
    QByteArray m_buffer;
    unsigned int m_sample_size;
};

#endif // OUTPUTBUFFER_H