avdevice.h 18 KB

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