FFmpeg
Loading...
Searching...
No Matches
openal-dec.c
Go to the documentation of this file.
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 "libavutil/time.h"
29#include "libavformat/demux.h"
31#include "avdevice.h"
32
33typedef struct {
34 AVClass *class;
35 /** OpenAL capture device context. **/
36 ALCdevice *device;
37 /** The number of channels in the captured audio. **/
39 /** The sample rate (in Hz) of the captured audio. **/
41 /** The sample size (in bits) of the captured audio. **/
43 /** The OpenAL sample format of the captured audio. **/
45 /** The number of bytes between two consecutive samples of the same channel/component. **/
47 /** If true, print a list of capture devices on this system and exit. **/
49} al_data;
50
51typedef struct {
52 ALCenum al_fmt;
56
57#define LOWEST_AL_FORMAT FFMIN(FFMIN(AL_FORMAT_MONO8,AL_FORMAT_MONO16),FFMIN(AL_FORMAT_STEREO8,AL_FORMAT_STEREO16))
58
59/**
60 * Get information about an AL_FORMAT value.
61 * @param al_fmt the AL_FORMAT value to find information about.
62 * @return A pointer to a structure containing information about the AL_FORMAT value.
63 */
64static const inline al_format_info* get_al_format_info(ALCenum al_fmt)
65{
66 static const al_format_info info_table[] = {
67 [AL_FORMAT_MONO8-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO8, AV_CODEC_ID_PCM_U8, 1},
68 [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 1},
69 [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO8, AV_CODEC_ID_PCM_U8, 2},
70 [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 2},
71 };
72
73 return &info_table[al_fmt-LOWEST_AL_FORMAT];
74}
75
76/**
77 * Get the OpenAL error code, translated into an av/errno error code.
78 * @param device The ALC device to check for errors.
79 * @param error_msg_ret A pointer to a char* in which to return the error message, or NULL if desired.
80 * @return The error code, or 0 if there is no error.
81 */
82static inline int al_get_error(ALCdevice *device, const char** error_msg_ret)
83{
84 ALCenum error = alcGetError(device);
85 if (error_msg_ret)
86 *error_msg_ret = (const char*) alcGetString(device, error);
87 switch (error) {
88 case ALC_NO_ERROR:
89 return 0;
90 case ALC_INVALID_DEVICE:
91 return AVERROR(ENODEV);
92 break;
93 case ALC_INVALID_CONTEXT:
94 case ALC_INVALID_ENUM:
95 case ALC_INVALID_VALUE:
96 return AVERROR(EINVAL);
97 break;
98 case ALC_OUT_OF_MEMORY:
99 return AVERROR(ENOMEM);
100 break;
101 default:
102 return AVERROR(EIO);
103 }
104}
105
106/**
107 * Print out a list of OpenAL capture devices on this system.
108 */
109static inline void print_al_capture_devices(void *log_ctx)
110{
111 const char *devices;
112
113 if (!(devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER)))
114 return;
115
116 av_log(log_ctx, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n");
117
118 for (; *devices != '\0'; devices += strlen(devices) + 1)
119 av_log(log_ctx, AV_LOG_INFO, " %s\n", devices);
120}
121
123{
124 al_data *ad = ctx->priv_data;
125 static const ALCenum sample_formats[2][2] = {
126 { AL_FORMAT_MONO8, AL_FORMAT_STEREO8 },
127 { AL_FORMAT_MONO16, AL_FORMAT_STEREO16 }
128 };
129 int error = 0;
130 const char *error_msg;
131 AVStream *st = NULL;
132 AVCodecParameters *par = NULL;
133
134 if (ad->list_devices) {
136 return AVERROR_EXIT;
137 }
138
139 ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1];
140
141 /* Open device for capture */
142 ad->device =
143 alcCaptureOpenDevice(ctx->url[0] ? ctx->url : NULL,
144 ad->sample_rate,
145 ad->sample_format,
146 ad->sample_rate); /* Maximum 1 second of sample data to be read at once */
147
148 if (error = al_get_error(ad->device, &error_msg)) goto fail;
149
150 /* Create stream */
151 if (!(st = avformat_new_stream(ctx, NULL))) {
152 error = AVERROR(ENOMEM);
153 goto fail;
154 }
155
156 /* We work in microseconds */
157 avpriv_set_pts_info(st, 64, 1, 1000000);
158
159 /* Set codec parameters */
160 par = st->codecpar;
162 par->sample_rate = ad->sample_rate;
165
166 /* This is needed to read the audio data */
169
170 /* Finally, start the capture process */
171 alcCaptureStart(ad->device);
172
173 return 0;
174
175fail:
176 /* Handle failure */
177 if (ad->device)
178 alcCaptureCloseDevice(ad->device);
179 if (error_msg)
180 av_log(ctx, AV_LOG_ERROR, "Cannot open device: %s\n", error_msg);
181 return error;
182}
183
185{
186 al_data *ad = ctx->priv_data;
187 int error=0;
188 const char *error_msg;
189 ALCint nb_samples;
190
191 for (;;) {
192 /* Get number of samples available */
193 alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples);
194 if (error = al_get_error(ad->device, &error_msg)) goto fail;
195 if (nb_samples > 0)
196 break;
197 if (ctx->flags & AVFMT_FLAG_NONBLOCK)
198 return AVERROR(EAGAIN);
199 av_usleep(1000);
200 }
201
202 /* Create a packet of appropriate size */
203 if ((error = av_new_packet(pkt, nb_samples*ad->sample_step)) < 0)
204 goto fail;
205 pkt->pts = av_gettime();
206
207 /* Fill the packet with the available samples */
208 alcCaptureSamples(ad->device, pkt->data, nb_samples);
209 if (error = al_get_error(ad->device, &error_msg)) goto fail;
210
211 return pkt->size;
212fail:
213 /* Handle failure */
214 if (pkt->data)
216 if (error_msg)
217 av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg);
218 return error;
219}
220
222{
223 al_data *ad = ctx->priv_data;
224
225 if (ad->device) {
226 alcCaptureStop(ad->device);
227 alcCaptureCloseDevice(ad->device);
228 }
229 return 0;
230}
231
232#define OFFSET(x) offsetof(al_data, x)
233
234static const AVOption options[] = {
235 {"channels", "set number of channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM },
236 {"sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM },
237 {"sample_size", "set sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64=16}, 8, 16, AV_OPT_FLAG_DECODING_PARAM },
238 {"list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, .unit = "list_devices" },
239 {"true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, .unit = "list_devices" },
240 {"false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, .unit = "list_devices" },
241 {NULL},
242};
243
244static const AVClass class = {
245 .class_name = "openal indev",
247 .option = options,
250};
251
253 .p.name = "openal",
254 .p.long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"),
255 .p.flags = AVFMT_NOFILE,
256 .p.priv_class = &class,
257 .priv_data_size = sizeof(al_data),
258 .read_probe = NULL,
262};
const FFInputFormat ff_openal_demuxer
Definition openal-dec.c:252
channels
Definition aptx.h:31
Main libavdevice API header.
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition avformat.h:1487
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition avformat.h:488
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define fail
Definition test.h:479
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:556
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition log.h:44
#define AV_NE(be, le)
Definition macros.h:33
static const struct @161106304217160064156176027033213377304043230355 sample_formats[]
static void print_al_capture_devices(void *log_ctx)
Print out a list of OpenAL capture devices on this system.
Definition openal-dec.c:109
static const al_format_info * get_al_format_info(ALCenum al_fmt)
Get information about an AL_FORMAT value.
Definition openal-dec.c:64
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition openal-dec.c:184
static int read_header(AVFormatContext *ctx)
Definition openal-dec.c:122
static int read_close(AVFormatContext *ctx)
Definition openal-dec.c:221
#define LOWEST_AL_FORMAT
Definition openal-dec.c:57
static int al_get_error(ALCdevice *device, const char **error_msg_ret)
Get the OpenAL error code, translated into an av/errno error code.
Definition openal-dec.c:82
#define OFFSET(x)
Definition openal-dec.c:232
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition log.h:81
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int sample_rate
The sample rate (in Hz) of the captured audio.
Definition openal-dec.c:40
ALCenum sample_format
The OpenAL sample format of the captured audio.
Definition openal-dec.c:44
int list_devices
If true, print a list of capture devices on this system and exit.
Definition openal-dec.c:48
int sample_size
The sample size (in bits) of the captured audio.
Definition openal-dec.c:42
int channels
The number of channels in the captured audio.
Definition openal-dec.c:38
ALCdevice * device
OpenAL capture device context.
Definition openal-dec.c:36
ALCint sample_step
The number of bytes between two consecutive samples of the same channel/component.
Definition openal-dec.c:46
ALCenum al_fmt
Definition openal-dec.c:52
enum AVCodecID codec_id
Definition openal-dec.c:53
#define av_log(a,...)
static void error(const char *err)
static AVFormatContext * ctx
Definition movenc.c:49
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime(void)
Get the current time in microseconds.
Definition time.c:40