FFmpeg
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/internal.h"
30 #include "avdevice.h"
31 
32 typedef struct {
33  AVClass *class;
34  /** OpenAL capture device context. **/
35  ALCdevice *device;
36  /** The number of channels in the captured audio. **/
37  int channels;
38  /** The sample rate (in Hz) of the captured audio. **/
40  /** The sample size (in bits) of the captured audio. **/
42  /** The OpenAL sample format of the captured audio. **/
43  ALCenum sample_format;
44  /** The number of bytes between two consecutive samples of the same channel/component. **/
45  ALCint sample_step;
46  /** If true, print a list of capture devices on this system and exit. **/
48 } al_data;
49 
50 typedef struct {
51  ALCenum al_fmt;
53  int channels;
55 
56 #define LOWEST_AL_FORMAT FFMIN(FFMIN(AL_FORMAT_MONO8,AL_FORMAT_MONO16),FFMIN(AL_FORMAT_STEREO8,AL_FORMAT_STEREO16))
57 
58 /**
59  * Get information about an AL_FORMAT value.
60  * @param al_fmt the AL_FORMAT value to find information about.
61  * @return A pointer to a structure containing information about the AL_FORMAT value.
62  */
63 static const inline al_format_info* get_al_format_info(ALCenum al_fmt)
64 {
65  static const al_format_info info_table[] = {
66  [AL_FORMAT_MONO8-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO8, AV_CODEC_ID_PCM_U8, 1},
67  [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 1},
68  [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO8, AV_CODEC_ID_PCM_U8, 2},
69  [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 2},
70  };
71 
72  return &info_table[al_fmt-LOWEST_AL_FORMAT];
73 }
74 
75 /**
76  * Get the OpenAL error code, translated into an av/errno error code.
77  * @param device The ALC device to check for errors.
78  * @param error_msg_ret A pointer to a char* in which to return the error message, or NULL if desired.
79  * @return The error code, or 0 if there is no error.
80  */
81 static inline int al_get_error(ALCdevice *device, const char** error_msg_ret)
82 {
83  ALCenum error = alcGetError(device);
84  if (error_msg_ret)
85  *error_msg_ret = (const char*) alcGetString(device, error);
86  switch (error) {
87  case ALC_NO_ERROR:
88  return 0;
89  case ALC_INVALID_DEVICE:
90  return AVERROR(ENODEV);
91  break;
92  case ALC_INVALID_CONTEXT:
93  case ALC_INVALID_ENUM:
94  case ALC_INVALID_VALUE:
95  return AVERROR(EINVAL);
96  break;
97  case ALC_OUT_OF_MEMORY:
98  return AVERROR(ENOMEM);
99  break;
100  default:
101  return AVERROR(EIO);
102  }
103 }
104 
105 /**
106  * Print out a list of OpenAL capture devices on this system.
107  */
108 static inline void print_al_capture_devices(void *log_ctx)
109 {
110  const char *devices;
111 
112  if (!(devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER)))
113  return;
114 
115  av_log(log_ctx, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n");
116 
117  for (; *devices != '\0'; devices += strlen(devices) + 1)
118  av_log(log_ctx, AV_LOG_INFO, " %s\n", devices);
119 }
120 
122 {
123  al_data *ad = ctx->priv_data;
124  static const ALCenum sample_formats[2][2] = {
125  { AL_FORMAT_MONO8, AL_FORMAT_STEREO8 },
126  { AL_FORMAT_MONO16, AL_FORMAT_STEREO16 }
127  };
128  int error = 0;
129  const char *error_msg;
130  AVStream *st = NULL;
131  AVCodecParameters *par = NULL;
132 
133  if (ad->list_devices) {
135  return AVERROR_EXIT;
136  }
137 
138  ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1];
139 
140  /* Open device for capture */
141  ad->device =
142  alcCaptureOpenDevice(ctx->url[0] ? ctx->url : NULL,
143  ad->sample_rate,
144  ad->sample_format,
145  ad->sample_rate); /* Maximum 1 second of sample data to be read at once */
146 
147  if (error = al_get_error(ad->device, &error_msg)) goto fail;
148 
149  /* Create stream */
150  if (!(st = avformat_new_stream(ctx, NULL))) {
151  error = AVERROR(ENOMEM);
152  goto fail;
153  }
154 
155  /* We work in microseconds */
156  avpriv_set_pts_info(st, 64, 1, 1000000);
157 
158  /* Set codec parameters */
159  par = st->codecpar;
161  par->sample_rate = ad->sample_rate;
164 
165  /* This is needed to read the audio data */
168 
169  /* Finally, start the capture process */
170  alcCaptureStart(ad->device);
171 
172  return 0;
173 
174 fail:
175  /* Handle failure */
176  if (ad->device)
177  alcCaptureCloseDevice(ad->device);
178  if (error_msg)
179  av_log(ctx, AV_LOG_ERROR, "Cannot open device: %s\n", error_msg);
180  return error;
181 }
182 
184 {
185  al_data *ad = ctx->priv_data;
186  int error=0;
187  const char *error_msg;
188  ALCint nb_samples;
189 
190  for (;;) {
191  /* Get number of samples available */
192  alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples);
193  if (error = al_get_error(ad->device, &error_msg)) goto fail;
194  if (nb_samples > 0)
195  break;
197  return AVERROR(EAGAIN);
198  av_usleep(1000);
199  }
200 
201  /* Create a packet of appropriate size */
202  if ((error = av_new_packet(pkt, nb_samples*ad->sample_step)) < 0)
203  goto fail;
204  pkt->pts = av_gettime();
205 
206  /* Fill the packet with the available samples */
207  alcCaptureSamples(ad->device, pkt->data, nb_samples);
208  if (error = al_get_error(ad->device, &error_msg)) goto fail;
209 
210  return pkt->size;
211 fail:
212  /* Handle failure */
213  if (pkt->data)
215  if (error_msg)
216  av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg);
217  return error;
218 }
219 
221 {
222  al_data *ad = ctx->priv_data;
223 
224  if (ad->device) {
225  alcCaptureStop(ad->device);
226  alcCaptureCloseDevice(ad->device);
227  }
228  return 0;
229 }
230 
231 #define OFFSET(x) offsetof(al_data, x)
232 
233 static const AVOption options[] = {
234  {"channels", "set number of channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM },
235  {"sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM },
236  {"sample_size", "set sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64=16}, 8, 16, AV_OPT_FLAG_DECODING_PARAM },
237  {"list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
238  {"true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
239  {"false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
240  {NULL},
241 };
242 
243 static const AVClass class = {
244  .class_name = "openal indev",
245  .item_name = av_default_item_name,
246  .option = options,
249 };
250 
252  .name = "openal",
253  .long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"),
254  .priv_data_size = sizeof(al_data),
255  .read_probe = NULL,
259  .flags = AVFMT_NOFILE,
260  .priv_class = &class
261 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:318
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:57
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:53
print_al_capture_devices
static void print_al_capture_devices(void *log_ctx)
Print out a list of OpenAL capture devices on this system.
Definition: openal-dec.c:108
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
al_get_error
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:81
read_header
static int read_header(AVFormatContext *ctx)
Definition: openal-dec.c:121
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
avpriv_set_pts_info
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:697
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:319
fail
#define fail()
Definition: checkasm.h:131
al_data::sample_step
ALCint sample_step
The number of bytes between two consecutive samples of the same channel/component.
Definition: openal-dec.c:45
al_data::sample_format
ALCenum sample_format
The OpenAL sample format of the captured audio.
Definition: openal-dec.c:43
read_close
static int read_close(AVFormatContext *ctx)
Definition: openal-dec.c:220
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:586
AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition: log.h:43
al_format_info::channels
int channels
Definition: openal-dec.c:53
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:656
al_format_info::al_fmt
ALCenum al_fmt
Definition: openal-dec.c:51
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1331
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:32
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:371
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
time.h
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:212
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:177
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
al_data::device
ALCdevice * device
OpenAL capture device context.
Definition: openal-dec.c:35
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1296
al_data::sample_rate
int sample_rate
The sample rate (in Hz) of the captured audio.
Definition: openal-dec.c:39
ff_openal_demuxer
const AVInputFormat ff_openal_demuxer
Definition: openal-dec.c:251
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:470
options
static const AVOption options[]
Definition: openal-dec.c:233
avdevice.h
OFFSET
#define OFFSET(x)
Definition: openal-dec.c:231
al_data::list_devices
int list_devices
If true, print a list of capture devices on this system and exit.
Definition: openal-dec.c:47
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
read_packet
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: openal-dec.c:183
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
al_format_info
Definition: openal-dec.c:50
AVFMT_FLAG_NONBLOCK
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1334
AVStream
Stream structure.
Definition: avformat.h:948
AVClass::class_name
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:71
al_data::channels
int channels
The number of channels in the captured audio.
Definition: openal-dec.c:37
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
al_format_info::codec_id
enum AVCodecID codec_id
Definition: openal-dec.c:52
al_data::sample_size
int sample_size
The sample size (in bits) of the captured audio.
Definition: openal-dec.c:41
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:323
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:61
AVPacket
This structure stores compressed data.
Definition: packet.h:351
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
LOWEST_AL_FORMAT
#define LOWEST_AL_FORMAT
Definition: openal-dec.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
get_al_format_info
static const al_format_info * get_al_format_info(ALCenum al_fmt)
Get information about an AL_FORMAT value.
Definition: openal-dec.c:63
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1241
al_data
Definition: openal-dec.c:32