#include "media.h"
#include "tools.h"
-static const char *media_entity_type_to_string(unsigned type)
-{
- static const struct {
- __u32 type;
- const char *name;
- } types[] = {
- { MEDIA_ENTITY_TYPE_NODE, "Node" },
- { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
- };
-
- unsigned int i;
-
- for (i = 0; i < ARRAY_SIZE(types); i++) {
- if (types[i].type == type)
- return types[i].name;
- }
-
- return "Unknown";
-}
-
-static const char *media_entity_subtype_to_string(unsigned type, unsigned subtype)
-{
- static const char *node_types[] = {
- "Unknown",
- "V4L",
- "FB",
- "ALSA",
- "DVB",
- };
- static const char *subdev_types[] = {
- "Unknown",
- "Video Decoder",
- "Video Encoder",
- "Miscellaneous",
- };
-
- switch (type) {
- case MEDIA_ENTITY_TYPE_NODE:
- if (subtype > 4)
- subtype = 0;
- return node_types[subtype];
-
- case MEDIA_ENTITY_TYPE_SUBDEV:
- if (subtype > 3)
- subtype = 0;
- return subdev_types[subtype];
- default:
- return node_types[0];
- }
-}
-
/*
* media_entity_remote_pad -
*/
-struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
+struct media_pad *media_entity_remote_source(struct media_pad *pad)
{
unsigned int i;
- for (i = 0; i < pad->entity->info.links; ++i) {
- struct media_entity_link *link = &pad->entity->links[i];
+ if (!(pad->flags & MEDIA_PAD_FLAG_INPUT))
+ return NULL;
+
+ for (i = 0; i < pad->entity->num_links; ++i) {
+ struct media_link *link = &pad->entity->links[i];
if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
continue;
- if (link->source == pad)
- return link->sink;
-
if (link->sink == pad)
return link->source;
}
}
/*
- * media_entity_by_name -
+ * media_get_entity_by_name -
*/
struct media_entity *media_get_entity_by_name(struct media_device *media,
const char *name, size_t length)
return NULL;
}
+/*
+ * media_get_entity_by_id -
+ */
+struct media_entity *media_get_entity_by_id(struct media_device *media,
+ __u32 id)
+{
+ unsigned int i;
+
+ for (i = 0; i < media->entities_count; ++i) {
+ struct media_entity *entity = &media->entities[i];
+
+ if (entity->info.id == id)
+ return entity;
+ }
+
+ return NULL;
+}
+
/*
* media_setup_link -
*/
int media_setup_link(struct media_device *media,
- struct media_entity_pad *source,
- struct media_entity_pad *sink,
+ struct media_pad *source,
+ struct media_pad *sink,
__u32 flags)
{
- struct media_entity_link *link;
- struct media_user_link ulink;
+ struct media_link *link;
+ struct media_link_desc ulink;
unsigned int i;
int ret;
- for (i = 0; i < source->entity->info.links; i++) {
+ for (i = 0; i < source->entity->num_links; i++) {
link = &source->entity->links[i];
if (link->source->entity == source->entity &&
break;
}
- if (i == source->entity->info.links) {
+ if (i == source->entity->num_links) {
printf("%s: Link not found\n", __func__);
return -EINVAL;
}
/* source pad */
ulink.source.entity = source->entity->info.id;
ulink.source.index = source->index;
- ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
+ ulink.source.flags = MEDIA_PAD_FLAG_OUTPUT;
/* sink pad */
ulink.sink.entity = sink->entity->info.id;
ulink.sink.index = sink->index;
- ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
+ ulink.sink.flags = MEDIA_PAD_FLAG_INPUT;
ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
for (i = 0; i < media->entities_count; ++i) {
struct media_entity *entity = &media->entities[i];
- for (j = 0; j < entity->info.links; j++) {
- struct media_entity_link *link = &entity->links[j];
+ for (j = 0; j < entity->num_links; j++) {
+ struct media_link *link = &entity->links[j];
- if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
+ if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE ||
+ link->source->entity != entity)
continue;
ret = media_setup_link(media, link->source, link->sink,
return 0;
}
-void media_print_topology(struct media_device *media)
+static struct media_link *media_entity_add_link(struct media_entity *entity)
{
- unsigned int i, j, k;
- unsigned int padding;
+ if (entity->num_links >= entity->max_links) {
+ struct media_link *links = entity->links;
+ unsigned int max_links = entity->max_links * 2;
+ unsigned int i;
- printf("Device topology\n");
+ links = realloc(links, max_links * sizeof *links);
+ if (links == NULL)
+ return NULL;
- for (i = 0; i < media->entities_count; ++i) {
- struct media_entity *entity = &media->entities[i];
+ for (i = 0; i < entity->num_links; ++i)
+ links[i].twin->twin = &links[i];
- padding = printf("- entity %u: ", entity->info.id);
- printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
- entity->info.pads, entity->info.pads > 1 ? "s" : "",
- entity->info.links, entity->info.links > 1 ? "s" : "");
- printf("%*ctype %s subtype %s\n", padding, ' ',
- media_entity_type_to_string(entity->info.type),
- media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
- if (entity->devname)
- printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
-
- for (j = 0; j < entity->info.pads; j++) {
- for (k = 0; k < entity->info.links; k++) {
- struct media_entity_link *link = &entity->links[k];
-
- if (link->source->entity != entity ||
- link->source->index != j)
- continue;
-
- printf("\tpad%u -> '%s':pad%u [", link->source->index,
- link->sink->entity->info.name, link->sink->index);
-
- if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
- printf("IMMUTABLE,");
- if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
- printf("ACTIVE");
-
- printf("]\n");
- }
- }
- printf("\n");
+ entity->max_links = max_links;
+ entity->links = links;
}
+
+ return &entity->links[entity->num_links++];
}
static int media_enum_links(struct media_device *media)
for (id = 1; id <= media->entities_count; id++) {
struct media_entity *entity = &media->entities[id - 1];
- struct media_user_links links;
+ struct media_links_enum links;
unsigned int i;
links.entity = entity->info.id;
- links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
- links.links = malloc(entity->info.links * sizeof(struct media_user_link));
+ links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
+ links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
printf("%s: Unable to enumerate pads and links (%s).\n",
for (i = 0; i < entity->info.pads; ++i) {
entity->pads[i].entity = entity;
- entity->pads[i].type = links.pads[i].type;
entity->pads[i].index = links.pads[i].index;
+ entity->pads[i].flags = links.pads[i].flags;
}
for (i = 0; i < entity->info.links; ++i) {
- struct media_user_link *link = &links.links[i];
+ struct media_link_desc *link = &links.links[i];
+ struct media_link *fwdlink;
+ struct media_link *backlink;
struct media_entity *source;
struct media_entity *sink;
- if (link->source.entity > media->entities_count ||
- link->sink.entity > media->entities_count) {
+ source = media_get_entity_by_id(media, link->source.entity);
+ sink = media_get_entity_by_id(media, link->sink.entity);
+
+ if (source == NULL || sink == NULL) {
printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
id, i, link->source.entity, link->source.index,
link->sink.entity, link->sink.index);
ret = -EINVAL;
+ } else {
+ fwdlink = media_entity_add_link(source);
+ fwdlink->source = &source->pads[link->source.index];
+ fwdlink->sink = &sink->pads[link->sink.index];
+ fwdlink->flags = links.links[i].flags;
+
+ backlink = media_entity_add_link(sink);
+ backlink->source = &source->pads[link->source.index];
+ backlink->sink = &sink->pads[link->sink.index];
+ backlink->flags = links.links[i].flags;
+
+ fwdlink->twin = backlink;
+ backlink->twin = fwdlink;
}
-
- source = &media->entities[link->source.entity - 1];
- sink = &media->entities[link->sink.entity - 1];
- entity->links[i].source = &source->pads[link->source.index];
- entity->links[i].sink = &sink->pads[link->sink.index];
- entity->links[i].flags = links.links[i].flags;
}
free(links.pads);
{
struct media_entity *entity;
struct stat devstat;
+ unsigned int size;
char devname[32];
- unsigned int i;
+ char sysname[32];
+ char target[1024];
+ char *p;
__u32 id;
int ret;
- for (id = 1; ; id++) {
- media->entities =
- realloc(media->entities, id * sizeof(*media->entities));
- entity = &media->entities[id - 1];
+ for (id = 0; ; id = entity->info.id) {
+ size = (media->entities_count + 1) * sizeof(*media->entities);
+ media->entities = realloc(media->entities, size);
+
+ entity = &media->entities[media->entities_count];
memset(entity, 0, sizeof(*entity));
entity->fd = -1;
- entity->info.id = id;
+ entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
if (ret < 0) {
return -errno;
}
+ /* Number of links (for outbound links) plus number of pads (for
+ * inbound links) is a good safe initial estimate of the total
+ * number of links.
+ */
+ entity->max_links = entity->info.pads + entity->info.links;
+
entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
- entity->links = malloc(entity->info.links * sizeof(*entity->links));
+ entity->links = malloc(entity->max_links * sizeof(*entity->links));
if (entity->pads == NULL || entity->links == NULL)
return -ENOMEM;
media->entities_count++;
/* Find the corresponding device name. */
- if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
- entity->info.type != MEDIA_NODE_TYPE_V4L) &&
- (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
+ if (media_entity_type(entity) != MEDIA_ENTITY_TYPE_NODE &&
+ media_entity_type(entity) != MEDIA_ENTITY_TYPE_SUBDEV)
continue;
- for (i = 0; i < 256; ++i) {
- if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
- sprintf(devname, "/dev/video%u", i);
- else
- sprintf(devname, "/dev/subdev%u", i);
+ sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
+ entity->info.v4l.minor);
+ ret = readlink(sysname, target, sizeof(target));
+ if (ret < 0)
+ continue;
- ret = stat(devname, &devstat);
- if (ret < 0)
- continue;
+ target[ret] = '\0';
+ p = strrchr(target, '/');
+ if (p == NULL)
+ continue;
- if (major(devstat.st_rdev) == entity->info.v4l.major &&
- minor(devstat.st_rdev) == entity->info.v4l.minor) {
- strcpy(entity->devname, devname);
- break;
- }
- }
+ sprintf(devname, "/dev/%s", p + 1);
+ ret = stat(devname, &devstat);
+ if (ret < 0)
+ continue;
+
+ /* Sanity check: udev might have reordered the device nodes.
+ * Make sure the major/minor match. We should really use
+ * libudev.
+ */
+ if (major(devstat.st_rdev) == entity->info.v4l.major &&
+ minor(devstat.st_rdev) == entity->info.v4l.minor)
+ strcpy(entity->devname, devname);
}
return 0;
ret = media_enum_entities(media);
if (ret < 0) {
printf("%s: Unable to enumerate entities for device %s (%s)\n",
- __func__, name, strerror(ret));
+ __func__, name, strerror(-ret));
media_close(media);
return NULL;
}