UDP latency issue when using OfxNetwork

I posted this in the “beginners” forums but didn’t get any feedback; maybe that wasn’t the right place to post it.

I’ll give you a basic run-through of my application. I’m using Ableton Live to host MIDI clips containing automation data. Ableton Live sends MIDI CC values to OpenFrameworks Application #1, which reads the MIDI automation data (via OfxMIDI) and sends it out as UDP packets to a specified IP address. Right now this is direct computer 1 to computer 2, in the future likely multicast. On the other end - either an iPhone or another Mac - OpenFrameworks Application #2 reads the UDP packets and draws simple information to the window. Right now it’s simple - automation values from 0-127 change the brightness of the window on the receiving computer. Sounds simple, yes? Unfortunately for this to sync up I need minimal latency - <20 ms, if possible. The applications all work, but there’s about a 5-10 second delay on the receiving end between receiving packets and making changes to the OpenFrameworks window. It is then synchronized to the beats, but it’s all delayed by ~5 seconds.

I’m printing all the MIDI messages in OpenFrameworks Application #1, so the issue is definitely in the UDP Tx -> UDP Rx part of the code.

Below is the relevant code from testApp.cpp.

Application #1: UDP Tx (including MIDI Rx)

  
  
//--------------------------------------------------------------  
void testApp::setup() {  
	ofSetVerticalSync(true);  
	ofBackground(255, 255, 255);  
	ofSetLogLevel(OF_LOG_VERBOSE);  
	  
	midiIn.openVirtualPort("Light Array Input");  
    midiIn.addListener(this);  
	  
	// print received messages to the console  
	midiIn.setVerbose(true);  
      
    //create the socket and set to send to 127.0.0.1:11999  
    string ipAddress = "127.0.0.1";  
	udpConnection.Create();  
	udpConnection.Connect(ipAddress.c_str(),11999);  
	udpConnection.SetNonBlocking(false);  
    val = 0;  
}  
  
//--------------------------------------------------------------  
void testApp::update() {  
}  
  
//--------------------------------------------------------------  
void testApp::draw() {  
	ofSetColor(0);  
      
//    if (val == 127) {  
//        val = 0;  
//    }  
//    else {  
//        val++;  
//    }  
    //string message="";  
      
    val = midiMessage.value;  
    string message=ofToString(val);  
	int sent = udpConnection.Send(message.c_str(),message.length());  
  
	  
  
	ofDrawBitmapString(ofToString(midiMessage.value), 20, 240);  
	text.str(""); // clear  
}  
  

Application #2: UDP Rx and Display

  
  
void testApp::setup(){  
	ofSetOrientation(OF_ORIENTATION_90_LEFT);  
  
	// listen on the given port  
	cout << "listening for UDP messages\n";  
  
	mouseX = 0;  
	mouseY = 0;  
	mouseButtonState = "";  
      
    //create the socket and bind to port 11999  
	udpConnection.Create();  
	udpConnection.Bind(11999);  
	udpConnection.SetNonBlocking(true);  
  
	ofBackground( 30, 30, 130 );  
      
    val = 0;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
      
      
      
    char udpMessage[10];  
	udpConnection.Receive(udpMessage,10);  
	string message=udpMessage;  
    if (message!="") {  
          
        recieved_val = atof(ofToString(message).c_str());  
        if (recieved_val < 256)  {  
            val = 2*recieved_val;  
        }  
          
    }  
    	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	string buf;  
	buf = "listening for UDP messages\n";  
	ofDrawBitmapString( buf, 10, 20 );  
      
    ofBackground(val, val, val);  
      
    ofDrawBitmapString(ofToString(recieved_val), 30, 40);  
  
	  
}  
  

Any ideas on why I’m having this ~5-10 second buffer problem?

Thanks!
Drew

Hi and welcome!
No, the place was alright, but you should probably allow more than 24hrs for a reply, especially on a weekend.
Also, please don’t double-post, people will see your question nonetheless. I’ll leave this one open and remove your other identical thread.

Thanks! Sorry about that!

I had a similar problem not too long ago. I was sending UDP from one computer to another, and would notice “lag” building up.

The problem turned out to be that I had misunderstood how UDP works. I thought that when new packets arrived, all the old ones were discarded. This isn’t true. What happens is that the packets pile up until the receiving computer reads them.

So if computer 1 is running at 60 fps, and computer 2 is running at 40 fps, than all the extra packets that computer 1 sends to computer 2 pile up. If all you care about is the latest packet from computer 1, than you should read through all waiting packets until you get to the last one.

here’s the code I used:

  
  
        char udpMessage[1000];  
        string message;  
        string tempMessage;  
        bool getNext = true;  
          
        while (getNext) {  
            udpConnectionIn.Receive(udpMessage,1000);  
            tempMessage=udpMessage;  
              
            if (tempMessage=="") {  
                getNext = false;  
            }  
            else {  
                message = tempMessage;  
            }  
        }  
          
        if(message!=""){  
            parseJSON(message);  
        }  
  

My god… that fixed it! Thank you SO much!