Skip to content

Commit

Permalink
fixed some numeric_limit and added playback class, a parent for both …
Browse files Browse the repository at this point in the history
…playbackhandlers
  • Loading branch information
taraldv committed Aug 1, 2020
1 parent ab4d47c commit 2f5e21c
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 543 deletions.
2 changes: 2 additions & 0 deletions components/Screen.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Rectangle {
height = _height;
}


//Brukes dette?
Image {
id: liveImage
anchors.fill: parent
Expand Down
422 changes: 163 additions & 259 deletions handlers/audioplaybackhandler.cpp

Large diffs are not rendered by default.

21 changes: 4 additions & 17 deletions handlers/audioplaybackhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,22 @@ extern "C" {
#include "libswresample/swresample.h"
}
#include "handlers/imagehandler.h"
#include "playback.h"

class AudioPlaybackHandler : public QObject
class AudioPlaybackHandler : public Playback
{
Q_OBJECT
public:
AudioPlaybackHandler(std::mutex* writeLock, QByteArray* buffer,
int bufferSize, QObject *parent = nullptr);
size_t bufferSize, QObject *parent = nullptr);
~AudioPlaybackHandler();
void getStream();
static int customReadPacket(void *opaque, uint8_t *buf, int buf_size);
void start();
int decodeAndPlay();
int mAudioStreamIndex = -1;
private:
int mVectorIndex;
struct mBufferAndLockStruct {
QByteArray* buffer;
std::mutex* writeLock;
};
int mBufferSize;
mBufferAndLockStruct* mStruct;
void initAudio(QObject *parent);
QByteArray mBuffer;
QAudioFormat mAudioFormat;
QAudioOutput* mpAudio;
QIODevice* mpOut;
uint8_t mSenderId = 0; //Value set to 0 just for testing.
ImageHandler* mImageHandler;
signals:

};

#endif // PLAYBACKHANDLER_H
26 changes: 14 additions & 12 deletions handlers/imagehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ ImageHandler::ImageHandler(Settings* settings) : QQuickImageProvider(QQuickImage

QImage ImageHandler::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
int index = 0;
uint8_t index = 0;
QStringList onlyId = id.split("=");
if(onlyId.size() >= 2)
{
QStringList idIndex = onlyId[1].split("&");
if(idIndex.size() >= 2)
{

index = idIndex[1].toInt();
if(index > 0)
{
//qDebug() << index;
}
index = idIndex[1].toUInt();
}
}
//This means the screen for the user, which is stored in numeric_value_max in the map
if(index==0)
{
index = std::numeric_limits<uint8_t>::max();
}
else
{
//All other participants are located in the map at their screen index-1
index--;
}
QImage result = mImageMap[index].first;

if(result.isNull())
Expand Down Expand Up @@ -101,10 +107,6 @@ void ImageHandler::updatePeerDisplayName(uint8_t index, QString displayName)
void ImageHandler::updateImage(const QImage &image, uint8_t index)
{
imgLock.lock();
if(index > 0)
{
//qDebug() << index;
}
if(mImageMap[index].first != image)
{
mImageMap[index].first = image;
Expand Down Expand Up @@ -169,7 +171,7 @@ void ImageHandler::readImage(AVCodecContext* codecContext, AVFrame* frame, uint8

if(codecContext == nullptr)
{
emit updateImage(generateGenericImage(mSettings->getDisplayName()), 0);
updateImage(generateGenericImage(mSettings->getDisplayName()), std::numeric_limits<uint8_t>::max());
return;
}

Expand Down Expand Up @@ -206,7 +208,7 @@ void ImageHandler::readImage(AVCodecContext* codecContext, AVFrame* frame, uint8
sws_freeContext(imgConvertCtx);

//av_frame_free(&frameRGB);
emit updateImage(img, index);
updateImage(img, index);
//av_frame_unref(frameRGB);
//av_frame_free(&frameRGB);
//delete frameRGB;
Expand Down
11 changes: 9 additions & 2 deletions handlers/inputstreamhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,18 @@ void InputStreamHandler::handleHeader(QByteArray data)
/**
* When recieving a TCP request with a header from a unknown streamId,
* we need to create new buffers, mutex and playbackhandlers for both video and audio.
* @param streamId QString to add to mStreamIdVector
* @param streamId QString to add to mStreamIdVector, used to identify packets and datagrams
* @param index int to add a peer to ImageHandler
* @param displayName QString to display on GUI
*/
void InputStreamHandler::addStreamToVector(int index, QString streamId, QString displayName)
{
qDebug() << "Adding streamId: " << streamId;
if(index >= std::numeric_limits<uint_8>::max())
{
qDebug() << "We currently do not allow for more than 255 participants in a room" << Q_FUNC_INFO;
return;
}
QByteArray* tempVideoHeaderBuffer = new QByteArray();
mVideoHeaderVector.push_back(tempVideoHeaderBuffer);
QByteArray* tempAudioBuffer = new QByteArray();
Expand All @@ -164,7 +170,8 @@ void InputStreamHandler::addStreamToVector(int index, QString streamId, QString
mAudioMutexVector.push_back(tempAudioLock);
mVideoMutexVector.push_back(tempVideoLock);
mAudioPlaybackHandlerVector.push_back(new AudioPlaybackHandler(tempAudioLock, tempAudioBuffer, mBufferSize));
mVideoPlaybackHandlerVector.push_back(new VideoPlaybackHandler(tempVideoLock, mImageHandler, tempVideoHeaderBuffer, tempVideoBuffer, mBufferSize, (index + 1)));
mVideoPlaybackHandlerVector.push_back(new VideoPlaybackHandler(tempVideoLock, tempVideoBuffer, mBufferSize,
mImageHandler, index));
mStreamIdVector.push_back(streamId);
mAudioPlaybackStartedVector.push_back(false);
mVideoPlaybackStartedVector.push_back(false);
Expand Down
10 changes: 6 additions & 4 deletions handlers/outputstreamhandler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "outputstreamhandler.h"


OutputStreamHandler::OutputStreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, int bufferSize, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent) : QObject(parent)
OutputStreamHandler::OutputStreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, size_t bufferSize, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent) : QObject(parent)
{
mSettings = settings;
mBufferSize = bufferSize;
Expand Down Expand Up @@ -91,7 +91,8 @@ void OutputStreamHandler::grabVideoHeader()
{
qDebug() << "Creating new VideoHandler";
mVideoHandler = new VideoHandler(mVideoDevice, &mUDPSendDatagramMutexLock,
mTime, mImageHandler, mSocketHandler,mBufferSize, mTcpSocketHandler);
mTime, mImageHandler, mSocketHandler,
mBufferSize, mTcpSocketHandler);
}
qDebug() << "Init videoHandler";
mVideoHandler->init();
Expand All @@ -112,7 +113,7 @@ int OutputStreamHandler::enableAudio()
qDebug() << "creating mAudioHandler";
//int64_t time = av_gettime();
mAudioHandler = new AudioHandler(mAudioDevice, &mUDPSendDatagramMutexLock,
mTime, mSocketHandler,mBufferSize);
mTime, mSocketHandler, mBufferSize);
}

int error = mAudioHandler->init();
Expand Down Expand Up @@ -150,7 +151,8 @@ int OutputStreamHandler::enableVideo()
qDebug() << "new videohandler";
//int64_t time = av_gettime();
mVideoHandler = new VideoHandler(mVideoDevice, &mUDPSendDatagramMutexLock,
mTime, mImageHandler, mSocketHandler, mBufferSize, mTcpSocketHandler);
mTime, mImageHandler, mSocketHandler,
mBufferSize, mTcpSocketHandler);
}

int error = mVideoHandler->init();
Expand Down
4 changes: 2 additions & 2 deletions handlers/outputstreamhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OutputStreamHandler : public QObject
{
Q_OBJECT
public:
OutputStreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, int buffer_size, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent = nullptr);
OutputStreamHandler(ImageHandler* _imageHandler, UdpSocketHandler* _socketHandler, size_t buffer_size, Settings* settings, TcpSocketHandler* tcpSocketHandler, QObject *parent = nullptr);
VideoHandler* mVideoHandler = nullptr;
AudioHandler* mAudioHandler = nullptr;
Q_INVOKABLE void disableAudio();
Expand All @@ -35,7 +35,7 @@ class OutputStreamHandler : public QObject
ImageHandler* mImageHandler;
bool mAudioEnabled = true;
bool mVideoEnabled = true;
int mBufferSize;
size_t mBufferSize;
std::mutex mUDPSendDatagramMutexLock;
QString mAudioDevice;
QString mVideoDevice;
Expand Down
2 changes: 1 addition & 1 deletion handlers/sessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool SessionHandler::joinSession(QString _roomId, QString _roomPassword)
qDebug() << "Adding peer with display name" << mSettings->getDisplayName();

//TODO maybe more intensive to find numeric_limit in map compared to 0?
uint_8 userIndex = std::numeric_limits<uint_8>::max();
uint8_t userIndex = std::numeric_limits<uint8_t>::max();

mImageHandler->addPeer(userIndex, mSettings->getDisplayName());

Expand Down
4 changes: 2 additions & 2 deletions handlers/videohandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void VideoHandler::grabFrames()
scaledFrame->pts = pts;
pts += ifmt_ctx->streams[0]->time_base.den/ifmt_ctx->streams[0]->r_frame_rate.num;
}
imageHandler->readImage(outputVideoCodecContext, scaledFrame, 0);
imageHandler->readImage(outputVideoCodecContext, scaledFrame, std::numeric_limits<uint8_t>::max());
ret = avcodec_send_frame(outputVideoCodecContext, scaledFrame);
if(ret < 0)
{
Expand All @@ -318,7 +318,7 @@ void VideoHandler::grabFrames()
videoFrame->pts = pts;
pts += ifmt_ctx->streams[0]->time_base.den/ifmt_ctx->streams[0]->r_frame_rate.num;
}
imageHandler->readImage(outputVideoCodecContext, videoFrame, 0);
imageHandler->readImage(outputVideoCodecContext, videoFrame, std::numeric_limits<uint8_t>::max());
ret = avcodec_send_frame(outputVideoCodecContext, videoFrame);
//av_frame_free(&videoFrame);
if(ret < 0)
Expand Down
Loading

0 comments on commit 2f5e21c

Please sign in to comment.