PDA

View Full Version : QAudioInput increases CPU load and causes segmentation fault



mc22_90
26th November 2014, 09:56
OS:Linux x86_64(Fedora18)
Qt:4.8
CPU:i7-3930K 3.20GHz

I am making a recording program using QAudioInput in Qt Multimedia. When I run my program, the CPU utilization is under a few percents initially but it becomes larger and larger up to more than 50% in some hours,and then segmentaion fault occurs.

The debugger told that was happened in pcm.c:2465
return pcm->fast_ops->avail_update(pcm->fast_op_arg);
called from qaudioinput_alsa_p.cpp:466 .
int frames = snd_pcm_avail_update(handle);

This is my code to reproduce that. This code records pcm data and clear it endlessly.
(Of cause, program termination is omitted, ex. destructor, and can not be terminated normally. Just for reproduction of run out of CPU resource.)

main.cpp


#include <QCoreApplication>
#include <QThread>
#include "recorder.hpp"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThread rec_thread;
Recorder* recorder = new Recorder(0);
recorder->moveToThread(&rec_thread);
rec_thread.start();

QMetaObject::invokeMethod(recorder, "startRecording");
return a.exec();
}



recorder.hpp


#include <QBuffer>
#include <QObject>
#include <QAudioInput>

class Recorder : public QObject
{
Q_OBJECT
public:
explicit Recorder(QObject *parent = 0);

signals:
void restartRecorder();
private slots:
void startRecording();
void stopRecording();

private:
QBuffer reading_buffer_;
QAudioInput* audio_input_;
QAudioFormat format_;
};



recorder.cpp


#include "recorder.hpp"
#include <QDebug>
#include <QTimer>

Recorder::Recorder(QObject *parent) :
QObject(parent)
{
format_.setSampleRate(44100); format_.setChannelCount(1);
format_.setSampleSize(16); format_.setSampleType(QAudioFormat::SignedInt);
format_.setCodec("audio/pcm"); format_.setByteOrder(QAudioFormat::LittleEndian);
audio_input_ = new QAudioInput(format_, 0);
connect(this, SIGNAL(restartRecorder()), this, SLOT(startRecording()));
}

void Recorder::startRecording()
{
reading_buffer_.open(QBuffer::WriteOnly | QBuffer::Truncate);
if(!reading_buffer_.isOpen()) qFatal("buffer not opened");
QTimer::singleShot(1000, this, SLOT(stopRecording()));
audio_input_->start(&reading_buffer_);
}

void Recorder::stopRecording()
{
audio_input_->stop();
audio_input_->reset();
reading_buffer_.close();
qDebug() << "Recording succeeded";
emit restartRecorder();
}




What is wrong with my program?

Thanks in advance