mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 19:03:07 +02:00
perform send of audio buffer on main thread
This commit is contained in:
parent
42a50e9bab
commit
9ef409b2ef
1 changed files with 177 additions and 193 deletions
|
@ -72,23 +72,40 @@ void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
|
|||
mixSample = normalizedSample;
|
||||
}
|
||||
|
||||
void *sendBuffer(void *args) {
|
||||
int sentBytes;
|
||||
void attachNewBufferToAgent(Agent *newAgent) {
|
||||
if (newAgent->getLinkedData() == NULL) {
|
||||
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
signal(SIGSEGV, printStacktrace);
|
||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||
|
||||
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
|
||||
|
||||
ssize_t receivedBytes = 0;
|
||||
|
||||
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
|
||||
|
||||
agentList->startSilentAgentRemovalThread();
|
||||
agentList->startDomainServerCheckInThread();
|
||||
|
||||
unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE];
|
||||
|
||||
sockaddr *agentAddress = new sockaddr;
|
||||
|
||||
int nextFrame = 0;
|
||||
timeval startTime;
|
||||
|
||||
AgentList* agentList = AgentList::getInstance();
|
||||
|
||||
gettimeofday(&startTime, NULL);
|
||||
|
||||
while (true) {
|
||||
sentBytes = 0;
|
||||
|
||||
// enumerate the agents, check if we can add audio from the agent to current mix
|
||||
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
|
||||
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
|
||||
|
||||
if (agentBuffer && agentBuffer->getEndOfLastWrite() != NULL) {
|
||||
|
||||
if (agentBuffer->getEndOfLastWrite() != NULL) {
|
||||
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());
|
||||
|
@ -112,7 +129,6 @@ void *sendBuffer(void *args) {
|
|||
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
|
||||
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
|
||||
|
||||
if (agentRingBuffer) {
|
||||
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
|
||||
|
||||
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
|
||||
|
@ -232,8 +248,8 @@ void *sendBuffer(void *args) {
|
|||
|
||||
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
|
||||
}
|
||||
}
|
||||
|
||||
// push forward the next output pointers for any audio buffers we used
|
||||
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
|
||||
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
|
||||
if (agentBuffer && agentBuffer->shouldBeAddedToMix()) {
|
||||
|
@ -247,46 +263,8 @@ void *sendBuffer(void *args) {
|
|||
}
|
||||
}
|
||||
|
||||
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||
|
||||
if (usecToSleep > 0) {
|
||||
usleep(usecToSleep);
|
||||
} else {
|
||||
std::cout << "Took too much time, not sleeping!\n";
|
||||
}
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
void attachNewBufferToAgent(Agent *newAgent) {
|
||||
if (newAgent->getLinkedData() == NULL) {
|
||||
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
signal(SIGSEGV, printStacktrace);
|
||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||
|
||||
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
|
||||
|
||||
ssize_t receivedBytes = 0;
|
||||
|
||||
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
|
||||
|
||||
agentList->startSilentAgentRemovalThread();
|
||||
agentList->startDomainServerCheckInThread();
|
||||
|
||||
unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE];
|
||||
|
||||
pthread_t sendBufferThread;
|
||||
pthread_create(&sendBufferThread, NULL, sendBuffer, NULL);
|
||||
|
||||
sockaddr *agentAddress = new sockaddr;
|
||||
|
||||
while (true) {
|
||||
if(agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
|
||||
// pull any new audio data from agents off of the network stack
|
||||
while (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
|
||||
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
|
||||
|
||||
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) {
|
||||
|
@ -296,9 +274,15 @@ int main(int argc, const char* argv[]) {
|
|||
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pthread_join(sendBufferThread, NULL);
|
||||
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||
|
||||
if (usecToSleep > 0) {
|
||||
usleep(usecToSleep);
|
||||
} else {
|
||||
std::cout << "Took too much time, not sleeping!\n";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue