comparison core/ReceiveAudioThread.cpp @ 132:e24c531220ee scope-refactoring

Added some sort of synchronization, not working great though
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 27 Aug 2015 01:42:04 +0100
parents ff28e56e5b7e
children 04b1678614c9
comparison
equal deleted inserted replaced
131:ff28e56e5b7e 132:e24c531220ee
57 if(listening==false || bufferReady==false) 57 if(listening==false || bufferReady==false)
58 return 0; 58 return 0;
59 if(writePointer<0) 59 if(writePointer<0)
60 return 0; 60 return 0;
61 if(socket.waitUntilReady(true, waitForSocketTime)){// TODO: if waitForSocketTime here is >>5, the 61 if(socket.waitUntilReady(true, waitForSocketTime)){// TODO: if waitForSocketTime here is >>5, the
62 #ifdef USE_JUCE
63 #else
64 lastTime=rt_timer_read();
65 // rt_printf("lastTimeread= %llu\n", lastTime);
66 #endif /* USE_JUCE */
62 // destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381 67 // destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381
63 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0 68 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0
64 //read header+payload 69 //read header+payload
65 int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting. 70 int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting.
66 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header 71 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header
78 } 83 }
79 if(channel!=(int)buffer[writePointer]){ 84 if(channel!=(int)buffer[writePointer]){
80 // printf("I am channel %d, but I received data for channel %d\n", channel, (int)buffer[writePointer]); 85 // printf("I am channel %d, but I received data for channel %d\n", channel, (int)buffer[writePointer]);
81 return -5; 86 return -5;
82 } 87 }
83 static int timestamp=0;
84 if(buffer[writePointer+1]!=timestamp+1) 88 if(buffer[writePointer+1]!=timestamp+1)
85 printf("missing a timestamp: %d\n",timestamp+1); 89 printf("missing a timestamp: %d\n",timestamp+1);
86 timestamp=buffer[writePointer+1]; 90 timestamp=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]); 91 // 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 92
159 bufferReady=true; 163 bufferReady=true;
160 bytesToRead=sizeof(float)*(payloadLength + headerLength); 164 bytesToRead=sizeof(float)*(payloadLength + headerLength);
161 writePointer=-1; 165 writePointer=-1;
162 readPointer=0; 166 readPointer=0;
163 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently 167 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently
168 timestamp=0;
164 #ifdef USE_JUCE 169 #ifdef USE_JUCE
165 startThread(threadPriority); 170 startThread(threadPriority);
166 #else 171 #else
167 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled 172 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
168 #endif /* USE_JUCE */ 173 #endif /* USE_JUCE */
239 threadIsExiting=true; 244 threadIsExiting=true;
240 } 245 }
241 bool ReceiveAudioThread::threadShouldExit(){ 246 bool ReceiveAudioThread::threadShouldExit(){
242 return(gShouldStop || threadIsExiting ); 247 return(gShouldStop || threadIsExiting );
243 } 248 }
244 #endif /* USE_JUCE */ 249 RTIME ReceiveAudioThread::getLastTime(){
250 return lastTime;
251 }
252 #endif /* USE_JUCE */
253 int ReceiveAudioThread::getTimestamp(){
254 return timestamp;
255 }
245 void ReceiveAudioThread::run(){ 256 void ReceiveAudioThread::run(){
246 // fd2=fopen("buffer.m","w"); //DEBUG 257 // fd2=fopen("buffer.m","w"); //DEBUG
247 // fprintf(fd2, "buf=["); //DEBUG 258 // fprintf(fd2, "buf=["); //DEBUG
248 threadRunning=true; 259 threadRunning=true;
249 int maxCount=10; 260 int maxCount=10;