openal-dec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 const inline al_format_info* get_al_format_info(ALCenum al_fmt)
  57. {
  58. static const 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. AVCodecParameters *par = 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->url[0] ? ctx->url : 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. par = st->codecpar;
  139. par->codec_type = AVMEDIA_TYPE_AUDIO;
  140. par->sample_rate = ad->sample_rate;
  141. par->channels = get_al_format_info(ad->sample_format)->channels;
  142. par->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. for (;;) {
  164. /* Get number of samples available */
  165. alcGetIntegerv(ad->device, ALC_CAPTURE_SAMPLES, (ALCsizei) sizeof(ALCint), &nb_samples);
  166. if (error = al_get_error(ad->device, &error_msg)) goto fail;
  167. if (nb_samples > 0)
  168. break;
  169. if (ctx->flags & AVFMT_FLAG_NONBLOCK)
  170. return AVERROR(EAGAIN);
  171. av_usleep(1000);
  172. }
  173. /* Create a packet of appropriate size */
  174. if ((error = av_new_packet(pkt, nb_samples*ad->sample_step)) < 0)
  175. goto fail;
  176. pkt->pts = av_gettime();
  177. /* Fill the packet with the available samples */
  178. alcCaptureSamples(ad->device, pkt->data, nb_samples);
  179. if (error = al_get_error(ad->device, &error_msg)) goto fail;
  180. return pkt->size;
  181. fail:
  182. /* Handle failure */
  183. if (pkt->data)
  184. av_packet_unref(pkt);
  185. if (error_msg)
  186. av_log(ctx, AV_LOG_ERROR, "Error: %s\n", error_msg);
  187. return error;
  188. }
  189. static int read_close(AVFormatContext* ctx)
  190. {
  191. al_data *ad = ctx->priv_data;
  192. if (ad->device) {
  193. alcCaptureStop(ad->device);
  194. alcCaptureCloseDevice(ad->device);
  195. }
  196. return 0;
  197. }
  198. #define OFFSET(x) offsetof(al_data, x)
  199. static const AVOption options[] = {
  200. {"channels", "set number of channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=2}, 1, 2, AV_OPT_FLAG_DECODING_PARAM },
  201. {"sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, 192000, AV_OPT_FLAG_DECODING_PARAM },
  202. {"sample_size", "set sample size", OFFSET(sample_size), AV_OPT_TYPE_INT, {.i64=16}, 8, 16, AV_OPT_FLAG_DECODING_PARAM },
  203. {"list_devices", "list available devices", OFFSET(list_devices), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
  204. {"true", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
  205. {"false", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "list_devices" },
  206. {NULL},
  207. };
  208. static const AVClass class = {
  209. .class_name = "openal indev",
  210. .item_name = av_default_item_name,
  211. .option = options,
  212. .version = LIBAVUTIL_VERSION_INT,
  213. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  214. };
  215. AVInputFormat ff_openal_demuxer = {
  216. .name = "openal",
  217. .long_name = NULL_IF_CONFIG_SMALL("OpenAL audio capture device"),
  218. .priv_data_size = sizeof(al_data),
  219. .read_probe = NULL,
  220. .read_header = read_header,
  221. .read_packet = read_packet,
  222. .read_close = read_close,
  223. .flags = AVFMT_NOFILE,
  224. .priv_class = &class
  225. };