giuliomoro@117
|
1 #include "ReceiveAudioThread.h"
|
giuliomoro@119
|
2
|
giuliomoro@119
|
3 #ifdef JUCE
|
giuliomoro@119
|
4 #else
|
giuliomoro@117
|
5 //initialise static members
|
giuliomoro@119
|
6 bool ReceiveAudioThread::staticConstructed=false;
|
giuliomoro@117
|
7 AuxiliaryTask ReceiveAudioThread::receiveDataTask=NULL;
|
giuliomoro@119
|
8 std::vector<ReceiveAudioThread *> ReceiveAudioThread::objAddrs(0);
|
giuliomoro@119
|
9 bool ReceiveAudioThread::threadRunning;
|
giuliomoro@119
|
10 bool ReceiveAudioThread::threadIsExiting;
|
giuliomoro@119
|
11 int ReceiveAudioThread::sleepTime;
|
giuliomoro@119
|
12
|
giuliomoro@119
|
13 void receiveData(){
|
giuliomoro@119
|
14 ReceiveAudioThread::run();
|
giuliomoro@119
|
15 }
|
giuliomoro@119
|
16 void ReceiveAudioThread::staticConstructor(){
|
giuliomoro@119
|
17 if(staticConstructed==true)
|
giuliomoro@119
|
18 return;
|
giuliomoro@119
|
19 staticConstructed=true;
|
giuliomoro@119
|
20 threadIsExiting=false;
|
giuliomoro@119
|
21 receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities
|
giuliomoro@119
|
22 }
|
giuliomoro@119
|
23 #endif
|
giuliomoro@117
|
24
|
giuliomoro@117
|
25 void ReceiveAudioThread::dealloc(){
|
giuliomoro@117
|
26 free(buffer);
|
giuliomoro@117
|
27 buffer=NULL;
|
giuliomoro@117
|
28 free(stackBuffer);
|
giuliomoro@117
|
29 stackBuffer=NULL;
|
giuliomoro@117
|
30 }
|
giuliomoro@117
|
31 void ReceiveAudioThread::wrapWritePointer(){
|
giuliomoro@117
|
32 //this is not quite a simple wrapping as you wold in a circular buffer,
|
giuliomoro@117
|
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
|
giuliomoro@117
|
34 //to hold a full payload
|
giuliomoro@117
|
35 // lastValidPointer indicates the last pointer in the buffer containing valid data
|
giuliomoro@117
|
36 //
|
giuliomoro@117
|
37 if(writePointer+payloadLength+headerLength>bufferLength){ //if we are going to exceed the length of the buffer with the next reading
|
giuliomoro@117
|
38 // lastValidPointer=writePointer+headerLength; //remember where the last valid data are
|
giuliomoro@117
|
39 // for(int n=headerLength;n<lastValidPointer; n++){
|
giuliomoro@117
|
40 // fprintf(fd2, "%f\n",buffer[n]); //DEBUG
|
giuliomoro@117
|
41 // }
|
giuliomoro@117
|
42 writePointer=0; //and reset to beginning of the buffer
|
giuliomoro@117
|
43 }
|
giuliomoro@117
|
44 }
|
giuliomoro@117
|
45 void ReceiveAudioThread::pushPayload(int startIndex){ //backup the payload samples that will be overwritten by the new header
|
giuliomoro@117
|
46 for(int n=0; n<headerLength; n++){
|
giuliomoro@117
|
47 stackBuffer[n]=buffer[startIndex+n];
|
giuliomoro@117
|
48 }
|
giuliomoro@117
|
49 }
|
giuliomoro@117
|
50 void ReceiveAudioThread::popPayload(int startIndex){
|
giuliomoro@117
|
51 for(int n=0; n<headerLength; n++){
|
giuliomoro@117
|
52 buffer[startIndex+n]=stackBuffer[n];
|
giuliomoro@117
|
53 }
|
giuliomoro@117
|
54 }
|
giuliomoro@117
|
55
|
giuliomoro@117
|
56 int ReceiveAudioThread::readUdpToBuffer(){
|
giuliomoro@117
|
57 if(listening==false || bufferReady==false)
|
giuliomoro@117
|
58 return 0;
|
giuliomoro@117
|
59 if(writePointer<0)
|
giuliomoro@117
|
60 return 0;
|
giuliomoro@117
|
61 if(1){ //TODO: implement waitUntilReady for UdpServer
|
giuliomoro@117
|
62 //JUCE if(socket.waitUntilReady(true, waitForSocketTime)){ // waitForSocketTime could have been set to -1 (wait forever),
|
giuliomoro@117
|
63 // but it would have made it more difficult for the thread to be killed
|
giuliomoro@117
|
64 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0
|
giuliomoro@117
|
65 //read header+payload
|
giuliomoro@117
|
66 //JUCE int numBytes=socket.read(buffer+writePointer, bytesToRead,1);
|
giuliomoro@117
|
67 int numBytes=socket.read(buffer+writePointer, bytesToRead);
|
giuliomoro@117
|
68 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header
|
giuliomoro@117
|
69 if(numBytes<0){
|
giuliomoro@117
|
70 printf("error numBytes1\n");
|
giuliomoro@117
|
71 return -3; //TODO: something went wrong, you have to discard the rest of the packet!
|
giuliomoro@117
|
72 }
|
giuliomoro@117
|
73 if(numBytes==0){//TODO: when inplementing waitUntilReady, this should not happen unless you actually receive a packate of size zero (is it at all possible?)
|
giuliomoro@117
|
74 // printf("received 0 bytes\n");
|
giuliomoro@117
|
75 return 0;
|
giuliomoro@117
|
76 }
|
giuliomoro@119
|
77 if(numBytes != bytesToRead){ //this is equivalent to (numBytes<bytesToRead)
|
giuliomoro@117
|
78 printf("error numBytes2: %d\n", numBytes);
|
giuliomoro@117
|
79 return -4; //TODO: something went wrong, less bytes than expected in the payload.
|
giuliomoro@117
|
80 }
|
giuliomoro@119
|
81 if(channel!=(int)buffer[writePointer]){
|
giuliomoro@119
|
82 // printf("I am channel %d, but I received data for channel %d\n", channel, (int)buffer[writePointer]);
|
giuliomoro@119
|
83 return -5;
|
giuliomoro@119
|
84 }
|
giuliomoro@119
|
85 //TODO: do something else with the data in the header (e.g.: check that timestamp is sequential)
|
giuliomoro@119
|
86 // 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]);
|
giuliomoro@117
|
87 popPayload(writePointer); //restore headerLength payload samples. This could be skipped if writePointer==0
|
giuliomoro@117
|
88
|
giuliomoro@117
|
89 //even though we just wrote (payloadLength+headerLength) samples in the buffer,
|
giuliomoro@117
|
90 //we only increment by payloadLength. This way, next time a socket.read is performed, we will
|
giuliomoro@117
|
91 //backup the last headerLength samples that we just wrote and we will overwrite them with
|
giuliomoro@117
|
92 //the header from the new read. After parsing the header we will then restore the backed up samples.
|
giuliomoro@117
|
93 //This way we guarantee that, apart from the first headerLength samples, buffer is a circular buffer!
|
giuliomoro@119
|
94 // printf("writepointer:%d\n", writePointer);
|
giuliomoro@117
|
95 writePointer+=payloadLength;
|
giuliomoro@117
|
96
|
giuliomoro@117
|
97 if(writePointer>lastValidPointer){
|
giuliomoro@117
|
98 // lastValidPointer=writePointer+headerLength;
|
giuliomoro@117
|
99 }
|
giuliomoro@117
|
100 wrapWritePointer();
|
giuliomoro@117
|
101 return numBytes;
|
giuliomoro@117
|
102 }
|
giuliomoro@117
|
103 return 0; //timeout occurred
|
giuliomoro@117
|
104 }
|
giuliomoro@117
|
105 ReceiveAudioThread::ReceiveAudioThread() :
|
giuliomoro@117
|
106 //JUCE Thread(threadName),
|
giuliomoro@117
|
107 socket(0),
|
giuliomoro@117
|
108 listening(false),
|
giuliomoro@117
|
109 bufferReady(false),
|
giuliomoro@117
|
110 buffer(NULL),
|
giuliomoro@117
|
111 stackBuffer(NULL),
|
giuliomoro@117
|
112 bufferLength(0),
|
giuliomoro@117
|
113 lastValidPointer(0),
|
giuliomoro@117
|
114 waitForSocketTime(100),
|
giuliomoro@117
|
115 threadPriority(95)
|
giuliomoro@117
|
116 {};
|
giuliomoro@117
|
117 ReceiveAudioThread::~ReceiveAudioThread(){
|
giuliomoro@117
|
118 //JUCE stopThread(1000);
|
giuliomoro@117
|
119 while(threadRunning){
|
giuliomoro@119
|
120 usleep(sleepTime*2); //wait for thread to stop
|
giuliomoro@117
|
121 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl;
|
giuliomoro@117
|
122 }
|
giuliomoro@117
|
123 //TODO: check if thread stopped, otherwise kill it before dealloc
|
giuliomoro@117
|
124 dealloc();
|
giuliomoro@117
|
125 }
|
giuliomoro@119
|
126 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){
|
giuliomoro@117
|
127 dealloc();
|
giuliomoro@119
|
128 #ifdef JUCE
|
giuliomoro@119
|
129 #else
|
giuliomoro@119
|
130 staticConstructor();
|
giuliomoro@119
|
131 objAddrs.push_back(this);//TODO: this line should be in the constructor
|
giuliomoro@119
|
132 #endif
|
giuliomoro@119
|
133 bindToPort(aPort);
|
giuliomoro@119
|
134 channel=aChannel;
|
giuliomoro@117
|
135 // fd=fopen("output.m","w"); //DEBUG
|
giuliomoro@117
|
136 // fprintf(fd,"var=["); //DEBUG
|
giuliomoro@117
|
137 headerLength=2;
|
giuliomoro@117
|
138 payloadLength=300; //TODO: make sure that payloadLength and headerLength are the same as the client is sending.
|
giuliomoro@117
|
139 bufferLength=std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ...
|
giuliomoro@117
|
140 //We keep a headerLength padding at the beginning of the array to allow full reads from the socket
|
giuliomoro@117
|
141 buffer=(float*)malloc(sizeof(float)*bufferLength);
|
giuliomoro@117
|
142 if(buffer==NULL) // something wrong
|
giuliomoro@117
|
143 return;
|
giuliomoro@117
|
144 bufferReady=true;
|
giuliomoro@117
|
145 lastValidPointer=headerLength+ ((bufferLength-headerLength)/payloadLength)*payloadLength;
|
giuliomoro@117
|
146 memset(buffer,0,bufferLength*sizeof(float));
|
giuliomoro@117
|
147 stackBuffer=(float*)malloc(sizeof(float)*headerLength);
|
giuliomoro@117
|
148 bytesToRead=sizeof(float)*(payloadLength + headerLength);
|
giuliomoro@117
|
149 writePointer=-1;
|
giuliomoro@117
|
150 readPointer=0; //TODO: this *4 is sortof a security margin
|
giuliomoro@117
|
151 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently
|
giuliomoro@117
|
152 //JUCE startThread(threadPriority);
|
giuliomoro@117
|
153 }
|
giuliomoro@117
|
154
|
giuliomoro@117
|
155 void ReceiveAudioThread::bindToPort(int aPort){
|
giuliomoro@117
|
156 listening=socket.bindToPort(aPort);
|
giuliomoro@119
|
157 #ifdef JUCE
|
giuliomoro@119
|
158 #else
|
giuliomoro@119
|
159 if(listening==false)
|
giuliomoro@119
|
160 printf("Could not bind to port %d\n",aPort);
|
giuliomoro@119
|
161 #endif
|
giuliomoro@117
|
162 }
|
giuliomoro@117
|
163 bool ReceiveAudioThread::isListening(){
|
giuliomoro@117
|
164 return listening;
|
giuliomoro@117
|
165 }
|
giuliomoro@117
|
166 float* ReceiveAudioThread::getCurrentBuffer(int length){ // NOTE: this cannot work all the time unless samplesPerBuffer and payloadLength are multiples
|
giuliomoro@117
|
167 //TODO: make it return the number of samples actually available at the specified location
|
giuliomoro@117
|
168 if(isListening()==false || length>bufferLength)
|
giuliomoro@117
|
169 return NULL;
|
giuliomoro@117
|
170 readPointer+=length;
|
giuliomoro@117
|
171 if(readPointer>lastValidPointer){
|
giuliomoro@117
|
172 readPointer=headerLength;
|
giuliomoro@117
|
173 }
|
giuliomoro@117
|
174 return buffer+(int)readPointer;
|
giuliomoro@117
|
175 };
|
giuliomoro@117
|
176 int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){//TODO: add interleaved version
|
giuliomoro@117
|
177 if (!(samplingRateRatio>0 && samplingRateRatio<=2))
|
giuliomoro@117
|
178 return -2;
|
giuliomoro@117
|
179 if(isListening()==false)
|
giuliomoro@117
|
180 return -1;
|
giuliomoro@117
|
181 if(writePointer<0){ //if writePointer has not been initalized yet ...
|
giuliomoro@117
|
182 writePointer=2*length; // do it, so that it starts writing at a safety margin from where we write.
|
giuliomoro@117
|
183 // This will help keeping them in sync.
|
giuliomoro@117
|
184 //TODO: handle what happens when the remote stream is interrupted and then restarted
|
giuliomoro@117
|
185 }
|
giuliomoro@117
|
186 if(length>lastValidPointer) {
|
giuliomoro@117
|
187 //not enough samples available, we fill the buffer with what is available, but the destination buffer will not be filled completely
|
giuliomoro@117
|
188 //at this very moment the other thread might be writing at most one payload into the buffer.
|
giuliomoro@117
|
189 //To avoid a race condition, we need to let alone the buffer where we are currently writing
|
giuliomoro@117
|
190 //as writing the payload also temporarily overwrites the previous headerLength samples, we need to account for them as well
|
giuliomoro@117
|
191 //TODO: This assumes that the writePointer and readPointer do not drift. When doing clock synchronization we will find out that it is not true!
|
giuliomoro@117
|
192 length=lastValidPointer-payloadLength-headerLength;
|
giuliomoro@117
|
193 if(length<0) //no samples available at all!
|
giuliomoro@117
|
194 return 0;
|
giuliomoro@117
|
195 }
|
giuliomoro@117
|
196 for(int n=0; n<length; n++){
|
giuliomoro@117
|
197 destination[n]=buffer[(int)(0.5+readPointer)];//simple ZOH non-interpolation (nearest neighbour)
|
giuliomoro@117
|
198 // fprintf(fd,"%f, %d, %f;\n",readPointer,writePointer,destination[n]); //DEBUG
|
giuliomoro@117
|
199 readPointer+=samplingRateRatio;
|
giuliomoro@117
|
200 if((int)(0.5+readPointer)>=lastValidPointer){
|
giuliomoro@117
|
201 readPointer=readPointer-lastValidPointer+headerLength;
|
giuliomoro@117
|
202 }
|
giuliomoro@117
|
203 }
|
giuliomoro@117
|
204 return readPointer;
|
giuliomoro@117
|
205 }
|
giuliomoro@117
|
206
|
giuliomoro@117
|
207 bool ReceiveAudioThread::isBufferReady(){
|
giuliomoro@117
|
208 return bufferReady;
|
giuliomoro@117
|
209 }
|
giuliomoro@117
|
210 void ReceiveAudioThread::startThread(){
|
giuliomoro@117
|
211 BeagleRT_scheduleAuxiliaryTask(receiveDataTask);
|
giuliomoro@117
|
212 }
|
giuliomoro@117
|
213 void ReceiveAudioThread::stopThread(){
|
giuliomoro@117
|
214 threadIsExiting=true;
|
giuliomoro@117
|
215 }
|
giuliomoro@117
|
216 bool ReceiveAudioThread::threadShouldExit(){
|
giuliomoro@117
|
217 return(gShouldStop || threadIsExiting );
|
giuliomoro@117
|
218 }
|
giuliomoro@117
|
219 void ReceiveAudioThread::run(){
|
giuliomoro@117
|
220 // fd2=fopen("buffer.m","w"); //DEBUG
|
giuliomoro@117
|
221 // fprintf(fd2, "buf=["); //DEBUG
|
giuliomoro@117
|
222 threadRunning=true;
|
giuliomoro@117
|
223 while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting
|
giuliomoro@119
|
224 #ifdef JUCE
|
giuliomoro@117
|
225 readUdpToBuffer(); // read into the oldBuffer
|
giuliomoro@117
|
226 usleep(sleepTime);
|
giuliomoro@119
|
227 #else
|
giuliomoro@119
|
228 for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){
|
giuliomoro@119
|
229 ReceiveAudioThread::objAddrs[n]->readUdpToBuffer();
|
giuliomoro@119
|
230 }
|
giuliomoro@119
|
231 usleep(sleepTime); //TODO: use rt_task_sleep instead
|
giuliomoro@119
|
232 #endif
|
giuliomoro@117
|
233 }
|
giuliomoro@117
|
234 threadRunning=false;
|
giuliomoro@117
|
235 // fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG
|
giuliomoro@117
|
236 // fclose(fd);//DEBUG
|
giuliomoro@117
|
237 // fprintf(fd2,"];");//DEBUG
|
giuliomoro@117
|
238 // fclose(fd2); //DEBUG
|
giuliomoro@117
|
239 }
|