/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <QCoreApplication>
#include <QAudioFormat>
#include <QAudioDeviceInfo>
#include <QDebug>
#include <QTimer>
#include <QFile>
#include <QAudioInput>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QAudioFormat audioFormat;

    audioFormat.setByteOrder(QAudioFormat::LittleEndian);
    audioFormat.setChannelCount(1);
    audioFormat.setCodec("audio/pcm");
    audioFormat.setSampleRate(16000);
    audioFormat.setSampleSize(16);
    audioFormat.setSampleType(QAudioFormat::SignedInt);

    // 判断设备，查看是否存在
    QAudioDeviceInfo devInfo = QAudioDeviceInfo::defaultInputDevice();
    if (devInfo.isNull()) {
        qWarning() << "No recording device was found!";
    }

    // 不支持格式，使用最接近格式
    if (!devInfo.isFormatSupported(audioFormat)) {  // 当前使用设备是否支持
        qWarning() << "Automatic conversion is made to the nearest format!";
        audioFormat = devInfo.nearestFormat(audioFormat);  // 转换为最接近格式
    }

    QAudioInput *audioInput = new QAudioInput(devInfo, audioFormat, nullptr);

    QFile destinationFile;
    destinationFile.setFileName("./test_record.flv");
    destinationFile.open( QIODevice::ReadWrite | QIODevice::Truncate );

    audioInput->start(&destinationFile);

    QTimer *timer = new QTimer();
    QObject::connect(timer, &QTimer::timeout, [&destinationFile, audioInput](){
        // 处理超时的逻辑
        qDebug() << "Timer timeout!";
        audioInput->stop();
        destinationFile.close();
        delete audioInput;
    });
    timer->start(5000);

    return a.exec();
}
