avdevice.c 4.1 KB

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