avdevice.c 4.1 KB

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