mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-04 06:24:41 +02:00
Merge remote-tracking branch 'origin'
This commit is contained in:
commit
9d368adcd2
7 changed files with 70 additions and 134 deletions
|
@ -7,5 +7,9 @@ set(GLM_ROOT_DIR ${CMAKE_SOURCE_DIR}/external)
|
||||||
set(LODEPNG_ROOT_DIR ${CMAKE_SOURCE_DIR}/external/LodePNG)
|
set(LODEPNG_ROOT_DIR ${CMAKE_SOURCE_DIR}/external/LodePNG)
|
||||||
set(PORTAUDIO_DIR ${CMAKE_SOURCE_DIR}/external/portaudio)
|
set(PORTAUDIO_DIR ${CMAKE_SOURCE_DIR}/external/portaudio)
|
||||||
|
|
||||||
|
set(HIFI_SHARED_DIR ${CMAKE_SOURCE_DIR}/shared/src)
|
||||||
|
file(GLOB HIFI_SHARED_SRCS ${HIFI_SHARED_DIR}/*.cpp ${HIFI_SHARED_DIR}/*.h)
|
||||||
|
|
||||||
add_subdirectory(interface)
|
add_subdirectory(interface)
|
||||||
add_subdirectory(domain)
|
add_subdirectory(domain)
|
||||||
|
add_subdirectory(mixer)
|
|
@ -12,9 +12,8 @@ endif (APPLE)
|
||||||
configure_file(InterfaceConfig.h.in ${PROJECT_BINARY_DIR}/includes/InterfaceConfig.h)
|
configure_file(InterfaceConfig.h.in ${PROJECT_BINARY_DIR}/includes/InterfaceConfig.h)
|
||||||
|
|
||||||
file(GLOB INTERFACE_SRCS src/*.cpp src/*.h)
|
file(GLOB INTERFACE_SRCS src/*.cpp src/*.h)
|
||||||
include_directories(${PROJECT_BINARY_DIR}/includes)
|
|
||||||
|
|
||||||
add_executable(interface ${INTERFACE_SRCS})
|
add_executable(interface ${INTERFACE_SRCS} ${HIFI_SHARED_SRCS})
|
||||||
|
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(GLUT REQUIRED)
|
find_package(GLUT REQUIRED)
|
||||||
|
@ -22,6 +21,8 @@ find_package(GLM REQUIRED)
|
||||||
find_package(LodePNG REQUIRED)
|
find_package(LodePNG REQUIRED)
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
|
${PROJECT_BINARY_DIR}/includes
|
||||||
|
${HIFI_SHARED_DIR}
|
||||||
${OPENGL_INCLUDE_DIRS}
|
${OPENGL_INCLUDE_DIRS}
|
||||||
${GLUT_INCLUDE_DIRS}
|
${GLUT_INCLUDE_DIRS}
|
||||||
${GLM_INCLUDE_DIRS}
|
${GLM_INCLUDE_DIRS}
|
||||||
|
@ -41,7 +42,7 @@ ExternalProject_Add(
|
||||||
PREFIX ${PORTAUDIO_PROJ_DIR}
|
PREFIX ${PORTAUDIO_PROJ_DIR}
|
||||||
BINARY_DIR ${PORTAUDIO_PROJ_DIR}/src/portaudio
|
BINARY_DIR ${PORTAUDIO_PROJ_DIR}/src/portaudio
|
||||||
URL ${PORTAUDIO_DIR}/pa_snapshot_020813.tgz
|
URL ${PORTAUDIO_DIR}/pa_snapshot_020813.tgz
|
||||||
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix "${CMAKE_CURRENT_SOURCE_DIR}/${PORTAUDIO_PROJ_DIR}"
|
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix "${PROJECT_BINARY_DIR}/${PORTAUDIO_PROJ_DIR}"
|
||||||
BUILD_COMMAND make
|
BUILD_COMMAND make
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Agent.h"
|
#include "Agent.h"
|
||||||
#include "Head.h"
|
#include "Head.h"
|
||||||
#include "util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
|
|
||||||
// Structure to hold references to other agents that are nearby
|
// Structure to hold references to other agents that are nearby
|
||||||
|
|
|
@ -4,4 +4,11 @@ project(mixer)
|
||||||
|
|
||||||
file(GLOB MIXER_SRCS src/*.cpp src/*.h)
|
file(GLOB MIXER_SRCS src/*.cpp src/*.h)
|
||||||
|
|
||||||
add_executable(mixer ${MIXER_SRCS})
|
add_executable(mixer ${MIXER_SRCS} ${HIFI_SHARED_SRCS})
|
||||||
|
|
||||||
|
include_directories(${HIFI_SHARED_DIR})
|
||||||
|
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
target_link_libraries(interface
|
||||||
|
${CMAKE_THREAD_LIBS_INIT}
|
||||||
|
)
|
|
@ -13,11 +13,12 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include "AudioRingBuffer.h"
|
#include "AudioRingBuffer.h"
|
||||||
|
#include "UDPSocket.h"
|
||||||
|
|
||||||
const int MAX_AGENTS = 1000;
|
const int MAX_AGENTS = 1000;
|
||||||
const int LOGOFF_CHECK_INTERVAL = 1000;
|
const int LOGOFF_CHECK_INTERVAL = 1000;
|
||||||
|
|
||||||
const int UDP_PORT = 55443;
|
const int MIXER_LISTEN_PORT = 55443;
|
||||||
|
|
||||||
const int BUFFER_LENGTH_BYTES = 1024;
|
const int BUFFER_LENGTH_BYTES = 1024;
|
||||||
const int BUFFER_LENGTH_SAMPLES = BUFFER_LENGTH_BYTES / sizeof(int16_t);
|
const int BUFFER_LENGTH_SAMPLES = BUFFER_LENGTH_BYTES / sizeof(int16_t);
|
||||||
|
@ -35,21 +36,17 @@ const long MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
|
||||||
|
|
||||||
const int MAX_SOURCE_BUFFERS = 20;
|
const int MAX_SOURCE_BUFFERS = 20;
|
||||||
|
|
||||||
int16_t* whiteNoiseBuffer;
|
sockaddr_in address, destAddress;
|
||||||
int whiteNoiseLength;
|
socklen_t destLength = sizeof(destAddress);
|
||||||
|
|
||||||
#define ECHO_DEBUG_MODE 0
|
|
||||||
|
|
||||||
sockaddr_in address, dest_address;
|
|
||||||
socklen_t destLength = sizeof(dest_address);
|
|
||||||
|
|
||||||
struct AgentList {
|
struct AgentList {
|
||||||
sockaddr_in agent_addr;
|
char *address;
|
||||||
|
unsigned short port;
|
||||||
bool active;
|
bool active;
|
||||||
timeval time;
|
timeval time;
|
||||||
} agents[MAX_AGENTS];
|
} agents[MAX_AGENTS];
|
||||||
|
|
||||||
int num_agents = 0;
|
int numAgents = 0;
|
||||||
|
|
||||||
AudioRingBuffer *sourceBuffers[MAX_SOURCE_BUFFERS];
|
AudioRingBuffer *sourceBuffers[MAX_SOURCE_BUFFERS];
|
||||||
|
|
||||||
|
@ -64,53 +61,24 @@ double usecTimestamp(timeval *time, double addedUsecs = 0) {
|
||||||
return (time->tv_sec * 1000000.0) + time->tv_usec + addedUsecs;
|
return (time->tv_sec * 1000000.0) + time->tv_usec + addedUsecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
int create_socket()
|
int addAgent(sockaddr_in agentAddress, void *audioData) {
|
||||||
{
|
|
||||||
// Create socket
|
|
||||||
int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
||||||
|
|
||||||
if (handle <= 0) {
|
|
||||||
printf("Failed to create socket: %d\n", handle);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
int network_init()
|
|
||||||
{
|
|
||||||
int handle = create_socket();
|
|
||||||
|
|
||||||
// Bind socket to port
|
|
||||||
address.sin_family = AF_INET;
|
|
||||||
address.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
address.sin_port = htons( (unsigned short) UDP_PORT );
|
|
||||||
|
|
||||||
if (bind(handle, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0) {
|
|
||||||
printf( "failed to bind socket\n" );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
int addAgent(sockaddr_in dest_address, void *audioData) {
|
|
||||||
// Search for agent in list and add if needed
|
// Search for agent in list and add if needed
|
||||||
int is_new = 0;
|
int is_new = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (i = 0; i < num_agents; i++) {
|
for (i = 0; i < numAgents; i++) {
|
||||||
if (dest_address.sin_addr.s_addr == agents[i].agent_addr.sin_addr.s_addr
|
if (strcmp(inet_ntoa(agentAddress.sin_addr), agents[i].address) == 0
|
||||||
&& dest_address.sin_port == agents[i].agent_addr.sin_port) {
|
&& agentAddress.sin_port == agents[i].port) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((i == num_agents) || (agents[i].active == false)) {
|
if ((i == numAgents) || (agents[i].active == false)) {
|
||||||
is_new = 1;
|
is_new = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
agents[i].agent_addr = dest_address;
|
agents[i].address = inet_ntoa(agentAddress.sin_addr);
|
||||||
|
agents[i].port = ntohs(agentAddress.sin_port);
|
||||||
agents[i].active = true;
|
agents[i].active = true;
|
||||||
gettimeofday(&agents[i].time, NULL);
|
gettimeofday(&agents[i].time, NULL);
|
||||||
|
|
||||||
|
@ -131,21 +99,21 @@ int addAgent(sockaddr_in dest_address, void *audioData) {
|
||||||
sourceBuffers[i]->endOfLastWrite = sourceBuffers[i]->buffer;
|
sourceBuffers[i]->endOfLastWrite = sourceBuffers[i]->buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == num_agents) {
|
if (i == numAgents) {
|
||||||
num_agents++;
|
numAgents++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return is_new;
|
return is_new;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct send_buffer_struct {
|
struct sendBufferStruct {
|
||||||
int socket_handle;
|
UDPSocket *audioSocket;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *send_buffer_thread(void *args)
|
void *sendBufferThread(void *args)
|
||||||
{
|
{
|
||||||
struct send_buffer_struct *buffer_args = (struct send_buffer_struct *) args;
|
struct sendBufferStruct *bufferArgs = (struct sendBufferStruct *)args;
|
||||||
int handle = buffer_args->socket_handle;
|
UDPSocket *audioSocket = bufferArgs->audioSocket;
|
||||||
|
|
||||||
int sentBytes;
|
int sentBytes;
|
||||||
int currentFrame = 1;
|
int currentFrame = 1;
|
||||||
|
@ -159,11 +127,8 @@ void *send_buffer_thread(void *args)
|
||||||
while (true) {
|
while (true) {
|
||||||
sentBytes = 0;
|
sentBytes = 0;
|
||||||
|
|
||||||
int sampleOffset = ((currentFrame - 1) * BUFFER_LENGTH_SAMPLES) % whiteNoiseLength;
|
|
||||||
int16_t *noisePointer = whiteNoiseBuffer + sampleOffset;
|
|
||||||
|
|
||||||
for (int wb = 0; wb < BUFFER_LENGTH_SAMPLES; wb++) {
|
for (int wb = 0; wb < BUFFER_LENGTH_SAMPLES; wb++) {
|
||||||
masterMix[wb] = noisePointer[wb];
|
masterMix[wb] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
gettimeofday(&sendTime, NULL);
|
gettimeofday(&sendTime, NULL);
|
||||||
|
@ -193,7 +158,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) {
|
if (diffclock(&agents[a].time, &sendTime) <= LOGOFF_CHECK_INTERVAL) {
|
||||||
|
|
||||||
int16_t *previousOutput = NULL;
|
int16_t *previousOutput = NULL;
|
||||||
|
@ -229,10 +194,7 @@ void *send_buffer_thread(void *args)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sockaddr_in dest_address = agents[a].agent_addr;
|
audioSocket->send(agents[a].address, agents[a].port, clientMix, BUFFER_LENGTH_BYTES);
|
||||||
|
|
||||||
sentBytes = sendto(handle, clientMix, BUFFER_LENGTH_BYTES,
|
|
||||||
0, (sockaddr *) &dest_address, sizeof(dest_address));
|
|
||||||
|
|
||||||
if (sentBytes < BUFFER_LENGTH_BYTES) {
|
if (sentBytes < BUFFER_LENGTH_BYTES) {
|
||||||
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
|
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
|
||||||
|
@ -256,99 +218,61 @@ void *send_buffer_thread(void *args)
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct process_arg_struct {
|
struct processArgStruct {
|
||||||
int16_t *packet_data;
|
int16_t *packetData;
|
||||||
sockaddr_in dest_address;
|
sockaddr_in destAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
void *process_client_packet(void *args)
|
void *processClientPacket(void *args)
|
||||||
{
|
{
|
||||||
struct process_arg_struct *process_args = (struct process_arg_struct *) args;
|
struct processArgStruct *processArgs = (struct processArgStruct *) 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: " <<
|
std::cout << "Added agent: " <<
|
||||||
inet_ntoa(dest_address.sin_addr) << " on " <<
|
inet_ntoa(destAddress.sin_addr) << " on " <<
|
||||||
dest_address.sin_port << "\n";
|
ntohs(destAddress.sin_port) << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_exit(0);
|
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[])
|
int main(int argc, const char * argv[])
|
||||||
{
|
{
|
||||||
timeval now, last_agent_update;
|
timeval lastAgentUpdate;
|
||||||
int received_bytes = 0;
|
int receivedBytes = 0;
|
||||||
|
|
||||||
// read in the workclub white noise file as a base layer of audio
|
// setup our socket
|
||||||
white_noise_buffer_init();
|
UDPSocket audioSocket = UDPSocket(MIXER_LISTEN_PORT);
|
||||||
|
|
||||||
int handle = network_init();
|
gettimeofday(&lastAgentUpdate, NULL);
|
||||||
|
|
||||||
if (!handle) {
|
int16_t packetData[BUFFER_LENGTH_SAMPLES];
|
||||||
std::cout << "Failed to create listening socket.\n";
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
std::cout << "Network Started. Waiting for packets.\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
gettimeofday(&last_agent_update, NULL);
|
|
||||||
|
|
||||||
int16_t packet_data[BUFFER_LENGTH_SAMPLES];
|
|
||||||
|
|
||||||
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
|
for (int b = 0; b < MAX_SOURCE_BUFFERS; b++) {
|
||||||
sourceBuffers[b] = new AudioRingBuffer(10 * BUFFER_LENGTH_SAMPLES);
|
sourceBuffers[b] = new AudioRingBuffer(10 * BUFFER_LENGTH_SAMPLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct send_buffer_struct send_buffer_args;
|
struct sendBufferStruct sendBufferArgs;
|
||||||
send_buffer_args.socket_handle = handle;
|
sendBufferArgs.audioSocket = &audioSocket;
|
||||||
|
|
||||||
pthread_t buffer_send_thread;
|
pthread_t bufferSendThread;
|
||||||
pthread_create(&buffer_send_thread, NULL, send_buffer_thread, (void *)&send_buffer_args);
|
pthread_create(&bufferSendThread, NULL, sendBufferThread, (void *)&sendBufferArgs);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
received_bytes = recvfrom(handle, (int16_t*)packet_data, BUFFER_LENGTH_BYTES,
|
if(audioSocket.receive(packetData, &receivedBytes)) {
|
||||||
0, (sockaddr*)&dest_address, &destLength);
|
struct processArgStruct args;
|
||||||
if (ECHO_DEBUG_MODE) {
|
args.packetData = packetData;
|
||||||
sendto(handle, packet_data, BUFFER_LENGTH_BYTES,
|
args.destAddress = destAddress;
|
||||||
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_t clientProcessThread;
|
||||||
pthread_create(&client_process_thread, NULL, process_client_packet, (void *)&args);
|
pthread_create(&clientProcessThread, NULL, processClientPacket, (void *)&args);
|
||||||
pthread_join(client_process_thread, NULL);
|
pthread_join(clientProcessThread, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_join(buffer_send_thread, NULL);
|
pthread_join(bufferSendThread, NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue