Commit c6f306c2 authored by jcacerec's avatar jcacerec
Browse files

Header added and need to fix threads priorities in os x (lib header added to globals.cpp)

parent 29bd7bc6
......@@ -39,6 +39,7 @@
#include "globals.h"
#include "JackAudioInterface.h"
#include "PacketHeader.h"
#include "JackTrip.h"
#include <iostream>
#include <cstdlib>
......
......@@ -118,7 +118,7 @@ void JackAudioInterface::setupClient()
mOutputPacket = new int8_t[size_output];
// Buffer size member
mNumFrames = getBufferSize();
mNumFrames = getBufferSizeInSamples();
// Initialize Buffer array to read and write audio
mInBuffer.resize(mNumInChans);
......@@ -128,7 +128,7 @@ void JackAudioInterface::setupClient()
mInProcessBuffer.resize(mNumInChans);
mOutProcessBuffer.resize(mNumOutChans);
int nframes = getBufferSize();
int nframes = getBufferSizeInSamples();
for (int i = 0; i < mNumInChans; i++) {
mInProcessBuffer[i] = new sample_t[nframes];
// set memory to 0
......@@ -243,7 +243,7 @@ int JackAudioInterface::getSampleRateFromType(samplingRateT rate_type)
}
//*******************************************************************************
uint32_t JackAudioInterface::getBufferSize() const
uint32_t JackAudioInterface::getBufferSizeInSamples() const
{
return jack_get_buffer_size(mClient);
}
......@@ -273,7 +273,7 @@ int JackAudioInterface::getNumOutputChannels() const
//*******************************************************************************
size_t JackAudioInterface::getSizeInBytesPerChannel() const
{
return (getBufferSize() * getAudioBitResolution()/8);
return (getBufferSizeInSamples() * getAudioBitResolution()/8);
}
//*******************************************************************************
......
......@@ -111,8 +111,15 @@ public:
/** \brief Get the Jack Server Buffer Size, in samples
*/
uint32_t getBufferSize() const;
uint32_t getBufferSizeInSamples() const;
/** \brief Get the Jack Server Buffer Size, in bytes
*/
uint32_t getBufferSizeInBytes() const
{
return (getBufferSizeInSamples()*sizeof(sample_t));
};
/** \brief Get the Audio Bit Resolution, in bits
*
* This is one of the audioBitResolutionT set in construction
......
......@@ -68,6 +68,9 @@ JackTrip::JackTrip(jacktripModeT JacktripMode,
mJackAudio(NULL)
{
setupJackAudio();
/// \todo CHECK THIS AND PUT IT IN A BETTER PLACE, also, get header type from options
createHeader(DataProtocol::DEFAULT);
}
......@@ -88,7 +91,7 @@ void JackTrip::setupJackAudio()
mSampleRate = mJackAudio->getSampleRate();
std::cout << "The Sampling Rate is: " << mSampleRate << std::endl;
std::cout << gPrintSeparator << std::endl;
mAudioBufferSize = mJackAudio->getBufferSize();
mAudioBufferSize = mJackAudio->getBufferSizeInSamples();
int AudioBufferSizeInBytes = mAudioBufferSize*sizeof(sample_t);
std::cout << "The Audio Buffer Size is: " << mAudioBufferSize << " samples" << std::endl;
std::cout << " or: " << AudioBufferSizeInBytes
......@@ -262,8 +265,7 @@ void JackTrip::serverStart()
}
//*******************************************************************************
void JackTrip::createHeader(const DataProtocol::packetHeaderTypeT headertype)
{
switch (headertype) {
......@@ -282,8 +284,30 @@ void JackTrip::createHeader(const DataProtocol::packetHeaderTypeT headertype)
}
void JackTrip::putHeaderInPacket(int8_t* full_packet)
//*******************************************************************************
void JackTrip::putHeaderInPacket(int8_t* full_packet, int8_t* audio_packet)
{
mPacketHeader->fillHeaderCommonFromJack(*mJackAudio);
mPacketHeader->putHeaderInPacket(full_packet);
int8_t* audio_part;
audio_part = full_packet + mPacketHeader->getHeaderSizeInBytes();
std::memcpy(audio_part, audio_packet, mJackAudio->getBufferSizeInBytes());
}
//*******************************************************************************
int JackTrip::getPacketSizeInBytes() const
{
return (mJackAudio->getBufferSizeInBytes() + mPacketHeader->getHeaderSizeInBytes());
}
//*******************************************************************************
void JackTrip::parseAudioPacket(int8_t* full_packet, int8_t* audio_packet)
{
int8_t* audio_part;
audio_part = full_packet + mPacketHeader->getHeaderSizeInBytes();
std::memcpy(audio_packet, audio_part, mJackAudio->getBufferSizeInBytes());
}
......@@ -130,6 +130,17 @@ public:
//@}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
/// \name Mediator Functions
//@{
/// \todo Document all these functions
void createHeader(const DataProtocol::packetHeaderTypeT headertype);
void putHeaderInPacket(int8_t* full_packet, int8_t* audio_packet);
int getPacketSizeInBytes() const;
void parseAudioPacket(int8_t* full_packet, int8_t* audio_packet);
//@}
//------------------------------------------------------------------------------------
private:
......@@ -145,10 +156,6 @@ private:
void serverStart();
void createHeader(const DataProtocol::packetHeaderTypeT headertype);
void putHeaderInPacket(int8_t* full_packet);
jacktripModeT mJackTripMode; ///< JackTrip::jacktripModeT
dataProtocolT mDataProtocol; ///< Data Protocol
int mNumChans; ///< Number of Channels (inputs = outputs)
......
......@@ -77,7 +77,7 @@ DefaultHeader::DefaultHeader()
//***********************************************************************
void DefaultHeader::fillHeaderCommonFromJack(const JackAudioInterface& JackAudio)
{
mHeader.BufferSize = JackAudio.getBufferSize();
mHeader.BufferSize = JackAudio.getBufferSizeInSamples();
mHeader.SamplingRate = JackAudio.getSampleRateType ();
mHeader.NumInChannels = JackAudio.getNumInputChannels();
mHeader.NumOutChannels = JackAudio.getNumOutputChannels();
......
......@@ -46,19 +46,7 @@
class JackAudioInterface; // Forward Declaration
/** \brief Template struct for Headers
*
* To create the header, just type HeaderStruct<HeaderType>
*/
template <typename T>
struct HeaderStruct
{
T data;
};
//---------JAMLINK HEADER DRAFT----------------------------
struct JamLinkHeaderStuct
{
// watch out for alignment -- need to be on 4 byte chunks
......@@ -66,14 +54,7 @@ struct JamLinkHeaderStuct
unsigned short seqnum;
unsigned int timeStamp;
};
/*
union HeaderUnion
{
DefaultHeaderStruct dh;
JamLinkHeaderStuct jl;
};
*/
//---------------------------------------------------------
//#######################################################################
......@@ -93,14 +74,13 @@ public:
*/
static uint64_t usecTime();
/// \todo Implement this using a JackTrip Method (Mediator) member instead of the
/// reference to JackAudio
virtual void fillHeaderCommonFromJack(const JackAudioInterface& JackAudio) = 0;
virtual void addHeaderToPacket(const int8_t* const audio_packet,
int8_t* full_packet) const {};
virtual void getAudioPacket(int8_t* audio_packet) const {};
virtual void parseHeader() = 0;
virtual void increaseSequenceNumber() = 0;
virtual int getHeaderSize() const = 0;
virtual int getHeaderSizeInBytes() const = 0;
virtual void putHeaderInPacket(int8_t* full_packet) = 0;
};
......@@ -137,14 +117,16 @@ public:
mHeader.SeqNumber++;
std::cout << "Sequence Number = " << static_cast<int>(mHeader.SeqNumber) << std::endl;
};
virtual int getHeaderSize() const { return sizeof(mHeader); };
virtual int getHeaderSizeInBytes() const { return sizeof(mHeader); };
virtual void putHeaderInPacket(int8_t* full_packet)
{
std::memcpy(full_packet, reinterpret_cast<const void*>(&mHeader),
getHeaderSizeInBytes() );
};
void printHeader() const;
private:
DefaultHeaderStruct mHeader;
DefaultHeaderStruct mHeader; ///< Header Struct
};
......@@ -158,6 +140,7 @@ private:
/*
class JamLinkHeader : public PacketHeader
{
public:
......
......@@ -37,6 +37,8 @@
#include "UdpDataProtocol.h"
#include "globals.h"
#include "JackTrip.h"
#include <cstring>
#include <iostream>
......@@ -143,13 +145,14 @@ void UdpDataProtocol::run()
{
//std::cout << "Running DataProtocol Thread in UDP Mode" << std::endl;
//std::cout << gPrintSeparator << std::endl;
size_t packet_size = getAudioPacketSize();
int8_t audio_packet[packet_size];
//int8_t full_packet[packet_size];
size_t audio_packet_size = getAudioPacketSize();
audio_packet = new int8_t[audio_packet_size];
int full_packet_size = mJackTrip->getPacketSizeInBytes();
cout << "full_packet_size: " << full_packet_size << endl;
full_packet = new int8_t[full_packet_size];
bool timeout = false;
//mHeader->fillHeaderCommonFromJack(const JackAudioInterface& JackAudio);
//mJackTrip->putHeaderInPacket(full_packet);
mJackTrip->putHeaderInPacket(full_packet, audio_packet);
#if defined ( __LINUX__ )
set_fifo_priority (false);
......@@ -165,7 +168,9 @@ void UdpDataProtocol::run()
/// the local ones. Extract this information from the header
std::cout << "Waiting for Peer..." << std::endl;
// This blocks waiting for the first packet
receivePacket( reinterpret_cast<char*>(audio_packet), packet_size);
//receivePacket( reinterpret_cast<char*>(audio_packet), audio_packet_size);
receivePacket( reinterpret_cast<char*>(full_packet), full_packet_size);
mJackTrip->parseAudioPacket(full_packet, audio_packet);
std::cout << "Received Connection for Peer!" << std::endl;
while ( !mStopped )
......@@ -177,7 +182,9 @@ void UdpDataProtocol::run()
}
else {
// This is blocking until we get a packet...
receivePacket( reinterpret_cast<char*>(audio_packet), packet_size);
//receivePacket( reinterpret_cast<char*>(audio_packet), audio_packet_size);
receivePacket( reinterpret_cast<char*>(full_packet), full_packet_size);
mJackTrip->parseAudioPacket(full_packet, audio_packet);
// ...so we want to send the packet to the buffer as soon as we get in from
// the socket, i.e., non-blocking
mRingBuffer->insertSlotNonBlocking(audio_packet);
......@@ -192,8 +199,10 @@ void UdpDataProtocol::run()
{
// We block until there's stuff available to read
mRingBuffer->readSlotBlocking(audio_packet);
mJackTrip->putHeaderInPacket(full_packet, audio_packet);
// This will send the packet immediately
sendPacket( reinterpret_cast<char*>(audio_packet), packet_size);
//sendPacket( reinterpret_cast<char*>(audio_packet), audio_packet_size);
sendPacket( reinterpret_cast<char*>(full_packet), full_packet_size);
}
break;
}
......
......@@ -111,6 +111,12 @@ private:
QUdpSocket mUdpSocket; ///< The UDP socket
QHostAddress mPeerAddress; ///< The Peer Address
/// \todo change this names and location
int8_t* audio_packet;
int8_t* full_packet;
};
#endif
......@@ -40,6 +40,9 @@
#include <sched.h>
#if defined ( __MAC_OSX__ )
#include <mach/thread_policy.h>
#endif
#if defined ( __LINUX__ )
//*******************************************************************************
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment