So I have a boosted queue class that helps multi-threading described here.
In my class declarations I have
//...
struct VideoSample
{
const unsigned char * buffer;
int len;
};
ConcurrentQueue<VideoSample * > VideoSamples;
//...
struct AudioSample
{
const unsigned char * buffer;
int len;
};
ConcurrentQueue<AudioSample * > AudioSamples;
//...
In my class I have a function:
void VideoEncoder::AddFrameToQueue(const unsigned char *buf, int size )
{
VideoSample * newVideoSample = new VideoSample;
VideoSamples.try_pop(newVideoSample);
newVideoSample->buffer = buf;
newVideoSample->len = size;
VideoSamples.push(newVideoSample);
//free(newVideoSample->buffer);
//delete newVideoSample;
}
keeping only one frame in queue is required for my app.
answer provided here on how to delete a structure is not helpful in this case because app crushes.
I have similar code for audio queue.
void VideoEncoder::AddSampleToQueue(const unsigned char *buf, int size )
{
AudioSample * newAudioSample = new AudioSample;
newAudioSample->buffer = buf;
newAudioSample->len = size;
AudioSamples.push(newAudioSample);
url_write (url_context, (unsigned char *)newAudioSample->buffer, newAudioSample->len);
AudioSamples.wait_and_pop(newAudioSample);
//delete newAudioSample;
}
and a function that runs in separate thread:
void VideoEncoder::UrlWriteData()
{
while(1){
switch (AudioSamples.empty()){
case true :
switch(VideoSamples.empty()){
case true : Sleep(5); break;
case false :
VideoSample * newVideoSample = new VideoSample;
VideoSamples.wait_and_pop(newVideoSample);
url_write (url_context, (unsigned char *)newVideoSample->buffer, newVideoSample->len);
break;
} break;
case false : Sleep(3); break;
}
}
}
BTW: to stream media data to url I use ffmpeg's function.
BTW: here code I use for queues:
#include <string>
#include <queue>
#include <iostream>
// Boost
#include <boost/thread.hpp>
#include <boost/timer.hpp>
template<typename Data>
class ConcurrentQueue
{
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
public:
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
void wait_and_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
popped_value=the_queue.front();
the_queue.pop();
}
Data& front()
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.front();
}
};
My question is: How to clean up AddSampleToQueue and AddFrameToQueue so that they would not make memory leaks?
BTW: I am quite new to all this C++ shared/auto stuff. So to say a beginner. So please provide code examples that work at least that are integrated into my code - because I provided all my code. So if you know what to do - please try and integrate your knowledge into my example. Thank you. And if you can show me how to do it with no use of shared/auto ptrs I' d be super glad.
use smart pointers: http://www.drdobbs.com/184401507
Use Shared_ptr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With