2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/avassert.h"
20 #include "libavutil/samplefmt.h"
21 #include "libavutil/pixfmt.h"
22 #include "libavcodec/avcodec.h"
26 #include "libavutil/ffversion.h"
27 const char av_device_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
29 #define E AV_OPT_FLAG_ENCODING_PARAM
30 #define D AV_OPT_FLAG_DECODING_PARAM
31 #define A AV_OPT_FLAG_AUDIO_PARAM
32 #define V AV_OPT_FLAG_VIDEO_PARAM
33 #define OFFSET(x) offsetof(AVDeviceCapabilitiesQuery, x)
35 const AVOption av_device_capabilities[] = {
36 { "codec", "codec", OFFSET(codec), AV_OPT_TYPE_INT,
37 {.i64 = AV_CODEC_ID_NONE}, AV_CODEC_ID_NONE, INT_MAX, E|D|A|V },
38 { "sample_format", "sample format", OFFSET(sample_format), AV_OPT_TYPE_SAMPLE_FMT,
39 {.i64 = AV_SAMPLE_FMT_NONE}, AV_SAMPLE_FMT_NONE, INT_MAX, E|D|A },
40 { "sample_rate", "sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT,
41 {.i64 = -1}, -1, INT_MAX, E|D|A },
42 { "channels", "channels", OFFSET(channels), AV_OPT_TYPE_INT,
43 {.i64 = -1}, -1, INT_MAX, E|D|A },
44 { "channel_layout", "channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT,
45 {.i64 = -1}, -1, INT_MAX, E|D|A },
46 { "pixel_format", "pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT,
47 {.i64 = AV_PIX_FMT_NONE}, AV_PIX_FMT_NONE, INT_MAX, E|D|V },
48 { "window_size", "window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,
49 {.str = NULL}, -1, INT_MAX, E|D|V },
50 { "frame_size", "frame size", OFFSET(frame_width), AV_OPT_TYPE_IMAGE_SIZE,
51 {.str = NULL}, -1, INT_MAX, E|D|V },
52 { "fps", "fps", OFFSET(fps), AV_OPT_TYPE_RATIONAL,
53 {.dbl = -1}, -1, INT_MAX, E|D|V },
63 unsigned avdevice_version(void)
65 av_assert0(LIBAVDEVICE_VERSION_MICRO >= 100);
66 return LIBAVDEVICE_VERSION_INT;
69 const char * avdevice_configuration(void)
71 return FFMPEG_CONFIGURATION;
74 const char * avdevice_license(void)
76 #define LICENSE_PREFIX "libavdevice license: "
77 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
80 static void *av_device_next(void *prev, int output,
81 AVClassCategory c1, AVClassCategory c2)
84 AVClassCategory category = AV_CLASS_CATEGORY_NA;
87 if (!(prev = av_oformat_next(prev)))
89 pc = ((AVOutputFormat *)prev)->priv_class;
91 if (!(prev = av_iformat_next(prev)))
93 pc = ((AVInputFormat *)prev)->priv_class;
97 category = pc->category;
98 } while (category != c1 && category != c2);
102 AVInputFormat *av_input_audio_device_next(AVInputFormat *d)
104 return av_device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
105 AV_CLASS_CATEGORY_DEVICE_INPUT);
108 AVInputFormat *av_input_video_device_next(AVInputFormat *d)
110 return av_device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
111 AV_CLASS_CATEGORY_DEVICE_INPUT);
114 AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d)
116 return av_device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
117 AV_CLASS_CATEGORY_DEVICE_OUTPUT);
120 AVOutputFormat *av_output_video_device_next(AVOutputFormat *d)
122 return av_device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
123 AV_CLASS_CATEGORY_DEVICE_OUTPUT);
126 int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type,
127 void *data, size_t data_size)
129 if (!s->oformat || !s->oformat->control_message)
130 return AVERROR(ENOSYS);
131 return s->oformat->control_message(s, type, data, data_size);
134 int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type,
135 void *data, size_t data_size)
137 if (!s->control_message_cb)
138 return AVERROR(ENOSYS);
139 return s->control_message_cb(s, type, data, data_size);
142 int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
143 AVDictionary **device_options)
146 av_assert0(s && caps);
147 av_assert0(s->iformat || s->oformat);
148 if ((s->oformat && !s->oformat->create_device_capabilities) ||
149 (s->iformat && !s->iformat->create_device_capabilities))
150 return AVERROR(ENOSYS);
151 *caps = av_mallocz(sizeof(**caps));
153 return AVERROR(ENOMEM);
154 (*caps)->device_context = s;
155 if (((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
158 if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
161 if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
164 av_opt_set_defaults(*caps);
171 void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
173 if (!s || !caps || !(*caps))
175 av_assert0(s->iformat || s->oformat);
177 if (s->iformat->free_device_capabilities)
178 s->iformat->free_device_capabilities(s, *caps);
180 if (s->oformat->free_device_capabilities)
181 s->oformat->free_device_capabilities(s, *caps);
186 int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
190 av_assert0(device_list);
191 av_assert0(s->oformat || s->iformat);
192 if ((s->oformat && !s->oformat->get_device_list) ||
193 (s->iformat && !s->iformat->get_device_list)) {
195 return AVERROR(ENOSYS);
197 *device_list = av_mallocz(sizeof(AVDeviceInfoList));
199 return AVERROR(ENOMEM);
200 /* no default device by default */
201 (*device_list)->default_device = -1;
203 ret = s->oformat->get_device_list(s, *device_list);
205 ret = s->iformat->get_device_list(s, *device_list);
207 avdevice_free_list_devices(device_list);
211 void avdevice_free_list_devices(AVDeviceInfoList **device_list)
213 AVDeviceInfoList *list;
217 av_assert0(device_list);
222 for (i = 0; i < list->nb_devices; i++) {
223 dev = list->devices[i];
225 av_freep(&dev->device_name);
226 av_freep(&dev->device_description);
230 av_freep(&list->devices);
231 av_freep(device_list);