Skip to content

Commit

Permalink
move handlers, rename udpsocket, individual ports
Browse files Browse the repository at this point in the history
  • Loading branch information
taraldv committed Jul 31, 2020
1 parent b3a4fca commit ece0fa4
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 102 deletions.
4 changes: 2 additions & 2 deletions audiohandler.cpp → handlers/audiohandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "audiohandler.h"
AudioHandler::AudioHandler(QString _audioDeviceName, std::mutex* _writeLock,int64_t _time, SocketHandler *_socketHandler, int bufferSize)/*, QObject* parent): QObject(parent)*/
AudioHandler::AudioHandler(QString _audioDeviceName, std::mutex* _writeLock,int64_t _time, UdpSocketHandler *_socketHandler, int bufferSize)/*, QObject* parent): QObject(parent)*/
{
mBufferSize = bufferSize;
mSocketHandler = _socketHandler;
Expand Down Expand Up @@ -918,7 +918,7 @@ void AudioHandler::changeAudioInputDevice(QString deviceName)
}
int AudioHandler::audioCustomSocketWrite(void* opaque, uint8_t *buffer, int buffer_size)
{
SocketHandler* socketHandler = reinterpret_cast<SocketHandler*>(opaque);
UdpSocketHandler* socketHandler = reinterpret_cast<UdpSocketHandler*>(opaque);
char *cptr = reinterpret_cast<char*>(const_cast<uint8_t*>(buffer));
QByteArray send;
send = QByteArray(reinterpret_cast<char*>(cptr), buffer_size);
Expand Down
6 changes: 3 additions & 3 deletions audiohandler.h → handlers/audiohandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ extern "C" {
#include "libswresample/swresample.h"
}
#include <stdio.h>
#include "sockethandler.h"
#include "handlers/udpsockethandler.h"

class AudioHandler
{
public:
AudioHandler(QString cDeviceName, std::mutex* _writeLock,int64_t time,SocketHandler*, int bufferSize);
AudioHandler(QString cDeviceName, std::mutex* _writeLock,int64_t time,UdpSocketHandler*, int bufferSize);
int grabFrames();
int init();
void changeAudioInputDevice(QString deviceName);
Expand Down Expand Up @@ -52,7 +52,7 @@ class AudioHandler
int writeOutputFileHeader();
int initFifo();
int initResampler();
SocketHandler *mSocketHandler;
UdpSocketHandler *mSocketHandler;
AVFormatContext *mInputFormatContext;
AVFormatContext *mOutputFormatContext;
AVCodecContext *mInputCodecContext;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion audioplaybackhandler.h → handlers/audioplaybackhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
#include <ao/ao.h>
#include "libswresample/swresample.h"
}
#include "imagehandler.h"
#include "handlers/imagehandler.h"

class AudioPlaybackHandler : public QObject
{
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions inputstreamhandler.cpp → handlers/inputstreamhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ void InputStreamHandler::removeStream(QString streamId)
index = i;
}
}

/* Kan dette slettes uten at mutex er locked?
Eg fikk en segmentation fault pga customRead
leste av en buffer som ikke eksisterte lengre
*/
if(index != -1)
{
delete mVideoHeaderVector.at(index);
Expand Down
4 changes: 2 additions & 2 deletions inputstreamhandler.h → handlers/inputstreamhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <QObject>
#include <mutex>
#include <vector>
#include <audioplaybackhandler.h>
#include <videoplaybackhandler.h>
#include <handlers/audioplaybackhandler.h>
#include <handlers/videoplaybackhandler.h>
#include <QHostAddress>


Expand Down
10 changes: 6 additions & 4 deletions handlers/sessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
SessionHandler::SessionHandler(Database* _db, UserHandler* _user,
ImageHandler* imageHandler,
Settings* settings, int bufferSize,
QHostAddress address, int port,
QHostAddress address, int _portNumberTCP,
int _portNumerUDP,
QObject *parent) : QObject(parent)
{
mDb = _db;
Expand All @@ -12,7 +13,8 @@ SessionHandler::SessionHandler(Database* _db, UserHandler* _user,
mIpAddress = "Ipaddress";
mSettings = settings;
mBufferSize = bufferSize;
mPort = port;
mPortNumberTCP = _portNumberTCP;
mPortNumberUDP = _portNumerUDP;
mAddress = address;
mImageHandler = imageHandler;
mSessionIsActive = false;
Expand Down Expand Up @@ -51,9 +53,9 @@ void SessionHandler::initOtherStuff()
QString displayName = mSettings->getDisplayName();
mSessionIsActive = true;
mInputStreamHandler = new InputStreamHandler(mImageHandler, mBufferSize, mAddress);
mSocketHandler = new SocketHandler(mBufferSize, mPort, mInputStreamHandler, streamId, roomId, mAddress);
mSocketHandler = new UdpSocketHandler(mBufferSize, mPortNumberUDP, mInputStreamHandler, streamId, roomId, mAddress);
//mTcpServerHandler = new TcpServerHandler(mInputStreamHandler, mPort);
mTcpSocketHandler = new TcpSocketHandler(mInputStreamHandler, streamId, roomId, displayName, mAddress, mPort);
mTcpSocketHandler = new TcpSocketHandler(mInputStreamHandler, streamId, roomId, displayName, mAddress, mPortNumberTCP);
mStreamHandler = new StreamHandler(mImageHandler, mSocketHandler, mBufferSize, mSettings, mTcpSocketHandler);
//Init tcpServerHandler
//mTcpServerHandler->init();
Expand Down
20 changes: 11 additions & 9 deletions handlers/sessionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>
#include "imagehandler.h"
#include "inputstreamhandler.h"
#include "sockethandler.h"
#include "tcpserverhandler.h"
#include "tcpsockethandler.h"
#include "streamhandler.h"
#include "handlers/imagehandler.h"
#include "handlers/inputstreamhandler.h"
#include "handlers/udpsockethandler.h"
#include "handlers/tcpserverhandler.h"
#include "handlers/tcpsockethandler.h"
#include "handlers/streamhandler.h"

class SessionHandler : public QObject
{
Expand All @@ -21,7 +21,8 @@ class SessionHandler : public QObject
explicit SessionHandler(Database* _db, UserHandler* _user,
ImageHandler* imageHandler,
Settings* settings, int bufferSize,
QHostAddress address, int port,
QHostAddress address, int _portNumberTCP,
int _portNumerUDP,
QObject *parent = nullptr);
Q_INVOKABLE void updateDisplayName();
Q_INVOKABLE void disableVideo();
Expand Down Expand Up @@ -50,7 +51,8 @@ class SessionHandler : public QObject
bool mUserHasRoom;
bool mSessionIsActive;
int mBufferSize;
int mPort;
int mPortNumberTCP;
int mPortNumberUDP;
QString mRoomId;
QString mRoomPassword;
QString mRoomHostUsername;
Expand All @@ -62,7 +64,7 @@ class SessionHandler : public QObject
StreamHandler* mStreamHandler;
ImageHandler* mImageHandler;
InputStreamHandler* mInputStreamHandler;
SocketHandler* mSocketHandler;
UdpSocketHandler* mSocketHandler;
TcpServerHandler* mTcpServerHandler;
TcpSocketHandler* mTcpSocketHandler;

Expand Down
2 changes: 1 addition & 1 deletion streamhandler.cpp → handlers/streamhandler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "streamhandler.h"


StreamHandler::StreamHandler(ImageHandler* _imageHandler, SocketHandler* _socketHandler, int bufferSize, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent) : QObject(parent)
StreamHandler::StreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, int bufferSize, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent) : QObject(parent)
{
mSettings = settings;
mBufferSize = bufferSize;
Expand Down
12 changes: 6 additions & 6 deletions streamhandler.h → handlers/streamhandler.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#ifndef STREAMHANDLER_H
#define STREAMHANDLER_H
#include "videohandler.h"
#include "audiohandler.h"
#include "sockethandler.h"
#include "handlers/videohandler.h"
#include "handlers/audiohandler.h"
#include "handlers/udpsockethandler.h"
#include <QObject>
#include "imagehandler.h"
#include "handlers/imagehandler.h"
#include "settings.h"
#include "handlers/errorhandler.h"

class StreamHandler : public QObject
{
Q_OBJECT
public:
StreamHandler(ImageHandler* _imageHandler, SocketHandler* _socketHandler, int buffer_size, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent = nullptr);
StreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, int buffer_size, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent = nullptr);
VideoHandler* mVideoHandler = nullptr;
AudioHandler* mAudioHandler = nullptr;
Q_INVOKABLE void disableAudio();
Expand All @@ -30,7 +30,7 @@ class StreamHandler : public QObject
private:
void grabVideoHeader();
int64_t mTime;
SocketHandler* mSocketHandler;
UdpSocketHandler* mSocketHandler;
TcpSocketHandler* mTcpSocketHandler;
ImageHandler* mImageHandler;
bool mAudioEnabled = true;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tcpserverhandler.h → handlers/tcpserverhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include "inputstreamhandler.h"
#include "handlers/inputstreamhandler.h"

//SKal lytte fra server, og opprette nye instanser av videoplaybackhandler og audioplaybackhandler basert på streamId og RoomId?
class TcpServerHandler : public QObject
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tcpsockethandler.h → handlers/tcpsockethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QHostAddress>
#include "videohandler.h"
#include "handlers/videohandler.h"
#include <vector>
#include "inputstreamhandler.h"
#include "handlers/inputstreamhandler.h"
#include <QString>
#include <QByteArray>

Expand Down
27 changes: 15 additions & 12 deletions sockethandler.cpp → handlers/udpsockethandler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sockethandler.h"
#include "udpsockethandler.h"

SocketHandler::SocketHandler(int bufferSize, int _port, InputStreamHandler* inputStreamHandler,
UdpSocketHandler::UdpSocketHandler(int bufferSize, int _port, InputStreamHandler* inputStreamHandler,
QString streamId, QString roomId, QHostAddress address,QObject *parent) : QObject(parent)
{
mBufferSize = bufferSize;
Expand All @@ -16,15 +16,15 @@ SocketHandler::SocketHandler(int bufferSize, int _port, InputStreamHandler* inpu
* When we used mAddress(ip address to the server) it did not recieve any datagrams.
* It also connects the readyRead signal to the readPendingDatagram function.
*/
void SocketHandler::initSocket()
void UdpSocketHandler::initSocket()
{
mUdpSocket = new QUdpSocket(this);
mUdpSocket->bind(QHostAddress::Any, mPort, QAbstractSocket::ShareAddress);
connect(mUdpSocket, &QUdpSocket::readyRead, this, &SocketHandler::readPendingDatagrams);
connect(mUdpSocket, &QUdpSocket::readyRead, this, &UdpSocketHandler::readPendingDatagrams);

}

void SocketHandler::closeSocket()
void UdpSocketHandler::closeSocket()
{
qDebug() << "Closing SocketHandler";
mUdpSocket->abort();
Expand All @@ -38,7 +38,7 @@ void SocketHandler::closeSocket()
* a single byte which will let us know if the datagram is audio or video.
* The function will use the streamId to send the datagram to the correct buffer/playbackhandler
*/
void SocketHandler::readPendingDatagrams()
void UdpSocketHandler::readPendingDatagrams()
{

while (mUdpSocket->hasPendingDatagrams())
Expand All @@ -49,7 +49,7 @@ void SocketHandler::readPendingDatagrams()


//To allow debugging on localhost. We added the operations the server performs on incoming datagrams before sending them to a client
if(mAddress.toIPv4Address() != QHostAddress("46.250.220.57").toIPv4Address() && mAddress.toIPv4Address() != QHostAddress("213.162.241.177").toIPv4Address())
/* if(mAddress.toIPv4Address() != QHostAddress("46.250.220.57").toIPv4Address() && mAddress.toIPv4Address() != QHostAddress("213.162.241.177").toIPv4Address())
{
//roomId is the first x bytes, then streamId
int roomIdLength = data[0];
Expand All @@ -59,7 +59,7 @@ void SocketHandler::readPendingDatagrams()
QByteArray roomIdArray = QByteArray(data, roomIdLength);
QString roomId(roomIdArray);
data.remove(0, roomIdLength);
}
}*/

int streamIdLength = data[0];
data.remove(0,1);
Expand All @@ -70,7 +70,10 @@ void SocketHandler::readPendingDatagrams()
data.remove(0, streamIdLength);

int index = findStreamIdIndex(streamId);
if(index < 0) continue;
if(index < 0)
{
continue;
}

//Checks the first byte in the datagram to determine if the datagram is audio or video
int audioOrVideoInt = data[0];
Expand Down Expand Up @@ -99,7 +102,7 @@ void SocketHandler::readPendingDatagrams()
QtConcurrent::run(mInputStreamHandler->mVideoPlaybackHandlerVector[index], &VideoPlaybackHandler::start);
mInputStreamHandler->mVideoPlaybackStartedVector[index] = true;
}
qDebug() << "video buffer size " << mInputStreamHandler->mVideoBufferVector[index]->size() << "after signal: " << signalCount;
// qDebug() << "video buffer size " << mInputStreamHandler->mVideoBufferVector[index]->size() << "after signal: " << signalCount;
}
else
{
Expand All @@ -117,7 +120,7 @@ void SocketHandler::readPendingDatagrams()
* @param streamId QString to find in mStreamIdVector
* @return int index where streamId was found
*/
int SocketHandler::findStreamIdIndex(QString streamId)
int UdpSocketHandler::findStreamIdIndex(QString streamId)
{
if(mInputStreamHandler->mStreamIdVector.size()>=1)
{
Expand All @@ -144,7 +147,7 @@ int SocketHandler::findStreamIdIndex(QString streamId)
* @param arr QByteArray to be sent
* @return Error code (0 if successful)
*/
int SocketHandler::sendDatagram(QByteArray arr)
int UdpSocketHandler::sendDatagram(QByteArray arr)
{
int ret = 0;
/*
Expand Down
10 changes: 5 additions & 5 deletions sockethandler.h → handlers/udpsockethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
#include <QNetworkDatagram>
#include <QProcess>
#include <mutex>
#include "imagehandler.h"
#include "videoplaybackhandler.h"
#include "audioplaybackhandler.h"
#include "handlers/imagehandler.h"
#include "handlers/videoplaybackhandler.h"
#include "handlers/audioplaybackhandler.h"

class VideoPlaybackHandler;
class AudioPlaybackHandler;
class InputStreamHandler;
class SocketHandler : public QObject
class UdpSocketHandler : public QObject
{
Q_OBJECT
public:
explicit SocketHandler(int bufferSize, int port, InputStreamHandler* inputStreamHandler, QString streamId, QString roomId, QHostAddress address, QObject *parent = nullptr);
explicit UdpSocketHandler(int bufferSize, int port, InputStreamHandler* inputStreamHandler, QString streamId, QString roomId, QHostAddress address, QObject *parent = nullptr);
void initSocket();
void closeSocket();
QTcpSocket* mTCPSocket;
Expand Down
2 changes: 1 addition & 1 deletion videohandler.cpp → handlers/videohandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


VideoHandler::VideoHandler(QString cDeviceName, std::mutex* _writeLock,int64_t _time,
ImageHandler* imageHandler, SocketHandler* _socketHandler,
ImageHandler* imageHandler, UdpSocketHandler* _socketHandler,
int bufferSize, TcpSocketHandler* tcpSocketHandler, QObject* parent): QObject(parent)
{
mBufferSize = bufferSize;
Expand Down
12 changes: 6 additions & 6 deletions videohandler.h → handlers/videohandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ extern "C" {
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"
}
#include "sockethandler.h"
#include "imagehandler.h"
class SocketHandler;
#include "handlers/udpsockethandler.h"
#include "handlers/imagehandler.h"
class UdpSocketHandler;
class TcpSocketHandler;

class VideoHandler : public QObject
{
Q_OBJECT
public:
VideoHandler(QString cDeviceName, std::mutex* _writeLock,int64_t time, ImageHandler* imageHandler, SocketHandler* _socketHandler, int bufferSize, TcpSocketHandler* tcpSocketHandler, QObject* parent = 0);
VideoHandler(QString cDeviceName, std::mutex* _writeLock,int64_t time, ImageHandler* imageHandler, UdpSocketHandler* _socketHandler, int bufferSize, TcpSocketHandler* tcpSocketHandler, QObject* parent = 0);
~VideoHandler();
int init();
void grabFrames();
Expand All @@ -45,7 +45,7 @@ class VideoHandler : public QObject
private:
//Trenger kanskje ikke denne likevel?
struct mSocketStruct {
SocketHandler* udpSocket;
UdpSocketHandler* udpSocket;
TcpSocketHandler* tcpSocket;
bool headerSent;
};
Expand All @@ -64,7 +64,7 @@ class VideoHandler : public QObject
AVCodec* inputVideoCodec;
AVCodec* outputVideoCodec;
int videoStream;
SocketHandler *socketHandler;
UdpSocketHandler *socketHandler;
struct SwsContext* img_convert_ctx;
ImageHandler* imageHandler;
bool mAbortGrabFrames = false;
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions videoplaybackhandler.h → handlers/videoplaybackhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#include <QObject>
#include <QByteArray>
#include <QtConcurrent/QtConcurrent>
#include "imagehandler.h"
#include <tcpsockethandler.h>
#include <tcpserverhandler.h>
#include "handlers/imagehandler.h"
#include <handlers/tcpsockethandler.h>

class VideoPlaybackHandler : public QObject
{
Expand Down
Loading

0 comments on commit ece0fa4

Please sign in to comment.