openal-dec.c 8.6 KB

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