diff core/ReceiveAudioThread.cpp @ 222:6a23c07d0fbb mergingClockSync

Working with UdpIoPlugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 14 Feb 2016 01:09:23 +0000
parents c42a6b4dc2d4
children cb47043c8c28
line wrap: on
line diff
--- a/core/ReceiveAudioThread.cpp	Sat Feb 13 16:56:29 2016 +0000
+++ b/core/ReceiveAudioThread.cpp	Sun Feb 14 01:09:23 2016 +0000
@@ -39,7 +39,7 @@
         //  for(int n=headerLength;n<lastValidPointer; n++){
             //  fprintf(fd2, "%f\n",buffer[n]); //DEBUG
         //  }
-        writePointer=0; //and reset to beginning of the buffer
+    	writePointer=0; //and reset to beginning of the buffer
     }
 }
 void ReceiveAudioThread::pushPayload(int startIndex){ //backup the payload samples that will be overwritten by the new header
@@ -54,17 +54,18 @@
 }
 
 int ReceiveAudioThread::readUdpToBuffer(){
-    if(listening==false || bufferReady==false)
+
+	if(listening==false || bufferReady==false)
         return 0;
     if(writePointer<0)
         return 0;
     if(socket.waitUntilReady(true, waitForSocketTime)){// TODO: if waitForSocketTime here is >>5, the
+    	// destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381
 #ifdef USE_JUCE
 #else
 		lastTime=rt_timer_read();
 //        rt_printf("lastTimeread= %llu\n", lastTime);
 #endif /* USE_JUCE */
-    	// destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381
         pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0
         //read header+payload
         int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting.
@@ -150,7 +151,7 @@
   //  fprintf(fd,"var=["); //DEBUG
   headerLength=2;
   payloadLength=300; //TODO: make sure that payloadLength and headerLength are the same as the client is sending.
-  bufferLength=std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ...
+  bufferLength=10 * std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ...
                       //We keep a headerLength padding at the beginning of the array to allow full reads from the socket
   buffer=(float*)malloc(sizeof(float)*bufferLength);
   if(buffer==NULL) // something wrong
@@ -206,12 +207,15 @@
     if(writePointer<0 /*|| (numCalls&16383)==0*/){ //if writePointer has not been initalized yet ...
 #ifdef USE_JUCE
 #else //debug
-    	rt_printf("reinit the writePointer, readPointer: %f;\n",readPointer);
-    	readPointer=0;
+    	readPointer = headerLength;
 #endif /* USE_JUCE */
-        writePointer=2*length;  // do it, so that it starts writing at a safety margin from where we write.
+    	// this cumbersome line means: start writing at a position which is as close as possible
+    	// to the center of the buffer, but still is aligned to (payloadLength*x)+headerLength
+    	// thus allowing buffering to allow clock drift to go either way
+        writePointer = headerLength + ((bufferLength-headerLength)/payloadLength/2)*payloadLength;
                             // This will help keeping them in sync.
                             //TODO: handle what happens when the remote stream is interrupted and then restarted
+        printf("write pointer inited at: %d\n", writePointer);
     }
     numCalls++;
     if(length>lastValidPointer) { 
@@ -295,7 +299,6 @@
         sleep(sleepTime);
 #else
 		for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){
-//			printf("%d\n", n);
 			ReceiveAudioThread::objAddrs[n]->readUdpToBuffer();
 		}
 		usleep(sleepTime); //TODO: use rt_task_sleep instead