@@ -37,9 +37,9 @@ ProviderImp* createProviderV4L2();
3737
3838// Global error callback storage
3939namespace {
40- std::mutex g_errorCallbackMutex;
41- ErrorCallback g_globalErrorCallback;
42- }
40+ std::mutex g_errorCallbackMutex;
41+ ErrorCallback g_globalErrorCallback;
42+ } // namespace
4343
4444void setErrorCallback (ErrorCallback callback) {
4545 std::lock_guard<std::mutex> lock (g_errorCallbackMutex);
@@ -67,6 +67,11 @@ void DefaultAllocator::resize(size_t size) {
6767 // 32字节对齐,满足主流SIMD指令集需求(AVX)
6868 size_t alignedSize = (size + 31 ) & ~size_t (31 );
6969 m_data = static_cast <uint8_t *>(ALIGNED_ALLOC (32 , alignedSize));
70+ if (!m_data) {
71+ ccap::reportError (ErrorCode::MemoryAllocationFailed, " Failed to allocate " + std::to_string (alignedSize) + " bytes of aligned memory" );
72+ m_size = 0 ;
73+ return ;
74+ }
7075 m_size = alignedSize;
7176 CCAP_LOG_V (" ccap: Allocated %zu bytes of memory at %p\n " , m_size, m_data);
7277}
@@ -114,12 +119,17 @@ ProviderImp* createProvider(std::string_view extraInfo) {
114119 if (warningLogEnabled ()) {
115120 CCAP_LOG_W (" ccap: Unsupported platform!\n " );
116121 }
122+ ccap::reportError (ErrorCode::InitializationFailed, " Unsupported platform" );
117123#endif
118124 return nullptr ;
119125}
120126
121127Provider::Provider () :
122- m_imp (createProvider(" " )) {}
128+ m_imp (createProvider(" " )) {
129+ if (!m_imp) {
130+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER );
131+ }
132+ }
123133
124134Provider::~Provider () {
125135 CCAP_LOG_V (" ccap: Provider::~Provider() called, this=%p, imp=%p\n " , this , m_imp);
@@ -130,24 +140,35 @@ Provider::Provider(std::string_view deviceName, std::string_view extraInfo) :
130140 m_imp (createProvider(extraInfo)) {
131141 if (m_imp) {
132142 open (deviceName);
143+ } else {
144+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER );
133145 }
134146}
135147
136148Provider::Provider (int deviceIndex, std::string_view extraInfo) :
137149 m_imp (createProvider(extraInfo)) {
138150 if (m_imp) {
139151 open (deviceIndex);
152+ } else {
153+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::FAILED_TO_CREATE_PROVIDER );
140154 }
141155}
142156
143157std::vector<std::string> Provider::findDeviceNames () { return m_imp ? m_imp->findDeviceNames () : std::vector<std::string>(); }
144158
145159bool Provider::open (std::string_view deviceName, bool autoStart) {
146- return m_imp && m_imp->open (deviceName) && (!autoStart || m_imp->start ());
160+ if (!m_imp) {
161+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
162+ return false ;
163+ }
164+ return m_imp->open (deviceName) && (!autoStart || m_imp->start ());
147165}
148166
149167bool Provider::open (int deviceIndex, bool autoStart) {
150- if (!m_imp) return false ;
168+ if (!m_imp) {
169+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
170+ return false ;
171+ }
151172
152173 std::string deviceName;
153174 if (deviceIndex >= 0 ) {
@@ -167,32 +188,74 @@ bool Provider::isOpened() const { return m_imp && m_imp->isOpened(); }
167188
168189std::optional<DeviceInfo> Provider::getDeviceInfo () const { return m_imp ? m_imp->getDeviceInfo () : std::nullopt ; }
169190
170- void Provider::close () { m_imp->close (); }
191+ void Provider::close () {
192+ if (m_imp) {
193+ m_imp->close ();
194+ }
195+ }
171196
172- bool Provider::start () { return m_imp && m_imp->start (); }
197+ bool Provider::start () {
198+ if (!m_imp) {
199+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
200+ return false ;
201+ }
202+ return m_imp->start ();
203+ }
173204
174205void Provider::stop () {
175206 if (m_imp) m_imp->stop ();
176207}
177208
178209bool Provider::isStarted () const { return m_imp && m_imp->isStarted (); }
179210
180- bool Provider::set (PropertyName prop, double value) { return m_imp->set (prop, value); }
211+ bool Provider::set (PropertyName prop, double value) {
212+ if (!m_imp) {
213+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
214+ return false ;
215+ }
216+ return m_imp->set (prop, value);
217+ }
181218
182219double Provider::get (PropertyName prop) { return m_imp ? m_imp->get (prop) : NAN ; }
183220
184- std::shared_ptr<VideoFrame> Provider::grab (uint32_t timeoutInMs) { return m_imp->grab (timeoutInMs); }
221+ std::shared_ptr<VideoFrame> Provider::grab (uint32_t timeoutInMs) {
222+ if (!m_imp) {
223+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
224+ return nullptr ;
225+ }
226+ return m_imp->grab (timeoutInMs);
227+ }
185228
186229void Provider::setNewFrameCallback (std::function<bool (const std::shared_ptr<VideoFrame>&)> callback) {
230+ if (!m_imp) {
231+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
232+ return ;
233+ }
187234 m_imp->setNewFrameCallback (std::move (callback));
188235}
189236
190237void Provider::setFrameAllocator (std::function<std::shared_ptr<Allocator>()> allocatorFactory) {
238+ if (!m_imp) {
239+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
240+ return ;
241+ }
191242 m_imp->setFrameAllocator (std::move (allocatorFactory));
192243}
193244
194- void Provider::setMaxAvailableFrameSize (uint32_t size) { m_imp->setMaxAvailableFrameSize (size); }
245+ void Provider::setMaxAvailableFrameSize (uint32_t size) {
246+ if (!m_imp) {
247+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
248+ return ;
249+ }
250+ m_imp->setMaxAvailableFrameSize (size);
251+ }
195252
196- void Provider::setMaxCacheFrameSize (uint32_t size) { m_imp->setMaxCacheFrameSize (size); }
253+ void Provider::setMaxCacheFrameSize (uint32_t size) {
254+ if (!m_imp) {
255+ ccap::reportError (ErrorCode::InitializationFailed, ErrorMessages::PROVIDER_IMPLEMENTATION_NULL );
256+ return ;
257+ }
258+ m_imp->setMaxCacheFrameSize (size);
259+ }
197260
198- } // namespace ccap
261+ } // namespace ccap
0 commit comments