You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

543 lines
18KB

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