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.

435 lines
15KB

  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 Special devices muxing/demuxing library
  28. * @{
  29. * Libavdevice is a complementary library to @ref libavf "libavformat". It
  30. * provides various "special" platform-specific muxers and demuxers, e.g. for
  31. * grabbing devices, audio capture and playback etc. As a consequence, the
  32. * (de)muxers in libavdevice are of the AVFMT_NOFILE type (they use their own
  33. * I/O functions). The filename passed to avformat_open_input() often does not
  34. * refer to an actually existing file, but has some special device-specific
  35. * meaning - e.g. for x11grab it is the display name.
  36. *
  37. * To use libavdevice, simply call avdevice_register_all() to register all
  38. * compiled muxers and demuxers. They all use standard libavformat API.
  39. * @}
  40. */
  41. #include "libavutil/log.h"
  42. #include "libavutil/opt.h"
  43. #include "libavutil/dict.h"
  44. #include "libavformat/avformat.h"
  45. /**
  46. * Return the LIBAVDEVICE_VERSION_INT constant.
  47. */
  48. unsigned avdevice_version(void);
  49. /**
  50. * Return the libavdevice build-time configuration.
  51. */
  52. const char *avdevice_configuration(void);
  53. /**
  54. * Return the libavdevice license.
  55. */
  56. const char *avdevice_license(void);
  57. /**
  58. * Initialize libavdevice and register all the input and output devices.
  59. * @warning This function is not thread safe.
  60. */
  61. void avdevice_register_all(void);
  62. /**
  63. * Audio input devices iterator.
  64. *
  65. * If d is NULL, returns the first registered input audio/video device,
  66. * if d is non-NULL, returns the next registered input audio/video device after d
  67. * or NULL if d is the last one.
  68. */
  69. AVInputFormat *av_input_audio_device_next(AVInputFormat *d);
  70. /**
  71. * Video 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_video_device_next(AVInputFormat *d);
  78. /**
  79. * Audio output devices iterator.
  80. *
  81. * If d is NULL, returns the first registered output audio/video device,
  82. * if d is non-NULL, returns the next registered output audio/video device after d
  83. * or NULL if d is the last one.
  84. */
  85. AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d);
  86. /**
  87. * Video 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_video_device_next(AVOutputFormat *d);
  94. typedef struct AVDeviceRect {
  95. int x; /**< x coordinate of top left corner */
  96. int y; /**< y coordinate of top left corner */
  97. int width; /**< width */
  98. int height; /**< height */
  99. } AVDeviceRect;
  100. /**
  101. * Message types used by avdevice_app_to_dev_control_message().
  102. */
  103. enum AVAppToDevMessageType {
  104. /**
  105. * Dummy message.
  106. */
  107. AV_APP_TO_DEV_NONE = MKBETAG('N','O','N','E'),
  108. /**
  109. * Window size change message.
  110. *
  111. * Message is sent to the device every time the application changes the size
  112. * of the window device renders to.
  113. * Message should also be sent right after window is created.
  114. *
  115. * data: AVDeviceRect: new window size.
  116. */
  117. AV_APP_TO_DEV_WINDOW_SIZE = MKBETAG('G','E','O','M'),
  118. /**
  119. * Repaint request message.
  120. *
  121. * Message is sent to the device when window have to be rapainted.
  122. *
  123. * data: AVDeviceRect: area required to be repainted.
  124. * NULL: whole area is required to be repainted.
  125. */
  126. AV_APP_TO_DEV_WINDOW_REPAINT = MKBETAG('R','E','P','A'),
  127. /**
  128. * Request pause/play.
  129. *
  130. * Application requests pause/unpause playback.
  131. * Mostly usable with devices that have internal buffer.
  132. * By default devices are not paused.
  133. *
  134. * data: NULL
  135. */
  136. AV_APP_TO_DEV_PAUSE = MKBETAG('P', 'A', 'U', ' '),
  137. AV_APP_TO_DEV_PLAY = MKBETAG('P', 'L', 'A', 'Y'),
  138. AV_APP_TO_DEV_TOGGLE_PAUSE = MKBETAG('P', 'A', 'U', 'T'),
  139. };
  140. /**
  141. * Message types used by avdevice_dev_to_app_control_message().
  142. */
  143. enum AVDevToAppMessageType {
  144. /**
  145. * Dummy message.
  146. */
  147. AV_DEV_TO_APP_NONE = MKBETAG('N','O','N','E'),
  148. /**
  149. * Create window buffer message.
  150. *
  151. * Device requests to create a window buffer. Exact meaning is device-
  152. * and application-dependent. Message is sent before rendering first
  153. * frame and all one-shot initializations should be done here.
  154. * Application is allowed to ignore preferred window buffer size.
  155. *
  156. * @note: Application is obligated to inform about window buffer size
  157. * with AV_APP_TO_DEV_WINDOW_SIZE message.
  158. *
  159. * data: AVDeviceRect: preferred size of the window buffer.
  160. * NULL: no preferred size of the window buffer.
  161. */
  162. AV_DEV_TO_APP_CREATE_WINDOW_BUFFER = MKBETAG('B','C','R','E'),
  163. /**
  164. * Prepare window buffer message.
  165. *
  166. * Device requests to prepare a window buffer for rendering.
  167. * Exact meaning is device- and application-dependent.
  168. * Message is sent before rendering of each frame.
  169. *
  170. * data: NULL.
  171. */
  172. AV_DEV_TO_APP_PREPARE_WINDOW_BUFFER = MKBETAG('B','P','R','E'),
  173. /**
  174. * Display window buffer message.
  175. *
  176. * Device requests to display a window buffer.
  177. * Message is sent when new frame is ready to be displyed.
  178. * Usually buffers need to be swapped in handler of this message.
  179. *
  180. * data: NULL.
  181. */
  182. AV_DEV_TO_APP_DISPLAY_WINDOW_BUFFER = MKBETAG('B','D','I','S'),
  183. /**
  184. * Destroy window buffer message.
  185. *
  186. * Device requests to destroy a window buffer.
  187. * Message is sent when device is about to be destroyed and window
  188. * buffer is not required anymore.
  189. *
  190. * data: NULL.
  191. */
  192. AV_DEV_TO_APP_DESTROY_WINDOW_BUFFER = MKBETAG('B','D','E','S'),
  193. /**
  194. * Buffer fullness status messages.
  195. *
  196. * Device signals buffer overflow/underflow.
  197. *
  198. * data: NULL.
  199. */
  200. AV_DEV_TO_APP_BUFFER_OVERFLOW = MKBETAG('B','O','F','L'),
  201. AV_DEV_TO_APP_BUFFER_UNDERFLOW = MKBETAG('B','U','F','L'),
  202. /**
  203. * Buffer readable/writable.
  204. *
  205. * Device informs that buffer is readable/writable.
  206. * When possible, device informs how many bytes can be read/write.
  207. *
  208. * @warning Device may not inform when number of bytes than can be read/write changes.
  209. *
  210. * data: int64_t: amount of bytes available to read/write.
  211. * NULL: amount of bytes available to read/write is not known.
  212. */
  213. AV_DEV_TO_APP_BUFFER_READABLE = MKBETAG('B','R','D',' '),
  214. AV_DEV_TO_APP_BUFFER_WRITABLE = MKBETAG('B','W','R',' '),
  215. };
  216. /**
  217. * Send control message from application to device.
  218. *
  219. * @param s device context.
  220. * @param type message type.
  221. * @param data message data. Exact type depends on message type.
  222. * @param data_size size of message data.
  223. * @return >= 0 on success, negative on error.
  224. * AVERROR(ENOSYS) when device doesn't implement handler of the message.
  225. */
  226. int avdevice_app_to_dev_control_message(struct AVFormatContext *s,
  227. enum AVAppToDevMessageType type,
  228. void *data, size_t data_size);
  229. /**
  230. * Send control message from device to application.
  231. *
  232. * @param s device context.
  233. * @param type message type.
  234. * @param data message data. Can be NULL.
  235. * @param data_size size of message data.
  236. * @return >= 0 on success, negative on error.
  237. * AVERROR(ENOSYS) when application doesn't implement handler of the message.
  238. */
  239. int avdevice_dev_to_app_control_message(struct AVFormatContext *s,
  240. enum AVDevToAppMessageType type,
  241. void *data, size_t data_size);
  242. /**
  243. * Following API allows user to probe device capabilities (supported codecs,
  244. * pixel formats, sample formats, resolutions, channel counts, etc).
  245. * It is build on top op AVOption API.
  246. * Queried capabilities allows to set up converters of video or audio
  247. * parameters that fit to the device.
  248. *
  249. * List of capablities that can be queried:
  250. * - Capabilities valid for both audio and video devices:
  251. * - codec: supported audio/video codecs.
  252. * type: AV_OPT_TYPE_INT (AVCodecID value)
  253. * - Capabilities valid for audio devices:
  254. * - sample_format: supported sample formats.
  255. * type: AV_OPT_TYPE_INT (AVSampleFormat value)
  256. * - sample_rate: supported sample rates.
  257. * type: AV_OPT_TYPE_INT
  258. * - channels: supported number of channels.
  259. * type: AV_OPT_TYPE_INT
  260. * - channel_layout: supported channel layouts.
  261. * type: AV_OPT_TYPE_INT64
  262. * - Capabilities valid for video devices:
  263. * - pixel_format: supported pixel formats.
  264. * type: AV_OPT_TYPE_INT (AVPixelFormat value)
  265. * - window_size: supported window sizes (describes size of the window size presented to the user).
  266. * type: AV_OPT_TYPE_IMAGE_SIZE
  267. * - frame_size: supported frame sizes (describes size of provided video frames).
  268. * type: AV_OPT_TYPE_IMAGE_SIZE
  269. * - fps: supported fps values
  270. * type: AV_OPT_TYPE_RATIONAL
  271. *
  272. * Value of the capability may be set by user using av_opt_set() function
  273. * and AVDeviceCapabilitiesQuery object. Following queries will
  274. * limit results to the values matching already set capabilities.
  275. * For example, setting a codec may impact number of formats or fps values
  276. * returned during next query. Setting invalid value may limit results to zero.
  277. *
  278. * Example of the usage basing on opengl output device:
  279. *
  280. * @code
  281. * AVFormatContext *oc = NULL;
  282. * AVDeviceCapabilitiesQuery *caps = NULL;
  283. * AVOptionRanges *ranges;
  284. * int ret;
  285. *
  286. * if ((ret = avformat_alloc_output_context2(&oc, NULL, "opengl", NULL)) < 0)
  287. * goto fail;
  288. * if (avdevice_capabilities_create(&caps, oc, NULL) < 0)
  289. * goto fail;
  290. *
  291. * //query codecs
  292. * if (av_opt_query_ranges(&ranges, caps, "codec", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
  293. * goto fail;
  294. * //pick codec here and set it
  295. * av_opt_set(caps, "codec", AV_CODEC_ID_RAWVIDEO, 0);
  296. *
  297. * //query format
  298. * if (av_opt_query_ranges(&ranges, caps, "pixel_format", AV_OPT_MULTI_COMPONENT_RANGE)) < 0)
  299. * goto fail;
  300. * //pick format here and set it
  301. * av_opt_set(caps, "pixel_format", AV_PIX_FMT_YUV420P, 0);
  302. *
  303. * //query and set more capabilities
  304. *
  305. * fail:
  306. * //clean up code
  307. * avdevice_capabilities_free(&query, oc);
  308. * avformat_free_context(oc);
  309. * @endcode
  310. */
  311. /**
  312. * Structure describes device capabilites.
  313. *
  314. * It is used by devices in conjuntion with av_device_capabilities AVOption table
  315. * to implement capabilities probing API based on AVOption API. Should not be used directly.
  316. */
  317. typedef struct AVDeviceCapabilitiesQuery {
  318. const AVClass *av_class;
  319. AVFormatContext *device_context;
  320. enum AVCodecID codec;
  321. enum AVSampleFormat sample_format;
  322. enum AVPixelFormat pixel_format;
  323. int sample_rate;
  324. int channels;
  325. int64_t channel_layout;
  326. int window_width;
  327. int window_height;
  328. int frame_width;
  329. int frame_height;
  330. AVRational fps;
  331. } AVDeviceCapabilitiesQuery;
  332. /**
  333. * AVOption table used by devices to implement device capabilites API. Should not be used by a user.
  334. */
  335. extern const AVOption av_device_capabilities[];
  336. /**
  337. * Initialize capabilities probing API based on AVOption API.
  338. *
  339. * avdevice_capabilities_free() must be called when query capabilities API is
  340. * not used anymore.
  341. *
  342. * @param[out] caps Device capabilities data. Pointer to a NULL pointer must be passed.
  343. * @param s Context of the device.
  344. * @param device_options An AVDictionary filled with device-private options.
  345. * On return this parameter will be destroyed and replaced with a dict
  346. * containing options that were not found. May be NULL.
  347. * The same options must be passed later to avformat_write_header() for output
  348. * devices or avformat_open_input() for input devices, or at any other place
  349. * that affects device-private options.
  350. *
  351. * @return >= 0 on success, negative otherwise.
  352. */
  353. int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
  354. AVDictionary **device_options);
  355. /**
  356. * Free resources created by avdevice_capabilities_create()
  357. *
  358. * @param caps Device capabilities data to be freed.
  359. * @param s Context of the device.
  360. */
  361. void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s);
  362. /**
  363. * Structure describes basic parameters of the device.
  364. */
  365. typedef struct AVDeviceInfo {
  366. char *device_name; /**< device name, format depends on device */
  367. char *device_description; /**< human friendly name */
  368. } AVDeviceInfo;
  369. /**
  370. * List of devices.
  371. */
  372. typedef struct AVDeviceInfoList {
  373. AVDeviceInfo **devices; /**< list of autodetected devices */
  374. int nb_devices; /**< number of autodetected devices */
  375. int default_device; /**< index of default device or -1 if no default */
  376. } AVDeviceInfoList;
  377. /**
  378. * List devices.
  379. *
  380. * Returns available device names and their parameters.
  381. *
  382. * @note: Some devices may accept system-dependent device names that cannot be
  383. * autodetected. The list returned by this function cannot be assumed to
  384. * be always completed.
  385. *
  386. * @param s device context.
  387. * @param[out] device_list list of autodetected devices.
  388. * @return count of autodetected devices, negative on error.
  389. */
  390. int avdevice_list_devices(struct AVFormatContext *s, AVDeviceInfoList **device_list);
  391. /**
  392. * Convinient function to free result of avdevice_list_devices().
  393. *
  394. * @param devices device list to be freed.
  395. */
  396. void avdevice_free_list_devices(AVDeviceInfoList **device_list);
  397. #endif /* AVDEVICE_AVDEVICE_H */