merge with origin/master

This commit is contained in:
Stephen Birarda 2013-02-07 17:30:08 -08:00
commit e9309a30e7
9 changed files with 65 additions and 89 deletions

View file

@ -17,14 +17,15 @@ struct AgentList {
in_addr sin_addr; in_addr sin_addr;
Head head; Head head;
} agents[MAX_AGENTS]; } agents[MAX_AGENTS];
int num_agents = 0; int num_agents = 0;
// //
// Process an incoming spaceserver packet telling you about other nearby agents // Process an incoming domainserver packet telling you about other nearby agents
// //
void update_agents(char * data, int length) { void update_agents(char * data, int length) {
std::string packet(data, length); std::string packet(data, length);
std::cout << " Update Agents, string: " << packet << "\n"; //std::cout << " Update Agents, string: " << packet << "\n";
size_t spot; size_t spot;
size_t start_spot = 0; size_t start_spot = 0;
spot = packet.find_first_of (",", 0); spot = packet.find_first_of (",", 0);

View file

@ -29,7 +29,7 @@ const short RING_BUFFER_FRAMES = 10;
const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES; const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES;
const int SAMPLE_RATE = 22050; const int SAMPLE_RATE = 22050;
const float JITTER_BUFFER_LENGTH_MSECS = 26.0; const float JITTER_BUFFER_LENGTH_MSECS = 10.0;
const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * (SAMPLE_RATE / 1000.0); const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * (SAMPLE_RATE / 1000.0);
const short NUM_AUDIO_SOURCES = 2; const short NUM_AUDIO_SOURCES = 2;
@ -105,31 +105,26 @@ int audioCallback (const void *inputBuffer,
if (ringBuffer->endOfLastWrite != NULL) { if (ringBuffer->endOfLastWrite != NULL) {
// play whatever we have in the audio buffer if (!ringBuffer->started && ringBuffer->diffLastWriteNextOutput() <= PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES) {
short silentTail = 0; printf("Held back\n");
} else if (ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES) {
// if the end of the last write to the ring is in front of the current output pointer ringBuffer->started = false;
// AND the difference between the two is less than a full output buffer
// we need to add some silence after the audio data, to avoid replaying old data
if ((ringBuffer->endOfLastWrite - ringBuffer->buffer) > (ringBuffer->nextOutput - ringBuffer->buffer)
&& (ringBuffer->endOfLastWrite - ringBuffer->nextOutput) < BUFFER_LENGTH_SAMPLES) {
silentTail = BUFFER_LENGTH_SAMPLES - (ringBuffer->endOfLastWrite - ringBuffer->nextOutput);
}
// no sample overlap, either a direct copy of the audio data, or a copy with some appended silence
memcpy(queueBuffer, ringBuffer->nextOutput, (BUFFER_LENGTH_SAMPLES - silentTail) * sizeof(int16_t));
ringBuffer->nextOutput += BUFFER_LENGTH_SAMPLES;
if (ringBuffer->nextOutput == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) {
ringBuffer->nextOutput = ringBuffer->buffer;
}
if (ringBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES) {
starve_counter++; starve_counter++;
printf("Starved #%d\n", starve_counter); printf("Starved #%d\n", starve_counter);
data->wasStarved = 10; // Frames to render the indication that the system was starved. data->wasStarved = 10; // Frames to render the indication that the system was starved.
ringBuffer->endOfLastWrite = NULL; } else {
ringBuffer->started = true;
// play whatever we have in the audio buffer
// no sample overlap, either a direct copy of the audio data, or a copy with some appended silence
memcpy(queueBuffer, ringBuffer->nextOutput, BUFFER_LENGTH_BYTES);
ringBuffer->nextOutput += BUFFER_LENGTH_SAMPLES;
if (ringBuffer->nextOutput == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) {
ringBuffer->nextOutput = ringBuffer->buffer;
}
} }
} }
@ -247,61 +242,36 @@ void *receiveAudioViaUDP(void *args) {
//printf(\n"; //printf(\n";
stdev.addValue(tDiff); stdev.addValue(tDiff);
if (stdev.getSamples() > 500) { if (stdev.getSamples() > 500) {
sharedAudioData->jitter = stdev.getStDev(); sharedAudioData->measuredJitter = stdev.getStDev();
printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), sharedAudioData->jitter); printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), sharedAudioData->measuredJitter);
stdev.reset(); stdev.reset();
} }
} }
AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer; AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer;
int16_t *copyToPointer; if (ringBuffer->endOfLastWrite == NULL) {
bool needsJitterBuffer = ringBuffer->endOfLastWrite == NULL; ringBuffer->endOfLastWrite = ringBuffer->buffer;
short bufferSampleOverlap = 0; } else if (ringBuffer->diffLastWriteNextOutput() > RING_BUFFER_SIZE_SAMPLES - PACKET_LENGTH_SAMPLES) {
std::cout << "NAB: " << ringBuffer->nextOutput - ringBuffer->buffer << "\n";
if (!needsJitterBuffer && ringBuffer->diffLastWriteNextOutput() > RING_BUFFER_SIZE_SAMPLES - PACKET_LENGTH_SAMPLES) { std::cout << "LAW: " << ringBuffer->endOfLastWrite - ringBuffer->buffer << "\n";
std::cout << "D: " << ringBuffer->diffLastWriteNextOutput() << "\n";
std::cout << "Full\n"; std::cout << "Full\n";
needsJitterBuffer = true;
// reset us to started state
ringBuffer->endOfLastWrite = ringBuffer->buffer;
ringBuffer->nextOutput = ringBuffer->buffer;
ringBuffer->started = false;
} }
if (needsJitterBuffer) { int16_t *copyToPointer = ringBuffer->endOfLastWrite;
// we'll need a jitter buffer
// reset the ring buffer and write
copyToPointer = ringBuffer->buffer;
//std::cout << "Writing jitter buffer\n";
} else {
copyToPointer = ringBuffer->endOfLastWrite;
// check for possibility of overlap
bufferSampleOverlap = ringBuffer->bufferOverlap(copyToPointer, PACKET_LENGTH_SAMPLES);
}
if (!bufferSampleOverlap) { // just copy the recieved data to the right spot and then add packet length to previous pointer
if (needsJitterBuffer) { memcpy(copyToPointer, receivedData, PACKET_LENGTH_BYTES);
// we need to inject a jitter buffer ringBuffer->endOfLastWrite += PACKET_LENGTH_SAMPLES;
// add silence for jitter buffer and then the received packet if (ringBuffer->endOfLastWrite == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) {
memset(copyToPointer, 0, JITTER_BUFFER_SAMPLES * sizeof(int16_t)); ringBuffer->endOfLastWrite = ringBuffer->buffer;
memcpy(copyToPointer + JITTER_BUFFER_SAMPLES, receivedData, PACKET_LENGTH_BYTES);
// the end of the write is the pointer to the buffer + packet + jitter buffer
ringBuffer->endOfLastWrite = ringBuffer->buffer + PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES;
ringBuffer->nextOutput = ringBuffer->buffer;
} else {
// no jitter buffer, no overlap
// just copy the recieved data to the right spot and then add packet length to previous pointer
memcpy(copyToPointer, receivedData, PACKET_LENGTH_BYTES);
ringBuffer->endOfLastWrite += PACKET_LENGTH_SAMPLES;
}
} else {
// no jitter buffer, but overlap
// copy to the end, and then from the begining to the overlap
memcpy(copyToPointer, receivedData, (PACKET_LENGTH_SAMPLES - bufferSampleOverlap) * sizeof(int16_t));
memcpy(ringBuffer->buffer, receivedData + (PACKET_LENGTH_SAMPLES - bufferSampleOverlap), bufferSampleOverlap * sizeof(int16_t));
// the end of the write is the amount of overlap
ringBuffer->endOfLastWrite = ringBuffer->buffer + bufferSampleOverlap;
} }
if (LOG_SAMPLE_DELAY) { if (LOG_SAMPLE_DELAY) {
@ -465,7 +435,7 @@ void Audio::render(int screenWidth, int screenHeight)
// Show a Cyan bar with the most recently measured jitter stdev // Show a Cyan bar with the most recently measured jitter stdev
int jitterPels = (float) data->jitter/ ((1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE)) * (float)frameWidth; int jitterPels = (float) data->measuredJitter/ ((1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE)) * (float)frameWidth;
glColor3f(0,1,1); glColor3f(0,1,1);
glBegin(GL_QUADS); glBegin(GL_QUADS);
@ -475,7 +445,7 @@ void Audio::render(int screenWidth, int screenHeight)
glVertex2f(startX + jitterPels - 2, bottomY + 2); glVertex2f(startX + jitterPels - 2, bottomY + 2);
glEnd(); glEnd();
sprintf(out,"%3.1f\n", data->jitter); sprintf(out,"%3.1f\n", data->measuredJitter);
drawtext(startX + jitterPels - 5, topY-10, 0.08, 0, 1, 0, out, 0,1,1); drawtext(startX + jitterPels - 5, topY-10, 0.08, 0, 1, 0, out, 0,1,1);
sprintf(out, "%3.1fms\n", JITTER_BUFFER_LENGTH_MSECS); sprintf(out, "%3.1fms\n", JITTER_BUFFER_LENGTH_MSECS);

View file

@ -27,7 +27,9 @@ AudioData::AudioData(int numberOfSources, int bufferLength) {
averagedLatency = 0.0; averagedLatency = 0.0;
lastCallback.tv_usec = 0; lastCallback.tv_usec = 0;
wasStarved = 0; wasStarved = 0;
jitter = 0; measuredJitter = 0;
jitterBuffer = 0;
} }
AudioData::~AudioData() { AudioData::~AudioData() {

View file

@ -28,7 +28,8 @@ class AudioData {
timeval lastCallback; timeval lastCallback;
float averagedLatency; float averagedLatency;
float jitter; float measuredJitter;
float jitterBuffer;
int wasStarved; int wasStarved;
AudioData(int bufferLength); AudioData(int bufferLength);

View file

@ -10,6 +10,7 @@
AudioRingBuffer::AudioRingBuffer(short ringBufferSamples) { AudioRingBuffer::AudioRingBuffer(short ringBufferSamples) {
ringBufferLengthSamples = ringBufferSamples; ringBufferLengthSamples = ringBufferSamples;
started = false;
endOfLastWrite = NULL; endOfLastWrite = NULL;

View file

@ -17,6 +17,7 @@ class AudioRingBuffer {
int16_t *endOfLastWrite; int16_t *endOfLastWrite;
int16_t *buffer; int16_t *buffer;
short ringBufferLengthSamples; short ringBufferLengthSamples;
bool started;
short diffLastWriteNextOutput(); short diffLastWriteNextOutput();
short bufferOverlap(int16_t *pointer, short addedDistance); short bufferOverlap(int16_t *pointer, short addedDistance);

View file

@ -19,7 +19,7 @@ int delay_size_received[MAX_DELAY_PACKETS];
int next_to_receive = 0; int next_to_receive = 0;
int next_to_send = 0; int next_to_send = 0;
sockaddr_in address, dest_address, spaceserver_address, from; sockaddr_in address, dest_address, domainserver_address, from;
socklen_t fromLength = sizeof( from ); socklen_t fromLength = sizeof( from );
int network_init() int network_init()
@ -65,9 +65,9 @@ int network_init()
dest_address.sin_addr.s_addr = inet_addr(DESTINATION_IP); dest_address.sin_addr.s_addr = inet_addr(DESTINATION_IP);
dest_address.sin_port = htons( (unsigned short) UDP_PORT ); dest_address.sin_port = htons( (unsigned short) UDP_PORT );
spaceserver_address.sin_family = AF_INET; domainserver_address.sin_family = AF_INET;
spaceserver_address.sin_addr.s_addr = inet_addr(SPACESERVER_IP); domainserver_address.sin_addr.s_addr = inet_addr(DOMAINSERVER_IP);
spaceserver_address.sin_port = htons( (unsigned short) SPACESERVER_PORT ); domainserver_address.sin_port = htons( (unsigned short) DOMAINSERVER_PORT );
from.sin_family = AF_INET; from.sin_family = AF_INET;
//from.sin_addr.s_addr = htonl(ip_address); //from.sin_addr.s_addr = htonl(ip_address);
@ -86,16 +86,16 @@ timeval network_send_ping(int handle) {
return check; return check;
} }
int notify_spaceserver(int handle, float x, float y, float z) { int notify_domainserver(int handle, float x, float y, float z) {
char data[100]; char data[100];
sprintf(data, "%f,%f,%f", x, y, z); sprintf(data, "%f,%f,%f", x, y, z);
//std::cout << "sending: " << data << "\n"; //std::cout << "sending: " << data << "\n";
int packet_size = strlen(data); int packet_size = strlen(data);
int sent_bytes = sendto( handle, (const char*)data, packet_size, int sent_bytes = sendto( handle, (const char*)data, packet_size,
0, (sockaddr*)&spaceserver_address, sizeof(sockaddr_in) ); 0, (sockaddr*)&domainserver_address, sizeof(sockaddr_in) );
if ( sent_bytes != packet_size ) if ( sent_bytes != packet_size )
{ {
printf( "failed to send to spaceserver: return value = %d\n", sent_bytes ); printf( "failed to send to domainserver: return value = %d\n", sent_bytes );
return false; return false;
} }
return sent_bytes; return sent_bytes;

View file

@ -21,9 +21,9 @@ const int MAX_PACKET_SIZE = 1500;
const int UDP_PORT = 30001; const int UDP_PORT = 30001;
const char DESTINATION_IP[] = "127.0.0.1"; const char DESTINATION_IP[] = "127.0.0.1";
// Address and port of spaceserver process to advertise other agents // Address and port of domainserver process to advertise other agents
const char SPACESERVER_IP[] = "127.0.0.1"; const char DOMAINSERVER_IP[] = "127.0.0.1";
const int SPACESERVER_PORT = 40000; const int DOMAINSERVER_PORT = 40000;
// Randomly send a ping packet every N packets sent // Randomly send a ping packet every N packets sent
const int PING_PACKET_COUNT = 20; const int PING_PACKET_COUNT = 20;
@ -32,6 +32,6 @@ int network_init();
int network_send(int handle, char * packet_data, int packet_size); int network_send(int handle, char * packet_data, int packet_size);
int network_receive(int handle, in_addr * from_addr, char * packet_data, int delay /*msecs*/); int network_receive(int handle, in_addr * from_addr, char * packet_data, int delay /*msecs*/);
timeval network_send_ping(int handle); timeval network_send_ping(int handle);
int notify_spaceserver(int handle, float x, float y, float z); int notify_domainserver(int handle, float x, float y, float z);
#endif #endif

View file

@ -229,8 +229,8 @@ void Timer(int extra)
glutTimerFunc(1000,Timer,0); glutTimerFunc(1000,Timer,0);
gettimeofday(&timer_start, NULL); gettimeofday(&timer_start, NULL);
// Send a message to the spaceserver telling it we are ALIVE // Send a message to the domainserver telling it we are ALIVE
notify_spaceserver(UDP_socket, location[0], location[1], location[2]); notify_domainserver(UDP_socket, location[0], location[1], location[2]);
} }
@ -782,7 +782,7 @@ void read_network()
ping_msecs = (float)diffclock(&ping_start, &check); ping_msecs = (float)diffclock(&ping_start, &check);
} else if (incoming_packet[0] == 'S') { } else if (incoming_packet[0] == 'S') {
// //
// Message from Spaceserver // Message from domainserver
// //
update_agents(&incoming_packet[1], bytes_recvd - 1); update_agents(&incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'H') { } else if (incoming_packet[0] == 'H') {