Commit 0185410d authored by jcaceres's avatar jcaceres
Browse files

add qt3 branch

parent 8a5575f0
......@@ -71,8 +71,7 @@ Stream::addOutput (OutputPlugin * newout)
{ // We are going to need output synchronization locking.
// add a lock for the first output (didn't need it when it was the only one.)
tmp = new QSemaphore (10);
//(*tmp)++;
(*tmp).acquire();//****JPC qt4 porting******
(*tmp)++;
APPEND (locks, tmp);
}
......@@ -85,8 +84,7 @@ Stream::addOutput (OutputPlugin * newout)
}
tmp = new QSemaphore (10);
//(*tmp)++;
(*tmp).acquire();//****JPC qt4 porting******
(*tmp)++;
APPEND (locks, tmp);
}
......@@ -124,8 +122,7 @@ Stream::read (void *buf, int key)
#if (debugMe)
cerr << key << "->POST ";
#endif
//(*sem)--;
(*sem).release();//****JPC qt4 porting******
(*sem)--;
}
}
}
......@@ -138,8 +135,7 @@ Stream::read (void *buf, int key)
#if (debugMe)
cerr << key << "->WAIT ";
#endif
//(*sem)++;
(*sem).acquire();//****JPC qt4 porting******
(*sem)++;
}
}
......
......@@ -8,7 +8,7 @@
#include "audioInfo.h"
#include "networkInfo.h"
#include "qthread.h"
#include "q3ptrvector.h"
#include "qptrvector.h"
/**
* @brief Connects an InputPlugin to an OutputPlugin with
......@@ -25,15 +25,15 @@ class Stream
protected:
// Vector inputs;
Q3PtrVector < InputPlugin > ins;
QPtrVector < InputPlugin > ins;
// Vector outputs;
Q3PtrVector < OutputPlugin > outs;
QPtrVector < OutputPlugin > outs;
// Vector processes;
Q3PtrVector < ProcessPlugin > procs;
QPtrVector < ProcessPlugin > procs;
// Vector circularBuffers;
Q3PtrVector < CircularBuffer > bufs;
QPtrVector < CircularBuffer > bufs;
// Vector outputLocks;
Q3PtrVector < QSemaphore >locks;
QPtrVector < QSemaphore >locks;
int insCount;
int procsCount;
int outsCount;
......
#include "udp_input.h"
#include "networkInfo.h"
#include "stream.h"
#include "unistd.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
extern QString *IPv4Addr (char *namebuf);
extern int set_fifo_priority (bool half);
UDPInput::UDPInput (NetworkInfoT netInfo, AudioInfoT audInfo):
InputPlugin ("UDP Input"), netInfo (netInfo), audInfo (audInfo)
{
bpp = netInfo->getDataBytesPerPacket ();
cout << "bpp = " << bpp << endl;
char localhostbuf[100];
has_peer = false;
_rcvr = NULL;
sock = new QSocketDevice (QSocketDevice::Datagram); // for an unreliable UDP socket
sock->setAddressReusable(true);
if (gethostname (localhostbuf, 99))
{
perror ("gethostname");
exit ();
}
cout << "Rx buff = " << sock->receiveBufferSize () << endl;
QHostAddress *ha = new QHostAddress ();
QString *s = IPv4Addr (localhostbuf); // dotted integer from name
ha->setAddress (*s);
if (!(sock->bind (*ha, netInfo->getInPort ())))
{
perror ("bind\n");
exit ();
}
if (!sock->isValid ())
{
cout << "socket creation error " << endl;
}
packetIndex = 0;
wholeSize = sizeof (nsHeader) + (netInfo->getChunksPerPacket () * bpp) + 1;
packetData = new char[wholeSize];
memset (packetData, 0, wholeSize);
numRedundantBuffers = netInfo->getChunksPerPacket() - 1;
maxPacketIndex = netInfo->getMaxSeq();
cout << endl << "UDPInput binding to " << localhostbuf
<< " port " << netInfo->getInPort () << endl;
}
/** cout that the UDPInput thread has started */
void
UDPInput::Initial ()
{
cout << "Started UDPInput thread" << endl;
}
/**
* Receive a buffer from the UDPSocket.
* @param buf is the location at which to store the buffer.
* @param le is the size of the buffer to be received.
*/
int
UDPInput::rcv (char *buf)
{
int rv = sock->readBlock (packetData, wholeSize);
char *datapart;
packetIndex = ((nsHeader *) packetData)->i_seq;
datapart = packetData + sizeof (nsHeader) +
((packetIndex % ((nsHeader *) packetData)->i_copies) * bpp);
memcpy (buf, datapart, bpp);
/*
((nsHeader *) packetData)->i_type = 0;
((nsHeader *) packetData)->i_nframes = 1;
((nsHeader *) packetData)->i_nchans = 2;
((nsHeader *) packetData)->i_copies = n;
((nsHeader *) packetData)->i_cksum = 4;
((nsHeader *) packetData)->i_seq = packetIndex;
((nsHeader *) packetData)->i_rtnseq = 6;
((nsHeader *) packetData)->i_rtt = 7;
*/
if (rv < 0)
{
cerr << "bad read..." << endl;
}
return packetIndex;
}
int
UDPInput::rcvz1 (char *bufz1, int z)
{
char *datapart;
packetIndex = ((nsHeader *) packetData)->i_seq-z;
if (packetIndex < 0)
{
packetIndex += maxPacketIndex;
// cout << "backed below 0 (a good thing)" << endl;
}
datapart = packetData + sizeof (nsHeader) +
((packetIndex % ((nsHeader *) packetData)->i_copies) * bpp);
memcpy (bufz1, datapart, bpp);
return packetIndex;
}
/**
* The main run loop. Calls waitForConnection(). Once connected,
* while running is true, it checks if the socket has a new buffer.
* If it does, it writes the buffer to the stream.
*/
void
UDPInput::stop ()
{
_running = false;
}
void
UDPInput::run ()
{
_running = true;
int seq;
char *buf = (char *) new char[bpp];
char *bufz1 = (char *) new char[bpp];
cout << "UDP Input waiting for peer." << endl;
while (has_peer == false)
{
if (sock->bytesAvailable () >= wholeSize) // not an error
{
cout <<"wholeSize = " << wholeSize << " " <<sock->bytesAvailable ()<< endl;
//sock->readBlock (buf, wholeSize);
this->rcv (buf);
has_peer = true; // really rcvd something
cout << "UDP Input Connected!" << endl;
} else
msleep (100);
}
msleep (10);
cout << "Started UDP Input Run" << endl;
set_fifo_priority (false);
bool timeout;
int ret;
unsigned long now = 0;
unsigned long lastTickTime = usecTime ();
int ctr = 0;
double max = 0.0;
int gap;
double gapAvg = 0.0;
while (_running)
{
// If timeout is non-null and no error occurred
// (i.e. it does not return -1): this function sets *timeout to TRUE,
// if the reason for returning was that the timeout was reached;
// otherwise it sets *timeout to FALSE. This is useful to find out
// if the peer closed the connection.
ret = (sock->waitForMore (30, &timeout));
if (ret == -1)
cerr << "udp in sock problems..." << endl;
else if (timeout)
cerr << "udp in waited too long..." << endl;
else
{
seq = this->rcv (buf);
if (stream == NULL)
{
cerr << "ERROR: UDPInput has no sream to write to!" << endl;
}
int z = numRedundantBuffers;
while (z)
{
int zseq = this->rcvz1 (bufz1, z);
stream->writeRedundant (bufz1, key, z, zseq);
z--;
}
gap = stream->writeRedundant (buf, key, 0, seq);
// cout << "writePosition " << gap <<"\t\t";
/*
now = usecTime ();
ctr++;
if (ctr == 40)
{
ctr = 0;
gapAvg = gapAvg / 40.0;
// plotVal (gapAvg);
// plotVal (max);
gapAvg = 0.0;
max = 0.0;
} else {
double xxx = (((double)now - (double)lastTickTime)/1000000.0);
// if (xxx>max) max = xxx;
max += xxx;
gapAvg += ((double)gap);
}
lastTickTime = now;
*/
// stream->write (buf, key);
}
}
cout << "UDP Input stop" << endl;
}
void UDPInput::plotVal (double v)
{
if(_rcvr!=NULL)
{
ThreadCommEvent *e = new ThreadCommEvent (-1.0,
v,
0.0);
QApplication::postEvent (_rcvr, e); // to app event loop
}
}
bool UDPInput::hasPeer ()
{
return has_peer;
}
/** Returns the address of the peer to which the socket is connected. */
QHostAddress UDPInput::peer ()
{
return sock->peerAddress ();
}
#ifndef _UDP_INPUT_H
#define _UDP_INPUT_H
#include "input_plugin.h"
#include "udp.h"
#include "networkInfo.h"
#include "audioInfo.h"
#include <qsocketdevice.h>
#include <qobject.h>
/**
* @brief Sends buffers received from a UDP input stream into
* a Stream.
*/
class UDPInput:public InputPlugin
{
private:
NetworkInfoT netInfo;
AudioInfoT audInfo;
QSocketDevice *sock;
bool _running;
bool has_peer;
int packetIndex; //used for netdebug, checking order of incoming packets
int maxPacketIndex;
char *packetData;
void setPacketSize (int size);
public:
UDPInput (NetworkInfoT netInfo, AudioInfoT audInfo);
int rcvz1 (char *bufz1, int z);
int rcv (char *buf);
bool hasPeer ();
QHostAddress peer ();
void Initial ();
void run ();
void stop ();
int bpp;
int wholeSize;
int numRedundantBuffers;
void plotVal (double v);
};
#endif
......@@ -14,23 +14,17 @@ audInfo (audInfo)
{
bpp = netInfo->getDataBytesPerPacket ();
char localhostbuf[100];
//sock = new Q3SocketDevice (Q3SocketDevice::Datagram);//***JPC Port to qt4*****************
//sock->setAddressReusable(true);//***JPC Port to qt4*****************
sock = new QUdpSocket;
sock = new QSocketDevice (QSocketDevice::Datagram);
sock->setAddressReusable(true);
if (gethostname (localhostbuf, 99))
{
perror ("gethostname");
exit ();
}
QHostAddress *ha = new QHostAddress ();
QString *s = IPv4Addr (localhostbuf);
//cout << "AAAAAAAAA" << (*s).latin1() << endl;
QString *s = IPv4Addr (localhostbuf); // dotted integer from name
ha->setAddress (*s);
cout << "AAAAAAAAA" << (*ha).toString ().latin1() << endl;
//if (!(sock->bind (*ha, netInfo->getOutPort ())))//***JPC Port to qt4*****************
if (!(sock->bind (*ha, netInfo->getOutPort (), QUdpSocket::ShareAddress ) ) )//***JPC Port to qt4*****************
if (!(sock->bind (*ha, netInfo->getOutPort ())))
{
perror ("bind\n");
exit ();
......@@ -59,13 +53,6 @@ audInfo (audInfo)
}
UDPOutput::~UDPOutput()
{
delete sock;
}
void
UDPOutput::Initial ()
{
......@@ -81,13 +68,9 @@ UDPOutput::connect (QHostAddress remote)
exit ();
}
// sets peerAddress
//sock->connect (remote, netInfo->getInPort ()); //***JPC Port to qt4*****************
sock->connectToHost (remote, netInfo->getInPort ()); //***JPC Port to qt4*****************
//**************JPC COMENTED OUT*******************
cout << "Connecting to " << remote.toString().latin1() << ":" << netInfo->
sock->connect (remote, netInfo->getInPort ());
cout << "Connecting to " << remote.toString () << ":" << netInfo->
getInPort () << endl;
//*************************************************
return 0;
}
......@@ -107,13 +90,9 @@ UDPOutput::send (char *buf)
memcpy (datapart, buf, bpp);
//int rv = sock->writeBlock (packetData, wholeSize,//***JPC Port to qt4*****************
// sock->peerAddress (),//***JPC Port to qt4*****************
// sock->peerPort ());//***JPC Port to qt4*****************
int rv = sock->writeDatagram (packetData, wholeSize,//***JPC Port to qt4*****************
sock->peerAddress (),//***JPC Port to qt4*****************
sock->peerPort ());//***JPC Port to qt4*****************
//cout << "WRITING!!!!!!!!!!! " << rv <<" "<< QString(sock->peerName ()).latin1() << " " << sock->peerPort ()<< endl;
int rv = sock->writeBlock (packetData, wholeSize,
sock->peerAddress (),
sock->peerPort ());
return rv;
}
......@@ -147,10 +126,8 @@ UDPOutput::run ()
if (res < 0)
{
perror ("Send");
//**************JPC COMENTED OUT*******************
//cout << "error sending to " << sock->peerAddress ().
// toString () << endl;
//*************************************************
cout << "error sending to " << sock->peerAddress ().
toString () << endl;
return;
}
/*
......@@ -179,15 +156,13 @@ UDPOutput::stop ()
cout << "UDP Output SHOULD stop" << endl;
}
//***JPC Port to qt4*****************
void UDPOutput::plotVal (double v)
{
/*
if(_rcvr!=NULL)
{
ThreadCommEvent *e = new ThreadCommEvent (v, -1.0, 0.0);
ThreadCommEvent *e = new ThreadCommEvent (v,
-1.0,
0.0);
QApplication::postEvent (_rcvr, e); // to app event loop
}
*/
}
#ifndef _UDP_OUTPUT_H
#define _UDP_OUTPUT_H
//#include <q3socketdevice.h>//***JPC Port to qt4*****************
#include <QUdpSocket> //***JPC Port to qt4*****************
#include <qsocketdevice.h>
#include "networkInfo.h"
#include "audioInfo.h"
......@@ -19,23 +18,20 @@
class UDPOutput:public OutputPlugin // , public Semaphore
{
private:
private:
int one_time_flags;
//Q3SocketDevice *sock;//***JPC Port to qt4*****************
QUdpSocket *sock;//***JPC Port to qt4*****************
QSocketDevice *sock;
bool _running;
int packetIndex; // only used by netdebug mode
int maxPacketIndex;
char *packetData; // only used by netdebug mode
int numBuffers;
int numBuffers;
NetworkInfoT netInfo;
AudioInfoT audInfo;
public:
public:
UDPOutput (NetworkInfoT netInfo, AudioInfoT audInfo);
virtual ~UDPOutput();
int connect (QHostAddress remote);
int send (char *buf);
void Initial ();
......
<!DOCTYPE UI><UI version="3.1" stdsetdef="1">
<class>MainWindow</class>
<widget class="QMainWindow">
<property name="name">
<cstring>MainWindow</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>619</width>
<height>531</height>
</rect>
</property>
<property name="caption">
<string>Clapper Experiment</string>
</property>
<property name="focusPolicy">
<enum>StrongFocus</enum>
</property>
<widget class="QFrame">
<property name="name">
<cstring>frame4</cstring>
</property>
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>410</width>
<height>120</height>
</rect>
</property>
<property name="frameShape">
<enum>StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>Raised</enum>
</property>
<widget class="QGroupBox">
<property name="name">
<cstring>groupBox2_2</cstring>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>190</width>
<height>60</height>
</rect>
</property>
<property name="title">
<string>Directory for files</string>
</property>
<widget class="QLineEdit">
<property name="name">
<cstring>dirLineEdit</cstring>
</property>
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>170</width>
<height>23</height>
</rect>
</property>
<property name="focusPolicy">
<enum>ClickFocus</enum>
</property>
<property name="text">
<string>/zap/trials</string>
</property>
</widget>
</widget>
<widget class="QLabel">
<property name="name">
<cstring>fullpathTextLabel</cstring>
</property>
<property name="geometry">
<rect>
<x>140</x>
<y>90</y>
<width>250</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>.wav</string>
</property>
</widget>
<widget class="QLabel">
<property name="name">
<cstring>textLabel1</cstring>
</property>
<property name="geometry">
<rect>
<x>220</x>
<y>10</y>
<width>160</width>
<height>94</height>
</rect>
</property>
<property name="text">
<string>Start jack first with
qjackctl &amp;
and make the buffersize
and sample rate match
</string>
</property>
</widget>
</widget>
<widget class="QGroupBox">
<property name="name">
<cstring>groupBox2</cstring>
</property>
<property name="geometry">
<rect>
<x>30</x>
<y>140</y>
<width>210</width>
<height>330</height>
</rect>
</property>
<property name="title">
<string>settings</string>
</property>
<widget class="QLabel">
<property name="name">
<cstring>sampleRateTextLabel</cstring>
</property>
<property name="geometry">
<rect>
<x>20</x>
<y>21</y>
<width>160</width>
<height>20</height>
</rect>
</property>