欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

QT 使用 websocket 实现语音通话-ctWsTalk.h

最编程 2024-10-13 07:29:57
...
#ifndef CTWSTALK_H
#define CTWSTALK_H

#include <QAudio>
#include <QFile>
#include <QElapsedTimer>
#include <list>
#include <QWebSocket>
#include <QObject>

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

    static ctWsTalk& getInstance();

    void startWsTalk(QString url);
    void stopWsTalk();

public slots:
    void onConnected();
    void onDisconnected();
    void onBinaryMessageReceived(const QByteArray& data);

private slots:
    void handleStateChanged_input(QAudio::State newState);
    void handleAudioNotify();
    void handleAudioData();

private:
    void wsClientStart();
    void wsClientClose();
    void wsSendBinary(const QByteArray& binaryData);
    void initMicrophoneInput();
    void initSpeakerOutput();
    void startIntercom();
    void stopIntercom();

private:
    bool m_bWsConnect = false;
    int m_nAudioSize = 0;
    QFile m_fileIn;
    QFile m_fileOut;
    QElapsedTimer m_timer;
    QString m_sUrl;

    class QAudioInput* m_pAudioInput = nullptr;
    class QAudioOutput* m_pAudioOutput = nullptr;
    class QIODevice* m_pInputDevice = nullptr;
    QIODevice* m_pOutputDevice = nullptr;
    QWebSocket m_websocket;
    std::list<QByteArray> m_audioinDataList;

};

#endif // CTWSTALK_H