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