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.

226 lines
7.3KB

  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. int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type,
  73. void *data, size_t data_size)
  74. {
  75. if (!s->oformat || !s->oformat->control_message)
  76. return AVERROR(ENOSYS);
  77. return s->oformat->control_message(s, type, data, data_size);
  78. }
  79. int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type,
  80. void *data, size_t data_size)
  81. {
  82. if (!s->control_message_cb)
  83. return AVERROR(ENOSYS);
  84. return s->control_message_cb(s, type, data, data_size);
  85. }
  86. int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
  87. AVDictionary **device_options)
  88. {
  89. int ret;
  90. av_assert0(s && caps);
  91. av_assert0(s->iformat || s->oformat);
  92. if ((s->oformat && !s->oformat->create_device_capabilities) ||
  93. (s->iformat && !s->iformat->create_device_capabilities))
  94. return AVERROR(ENOSYS);
  95. *caps = av_mallocz(sizeof(**caps));
  96. if (!(*caps))
  97. return AVERROR(ENOMEM);
  98. (*caps)->device_context = s;
  99. if (((ret = av_opt_set_dict(s->priv_data, device_options)) < 0))
  100. goto fail;
  101. if (s->iformat) {
  102. if ((ret = s->iformat->create_device_capabilities(s, *caps)) < 0)
  103. goto fail;
  104. } else {
  105. if ((ret = s->oformat->create_device_capabilities(s, *caps)) < 0)
  106. goto fail;
  107. }
  108. av_opt_set_defaults(*caps);
  109. return 0;
  110. fail:
  111. av_freep(caps);
  112. return ret;
  113. }
  114. void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
  115. {
  116. if (!s || !caps || !(*caps))
  117. return;
  118. av_assert0(s->iformat || s->oformat);
  119. if (s->iformat) {
  120. if (s->iformat->free_device_capabilities)
  121. s->iformat->free_device_capabilities(s, *caps);
  122. } else {
  123. if (s->oformat->free_device_capabilities)
  124. s->oformat->free_device_capabilities(s, *caps);
  125. }
  126. av_freep(caps);
  127. }
  128. int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
  129. {
  130. int ret;
  131. av_assert0(s);
  132. av_assert0(device_list);
  133. av_assert0(s->oformat || s->iformat);
  134. if ((s->oformat && !s->oformat->get_device_list) ||
  135. (s->iformat && !s->iformat->get_device_list)) {
  136. *device_list = NULL;
  137. return AVERROR(ENOSYS);
  138. }
  139. *device_list = av_mallocz(sizeof(AVDeviceInfoList));
  140. if (!(*device_list))
  141. return AVERROR(ENOMEM);
  142. /* no default device by default */
  143. (*device_list)->default_device = -1;
  144. if (s->oformat)
  145. ret = s->oformat->get_device_list(s, *device_list);
  146. else
  147. ret = s->iformat->get_device_list(s, *device_list);
  148. if (ret < 0)
  149. avdevice_free_list_devices(device_list);
  150. return ret;
  151. }
  152. static int list_devices_for_context(AVFormatContext *s, AVDictionary *options,
  153. AVDeviceInfoList **device_list)
  154. {
  155. AVDictionary *tmp = NULL;
  156. int ret;
  157. av_dict_copy(&tmp, options, 0);
  158. if ((ret = av_opt_set_dict2(s, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  159. goto fail;
  160. ret = avdevice_list_devices(s, device_list);
  161. fail:
  162. av_dict_free(&tmp);
  163. avformat_free_context(s);
  164. return ret;
  165. }
  166. int avdevice_list_input_sources(AVInputFormat *device, const char *device_name,
  167. AVDictionary *device_options, AVDeviceInfoList **device_list)
  168. {
  169. AVFormatContext *s = NULL;
  170. int ret;
  171. if ((ret = ff_alloc_input_device_context(&s, device, device_name)) < 0)
  172. return ret;
  173. return list_devices_for_context(s, device_options, device_list);
  174. }
  175. int avdevice_list_output_sinks(AVOutputFormat *device, const char *device_name,
  176. AVDictionary *device_options, AVDeviceInfoList **device_list)
  177. {
  178. AVFormatContext *s = NULL;
  179. int ret;
  180. if ((ret = avformat_alloc_output_context2(&s, device, device_name, NULL)) < 0)
  181. return ret;
  182. return list_devices_for_context(s, device_options, device_list);
  183. }
  184. void avdevice_free_list_devices(AVDeviceInfoList **device_list)
  185. {
  186. AVDeviceInfoList *list;
  187. AVDeviceInfo *dev;
  188. int i;
  189. av_assert0(device_list);
  190. list = *device_list;
  191. if (!list)
  192. return;
  193. for (i = 0; i < list->nb_devices; i++) {
  194. dev = list->devices[i];
  195. if (dev) {
  196. av_freep(&dev->device_name);
  197. av_freep(&dev->device_description);
  198. av_free(dev);
  199. }
  200. }
  201. av_freep(&list->devices);
  202. av_freep(device_list);
  203. }