FFmpeg
avdevice.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "libavutil/avassert.h"
20 #include "libavutil/samplefmt.h"
21 #include "libavutil/pixfmt.h"
22 #include "libavcodec/avcodec.h"
23 #include "avdevice.h"
24 #include "internal.h"
25 #include "config.h"
26 
27 #include "libavutil/ffversion.h"
28 const char av_device_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
29 
30 #define E AV_OPT_FLAG_ENCODING_PARAM
31 #define D AV_OPT_FLAG_DECODING_PARAM
32 #define A AV_OPT_FLAG_AUDIO_PARAM
33 #define V AV_OPT_FLAG_VIDEO_PARAM
34 #define OFFSET(x) offsetof(AVDeviceCapabilitiesQuery, x)
35 
37  { "codec", "codec", OFFSET(codec), AV_OPT_TYPE_INT,
38  {.i64 = AV_CODEC_ID_NONE}, AV_CODEC_ID_NONE, INT_MAX, E|D|A|V },
39  { "sample_format", "sample format", OFFSET(sample_format), AV_OPT_TYPE_SAMPLE_FMT,
40  {.i64 = AV_SAMPLE_FMT_NONE}, AV_SAMPLE_FMT_NONE, INT_MAX, E|D|A },
41  { "sample_rate", "sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT,
42  {.i64 = -1}, -1, INT_MAX, E|D|A },
43  { "channels", "channels", OFFSET(channels), AV_OPT_TYPE_INT,
44  {.i64 = -1}, -1, INT_MAX, E|D|A },
45  { "channel_layout", "channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT,
46  {.i64 = -1}, -1, INT_MAX, E|D|A },
47  { "pixel_format", "pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT,
48  {.i64 = AV_PIX_FMT_NONE}, AV_PIX_FMT_NONE, INT_MAX, E|D|V },
49  { "window_size", "window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,
50  {.str = NULL}, -1, INT_MAX, E|D|V },
51  { "frame_size", "frame size", OFFSET(frame_width), AV_OPT_TYPE_IMAGE_SIZE,
52  {.str = NULL}, -1, INT_MAX, E|D|V },
53  { "fps", "fps", OFFSET(fps), AV_OPT_TYPE_RATIONAL,
54  {.dbl = -1}, -1, INT_MAX, E|D|V },
55  { NULL }
56 };
57 
58 #undef E
59 #undef D
60 #undef A
61 #undef V
62 #undef OFFSET
63 
64 unsigned avdevice_version(void)
65 {
68 }
69 
70 const char * avdevice_configuration(void)
71 {
72  return FFMPEG_CONFIGURATION;
73 }
74 
75 const char * avdevice_license(void)
76 {
77 #define LICENSE_PREFIX "libavdevice license: "
78  return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
79 }
80 
81 static void *device_next(void *prev, int output,
83 {
84  const AVClass *pc;
86  do {
87  if (output) {
88  if (!(prev = av_oformat_next(prev)))
89  break;
90  pc = ((AVOutputFormat *)prev)->priv_class;
91  } else {
92  if (!(prev = av_iformat_next(prev)))
93  break;
94  pc = ((AVInputFormat *)prev)->priv_class;
95  }
96  if (!pc)
97  continue;
98  category = pc->category;
99  } while (category != c1 && category != c2);
100  return prev;
101 }
102 
104 {
107 }
108 
110 {
113 }
114 
116 {
119 }
120 
122 {
125 }
126 
128  void *data, size_t data_size)
129 {
130  if (!s->oformat || !s->oformat->control_message)
131  return AVERROR(ENOSYS);
132  return s->oformat->control_message(s, type, data, data_size);
133 }
134 
136  void *data, size_t data_size)
137 {
138  if (!s->control_message_cb)
139  return AVERROR(ENOSYS);
140  return s->control_message_cb(s, type, data, data_size);
141 }
142 
144  AVDictionary **device_options)
145 {
146  int ret;
147  av_assert0(s && caps);
148  av_assert0(s->iformat || s->oformat);
149  if ((s->oformat && !s->oformat->create_device_capabilities) ||
150  (s->iformat && !s->iformat->create_device_capabilities))
151  return AVERROR(ENOSYS);
152  *caps = av_mallocz(sizeof(**caps));
153  if (!(*caps))
154  return AVERROR(ENOMEM);
155  (*caps)->device_context = s;
156  if (((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
157  goto fail;
158  if (s->iformat) {
159  if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
160  goto fail;
161  } else {
162  if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
163  goto fail;
164  }
165  av_opt_set_defaults(*caps);
166  return 0;
167  fail:
168  av_freep(caps);
169  return ret;
170 }
171 
173 {
174  if (!s || !caps || !(*caps))
175  return;
176  av_assert0(s->iformat || s->oformat);
177  if (s->iformat) {
178  if (s->iformat->free_device_capabilities)
179  s->iformat->free_device_capabilities(s, *caps);
180  } else {
181  if (s->oformat->free_device_capabilities)
182  s->oformat->free_device_capabilities(s, *caps);
183  }
184  av_freep(caps);
185 }
186 
188 {
189  int ret;
190  av_assert0(s);
191  av_assert0(device_list);
192  av_assert0(s->oformat || s->iformat);
193  if ((s->oformat && !s->oformat->get_device_list) ||
194  (s->iformat && !s->iformat->get_device_list)) {
195  *device_list = NULL;
196  return AVERROR(ENOSYS);
197  }
198  *device_list = av_mallocz(sizeof(AVDeviceInfoList));
199  if (!(*device_list))
200  return AVERROR(ENOMEM);
201  /* no default device by default */
202  (*device_list)->default_device = -1;
203  if (s->oformat)
204  ret = s->oformat->get_device_list(s, *device_list);
205  else
206  ret = s->iformat->get_device_list(s, *device_list);
207  if (ret < 0)
208  avdevice_free_list_devices(device_list);
209  return ret;
210 }
211 
213  AVDeviceInfoList **device_list)
214 {
215  AVDictionary *tmp = NULL;
216  int ret;
217 
218  av_dict_copy(&tmp, options, 0);
220  goto fail;
221  ret = avdevice_list_devices(s, device_list);
222  fail:
223  av_dict_free(&tmp);
225  return ret;
226 }
227 
228 int avdevice_list_input_sources(AVInputFormat *device, const char *device_name,
229  AVDictionary *device_options, AVDeviceInfoList **device_list)
230 {
232  int ret;
233 
234  if ((ret = ff_alloc_input_device_context(&s, device, device_name)) < 0)
235  return ret;
236  return list_devices_for_context(s, device_options, device_list);
237 }
238 
239 int avdevice_list_output_sinks(AVOutputFormat *device, const char *device_name,
240  AVDictionary *device_options, AVDeviceInfoList **device_list)
241 {
243  int ret;
244 
245  if ((ret = avformat_alloc_output_context2(&s, device, device_name, NULL)) < 0)
246  return ret;
247  return list_devices_for_context(s, device_options, device_list);
248 }
249 
251 {
253  AVDeviceInfo *dev;
254  int i;
255 
256  av_assert0(device_list);
257  list = *device_list;
258  if (!list)
259  return;
260 
261  for (i = 0; i < list->nb_devices; i++) {
262  dev = list->devices[i];
263  if (dev) {
264  av_freep(&dev->device_name);
266  av_free(dev);
267  }
268  }
269  av_freep(&list->devices);
270  av_freep(device_list);
271 }
avdevice_list_input_sources
int avdevice_list_input_sources(AVInputFormat *device, const char *device_name, AVDictionary *device_options, AVDeviceInfoList **device_list)
List devices.
Definition: avdevice.c:228
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_OPT_TYPE_SAMPLE_FMT
@ AV_OPT_TYPE_SAMPLE_FMT
Definition: opt.h:235
av_opt_set_defaults
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1357
LIBAVDEVICE_VERSION_INT
#define LIBAVDEVICE_VERSION_INT
Definition: version.h:34
E
#define E
Definition: avdevice.c:30
AVDeviceInfo::device_name
char * device_name
device name, format depends on device
Definition: avdevice.h:453
AVDeviceCapabilitiesQuery
Following API allows user to probe device capabilities (supported codecs, pixel formats,...
Definition: avdevice.h:400
D
#define D
Definition: avdevice.c:31
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
V
#define V
Definition: avdevice.c:33
AV_CLASS_CATEGORY_NA
@ AV_CLASS_CATEGORY_NA
Definition: log.h:30
avdevice_list_devices
int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
List devices.
Definition: avdevice.c:187
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
AVOption
AVOption.
Definition: opt.h:246
AVAppToDevMessageType
AVAppToDevMessageType
Message types used by avdevice_app_to_dev_control_message().
Definition: avdevice.h:119
data
const char data[16]
Definition: mxf.c:91
avdevice_dev_to_app_control_message
int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type, void *data, size_t data_size)
Send control message from device to application.
Definition: avdevice.c:135
category
category
Definition: openal-dec.c:248
AVDictionary
Definition: dict.c:30
c1
static const uint64_t c1
Definition: murmur3.c:49
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:228
sample_rate
sample_rate
Definition: ffmpeg_filter.c:192
LIBAVDEVICE_VERSION_MICRO
#define LIBAVDEVICE_VERSION_MICRO
Definition: version.h:32
fail
#define fail()
Definition: checkasm.h:123
av_output_video_device_next
AVOutputFormat * av_output_video_device_next(AVOutputFormat *d)
Video output devices iterator.
Definition: avdevice.c:121
samplefmt.h
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_CLASS_CATEGORY_DEVICE_INPUT
@ AV_CLASS_CATEGORY_DEVICE_INPUT
Definition: log.h:46
AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition: log.h:44
avassert.h
AVInputFormat
Definition: avformat.h:636
av_opt_set_dict
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1655
s
#define s(width, name)
Definition: cbs_vp9.c:257
avdevice_capabilities_create
int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s, AVDictionary **device_options)
Initialize capabilities probing API based on AVOption API.
Definition: avdevice.c:143
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
channels
channels
Definition: aptx.h:33
avdevice_license
const char * avdevice_license(void)
Return the libavdevice license.
Definition: avdevice.c:75
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
list_devices_for_context
static int list_devices_for_context(AVFormatContext *s, AVDictionary *options, AVDeviceInfoList **device_list)
Definition: avdevice.c:212
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
internal.h
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:233
avdevice_configuration
const char * avdevice_configuration(void)
Return the libavdevice build-time configuration.
Definition: avdevice.c:70
AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT
Definition: log.h:41
list
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
Definition: filter_design.txt:25
AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition: log.h:42
av_output_audio_device_next
AVOutputFormat * av_output_audio_device_next(AVOutputFormat *d)
Audio output devices iterator.
Definition: avdevice.c:115
av_input_video_device_next
AVInputFormat * av_input_video_device_next(AVInputFormat *d)
Video input devices iterator.
Definition: avdevice.c:109
options
const OptionDef options[]
AVClass::category
AVClassCategory category
Category used for visualization (like color) This is only set if the category is equal for all object...
Definition: log.h:130
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AVDeviceInfo
Structure describes basic parameters of the device.
Definition: avdevice.h:452
AV_OPT_TYPE_CHANNEL_LAYOUT
@ AV_OPT_TYPE_CHANNEL_LAYOUT
Definition: opt.h:239
avdevice.h
avdevice_free_list_devices
void avdevice_free_list_devices(AVDeviceInfoList **device_list)
Convenient function to free result of avdevice_list_devices().
Definition: avdevice.c:250
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:558
av_input_audio_device_next
AVInputFormat * av_input_audio_device_next(AVInputFormat *d)
Audio input devices iterator.
Definition: avdevice.c:103
ff_alloc_input_device_context
av_warn_unused_result int ff_alloc_input_device_context(struct AVFormatContext **avctx, struct AVInputFormat *iformat, const char *format)
Definition: utils.c:23
av_device_ffversion
const char av_device_ffversion[]
Definition: avdevice.c:28
av_opt_set_dict2
int av_opt_set_dict2(void *obj, AVDictionary **options, int search_flags)
Set all the options from a given dictionary on an object.
Definition: opt.c:1630
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
AVDevToAppMessageType
AVDevToAppMessageType
Message types used by avdevice_dev_to_app_control_message().
Definition: avdevice.h:198
avformat_alloc_output_context2
int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:135
AVDeviceInfo::device_description
char * device_description
human friendly name
Definition: avdevice.h:454
avdevice_list_output_sinks
int avdevice_list_output_sinks(AVOutputFormat *device, const char *device_name, AVDictionary *device_options, AVDeviceInfoList **device_list)
Definition: avdevice.c:239
A
#define A
Definition: avdevice.c:32
LICENSE_PREFIX
#define LICENSE_PREFIX
av_device_capabilities
const AVOption av_device_capabilities[]
AVOption table used by devices to implement device capabilities API.
Definition: avdevice.c:36
avdevice_version
unsigned avdevice_version(void)
Return the LIBAVDEVICE_VERSION_INT constant.
Definition: avdevice.c:64
AVClassCategory
AVClassCategory
Definition: log.h:29
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
AVOutputFormat
Definition: avformat.h:490
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
Definition: log.h:43
OFFSET
#define OFFSET(x)
Definition: avdevice.c:34
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
device_next
static void * device_next(void *prev, int output, AVClassCategory c1, AVClassCategory c2)
Definition: avdevice.c:81
avcodec.h
ret
ret
Definition: filter_design.txt:187
pixfmt.h
AVDeviceInfoList
List of devices.
Definition: avdevice.h:460
avdevice_capabilities_free
void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
Free resources created by avdevice_capabilities_create()
Definition: avdevice.c:172
c2
static const uint64_t c2
Definition: murmur3.c:50
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4455
avdevice_app_to_dev_control_message
int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type, void *data, size_t data_size)
Send control message from application to device.
Definition: avdevice.c:127
AV_OPT_TYPE_PIXEL_FMT
@ AV_OPT_TYPE_PIXEL_FMT
Definition: opt.h:234
AV_CLASS_CATEGORY_DEVICE_OUTPUT
@ AV_CLASS_CATEGORY_DEVICE_OUTPUT
Definition: log.h:45
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217