fix for UDPSocket receive crash in Audio class and main.cpp

This commit is contained in:
Stephen Birarda 2013-02-22 12:20:16 -08:00
parent f4a1c405fc
commit 200982dddc
3 changed files with 13 additions and 6 deletions

View file

@ -44,6 +44,7 @@ const char EC2_WEST_MIXER[] = "54.241.92.53";
const int AUDIO_UDP_LISTEN_PORT = 55444; const int AUDIO_UDP_LISTEN_PORT = 55444;
int starve_counter = 0; int starve_counter = 0;
bool stopAudioReceiveThread = false;
StDev stdev; StDev stdev;
@ -202,7 +203,7 @@ void *receiveAudioViaUDP(void *args) {
delete[] filename; delete[] filename;
} }
while (true) { while (!stopAudioReceiveThread) {
if (sharedAudioData->audioSocket->receive((void *)receivedData, &receivedBytes)) { if (sharedAudioData->audioSocket->receive((void *)receivedData, &receivedBytes)) {
bool firstSample = (currentReceiveTime.tv_sec == 0); bool firstSample = (currentReceiveTime.tv_sec == 0);
@ -284,8 +285,6 @@ Audio::Audio(Oscilloscope * s)
audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT); audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT);
audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SIZE_SAMPLES); audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SIZE_SAMPLES);
pthread_t audioReceiveThread;
AudioRecThreadStruct threadArgs; AudioRecThreadStruct threadArgs;
threadArgs.sharedAudioData = audioData; threadArgs.sharedAudioData = audioData;
@ -435,6 +434,9 @@ bool Audio::terminate ()
logFile.close(); logFile.close();
} }
stopAudioReceiveThread = true;
pthread_join(audioReceiveThread, NULL);
return true; return true;
error: error:

View file

@ -41,6 +41,9 @@ private:
// audio stream handle // audio stream handle
PaStream *stream; PaStream *stream;
// audio receive thread
pthread_t audioReceiveThread;
// give access to AudioData class from audioCallback // give access to AudioData class from audioCallback
friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*); friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*);
}; };

View file

@ -59,6 +59,8 @@ char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty stri
const int DOMAINSERVER_PORT = 40102; const int DOMAINSERVER_PORT = 40102;
AgentList agentList; AgentList agentList;
pthread_t networkReceiveThread;
bool stopAgentDataReceiveThread = false;
// For testing, add milliseconds of delay for received UDP packets // For testing, add milliseconds of delay for received UDP packets
int packetcount = 0; int packetcount = 0;
@ -360,6 +362,8 @@ void terminate () {
//close(serial_fd); //close(serial_fd);
audio.terminate(); audio.terminate();
stopAgentDataReceiveThread = true;
pthread_join(networkReceiveThread, NULL);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -763,7 +767,7 @@ void *networkReceive(void *args)
ssize_t bytesReceived; ssize_t bytesReceived;
char *incomingPacket = new char[MAX_PACKET_SIZE]; char *incomingPacket = new char[MAX_PACKET_SIZE];
while (true) { while (!stopAgentDataReceiveThread) {
if (agentList.getAgentSocket()->receive(&senderAddress, incomingPacket, &bytesReceived)) { if (agentList.getAgentSocket()->receive(&senderAddress, incomingPacket, &bytesReceived)) {
packetcount++; packetcount++;
bytescount += bytesReceived; bytescount += bytesReceived;
@ -938,7 +942,6 @@ int main(int argc, char** argv)
agentList.audioMixerSocketUpdate = &audioMixerUpdate; agentList.audioMixerSocketUpdate = &audioMixerUpdate;
// create thread for receipt of data via UDP // create thread for receipt of data via UDP
pthread_t networkReceiveThread;
pthread_create(&networkReceiveThread, NULL, networkReceive, NULL); pthread_create(&networkReceiveThread, NULL, networkReceive, NULL);
glutInit(&argc, argv); glutInit(&argc, argv);
@ -969,7 +972,6 @@ int main(int argc, char** argv)
glutMainLoop(); glutMainLoop();
pthread_join(networkReceiveThread, NULL);
::terminate(); ::terminate();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }