annotate core/NetworkSend.cpp @ 217:c42a6b4dc2d4 mergingClockSync

Recovered some files from ClockSync
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 13 Feb 2016 04:09:12 +0000
parents
children 0183185cab1b
rev   line source
giuliomoro@217 1 #include "NetworkSend.h"
giuliomoro@217 2
giuliomoro@217 3 #ifdef USE_JUCE
giuliomoro@217 4 #else
giuliomoro@217 5 //initialize the static members of NetworkSend
giuliomoro@217 6 bool NetworkSend::staticConstructed=false;
giuliomoro@217 7 int NetworkSend::sleepTimeMs;
giuliomoro@217 8 bool NetworkSend::threadIsExiting;
giuliomoro@217 9 bool NetworkSend::threadRunning;
giuliomoro@217 10 std::vector<NetworkSend*> NetworkSend::objAddrs(0);
giuliomoro@217 11 AuxiliaryTask NetworkSend::sendDataTask=NULL;
giuliomoro@217 12
giuliomoro@217 13 void sendData(){
giuliomoro@217 14 NetworkSend::run();
giuliomoro@217 15 }
giuliomoro@217 16 void NetworkSend::staticConstructor(){
giuliomoro@217 17 if(staticConstructed==true)
giuliomoro@217 18 return;
giuliomoro@217 19 staticConstructed=true;
giuliomoro@217 20 threadIsExiting=false;
giuliomoro@217 21 threadRunning=false;
giuliomoro@217 22 sendDataTask = BeagleRT_createAuxiliaryTask(::sendData, 95, "sendDataTask"); //TODO: allow variable priority
giuliomoro@217 23 }
giuliomoro@217 24 void NetworkSend::sendAllData(){
giuliomoro@217 25 for(unsigned int n=0; n<NetworkSend::objAddrs.size(); n++){
giuliomoro@217 26 NetworkSend::objAddrs[n]->sendData();
giuliomoro@217 27 }
giuliomoro@217 28 }
giuliomoro@217 29 int NetworkSend::getNumInstances(){
giuliomoro@217 30 return objAddrs.size();
giuliomoro@217 31 }
giuliomoro@217 32 void NetworkSend::startThread(){
giuliomoro@217 33 BeagleRT_scheduleAuxiliaryTask(sendDataTask);
giuliomoro@217 34 }
giuliomoro@217 35 void NetworkSend::stopThread(){
giuliomoro@217 36 threadIsExiting=true;
giuliomoro@217 37 }
giuliomoro@217 38 bool NetworkSend::threadShouldExit(){
giuliomoro@217 39 return(gShouldStop || threadIsExiting);
giuliomoro@217 40 }
giuliomoro@217 41 bool NetworkSend::isThreadRunning(){
giuliomoro@217 42 return threadRunning;
giuliomoro@217 43 }
giuliomoro@217 44 #endif /* USE_JUCE */
giuliomoro@217 45
giuliomoro@217 46 #ifdef USE_JUCE
giuliomoro@217 47 NetworkSend::NetworkSend(const String &threadName):
giuliomoro@217 48 Thread(threadName)
giuliomoro@217 49 #else
giuliomoro@217 50 NetworkSend::NetworkSend()
giuliomoro@217 51 #endif /* USE_JUCE */
giuliomoro@217 52 {
giuliomoro@217 53 channel.buffers=NULL;
giuliomoro@217 54 channel.doneOnTime=NULL;
giuliomoro@217 55 channel.readyToBeSent=NULL;
giuliomoro@217 56 channel.enabled=false;
giuliomoro@217 57 sleepTimeMs=2; //This should actually be initialized in the staticConstructor for non-Juce code, but doing it here makes it more portable
giuliomoro@217 58 channel.sampleCount=0;
giuliomoro@217 59 }
giuliomoro@217 60
giuliomoro@217 61 NetworkSend::~NetworkSend(){
giuliomoro@217 62 #ifdef USE_JUCE
giuliomoro@217 63 stopThread(1000);
giuliomoro@217 64 #else
giuliomoro@217 65 stopThread();
giuliomoro@217 66 for(unsigned int n=0; n<objAddrs.size(); n++){ //keep track of deleted instances;
giuliomoro@217 67 if(objAddrs[n]==this){
giuliomoro@217 68 objAddrs.erase(objAddrs.begin()+n);
giuliomoro@217 69 break;
giuliomoro@217 70 }
giuliomoro@217 71 }
giuliomoro@217 72 #endif
giuliomoro@217 73 dealloc();
giuliomoro@217 74 }
giuliomoro@217 75 void NetworkSend::dealloc(){
giuliomoro@217 76 channel.enabled=false;
giuliomoro@217 77 if(channel.buffers!=NULL){
giuliomoro@217 78 for(int n=0; n<channel.numBuffers; n++){
giuliomoro@217 79 free(channel.buffers[n]);
giuliomoro@217 80 channel.buffers[n]=NULL;
giuliomoro@217 81 }
giuliomoro@217 82 free(channel.buffers);
giuliomoro@217 83 channel.buffers=NULL;
giuliomoro@217 84 }
giuliomoro@217 85 free(channel.readyToBeSent);
giuliomoro@217 86 channel.readyToBeSent=NULL;
giuliomoro@217 87 free(channel.doneOnTime);
giuliomoro@217 88 channel.doneOnTime=NULL;
giuliomoro@217 89 }
giuliomoro@217 90 void NetworkSend::cleanup(){
giuliomoro@217 91 dealloc();
giuliomoro@217 92 }
giuliomoro@217 93
giuliomoro@217 94 void NetworkSend::setup(float aSampleRate, int blockSize, int aChannelNumber, int aPort, const char *aServer){
giuliomoro@217 95 #ifdef USE_JUCE
giuliomoro@217 96 #else
giuliomoro@217 97 staticConstructor(); //FIXME: ideally this should be in the constructor, but this is not currently possible
giuliomoro@217 98 //because of limitations in BeagleRT_createAuxiliaryTask()
giuliomoro@217 99 //keep track of added active instances
giuliomoro@217 100 objAddrs.push_back(this);//TODO: this line should be in the constructor, but something weird happens if
giuliomoro@217 101 // an instance of NetworkSend is then declared globally: the constructor gets called,
giuliomoro@217 102 // and objAddrs.size()==1 but when you get to setup, objAddrs.size() has reverted back to 0, without
giuliomoro@217 103 // any destructor being called in between ...
giuliomoro@217 104 #endif /* USE_JUCE */
giuliomoro@217 105 cleanup();
giuliomoro@217 106 int numSamples=blockSize*4>4*channel.bufferLength ? blockSize*4 : 4*channel.bufferLength;
giuliomoro@217 107 channel.numBuffers= 1+numSamples/channel.bufferLength; //the +1 takes the ceil() of the division
giuliomoro@217 108 channel.buffers=(float**)malloc(channel.numBuffers*sizeof(float*));
giuliomoro@217 109 printf("NumBuffers: %d\n", channel.numBuffers);
giuliomoro@217 110 if(channel.buffers==NULL)
giuliomoro@217 111 return;
giuliomoro@217 112 for(int n=0; n<channel.numBuffers; n++){
giuliomoro@217 113 channel.buffers[n]=(float*)malloc(channel.bufferLength*sizeof(float));
giuliomoro@217 114 if(channel.buffers[n]==NULL)
giuliomoro@217 115 return;
giuliomoro@217 116 }
giuliomoro@217 117 channel.readyToBeSent=(bool*)malloc(channel.numBuffers*sizeof(bool));
giuliomoro@217 118 channel.doneOnTime=(bool*)malloc(channel.numBuffers*sizeof(bool));
giuliomoro@217 119 for(int n=0; n<channel.numBuffers; n++){
giuliomoro@217 120 channel.readyToBeSent[n]=false;
giuliomoro@217 121 channel.doneOnTime[n]=true;
giuliomoro@217 122 }
giuliomoro@217 123 if(channel.readyToBeSent==NULL || channel.doneOnTime==NULL)
giuliomoro@217 124 return;
giuliomoro@217 125 channel.writePointer=0;
giuliomoro@217 126 channel.writeBuffer=0;
giuliomoro@217 127 channel.readBuffer=0;
giuliomoro@217 128 setChannelNumber(aChannelNumber);
giuliomoro@217 129 setPort(aPort); //TODO: check for the return value
giuliomoro@217 130 setServer(aServer); //TODO: check for the return value
giuliomoro@217 131 printf("Channel %d is sending messages to: %s:%d at %fHz\n", getChannelNumber(), aServer, aPort, aSampleRate);
giuliomoro@217 132 channel.enabled=true;
giuliomoro@217 133 }
giuliomoro@217 134
giuliomoro@217 135 void NetworkSend::log(float value){ //TODO: add a vectorized version of this method
giuliomoro@217 136 if(channel.enabled==false)
giuliomoro@217 137 return;
giuliomoro@217 138 if(channel.writePointer==channel.bufferLength){ // when the buffer is filled ...
giuliomoro@217 139 channel.readyToBeSent[channel.writeBuffer]=true; // flag it as such
giuliomoro@217 140 // printf("Scheduling for send %d\n",(int)channel.buffers[channel.writeBuffer][channel.headerTimestampIndex]);
giuliomoro@217 141 channel.writePointer=channel.headerLength; //reset the writePointer
giuliomoro@217 142 channel.writeBuffer=(channel.writeBuffer+1); //switch buffer
giuliomoro@217 143 if(channel.writeBuffer==channel.numBuffers) // and wrap it
giuliomoro@217 144 channel.writeBuffer=0;
giuliomoro@217 145 // printf("WriteBuffer:%d\n", channel.writeBuffer);
giuliomoro@217 146 if(channel.doneOnTime[channel.writeBuffer]==false){ //check if this buffer's last sending has completed on time ...
giuliomoro@217 147 printf("Network buffer underrun. timestamp: %d :-{\n",
giuliomoro@217 148 (int)channel.buffers[channel.writeBuffer][channel.headerTimestampIndex]);
giuliomoro@217 149 }
giuliomoro@217 150 channel.doneOnTime[channel.writeBuffer]=false; // ... and then reset the flag
giuliomoro@217 151 #ifdef USE_JUCE
giuliomoro@217 152 if(isThreadRunning()==false){
giuliomoro@217 153 startThread(10);
giuliomoro@217 154 }
giuliomoro@217 155 #else
giuliomoro@217 156 if(isThreadRunning()==false){
giuliomoro@217 157 startThread();
giuliomoro@217 158 }
giuliomoro@217 159 #endif /* USE_JUCE */
giuliomoro@217 160 }
giuliomoro@217 161 if(channel.writePointer==channel.headerLength){ // we are about to start writing in the buffer, let's set the header
giuliomoro@217 162 //set dynamic header values here. Static values are set in setup() and setChannelNumber().
giuliomoro@217 163 channel.buffers[channel.writeBuffer][channel.headerTimestampIndex]=(float)channel.sampleCount; //timestamp
giuliomoro@217 164 channel.sampleCount++;
giuliomoro@217 165 //add here more header fields
giuliomoro@217 166 }
giuliomoro@217 167 channel.buffers[channel.writeBuffer][channel.writePointer++]=value;
giuliomoro@217 168 // sampleCount++;
giuliomoro@217 169 };
giuliomoro@217 170
giuliomoro@217 171 void NetworkSend::setServer(const char *aServer){
giuliomoro@217 172 #ifdef USE_JUCE
giuliomoro@217 173 remoteHostname=String::fromUTF8(aServer);
giuliomoro@217 174 #else
giuliomoro@217 175 udpClient.setServer(aServer);
giuliomoro@217 176 #endif /* USE_JUCE */
giuliomoro@217 177 }
giuliomoro@217 178 void NetworkSend::setPort(int aPort){
giuliomoro@217 179 #ifdef USE_JUCE
giuliomoro@217 180 remotePortNumber=aPort;
giuliomoro@217 181 #else
giuliomoro@217 182 udpClient.setPort(aPort);
giuliomoro@217 183 #endif /* USE_JUCE */
giuliomoro@217 184 }
giuliomoro@217 185
giuliomoro@217 186 void NetworkSend::setChannelNumber(int aChannelNumber){
giuliomoro@217 187 channel.channelNumber=aChannelNumber;
giuliomoro@217 188 for(int n=0; n<channel.numBuffers; n++){ //initialize the header
giuliomoro@217 189 channel.buffers[n][channel.headerChannelIndex]=channel.channelNumber;
giuliomoro@217 190 //add here more static header fields
giuliomoro@217 191 }
giuliomoro@217 192 };
giuliomoro@217 193 int NetworkSend::getChannelNumber(){
giuliomoro@217 194 return channel.channelNumber;
giuliomoro@217 195 };
giuliomoro@217 196
giuliomoro@217 197 int NetworkSend::getTimestamp(){
giuliomoro@217 198 return channel.buffers[channel.readBuffer][channel.headerTimestampIndex];
giuliomoro@217 199 }
giuliomoro@217 200
giuliomoro@217 201 void NetworkSend::sendData(){
giuliomoro@217 202 if(channel.enabled==false)
giuliomoro@217 203 return;
giuliomoro@217 204 while(channel.readyToBeSent[channel.readBuffer]==true){
giuliomoro@217 205 channel.readyToBeSent[channel.readBuffer]=false;
giuliomoro@217 206 void* sourceBuffer=channel.buffers[channel.readBuffer];
giuliomoro@217 207 // printf("Trying to send timestamp %d\n",(int)((float*)sourceBuffer)[channel.headerTimestampIndex]);
giuliomoro@217 208 // printf("ReadBuffer:%d\n", channel.readBuffer);
giuliomoro@217 209 unsigned int numBytesToSend=NETWORK_AUDIO_BUFFER_SIZE*sizeof(float);
giuliomoro@217 210 //TODO: call waitUntilReady before trying to write/send, to avoid blocks! (OR NOT?)
giuliomoro@217 211 #ifdef USE_JUCE
giuliomoro@217 212 if(1==udpClient.waitUntilReady(0, 5)){
giuliomoro@217 213 udpClient.write(remoteHostname, remotePortNumber, sourceBuffer, numBytesToSend);
giuliomoro@217 214 channel.doneOnTime[channel.readBuffer]=true;
giuliomoro@217 215 // printf ("Sent timestamp: %d\n", (int)((float*)sourceBuffer)[1]);
giuliomoro@217 216 } else {
giuliomoro@217 217 // printf ("Not ready timestamp: %d\n", (int)((float*)sourceBuffer)[1]);
giuliomoro@217 218 }
giuliomoro@217 219 #else
giuliomoro@217 220 udpClient.send(sourceBuffer, numBytesToSend);
giuliomoro@217 221 channel.doneOnTime[channel.readBuffer]=true;
giuliomoro@217 222 #endif /* USE_JUCE */
giuliomoro@217 223 channel.readBuffer++;
giuliomoro@217 224 if(channel.readBuffer==channel.numBuffers)
giuliomoro@217 225 channel.readBuffer=0;
giuliomoro@217 226 }
giuliomoro@217 227 }
giuliomoro@217 228
giuliomoro@217 229 void NetworkSend::run(){
giuliomoro@217 230 #ifdef USE_JUCE
giuliomoro@217 231 // std::chrono::high_resolution_clock::time_point t1;
giuliomoro@217 232 // std::chrono::high_resolution_clock::time_point t2;
giuliomoro@217 233 // std::chrono::high_resolution_clock::time_point t3;
giuliomoro@217 234 while(threadShouldExit()==false){
giuliomoro@217 235 // t3 = std::chrono::high_resolution_clock::now();
giuliomoro@217 236 // t1 = std::chrono::high_resolution_clock::now();
giuliomoro@217 237 sendData();
giuliomoro@217 238 // t2 = std::chrono::high_resolution_clock::now();
giuliomoro@217 239 // auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t3 - t1 ).count();
giuliomoro@217 240 // auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
giuliomoro@217 241 // if(duration2>0)
giuliomoro@217 242 // std::cout << "Duration is: " << duration2 <<". Whole loop is: " << duration1 << "\n";
giuliomoro@217 243 sleep(1);
giuliomoro@217 244 }
giuliomoro@217 245 #else
giuliomoro@217 246 threadRunning=true;
giuliomoro@217 247 while(threadShouldExit()==false){
giuliomoro@217 248 sendAllData();
giuliomoro@217 249 usleep(sleepTimeMs*1000);
giuliomoro@217 250 }
giuliomoro@217 251 threadRunning=false;
giuliomoro@217 252 #endif
giuliomoro@217 253 }
giuliomoro@217 254 #ifdef USE_JUCE
giuliomoro@217 255 #else
giuliomoro@217 256 Scope::Scope(int aNumChannels):
giuliomoro@217 257 channels(aNumChannels)
giuliomoro@217 258 {};
giuliomoro@217 259 Scope::~Scope(){};
giuliomoro@217 260
giuliomoro@217 261 void Scope::log(int channel, float value){
giuliomoro@217 262 if(channel>=getNumChannels()) //TODO: assert this
giuliomoro@217 263 return;
giuliomoro@217 264 channels[channel].log(value);
giuliomoro@217 265 }
giuliomoro@217 266
giuliomoro@217 267 void Scope::setup(){
giuliomoro@217 268 setup(44100, 9999, "127.0.0.1");
giuliomoro@217 269 }
giuliomoro@217 270
giuliomoro@217 271 void Scope::setup(float sampleRate, int aPort, const char* aServer){
giuliomoro@217 272 for(int n=0; n<getNumChannels(); n++){
giuliomoro@217 273 channels[n].setup(sampleRate, 128, n, aPort, aServer); //TODO: replace 128 with the actual block size
giuliomoro@217 274 }
giuliomoro@217 275 }
giuliomoro@217 276
giuliomoro@217 277 void Scope::setPort(int port){
giuliomoro@217 278 for(int n=0; n<getNumChannels(); n++){
giuliomoro@217 279 channels[n].setPort(port);
giuliomoro@217 280 }
giuliomoro@217 281 }
giuliomoro@217 282 void Scope::setPort(int channel, int aPort){
giuliomoro@217 283 channels[channel].setPort(aPort);
giuliomoro@217 284 printf("Channel %d is now sending to port %d\n", channel, aPort);
giuliomoro@217 285 }
giuliomoro@217 286
giuliomoro@217 287 int Scope::getNumChannels(){
giuliomoro@217 288 return channels.size();
giuliomoro@217 289 }
giuliomoro@217 290
giuliomoro@217 291 void Scope::sendData(){
giuliomoro@217 292 NetworkSend::sendAllData();
giuliomoro@217 293 }
giuliomoro@217 294 #endif