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