Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSimek committed Feb 4, 2017
0 parents commit 59eaecf
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 0 deletions.
39 changes: 39 additions & 0 deletions QtIntercom.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#-------------------------------------------------
#
# Project created by QtCreator 2017-02-04T14:41:59
#
#-------------------------------------------------

QT += core gui multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QtIntercom
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
mainwindow.cpp \
voiceio.cpp \
voicesocket.cpp \
buffer.cpp \
messenger.cpp

HEADERS += mainwindow.h \
voiceio.h \
voicesocket.h \
buffer.h \
messenger.h

FORMS += mainwindow.ui
17 changes: 17 additions & 0 deletions buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "buffer.h"

Buffer::Buffer(quint16 size, QObject *parent) :
QObject(parent)
{
this->size = size;
}

void Buffer::input(QByteArray data)
{
buffer.append(data);
if(buffer.length() >= size)
{
emit output(buffer);
buffer.clear();
}
}
24 changes: 24 additions & 0 deletions buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef BUFFER_H
#define BUFFER_H

#include <QObject>
#include <QByteArray>

class Buffer : public QObject
{
Q_OBJECT
public:
explicit Buffer(quint16 size, QObject *parent = 0);

signals:
void output(QByteArray data);

public slots:
void input(QByteArray data);

private:
QByteArray buffer;
quint16 size;
};

#endif // BUFFER_H
34 changes: 34 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "mainwindow.h"
#include <QApplication>

#include "messenger.h"

#include <QCommandLineParser>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("voice-over-lan");
a.setApplicationVersion("0.1");

QCommandLineParser parser;
parser.setApplicationDescription("Voice over LAN");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("address", QApplication::translate("main", "Address of the counterpart"));

parser.process(a);

QString address("");
if(!parser.positionalArguments().isEmpty())
{
address = parser.positionalArguments().at(0);
}

Messenger msgr(address);

//MainWindow w;
//w.show();

return a.exec();
}
16 changes: 16 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QHostAddress>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}
25 changes: 25 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:

private:
Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H
24 changes: 24 additions & 0 deletions mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
32 changes: 32 additions & 0 deletions messenger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "messenger.h"

Messenger::Messenger(QString address, QObject *parent) : QObject(parent)
{
quint16 buffersize = 0;
buf1 = new Buffer(buffersize);
buf2 = new Buffer(buffersize);

vso = new VoiceSocket();
vio = new VoiceIO();

if(address != "")
{
vso->connectToHost(QHostAddress(address), 30011); // QHostAddress::LocalHost
qDebug() << "Connecting to " << address;
}
else
{
qDebug() << "No peer address specified. Will only play sound from others";
}

vso->setEnabled(true);
vso->startListen();

// Voice in
connect(vio, SIGNAL(readVoice(QByteArray)), buf1, SLOT(input(QByteArray)));
connect(buf1, SIGNAL(output(QByteArray)), vso, SLOT(writeData(QByteArray)));

// Voice out
connect(vso, SIGNAL(readData(QByteArray)), buf2, SLOT(input(QByteArray)));
connect(buf2, SIGNAL(output(QByteArray)), vio, SLOT(writeVoice(QByteArray)));
}
36 changes: 36 additions & 0 deletions messenger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef MESSENGER_H
#define MESSENGER_H

#include <QUdpSocket>

#include <QAudio>
#include <QAudioInput>
#include <QAudioOutput>

#include <QDebug>

#include "buffer.h"
#include "voiceio.h"
#include "voicesocket.h"

class Messenger : public QObject
{
Q_OBJECT
public:
explicit Messenger(QString address, QObject *parent = 0);

signals:

public slots:

private:
QUdpSocket _udp;

// Voice
VoiceIO* vio;
VoiceSocket* vso;
Buffer* buf1;
Buffer* buf2;
};

#endif // MESSENGER_H
52 changes: 52 additions & 0 deletions voiceio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "voiceio.h"

#include <QAudio>
#include <QAudioInput>
#include <QAudioOutput>

#include <QDebug>

VoiceIO::VoiceIO(QObject *parent) : QObject(parent)
{
QAudioFormat format;
format.setSampleRate(8000); // 22050
format.setSampleSize(8); // 16
format.setChannelCount(1);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);

QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
qWarning() << "Default format not supported, trying to use the nearest.";
format = info.nearestFormat(format);
qDebug() << format.sampleRate();
qDebug() << format.sampleSize();
}

QAudioInput *InputAudio = new QAudioInput(format, this);

QAudioOutput *OutputAudio = new QAudioOutput(format, this);

devInput = InputAudio->start();
qDebug() << "Started " << info.deviceName();

//OutputAudio->start(devInput);
devOutput = OutputAudio->start();
QAudioDeviceInfo infoOutput(QAudioDeviceInfo::defaultOutputDevice());
qDebug() << "Started " << infoOutput.deviceName();

connect(devInput, SIGNAL(readyRead()), this, SLOT(devInput_readyRead()));
}

void VoiceIO::writeVoice(QByteArray data)
{
devOutput->write(data);
}

void VoiceIO::devInput_readyRead()
{
QByteArray data = devInput->readAll();
emit readVoice(data);
}
27 changes: 27 additions & 0 deletions voiceio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef VOICEIO_H
#define VOICEIO_H

#include <QIODevice>

class VoiceIO : public QObject
{
Q_OBJECT
public:
explicit VoiceIO(QObject *parent = 0);

signals:
void readVoice(QByteArray data);

public slots:
void writeVoice(QByteArray data);

private slots:
void devInput_readyRead();

private:
QIODevice* devInput;
QIODevice* devOutput;

};

#endif // VOICEIO_H
51 changes: 51 additions & 0 deletions voicesocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "voicesocket.h"

VoiceSocket::VoiceSocket(QObject *parent) :
QObject(parent)
{
//isEnabled = false;
connect(&udp, SIGNAL(readyRead()), this, SLOT(udp_readyRead()));
}

void VoiceSocket::connectToHost(QHostAddress ip, quint16 port)
{
peer_ip = ip;
peer_port = port;
}

void VoiceSocket::startListen(quint16 port)
{
udp.bind(QHostAddress::Any, port);
}

void VoiceSocket::writeData(QByteArray data)
{
//if(!isEnabled) return;
QByteArray data2 = qCompress(data, 9);
//qDebug() << "Sent " << peer_ip;
//qDebug() << "Sent1 " << data.length();
//qDebug() << "Sent2 " << data2.length();
udp.writeDatagram(data2, peer_ip, peer_port);
}

void VoiceSocket::udp_readyRead()
{
int size = udp.pendingDatagramSize();
QHostAddress host;
quint16 port;
QByteArray data;
data.resize(size);
udp.readDatagram(data.data(), size, &host, &port);
//qDebug() << "Recd " << host;
//if(!isEnabled) return;
//if(host != peer_ip) return;
QByteArray data2 = qUncompress(data);
//qDebug() << "Recd1 " << data.length();
//qDebug() << "Recd2 " << data2.length();
emit readData(data2);
}

void VoiceSocket::setEnabled(bool val)
{
//isEnabled = val;
}
Loading

0 comments on commit 59eaecf

Please sign in to comment.