avdevice.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "avdevice.h"
  20. #include "internal.h"
  21. #if FF_API_DEVICE_CAPABILITIES
  22. const AVOption av_device_capabilities[] = {
  23. { NULL }
  24. };
  25. #endif
  26. int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type,
  27. void *data, size_t data_size)
  28. {
  29. if (!s->oformat || !s->oformat->control_message)
  30. return AVERROR(ENOSYS);
  31. return s->oformat->control_message(s, type, data, data_size);
  32. }
  33. int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type,
  34. void *data, size_t data_size)
  35. {
  36. if (!s->control_message_cb)
  37. return AVERROR(ENOSYS);
  38. return s->control_message_cb(s, type, data, data_size);
  39. }
  40. #if FF_API_DEVICE_CAPABILITIES
  41. int avdevice_capabilities_create(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s,
  42. AVDictionary **device_options)
  43. {
  44. return AVERROR(ENOSYS);
  45. }
  46. void avdevice_capabilities_free(AVDeviceCapabilitiesQuery **caps, AVFormatContext *s)
  47. {
  48. return;
  49. }
  50. #endif
  51. int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
  52. {
  53. int ret;
  54. av_assert0(s);
  55. av_assert0(device_list);
  56. av_assert0(s->oformat || s->iformat);
  57. if ((s->oformat && !s->oformat->get_device_list) ||
  58. (s->iformat && !s->iformat->get_device_list)) {
  59. *device_list = NULL;
  60. return AVERROR(ENOSYS);
  61. }
  62. *device_list = av_mallocz(sizeof(AVDeviceInfoList));
  63. if (!(*device_list))
  64. return AVERROR(ENOMEM);
  65. /* no default device by default */
  66. (*device_list)->default_device = -1;
  67. if (s->oformat)
  68. ret = s->oformat->get_device_list(s, *device_list);
  69. else
  70. ret = s->iformat->get_device_list(s, *device_list);
  71. if (ret < 0) {
  72. avdevice_free_list_devices(device_list);
  73. return ret;
  74. }
  75. return (*device_list)->nb_devices;
  76. }
  77. static int list_devices_for_context(AVFormatContext *s, AVDictionary *options,
  78. AVDeviceInfoList **device_list)
  79. {
  80. AVDictionary *tmp = NULL;
  81. int ret;
  82. av_dict_copy(&tmp, options, 0);
  83. if ((ret = av_opt_set_dict2(s, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
  84. goto fail;
  85. ret = avdevice_list_devices(s, device_list);
  86. fail:
  87. av_dict_free(&tmp);
  88. avformat_free_context(s);
  89. return ret;
  90. }
  91. int avdevice_list_input_sources(const AVInputFormat *device, const char *device_name,
  92. AVDictionary *device_options, AVDeviceInfoList **device_list)
  93. {
  94. AVFormatContext *s = NULL;
  95. int ret;
  96. if ((ret = ff_alloc_input_device_context(&s, device, device_name)) < 0)
  97. return ret;
  98. return list_devices_for_context(s, device_options, device_list);
  99. }
  100. int avdevice_list_output_sinks(const AVOutputFormat *device, const char *device_name,
  101. AVDictionary *device_options, AVDeviceInfoList **device_list)
  102. {
  103. AVFormatContext *s = NULL;
  104. int ret;
  105. if ((ret = avformat_alloc_output_context2(&s, device, device_name, NULL)) < 0)
  106. return ret;
  107. return list_devices_for_context(s, device_options, device_list);
  108. }
  109. void avdevice_free_list_devices(AVDeviceInfoList **device_list)
  110. {
  111. AVDeviceInfoList *list;
  112. AVDeviceInfo *dev;
  113. int i;
  114. av_assert0(device_list);
  115. list = *device_list;
  116. if (!list)
  117. return;
  118. for (i = 0; i < list->nb_devices; i++) {
  119. dev = list->devices[i];
  120. if (dev) {
  121. av_freep(&dev->device_name);
  122. av_freep(&dev->device_description);
  123. av_freep(&dev->media_types);
  124. av_free(dev);
  125. }
  126. }
  127. av_freep(&list->devices);
  128. av_freep(device_list);
  129. }