mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 17:35:08 +02:00
switch to dynamic sleep for send buffer repititions
This commit is contained in:
parent
0cfc2dae52
commit
6f904204fb
2 changed files with 58 additions and 85 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
socket
|
||||
sftp-config.json
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
*.raw
|
140
socket.cpp
140
socket.cpp
|
@ -49,10 +49,10 @@ struct SourceBuffer {
|
|||
bool transmitted;
|
||||
} sourceBuffers[MAX_SOURCE_BUFFERS];
|
||||
|
||||
double diffclock(timeval clock1, timeval clock2)
|
||||
double diffclock(timeval *clock1, timeval *clock2)
|
||||
{
|
||||
double diffms = (clock2.tv_sec - clock1.tv_sec) * 1000.0;
|
||||
diffms += (clock2.tv_usec - clock1.tv_usec) / 1000.0; // us to ms
|
||||
double diffms = (clock2->tv_sec - clock1->tv_sec) * 1000.0;
|
||||
diffms += (clock2->tv_usec - clock1->tv_usec) / 1000.0; // us to ms
|
||||
return diffms;
|
||||
}
|
||||
|
||||
|
@ -120,18 +120,6 @@ int addAgent(sockaddr_in dest_address, void *audioData) {
|
|||
return is_new;
|
||||
}
|
||||
|
||||
void update_agent_list(timeval now) {
|
||||
int i;
|
||||
// std::cout << "Checking agent list" << "\n";
|
||||
for (i = 0; i < num_agents; i++) {
|
||||
if ((diffclock(agents[i].time, now) > LOGOFF_CHECK_INTERVAL) &&
|
||||
agents[i].active) {
|
||||
std::cout << "Expired Agent #" << i << "\n";
|
||||
agents[i].active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct send_buffer_struct {
|
||||
int socket_handle;
|
||||
};
|
||||
|
@ -143,9 +131,7 @@ void *send_buffer_thread(void *args)
|
|||
|
||||
int sentBytes;
|
||||
int currentFrame = 1;
|
||||
timeval startTime;
|
||||
timeval lastSend;
|
||||
timeval now;
|
||||
timeval startTime, sendTime, now;
|
||||
|
||||
int16_t *clientMix = new int16_t[BUFFER_LENGTH_SAMPLES];
|
||||
int16_t *masterMix = new int16_t[BUFFER_LENGTH_SAMPLES];
|
||||
|
@ -153,52 +139,55 @@ void *send_buffer_thread(void *args)
|
|||
gettimeofday(&startTime, NULL);
|
||||
|
||||
while (true) {
|
||||
gettimeofday(&now, NULL);
|
||||
sentBytes = 0;
|
||||
|
||||
while (usecTimestamp(&startTime, (currentFrame * BUFFER_SEND_INTERVAL_USECS)) <= usecTimestamp(&now)) {
|
||||
sentBytes = 0;
|
||||
int sampleOffset = ((currentFrame - 1) * BUFFER_LENGTH_SAMPLES) % whiteNoiseLength;
|
||||
memcpy(masterMix, whiteNoiseBuffer + sampleOffset, BUFFER_LENGTH_BYTES);
|
||||
|
||||
std::cout << "The difference was " << diffclock(lastSend, now) << "ms.\n";
|
||||
gettimeofday(&sendTime, NULL);
|
||||
|
||||
int sampleOffset = ((currentFrame - 1) * BUFFER_LENGTH_SAMPLES) % whiteNoiseLength;
|
||||
for (int a = 0; a < num_agents; a++) {
|
||||
if (diffclock(&agents[a].time, &sendTime) <= LOGOFF_CHECK_INTERVAL) {
|
||||
memcpy(clientMix, masterMix, BUFFER_LENGTH_BYTES);
|
||||
|
||||
memcpy(masterMix, whiteNoiseBuffer + sampleOffset, BUFFER_LENGTH_BYTES);
|
||||
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
|
||||
if (b != a && !sourceBuffers[b].transmitted) {
|
||||
for (int s = 0; s < BUFFER_LENGTH_SAMPLES; s++) {
|
||||
// we have source buffer data for this sample
|
||||
int mixSample = clientMix[s] + sourceBuffers[b].sourceAudioData[s];
|
||||
|
||||
int sampleToAdd = std::max(mixSample, MIN_SAMPLE_VALUE);
|
||||
sampleToAdd = std::min(sampleToAdd, MAX_SAMPLE_VALUE);
|
||||
|
||||
for (int i = 0; i < num_agents; i++) {
|
||||
if (agents[i].active) {
|
||||
memcpy(clientMix, masterMix, BUFFER_LENGTH_BYTES);
|
||||
|
||||
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
|
||||
if (b != i && !sourceBuffers[b].transmitted) {
|
||||
for (int s = 0; s < BUFFER_LENGTH_SAMPLES; s++) {
|
||||
// we have source buffer data for this sample
|
||||
int mixSample = clientMix[s] + sourceBuffers[b].sourceAudioData[s];
|
||||
|
||||
int sampleToAdd = std::max(mixSample, MIN_SAMPLE_VALUE);
|
||||
sampleToAdd = std::min(sampleToAdd, MAX_SAMPLE_VALUE);
|
||||
|
||||
clientMix[s] = sampleToAdd;
|
||||
}
|
||||
|
||||
sourceBuffers[b].transmitted = true;
|
||||
clientMix[s] = sampleToAdd;
|
||||
}
|
||||
|
||||
sourceBuffers[b].transmitted = true;
|
||||
}
|
||||
|
||||
sockaddr_in dest_address = agents[i].agent_addr;
|
||||
|
||||
sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
||||
|
||||
if (sentBytes < BUFFER_LENGTH_BYTES) {
|
||||
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
gettimeofday(&lastSend, NULL);
|
||||
currentFrame++;
|
||||
sockaddr_in dest_address = agents[a].agent_addr;
|
||||
|
||||
sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
||||
|
||||
if (sentBytes < BUFFER_LENGTH_BYTES) {
|
||||
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
double usecToSleep = usecTimestamp(&startTime, (currentFrame * BUFFER_SEND_INTERVAL_USECS)) - usecTimestamp(&now);
|
||||
|
||||
if (usecToSleep > 0) {
|
||||
usleep(usecToSleep);
|
||||
} else {
|
||||
std::cout << "NOT SLEEPING!";
|
||||
}
|
||||
|
||||
currentFrame++;
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
|
@ -233,7 +222,7 @@ bool different_clients(sockaddr_in addr1, sockaddr_in addr2)
|
|||
|
||||
void white_noise_buffer_init() {
|
||||
// open a pointer to the audio file
|
||||
FILE *whiteNoiseFile = fopen("wild.raw", "r");
|
||||
FILE *whiteNoiseFile = fopen("opera.raw", "r");
|
||||
|
||||
// get length of file
|
||||
std::fseek(whiteNoiseFile, 0, SEEK_END);
|
||||
|
@ -265,13 +254,6 @@ int main(int argc, const char * argv[])
|
|||
std::cout << "Network Started. Waiting for packets.\n";
|
||||
}
|
||||
|
||||
// Set socket as non-blocking
|
||||
int nonBlocking = 1;
|
||||
if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1) {
|
||||
printf( "failed to set non-blocking socket\n" );
|
||||
return false;
|
||||
}
|
||||
|
||||
gettimeofday(&last_agent_update, NULL);
|
||||
|
||||
int16_t packet_data[BUFFER_LENGTH_SAMPLES];
|
||||
|
@ -286,31 +268,21 @@ int main(int argc, const char * argv[])
|
|||
pthread_t buffer_send_thread;
|
||||
pthread_create(&buffer_send_thread, NULL, send_buffer_thread, (void *)&send_buffer_args);
|
||||
|
||||
while (1) {
|
||||
while (true) {
|
||||
received_bytes = recvfrom(handle, (int16_t*)packet_data, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr*)&dest_address, &destLength);
|
||||
0, (sockaddr*)&dest_address, &destLength);
|
||||
if (ECHO_DEBUG_MODE) {
|
||||
sendto(handle, packet_data, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
||||
} else {
|
||||
struct process_arg_struct args;
|
||||
args.packet_data = packet_data;
|
||||
args.dest_address = dest_address;
|
||||
|
||||
if (received_bytes > 0) {
|
||||
if (ECHO_DEBUG_MODE) {
|
||||
sendto(handle, packet_data, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
||||
} else {
|
||||
struct process_arg_struct args;
|
||||
args.packet_data = packet_data;
|
||||
args.dest_address = dest_address;
|
||||
|
||||
pthread_t client_process_thread;
|
||||
pthread_create(&client_process_thread, NULL, process_client_packet, (void *)&args);
|
||||
pthread_join(client_process_thread, NULL);
|
||||
}
|
||||
pthread_t client_process_thread;
|
||||
pthread_create(&client_process_thread, NULL, process_client_packet, (void *)&args);
|
||||
pthread_join(client_process_thread, NULL);
|
||||
}
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
if (diffclock(last_agent_update, now) > LOGOFF_CHECK_INTERVAL) {
|
||||
gettimeofday(&last_agent_update, NULL);
|
||||
update_agent_list(last_agent_update);
|
||||
}
|
||||
}
|
||||
|
||||
pthread_join(buffer_send_thread, NULL);
|
||||
|
|
Loading…
Reference in a new issue