comparison core/ReceiveAudioThread.cpp @ 131:ff28e56e5b7e scope-refactoring

Updated Network files to match Udpioplugin 18:fb5a61b10223
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 26 Aug 2015 02:02:10 +0100
parents 2696a7f00053
children e24c531220ee
comparison
equal deleted inserted replaced
130:da1c61aa97ea 131:ff28e56e5b7e
78 } 78 }
79 if(channel!=(int)buffer[writePointer]){ 79 if(channel!=(int)buffer[writePointer]){
80 // printf("I am channel %d, but I received data for channel %d\n", channel, (int)buffer[writePointer]); 80 // printf("I am channel %d, but I received data for channel %d\n", channel, (int)buffer[writePointer]);
81 return -5; 81 return -5;
82 } 82 }
83 //TODO: do something else with the data in the header (e.g.: check that timestamp is sequential) 83 static int timestamp=0;
84 if(buffer[writePointer+1]!=timestamp+1)
85 printf("missing a timestamp: %d\n",timestamp+1);
86 timestamp=buffer[writePointer+1];
84 // rt_printf("Received a message of length %d, it was on channel %d and timestamp %d\n", numBytes, (int)buffer[writePointer], (int)buffer[writePointer+1]); 87 // rt_printf("Received a message of length %d, it was on channel %d and timestamp %d\n", numBytes, (int)buffer[writePointer], (int)buffer[writePointer+1]);
88
85 popPayload(writePointer); //restore headerLength payload samples. This could be skipped if writePointer==0 89 popPayload(writePointer); //restore headerLength payload samples. This could be skipped if writePointer==0
86
87 //even though we just wrote (payloadLength+headerLength) samples in the buffer, 90 //even though we just wrote (payloadLength+headerLength) samples in the buffer,
88 //we only increment by payloadLength. This way, next time a socket.read is performed, we will 91 //we only increment by payloadLength. This way, next time a socket.read is performed, we will
89 //backup the last headerLength samples that we just wrote and we will overwrite them with 92 //backup the last headerLength samples that we just wrote and we will overwrite them with
90 //the header from the new read. After parsing the header we will then restore the backed up samples. 93 //the header from the new read. After parsing the header we will then restore the backed up samples.
91 //This way we guarantee that, apart from the first headerLength samples, buffer is a circular buffer! 94 //This way we guarantee that, apart from the first headerLength samples, buffer is a circular buffer!
92 // printf("writepointer:%d\n", writePointer);
93 writePointer+=payloadLength; 95 writePointer+=payloadLength;
94
95 if(writePointer>lastValidPointer){
96 // lastValidPointer=writePointer+headerLength;
97 }
98 wrapWritePointer(); 96 wrapWritePointer();
99 return numBytes; 97 return numBytes;
100 } 98 }
101 return 0; //timeout occurred 99 return 0; //timeout occurred
102 } 100 }
123 {}; 121 {};
124 ReceiveAudioThread::~ReceiveAudioThread(){ 122 ReceiveAudioThread::~ReceiveAudioThread(){
125 #ifdef USE_JUCE 123 #ifdef USE_JUCE
126 stopThread(1000); 124 stopThread(1000);
127 #else 125 #else
126 stopThread();
128 while(threadRunning){ 127 while(threadRunning){
129 usleep(sleepTime*2); //wait for thread to stop 128 usleep(sleepTime*2); //wait for thread to stop
130 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; 129 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl;
131 } 130 }
132 #endif /* USE_JUCE */ 131 #endif /* USE_JUCE */
245 #endif /* USE_JUCE */ 244 #endif /* USE_JUCE */
246 void ReceiveAudioThread::run(){ 245 void ReceiveAudioThread::run(){
247 // fd2=fopen("buffer.m","w"); //DEBUG 246 // fd2=fopen("buffer.m","w"); //DEBUG
248 // fprintf(fd2, "buf=["); //DEBUG 247 // fprintf(fd2, "buf=["); //DEBUG
249 threadRunning=true; 248 threadRunning=true;
249 int maxCount=10;
250 int count=0;
251 // Clean the socket from anything that is currently in it.
252 #ifdef USE_JUCE
253 // this is borrowed from BeagleRT's UdpServer class.
254 int n;
255 do {
256 float waste;
257 if(socket.waitUntilReady(true, 0)==0)
258 break;
259 n=socket.read((void*)&waste, sizeof(float), false);
260 count++;
261 if(n<0){
262 printf("error\n");
263 break;
264 }
265 printf("n: %d\n",n);
266 } while (n>0 && (maxCount<=0 || count<maxCount));
267 #else
268 for(unsigned int n=0; n<objAddrs.size(); n++){
269 count=objAddrs[n]->socket.empty(maxCount);
270 }
271 #endif /* USE_JUCE */
272 printf("socket emptied with %d reads\n", count);
273
250 while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting 274 while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting
251 #ifdef USE_JUCE 275 #ifdef USE_JUCE
252 readUdpToBuffer(); // read into the oldBuffer 276 readUdpToBuffer(); // read into the oldBuffer
253 sleep(sleepTime); 277 sleep(sleepTime);
254 #else 278 #else