]> git.sesse.net Git - ffmpeg/blob - libavdevice/avdevice.c
Merge commit 'a420ccd4f2a011887451a7d5e1bebba4fd7c40e2'
[ffmpeg] / libavdevice / avdevice.c
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 "config.h"
22
23 unsigned avdevice_version(void)
24 {
25     av_assert0(LIBAVDEVICE_VERSION_MICRO >= 100);
26     return LIBAVDEVICE_VERSION_INT;
27 }
28
29 const char * avdevice_configuration(void)
30 {
31     return FFMPEG_CONFIGURATION;
32 }
33
34 const char * avdevice_license(void)
35 {
36 #define LICENSE_PREFIX "libavdevice license: "
37     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
38 }
39
40 static void *av_device_next(void *prev, int output,
41                             AVClassCategory c1, AVClassCategory c2)
42 {
43     const AVClass *pc;
44     AVClassCategory category = AV_CLASS_CATEGORY_NA;
45     do {
46         if (output) {
47             if (!(prev = av_oformat_next(prev)))
48                 break;
49             pc = ((AVOutputFormat *)prev)->priv_class;
50         } else {
51             if (!(prev = av_iformat_next(prev)))
52                 break;
53             pc = ((AVInputFormat *)prev)->priv_class;
54         }
55         if (!pc)
56             continue;
57         category = pc->category;
58     } while (category != c1 && category != c2);
59     return prev;
60 }
61
62 AVInputFormat *av_input_audio_device_next(AVInputFormat  *d)
63 {
64     return av_device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
65                           AV_CLASS_CATEGORY_DEVICE_INPUT);
66 }
67
68 AVInputFormat *av_input_video_device_next(AVInputFormat  *d)
69 {
70     return av_device_next(d, 0, AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
71                           AV_CLASS_CATEGORY_DEVICE_INPUT);
72 }
73
74 AVOutputFormat *av_output_audio_device_next(AVOutputFormat *d)
75 {
76     return av_device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
77                           AV_CLASS_CATEGORY_DEVICE_OUTPUT);
78 }
79
80 AVOutputFormat *av_output_video_device_next(AVOutputFormat *d)
81 {
82     return av_device_next(d, 1, AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
83                           AV_CLASS_CATEGORY_DEVICE_OUTPUT);
84 }
85
86 int avdevice_app_to_dev_control_message(struct AVFormatContext *s, enum AVAppToDevMessageType type,
87                                         void *data, size_t data_size)
88 {
89     if (!s->oformat || !s->oformat->control_message)
90         return AVERROR(ENOSYS);
91     return s->oformat->control_message(s, type, data, data_size);
92 }
93
94 int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type,
95                                         void *data, size_t data_size)
96 {
97     if (!s->control_message_cb)
98         return AVERROR(ENOSYS);
99     return s->control_message_cb(s, type, data, data_size);
100 }
101
102 int avdevice_list_devices(AVFormatContext *s, AVDeviceInfoList **device_list)
103 {
104     int ret;
105     av_assert0(s);
106     av_assert0(device_list);
107     av_assert0(s->oformat || s->iformat);
108     if ((s->oformat && !s->oformat->get_device_list) ||
109         (s->iformat && !s->iformat->get_device_list)) {
110         *device_list = NULL;
111         return AVERROR(ENOSYS);
112     }
113     *device_list = av_mallocz(sizeof(AVDeviceInfoList));
114     if (!(*device_list))
115         return AVERROR(ENOMEM);
116     if (s->oformat)
117         ret = s->oformat->get_device_list(s, *device_list);
118     else
119         ret = s->iformat->get_device_list(s, *device_list);
120     if (ret < 0)
121         avdevice_free_list_devices(device_list);
122     return ret;
123 }
124
125 void avdevice_free_list_devices(AVDeviceInfoList **device_list)
126 {
127     AVDeviceInfoList *list;
128     AVDeviceInfo *dev;
129     int i;
130
131     av_assert0(device_list);
132     list = *device_list;
133     if (!list)
134         return;
135
136     for (i = 0; i < list->nb_devices; i++) {
137         dev = list->devices[i];
138         if (dev) {
139             av_free(dev->device_name);
140             av_free(dev->device_description);
141             av_free(dev);
142         }
143     }
144     av_free(list->devices);
145     av_freep(device_list);
146 }