From aa9f4f966e6ce3334ecf26c297128b02cf1df535 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 21 Feb 2013 15:43:52 -0800 Subject: [PATCH] allow switchover to new audio mixer received from audio server --- interface/src/Audio.cpp | 21 ++++++++++----------- interface/src/Audio.h | 6 +----- interface/src/AudioData.cpp | 2 +- interface/src/AudioData.h | 5 +++-- interface/src/main.cpp | 5 +++++ mixer/src/main.cpp | 8 ++++---- shared/src/AgentList.cpp | 35 ++++++++++++++++++++++++----------- shared/src/AgentList.h | 1 + 8 files changed, 49 insertions(+), 34 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index a5bb02d41f..e8dd588a5f 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -84,8 +84,13 @@ int audioCallback (const void *inputBuffer, // int16_t *inputRight = ((int16_t **) inputBuffer)[1]; if (inputLeft != NULL) { - if (data->mixerAddress != NULL) { - data->audioSocket->send(data->mixerAddress, data->mixerPort, (void *)inputLeft, BUFFER_LENGTH_BYTES); + + if (data->mixerAddress != 0) { + sockaddr_in audioMixerSocket; + audioMixerSocket.sin_family = AF_INET; + audioMixerSocket.sin_addr.s_addr = data->mixerAddress; + audioMixerSocket.sin_port = data->mixerPort; + data->audioSocket->send((sockaddr *)&audioMixerSocket, (void *)inputLeft, BUFFER_LENGTH_BYTES); } // @@ -162,15 +167,9 @@ int audioCallback (const void *inputBuffer, return paContinue; } -void Audio::updateMixerParams(char *newAddress, unsigned short newPort) { - if (audioData->mixerAddress == NULL) { - audioData->mixerAddress = new char[255]; - } - - strcpy(audioData->mixerAddress, newAddress); - audioData->mixerPort = newPort; - - std::cout << "Audio Mixer now at " << audioData->mixerAddress << ":" << newPort << ".\n"; +void Audio::updateMixerParams(in_addr_t newMixerAddress, in_port_t newMixerPort) { + audioData->mixerAddress = newMixerAddress; + audioData->mixerPort = newMixerPort; } struct AudioRecThreadStruct { diff --git a/interface/src/Audio.h b/interface/src/Audio.h index 5cbe869e98..c9f2e55cfc 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -23,7 +23,7 @@ public: void render(int screenWidth, int screenHeight); void getInputLoudness(float * lastLoudness, float * averageLoudness); - void updateMixerParams(char *mixerAddress, unsigned short mixerPort); + void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort); // terminates audio I/O bool terminate(); @@ -35,10 +35,6 @@ private: // protects constructor so that public init method is used Audio(); - // store current mixer address and port - char *mixerAddress; - int mixerPort; - // hold potential error returned from PortAudio functions PaError paError; diff --git a/interface/src/AudioData.cpp b/interface/src/AudioData.cpp index 9ef1cbc8ed..afba708db4 100644 --- a/interface/src/AudioData.cpp +++ b/interface/src/AudioData.cpp @@ -9,7 +9,7 @@ #include "AudioData.h" AudioData::AudioData(int bufferLength) { - mixerAddress = NULL; + mixerAddress = 0; mixerPort = 0; samplesToQueue = new int16_t[bufferLength / sizeof(int16_t)]; diff --git a/interface/src/AudioData.h b/interface/src/AudioData.h index 26468d492b..e5094f8263 100644 --- a/interface/src/AudioData.h +++ b/interface/src/AudioData.h @@ -22,8 +22,9 @@ class AudioData { int16_t *samplesToQueue; - char *mixerAddress; - unsigned short mixerPort; + // store current mixer address and port + in_addr_t mixerAddress; + in_port_t mixerPort; timeval lastCallback; float averagedLatency; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 28f7050f42..bdf6681c1a 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -876,6 +876,10 @@ void attachNewHeadToAgent(Agent *newAgent) { } } +void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { + audio.updateMixerParams(newMixerAddress, newMixerPort); +} + int main(int argc, char** argv) { struct ifaddrs * ifAddrStruct=NULL; @@ -930,6 +934,7 @@ int main(int argc, char** argv) // the callback for our instance of AgentList is attachNewHeadToAgent agentList.linkedDataCreateCallback = &attachNewHeadToAgent; + agentList.audioMixerSocketUpdate = &audioMixerUpdate; // create thread for receipt of data via UDP pthread_t networkReceiveThread; diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index 94cccdddf0..eb125e1831 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -224,14 +224,14 @@ int addAgent(sockaddr_in *newAddress, void *audioData) { void *reportAliveToDS(void *args) { timeval lastSend, now; - char output[100]; + unsigned char output[7]; while (true) { gettimeofday(&lastSend, NULL); - sprintf(output, "%c %f,%f,%f,54.241.92.53 %hd", 'M', 0.f, 0.f, 0.f, MIXER_LISTEN_PORT); - int packetSize = strlen(output); - audioSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, output, packetSize); + *output = 'M'; + packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); + audioSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); gettimeofday(&now, NULL); diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 09ed0ecfba..84036373ef 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -11,6 +11,7 @@ AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) { linkedDataCreateCallback = NULL; + audioMixerSocketUpdate = NULL; } AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) { @@ -123,6 +124,14 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, newAgent.activeSocket = newAgent.localSocket; } + if (newAgent.type == 'M' && audioMixerSocketUpdate != NULL) { + // this is an audio mixer + // for now that means we need to tell the audio class + // to use the local socket information the domain server gave us + sockaddr_in *localSocketIn = (sockaddr_in *)localSocket; + audioMixerSocketUpdate(localSocketIn->sin_addr.s_addr, localSocketIn->sin_port); + } + std::cout << "Added agent - " << &newAgent << "\n"; agents.push_back(newAgent); @@ -136,7 +145,9 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, void AgentList::broadcastToAgents(char *broadcastData, size_t dataBytes) { for(std::vector::iterator agent = agents.begin(); agent != agents.end(); agent++) { - if (agent->activeSocket != NULL) { + // for now assume we only want to send to other interface clients + // until the Audio class uses the AgentList + if (agent->activeSocket != NULL && agent->type == 'I') { // we know which socket is good for this agent, send there agentSocket.send((sockaddr *)agent->activeSocket, broadcastData, dataBytes); } @@ -145,17 +156,19 @@ void AgentList::broadcastToAgents(char *broadcastData, size_t dataBytes) { void AgentList::pingAgents() { + char payload[] = "P"; + for(std::vector::iterator agent = agents.begin(); agent != agents.end(); agent++) { - char payload[] = "P"; - - if (agent->activeSocket != NULL) { - // we know which socket is good for this agent, send there - agentSocket.send((sockaddr *)agent->activeSocket, payload, 1); - } else { - // ping both of the sockets for the agent so we can figure out - // which socket we can use - agentSocket.send(agent->publicSocket, payload, 1); - agentSocket.send(agent->localSocket, payload, 1); + if (agent->type == 'I') { + if (agent->activeSocket != NULL) { + // we know which socket is good for this agent, send there + agentSocket.send((sockaddr *)agent->activeSocket, payload, 1); + } else { + // ping both of the sockets for the agent so we can figure out + // which socket we can use + agentSocket.send(agent->publicSocket, payload, 1); + agentSocket.send(agent->localSocket, payload, 1); + } } } } diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index c4b646e9fe..c04a18bf8c 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -22,6 +22,7 @@ class AgentList { AgentList(int socketListenPort); std::vector agents; void(*linkedDataCreateCallback)(Agent *); + void(*audioMixerSocketUpdate)(in_addr_t, in_port_t); UDPSocket* getAgentSocket();