Skip to content

Commit 37cce94

Browse files
committed
optimize imp
1 parent ee3895f commit 37cce94

5 files changed

Lines changed: 36 additions & 39 deletions

File tree

src/ccap_core.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void DefaultAllocator::resize(size_t size) {
6868
size_t alignedSize = (size + 31) & ~size_t(31);
6969
m_data = static_cast<uint8_t*>(ALIGNED_ALLOC(32, alignedSize));
7070
if (!m_data) {
71-
ccap::reportError(ErrorCode::MemoryAllocationFailed, "Failed to allocate " + std::to_string(alignedSize) + " bytes of aligned memory");
71+
reportError(ErrorCode::MemoryAllocationFailed, "Failed to allocate " + std::to_string(alignedSize) + " bytes of aligned memory");
7272
m_size = 0;
7373
return;
7474
}
@@ -119,15 +119,15 @@ ProviderImp* createProvider(std::string_view extraInfo) {
119119
if (warningLogEnabled()) {
120120
CCAP_LOG_W("ccap: Unsupported platform!\n");
121121
}
122-
ccap::reportError(ErrorCode::InitializationFailed, "Unsupported platform");
122+
reportError(ErrorCode::InitializationFailed, "Unsupported platform");
123123
#endif
124124
return nullptr;
125125
}
126126

127127
Provider::Provider() :
128128
m_imp(createProvider("")) {
129129
if (!m_imp) {
130-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
130+
reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
131131
}
132132
}
133133

@@ -141,7 +141,7 @@ Provider::Provider(std::string_view deviceName, std::string_view extraInfo) :
141141
if (m_imp) {
142142
open(deviceName);
143143
} else {
144-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
144+
reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
145145
}
146146
}
147147

@@ -150,23 +150,23 @@ Provider::Provider(int deviceIndex, std::string_view extraInfo) :
150150
if (m_imp) {
151151
open(deviceIndex);
152152
} else {
153-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
153+
reportError(ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER);
154154
}
155155
}
156156

157157
std::vector<std::string> Provider::findDeviceNames() { return m_imp ? m_imp->findDeviceNames() : std::vector<std::string>(); }
158158

159159
bool Provider::open(std::string_view deviceName, bool autoStart) {
160160
if (!m_imp) {
161-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
161+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
162162
return false;
163163
}
164164
return m_imp->open(deviceName) && (!autoStart || m_imp->start());
165165
}
166166

167167
bool Provider::open(int deviceIndex, bool autoStart) {
168168
if (!m_imp) {
169-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
169+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
170170
return false;
171171
}
172172

@@ -196,7 +196,7 @@ void Provider::close() {
196196

197197
bool Provider::start() {
198198
if (!m_imp) {
199-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
199+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
200200
return false;
201201
}
202202
return m_imp->start();
@@ -210,7 +210,7 @@ bool Provider::isStarted() const { return m_imp && m_imp->isStarted(); }
210210

211211
bool Provider::set(PropertyName prop, double value) {
212212
if (!m_imp) {
213-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
213+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
214214
return false;
215215
}
216216
return m_imp->set(prop, value);
@@ -220,39 +220,39 @@ double Provider::get(PropertyName prop) { return m_imp ? m_imp->get(prop) : NAN;
220220

221221
std::shared_ptr<VideoFrame> Provider::grab(uint32_t timeoutInMs) {
222222
if (!m_imp) {
223-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
223+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
224224
return nullptr;
225225
}
226226
return m_imp->grab(timeoutInMs);
227227
}
228228

229229
void Provider::setNewFrameCallback(std::function<bool(const std::shared_ptr<VideoFrame>&)> callback) {
230230
if (!m_imp) {
231-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
231+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
232232
return;
233233
}
234234
m_imp->setNewFrameCallback(std::move(callback));
235235
}
236236

237237
void Provider::setFrameAllocator(std::function<std::shared_ptr<Allocator>()> allocatorFactory) {
238238
if (!m_imp) {
239-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
239+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
240240
return;
241241
}
242242
m_imp->setFrameAllocator(std::move(allocatorFactory));
243243
}
244244

245245
void Provider::setMaxAvailableFrameSize(uint32_t size) {
246246
if (!m_imp) {
247-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
247+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
248248
return;
249249
}
250250
m_imp->setMaxAvailableFrameSize(size);
251251
}
252252

253253
void Provider::setMaxCacheFrameSize(uint32_t size) {
254254
if (!m_imp) {
255-
ccap::reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
255+
reportError(ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL);
256256
return;
257257
}
258258
m_imp->setMaxCacheFrameSize(size);

src/ccap_imp.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ std::shared_ptr<VideoFrame> ProviderImp::grab(uint32_t timeoutInMs) {
110110

111111
if (m_availableFrames.empty() && timeoutInMs > 0) {
112112
if (!isStarted()) {
113-
CCAP_LOG_W("ccap: Grab called when camera is not started!");
114-
ccap::reportError(ErrorCode::DeviceStartFailed, "Grab called when camera is not started");
113+
reportError(ErrorCode::DeviceStartFailed, "Grab called when camera is not started");
115114
return nullptr;
116115
}
117116

@@ -127,8 +126,7 @@ std::shared_ptr<VideoFrame> ProviderImp::grab(uint32_t timeoutInMs) {
127126

128127
m_grabFrameWaiting = false;
129128
if (!waitSuccess) {
130-
CCAP_LOG_V("ccap: Grab timed out after %u ms\n", timeoutInMs);
131-
ccap::reportError(ErrorCode::FrameCaptureTimeout, "Grab timed out after " + std::to_string(timeoutInMs) + " ms");
129+
reportError(ErrorCode::FrameCaptureTimeout, "Grab timed out after " + std::to_string(timeoutInMs) + " ms");
132130
return nullptr;
133131
}
134132
}

src/ccap_imp_apple.mm

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ - (BOOL)open {
297297
}
298298

299299
if (![_session canSetSessionPreset:preset]) {
300-
CCAP_NSLOG_E(@"ccap: CameraCaptureObjc init - session preset not supported, using AVCaptureSessionPresetHigh");
301-
ccap::reportError(ErrorCode::UnsupportedResolution, "Session preset not supported");
300+
CCAP_NSLOG_W(@"ccap: CameraCaptureObjc init - session preset not supported, using AVCaptureSessionPresetHigh");
302301
preset = AVCaptureSessionPresetHigh;
303302
}
304303

@@ -455,6 +454,7 @@ - (BOOL)open {
455454
auto fallbackInfo = getPixelFormatInfo(_cvPixelFormat);
456455
CCAP_NSLOG_E(@"ccap: Preferred pixel format (%@-%s) not supported, fallback to: (%@-%s)", preferredInfo.name,
457456
preferredInfo.description.c_str(), fallbackInfo.name, fallbackInfo.description.c_str());
457+
reportError(ErrorCode::UnsupportedPixelFormat, "Preferred pixel format not supported, fallback to: " + fallbackInfo.description);
458458
}
459459
}
460460

@@ -644,8 +644,7 @@ - (void)setFrameRate:(double)fps {
644644
}
645645
}
646646
} else {
647-
CCAP_NSLOG_E(@"ccap: Desired fps (%g) not supported, skipping", fps);
648-
ccap::reportError(ErrorCode::FrameRateSetFailed, "Desired fps not supported");
647+
reportError(ErrorCode::FrameRateSetFailed, "Desired fps (" + std::to_string(fps) + ") not supported, using fallback");
649648
}
650649
}
651650
} else {
@@ -735,7 +734,7 @@ - (void)flushResolution {
735734
}
736735
}
737736

738-
CCAP_NSLOG_E(@"ccap: No connections available");
737+
reportError(ErrorCode::InitializationFailed, "No connections available");
739738
}
740739

741740
- (BOOL)start {
@@ -793,7 +792,7 @@ - (void)captureOutput:(AVCaptureOutput*)output
793792
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
794793
fromConnection:(AVCaptureConnection*)connection {
795794
if (!_provider) {
796-
CCAP_NSLOG_E(@"ccap: CameraCaptureObjc captureOutput - provider is nil");
795+
reportError(ErrorCode::InitializationFailed, "CameraCaptureObjc captureOutput - provider is nil");
797796
return;
798797
}
799798

@@ -1023,7 +1022,7 @@ - (void)captureOutput:(AVCaptureOutput*)output
10231022
}
10241023

10251024
if (!deviceInfo) {
1026-
CCAP_NSLOG_E(@"ccap: getDeviceInfo called with no device opened");
1025+
reportError(ErrorCode::InitializationFailed, "getDeviceInfo called with no device opened");
10271026
}
10281027
return deviceInfo;
10291028
}
@@ -1039,7 +1038,7 @@ - (void)captureOutput:(AVCaptureOutput*)output
10391038
bool ProviderApple::start() {
10401039
if (!isOpened()) {
10411040
CCAP_NSLOG_W(@"ccap: camera start called with no device opened");
1042-
ccap::reportError(ErrorCode::DeviceStartFailed, "Camera start called with no device opened");
1041+
reportError(ErrorCode::DeviceStartFailed, "Camera start called with no device opened");
10431042
return false;
10441043
}
10451044

src/ccap_imp_linux.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ bool ProviderV4L2::open(std::string_view deviceName) {
126126
if (!setupDevice()) {
127127
::close(m_fd);
128128
m_fd = -1;
129-
ccap::reportError(ErrorCode::DeviceOpenFailed, "Failed to setup device " + m_devicePath);
129+
reportError(ErrorCode::DeviceOpenFailed, "Failed to setup device " + m_devicePath);
130130
return false;
131131
}
132132

@@ -187,7 +187,7 @@ bool ProviderV4L2::start() {
187187
}
188188

189189
if (!negotiateFormat() || !allocateBuffers() || !startStreaming()) {
190-
ccap::reportError(ErrorCode::DeviceStartFailed, "Failed to start streaming");
190+
reportError(ErrorCode::DeviceStartFailed, "Failed to start streaming");
191191
return false;
192192
}
193193

@@ -451,7 +451,7 @@ void ProviderV4L2::stopStreaming() {
451451
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
452452
if (ioctl(m_fd, VIDIOC_STREAMOFF, &type) < 0) {
453453
CCAP_LOG_E("ccap: VIDIOC_STREAMOFF failed: %s\n", strerror(errno));
454-
ccap::reportError(ErrorCode::DeviceStopFailed, "VIDIOC_STREAMOFF failed: " + std::string(strerror(errno)));
454+
reportError(ErrorCode::DeviceStopFailed, "VIDIOC_STREAMOFF failed: " + std::string(strerror(errno)));
455455
}
456456
}
457457

@@ -637,7 +637,7 @@ bool ProviderV4L2::readFrame() {
637637

638638
if (ioctl(m_fd, VIDIOC_QBUF, &requeueBuf) < 0) {
639639
CCAP_LOG_E("ccap: VIDIOC_QBUF failed in destructor: %s\n", strerror(errno));
640-
ccap::reportError(ErrorCode::FrameCaptureFailed, "VIDIOC_QBUF failed in destructor: " + std::string(strerror(errno)));
640+
reportError(ErrorCode::FrameCaptureFailed, "VIDIOC_QBUF failed in destructor: " + std::string(strerror(errno)));
641641
}
642642
}
643643
frame = nullptr;
@@ -653,7 +653,7 @@ bool ProviderV4L2::readFrame() {
653653
// Requeue buffer immediately after copying data
654654
if (ioctl(m_fd, VIDIOC_QBUF, &buf) < 0) {
655655
CCAP_LOG_E("ccap: VIDIOC_QBUF failed: %s\n", strerror(errno));
656-
ccap::reportError(ErrorCode::FrameCaptureFailed, "VIDIOC_QBUF failed: " + std::string(strerror(errno)));
656+
reportError(ErrorCode::FrameCaptureFailed, "VIDIOC_QBUF failed: " + std::string(strerror(errno)));
657657
return false;
658658
}
659659
}

src/ccap_imp_windows.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ void ProviderDirectShow::enumerateDevices(std::function<bool(IMoniker* moniker,
308308
deviceEnum->Release();
309309
if (auto failed = FAILED(result); failed || !enumerator) {
310310
if (failed) {
311-
reportError(ErrorCode::NoDeviceFound, "CreateClassEnumerator CLSID_VideoInputDeviceCategory failed, result=0x" + std::to_string(result));
311+
// result is formatted as decimal by std::to_string, don't prefix with 0x
312+
reportError(ErrorCode::NoDeviceFound, "CreateClassEnumerator CLSID_VideoInputDeviceCategory failed, result=" + std::to_string(result));
312313
} else {
313314
reportError(ErrorCode::NoDeviceFound, "No video capture devices found");
314315
}
@@ -589,7 +590,7 @@ bool ProviderDirectShow::createStream() {
589590
}
590591
} else {
591592
CCAP_LOG_E("ccap: SetFormat failed, result=0x%lx\n", setFormatResult);
592-
ccap::reportError(ErrorCode::UnsupportedPixelFormat, "SetFormat failed");
593+
reportError(ErrorCode::UnsupportedPixelFormat, "SetFormat failed, result=" + std::to_string(setFormatResult));
593594
}
594595
}
595596
}
@@ -624,7 +625,7 @@ bool ProviderDirectShow::createStream() {
624625
hr = m_graph->QueryInterface(IID_IMediaFilter, (void**)&pMediaFilter);
625626
if (FAILED(hr)) {
626627
CCAP_LOG_E("ccap: QueryInterface IMediaFilter failed, result=0x%lx\n", hr);
627-
ccap::reportError(ErrorCode::DeviceOpenFailed, "QueryInterface IMediaFilter failed");
628+
reportError(ErrorCode::DeviceOpenFailed, "QueryInterface IMediaFilter failed");
628629
} else {
629630
pMediaFilter->SetSyncSource(NULL);
630631
pMediaFilter->Release();
@@ -688,12 +689,12 @@ bool ProviderDirectShow::open(std::string_view deviceName) {
688689
CCAP_LOG_I("ccap: Found video capture device: %s\n", m_deviceName.c_str());
689690

690691
if (!buildGraph()) {
691-
ccap::reportError(ErrorCode::DeviceOpenFailed, "Failed to build DirectShow graph");
692+
reportError(ErrorCode::DeviceOpenFailed, "Failed to build DirectShow graph");
692693
return false;
693694
}
694695

695696
if (!createStream()) {
696-
ccap::reportError(ErrorCode::DeviceOpenFailed, "Failed to create DirectShow stream");
697+
reportError(ErrorCode::DeviceOpenFailed, "Failed to create DirectShow stream");
697698
return false;
698699
}
699700

@@ -709,7 +710,7 @@ bool ProviderDirectShow::open(std::string_view deviceName) {
709710
hr = m_graph->QueryInterface(IID_IVideoWindow, (LPVOID*)&videoWindow);
710711
if (FAILED(hr)) {
711712
CCAP_LOG_E("ccap: QueryInterface IVideoWindow failed, result=0x%lx\n", hr);
712-
ccap::reportError(ErrorCode::DeviceOpenFailed, "QueryInterface IVideoWindow failed");
713+
reportError(ErrorCode::DeviceOpenFailed, "QueryInterface IVideoWindow failed, result=" + std::to_string(hr));
713714
return false;
714715
}
715716
videoWindow->put_AutoShow(false);
@@ -736,7 +737,7 @@ HRESULT STDMETHODCALLTYPE ProviderDirectShow::SampleCB(double sampleTime, IMedia
736737
BYTE* sampleData = nullptr;
737738
if (auto hr = mediaSample->GetPointer(&sampleData); FAILED(hr)) {
738739
CCAP_LOG_E("ccap: GetPointer failed, hr=0x%lx\n", hr);
739-
ccap::reportError(ErrorCode::FrameCaptureFailed, "GetPointer failed");
740+
reportError(ErrorCode::FrameCaptureFailed, "GetPointer failed");
740741
return S_OK;
741742
}
742743

@@ -914,7 +915,6 @@ HRESULT STDMETHODCALLTYPE ProviderDirectShow::SampleCB(double sampleTime, IMedia
914915

915916
HRESULT STDMETHODCALLTYPE ProviderDirectShow::BufferCB(double SampleTime, BYTE* pBuffer, long BufferLen) {
916917
CCAP_LOG_E("ccap: BufferCB called, SampleTime: %f, BufferLen: %ld\n", SampleTime, BufferLen);
917-
// This callback is not used in this implementation
918918
return S_OK;
919919
}
920920

0 commit comments

Comments
 (0)