Mercurial > hg > beaglert
comparison core/ReceiveAudioThread.cpp @ 122:bc514f29c3aa scope-refactoring
ReceiveAudioThread updated to match the newest UdpServer
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sat, 22 Aug 2015 01:20:55 +0100 |
parents | cdd441a304a9 |
children | 850a4a9bd832 |
comparison
equal
deleted
inserted
replaced
121:2197435e8fb4 | 122:bc514f29c3aa |
---|---|
27 buffer=NULL; | 27 buffer=NULL; |
28 free(stackBuffer); | 28 free(stackBuffer); |
29 stackBuffer=NULL; | 29 stackBuffer=NULL; |
30 } | 30 } |
31 void ReceiveAudioThread::wrapWritePointer(){ | 31 void ReceiveAudioThread::wrapWritePointer(){ |
32 //this is not quite a simple wrapping as you wold in a circular buffer, | 32 //this is not quite a simple wrapping as you would do in a circular buffer, |
33 //as there is no guarantee the buffer will be full at all times, given that there must alwas be enough space at the end of it | 33 //as there is no guarantee the buffer will be full at all times, given that there must alwas be enough space at the end of it |
34 //to hold a full payload | 34 //to hold a full payload |
35 // lastValidPointer indicates the last pointer in the buffer containing valid data | 35 // lastValidPointer indicates the last pointer in the buffer containing valid data |
36 // | 36 // |
37 if(writePointer+payloadLength+headerLength>bufferLength){ //if we are going to exceed the length of the buffer with the next reading | 37 if(writePointer+payloadLength+headerLength>bufferLength){ //if we are going to exceed the length of the buffer with the next reading |
56 int ReceiveAudioThread::readUdpToBuffer(){ | 56 int ReceiveAudioThread::readUdpToBuffer(){ |
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(1){ //TODO: implement waitUntilReady for UdpServer | 61 if(socket.waitUntilReady(true, waitForSocketTime)){ // waitForSocketTime could have been set to -1 (wait forever), |
62 //JUCE if(socket.waitUntilReady(true, waitForSocketTime)){ // waitForSocketTime could have been set to -1 (wait forever), | |
63 // but it would have made it more difficult for the thread to be killed | 62 // but it would have made it more difficult for the thread to be killed |
64 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0 | 63 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0 |
65 //read header+payload | 64 //read header+payload |
66 //JUCE int numBytes=socket.read(buffer+writePointer, bytesToRead,1); | 65 //JUCE int numBytes=socket.read(buffer+writePointer, bytesToRead,1); |
67 int numBytes=socket.read(buffer+writePointer, bytesToRead); | 66 int numBytes=socket.read(buffer+writePointer, bytesToRead, false); //read without waiting. |
68 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header | 67 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header |
69 if(numBytes<0){ | 68 if(numBytes<0){ |
70 printf("error numBytes1\n"); | 69 printf("error numBytes1\n"); |
71 return -3; //TODO: something went wrong, you have to discard the rest of the packet! | 70 return -3; //TODO: something went wrong, you have to discard the rest of the packet! |
72 } | 71 } |
214 } | 213 } |
215 bool ReceiveAudioThread::isBufferReady(){ | 214 bool ReceiveAudioThread::isBufferReady(){ |
216 return bufferReady; | 215 return bufferReady; |
217 } | 216 } |
218 void ReceiveAudioThread::startThread(){ | 217 void ReceiveAudioThread::startThread(){ |
218 printf("receivedata is going to be scheduled\n"); | |
219 BeagleRT_scheduleAuxiliaryTask(receiveDataTask); | 219 BeagleRT_scheduleAuxiliaryTask(receiveDataTask); |
220 printf("receivedata has been scheduled\n"); | |
220 } | 221 } |
221 void ReceiveAudioThread::stopThread(){ | 222 void ReceiveAudioThread::stopThread(){ |
222 threadIsExiting=true; | 223 threadIsExiting=true; |
223 } | 224 } |
224 bool ReceiveAudioThread::threadShouldExit(){ | 225 bool ReceiveAudioThread::threadShouldExit(){ |
232 #ifdef JUCE | 233 #ifdef JUCE |
233 readUdpToBuffer(); // read into the oldBuffer | 234 readUdpToBuffer(); // read into the oldBuffer |
234 usleep(sleepTime); | 235 usleep(sleepTime); |
235 #else | 236 #else |
236 for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){ | 237 for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){ |
238 // printf("%d\n", n); | |
237 ReceiveAudioThread::objAddrs[n]->readUdpToBuffer(); | 239 ReceiveAudioThread::objAddrs[n]->readUdpToBuffer(); |
238 } | 240 } |
239 usleep(sleepTime); //TODO: use rt_task_sleep instead | 241 usleep(sleepTime); //TODO: use rt_task_sleep instead |
240 #endif | 242 #endif |
241 } | 243 } |