#include <ukui-log4qt.h>

#include "runtimerecorder.h"
#include <kylin-ai/ai-base/speech.h>

/**
 * @brief runtimeSpeechCallback
 *          聊天结果返回回调函数，数据流式返回
 * @param result
 * @param usr_data
 */
static void runtimeSpeechCallback(RecognitionResult result, void *usr_data)
{
    RuntimeRecorder *runtimeRecorder = (RuntimeRecorder*)usr_data;

    emit runtimeRecorder->sigShowText("");
}

RuntimeRecorder::RuntimeRecorder(QObject *parent) : QObject(parent)
{
    m_aRecorder = new QAudioRecorder();
    m_aProbe = new QAudioProbe();

    QStringList listCodecs = m_aRecorder->supportedAudioCodecs();
    QStringList listContainers = m_aRecorder->supportedContainers();

    m_aRecorder->setAudioInput(m_aRecorder->defaultAudioInput());
    m_aEncoder.setCodec("audio/x-raw");
    m_aEncoder.setSampleRate(8000);
    m_aEncoder.setChannelCount(1);
    m_aEncoder.setEncodingMode( QMultimedia::ConstantQualityEncoding );
    //将编码器设置给录音对象
    m_aRecorder->setAudioSettings( m_aEncoder );
    m_aRecorder->setContainerFormat("audio/x-wav");
    m_aRecorder->setOutputLocation( QUrl::fromLocalFile("test.wav"));
}

void RuntimeRecorder::startRuntimeRecord()
{
    m_aRecorder->record();

    emit sigShowText("我在我在我在我在我在我在");
}

void RuntimeRecorder::stopRuntimeRecord()
{
    m_aRecorder->stop();
}

void RuntimeRecorder::sendRuntimeRcord()
{

}

void RuntimeRecorder::initSession()
{
    //    QThread::create([this]{
    //        void *session = nullptr;
    //        if (speech_init_session(&session) != SPEECH_SUCCESS)
    //            KyInfo() << "speech create session error";
    //        else
    //        {
    //            this->session;
    //            speech_set_recognizing_callback(this->session, speechCallback, this);

    //        }
    //    })->start();
}

void RuntimeRecorder::closeSession()
{

}
