avdevice.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #include "libavutil/avassert.h"
  19. #include "libavutil/samplefmt.h"
  20. #include "libavutil/pixfmt.h"
  21. #include "libavcodec/avcodec.h"
  22. #include "avdevice.h"
  23. #include "internal.h"
  24. #include "config.h"
  25. #include "libavutil/ffversion.h"
  26. const char av_device_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  27. #define E AV_OPT_FLAG_ENCODING_PARAM
  28. #define D AV_OPT_FLAG_DECODING_PARAM
  29. #define A AV_OPT_FLAG_AUDIO_PARAM
  30. #define V AV_OPT_FLAG_VIDEO_PARAM
  31. #define OFFSET(x) offsetof(AVDeviceCapabilitiesQuery, x)
  32. const AVOption av_device_capabilities[] = {
  33. { "codec", "codec", OFFSET(codec), AV_OPT_TYPE_INT,
  34. {.i64 = AV_CODEC_ID_NONE}, AV_CODEC_ID_NONE, INT_MAX, E|D|A|V },
  35. { "sample_format", "sample format", OFFSET(sample_format), AV_OPT_TYPE_SAMPLE_FMT,
  36. {.i64 = AV_SAMPLE_FMT_NONE}, AV_SAMPLE_FMT_NONE, INT_MAX, E|D|A },
  37. { "sample_rate", "sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT,
  38. {.i64 = -1}, -1, INT_MAX, E|D|A },
  39. { "channels", "channels", OFFSET(channels), AV_OPT_TYPE_INT,
  40. {.i64 = -1}, -1, INT_MAX, E|D|A },
  41. { "channel_layout", "channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT,
  42. {.i64 = -1}, -1, INT_MAX, E|D|A },
  43. { "pixel_format", "pixel format", OFFSET(pixel_format), AV_OPT_TYPE_PIXEL_FMT,
  44. {.i64 = AV_PIX_FMT_NONE}, AV_PIX_FMT_NONE, INT_MAX, E|D|V },
  45. { "window_size", "window size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,
  46. {.str = NULL}, -1, INT_MAX, E|D|V },
  47. { "frame_size", "frame size", OFFSET(frame_width), AV_OPT_TYPE_IMAGE_SIZE,
  48. {.str = NULL}, -1, INT_MAX, E|D|V },
  49. { "fps", "fps", OFFSET(fps), AV_OPT_TYPE_RATIONAL,
  50. {.dbl = -1}, -1, INT_MAX, E|D|V },
  51. { NULL }
  52. };
  53. #undef E
  54. #undef D
  55. #undef A
  56. #undef V
  57. #undef OFFSET
  58. unsigned avdevice_version(void)
  59. {
  60. av_assert0(LIBAVDEVICE_VERSION_MICRO >= 100);
  61. return LIBAVDEVICE_VERSION_INT;
  62. }
  63. const char * avdevice_configuration(void)
  64. {
  65. return FFMPEG_CONFIGURATION;
  66. }
  67. const char * avdevice_license(void)
  68. {
  69. #define LICENSE_PREFIX "libavdevice license: "
  70. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  71. }
  72. static void *device_next(void *prev, int output,
  73. AVClassCategory c1, AVClassCategory c2)
  74. {
  75. const AVClass *pc;
  76. AVClassCategory category = AV_CLASS_CATEGORY_NA;
  77. do {
  78. if (output) {
  79. if (!(prev = av_oformat_next(prev)))
  80. break;
  81. pc = ((AVOutputFormat *)prev)->priv_class;
  82. } else {
  83. if (!(prev = av_iformat_next(prev)))
  84. break;
  85. pc = ((AVInputFormat *)prev)->priv_class;
  86. }
  87. if (!pc)
  88. continue;
  89. category = pc->category;
  90. } while (category != c1 && category != c2);
  91. return prev;
  92. }
  93. AVInputFormat *av_input_audio_device_next(AVInputFormat *d)
  94. {
  95. return device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  96. AV_CLASS_CATEGORY_DEVICE_INPUT);
  97. }
  98. AVInputFormat *av_input_video_device_next(AVInputFormat *d)
  99. {
  100. return device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
  101. AV_CLASS_CATEGORY_DEVICE_INPUT);
  102. }
  103. AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d)
  104. {
  105. return device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  106. AV_CLASS_CATEGORY_DEVICE_OUTPUT);
  107. }
  108. AVOutputFormat *av_output_video_device_next(AVOutputFormat *d)
  109. {
  110. return device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  111. AV_CLASS_CATEGORY_DEVICE_OUTPUT);
  112. }
  113. int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type,
  114. void *data, size_t data_size)
  115. {
  116. if (!s->oformat || !s->oformat->control_message)
  117. return AVERROR(ENOSYS);
  118. return s->oformat->control_message(s, type, data, data_size);
  119. }
  120. int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type,
  121. void *data, size_t data_size)
  122. {
  123. if (!av_format_get_control_message_cb(s))
  124. return AVERROR(ENOSYS);
  125. return av_format_get_control_message_cb(s)(s, type, data, data_size);
  126. }
  127. int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
  128. AVDictionary **device_options)
  129. {
  130. int ret;
  131. av_assert0(s && caps);
  132. av_assert0(s->iformat || s->oformat);
  133. if ((s->oformat && !s->oformat->create_device_capabilities) ||
  134. (s->iformat && !s->iformat->create_device_capabilities))
  135. return AVERROR(ENOSYS);
  136. *caps = av_mallocz(sizeof(**caps));
  137. if (!(*caps))
  138. return AVERROR(ENOMEM);
  139. (*caps)->device_context = s;
  140. if (((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
  141. goto fail;
  142. if (s->iformat) {
  143. if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
  144. goto fail;
  145. } else {
  146. if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
  147. goto fail;
  148. }
  149. av_opt_set_defaults(*caps);
  150. return 0;
  151. fail:
  152. av_freep(caps);
  153. return ret;
  154. }
  155. void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
  156. {
  157. if (!s || !caps || !(*caps))
  158. return;
  159. av_assert0(s->iformat || s->oformat);
  160. if (s->iformat) {
  161. if (s->iformat->free_device_capabilities)
  162. s->iformat->free_device_capabilities(s, *caps);
  163. } else {
  164. if (s->oformat->free_device_capabilities)
  165. s->oformat->free_device_capabilities(s, *caps);
  166. }
  167. av_freep(caps);
  168. }
  169. int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
  170. {
  171. int ret;
  172. av_assert0(s);
  173. av_assert0(device_list);
  174. av_assert0(s->oformat || s->iformat);
  175. if ((s->oformat && !s->oformat->get_device_list) ||
  176. (s->iformat && !s->iformat->get_device_list)) {
  177. *device_list = NULL;
  178. return AVERROR(ENOSYS);
  179. }
  180. *device_list = av_mallocz(sizeof(AVDeviceInfoList));
  181. if (!(*device_list))
  182. return AVERROR(ENOMEM);
  183. /* no default device by default */
  184. (*device_list)->default_device = -1;
  185. if (s->oformat)
  186. ret = s->oformat->get_device_list(s, *device_list);
  187. else
  188. ret = s->iformat->get_device_list(s, *device_list);
  189. if (ret < 0)
  190. avdevice_free_list_devices(device_list);
  191. return ret;
  192. }
  193. static int list_devices_for_context(AVFormatContext *s, AVDictionary *options,
  194. AVDeviceInfoList **device_list)
  195. {
  196. AVDictionary *tmp = NULL;
  197. int ret;
  198. av_dict_copy(&tmp, options, 0);
  199. if ((ret = av_opt_set_dict2(s, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  200. goto fail;
  201. ret = avdevice_list_devices(s, device_list);
  202. fail:
  203. av_dict_free(&tmp);
  204. avformat_free_context(s);
  205. return ret;
  206. }
  207. int avdevice_list_input_sources(AVInputFormat *device, const char *device_name,
  208. AVDictionary *device_options, AVDeviceInfoList **device_list)
  209. {
  210. AVFormatContext *s = NULL;
  211. int ret;
  212. if ((ret = ff_alloc_input_device_context(&s, device, device_name)) < 0)
  213. return ret;
  214. return list_devices_for_context(s, device_options, device_list);
  215. }
  216. int avdevice_list_output_sinks(AVOutputFormat *device, const char *device_name,
  217. AVDictionary *device_options, AVDeviceInfoList **device_list)
  218. {
  219. AVFormatContext *s = NULL;
  220. int ret;
  221. if ((ret = avformat_alloc_output_context2(&s, device, device_name, NULL)) < 0)
  222. return ret;
  223. return list_devices_for_context(s, device_options, device_list);
  224. }
  225. void avdevice_free_list_devices(AVDeviceInfoList **device_list)
  226. {
  227. AVDeviceInfoList *list;
  228. AVDeviceInfo *dev;
  229. int i;
  230. av_assert0(device_list);
  231. list = *device_list;
  232. if (!list)
  233. return;
  234. for (i = 0; i < list->nb_devices; i++) {
  235. dev = list->devices[i];
  236. if (dev) {
  237. av_freep(&dev->device_name);
  238. av_freep(&dev->device_description);
  239. av_free(dev);
  240. }
  241. }
  242. av_freep(&list->devices);
  243. av_freep(device_list);
  244. }