mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	spa: libcamera: properly construct/destruct libcamera manager impl
Previously, the libcamera manager `impl` object was neither properly constructed neither properly destructed. As a consequence, for example, the shared pointers in the `devices` array weren't properly destructed, although this has been somewhat mitigated by a previous change that modifed `clear_devices()`.
This commit is contained in:
		
							parent
							
								
									cc229d4b05
								
							
						
					
					
						commit
						222368e562
					
				
					 1 changed files with 35 additions and 29 deletions
				
			
		| 
						 | 
					@ -54,6 +54,8 @@ using namespace libcamera;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define MAX_DEVICES	64
 | 
					#define MAX_DEVICES	64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct global {
 | 
					struct global {
 | 
				
			||||||
	int ref;
 | 
						int ref;
 | 
				
			||||||
	CameraManager *manager;
 | 
						CameraManager *manager;
 | 
				
			||||||
| 
						 | 
					@ -68,23 +70,27 @@ struct device {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct impl {
 | 
					typedef struct impl {
 | 
				
			||||||
	struct spa_handle handle;
 | 
						struct spa_handle handle;
 | 
				
			||||||
	struct spa_device device;
 | 
						struct spa_device device = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct spa_log *log;
 | 
						struct spa_log *log;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct spa_hook_list hooks;
 | 
						struct spa_hook_list hooks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uint64_t info_all;
 | 
						static constexpr uint64_t info_all = SPA_DEVICE_CHANGE_MASK_FLAGS | SPA_DEVICE_CHANGE_MASK_PROPS;
 | 
				
			||||||
	struct spa_device_info info;
 | 
						struct spa_device_info info = SPA_DEVICE_INFO_INIT();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	CameraManager *manager;
 | 
						CameraManager *manager = nullptr;
 | 
				
			||||||
	void addCamera(std::shared_ptr<libcamera::Camera> camera);
 | 
						void addCamera(std::shared_ptr<libcamera::Camera> camera);
 | 
				
			||||||
	void removeCamera(std::shared_ptr<libcamera::Camera> camera);
 | 
						void removeCamera(std::shared_ptr<libcamera::Camera> camera);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct device devices[MAX_DEVICES];
 | 
						struct device devices[MAX_DEVICES];
 | 
				
			||||||
	uint32_t n_devices;
 | 
						uint32_t n_devices = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						impl(spa_log *log);
 | 
				
			||||||
} Impl;
 | 
					} Impl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int libcamera_manager_release(CameraManager *manager)
 | 
					int libcamera_manager_release(CameraManager *manager)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (global.manager != manager)
 | 
						if (global.manager != manager)
 | 
				
			||||||
| 
						 | 
					@ -326,14 +332,32 @@ static int impl_get_interface(struct spa_handle *handle, const char *type, void
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int impl_clear(struct spa_handle *handle)
 | 
					static int impl_clear(struct spa_handle *handle)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = (struct impl *) handle;
 | 
						auto impl = reinterpret_cast<struct impl *>(handle);
 | 
				
			||||||
 | 
						auto manager = impl->manager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	stop_monitor(impl);
 | 
						stop_monitor(impl);
 | 
				
			||||||
	if (impl->manager)
 | 
						std::destroy_at(impl);
 | 
				
			||||||
		libcamera_manager_release(impl->manager);
 | 
					
 | 
				
			||||||
	impl->manager = NULL;
 | 
						if (manager)
 | 
				
			||||||
 | 
							libcamera_manager_release(manager);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl::impl(spa_log *log)
 | 
				
			||||||
 | 
						: handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }),
 | 
				
			||||||
 | 
						  log(log)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						libcamera_log_topic_init(log);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						spa_hook_list_init(&hooks);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						device.iface = SPA_INTERFACE_INIT(
 | 
				
			||||||
 | 
								SPA_TYPE_INTERFACE_Device,
 | 
				
			||||||
 | 
								SPA_VERSION_DEVICE,
 | 
				
			||||||
 | 
								&impl_device, this);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static size_t
 | 
					static size_t
 | 
				
			||||||
impl_get_size(const struct spa_handle_factory *factory,
 | 
					impl_get_size(const struct spa_handle_factory *factory,
 | 
				
			||||||
	      const struct spa_dict *params)
 | 
						      const struct spa_dict *params)
 | 
				
			||||||
| 
						 | 
					@ -348,30 +372,12 @@ impl_init(const struct spa_handle_factory *factory,
 | 
				
			||||||
	  const struct spa_support *support,
 | 
						  const struct spa_support *support,
 | 
				
			||||||
	  uint32_t n_support)
 | 
						  uint32_t n_support)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	spa_return_val_if_fail(factory != NULL, -EINVAL);
 | 
						spa_return_val_if_fail(factory != NULL, -EINVAL);
 | 
				
			||||||
	spa_return_val_if_fail(handle != NULL, -EINVAL);
 | 
						spa_return_val_if_fail(handle != NULL, -EINVAL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	handle->get_interface = impl_get_interface;
 | 
						auto log = static_cast<spa_log *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log));
 | 
				
			||||||
	handle->clear = impl_clear;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	impl = (struct impl *) handle;
 | 
						new (handle) impl(log);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
 | 
					 | 
				
			||||||
	libcamera_log_topic_init(impl->log);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	spa_hook_list_init(&impl->hooks);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	impl->device.iface = SPA_INTERFACE_INIT(
 | 
					 | 
				
			||||||
			SPA_TYPE_INTERFACE_Device,
 | 
					 | 
				
			||||||
			SPA_VERSION_DEVICE,
 | 
					 | 
				
			||||||
			&impl_device, impl);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	impl->info = SPA_DEVICE_INFO_INIT();
 | 
					 | 
				
			||||||
	impl->info_all = SPA_DEVICE_CHANGE_MASK_FLAGS |
 | 
					 | 
				
			||||||
			SPA_DEVICE_CHANGE_MASK_PROPS;
 | 
					 | 
				
			||||||
	impl->info.flags = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue