libcamera: media_device: Open and close media device inside populate()

Remove the need for the caller to open and close the media device when
populating the MediaDevice. This is done as an effort to make the usage
of the MediaDevice less error prone and the interface stricter.

The rework also revealed and fixes a potential memory leak in
MediaDevice::populate() where resources would not be deleted if the
second MEDIA_IOC_G_TOPOLOGY would fail.

Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Niklas Söderlund 2019-04-13 23:58:31 +02:00
parent 12053cf8e6
commit 5868d73e77
5 changed files with 12 additions and 29 deletions

View file

@ -207,11 +207,7 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)
{ {
std::shared_ptr<MediaDevice> media = std::make_shared<MediaDevice>(deviceNode); std::shared_ptr<MediaDevice> media = std::make_shared<MediaDevice>(deviceNode);
int ret = media->open(); int ret = media->populate();
if (ret < 0)
return ret;
ret = media->populate();
if (ret < 0) { if (ret < 0) {
LOG(DeviceEnumerator, Info) LOG(DeviceEnumerator, Info)
<< "Unable to populate media device " << deviceNode << "Unable to populate media device " << deviceNode
@ -238,8 +234,6 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)
return ret; return ret;
} }
media->close();
LOG(DeviceEnumerator, Debug) LOG(DeviceEnumerator, Debug)
<< "Added device " << deviceNode << ": " << media->driver(); << "Added device " << deviceNode << ": " << media->driver();

View file

@ -221,6 +221,10 @@ int MediaDevice::populate()
clear(); clear();
ret = open();
if (ret)
return ret;
/* /*
* Keep calling G_TOPOLOGY until the version number stays stable. * Keep calling G_TOPOLOGY until the version number stays stable.
*/ */
@ -237,7 +241,7 @@ int MediaDevice::populate()
LOG(MediaDevice, Error) LOG(MediaDevice, Error)
<< "Failed to enumerate topology: " << "Failed to enumerate topology: "
<< strerror(-ret); << strerror(-ret);
return ret; goto done;
} }
if (version == topology.topology_version) if (version == topology.topology_version)
@ -262,6 +266,10 @@ int MediaDevice::populate()
populateLinks(topology)) populateLinks(topology))
valid_ = true; valid_ = true;
ret = 0;
done:
close();
delete[] ents; delete[] ents;
delete[] interfaces; delete[] interfaces;
delete[] pads; delete[] pads;
@ -272,7 +280,7 @@ int MediaDevice::populate()
return -EINVAL; return -EINVAL;
} }
return 0; return ret;
} }
/** /**

View file

@ -124,10 +124,6 @@ int MediaDevicePrintTest::testMediaDevice(const string deviceNode)
dev.close(); dev.close();
ret = dev.open();
if (ret)
return ret;
ret = dev.populate(); ret = dev.populate();
if (ret) if (ret)
return ret; return ret;
@ -135,8 +131,6 @@ int MediaDevicePrintTest::testMediaDevice(const string deviceNode)
/* Print out the media graph. */ /* Print out the media graph. */
printMediaGraph(dev, cerr); printMediaGraph(dev, cerr);
dev.close();
return 0; return 0;
} }

View file

@ -71,11 +71,6 @@ int IPU3PipelineTest::init()
return TestSkip; return TestSkip;
} }
if (cio2->open()) {
cerr << "Failed to open media device " << cio2->deviceNode() << endl;
return TestFail;
}
/* /*
* Camera sensor are connected to the CIO2 unit. * Camera sensor are connected to the CIO2 unit.
* Count how many sensors are connected in the system * Count how many sensors are connected in the system

View file

@ -45,13 +45,6 @@ int V4L2SubdeviceTest::init()
return TestSkip; return TestSkip;
} }
int ret = media_->open();
if (ret) {
cerr << "Unable to open media device: " << media_->deviceNode()
<< ": " << strerror(ret) << endl;
return TestSkip;
}
MediaEntity *videoEntity = media_->getEntityByName("Scaler"); MediaEntity *videoEntity = media_->getEntityByName("Scaler");
if (!videoEntity) { if (!videoEntity) {
cerr << "Unable to find media entity 'Scaler'" << endl; cerr << "Unable to find media entity 'Scaler'" << endl;
@ -59,8 +52,7 @@ int V4L2SubdeviceTest::init()
} }
scaler_ = new V4L2Subdevice(videoEntity); scaler_ = new V4L2Subdevice(videoEntity);
ret = scaler_->open(); if (scaler_->open()) {
if (ret) {
cerr << "Unable to open video subdevice " cerr << "Unable to open video subdevice "
<< scaler_->entity()->deviceNode() << endl; << scaler_->entity()->deviceNode() << endl;
return TestSkip; return TestSkip;