avdevice.c 4.1 KB

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