comparison core/ReceiveAudioThread.cpp @ 127:6c8fb6f07b47 scope-refactoring

Prevented from hanging upon exit. The issue is with the waitForSocketTime value. See issue#1381
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 24 Aug 2015 15:36:09 +0100
parents 850a4a9bd832
children 2696a7f00053
comparison
equal deleted inserted replaced
126:719119fb2905 127:6c8fb6f07b47
56 int ReceiveAudioThread::readUdpToBuffer(){ 56 int ReceiveAudioThread::readUdpToBuffer(){
57 if(listening==false || bufferReady==false) 57 if(listening==false || bufferReady==false)
58 return 0; 58 return 0;
59 if(writePointer<0) 59 if(writePointer<0)
60 return 0; 60 return 0;
61 if(socket.waitUntilReady(true, waitForSocketTime)){ // waitForSocketTime could have been set to -1 (wait forever), 61 if(socket.waitUntilReady(true, waitForSocketTime)){// TODO: if waitForSocketTime here is >>5, the
62 // but it would have made it more difficult for the thread to be killed 62 // destructor (always or sometimes) never actually gets called, despite run() returns ...see issue #1381
63 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0 63 pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0
64 //read header+payload 64 //read header+payload
65 //JUCE int numBytes=socket.read(buffer+writePointer, bytesToRead,1);
66 int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting. 65 int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting.
67 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header 66 //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header
68 if(numBytes<0){ 67 if(numBytes<0){
69 printf("error numBytes1\n"); 68 printf("error numBytes1\n");
70 return -3; //TODO: something went wrong, you have to discard the rest of the packet! 69 return -3; //TODO: something went wrong, you have to discard the rest of the packet!
106 ReceiveAudioThread::ReceiveAudioThread(const String &threadName) : 105 ReceiveAudioThread::ReceiveAudioThread(const String &threadName) :
107 Thread(threadName), 106 Thread(threadName),
108 #else 107 #else
109 ReceiveAudioThread::ReceiveAudioThread() : 108 ReceiveAudioThread::ReceiveAudioThread() :
110 #endif /* JUCE */ 109 #endif /* JUCE */
111 socket(NULL), 110 socket(0),
112 listening(false), 111 listening(false),
113 bufferReady(false), 112 bufferReady(false),
114 buffer(NULL), 113 buffer(NULL),
115 stackBuffer(NULL), 114 stackBuffer(NULL),
116 bufferLength(0), 115 bufferLength(0),
117 lastValidPointer(0), 116 lastValidPointer(0),
118 waitForSocketTime(100), 117 waitForSocketTime(5),
119 #ifdef JUCE 118 #ifdef JUCE
120 threadPriority(5) 119 threadPriority(5)
121 #else 120 #else
122 threadPriority(88) 121 threadPriority(88)
123 #endif /* JUCE */ 122 #endif /* JUCE */
124 {}; 123 {};
125 ReceiveAudioThread::~ReceiveAudioThread(){ 124 ReceiveAudioThread::~ReceiveAudioThread(){
126 #ifdef JUCE 125 #ifdef JUCE
127 stopThread(1000); 126 stopThread(1000);
128 #else 127 #else
129 printf("inside the destructor\n");
130 while(threadRunning){ 128 while(threadRunning){
131 printf("while in the destructor\n");
132 usleep(sleepTime*2); //wait for thread to stop 129 usleep(sleepTime*2); //wait for thread to stop
133 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; 130 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl;
134 } 131 }
135 #endif /* JUCE */ 132 #endif /* JUCE */
136 //TODO: check if thread stopped, otherwise kill it before dealloc 133 //TODO: check if thread stopped, otherwise kill it before dealloc
137 printf("dealloc\n");
138 dealloc(); 134 dealloc();
139 } 135 }
140 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){ 136 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){
141 dealloc(); 137 dealloc();
142 #ifdef JUCE 138 #ifdef JUCE