Hi all,

I am having a problem with VoIP streaming. There are two computers, the source computer and the Server computer. The source sends the packets to the Server and Server simply sends them back to the source.

Now the source computer has two threads, the packet sender and the receiver. If the computer is left on its own to handle the sender and the receiver it is unpredictable to know how long the processor will be at the sender thread or at the receiver thread, consequently loosing packets. So, I set a method where each time a packet is sent I check the receiver once. Now since the sender requires at least 20 ms to capture the data i realized that the receiver is losing packets, almost 50%, that is i am receiving packets at a higher speed than sending them, or in other words, packets i receive packets in short burst. So to overcome that, i tried to double the frequency of the receiver by adding the OPTIONAL section in the sender section. Now in the other hand i get overrun complains because the sender can not read fast enough the information from the audio service, that is I am slower than 20ms reading the audio buffer.

Question: How can i make this two threads completely independent? I am really struggling to understand how this can be done. Also I would like to mention that I am using threads since both threads are sharing a buffer.

Looking forward to hear some ideas.

Thanks
ps: i could upload the entire files but since it is a bit messy i thought pseudo code would be enough.
Code:
volatile sig_atomic_t runcond;

pthread_mutex_t token_mutex;
pthread_cond_t ReceiveData_cond;//=PTHREAD_COND_INITIALIZER;
int breceive;
pthread_cond_t SendData_cond;//=PTHREAD_COND_INITIALIZER;
int bsend;
pthread_cond_t Start_cond;


int main (int argc, char ** argv) 
{ 

breceive=0;
bsend=0;
pthread_cond_init(&ReceiveData_cond,NULL);
pthread_cond_init(&SendData_cond,NULL);
pthread_cond_init(&Start_cond,NULL);
...
runcond=1;
pthread_create (&pid_rtp_sendera, NULL, &send_rtpa, (void *) &channels);
pthread_create (&pid_rtp_receiver, NULL, &receive_rtp_ntwcdn, (void *) &channels);

pthread_join (pid_rtp_sendera,NULL);
pthread_join (pid_rtp_receiver,NULL);
...
}

void* receive_rtp_ntwcdn (void* unused)
{
...
unsined char buffer[160];
while (runcond){
	pthread_mutex_lock(&token_mutex);
	while ((!bsend)&&(runcond))
		pthread_cond_wait(&SendData_cond,&token_mutex);
	receive_rtp(buffer);
	...
	breceive=1;
	pthread_cond_signal(&ReceiveData_cond);
		bsend=0;
	pthread_mutex_unlock(&token_mutex);
	}

}

void* send_rtpa (void* unused)
{
...
unsined char buffer[160];
while (runcond){
	pthread_mutex_lock(&token_mutex);

		/* OPTIONAL CODE to double frecuency */
		/*
			bsend=1;
			pthread_cond_signal(&SendData_cond);
			breceive=0;
			while(!breceive)
				pthread_cond_wait(&ReceiveData_cond, &token_mutex);	
			pthread_mutex_unlock(&token_mutex);
			pthread_mutex_lock(&token_mutex);
		*/
		read_audio(buffer);
		send_rtp(buffer);
		bsend=1;
		pthread_cond_signal(&SendData_cond);
		breceive=0;
		while(!breceive)
			pthread_cond_wait(&ReceiveData_cond, &token_mutex);		
	pthread_mutex_unlock(&token_mutex);
	}
}