avdevice.c 7.4 KB

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