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 module ffmpeg.libavdevice.avdevice;
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 import std.stdint;
46 import ffmpeg.libavutil.avutil;
47 import ffmpeg.libavutil.common;
48 import ffmpeg.libavutil.log;
49 import ffmpeg.libavutil.opt;
50 import ffmpeg.libavutil.dict;
51 import ffmpeg.libavformat.avformat;
52 import ffmpeg.libavcodec.avcodec;
53 import ffmpeg.libavdevice.avdevice_version;
54 
55 @nogc nothrow extern(C):
56 
57 /**
58  * Return the LIBAVDEVICE_VERSION_INT constant.
59  */
60 uint avdevice_version();
61 
62 /**
63  * Return the libavdevice build-time configuration.
64  */
65 char *avdevice_configuration();
66 
67 /**
68  * Return the libavdevice license.
69  */
70 char *avdevice_license();
71 
72 /**
73  * Initialize libavdevice and register all the input and output devices.
74  * @warning This function is not thread safe.
75  */
76 void avdevice_register_all();
77 
78 /**
79  * Audio input devices iterator.
80  *
81  * If d is NULL, returns the first registered input audio/video device,
82  * if d is non-NULL, returns the next registered input audio/video device after d
83  * or NULL if d is the last one.
84  */
85 AVInputFormat *av_input_audio_device_next(AVInputFormat  *d);
86 
87 /**
88  * Video input devices iterator.
89  *
90  * If d is NULL, returns the first registered input audio/video device,
91  * if d is non-NULL, returns the next registered input audio/video device after d
92  * or NULL if d is the last one.
93  */
94 AVInputFormat *av_input_video_device_next(AVInputFormat  *d);
95 
96 /**
97  * Audio output devices iterator.
98  *
99  * If d is NULL, returns the first registered output audio/video device,
100  * if d is non-NULL, returns the next registered output audio/video device after d
101  * or NULL if d is the last one.
102  */
103 AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d);
104 
105 /**
106  * Video output devices iterator.
107  *
108  * If d is NULL, returns the first registered output audio/video device,
109  * if d is non-NULL, returns the next registered output audio/video device after d
110  * or NULL if d is the last one.
111  */
112 AVOutputFormat *av_output_video_device_next(AVOutputFormat *d);
113 
114 struct AVDeviceRect {
115     int x;      /**< x coordinate of top left corner */
116     int y;      /**< y coordinate of top left corner */
117     int width;  /**< width */
118     int height; /**< height */
119 }
120 
121 /**
122  * Message types used by avdevice_app_to_dev_control_message().
123  */
124 enum AVAppToDevMessageType {
125     /**
126      * Dummy message.
127      */
128     AV_APP_TO_DEV_NONE = MKBETAG!('N','O','N','E'),
129 
130     /**
131      * Window size change message.
132      *
133      * Message is sent to the device every time the application changes the size
134      * of the window device renders to.
135      * Message should also be sent right after window is created.
136      *
137      * data: AVDeviceRect: new window size.
138      */
139     AV_APP_TO_DEV_WINDOW_SIZE = MKBETAG!('G','E','O','M'),
140 
141     /**
142      * Repaint request message.
143      *
144      * Message is sent to the device when window has to be repainted.
145      *
146      * data: AVDeviceRect: area required to be repainted.
147      *       NULL: whole area is required to be repainted.
148      */
149     AV_APP_TO_DEV_WINDOW_REPAINT = MKBETAG!('R','E','P','A'),
150 
151     /**
152      * Request pause/play.
153      *
154      * Application requests pause/unpause playback.
155      * Mostly usable with devices that have internal buffer.
156      * By default devices are not paused.
157      *
158      * data: NULL
159      */
160     AV_APP_TO_DEV_PAUSE        = MKBETAG!('P', 'A', 'U', ' '),
161     AV_APP_TO_DEV_PLAY         = MKBETAG!('P', 'L', 'A', 'Y'),
162     AV_APP_TO_DEV_TOGGLE_PAUSE = MKBETAG!('P', 'A', 'U', 'T'),
163 
164     /**
165      * Volume control message.
166      *
167      * Set volume level. It may be device-dependent if volume
168      * is changed per stream or system wide. Per stream volume
169      * change is expected when possible.
170      *
171      * data: double: new volume with range of 0.0 - 1.0.
172      */
173     AV_APP_TO_DEV_SET_VOLUME = MKBETAG!('S', 'V', 'O', 'L'),
174 
175     /**
176      * Mute control messages.
177      *
178      * Change mute state. It may be device-dependent if mute status
179      * is changed per stream or system wide. Per stream mute status
180      * change is expected when possible.
181      *
182      * data: NULL.
183      */
184     AV_APP_TO_DEV_MUTE        = MKBETAG!(' ', 'M', 'U', 'T'),
185     AV_APP_TO_DEV_UNMUTE      = MKBETAG!('U', 'M', 'U', 'T'),
186     AV_APP_TO_DEV_TOGGLE_MUTE = MKBETAG!('T', 'M', 'U', 'T'),
187 
188     /**
189      * Get volume/mute messages.
190      *
191      * Force the device to send AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED or
192      * AV_DEV_TO_APP_MUTE_STATE_CHANGED command respectively.
193      *
194      * data: NULL.
195      */
196     AV_APP_TO_DEV_GET_VOLUME = MKBETAG!('G', 'V', 'O', 'L'),
197     AV_APP_TO_DEV_GET_MUTE   = MKBETAG!('G', 'M', 'U', 'T')
198 }
199 
200 /**
201  * Message types used by avdevice_dev_to_app_control_message().
202  */
203 enum AVDevToAppMessageType {
204     /**
205      * Dummy message.
206      */
207     AV_DEV_TO_APP_NONE = MKBETAG!('N','O','N','E'),
208 
209     /**
210      * Create window buffer message.
211      *
212      * Device requests to create a window buffer. Exact meaning is device-
213      * and application-dependent. Message is sent before rendering first
214      * frame and all one-shot initializations should be done here.
215      * Application is allowed to ignore preferred window buffer size.
216      *
217      * @note: Application is obligated to inform about window buffer size
218      *        with AV_APP_TO_DEV_WINDOW_SIZE message.
219      *
220      * data: AVDeviceRect: preferred size of the window buffer.
221      *       NULL: no preferred size of the window buffer.
222      */
223     AV_DEV_TO_APP_CREATE_WINDOW_BUFFER = MKBETAG!('B','C','R','E'),
224 
225     /**
226      * Prepare window buffer message.
227      *
228      * Device requests to prepare a window buffer for rendering.
229      * Exact meaning is device- and application-dependent.
230      * Message is sent before rendering of each frame.
231      *
232      * data: NULL.
233      */
234     AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER = MKBETAG!('B','P','R','E'),
235 
236     /**
237      * Display window buffer message.
238      *
239      * Device requests to display a window buffer.
240      * Message is sent when new frame is ready to be displayed.
241      * Usually buffers need to be swapped in handler of this message.
242      *
243      * data: NULL.
244      */
245     AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER = MKBETAG!('B','D','I','S'),
246 
247     /**
248      * Destroy window buffer message.
249      *
250      * Device requests to destroy a window buffer.
251      * Message is sent when device is about to be destroyed and window
252      * buffer is not required anymore.
253      *
254      * data: NULL.
255      */
256     AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER = MKBETAG!('B','D','E','S'),
257 
258     /**
259      * Buffer fullness status messages.
260      *
261      * Device signals buffer overflow/underflow.
262      *
263      * data: NULL.
264      */
265     AV_DEV_TO_APP_BUFFER_OVERFLOW = MKBETAG!('B','O','F','L'),
266     AV_DEV_TO_APP_BUFFER_UNDERFLOW = MKBETAG!('B','U','F','L'),
267 
268     /**
269      * Buffer readable/writable.
270      *
271      * Device informs that buffer is readable/writable.
272      * When possible, device informs how many bytes can be read/write.
273      *
274      * @warning Device may not inform when number of bytes than can be read/write changes.
275      *
276      * data: int64_t: amount of bytes available to read/write.
277      *       NULL: amount of bytes available to read/write is not known.
278      */
279     AV_DEV_TO_APP_BUFFER_READABLE = MKBETAG!('B','R','D',' '),
280     AV_DEV_TO_APP_BUFFER_WRITABLE = MKBETAG!('B','W','R',' '),
281 
282     /**
283      * Mute state change message.
284      *
285      * Device informs that mute state has changed.
286      *
287      * data: int: 0 for not muted state, non-zero for muted state.
288      */
289     AV_DEV_TO_APP_MUTE_STATE_CHANGED = MKBETAG!('C','M','U','T'),
290 
291     /**
292      * Volume level change message.
293      *
294      * Device informs that volume level has changed.
295      *
296      * data: double: new volume with range of 0.0 - 1.0.
297      */
298     AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED = MKBETAG!('C','V','O','L')
299 }
300 
301 /**
302  * Send control message from application to device.
303  *
304  * @param s         device context.
305  * @param type      message type.
306  * @param data      message data. Exact type depends on message type.
307  * @param data_size size of message data.
308  * @return >= 0 on success, negative on error.
309  *         AVERROR(ENOSYS) when device doesn't implement handler of the message.
310  */
311 int avdevice_app_to_dev_control_message(AVFormatContext *s,
312                                         AVAppToDevMessageType type,
313                                         void *data, size_t data_size);
314 
315 /**
316  * Send control message from device to application.
317  *
318  * @param s         device context.
319  * @param type      message type.
320  * @param data      message data. Can be NULL.
321  * @param data_size size of message data.
322  * @return >= 0 on success, negative on error.
323  *         AVERROR(ENOSYS) when application doesn't implement handler of the message.
324  */
325 int avdevice_dev_to_app_control_message(AVFormatContext *s,
326                                         AVDevToAppMessageType type,
327                                         void *data, size_t data_size);
328 
329 /**
330  * Following API allows user to probe device capabilities (supported codecs,
331  * pixel formats, sample formats, resolutions, channel counts, etc).
332  * It is build on top op AVOption API.
333  * Queried capabilities allows to set up converters of video or audio
334  * parameters that fit to the device.
335  *
336  * List of capabilities that can be queried:
337  *  - Capabilities valid for both audio and video devices:
338  *    - codec:          supported audio/video codecs.
339  *                      type: AV_OPT_TYPE_INT (AVCodecID value)
340  *  - Capabilities valid for audio devices:
341  *    - sample_format:  supported sample formats.
342  *                      type: AV_OPT_TYPE_INT (AVSampleFormat value)
343  *    - sample_rate:    supported sample rates.
344  *                      type: AV_OPT_TYPE_INT
345  *    - channels:       supported number of channels.
346  *                      type: AV_OPT_TYPE_INT
347  *    - channel_layout: supported channel layouts.
348  *                      type: AV_OPT_TYPE_INT64
349  *  - Capabilities valid for video devices:
350  *    - pixel_format:   supported pixel formats.
351  *                      type: AV_OPT_TYPE_INT (AVPixelFormat value)
352  *    - window_size:    supported window sizes (describes size of the window size presented to the user).
353  *                      type: AV_OPT_TYPE_IMAGE_SIZE
354  *    - frame_size:     supported frame sizes (describes size of provided video frames).
355  *                      type: AV_OPT_TYPE_IMAGE_SIZE
356  *    - fps:            supported fps values
357  *                      type: AV_OPT_TYPE_RATIONAL
358  *
359  * Value of the capability may be set by user using av_opt_set() function
360  * and AVDeviceCapabilitiesQuery object. Following queries will
361  * limit results to the values matching already set capabilities.
362  * For example, setting a codec may impact number of formats or fps values
363  * returned during next query. Setting invalid value may limit results to zero.
364  *
365  * Example of the usage basing on opengl output device:
366  *
367  * @code
368  *  AVFormatContext *oc = NULL;
369  *  AVDeviceCapabilitiesQuery *caps = NULL;
370  *  AVOptionRanges *ranges;
371  *  int ret;
372  *
373  *  if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
374  *      goto fail;
375  *  if (avdevice_capabilities_create(&caps, oc, NULL) < 0)
376  *      goto fail;
377  *
378  *  //query codecs
379  *  if (av_opt_query_ranges(&ranges, caps, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
380  *      goto fail;
381  *  //pick codec here and set it
382  *  av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);
383  *
384  *  //query format
385  *  if (av_opt_query_ranges(&ranges, caps, "pixel_format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
386  *      goto fail;
387  *  //pick format here and set it
388  *  av_opt_set(caps, "pixel_format", AV_PIX_FMT_YUV420P, 0);
389  *
390  *  //query and set more capabilities
391  *
392  * fail:
393  *  //clean up code
394  *  avdevice_capabilities_free(&query, oc);
395  *  avformat_free_context(oc);
396  * @endcode
397  */
398 
399 /**
400  * Structure describes device capabilities.
401  *
402  * It is used by devices in conjunction with av_device_capabilities AVOption table
403  * to implement capabilities probing API based on AVOption API. Should not be used directly.
404  */
405 struct AVDeviceCapabilitiesQuery {
406     const AVClass *av_class;
407     AVFormatContext *device_context;
408     AVCodecID codec;
409     AVSampleFormat sample_format;
410     AVPixelFormat pixel_format;
411     int sample_rate;
412     int channels;
413     int64_t channel_layout;
414     int window_width;
415     int window_height;
416     int frame_width;
417     int frame_height;
418     AVRational fps;
419 }
420 
421 /**
422  * AVOption table used by devices to implement device capabilities API. Should not be used by a user.
423  */
424 const AVOption []av_device_capabilities;
425 
426 /**
427  * Initialize capabilities probing API based on AVOption API.
428  *
429  * avdevice_capabilities_free() must be called when query capabilities API is
430  * not used anymore.
431  *
432  * @param[out] caps      Device capabilities data. Pointer to a NULL pointer must be passed.
433  * @param s              Context of the device.
434  * @param device_options An AVDictionary filled with device-private options.
435  *                       On return this parameter will be destroyed and replaced with a dict
436  *                       containing options that were not found. May be NULL.
437  *                       The same options must be passed later to avformat_write_header() for output
438  *                       devices or avformat_open_input() for input devices, or at any other place
439  *                       that affects device-private options.
440  *
441  * @return >= 0 on success, negative otherwise.
442  */
443 int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
444                                  AVDictionary **device_options);
445 
446 /**
447  * Free resources created by avdevice_capabilities_create()
448  *
449  * @param caps Device capabilities data to be freed.
450  * @param s    Context of the device.
451  */
452 void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s);
453 
454 /**
455  * Structure describes basic parameters of the device.
456  */
457 struct AVDeviceInfo {
458     char *device_name;                   /**< device name, format depends on device */
459     char *device_description;            /**< human friendly name */
460 }
461 
462 /**
463  * List of devices.
464  */
465 struct AVDeviceInfoList {
466     AVDeviceInfo **devices;              /**< list of autodetected devices */
467     int nb_devices;                      /**< number of autodetected devices */
468     int default_device;                  /**< index of default device or -1 if no default */
469 }
470 
471 /**
472  * List devices.
473  *
474  * Returns available device names and their parameters.
475  *
476  * @note: Some devices may accept system-dependent device names that cannot be
477  *        autodetected. The list returned by this function cannot be assumed to
478  *        be always completed.
479  *
480  * @param s                device context.
481  * @param[out] device_list list of autodetected devices.
482  * @return count of autodetected devices, negative on error.
483  */
484 int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list);
485 
486 /**
487  * Convenient function to free result of avdevice_list_devices().
488  *
489  * @param devices device list to be freed.
490  */
491 void avdevice_free_list_devices(AVDeviceInfoList **device_list);
492 
493 
494 /**
495  * List devices.
496  *
497  * Returns available device names and their parameters.
498  * These are convinient wrappers for avdevice_list_devices().
499  * Device context is allocated and deallocated internally.
500  *
501  * @param device           device format. May be NULL if device name is set.
502  * @param device_name      device name. May be NULL if device format is set.
503  * @param device_options   An AVDictionary filled with device-private options. May be NULL.
504  *                         The same options must be passed later to avformat_write_header() for output
505  *                         devices or avformat_open_input() for input devices, or at any other place
506  *                         that affects device-private options.
507  * @param[out] device_list list of autodetected devices
508  * @return count of autodetected devices, negative on error.
509  * @note device argument takes precedence over device_name when both are set.
510  */
511 int avdevice_list_input_sources(AVInputFormat *device, const char *device_name,
512                                 AVDictionary *device_options, AVDeviceInfoList **device_list);
513 int avdevice_list_output_sinks(AVOutputFormat *device, const char *device_name,
514                                AVDictionary *device_options, AVDeviceInfoList **device_list);
515 
516 //#endif /* AVDEVICE_AVDEVICE_H */