]> git.sesse.net Git - ffmpeg/blob - libavdevice/avdevice.h
Merge commit 'eed752d61da332fb13e9893a175a90fed7b1d7d3'
[ffmpeg] / libavdevice / avdevice.h
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 #ifndef AVDEVICE_AVDEVICE_H
20 #define AVDEVICE_AVDEVICE_H
21
22 #include "version.h"
23
24 /**
25  * @file
26  * @ingroup lavd
27  * Main libavdevice API header
28  */
29
30 /**
31  * @defgroup lavd Special devices muxing/demuxing library
32  * @{
33  * Libavdevice is a complementary library to @ref libavf "libavformat". It
34  * provides various "special" platform-specific muxers and demuxers, e.g. for
35  * grabbing devices, audio capture and playback etc. As a consequence, the
36  * (de)muxers in libavdevice are of the AVFMT_NOFILE type (they use their own
37  * I/O functions). The filename passed to avformat_open_input() often does not
38  * refer to an actually existing file, but has some special device-specific
39  * meaning - e.g. for x11grab it is the display name.
40  *
41  * To use libavdevice, simply call avdevice_register_all() to register all
42  * compiled muxers and demuxers. They all use standard libavformat API.
43  * @}
44  */
45
46 #include "libavutil/log.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/dict.h"
49 #include "libavformat/avformat.h"
50
51 /**
52  * Return the LIBAVDEVICE_VERSION_INT constant.
53  */
54 unsigned avdevice_version(void);
55
56 /**
57  * Return the libavdevice build-time configuration.
58  */
59 const char *avdevice_configuration(void);
60
61 /**
62  * Return the libavdevice license.
63  */
64 const char *avdevice_license(void);
65
66 /**
67  * Initialize libavdevice and register all the input and output devices.
68  * @warning This function is not thread safe.
69  */
70 void avdevice_register_all(void);
71
72 /**
73  * Audio input devices iterator.
74  *
75  * If d is NULL, returns the first registered input audio/video device,
76  * if d is non-NULL, returns the next registered input audio/video device after d
77  * or NULL if d is the last one.
78  */
79 AVInputFormat *av_input_audio_device_next(AVInputFormat  *d);
80
81 /**
82  * Video input devices iterator.
83  *
84  * If d is NULL, returns the first registered input audio/video device,
85  * if d is non-NULL, returns the next registered input audio/video device after d
86  * or NULL if d is the last one.
87  */
88 AVInputFormat *av_input_video_device_next(AVInputFormat  *d);
89
90 /**
91  * Audio output devices iterator.
92  *
93  * If d is NULL, returns the first registered output audio/video device,
94  * if d is non-NULL, returns the next registered output audio/video device after d
95  * or NULL if d is the last one.
96  */
97 AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d);
98
99 /**
100  * Video output devices iterator.
101  *
102  * If d is NULL, returns the first registered output audio/video device,
103  * if d is non-NULL, returns the next registered output audio/video device after d
104  * or NULL if d is the last one.
105  */
106 AVOutputFormat *av_output_video_device_next(AVOutputFormat *d);
107
108 typedef struct AVDeviceRect {
109     int x;      /**< x coordinate of top left corner */
110     int y;      /**< y coordinate of top left corner */
111     int width;  /**< width */
112     int height; /**< height */
113 } AVDeviceRect;
114
115 /**
116  * Message types used by avdevice_app_to_dev_control_message().
117  */
118 enum AVAppToDevMessageType {
119     /**
120      * Dummy message.
121      */
122     AV_APP_TO_DEV_NONE = MKBETAG('N','O','N','E'),
123
124     /**
125      * Window size change message.
126      *
127      * Message is sent to the device every time the application changes the size
128      * of the window device renders to.
129      * Message should also be sent right after window is created.
130      *
131      * data: AVDeviceRect: new window size.
132      */
133     AV_APP_TO_DEV_WINDOW_SIZE = MKBETAG('G','E','O','M'),
134
135     /**
136      * Repaint request message.
137      *
138      * Message is sent to the device when window have to be rapainted.
139      *
140      * data: AVDeviceRect: area required to be repainted.
141      *       NULL: whole area is required to be repainted.
142      */
143     AV_APP_TO_DEV_WINDOW_REPAINT = MKBETAG('R','E','P','A'),
144
145     /**
146      * Request pause/play.
147      *
148      * Application requests pause/unpause playback.
149      * Mostly usable with devices that have internal buffer.
150      * By default devices are not paused.
151      *
152      * data: NULL
153      */
154     AV_APP_TO_DEV_PAUSE        = MKBETAG('P', 'A', 'U', ' '),
155     AV_APP_TO_DEV_PLAY         = MKBETAG('P', 'L', 'A', 'Y'),
156     AV_APP_TO_DEV_TOGGLE_PAUSE = MKBETAG('P', 'A', 'U', 'T'),
157 };
158
159 /**
160  * Message types used by avdevice_dev_to_app_control_message().
161  */
162 enum AVDevToAppMessageType {
163     /**
164      * Dummy message.
165      */
166     AV_DEV_TO_APP_NONE = MKBETAG('N','O','N','E'),
167
168     /**
169      * Create window buffer message.
170      *
171      * Device requests to create a window buffer. Exact meaning is device-
172      * and application-dependent. Message is sent before rendering first
173      * frame and all one-shot initializations should be done here.
174      * Application is allowed to ignore preferred window buffer size.
175      *
176      * @note: Application is obligated to inform about window buffer size
177      *        with AV_APP_TO_DEV_WINDOW_SIZE message.
178      *
179      * data: AVDeviceRect: preferred size of the window buffer.
180      *       NULL: no preferred size of the window buffer.
181      */
182     AV_DEV_TO_APP_CREATE_WINDOW_BUFFER = MKBETAG('B','C','R','E'),
183
184     /**
185      * Prepare window buffer message.
186      *
187      * Device requests to prepare a window buffer for rendering.
188      * Exact meaning is device- and application-dependent.
189      * Message is sent before rendering of each frame.
190      *
191      * data: NULL.
192      */
193     AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER = MKBETAG('B','P','R','E'),
194
195     /**
196      * Display window buffer message.
197      *
198      * Device requests to display a window buffer.
199      * Message is sent when new frame is ready to be displyed.
200      * Usually buffers need to be swapped in handler of this message.
201      *
202      * data: NULL.
203      */
204     AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER = MKBETAG('B','D','I','S'),
205
206     /**
207      * Destroy window buffer message.
208      *
209      * Device requests to destroy a window buffer.
210      * Message is sent when device is about to be destroyed and window
211      * buffer is not required anymore.
212      *
213      * data: NULL.
214      */
215     AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER = MKBETAG('B','D','E','S'),
216
217     /**
218      * Buffer fullness status messages.
219      *
220      * Device signals buffer overflow/underflow.
221      *
222      * data: NULL.
223      */
224     AV_DEV_TO_APP_BUFFER_OVERFLOW = MKBETAG('B','O','F','L'),
225     AV_DEV_TO_APP_BUFFER_UNDERFLOW = MKBETAG('B','U','F','L'),
226
227     /**
228      * Buffer readable/writable.
229      *
230      * Device informs that buffer is readable/writable.
231      * When possible, device informs how many bytes can be read/write.
232      *
233      * @warning Device may not inform when number of bytes than can be read/write changes.
234      *
235      * data: int64_t: amount of bytes available to read/write.
236      *       NULL: amount of bytes available to read/write is not known.
237      */
238     AV_DEV_TO_APP_BUFFER_READABLE = MKBETAG('B','R','D',' '),
239     AV_DEV_TO_APP_BUFFER_WRITABLE = MKBETAG('B','W','R',' '),
240 };
241
242 /**
243  * Send control message from application to device.
244  *
245  * @param s         device context.
246  * @param type      message type.
247  * @param data      message data. Exact type depends on message type.
248  * @param data_size size of message data.
249  * @return >= 0 on success, negative on error.
250  *         AVERROR(ENOSYS) when device doesn't implement handler of the message.
251  */
252 int avdevice_app_to_dev_control_message(struct AVFormatContext *s,
253                                         enum AVAppToDevMessageType type,
254                                         void *data, size_t data_size);
255
256 /**
257  * Send control message from device to application.
258  *
259  * @param s         device context.
260  * @param type      message type.
261  * @param data      message data. Can be NULL.
262  * @param data_size size of message data.
263  * @return >= 0 on success, negative on error.
264  *         AVERROR(ENOSYS) when application doesn't implement handler of the message.
265  */
266 int avdevice_dev_to_app_control_message(struct AVFormatContext *s,
267                                         enum AVDevToAppMessageType type,
268                                         void *data, size_t data_size);
269
270 /**
271  * Following API allows user to probe device capabilities (supported codecs,
272  * pixel formats, sample formats, resolutions, channel counts, etc).
273  * It is build on top op AVOption API.
274  * Queried capabilities allows to set up converters of video or audio
275  * parameters that fit to the device.
276  *
277  * List of capablities that can be queried:
278  *  - Capabilities valid for both audio and video devices:
279  *    - codec:          supported audio/video codecs.
280  *                      type: AV_OPT_TYPE_INT (AVCodecID value)
281  *  - Capabilities valid for audio devices:
282  *    - sample_format:  supported sample formats.
283  *                      type: AV_OPT_TYPE_INT (AVSampleFormat value)
284  *    - sample_rate:    supported sample rates.
285  *                      type: AV_OPT_TYPE_INT
286  *    - channels:       supported number of channels.
287  *                      type: AV_OPT_TYPE_INT
288  *    - channel_layout: supported channel layouts.
289  *                      type: AV_OPT_TYPE_INT64
290  *  - Capabilities valid for video devices:
291  *    - pixel_format:   supported pixel formats.
292  *                      type: AV_OPT_TYPE_INT (AVPixelFormat value)
293  *    - window_size:    supported window sizes (describes size of the window size presented to the user).
294  *                      type: AV_OPT_TYPE_IMAGE_SIZE
295  *    - frame_size:     supported frame sizes (describes size of provided video frames).
296  *                      type: AV_OPT_TYPE_IMAGE_SIZE
297  *    - fps:            supported fps values
298  *                      type: AV_OPT_TYPE_RATIONAL
299  *
300  * Value of the capability may be set by user using av_opt_set() function
301  * and AVDeviceCapabilitiesQuery object. Following queries will
302  * limit results to the values matching already set capabilities.
303  * For example, setting a codec may impact number of formats or fps values
304  * returned during next query. Setting invalid value may limit results to zero.
305  *
306  * Example of the usage basing on opengl output device:
307  *
308  * @code
309  *  AVFormatContext *oc = NULL;
310  *  AVDeviceCapabilitiesQuery *caps = NULL;
311  *  AVOptionRanges *ranges;
312  *  int ret;
313  *
314  *  if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
315  *      goto fail;
316  *  if (avdevice_capabilities_create(&caps, oc, NULL) < 0)
317  *      goto fail;
318  *
319  *  //query codecs
320  *  if (av_opt_query_ranges(&ranges, caps, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
321  *      goto fail;
322  *  //pick codec here and set it
323  *  av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);
324  *
325  *  //query format
326  *  if (av_opt_query_ranges(&ranges, caps, "pixel_format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
327  *      goto fail;
328  *  //pick format here and set it
329  *  av_opt_set(caps, "pixel_format", AV_PIX_FMT_YUV420P, 0);
330  *
331  *  //query and set more capabilities
332  *
333  * fail:
334  *  //clean up code
335  *  avdevice_capabilities_free(&query, oc);
336  *  avformat_free_context(oc);
337  * @endcode
338  */
339
340 /**
341  * Structure describes device capabilites.
342  *
343  * It is used by devices in conjuntion with av_device_capabilities AVOption table
344  * to implement capabilities probing API based on AVOption API. Should not be used directly.
345  */
346 typedef struct AVDeviceCapabilitiesQuery {
347     const AVClass *class;
348     AVFormatContext *device_context;
349     enum AVCodecID codec;
350     enum AVSampleFormat sample_format;
351     enum AVPixelFormat pixel_format;
352     int sample_rate;
353     int channels;
354     int64_t channel_layout;
355     int window_width;
356     int window_height;
357     int frame_width;
358     int frame_height;
359     AVRational fps;
360 } AVDeviceCapabilitiesQuery;
361
362 /**
363  * AVOption table used by devices to implement device capabilites API. Should not be used by a user.
364  */
365 extern const AVOption av_device_capabilities[];
366
367 /**
368  * Initialize capabilities probing API based on AVOption API.
369  *
370  * avdevice_capabilities_free() must be called when query capabilities API is
371  * not used anymore.
372  *
373  * @param[out] caps      Device capabilities data. Pointer to a NULL pointer must be passed.
374  * @param s              Context of the device.
375  * @param device_options An AVDictionary filled with device-private options.
376  *                       On return this parameter will be destroyed and replaced with a dict
377  *                       containing options that were not found. May be NULL.
378  *                       The same options must be passed later to avformat_write_header() for output
379  *                       devices or avformat_open_input() for input devices, or at any other place
380  *                       that affects device-private options.
381  *
382  * @return >= 0 on success, negative otherwise.
383  */
384 int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
385                                  AVDictionary **device_options);
386
387 /**
388  * Free resources created by avdevice_capabilities_create()
389  *
390  * @param caps Device capabilities data to be freed.
391  * @param s    Context of the device.
392  */
393 void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s);
394
395 /**
396  * Structure describes basic parameters of the device.
397  */
398 typedef struct AVDeviceInfo {
399     char *device_name;                   /**< device name, format depends on device */
400     char *device_description;            /**< human friendly name */
401 } AVDeviceInfo;
402
403 /**
404  * List of devices.
405  */
406 typedef struct AVDeviceInfoList {
407     AVDeviceInfo **devices;              /**< list of autodetected devices */
408     int nb_devices;                      /**< number of autodetected devices */
409     int default_device;                  /**< index of default device or -1 if no default */
410 } AVDeviceInfoList;
411
412 /**
413  * List devices.
414  *
415  * Returns available device names and their parameters.
416  *
417  * @note: Some devices may accept system-dependent device names that cannot be
418  *        autodetected. The list returned by this function cannot be assumed to
419  *        be always completed.
420  *
421  * @param s                device context.
422  * @param[out] device_list list of autodetected devices.
423  * @return count of autodetected devices, negative on error.
424  */
425 int avdevice_list_devices(struct AVFormatContext *s, AVDeviceInfoList **device_list);
426
427 /**
428  * Convinient function to free result of avdevice_list_devices().
429  *
430  * @param devices device list to be freed.
431  */
432 void avdevice_free_list_devices(AVDeviceInfoList **device_list);
433
434 #endif /* AVDEVICE_AVDEVICE_H */