QAudioOutput and QAudioInput problem



  • hi,i got a problem.My APP is a video broadcast APP which get vedio and audio data from ipc(my hardware),i played audio data which got from ipc through QAudioOutput and collected audio data which got from my phone through QAudioInput,my problem is Once i started QAudioInput,loudspeaker volume became very light,i tried many method,but all of them had no effect,Below is my code,any advice would be very grateful.

    ioaudio.h
    class AudioIODevice : public QIODevice
    {
    Q_OBJECT

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

    void Open(OpenModeFlag flag);
    void Close();
    qint64 readData(char *data, qint64 maxlen);
    qint64 writeData(const char *data, qint64 len);
    qint64 bytesAvailable() const;
    void   InputData(const char *data, qint64 len);
    void clearBuffer();
    
    std::mutex *m_mutexBuffer;
    QByteArray  m_szBuffer;
    

    private:
    bool m_bOpen;
    quint32 m_maxAmplitude;
    const QAudioFormat m_format;
    qreal m_level; // 0.0 <= m_level <= 1.0
    void *m_parent;
    };

    typedef enum {
    PLAY = 0,
    TALK,
    NONE
    }AUDIO_TYPE;

    class IOAudio: public QObject {
    Q_OBJECT
    public:
    IOAudio();
    ~IOAudio();

    private:
    void openOutputAudio();
    void closeOutputAudio();
    void openInputAudio();
    void closeInputAudio();

    QAudioOutput *m_audioOutput;
    QAudioInput *m_audioInput;
    AudioIODevice *m_input;
    AudioIODevice *m_output;
    QAudioFormat m_inputformat;
    QAudioFormat m_outputformat;
    int     m_AudioType;
    QAudioDeviceInfo m_inputDevice;
    QAudioDeviceInfo m_outputDevice;
    bool m_bTalkStatus;
    

    public:
    void InputAudioData(char *pbuf, long lsize);
    public slots:
    void onInitIOAudio();
    void onUninitIOAudio();
    void onSetAudioType(int AudioType);
    void onStartTalk(int iCameraID);
    void onStopTalk(int iCameraID);
    void onTalkLevel(qreal level);
    signals:
    void startSendTalkData(QByteArray *sendbuf, long mutex, int iCameraID);
    void startTalkResult(bool isSuccessed);
    void stopTalkResult(bool isSuccessed);
    void talkLevel(qreal level);
    };

    ioaudio.cpp
    #include "ioaudio.h"
    #include <QDebug>
    #include <QThread>
    #include <QtEndian>
    #include <QMetaType>

    IOAudio::IOAudio()
    {
    m_bTalkStatus = false;
    m_audioOutput = NULL;
    m_audioInput = NULL;
    }

    IOAudio::~IOAudio()
    {

    }

    void IOAudio::onInitIOAudio()
    {
    m_AudioType = NONE;
    m_inputformat.setSampleRate(8000);
    m_inputformat.setChannelCount(1);
    m_inputformat.setSampleSize(16);
    m_inputformat.setCodec("audio/pcm");
    m_inputformat.setByteOrder(QAudioFormat::LittleEndian);
    m_inputformat.setSampleType(QAudioFormat::SignedInt);

    m_outputformat.setSampleRate(8000);
    m_outputformat.setChannelCount(1);
    m_outputformat.setSampleSize(16);
    m_outputformat.setCodec("audio/pcm");
    m_outputformat.setByteOrder(QAudioFormat::LittleEndian);
    m_outputformat.setSampleType(QAudioFormat::SignedInt);
    m_inputDevice = QAudioDeviceInfo::defaultInputDevice();
    if (!m_inputDevice.isFormatSupported(m_inputformat)) {
        qDebug() << "Default format not supported - trying to use nearest";
        m_inputformat = m_inputDevice.nearestFormat(m_inputformat);
    }
    m_outputDevice = QAudioDeviceInfo::defaultOutputDevice();
    if (!m_outputDevice.isFormatSupported(m_outputformat)) {
        qDebug() << "Default format not supported - trying to use nearest";
        m_outputformat = m_outputDevice.nearestFormat(m_outputformat);
    }
    m_input = new AudioIODevice(m_inputformat, this);
    m_output = new AudioIODevice(m_outputformat, this);
    

    }

    void IOAudio::onUninitIOAudio()
    {
    if( m_audioInput ) {
    delete m_audioInput;
    m_audioInput = NULL;
    }
    if( m_audioOutput ) {
    delete m_audioOutput;
    m_audioOutput = NULL;
    }

    if( m_input ) {
        delete m_input;
        m_input = NULL;
    }
    if( m_output ) {
        delete m_output;
        m_output = NULL;
    }
    

    }

    void IOAudio::onSetAudioType(int AudioType)
    {
    qDebug()<<"onSetAudioType"<<AudioType;
    if(AudioType == NONE) {
    closeOutputAudio();
    closeInputAudio();
    } else if(AudioType == PLAY) {
    closeInputAudio();
    openOutputAudio();
    } else {
    closeOutputAudio();
    openInputAudio();
    }
    }

    void IOAudio::InputAudioData(char *pbuf, long lsize)
    {
    if(!m_bTalkStatus)
    m_output->InputData(pbuf, lsize);
    }

    void IOAudio::openOutputAudio()
    {
    if(m_audioOutput == NULL) {
    m_output->m_szBuffer.clear();
    m_audioOutput = new QAudioOutput(m_outputDevice, m_outputformat, this);
    m_output->Open(QIODevice::ReadOnly);
    //if(m_audioOutput->state() == QAudio::SuspendedState || m_audioOutput->state() == QAudio::StoppedState) {
    m_audioOutput->reset();
    m_audioOutput->start(m_output);
    //}
    m_audioOutput->setVolume(1);
    }
    }

    void IOAudio::closeOutputAudio()
    {
    if(m_audioOutput) {
    //if(m_audioOutput->state() == QAudio::ActiveState || m_audioOutput->state() == QAudio::IdleState)
    m_audioOutput->stop();
    m_output->Close();
    delete m_audioOutput;
    m_audioOutput = NULL;
    m_output->m_szBuffer.clear();
    }
    }

    void IOAudio::openInputAudio()
    {
    if(m_audioInput == NULL) {
    qDebug()<<"openInputAudio";
    m_audioInput = new QAudioInput(m_inputDevice, m_inputformat, this);
    m_input->Open(QIODevice::WriteOnly);
    m_audioInput->start(m_input);
    //m_audioInput->setVolume(1);
    }
    }

    void IOAudio::closeInputAudio()
    {
    if(m_audioInput) {
    m_audioInput->stop();
    m_input->Close();
    delete m_audioInput;
    m_audioInput = NULL;
    m_input->m_szBuffer.clear();
    }
    }

    //void IOAudio::setAudioVolume(qreal VolumeVal)
    //{
    // m_audioOutput->setVolume(VolumeVal);
    //}


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.