So, I have a high-queue class that helps multi-threaded describe 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);
my application only needs one frame in the queue.
The answer here on how to remove the structure does not help in this case, because the application is overwhelming.
I have similar code for an 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);
and a function that works in a 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 transfer media to url I use the ffmpeg function.
BTW: here is the 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 clear AddSampleToQueue and AddFrameToQueue so that they do not leak memory?
BTW: I am completely new to this whole thing C + + shared / auto. So to speak a beginner. So please provide code examples that work, at least in my code, because I have provided all my code. Therefore, if you know what to do, try integrating your knowledge into my example. Thanks. And if you can show me how to do this without using general / automatic ptrs, I will be very happy.