Skip to content

Instantly share code, notes, and snippets.

@cynthia2006
Created April 12, 2024 14:20
Show Gist options
  • Save cynthia2006/150e7a20e9005936f35c3b599bc0c4ad to your computer and use it in GitHub Desktop.
Save cynthia2006/150e7a20e9005936f35c3b599bc0c4ad to your computer and use it in GitHub Desktop.
Enumerate input/output devices discoverable though libavdevice
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
#include <libavutil/avutil.h>
static char errbuf[AV_ERROR_MAX_STRING_SIZE];
static void print_streams (int i, int ddev, AVDeviceInfo *d)
{
printf ("\t%c[%d] %s (%s)\n",
/* star = device is default */
ddev == i ? '*' : ' ', i,
d->device_name, d->device_description);
}
static void print_input_device (const AVInputFormat *d)
{
AVDeviceInfoList *list;
int i, ret;
ret = avdevice_list_input_sources (d, NULL, NULL, &list);
if (ret < 0)
{
av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
fprintf (stderr, "\terror: libavdevice: %s\n", errbuf);
return;
}
for (i = 0; i < list->nb_devices; ++i)
print_streams(i, list->default_device, list->devices[i]);
avdevice_free_list_devices(&list);
}
static void print_output_device (const AVOutputFormat *d)
{
AVDeviceInfoList *list;
int i, ret;
ret = avdevice_list_output_sinks (d, NULL, NULL, &list);
if (ret < 0)
{
av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
fprintf (stderr, "\terror: libavdevice: %s\n", errbuf);
return;
}
for (i = 0; i < list->nb_devices; ++i)
print_streams(i, list->default_device, list->devices[i]);
avdevice_free_list_devices(&list);
}
int main()
{
avdevice_register_all();
const AVInputFormat *in_device = NULL;
const AVOutputFormat *out_device = NULL;
printf ("-- Audio --\n-- Inputs --\n");
while ((in_device = av_input_audio_device_next (in_device)) != NULL)
{
printf ("%s (%s)\n", in_device->name, in_device->long_name);
print_input_device(in_device);
putc('\n', stdout);
}
printf ("-- Outputs --\n");
while ((out_device = av_output_audio_device_next (out_device)) != NULL)
{
printf ("%s (%s)\n", out_device->name, out_device->long_name);
print_output_device(out_device);
putc('\n', stdout);
}
printf ("-- Video --\n-- Inputs --\n");
in_device = NULL;
while ((in_device = av_input_video_device_next (in_device)) != NULL)
{
printf ("%s (%s)\n", in_device->name, in_device->long_name);
print_input_device(in_device);
putc('\n', stdout);
}
printf ("-- Outputs --\n");
out_device = NULL;
while ((out_device = av_output_video_device_next (out_device)) != NULL)
{
printf ("%s (%s)\n", out_device->name, out_device->long_name);
print_output_device(out_device);
putc('\n', stdout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment