OSDN Git Service

c1bc3513937f4e4da55ef406ee1a8f2a8fc2c469
[coroid/ffmpeg_saccubus.git] / libavdevice / openal-dec.c
1 /*
2  * Copyright (c) 2011 Jonathan Baldwin
3  *
4  * This file is part of FFmpeg.
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 /**
20  * @file
21  * OpenAL 1.1 capture device for libavdevice
22  **/
23
24 #include <AL/al.h>
25 #include <AL/alc.h>
26
27 #include "libavutil/opt.h"
28 #include "avdevice.h"
29
30 typedef struct {
31     AVClass *class;
32     /** OpenAL capture device context. **/
33     ALCdevice *device;
34     /** The number of channels in the captured audio. **/
35     int channels;
36     /** The sample rate (in Hz) of the captured audio. **/
37     int sample_rate;
38     /** The sample size (in bits) of the captured audio. **/
39     int sample_size;
40     /** The OpenAL sample format of the captured audio. **/
41     ALCenum sample_format;
42     /** The number of bytes between two consecutive samples of the same channel/component. **/
43     ALCint sample_step;
44     /** If true, print a list of capture devices on this system and exit. **/
45     int list_devices;
46 } al_data;
47
48 typedef struct {
49     ALCenum al_fmt;
50     enum CodecID codec_id;
51     int channels;
52 } al_format_info;
53
54 #define LOWEST_AL_FORMAT FFMIN(FFMIN(AL_FORMAT_MONO8,AL_FORMAT_MONO16),FFMIN(AL_FORMAT_STEREO8,AL_FORMAT_STEREO16))
55
56 /**
57  * Get information about an AL_FORMAT value.
58  * @param al_fmt the AL_FORMAT value to find information about.
59  * @return A pointer to a structure containing information about the AL_FORMAT value.
60  */
61 static inline al_format_info* get_al_format_info(ALCenum al_fmt)
62 {
63     static al_format_info info_table[] = {
64         [AL_FORMAT_MONO8-LOWEST_AL_FORMAT]    = {AL_FORMAT_MONO8, CODEC_ID_PCM_U8, 1},
65         [AL_FORMAT_MONO16-LOWEST_AL_FORMAT]   = {AL_FORMAT_MONO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 1},
66         [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT]  = {AL_FORMAT_STEREO8, CODEC_ID_PCM_U8, 2},
67         [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE), 2},
68     };
69
70     return &info_table[al_fmt-LOWEST_AL_FORMAT];
71 }
72
73 /**
74  * Get the OpenAL error code, translated into an av/errno error code.
75  * @param device The ALC device to check for errors.
76  * @param error_msg_ret A pointer to a char* in which to return the error message, or NULL if desired.
77  * @return The error code, or 0 if there is no error.
78  */
79 static inline int al_get_error(ALCdevice *device, const char** error_msg_ret)
80 {
81     ALCenum error = alcGetError(device);
82     if (error_msg_ret)
83         *error_msg_ret = (const char*) alcGetString(device, error);
84     switch (error) {
85     case ALC_NO_ERROR:
86         return 0;
87     case ALC_INVALID_DEVICE:
88         return AVERROR(ENODEV);
89         break;
90     case ALC_INVALID_CONTEXT:
91     case ALC_INVALID_ENUM:
92     case ALC_INVALID_VALUE:
93         return AVERROR(EINVAL);
94         break;
95     case ALC_OUT_OF_MEMORY:
96         return AVERROR(ENOMEM);
97         break;
98     default:
99         return AVERROR(EIO);
100     }
101 }
102
103 /**
104  * Print out a list of OpenAL capture devices on this system.
105  */
106 static inline void print_al_capture_devices(void *log_ctx)
107 {
108     const char *devices;
109
110     if (!(devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER)))
111         return;
112
113     av_log(log_ctx, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n");
114
115     for (; *devices != '\0'; devices += strlen(devices) + 1)
116         av_log(log_ctx, AV_LOG_INFO, "  %s\n", devices);
117 }
118
119 static int read_header(AVFormatContext *ctx, AVFormatParameters *ap)
120 {
121     al_data *ad = ctx->priv_data;
122     static const ALCenum sample_formats[2][2] = {
123         { AL_FORMAT_MONO8,  AL_FORMAT_STEREO8  },
124         { AL_FORMAT_MONO16, AL_FORMAT_STEREO16 }
125     };
126     int error = 0;
127     const char *error_msg;
128     AVStream *st = NULL;
129     AVCodecContext *codec = NULL;
130
131     if (ad->list_devices) {
132         print_al_capture_devices(ctx);
133         return AVERROR_EXIT;
134     }
135
136     ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1];
137
138     /* Open device for capture */
139     ad->device =
140         alcCaptureOpenDevice(ctx->filename[0] ? ctx->filename : NULL,
141                              ad->sample_rate,
142                              ad->sample_format,
143                              ad->sample_rate); /* Maximum 1 second of sample data to be read at once */
144
145     if (error = al_get_error(ad->device, &error_msg)) goto fail;
146
147     /* Create stream */
148     if (!(st = av_new_stream(ctx, 0))) {
149         error = AVERROR(ENOMEM);
150         goto fail;
151     }
152
153     /* We work in microseconds */
154     av_set_pts_info(st, 64, 1, 1000000);
155
156     /* Set codec parameters */
157     codec = st->codec;
158     codec->codec_type = AVMEDIA_TYPE_AUDIO;
159     codec->sample_rate = ad->sample_rate;
160     codec->channels = get_al_format_info(ad->sample_format)->channels;
161     codec->codec_id = get_al_format_info(ad->sample_format)->codec_id;
162
163     /* This is needed to read the audio data */
164     ad->sample_step = (av_get_bits_per_sample(get_al_format_info(ad->sample_format)->codec_id) *
165                        get_al_format_info(ad->sample_format)->channels) / 8;
166
167     /* Finally, start the capture process */
168     alcCaptureStart(ad->device);
169
170     return 0;
171
172 fail:
173     /* Handle failure */
174     if (ad->device)
175         alcCaptureCloseDevice(ad->device);
176     if (error_msg)
177         av_log(ctx, AV_LOG_ERROR, "Cannot open device: %s\n", error_msg);
178     return error;
179 }
180
181 static int read_packet(AVFormatContext* ctx, AVPacket *pkt)
182 {
183     al_data *ad = ctx->priv_data;
184     int error=0;
185     const char *error_msg;
186     ALCint nb_samples;
187
188     /* Get number of samples available */
189     alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples);
190     if (error = al_get_error(ad->device, &error_msg)) goto fail;
191
192     /* Create a packet of appropriate size */
193     av_new_packet(pkt, nb_samples*ad->sample_step);
194     pkt->pts = av_gettime();
195
196     /* Fill the packet with the available samples */
197     alcCaptureSamples(ad->device, pkt->data, nb_samples);
198     if (error = al_get_error(ad->device, &error_msg)) goto fail;
199
200     return pkt->size;
201 fail:
202     /* Handle failure */
203     if (pkt->data)
204         av_destruct_packet(pkt);
205     if (error_msg)
206         av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg);
207     return error;
208 }
209
210 static int read_close(AVFormatContext* ctx)
211 {
212     al_data *ad = ctx->priv_data;
213
214     if (ad->device) {
215         alcCaptureStop(ad->device);
216         alcCaptureCloseDevice(ad->device);
217     }
218     return 0;
219 }
220
221 #define OFFSET(x) offsetof(al_data, x)
222
223 static const AVOption options[] = {
224     {"channels", "set number of channels",     OFFSET(channels),     FF_OPT_TYPE_INT, {.dbl=2},     1, 2,      AV_OPT_FLAG_DECODING_PARAM },
225     {"sample_rate", "set sample rate",         OFFSET(sample_rate),  FF_OPT_TYPE_INT, {.dbl=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM },
226     {"sample_size", "set sample size",         OFFSET(sample_size),  FF_OPT_TYPE_INT, {.dbl=16},    8, 16,     AV_OPT_FLAG_DECODING_PARAM },
227     {"list_devices", "list available devices", OFFSET(list_devices), FF_OPT_TYPE_INT, {.dbl=0},     0, 1,      AV_OPT_FLAG_DECODING_PARAM },
228     {"true",  "", 0, FF_OPT_TYPE_CONST, {.dbl=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
229     {"false", "", 0, FF_OPT_TYPE_CONST, {.dbl=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
230     {NULL},
231 };
232
233 static const AVClass class = {
234     .class_name = "openal",
235     .item_name = av_default_item_name,
236     .option = options,
237     .version = LIBAVUTIL_VERSION_INT
238 };
239
240 AVInputFormat ff_openal_demuxer = {
241     .name = "openal",
242     .long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"),
243     .priv_data_size = sizeof(al_data),
244     .read_probe = NULL,
245     .read_header = read_header,
246     .read_packet = read_packet,
247     .read_close = read_close,
248     .flags = AVFMT_NOFILE,
249     .priv_class = &class
250 };