avdevice.c 7.3 KB

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