Skip to content

Commit 10c6463

Browse files
committed
Cleanup when mem mapping failed
1 parent db4e04c commit 10c6463

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/ccap_imp_linux.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ bool ProviderV4L2::allocateBuffers() {
407407

408408
if (ioctl(m_fd, VIDIOC_QUERYBUF, &buf) < 0) {
409409
reportError(ErrorCode::MemoryAllocationFailed, "Query device buffer failed: " + std::string(strerror(errno)));
410+
releaseAndFreeDriverBuffers();
410411
return false;
411412
}
412413

@@ -416,6 +417,7 @@ bool ProviderV4L2::allocateBuffers() {
416417

417418
if (m_buffers[i].start == MAP_FAILED) {
418419
reportError(ErrorCode::MemoryAllocationFailed, "Memory mapping failed: " + std::string(strerror(errno)));
420+
releaseAndFreeDriverBuffers();
419421
return false;
420422
}
421423
}
@@ -433,6 +435,19 @@ void ProviderV4L2::releaseBuffers() {
433435
m_buffers.clear();
434436
}
435437

438+
void ProviderV4L2::releaseAndFreeDriverBuffers() {
439+
// Unmap any mapped buffers we have and clear the vector
440+
releaseBuffers();
441+
442+
// Hint the driver to free any requested buffers
443+
struct v4l2_requestbuffers zero = {};
444+
zero.count = 0;
445+
zero.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
446+
zero.memory = V4L2_MEMORY_MMAP;
447+
// Ignoring return value: this is a best-effort hint during cleanup
448+
ioctl(m_fd, VIDIOC_REQBUFS, &zero);
449+
}
450+
436451
bool ProviderV4L2::startStreaming() {
437452
// Queue all buffers
438453
for (size_t i = 0; i < m_buffers.size(); i++) {

src/ccap_imp_linux.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class ProviderV4L2 : public ProviderImp {
9090
bool isVideoDevice(const std::string& devicePath);
9191
std::string getDeviceDescription(const std::string& devicePath);
9292

93+
void releaseAndFreeDriverBuffers();
94+
9395
private:
9496
// Device state
9597
int m_fd = -1;
@@ -99,12 +101,12 @@ class ProviderV4L2 : public ProviderImp {
99101
bool m_isStreaming = false;
100102

101103
// V4L2 device capabilities
102-
struct v4l2_capability m_caps {};
104+
struct v4l2_capability m_caps{};
103105
std::vector<V4L2Format> m_supportedFormats;
104106
std::vector<DeviceInfo::Resolution> m_supportedResolutions;
105107

106108
// Current format
107-
struct v4l2_format m_currentFormat {};
109+
struct v4l2_format m_currentFormat{};
108110

109111
// Buffer management
110112
std::vector<V4L2Buffer> m_buffers;

0 commit comments

Comments
 (0)