openal-dec.c 8.3 KB

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