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 module ffmpeg.libavdevice.avdevice;
19 
20 import ffmpeg.libavformat;
21 import ffmpeg.libavutil;
22 import ffmpeg.libavcodec;
23 
24 extern (C) @nogc nothrow:
25 
26 /**
27  * @file
28  * @ingroup lavd
29  * Main libavdevice API header
30  */
31 
32 /**
33  * @defgroup lavd libavdevice
34  * Special devices muxing/demuxing library.
35  *
36  * Libavdevice is a complementary library to @ref libavf "libavformat". It
37  * provides various "special" platform-specific muxers and demuxers, e.g. for
38  * grabbing devices, audio capture and playback etc. As a consequence, the
39  * (de)muxers in libavdevice are of the AVFMT_NOFILE type (they use their own
40  * I/O functions). The filename passed to avformat_open_input() often does not
41  * refer to an actually existing file, but has some special device-specific
42  * meaning - e.g. for xcbgrab it is the display name.
43  *
44  * To use libavdevice, simply call avdevice_register_all() to register all
45  * compiled muxers and demuxers. They all use standard libavformat API.
46  *
47  * @{
48  */
49 
50 /**
51  * Return the LIBAVDEVICE_VERSION_INT constant.
52  */
53 uint avdevice_version ();
54 
55 /**
56  * Return the libavdevice build-time configuration.
57  */
58 const(char)* avdevice_configuration ();
59 
60 /**
61  * Return the libavdevice license.
62  */
63 const(char)* avdevice_license ();
64 
65 /**
66  * Initialize libavdevice and register all the input and output devices.
67  */
68 void avdevice_register_all ();
69 
70 /**
71  * Audio input devices iterator.
72  *
73  * If d is NULL, returns the first registered input audio/video device,
74  * if d is non-NULL, returns the next registered input audio/video device after d
75  * or NULL if d is the last one.
76  */
77 AVInputFormat* av_input_audio_device_next (AVInputFormat* d);
78 
79 /**
80  * Video input devices iterator.
81  *
82  * If d is NULL, returns the first registered input audio/video device,
83  * if d is non-NULL, returns the next registered input audio/video device after d
84  * or NULL if d is the last one.
85  */
86 AVInputFormat* av_input_video_device_next (AVInputFormat* d);
87 
88 /**
89  * Audio output devices iterator.
90  *
91  * If d is NULL, returns the first registered output audio/video device,
92  * if d is non-NULL, returns the next registered output audio/video device after d
93  * or NULL if d is the last one.
94  */
95 AVOutputFormat* av_output_audio_device_next (AVOutputFormat* d);
96 
97 /**
98  * Video output devices iterator.
99  *
100  * If d is NULL, returns the first registered output audio/video device,
101  * if d is non-NULL, returns the next registered output audio/video device after d
102  * or NULL if d is the last one.
103  */
104 AVOutputFormat* av_output_video_device_next (AVOutputFormat* d);
105 
106 struct AVDeviceRect
107 {
108     int x; /**< x coordinate of top left corner */
109     int y; /**< y coordinate of top left corner */
110     int width; /**< width */
111     int height; /**< height */
112 }
113 
114 /**
115  * Message types used by avdevice_app_to_dev_control_message().
116  */
117 enum AVAppToDevMessageType
118 {
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 has to be repainted.
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      * Volume control message.
160      *
161      * Set volume level. It may be device-dependent if volume
162      * is changed per stream or system wide. Per stream volume
163      * change is expected when possible.
164      *
165      * data: double: new volume with range of 0.0 - 1.0.
166      */
167     AV_APP_TO_DEV_SET_VOLUME = MKBETAG('S', 'V', 'O', 'L'),
168 
169     /**
170      * Mute control messages.
171      *
172      * Change mute state. It may be device-dependent if mute status
173      * is changed per stream or system wide. Per stream mute status
174      * change is expected when possible.
175      *
176      * data: NULL.
177      */
178     AV_APP_TO_DEV_MUTE = MKBETAG(' ', 'M', 'U', 'T'),
179     AV_APP_TO_DEV_UNMUTE = MKBETAG('U', 'M', 'U', 'T'),
180     AV_APP_TO_DEV_TOGGLE_MUTE = MKBETAG('T', 'M', 'U', 'T'),
181 
182     /**
183      * Get volume/mute messages.
184      *
185      * Force the device to send AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED or
186      * AV_DEV_TO_APP_MUTE_STATE_CHANGED command respectively.
187      *
188      * data: NULL.
189      */
190     AV_APP_TO_DEV_GET_VOLUME = MKBETAG('G', 'V', 'O', 'L'),
191     AV_APP_TO_DEV_GET_MUTE = MKBETAG('G', 'M', 'U', 'T')
192 }
193 
194 /**
195  * Message types used by avdevice_dev_to_app_control_message().
196  */
197 enum AVDevToAppMessageType
198 {
199     /**
200      * Dummy message.
201      */
202     AV_DEV_TO_APP_NONE = MKBETAG('N', 'O', 'N', 'E'),
203 
204     /**
205      * Create window buffer message.
206      *
207      * Device requests to create a window buffer. Exact meaning is device-
208      * and application-dependent. Message is sent before rendering first
209      * frame and all one-shot initializations should be done here.
210      * Application is allowed to ignore preferred window buffer size.
211      *
212      * @note: Application is obligated to inform about window buffer size
213      *        with AV_APP_TO_DEV_WINDOW_SIZE message.
214      *
215      * data: AVDeviceRect: preferred size of the window buffer.
216      *       NULL: no preferred size of the window buffer.
217      */
218     AV_DEV_TO_APP_CREATE_WINDOW_BUFFER = MKBETAG('B', 'C', 'R', 'E'),
219 
220     /**
221      * Prepare window buffer message.
222      *
223      * Device requests to prepare a window buffer for rendering.
224      * Exact meaning is device- and application-dependent.
225      * Message is sent before rendering of each frame.
226      *
227      * data: NULL.
228      */
229     AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER = MKBETAG('B', 'P', 'R', 'E'),
230 
231     /**
232      * Display window buffer message.
233      *
234      * Device requests to display a window buffer.
235      * Message is sent when new frame is ready to be displayed.
236      * Usually buffers need to be swapped in handler of this message.
237      *
238      * data: NULL.
239      */
240     AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER = MKBETAG('B', 'D', 'I', 'S'),
241 
242     /**
243      * Destroy window buffer message.
244      *
245      * Device requests to destroy a window buffer.
246      * Message is sent when device is about to be destroyed and window
247      * buffer is not required anymore.
248      *
249      * data: NULL.
250      */
251     AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER = MKBETAG('B', 'D', 'E', 'S'),
252 
253     /**
254      * Buffer fullness status messages.
255      *
256      * Device signals buffer overflow/underflow.
257      *
258      * data: NULL.
259      */
260     AV_DEV_TO_APP_BUFFER_OVERFLOW = MKBETAG('B', 'O', 'F', 'L'),
261     AV_DEV_TO_APP_BUFFER_UNDERFLOW = MKBETAG('B', 'U', 'F', 'L'),
262 
263     /**
264      * Buffer readable/writable.
265      *
266      * Device informs that buffer is readable/writable.
267      * When possible, device informs how many bytes can be read/write.
268      *
269      * @warning Device may not inform when number of bytes than can be read/write changes.
270      *
271      * data: int64_t: amount of bytes available to read/write.
272      *       NULL: amount of bytes available to read/write is not known.
273      */
274     AV_DEV_TO_APP_BUFFER_READABLE = MKBETAG('B', 'R', 'D', ' '),
275     AV_DEV_TO_APP_BUFFER_WRITABLE = MKBETAG('B', 'W', 'R', ' '),
276 
277     /**
278      * Mute state change message.
279      *
280      * Device informs that mute state has changed.
281      *
282      * data: int: 0 for not muted state, non-zero for muted state.
283      */
284     AV_DEV_TO_APP_MUTE_STATE_CHANGED = MKBETAG('C', 'M', 'U', 'T'),
285 
286     /**
287      * Volume level change message.
288      *
289      * Device informs that volume level has changed.
290      *
291      * data: double: new volume with range of 0.0 - 1.0.
292      */
293     AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED = MKBETAG('C', 'V', 'O', 'L')
294 }
295 
296 /**
297  * Send control message from application to device.
298  *
299  * @param s         device context.
300  * @param type      message type.
301  * @param data      message data. Exact type depends on message type.
302  * @param data_size size of message data.
303  * @return >= 0 on success, negative on error.
304  *         AVERROR(ENOSYS) when device doesn't implement handler of the message.
305  */
306 int avdevice_app_to_dev_control_message (
307     AVFormatContext* s,
308     AVAppToDevMessageType type,
309     void* data,
310     size_t data_size);
311 
312 /**
313  * Send control message from device to application.
314  *
315  * @param s         device context.
316  * @param type      message type.
317  * @param data      message data. Can be NULL.
318  * @param data_size size of message data.
319  * @return >= 0 on success, negative on error.
320  *         AVERROR(ENOSYS) when application doesn't implement handler of the message.
321  */
322 int avdevice_dev_to_app_control_message (
323     AVFormatContext* s,
324     AVDevToAppMessageType type,
325     void* data,
326     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 make it possible 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 {
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     long 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 extern __gshared 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 (
444     AVDeviceCapabilitiesQuery** caps,
445     AVFormatContext* s,
446     AVDictionary** device_options);
447 
448 /**
449  * Free resources created by avdevice_capabilities_create()
450  *
451  * @param caps Device capabilities data to be freed.
452  * @param s    Context of the device.
453  */
454 void avdevice_capabilities_free (
455     AVDeviceCapabilitiesQuery** caps,
456     AVFormatContext* s);
457 
458 /**
459  * Structure describes basic parameters of the device.
460  */
461 struct AVDeviceInfo
462 {
463     char* device_name; /**< device name, format depends on device */
464     char* device_description; /**< human friendly name */
465 }
466 
467 /**
468  * List of devices.
469  */
470 struct AVDeviceInfoList
471 {
472     AVDeviceInfo** devices; /**< list of autodetected devices */
473     int nb_devices; /**< number of autodetected devices */
474     int default_device; /**< index of default device or -1 if no default */
475 }
476 
477 /**
478  * List devices.
479  *
480  * Returns available device names and their parameters.
481  *
482  * @note: Some devices may accept system-dependent device names that cannot be
483  *        autodetected. The list returned by this function cannot be assumed to
484  *        be always completed.
485  *
486  * @param s                device context.
487  * @param[out] device_list list of autodetected devices.
488  * @return count of autodetected devices, negative on error.
489  */
490 int avdevice_list_devices (AVFormatContext* s, AVDeviceInfoList** device_list);
491 
492 /**
493  * Convenient function to free result of avdevice_list_devices().
494  *
495  * @param devices device list to be freed.
496  */
497 void avdevice_free_list_devices (AVDeviceInfoList** device_list);
498 
499 /**
500  * List devices.
501  *
502  * Returns available device names and their parameters.
503  * These are convinient wrappers for avdevice_list_devices().
504  * Device context is allocated and deallocated internally.
505  *
506  * @param device           device format. May be NULL if device name is set.
507  * @param device_name      device name. May be NULL if device format is set.
508  * @param device_options   An AVDictionary filled with device-private options. May be NULL.
509  *                         The same options must be passed later to avformat_write_header() for output
510  *                         devices or avformat_open_input() for input devices, or at any other place
511  *                         that affects device-private options.
512  * @param[out] device_list list of autodetected devices
513  * @return count of autodetected devices, negative on error.
514  * @note device argument takes precedence over device_name when both are set.
515  */
516 int avdevice_list_input_sources (
517     AVInputFormat* device,
518     const(char)* device_name,
519     AVDictionary* device_options,
520     AVDeviceInfoList** device_list);
521 int avdevice_list_output_sinks (
522     AVOutputFormat* device,
523     const(char)* device_name,
524     AVDictionary* device_options,
525     AVDeviceInfoList** device_list);
526 
527 /**
528  * @}
529  */
530 
531 /* AVDEVICE_AVDEVICE_H */