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