This commit is contained in:
Philip Rosedale 2013-05-15 21:17:34 -06:00
commit 29889725ef
20 changed files with 262 additions and 268 deletions

View file

@ -94,7 +94,7 @@ int main(int argc, const char* argv[]) {
sockaddr* agentAddress = new sockaddr;
// make sure our agent socket is non-blocking
agentList->getAgentSocket().setBlocking(false);
agentList->getAgentSocket()->setBlocking(false);
int nextFrame = 0;
timeval startTime;
@ -114,10 +114,10 @@ int main(int argc, const char* argv[]) {
if (agentBuffer->getEndOfLastWrite()) {
if (!agentBuffer->isStarted()
&& agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) {
printf("Held back buffer for agent with ID %d.\n", agent->getAgentId());
printf("Held back buffer for agent with ID %d.\n", agent->getAgentID());
agentBuffer->setShouldBeAddedToMix(false);
} else if (agentBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
printf("Buffer from agent with ID %d starved.\n", agent->getAgentId());
printf("Buffer from agent with ID %d starved.\n", agent->getAgentID());
agentBuffer->setStarted(false);
agentBuffer->setShouldBeAddedToMix(false);
} else {
@ -149,9 +149,7 @@ int main(int argc, const char* argv[]) {
int numSamplesDelay = 0;
float weakChannelAmplitudeRatio = 1.f;
if (otherAgent != agent) {
printf("DEBUG: The bearing for this agent is %f\n", agentRingBuffer->getBearing());
if (otherAgent != agent) {
Position agentPosition = agentRingBuffer->getPosition();
Position otherAgentPosition = otherAgentBuffer->getPosition();
@ -256,7 +254,7 @@ int main(int argc, const char* argv[]) {
}
memcpy(clientPacket + 1, clientSamples, sizeof(clientSamples));
agentList->getAgentSocket().send(agent->getPublicSocket(), clientPacket, BUFFER_LENGTH_BYTES + 1);
agentList->getAgentSocket()->send(agent->getPublicSocket(), clientPacket, BUFFER_LENGTH_BYTES + 1);
}
// push forward the next output pointers for any audio buffers we used
@ -274,7 +272,7 @@ int main(int argc, const char* argv[]) {
}
// pull any new audio data from agents off of the network stack
while (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
while (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) {
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO || packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO) {
char agentType = (packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO)
? AGENT_TYPE_AVATAR

View file

@ -37,7 +37,7 @@
const int AVATAR_LISTEN_PORT = 55444;
unsigned char *addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) {
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId());
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentID());
AvatarData *agentData = (AvatarData *)agentToAdd->getLinkedData();
currentPosition += agentData->getBroadcastData(currentPosition);
@ -72,7 +72,7 @@ int main(int argc, const char* argv[]) {
uint16_t agentID = 0;
while (true) {
if (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
if (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) {
switch (packetData[0]) {
case PACKET_HEADER_HEAD_DATA:
// grab the agent ID from the packet
@ -93,7 +93,7 @@ int main(int argc, const char* argv[]) {
}
}
agentList->getAgentSocket().send(agentAddress, broadcastPacket, currentBufferPosition - broadcastPacket);
agentList->getAgentSocket()->send(agentAddress, broadcastPacket, currentBufferPosition - broadcastPacket);
break;
case PACKET_HEADER_DOMAIN:

View file

@ -50,7 +50,7 @@ int lastActiveCount = 0;
unsigned char* addAgentToBroadcastPacket(unsigned char* currentPosition, Agent* agentToAdd) {
*currentPosition++ = agentToAdd->getType();
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId());
currentPosition += packAgentId(currentPosition, agentToAdd->getAgentID());
currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket());
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
@ -97,7 +97,7 @@ int main(int argc, const char * argv[])
uint16_t packetAgentID = 0;
while (true) {
if (agentList->getAgentSocket().receive((sockaddr *)&agentPublicAddress, packetData, &receivedBytes) &&
if (agentList->getAgentSocket()->receive((sockaddr *)&agentPublicAddress, packetData, &receivedBytes) &&
(packetData[0] == PACKET_HEADER_DOMAIN_RFD || packetData[0] == PACKET_HEADER_DOMAIN_LIST_REQUEST)) {
std::map<char, Agent *> newestSoloAgents;
@ -149,7 +149,7 @@ int main(int argc, const char * argv[])
agent->setLastHeardMicrostamp(timeNow);
// grab the ID for this agent so we can send it back with the packet
packetAgentID = agent->getAgentId();
packetAgentID = agent->getAgentID();
if (packetData[0] == PACKET_HEADER_DOMAIN_RFD
&& memchr(SOLO_AGENT_TYPES, agentType, sizeof(SOLO_AGENT_TYPES))) {
@ -169,7 +169,7 @@ int main(int argc, const char * argv[])
currentBufferPos += packAgentId(currentBufferPos, packetAgentID);
// send the constructed list back to this agent
agentList->getAgentSocket().send((sockaddr*) &agentPublicAddress,
agentList->getAgentSocket()->send((sockaddr*) &agentPublicAddress,
broadcastPacket,
(currentBufferPos - startPointer) + 1);
}

View file

@ -7,6 +7,7 @@
//
#include <sys/time.h>
#include <cstring>
#include <SharedUtil.h>
#include <AgentTypes.h>
@ -34,10 +35,6 @@ const float EVE_PELVIS_HEIGHT = 0.565925f;
const float AUDIO_INJECT_PROXIMITY = 0.4f;
bool stopReceiveAgentDataThread;
bool injectAudioThreadRunning = false;
int TEMP_AUDIO_LISTEN_PORT = 55439;
UDPSocket audioSocket(TEMP_AUDIO_LISTEN_PORT);
void *receiveAgentData(void *args) {
sockaddr senderAddress;
@ -47,7 +44,7 @@ void *receiveAgentData(void *args) {
AgentList* agentList = AgentList::getInstance();
while (!::stopReceiveAgentDataThread) {
if (agentList->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) {
if (agentList->getAgentSocket()->receive(&senderAddress, incomingPacket, &bytesReceived)) {
switch (incomingPacket[0]) {
case PACKET_HEADER_BULK_AVATAR_DATA:
// this is the positional data for other agents
@ -67,29 +64,6 @@ void *receiveAgentData(void *args) {
return NULL;
}
void *injectAudio(void *args) {
::injectAudioThreadRunning = true;
AudioInjector* eveAudioInjector = (AudioInjector *)args;
// look for an audio mixer in our agent list
Agent* audioMixer = AgentList::getInstance()->soloAgentOfType(AGENT_TYPE_AUDIO_MIXER);
if (audioMixer) {
// until the audio mixer is setup for ping-reply, activate the public socket if it's not active
if (!audioMixer->getActiveSocket()) {
audioMixer->activatePublicSocket();
}
// we have an active audio mixer we can send data to
eveAudioInjector->injectAudio(&::audioSocket, audioMixer->getActiveSocket());
}
::injectAudioThreadRunning = false;
pthread_exit(0);
return NULL;
}
void createAvatarDataForAgent(Agent* agent) {
if (!agent->getLinkedData()) {
agent->setLinkedData(new AvatarData());
@ -138,6 +112,14 @@ int main(int argc, const char* argv[]) {
// lower Eve's volume by setting the attentuation modifier (this is a value out of 255)
eveAudioInjector.setAttenuationModifier(190);
// pass the agentList UDPSocket pointer to the audio injector
eveAudioInjector.setInjectorSocket(agentList->getAgentSocket());
// set the position of the audio injector
float injectorPosition[3];
memcpy(injectorPosition, &eve.getPosition(), sizeof(injectorPosition));
eveAudioInjector.setPosition(injectorPosition);
// register the callback for agent data creation
agentList->linkedDataCreateCallback = createAvatarDataForAgent;
@ -146,8 +128,6 @@ int main(int argc, const char* argv[]) {
timeval thisSend;
double numMicrosecondsSleep = 0;
pthread_t injectAudioThread;
int handStateTimer = 0;
@ -167,10 +147,10 @@ int main(int argc, const char* argv[]) {
packetPosition += eve.getBroadcastData(packetPosition);
// use the UDPSocket instance attached to our agent list to send avatar data to mixer
agentList->getAgentSocket().send(avatarMixer->getActiveSocket(), broadcastPacket, packetPosition - broadcastPacket);
}
agentList->getAgentSocket()->send(avatarMixer->getActiveSocket(), broadcastPacket, packetPosition - broadcastPacket);
}
if (!::injectAudioThreadRunning) {
if (!eveAudioInjector.isInjectingAudio()) {
// enumerate the other agents to decide if one is close enough that eve should talk
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AvatarData* avatarData = (AvatarData*) agent->getLinkedData();
@ -180,7 +160,20 @@ int main(int argc, const char* argv[]) {
float squareDistance = glm::dot(tempVector, tempVector);
if (squareDistance <= AUDIO_INJECT_PROXIMITY) {
pthread_create(&injectAudioThread, NULL, injectAudio, (void*) &eveAudioInjector);
// look for an audio mixer in our agent list
Agent* audioMixer = AgentList::getInstance()->soloAgentOfType(AGENT_TYPE_AUDIO_MIXER);
if (audioMixer) {
// until the audio mixer is setup for ping-reply, activate the public socket if it's not active
if (!audioMixer->getActiveSocket()) {
audioMixer->activatePublicSocket();
}
eveAudioInjector.setDestinationSocket(audioMixer->getActiveSocket());
// we have an active audio mixer we can send data to
eveAudioInjector.threadInjectionOfAudio();
}
}
}
}

View file

@ -104,6 +104,7 @@ int main(int argc, char* argv[]) {
mixerSocket.sin_family = AF_INET;
mixerSocket.sin_addr.s_addr = inet_addr(EC2_WEST_AUDIO_SERVER);
mixerSocket.sin_port = htons((uint16_t)AUDIO_UDP_LISTEN_PORT);
if (processParameters(argc, argv)) {
if (::sourceAudioFile == NULL) {
@ -111,6 +112,8 @@ int main(int argc, char* argv[]) {
exit(-1);
} else {
AudioInjector injector(sourceAudioFile);
injector.setInjectorSocket(&streamSocket);
injector.setDestinationSocket((sockaddr*) &mixerSocket);
injector.setPosition(::floatArguments);
injector.setBearing(*(::floatArguments + 3));
@ -120,7 +123,7 @@ int main(int argc, char* argv[]) {
int usecDelay = 0;
while (true) {
injector.injectAudio(&streamSocket, (sockaddr*) &mixerSocket);
injector.injectAudio();
if (!::loopAudio) {
delay = randFloatInRange(::sleepIntervalMin, ::sleepIntervalMax);

Binary file not shown.

BIN
interface/interface.icns Normal file

Binary file not shown.

View file

@ -170,7 +170,7 @@ Application::Application(int& argc, char** argv) :
AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort);
_enableNetworkThread = !cmdOptionExists(argc, constArgv, "--nonblocking");
if (!_enableNetworkThread) {
AgentList::getInstance()->getAgentSocket().setBlocking(false);
AgentList::getInstance()->getAgentSocket()->setBlocking(false);
}
const char* domainIP = getCmdOption(argc, constArgv, "--domain");
@ -2010,7 +2010,7 @@ void* Application::networkReceive(void* args) {
app->_wantToKillLocalVoxels = false;
}
if (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, app->_incomingPacket, &bytesReceived)) {
if (AgentList::getInstance()->getAgentSocket()->receive(&senderAddress, app->_incomingPacket, &bytesReceived)) {
app->_packetCount++;
app->_bytesCount += bytesReceived;

View file

@ -59,7 +59,7 @@ public:
void wheelEvent(QWheelEvent* event);
const Avatar& getAvatar() const { return _myAvatar; }
Avatar* getAvatar() { return &_myAvatar; }
private slots:

View file

@ -85,7 +85,7 @@ int audioCallback (const void* inputBuffer,
AgentList* agentList = AgentList::getInstance();
Application* interface = (Application*) QCoreApplication::instance();
Avatar interfaceAvatar = interface->getAvatar();
Avatar* interfaceAvatar = interface->getAvatar();
int16_t *inputLeft = ((int16_t **) inputBuffer)[0];
int16_t *outputLeft = ((int16_t **) outputBuffer)[0];
@ -134,14 +134,14 @@ int audioCallback (const void* inputBuffer,
unsigned char *currentPacketPtr = dataPacket + 1;
// memcpy the three float positions
memcpy(currentPacketPtr, &interfaceAvatar.getHeadPosition(), sizeof(float) * 3);
memcpy(currentPacketPtr, &interfaceAvatar->getHeadPosition(), sizeof(float) * 3);
currentPacketPtr += (sizeof(float) * 3);
// tell the mixer not to add additional attenuation to our source
*(currentPacketPtr++) = 255;
// memcpy the corrected render yaw
float correctedYaw = fmodf(-1 * interfaceAvatar.getAbsoluteHeadYaw(), 360);
float correctedYaw = fmodf(-1 * interfaceAvatar->getAbsoluteHeadYaw(), 360);
if (correctedYaw > 180) {
correctedYaw -= 360;
@ -161,7 +161,7 @@ int audioCallback (const void* inputBuffer,
// copy the audio data to the last BUFFER_LENGTH_BYTES bytes of the data packet
memcpy(currentPacketPtr, inputLeft, BUFFER_LENGTH_BYTES);
agentList->getAgentSocket().send(audioMixer->getActiveSocket(), dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
agentList->getAgentSocket()->send(audioMixer->getActiveSocket(), dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
}
}
@ -199,7 +199,7 @@ int audioCallback (const void* inputBuffer,
// if we haven't fired off the flange effect, check if we should
// TODO: lastMeasuredHeadYaw is now relative to body - check if this still works.
int lastYawMeasured = fabsf(interfaceAvatar.getLastMeasuredHeadYaw());
int lastYawMeasured = fabsf(interfaceAvatar->getLastMeasuredHeadYaw());
if (!::samplesLeftForFlange && lastYawMeasured > MIN_FLANGE_EFFECT_THRESHOLD) {
// we should flange for one second

View file

@ -19,7 +19,7 @@ const int PAIRING_SERVER_PORT = 7247;
void PairingHandler::sendPairRequest() {
// grab the agent socket from the AgentList singleton
UDPSocket *agentSocket = &AgentList::getInstance()->getAgentSocket();
UDPSocket *agentSocket = AgentList::getInstance()->getAgentSocket();
// prepare the pairing request packet

View file

@ -22,79 +22,70 @@
using shared_lib::printLog;
int unpackAgentId(unsigned char *packedData, uint16_t *agentId) {
int unpackAgentId(unsigned char* packedData, uint16_t* agentId) {
memcpy(agentId, packedData, sizeof(uint16_t));
return sizeof(uint16_t);
}
int packAgentId(unsigned char *packStore, uint16_t agentId) {
int packAgentId(unsigned char* packStore, uint16_t agentId) {
memcpy(packStore, &agentId, sizeof(uint16_t));
return sizeof(uint16_t);
}
Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId) :
Agent::Agent(sockaddr* publicSocket, sockaddr* localSocket, char type, uint16_t agentID) :
_type(type),
_agentID(agentID),
_wakeMicrostamp(usecTimestampNow()),
_lastHeardMicrostamp(usecTimestampNow()),
_activeSocket(NULL),
_bytesReceivedMovingAverage(NULL),
_linkedData(NULL),
_isAlive(true)
{
if (agentPublicSocket != NULL) {
publicSocket = new sockaddr;
memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr));
if (publicSocket) {
_publicSocket = new sockaddr(*publicSocket);
} else {
publicSocket = NULL;
_publicSocket = NULL;
}
if (agentLocalSocket != NULL) {
localSocket = new sockaddr;
memcpy(localSocket, agentLocalSocket, sizeof(sockaddr));
if (localSocket) {
_localSocket = new sockaddr(*localSocket);
} else {
localSocket = NULL;
_localSocket = NULL;
}
type = agentType;
agentId = thisAgentId;
_wakeMicrostamp = usecTimestampNow();
_lastHeardMicrostamp = usecTimestampNow();
activeSocket = NULL;
linkedData = NULL;
_bytesReceivedMovingAverage = NULL;
}
Agent::Agent(const Agent &otherAgent) {
_isAlive = otherAgent._isAlive;
if (otherAgent.publicSocket != NULL) {
publicSocket = new sockaddr;
memcpy(publicSocket, otherAgent.publicSocket, sizeof(sockaddr));
Agent::Agent(const Agent &otherAgent) :
_type(otherAgent._type),
_agentID(otherAgent._agentID),
_wakeMicrostamp(otherAgent._wakeMicrostamp),
_lastHeardMicrostamp(otherAgent._lastHeardMicrostamp),
_isAlive(otherAgent._isAlive)
{
if (otherAgent._publicSocket) {
_publicSocket = new sockaddr(*otherAgent._localSocket);
} else {
publicSocket = NULL;
_publicSocket = NULL;
}
if (otherAgent.localSocket != NULL) {
localSocket = new sockaddr;
memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr));
if (otherAgent._localSocket) {
_localSocket = new sockaddr(*otherAgent._localSocket);
} else {
localSocket = NULL;
_localSocket = NULL;
}
agentId = otherAgent.agentId;
if (otherAgent.activeSocket == otherAgent.publicSocket) {
activeSocket = publicSocket;
} else if (otherAgent.activeSocket == otherAgent.localSocket) {
activeSocket = localSocket;
if (otherAgent._activeSocket == otherAgent._publicSocket) {
_activeSocket = _publicSocket;
} else if (otherAgent._activeSocket == otherAgent._localSocket) {
_activeSocket = _localSocket;
} else {
activeSocket = NULL;
_activeSocket = NULL;
}
_wakeMicrostamp = otherAgent._wakeMicrostamp;
_lastHeardMicrostamp = otherAgent._lastHeardMicrostamp;
type = otherAgent.type;
if (otherAgent.linkedData != NULL) {
linkedData = otherAgent.linkedData->clone();
if (otherAgent._linkedData) {
_linkedData = otherAgent._linkedData->clone();
} else {
linkedData = NULL;
_linkedData = NULL;
}
if (otherAgent._bytesReceivedMovingAverage != NULL) {
@ -114,115 +105,69 @@ void Agent::swap(Agent &first, Agent &second) {
using std::swap;
swap(first._isAlive, second._isAlive);
swap(first.publicSocket, second.publicSocket);
swap(first.localSocket, second.localSocket);
swap(first.activeSocket, second.activeSocket);
swap(first.type, second.type);
swap(first.linkedData, second.linkedData);
swap(first.agentId, second.agentId);
swap(first._publicSocket, second._publicSocket);
swap(first._localSocket, second._localSocket);
swap(first._activeSocket, second._activeSocket);
swap(first._type, second._type);
swap(first._linkedData, second._linkedData);
swap(first._agentID, second._agentID);
swap(first._wakeMicrostamp, second._wakeMicrostamp);
swap(first._lastHeardMicrostamp, second._lastHeardMicrostamp);
swap(first._bytesReceivedMovingAverage, second._bytesReceivedMovingAverage);
}
Agent::~Agent() {
delete publicSocket;
delete localSocket;
delete linkedData;
delete _publicSocket;
delete _localSocket;
delete _linkedData;
delete _bytesReceivedMovingAverage;
}
char Agent::getType() const {
return type;
}
// Names of Agent Types
const char* AGENT_TYPE_NAME_DOMAIN = "Domain";
const char* AGENT_TYPE_NAME_VOXEL = "Voxel Server";
const char* AGENT_TYPE_NAME_INTERFACE = "Client Interface";
const char* AGENT_TYPE_NAME_AUDIO_MIXER = "Audio Mixer";
const char* AGENT_TYPE_NAME_AVATAR_MIXER = "Avatar Mixer";
const char* AGENT_TYPE_NAME_AUDIO_INJECTOR = "Audio Injector";
const char* AGENT_TYPE_NAME_UNKNOWN = "Unknown";
const char* Agent::getTypeName() const {
const char* name = AGENT_TYPE_NAME_UNKNOWN;
switch (this->type) {
switch (this->_type) {
case AGENT_TYPE_DOMAIN:
name = AGENT_TYPE_NAME_DOMAIN;
break;
return AGENT_TYPE_NAME_DOMAIN;
case AGENT_TYPE_VOXEL:
name = AGENT_TYPE_NAME_VOXEL;
break;
return AGENT_TYPE_NAME_VOXEL;
case AGENT_TYPE_AVATAR:
name = AGENT_TYPE_NAME_INTERFACE;
break;
return AGENT_TYPE_NAME_INTERFACE;
case AGENT_TYPE_AUDIO_MIXER:
name = AGENT_TYPE_NAME_AUDIO_MIXER;
break;
return AGENT_TYPE_NAME_AUDIO_MIXER;
case AGENT_TYPE_AVATAR_MIXER:
name = AGENT_TYPE_NAME_AVATAR_MIXER;
break;
return AGENT_TYPE_NAME_AVATAR_MIXER;
case AGENT_TYPE_AUDIO_INJECTOR:
return AGENT_TYPE_NAME_AUDIO_INJECTOR;
default:
return AGENT_TYPE_NAME_UNKNOWN;
}
return name;
}
void Agent::setType(char newType) {
type = newType;
}
uint16_t Agent::getAgentId() {
return agentId;
}
void Agent::setAgentId(uint16_t thisAgentId) {
agentId = thisAgentId;
}
sockaddr* Agent::getPublicSocket() {
return publicSocket;
}
void Agent::setPublicSocket(sockaddr *newSocket) {
publicSocket = newSocket;
}
sockaddr* Agent::getLocalSocket() {
return localSocket;
}
void Agent::setLocalSocket(sockaddr *newSocket) {
publicSocket = newSocket;
}
sockaddr* Agent::getActiveSocket() {
return activeSocket;
}
void Agent::activateLocalSocket() {
activeSocket = localSocket;
_activeSocket = _localSocket;
}
void Agent::activatePublicSocket() {
activeSocket = publicSocket;
}
AgentData* Agent::getLinkedData() {
return linkedData;
}
void Agent::setLinkedData(AgentData *newData) {
linkedData = newData;
_activeSocket = _publicSocket;
}
bool Agent::operator==(const Agent& otherAgent) {
return matches(otherAgent.publicSocket, otherAgent.localSocket, otherAgent.type);
return matches(otherAgent._publicSocket, otherAgent._localSocket, otherAgent._type);
}
bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) {
// checks if two agent objects are the same agent (same type + local + public address)
return type == otherAgentType
&& socketMatch(publicSocket, otherPublicSocket)
&& socketMatch(localSocket, otherLocalSocket);
return _type == otherAgentType
&& socketMatch(_publicSocket, otherPublicSocket)
&& socketMatch(_localSocket, otherLocalSocket);
}
void Agent::recordBytesReceived(int bytesReceived) {
@ -252,15 +197,15 @@ float Agent::getAverageKilobitsPerSecond() {
void Agent::printLog(Agent const& agent) {
char publicAddressBuffer[16] = {'\0'};
unsigned short publicAddressPort = loadBufferWithSocketInfo(publicAddressBuffer, agent.publicSocket);
unsigned short publicAddressPort = loadBufferWithSocketInfo(publicAddressBuffer, agent._publicSocket);
//char localAddressBuffer[16] = {'\0'};
//unsigned short localAddressPort = loadBufferWithSocketInfo(localAddressBuffer, agent.localSocket);
::printLog("# %d %s (%c) @ %s:%d\n",
agent.agentId,
agent._agentID,
agent.getTypeName(),
agent.type,
agent._type,
publicAddressBuffer,
publicAddressPort);
}

View file

@ -23,20 +23,20 @@
class Agent {
public:
Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId);
Agent(sockaddr* publicSocket, sockaddr* localSocket, char type, uint16_t agentID);
Agent(const Agent &otherAgent);
~Agent();
Agent& operator=(Agent otherAgent);
bool operator==(const Agent& otherAgent);
bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType);
bool matches(sockaddr* otherPublicSocket, sockaddr* otherLocalSocket, char otherAgentType);
char getType() const;
char getType() const { return _type; }
void setType(char type) { _type = type; }
const char* getTypeName() const;
void setType(char newType);
uint16_t getAgentId();
void setAgentId(uint16_t thisAgentId);
uint16_t getAgentID() const { return _agentID; }
void setAgentID(uint16_t agentID) { _agentID = agentID;}
double getWakeMicrostamp() const { return _wakeMicrostamp; }
void setWakeMicrostamp(double wakeMicrostamp) { _wakeMicrostamp = wakeMicrostamp; }
@ -44,17 +44,18 @@ public:
double getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
void setLastHeardMicrostamp(double lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
sockaddr* getPublicSocket();
void setPublicSocket(sockaddr *newSocket);
sockaddr* getLocalSocket();
void setLocalSocket(sockaddr *newSocket);
sockaddr* getActiveSocket();
sockaddr* getPublicSocket() const { return _publicSocket; }
void setPublicSocket(sockaddr* publicSocket) { _publicSocket = publicSocket; }
sockaddr* getLocalSocket() const { return _localSocket; }
void setLocalSocket(sockaddr* localSocket) { _localSocket = localSocket; }
sockaddr* getActiveSocket() const { return _activeSocket; }
void activatePublicSocket();
void activateLocalSocket();
AgentData* getLinkedData();
void setLinkedData(AgentData *newData);
AgentData* getLinkedData() const { return _linkedData; }
void setLinkedData(AgentData* linkedData) { _linkedData = linkedData; }
bool isAlive() const { return _isAlive; };
void setAlive(bool isAlive) { _isAlive = isAlive; };
@ -67,13 +68,15 @@ public:
private:
void swap(Agent &first, Agent &second);
sockaddr *publicSocket, *localSocket, *activeSocket;
char type;
uint16_t agentId;
char _type;
uint16_t _agentID;
double _wakeMicrostamp;
double _lastHeardMicrostamp;
sockaddr* _publicSocket;
sockaddr* _localSocket;
sockaddr* _activeSocket;
SimpleMovingAverage* _bytesReceivedMovingAverage;
AgentData* linkedData;
AgentData* _linkedData;
bool _isAlive;
};

View file

@ -78,10 +78,6 @@ AgentList::~AgentList() {
pthread_mutex_destroy(&mutex);
}
UDPSocket& AgentList::getAgentSocket() {
return agentSocket;
}
unsigned int AgentList::getSocketListenPort() {
return socketListenPort;
}
@ -184,7 +180,7 @@ Agent* AgentList::agentWithAddress(sockaddr *senderAddress) {
Agent* AgentList::agentWithID(uint16_t agentID) {
for(AgentList::iterator agent = begin(); agent != end(); agent++) {
if (agent->getAgentId() == agentID) {
if (agent->getAgentID() == agentID) {
return &(*agent);
}
}
@ -337,8 +333,8 @@ void *pingUnknownAgents(void *args) {
&& (agent->getPublicSocket() != NULL && agent->getLocalSocket() != NULL)) {
// ping both of the sockets for the agent so we can figure out
// which socket we can use
agentList->getAgentSocket().send(agent->getPublicSocket(), &PACKET_HEADER_PING, 1);
agentList->getAgentSocket().send(agent->getLocalSocket(), &PACKET_HEADER_PING, 1);
agentList->getAgentSocket()->send(agent->getPublicSocket(), &PACKET_HEADER_PING, 1);
agentList->getAgentSocket()->send(agent->getLocalSocket(), &PACKET_HEADER_PING, 1);
}
}
@ -433,7 +429,7 @@ void *checkInWithDomainServer(void *args) {
packSocket(packet + 2, localAddress, htons(parentAgentList->getSocketListenPort()));
parentAgentList->getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, packet, sizeof(packet));
parentAgentList->getAgentSocket()->send(DOMAIN_IP, DOMAINSERVER_PORT, packet, sizeof(packet));
packet[0] = PACKET_HEADER_DOMAIN_LIST_REQUEST;

View file

@ -49,7 +49,7 @@ public:
int size() { return _numAgents; }
UDPSocket& getAgentSocket();
UDPSocket* getAgentSocket() { return &agentSocket; }
void lock() { pthread_mutex_lock(&mutex); }
void unlock() { pthread_mutex_unlock(&mutex); }

View file

@ -21,30 +21,43 @@ const float SAMPLE_RATE = 22050.0f;
const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES / SAMPLE_RATE) * 1000000;
AudioInjector::AudioInjector(const char* filename) :
_numTotalBytesAudio(0),
_position(),
_bearing(0),
_attenuationModifier(255) {
_attenuationModifier(255),
_indexOfNextSlot(0),
_isInjectingAudio(false)
{
std::fstream sourceFile;
sourceFile.open(filename, std::ios::in | std::ios::binary);
sourceFile.seekg(0, std::ios::end);
_numTotalBytesAudio = sourceFile.tellg();
if (_numTotalBytesAudio == -1) {
int totalBytes = sourceFile.tellg();
if (totalBytes == -1) {
printf("Error reading audio data from file %s\n", filename);
_audioSampleArray = NULL;
} else {
printf("Read %d bytes from audio file\n", _numTotalBytesAudio);
printf("Read %d bytes from audio file\n", totalBytes);
sourceFile.seekg(0, std::ios::beg);
long sizeOfShortArray = _numTotalBytesAudio / 2;
_audioSampleArray = new int16_t[sizeOfShortArray];
_numTotalSamples = totalBytes / 2;
_audioSampleArray = new int16_t[_numTotalSamples];
sourceFile.read((char *)_audioSampleArray, _numTotalBytesAudio);
sourceFile.read((char *)_audioSampleArray, totalBytes);
}
}
AudioInjector::AudioInjector(int maxNumSamples) :
_numTotalSamples(maxNumSamples),
_position(),
_bearing(0),
_attenuationModifier(255),
_indexOfNextSlot(0),
_isInjectingAudio(false)
{
_audioSampleArray = new int16_t[maxNumSamples];
memset(_audioSampleArray, 0, _numTotalSamples * sizeof(int16_t));
}
AudioInjector::~AudioInjector() {
delete[] _audioSampleArray;
}
@ -55,8 +68,25 @@ void AudioInjector::setPosition(float* position) {
_position[2] = position[2];
}
void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destinationSocket) const {
if (_audioSampleArray != NULL) {
void AudioInjector::addSample(const int16_t sample) {
if (_indexOfNextSlot != _numTotalSamples) {
// only add this sample if we actually have space for it
_audioSampleArray[_indexOfNextSlot++] = sample;
}
}
void AudioInjector::addSamples(int16_t* sampleBuffer, int numSamples) {
if (_audioSampleArray + _indexOfNextSlot + numSamples <= _audioSampleArray + (_numTotalSamples / sizeof(int16_t))) {
// only copy the audio from the sample buffer if there's space
memcpy(_audioSampleArray + _indexOfNextSlot, sampleBuffer, numSamples * sizeof(int16_t));
_indexOfNextSlot += numSamples;
}
}
void AudioInjector::injectAudio() {
if (_audioSampleArray) {
_isInjectingAudio = true;
timeval startTime;
// one byte for header, 3 positional floats, 1 bearing float, 1 attenuation modifier byte
@ -77,24 +107,38 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination
memcpy(currentPacketPtr, &_bearing, sizeof(float));
currentPacketPtr += sizeof(float);
for (int i = 0; i < _numTotalBytesAudio; i += BUFFER_LENGTH_BYTES) {
for (int i = 0; i < _numTotalSamples; i += BUFFER_LENGTH_SAMPLES) {
gettimeofday(&startTime, NULL);
int numBytesToCopy = BUFFER_LENGTH_BYTES;
int numSamplesToCopy = BUFFER_LENGTH_SAMPLES;
if (_numTotalBytesAudio - i < BUFFER_LENGTH_BYTES) {
numBytesToCopy = _numTotalBytesAudio - i;
memset(currentPacketPtr + numBytesToCopy, 0, BUFFER_LENGTH_BYTES - numBytesToCopy);
if (_numTotalSamples - i < BUFFER_LENGTH_SAMPLES) {
numSamplesToCopy = _numTotalSamples - i;
memset(currentPacketPtr + numSamplesToCopy, 0, BUFFER_LENGTH_BYTES - (numSamplesToCopy * sizeof(int16_t)));
}
memcpy(currentPacketPtr, _audioSampleArray + (i / 2), numBytesToCopy);
memcpy(currentPacketPtr, _audioSampleArray + i, numSamplesToCopy * sizeof(int16_t));
injectorSocket->send(destinationSocket, dataPacket, sizeof(dataPacket));
_injectorSocket->send(&_destinationSocket, dataPacket, sizeof(dataPacket));
double usecToSleep = BUFFER_SEND_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&startTime));
if (usecToSleep > 0) {
usleep(usecToSleep);
}
}
_isInjectingAudio = false;
}
}
void* injectAudioViaThread(void* args) {
AudioInjector* parentInjector = (AudioInjector*) args;
parentInjector->injectAudio();
pthread_exit(0);
}
void AudioInjector::threadInjectionOfAudio() {
pthread_t audioInjectThread;
pthread_create(&audioInjectThread, NULL, injectAudioViaThread, (void*) this);
}

View file

@ -17,19 +17,32 @@
class AudioInjector {
public:
AudioInjector(const char* filename);
AudioInjector(int maxNumSamples);
~AudioInjector();
bool isInjectingAudio() const { return _isInjectingAudio; }
void setPosition(float* position);
void setBearing(float bearing) { _bearing = bearing; }
void setAttenuationModifier(unsigned char attenuationModifier) { _attenuationModifier = attenuationModifier; }
void setInjectorSocket(UDPSocket* injectorSocket) { _injectorSocket = injectorSocket; }
void setDestinationSocket(sockaddr* destinationSocket) { _destinationSocket = *destinationSocket; }
void injectAudio(UDPSocket* injectorSocket, sockaddr* destinationSocket) const;
void addSample(const int16_t sample);
void addSamples(int16_t* sampleBuffer, int numSamples);
void injectAudio();
void threadInjectionOfAudio();
private:
int16_t* _audioSampleArray;
int _numTotalBytesAudio;
int _numTotalSamples;
float _position[3];
float _bearing;
unsigned char _attenuationModifier;
int _indexOfNextSlot;
UDPSocket* _injectorSocket;
sockaddr _destinationSocket;
bool _isInjectingAudio;
};
#endif /* defined(__hifi__AudioInjector__) */

View file

@ -27,7 +27,7 @@ using shared_lib::printLog;
sockaddr_in destSockaddr, senderAddress;
bool socketMatch(sockaddr *first, sockaddr *second) {
bool socketMatch(sockaddr* first, sockaddr* second) {
if (first != NULL && second != NULL) {
// utility function that indicates if two sockets are equivalent
@ -51,7 +51,7 @@ bool socketMatch(sockaddr *first, sockaddr *second) {
}
}
int packSocket(unsigned char *packStore, in_addr_t inAddress, in_port_t networkOrderPort) {
int packSocket(unsigned char* packStore, in_addr_t inAddress, in_port_t networkOrderPort) {
packStore[0] = inAddress >> 24;
packStore[1] = inAddress >> 16;
packStore[2] = inAddress >> 8;
@ -63,12 +63,12 @@ int packSocket(unsigned char *packStore, in_addr_t inAddress, in_port_t networkO
return 6; // could be dynamically more if we need IPv6
}
int packSocket(unsigned char *packStore, sockaddr *socketToPack) {
return packSocket(packStore, ((sockaddr_in *) socketToPack)->sin_addr.s_addr, ((sockaddr_in *) socketToPack)->sin_port);
int packSocket(unsigned char* packStore, sockaddr* socketToPack) {
return packSocket(packStore, ((sockaddr_in*) socketToPack)->sin_addr.s_addr, ((sockaddr_in*) socketToPack)->sin_port);
}
int unpackSocket(unsigned char *packedData, sockaddr *unpackDestSocket) {
sockaddr_in *destinationSocket = (sockaddr_in *) unpackDestSocket;
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket) {
sockaddr_in* destinationSocket = (sockaddr_in*) unpackDestSocket;
destinationSocket->sin_addr.s_addr = (packedData[0] << 24) + (packedData[1] << 16) + (packedData[2] << 8) + packedData[3];
destinationSocket->sin_port = (packedData[4] << 8) + packedData[5];
return 6; // this could be more if we ever need IPv6
@ -76,8 +76,8 @@ int unpackSocket(unsigned char *packedData, sockaddr *unpackDestSocket) {
int getLocalAddress() {
// get this agent's local address so we can pass that to DS
struct ifaddrs * ifAddrStruct = NULL;
struct ifaddrs * ifa = NULL;
struct ifaddrs* ifAddrStruct = NULL;
struct ifaddrs* ifa = NULL;
int family;
int localAddress = 0;
@ -107,9 +107,9 @@ int getLocalAddress() {
return localAddress;
}
unsigned short loadBufferWithSocketInfo(char *addressBuffer, sockaddr *socket) {
unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket) {
if (socket != NULL) {
char *copyBuffer = inet_ntoa(((sockaddr_in*) socket)->sin_addr);
char* copyBuffer = inet_ntoa(((sockaddr_in*) socket)->sin_addr);
memcpy(addressBuffer, copyBuffer, strlen(copyBuffer));
return htons(((sockaddr_in*) socket)->sin_port);
} else {
@ -204,13 +204,12 @@ void UDPSocket::setBlocking(bool blocking) {
}
// Receive data on this socket with retrieving address of sender
bool UDPSocket::receive(void *receivedData, ssize_t *receivedBytes) {
return receive((sockaddr *)&senderAddress, receivedData, receivedBytes);
bool UDPSocket::receive(void* receivedData, ssize_t* receivedBytes) const {
return receive((sockaddr*) &senderAddress, receivedData, receivedBytes);
}
// Receive data on this socket with the address of the sender
bool UDPSocket::receive(sockaddr *recvAddress, void *receivedData, ssize_t *receivedBytes) {
bool UDPSocket::receive(sockaddr* recvAddress, void* receivedData, ssize_t* receivedBytes) const {
#ifdef _WIN32
int addressSize = sizeof(*recvAddress);
@ -223,7 +222,7 @@ bool UDPSocket::receive(sockaddr *recvAddress, void *receivedData, ssize_t *rece
return (*receivedBytes > 0);
}
int UDPSocket::send(sockaddr *destAddress, const void *data, size_t byteLength) {
int UDPSocket::send(sockaddr* destAddress, const void* data, size_t byteLength) const {
// send data via UDP
int sent_bytes = sendto(handle, (const char*)data, byteLength,
0, (sockaddr *) destAddress, sizeof(sockaddr_in));
@ -236,7 +235,7 @@ int UDPSocket::send(sockaddr *destAddress, const void *data, size_t byteLength)
return sent_bytes;
}
int UDPSocket::send(char * destAddress, int destPort, const void *data, size_t byteLength) {
int UDPSocket::send(char* destAddress, int destPort, const void* data, size_t byteLength) const {
// change address and port on reusable global to passed variables
destSockaddr.sin_addr.s_addr = inet_addr(destAddress);

View file

@ -19,26 +19,26 @@
#define MAX_BUFFER_LENGTH_BYTES 1500
class UDPSocket {
public:
UDPSocket(int listening_port);
~UDPSocket();
bool init();
void setBlocking(bool blocking);
bool isBlocking() { return blocking; }
int send(sockaddr *destAddress, const void *data, size_t byteLength);
int send(char *destAddress, int destPort, const void *data, size_t byteLength);
bool receive(void *receivedData, ssize_t *receivedBytes);
bool receive(sockaddr *recvAddress, void *receivedData, ssize_t *receivedBytes);
private:
int handle;
bool blocking;
public:
UDPSocket(int listening_port);
~UDPSocket();
bool init();
void setBlocking(bool blocking);
bool isBlocking() { return blocking; }
int send(sockaddr* destAddress, const void* data, size_t byteLength) const;
int send(char* destAddress, int destPort, const void* data, size_t byteLength) const;
bool receive(void* receivedData, ssize_t* receivedBytes) const;
bool receive(sockaddr* recvAddress, void* receivedData, ssize_t* receivedBytes) const;
private:
int handle;
bool blocking;
};
bool socketMatch(sockaddr *first, sockaddr *second);
int packSocket(unsigned char *packStore, in_addr_t inAddress, in_port_t networkOrderPort);
int packSocket(unsigned char *packStore, sockaddr *socketToPack);
int unpackSocket(unsigned char *packedData, sockaddr *unpackDestSocket);
bool socketMatch(sockaddr* first, sockaddr* second);
int packSocket(unsigned char* packStore, in_addr_t inAddress, in_port_t networkOrderPort);
int packSocket(unsigned char* packStore, sockaddr* socketToPack);
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket);
int getLocalAddress();
unsigned short loadBufferWithSocketInfo(char *addressBuffer, sockaddr *socket);
unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket);
#endif /* defined(__interface__UDPSocket__) */

View file

@ -168,7 +168,7 @@ void resInVoxelDistributor(AgentList* agentList,
if (agentData->getAvailable() >= bytesWritten) {
agentData->writeToPacket(&tempOutputBuffer[0], bytesWritten);
} else {
agentList->getAgentSocket().send(agent->getActiveSocket(),
agentList->getAgentSocket()->send(agent->getActiveSocket(),
agentData->getPacket(), agentData->getPacketLength());
trueBytesSent += agentData->getPacketLength();
truePacketsSent++;
@ -178,8 +178,8 @@ void resInVoxelDistributor(AgentList* agentList,
}
} else {
if (agentData->isPacketWaiting()) {
agentList->getAgentSocket().send(agent->getActiveSocket(),
agentData->getPacket(), agentData->getPacketLength());
agentList->getAgentSocket()->send(agent->getActiveSocket(),
agentData->getPacket(), agentData->getPacketLength());
trueBytesSent += agentData->getPacketLength();
truePacketsSent++;
agentData->resetVoxelPacket();
@ -190,7 +190,7 @@ void resInVoxelDistributor(AgentList* agentList,
}
// send the environment packet
int envPacketLength = environmentData.getBroadcastData(tempOutputBuffer);
agentList->getAgentSocket().send(agent->getActiveSocket(), tempOutputBuffer, envPacketLength);
agentList->getAgentSocket()->send(agent->getActiveSocket(), tempOutputBuffer, envPacketLength);
trueBytesSent += envPacketLength;
truePacketsSent++;
@ -288,7 +288,7 @@ void deepestLevelVoxelDistributor(AgentList* agentList,
if (agentData->getAvailable() >= bytesWritten) {
agentData->writeToPacket(&tempOutputBuffer[0], bytesWritten);
} else {
agentList->getAgentSocket().send(agent->getActiveSocket(),
agentList->getAgentSocket()->send(agent->getActiveSocket(),
agentData->getPacket(), agentData->getPacketLength());
trueBytesSent += agentData->getPacketLength();
truePacketsSent++;
@ -298,7 +298,7 @@ void deepestLevelVoxelDistributor(AgentList* agentList,
}
} else {
if (agentData->isPacketWaiting()) {
agentList->getAgentSocket().send(agent->getActiveSocket(),
agentList->getAgentSocket()->send(agent->getActiveSocket(),
agentData->getPacket(), agentData->getPacketLength());
trueBytesSent += agentData->getPacketLength();
truePacketsSent++;
@ -310,7 +310,7 @@ void deepestLevelVoxelDistributor(AgentList* agentList,
}
// send the environment packet
int envPacketLength = environmentData.getBroadcastData(tempOutputBuffer);
agentList->getAgentSocket().send(agent->getActiveSocket(), tempOutputBuffer, envPacketLength);
agentList->getAgentSocket()->send(agent->getActiveSocket(), tempOutputBuffer, envPacketLength);
trueBytesSent += envPacketLength;
truePacketsSent++;
@ -501,7 +501,7 @@ int main(int argc, const char * argv[])
// check to see if we need to persist our voxel state
persistVoxelsWhenDirty();
if (agentList->getAgentSocket().receive(&agentPublicAddress, packetData, &receivedBytes)) {
if (agentList->getAgentSocket()->receive(&agentPublicAddress, packetData, &receivedBytes)) {
// XXXBHG: Hacked in support for 'S' SET command
if (packetData[0] == PACKET_HEADER_SET_VOXEL || packetData[0] == PACKET_HEADER_SET_VOXEL_DESTRUCTIVE) {
bool destructive = (packetData[0] == PACKET_HEADER_SET_VOXEL_DESTRUCTIVE);