summaryrefslogtreecommitdiff
path: root/src/libcamera/device_enumerator.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-24 00:59:40 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-24 22:24:11 +0200
commit4f043b05e74cf66f2a78f593eaef2772f11af33f (patch)
treea446daff4ff89c50c637d5a1ced86c23132725a0 /src/libcamera/device_enumerator.cpp
parent5b02e03199b79165086135236d8fb9b2c673aae1 (diff)
libcamera: device_enumerator: Reference-count MediaDevice instances
The MediaDevice class will be the entry point to hot-unplug, as it corresponds to the kernel devices that will report device removal events. The class will signal media device disconnection to pipeline handlers, which will clean up resources as a result. This can't be performed synchronously as references may exist to the related Camera objects in applications. The MediaDevice object thus needs to be reference-counted in order to support unplugging, as otherwise pipeline handlers would be required to drop all the references to the media device they have borrowed synchronously with the disconnection signal handler, which would be very error prone (if even possible at all in a sane way). Handle MedieDevice instances with std::shared_ptr<> to support this. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/libcamera/device_enumerator.cpp')
-rw-r--r--src/libcamera/device_enumerator.cpp23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index 8100972a..149ffbf9 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -166,12 +166,10 @@ std::unique_ptr<DeviceEnumerator> DeviceEnumerator::create()
DeviceEnumerator::~DeviceEnumerator()
{
- for (MediaDevice *dev : devices_) {
- if (dev->busy())
+ for (std::shared_ptr<MediaDevice> media : devices_) {
+ if (media->busy())
LOG(DeviceEnumerator, Error)
<< "Removing media device while still in use";
-
- delete dev;
}
}
@@ -211,7 +209,7 @@ DeviceEnumerator::~DeviceEnumerator()
*/
int DeviceEnumerator::addDevice(const std::string &deviceNode)
{
- MediaDevice *media = new MediaDevice(deviceNode);
+ std::shared_ptr<MediaDevice> media = std::make_shared<MediaDevice>(deviceNode);
int ret = media->open();
if (ret < 0)
@@ -243,9 +241,10 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)
return ret;
}
- devices_.push_back(media);
media->close();
+ devices_.push_back(std::move(media));
+
return 0;
}
@@ -260,17 +259,17 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)
*
* \return pointer to the matching MediaDevice, or nullptr if no match is found
*/
-MediaDevice *DeviceEnumerator::search(const DeviceMatch &dm)
+std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
{
- for (MediaDevice *dev : devices_) {
- if (dev->busy())
+ for (std::shared_ptr<MediaDevice> media : devices_) {
+ if (media->busy())
continue;
- if (dm.match(dev)) {
+ if (dm.match(media.get())) {
LOG(DeviceEnumerator, Debug)
<< "Successful match for media device \""
- << dev->driver() << "\"";
- return dev;
+ << media->driver() << "\"";
+ return std::move(media);
}
}