So I have a class. .h file with some variables like:
struct AudioSample
{
const unsigned char * buffer;
int len;
};
//...
const unsigned char * VideoFrameBuffer;
int VideoFrameLen;
std::queue<AudioSample> AudioSamples;
bool sampleSendingFinished;
bool frameSendingFinished;
//...
And .CPP file with functions
void VideoEncoder::UrlWriteData()
{
while(1){
if (AudioSamples.empty() && VideoFrameBuffer == NULL)
{
Sleep(1);
}
else if(!AudioSamples.empty())
{
struct AudioSample newAudioSample = AudioSamples.front();
url_write (url_context, (unsigned char *)newAudioSample.buffer, newAudioSample.len);
}
else if (AudioSamples.empty() && VideoFrameBuffer != NULL)
{
url_write (url_context, (unsigned char *)VideoFrameBuffer, VideoFrameLen);
}
}
}
void VideoEncoder::AddSampleToQueue(const unsigned char *buf, int size )
{
struct AudioSample newAudioSample;
newAudioSample.buffer = buf;
newAudioSample.len = size;
AudioSamples.push(newAudioSample);
}
void VideoEncoder::AddFrameToQueue(const unsigned char *buf, int size )
{
VideoFrameBuffer = buf;
VideoFrameLen = size;
}
once in a while some functions call AddFrameToQueue and AddSampleToQueue. UrlWriteData lives in separete thread.
My app compiles. But my problem is – when I run it it dies on line:
else if(!AudioSamples.empty())
{
struct AudioSample newAudioSample = AudioSamples.front();
Why it dies and how to make it not die?
There is a thread-safe producer-consumer queue using soon-to-be standard threading mechanisms here. This would be a good intro to the threading concepts you have to understand to make this design work.
The producer(s) are the functions that add to your queue, and the consumer(s) are the function(s) that read and (usually) process queue entries –
UrlWriteData, here.