Subversion Repositories tpanel

Rev

Rev 474 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
457 andreas 1
/*
2
 * Copyright (C) 2024 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef TQINTERCOM_H
20
#define TQINTERCOM_H
21
 
458 andreas 22
#include <string>
460 andreas 23
#include <QObject>
461 andreas 24
#include <QAudioFormat>
460 andreas 25
#include <QAbstractSocket>
468 andreas 26
#include <QAudioSink>
469 andreas 27
#include <QUdpSocket>
470 andreas 28
#include <QScopedPointer>
458 andreas 29
 
477 andreas 30
#include "taudioconvert.h"
31
 
458 andreas 32
typedef struct INTERCOM_t
33
{
34
    std::string ip;         // IP address of panel to connect to
35
    long rxPort{0};          // Network port number for receiver
36
    long txPort{0};          // Network port number for transmitter
37
    int mode{0};            // The mode (0 = talk, 1 = listen, 2 = both)
38
}INTERCOM_t;
39
 
461 andreas 40
class QAudioSource;
465 andreas 41
class QBuffer;
469 andreas 42
class QTimer;
43
class QTimerEvent;
460 andreas 44
 
477 andreas 45
class TMicrophone : public QIODevice, public TAudioConvert
467 andreas 46
{
47
    public:
470 andreas 48
        TMicrophone();
469 andreas 49
        ~TMicrophone();
467 andreas 50
 
470 andreas 51
        void start();
52
        void stop();
53
 
467 andreas 54
        qint64 readData(char *data, qint64 maxlen) override;
55
        qint64 writeData(const char *data, qint64 len) override;
56
        qint64 bytesAvailable() const override;
57
        qint64 size() const override { return mBuffer.size(); }
58
 
59
    private:
60
        qint64 mPos{0};
61
        QByteArray mBuffer;
62
};
63
 
477 andreas 64
class TQIntercom : public QObject, public TAudioConvert
457 andreas 65
{
460 andreas 66
    Q_OBJECT
67
 
458 andreas 68
    public:
460 andreas 69
        TQIntercom(QObject *parent);
70
        TQIntercom(QObject *parent, INTERCOM_t ic);
458 andreas 71
        ~TQIntercom();
457 andreas 72
 
458 andreas 73
        void start();
74
        void stop();
461 andreas 75
        void setMicrophoneLevel(int level);
458 andreas 76
        void setSpeakerLevel(int level);
77
        void setMute(bool mute);
78
        void setIntercom(INTERCOM_t ic);
79
 
459 andreas 80
    protected:
460 andreas 81
        bool connectTalker();   // Talker
82
        bool spawnServer();     // Listener
459 andreas 83
 
465 andreas 84
        void onInputStateChanged(QAbstractSocket::SocketState socketState);
85
        void onOutputStateChanged(QAbstractSocket::SocketState socketState);
86
        void stateChangedMessage(QAbstractSocket::SocketState socketState, bool in);
87
        void onInputErrorOccurred(QAbstractSocket::SocketError socketError);
88
        void onOutputErrorOccurred(QAbstractSocket::SocketError socketError);
89
        void socketErrorMessages(QAbstractSocket::SocketError socketError, const std::string& msg);
90
        void onReadPendingDatagrams();
469 andreas 91
        void onRecordPermissionGranted();
471 andreas 92
        void onMicStateChanged(QAudio::State state);
460 andreas 93
 
470 andreas 94
        qreal convertVolume(int volume);
466 andreas 95
 
458 andreas 96
    private:
474 andreas 97
        typedef struct _HEADER_t
98
        {
99
            uint16_t ident{0x8000};
100
            uint16_t counter{0};
101
            uint32_t position{0};
102
            uint16_t unk1{0xf8a8};
103
            uint16_t unk2{0xe554};
104
        }_HEADER_t;
105
 
106
        long getNextBlock(QByteArray *target, const QByteArray& data);
107
 
458 andreas 108
        INTERCOM_t mIntercom;
470 andreas 109
        int mMicLevel{100};
110
        int mSpkLevel{100};
458 andreas 111
        bool mInitialized{false};
471 andreas 112
        bool mTalkConnected{false};
469 andreas 113
        bool mRecordPermissionGranted{false};
474 andreas 114
        bool mMicOpen{false};
115
        _HEADER_t mHeader;
459 andreas 116
 
460 andreas 117
        QUdpSocket *mUdpTalker{nullptr};
118
        QUdpSocket *mUdpListener{nullptr};
461 andreas 119
        QAudioFormat mAudioFormat;
120
        QAudioSource *mMicrophone{nullptr}; // Talk --> from mic to remote
121
        QAudioSink *mRemote{nullptr};       // Listen --> from remote to speaker
469 andreas 122
        QByteArray mReadBuffer;
123
        TMicrophone *mMicDevice{nullptr};
470 andreas 124
        QTimer *mPushTimer{nullptr};
473 andreas 125
        QTimer *mPullTimer{nullptr};
471 andreas 126
        QAudioDevice mAudioMicDevice;
457 andreas 127
};
128
 
129
#endif // TQINTERCOM_H