remove useless allocation

pull/214/head
mpromonet 4 years ago
parent 682a4bd076
commit 0fa3368bd0

@ -33,14 +33,15 @@ class V4L2DeviceSource: public FramedSource
// ---------------------------------
struct Frame
{
Frame(char* buffer, int size, timeval timestamp) : m_buffer(buffer), m_size(size), m_timestamp(timestamp) {};
Frame(char* buffer, int size, timeval timestamp, char * allocatedBuffer = NULL) : m_buffer(buffer), m_size(size), m_timestamp(timestamp), m_allocatedBuffer(allocatedBuffer) {};
Frame(const Frame&);
Frame& operator=(const Frame&);
~Frame() { delete [] m_buffer; };
~Frame() { delete [] m_allocatedBuffer; };
char* m_buffer;
unsigned int m_size;
timeval m_timestamp;
char* m_allocatedBuffer;
};
// ---------------------------------
@ -82,14 +83,13 @@ class V4L2DeviceSource: public FramedSource
void incomingPacketHandler();
int getNextFrame();
void processFrame(char * frame, int frameSize, const timeval &ref);
void queueFrame(char * frame, int frameSize, const timeval &tv);
void queueFrame(char * frame, int frameSize, const timeval &tv, char * allocatedBuffer = NULL);
// split packet in frames
virtual std::list< std::pair<unsigned char*,size_t> > splitFrames(unsigned char* frame, unsigned frameSize);
// overide FramedSource
virtual void doGetNextFrame();
virtual void doStopGettingFrames();
protected:
std::list<Frame*> m_captureQueue;

@ -126,13 +126,6 @@ void V4L2DeviceSource::doGetNextFrame()
deliverFrame();
}
// stopping FrameSource callback
void V4L2DeviceSource::doStopGettingFrames()
{
LOG(NOTICE) << "V4L2DeviceSource::doStopGettingFrames";
FramedSource::doStopGettingFrames();
}
// deliver frame to the sink
void V4L2DeviceSource::deliverFrame()
{
@ -171,6 +164,10 @@ void V4L2DeviceSource::deliverFrame()
fPresentationTime = frame->m_timestamp;
memcpy(fTo, frame->m_buffer, fFrameSize);
delete frame;
if (!m_captureQueue.empty()) {
envir().taskScheduler().triggerEvent(m_eventTriggerId, this);
}
}
pthread_mutex_unlock (&m_mutex);
@ -196,7 +193,7 @@ int V4L2DeviceSource::getNextFrame()
{
timeval ref;
gettimeofday(&ref, NULL);
char buffer[m_device->getBufferSize()];
char* buffer = new char[m_device->getBufferSize()];
int frameSize = m_device->read(buffer, m_device->getBufferSize());
if (frameSize < 0)
{
@ -237,9 +234,12 @@ void V4L2DeviceSource::processFrame(char * frame, int frameSize, const timeval &
{
std::pair<unsigned char*,size_t>& item = frameList.front();
size_t size = item.second;
char* buf = new char[size];
memcpy(buf, item.first, size);
queueFrame(buf,size,ref);
char* allocatedBuffer = NULL;
if (frameList.size() == 1) {
// last frame will release buffer
allocatedBuffer = frame;
}
queueFrame((char*)item.first,size,ref,allocatedBuffer);
frameList.pop_front();
LOG(DEBUG) << "queueFrame\ttimestamp:" << ref.tv_sec << "." << ref.tv_usec << "\tsize:" << size <<"\tdiff:" << (diff.tv_sec*1000+diff.tv_usec/1000) << "ms";
@ -247,7 +247,7 @@ void V4L2DeviceSource::processFrame(char * frame, int frameSize, const timeval &
}
// post a frame to fifo
void V4L2DeviceSource::queueFrame(char * frame, int frameSize, const timeval &tv)
void V4L2DeviceSource::queueFrame(char * frame, int frameSize, const timeval &tv, char * allocatedBuffer)
{
pthread_mutex_lock (&m_mutex);
while (m_captureQueue.size() >= m_queueSize)
@ -256,7 +256,7 @@ void V4L2DeviceSource::queueFrame(char * frame, int frameSize, const timeval &tv
delete m_captureQueue.front();
m_captureQueue.pop_front();
}
m_captureQueue.push_back(new Frame(frame, frameSize, tv));
m_captureQueue.push_back(new Frame(frame, frameSize, tv, allocatedBuffer));
pthread_mutex_unlock (&m_mutex);
// post an event to ask to deliver the frame

Loading…
Cancel
Save