openal-dec.c 8.2 KB

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