libcamera/test/v4l2_subdevice/v4l2_subdevice_test.cpp
Niklas Söderlund 5868d73e77 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>
2019-05-17 01:33:53 +02:00

67 lines
1.5 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* v4l2_subdevice_test.cpp - VIMC-based V4L2 subdevice test
*/
#include <iostream>
#include <string.h>
#include <sys/stat.h>
#include "device_enumerator.h"
#include "media_device.h"
#include "v4l2_subdevice.h"
#include "v4l2_subdevice_test.h"
using namespace std;
using namespace libcamera;
/*
* This test runs on vimc media device. For a description of vimc, in the
* context of libcamera testing, please refer to
* 'test/media_device/media_device_link_test.cpp' file.
*
* If the vimc module is not loaded, the test gets skipped.
*/
int V4L2SubdeviceTest::init()
{
enumerator_ = DeviceEnumerator::create();
if (!enumerator_) {
cerr << "Failed to create device enumerator" << endl;
return TestFail;
}
if (enumerator_->enumerate()) {
cerr << "Failed to enumerate media devices" << endl;
return TestFail;
}
DeviceMatch dm("vimc");
media_ = enumerator_->search(dm);
if (!media_) {
cerr << "Unable to find \'vimc\' media device node" << endl;
return TestSkip;
}
MediaEntity *videoEntity = media_->getEntityByName("Scaler");
if (!videoEntity) {
cerr << "Unable to find media entity 'Scaler'" << endl;
return TestFail;
}
scaler_ = new V4L2Subdevice(videoEntity);
if (scaler_->open()) {
cerr << "Unable to open video subdevice "
<< scaler_->entity()->deviceNode() << endl;
return TestSkip;
}
return 0;
}
void V4L2SubdeviceTest::cleanup()
{
delete scaler_;
}