FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavformat/internal.h"
29 #include "avdevice.h"
30 
31 typedef struct {
32  AVClass *class;
33  /** OpenAL capture device context. **/
34  ALCdevice *device;
35  /** The number of channels in the captured audio. **/
36  int channels;
37  /** The sample rate (in Hz) of the captured audio. **/
39  /** The sample size (in bits) of the captured audio. **/
41  /** The OpenAL sample format of the captured audio. **/
42  ALCenum sample_format;
43  /** The number of bytes between two consecutive samples of the same channel/component. **/
44  ALCint sample_step;
45  /** If true, print a list of capture devices on this system and exit. **/
47 } al_data;
48 
49 typedef struct {
50  ALCenum al_fmt;
52  int channels;
54 
55 #define LOWEST_AL_FORMAT FFMIN(FFMIN(AL_FORMAT_MONO8,AL_FORMAT_MONO16),FFMIN(AL_FORMAT_STEREO8,AL_FORMAT_STEREO16))
56 
57 /**
58  * Get information about an AL_FORMAT value.
59  * @param al_fmt the AL_FORMAT value to find information about.
60  * @return A pointer to a structure containing information about the AL_FORMAT value.
61  */
62 static inline al_format_info* get_al_format_info(ALCenum al_fmt)
63 {
64  static al_format_info info_table[] = {
65  [AL_FORMAT_MONO8-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO8, AV_CODEC_ID_PCM_U8, 1},
66  [AL_FORMAT_MONO16-LOWEST_AL_FORMAT] = {AL_FORMAT_MONO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 1},
67  [AL_FORMAT_STEREO8-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO8, AV_CODEC_ID_PCM_U8, 2},
68  [AL_FORMAT_STEREO16-LOWEST_AL_FORMAT] = {AL_FORMAT_STEREO16, AV_NE (AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE), 2},
69  };
70 
71  return &info_table[al_fmt-LOWEST_AL_FORMAT];
72 }
73 
74 /**
75  * Get the OpenAL error code, translated into an av/errno error code.
76  * @param device The ALC device to check for errors.
77  * @param error_msg_ret A pointer to a char* in which to return the error message, or NULL if desired.
78  * @return The error code, or 0 if there is no error.
79  */
80 static inline int al_get_error(ALCdevice *device, const char** error_msg_ret)
81 {
82  ALCenum error = alcGetError(device);
83  if (error_msg_ret)
84  *error_msg_ret = (const char*) alcGetString(device, error);
85  switch (error) {
86  case ALC_NO_ERROR:
87  return 0;
88  case ALC_INVALID_DEVICE:
89  return AVERROR(ENODEV);
90  break;
91  case ALC_INVALID_CONTEXT:
92  case ALC_INVALID_ENUM:
93  case ALC_INVALID_VALUE:
94  return AVERROR(EINVAL);
95  break;
96  case ALC_OUT_OF_MEMORY:
97  return AVERROR(ENOMEM);
98  break;
99  default:
100  return AVERROR(EIO);
101  }
102 }
103 
104 /**
105  * Print out a list of OpenAL capture devices on this system.
106  */
107 static inline void print_al_capture_devices(void *log_ctx)
108 {
109  const char *devices;
110 
111  if (!(devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER)))
112  return;
113 
114  av_log(log_ctx, AV_LOG_INFO, "List of OpenAL capture devices on this system:\n");
115 
116  for (; *devices != '\0'; devices += strlen(devices) + 1)
117  av_log(log_ctx, AV_LOG_INFO, " %s\n", devices);
118 }
119 
120 static int read_header(AVFormatContext *ctx)
121 {
122  al_data *ad = ctx->priv_data;
123  static const ALCenum sample_formats[2][2] = {
124  { AL_FORMAT_MONO8, AL_FORMAT_STEREO8 },
125  { AL_FORMAT_MONO16, AL_FORMAT_STEREO16 }
126  };
127  int error = 0;
128  const char *error_msg;
129  AVStream *st = NULL;
130  AVCodecContext *codec = NULL;
131 
132  if (ad->list_devices) {
134  return AVERROR_EXIT;
135  }
136 
137  ad->sample_format = sample_formats[ad->sample_size/8-1][ad->channels-1];
138 
139  /* Open device for capture */
140  ad->device =
141  alcCaptureOpenDevice(ctx->filename[0] ? ctx->filename : NULL,
142  ad->sample_rate,
143  ad->sample_format,
144  ad->sample_rate); /* Maximum 1 second of sample data to be read at once */
145 
146  if (error = al_get_error(ad->device, &error_msg)) goto fail;
147 
148  /* Create stream */
149  if (!(st = avformat_new_stream(ctx, NULL))) {
150  error = AVERROR(ENOMEM);
151  goto fail;
152  }
153 
154  /* We work in microseconds */
155  avpriv_set_pts_info(st, 64, 1, 1000000);
156 
157  /* Set codec parameters */
158  codec = st->codec;
160  codec->sample_rate = ad->sample_rate;
163 
164  /* This is needed to read the audio data */
167 
168  /* Finally, start the capture process */
169  alcCaptureStart(ad->device);
170 
171  return 0;
172 
173 fail:
174  /* Handle failure */
175  if (ad->device)
176  alcCaptureCloseDevice(ad->device);
177  if (error_msg)
178  av_log(ctx, AV_LOG_ERROR, "Cannot open device: %s\n", error_msg);
179  return error;
180 }
181 
183 {
184  al_data *ad = ctx->priv_data;
185  int error=0;
186  const char *error_msg;
187  ALCint nb_samples;
188 
189  /* Get number of samples available */
190  alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples);
191  if (error = al_get_error(ad->device, &error_msg)) goto fail;
192 
193  /* Create a packet of appropriate size */
194  av_new_packet(pkt, nb_samples*ad->sample_step);
195  pkt->pts = av_gettime();
196 
197  /* Fill the packet with the available samples */
198  alcCaptureSamples(ad->device, pkt->data, nb_samples);
199  if (error = al_get_error(ad->device, &error_msg)) goto fail;
200 
201  return pkt->size;
202 fail:
203  /* Handle failure */
204  if (pkt->data)
205  av_destruct_packet(pkt);
206  if (error_msg)
207  av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg);
208  return error;
209 }
210 
211 static int read_close(AVFormatContext* ctx)
212 {
213  al_data *ad = ctx->priv_data;
214 
215  if (ad->device) {
216  alcCaptureStop(ad->device);
217  alcCaptureCloseDevice(ad->device);
218  }
219  return 0;
220 }
221 
222 #define OFFSET(x) offsetof(al_data, x)
223 
224 static const AVOption options[] = {
225  {"channels", "set number of channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM },
226  {"sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM },
227  {"sample_size", "set sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64=16}, 8, 16, AV_OPT_FLAG_DECODING_PARAM },
228  {"list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
229  {"true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
230  {"false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
231  {NULL},
232 };
233 
234 static const AVClass class = {
235  .class_name = "openal",
236  .item_name = av_default_item_name,
237  .option = options,
238  .version = LIBAVUTIL_VERSION_INT
239 };
240 
242  .name = "openal",
243  .long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"),
244  .priv_data_size = sizeof(al_data),
245  .read_probe = NULL,
249  .flags = AVFMT_NOFILE,
250  .priv_class = &class
251 };