mirror of
https://github.com/overte-org/overte.git
synced 2025-04-16 05:48:39 +02:00
rename socket.cpp to main.cpp, camelcase variables
This commit is contained in:
parent
c45eeb2a51
commit
4aca25297b
1 changed files with 36 additions and 74 deletions
|
@ -35,21 +35,16 @@ const long MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
|
|||
|
||||
const int MAX_SOURCE_BUFFERS = 20;
|
||||
|
||||
int16_t* whiteNoiseBuffer;
|
||||
int whiteNoiseLength;
|
||||
|
||||
#define ECHO_DEBUG_MODE 0
|
||||
|
||||
sockaddr_in address, dest_address;
|
||||
socklen_t destLength = sizeof(dest_address);
|
||||
sockaddr_in address, destAddress;
|
||||
socklen_t destLength = sizeof(destAddress);
|
||||
|
||||
struct AgentList {
|
||||
sockaddr_in agent_addr;
|
||||
sockaddr_in agentAddr;
|
||||
bool active;
|
||||
timeval time;
|
||||
} agents[MAX_AGENTS];
|
||||
|
||||
int num_agents = 0;
|
||||
int numAgents = 0;
|
||||
|
||||
AudioRingBuffer *sourceBuffers[MAX_SOURCE_BUFFERS];
|
||||
|
||||
|
@ -99,18 +94,18 @@ int addAgent(sockaddr_in dest_address, void *audioData) {
|
|||
int is_new = 0;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < num_agents; i++) {
|
||||
if (dest_address.sin_addr.s_addr == agents[i].agent_addr.sin_addr.s_addr
|
||||
&& dest_address.sin_port == agents[i].agent_addr.sin_port) {
|
||||
for (i = 0; i < numAgents; i++) {
|
||||
if (dest_address.sin_addr.s_addr == agents[i].agentAddr.sin_addr.s_addr
|
||||
&& dest_address.sin_port == agents[i].agentAddr.sin_port) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((i == num_agents) || (agents[i].active == false)) {
|
||||
if ((i == numAgents) || (agents[i].active == false)) {
|
||||
is_new = 1;
|
||||
}
|
||||
|
||||
agents[i].agent_addr = dest_address;
|
||||
agents[i].agentAddr = dest_address;
|
||||
agents[i].active = true;
|
||||
gettimeofday(&agents[i].time, NULL);
|
||||
|
||||
|
@ -131,8 +126,8 @@ int addAgent(sockaddr_in dest_address, void *audioData) {
|
|||
sourceBuffers[i]->endOfLastWrite = sourceBuffers[i]->buffer;
|
||||
}
|
||||
|
||||
if (i == num_agents) {
|
||||
num_agents++;
|
||||
if (i == numAgents) {
|
||||
numAgents++;
|
||||
}
|
||||
|
||||
return is_new;
|
||||
|
@ -159,11 +154,8 @@ void *send_buffer_thread(void *args)
|
|||
while (true) {
|
||||
sentBytes = 0;
|
||||
|
||||
int sampleOffset = ((currentFrame - 1) * BUFFER_LENGTH_SAMPLES) % whiteNoiseLength;
|
||||
int16_t *noisePointer = whiteNoiseBuffer + sampleOffset;
|
||||
|
||||
for (int wb = 0; wb < BUFFER_LENGTH_SAMPLES; wb++) {
|
||||
masterMix[wb] = noisePointer[wb];
|
||||
masterMix[wb] = 0;
|
||||
}
|
||||
|
||||
gettimeofday(&sendTime, NULL);
|
||||
|
@ -193,7 +185,7 @@ void *send_buffer_thread(void *args)
|
|||
}
|
||||
}
|
||||
|
||||
for (int a = 0; a < num_agents; a++) {
|
||||
for (int a = 0; a < numAgents; a++) {
|
||||
if (diffclock(&agents[a].time, &sendTime) <= LOGOFF_CHECK_INTERVAL) {
|
||||
|
||||
int16_t *previousOutput = NULL;
|
||||
|
@ -229,10 +221,10 @@ void *send_buffer_thread(void *args)
|
|||
|
||||
}
|
||||
|
||||
sockaddr_in dest_address = agents[a].agent_addr;
|
||||
sockaddr_in destAddress = agents[a].agentAddr;
|
||||
|
||||
sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
||||
0, (sockaddr *) &destAddress, sizeof(destAddress));
|
||||
|
||||
if (sentBytes < BUFFER_LENGTH_BYTES) {
|
||||
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
|
||||
|
@ -257,56 +249,30 @@ void *send_buffer_thread(void *args)
|
|||
}
|
||||
|
||||
struct process_arg_struct {
|
||||
int16_t *packet_data;
|
||||
sockaddr_in dest_address;
|
||||
int16_t *packetData;
|
||||
sockaddr_in destAddress;
|
||||
};
|
||||
|
||||
void *process_client_packet(void *args)
|
||||
{
|
||||
struct process_arg_struct *process_args = (struct process_arg_struct *) args;
|
||||
struct process_arg_struct *processArgs = (struct process_arg_struct *) args;
|
||||
|
||||
sockaddr_in dest_address = process_args->dest_address;
|
||||
sockaddr_in destAddress = processArgs->destAddress;
|
||||
|
||||
if (addAgent(dest_address, process_args->packet_data)) {
|
||||
if (addAgent(destAddress, processArgs->packetData)) {
|
||||
std::cout << "Added agent: " <<
|
||||
inet_ntoa(dest_address.sin_addr) << " on " <<
|
||||
dest_address.sin_port << "\n";
|
||||
inet_ntoa(destAddress.sin_addr) << " on " <<
|
||||
destAddress.sin_port << "\n";
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
bool different_clients(sockaddr_in addr1, sockaddr_in addr2)
|
||||
{
|
||||
return addr1.sin_addr.s_addr != addr2.sin_addr.s_addr ||
|
||||
(addr1.sin_addr.s_addr == addr2.sin_addr.s_addr &&
|
||||
addr1.sin_port != addr2.sin_port);
|
||||
}
|
||||
|
||||
void white_noise_buffer_init() {
|
||||
// open a pointer to the audio file
|
||||
FILE *whiteNoiseFile = fopen("opera.raw", "r");
|
||||
|
||||
// get length of file
|
||||
std::fseek(whiteNoiseFile, 0, SEEK_END);
|
||||
whiteNoiseLength = std::ftell(whiteNoiseFile) / sizeof(int16_t);
|
||||
std::rewind(whiteNoiseFile);
|
||||
|
||||
// read that amount of samples from the file
|
||||
whiteNoiseBuffer = new int16_t[whiteNoiseLength];
|
||||
std::fread(whiteNoiseBuffer, sizeof(int16_t), whiteNoiseLength, whiteNoiseFile);
|
||||
|
||||
// close it
|
||||
std::fclose(whiteNoiseFile);
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
timeval now, last_agent_update;
|
||||
int received_bytes = 0;
|
||||
timeval lastAgentUpdate;
|
||||
int receivedBytes = 0;
|
||||
|
||||
// read in the workclub white noise file as a base layer of audio
|
||||
white_noise_buffer_init();
|
||||
|
||||
int handle = network_init();
|
||||
|
||||
|
@ -317,9 +283,9 @@ int main(int argc, const char * argv[])
|
|||
std::cout << "Network Started. Waiting for packets.\n";
|
||||
}
|
||||
|
||||
gettimeofday(&last_agent_update, NULL);
|
||||
gettimeofday(&lastAgentUpdate, NULL);
|
||||
|
||||
int16_t packet_data[BUFFER_LENGTH_SAMPLES];
|
||||
int16_t packetData[BUFFER_LENGTH_SAMPLES];
|
||||
|
||||
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
|
||||
sourceBuffers[b] = new AudioRingBuffer(10 * BUFFER_LENGTH_SAMPLES);
|
||||
|
@ -332,20 +298,16 @@ int main(int argc, const char * argv[])
|
|||
pthread_create(&buffer_send_thread, NULL, send_buffer_thread, (void *)&send_buffer_args);
|
||||
|
||||
while (true) {
|
||||
received_bytes = recvfrom(handle, (int16_t*)packet_data, BUFFER_LENGTH_BYTES,
|
||||
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;
|
||||
|
||||
pthread_t client_process_thread;
|
||||
pthread_create(&client_process_thread, NULL, process_client_packet, (void *)&args);
|
||||
pthread_join(client_process_thread, NULL);
|
||||
}
|
||||
receivedBytes = recvfrom(handle, (int16_t*)packetData, BUFFER_LENGTH_BYTES,
|
||||
0, (sockaddr*)&destAddress, &destLength);
|
||||
|
||||
struct process_arg_struct args;
|
||||
args.packetData = packetData;
|
||||
args.destAddress = destAddress;
|
||||
|
||||
pthread_t client_process_thread;
|
||||
pthread_create(&client_process_thread, NULL, process_client_packet, (void *)&args);
|
||||
pthread_join(client_process_thread, NULL);
|
||||
}
|
||||
|
||||
pthread_join(buffer_send_thread, NULL);
|
Loading…
Reference in a new issue