You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.8KB

  1. /*
  2. * Linux audio play and grab interface
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <linux/soundcard.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/mman.h>
  28. #include <sys/time.h>
  29. const char *audio_device = "/dev/dsp";
  30. typedef struct {
  31. int fd;
  32. int rate;
  33. int channels;
  34. } AudioData;
  35. #define AUDIO_BLOCK_SIZE 4096
  36. /* audio read support */
  37. static int audio_read(URLContext *h, UINT8 *buf, int size)
  38. {
  39. AudioData *s = h->priv_data;
  40. int ret;
  41. ret = read(s->fd, buf, size);
  42. if (ret < 0)
  43. return -errno;
  44. else
  45. return ret;
  46. }
  47. static int audio_write(URLContext *h, UINT8 *buf, int size)
  48. {
  49. AudioData *s = h->priv_data;
  50. int ret;
  51. ret = write(s->fd, buf, size);
  52. if (ret < 0)
  53. return -errno;
  54. else
  55. return ret;
  56. }
  57. static int audio_get_format(URLContext *h, URLFormat *f)
  58. {
  59. AudioData *s = h->priv_data;
  60. strcpy(f->format_name, "pcm");
  61. f->sample_rate = s->rate;
  62. f->channels = s->channels;
  63. return 0;
  64. }
  65. /* URI syntax: 'audio:[rate[,channels]]'
  66. default: rate=44100, channels=2
  67. */
  68. static int audio_open(URLContext *h, const char *uri, int flags)
  69. {
  70. AudioData *s;
  71. const char *p;
  72. int freq, channels, audio_fd;
  73. int tmp, err;
  74. h->is_streamed = 1;
  75. h->packet_size = AUDIO_BLOCK_SIZE;
  76. s = malloc(sizeof(AudioData));
  77. if (!s)
  78. return -ENOMEM;
  79. h->priv_data = s;
  80. /* extract parameters */
  81. p = uri;
  82. strstart(p, "audio:", &p);
  83. freq = strtol(p, (char **)&p, 0);
  84. if (freq <= 0)
  85. freq = 44100;
  86. if (*p == ',')
  87. p++;
  88. channels = strtol(p, (char **)&p, 0);
  89. if (channels <= 0)
  90. channels = 2;
  91. s->rate = freq;
  92. s->channels = channels;
  93. /* open linux audio device */
  94. if (flags & URL_WRONLY)
  95. audio_fd = open(audio_device,O_WRONLY);
  96. else
  97. audio_fd = open(audio_device,O_RDONLY);
  98. if (audio_fd < 0) {
  99. perror(audio_device);
  100. return -EIO;
  101. }
  102. /* non blocking mode */
  103. fcntl(audio_fd, F_SETFL, O_NONBLOCK);
  104. #if 0
  105. tmp=(NB_FRAGMENTS << 16) | FRAGMENT_BITS;
  106. err=ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
  107. if (err < 0) {
  108. perror("SNDCTL_DSP_SETFRAGMENT");
  109. }
  110. #endif
  111. tmp=AFMT_S16_LE;
  112. err=ioctl(audio_fd,SNDCTL_DSP_SETFMT,&tmp);
  113. if (err < 0) {
  114. perror("SNDCTL_DSP_SETFMT");
  115. goto fail;
  116. }
  117. tmp= (channels == 2);
  118. err=ioctl(audio_fd,SNDCTL_DSP_STEREO,&tmp);
  119. if (err < 0) {
  120. perror("SNDCTL_DSP_STEREO");
  121. goto fail;
  122. }
  123. tmp = freq;
  124. err=ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
  125. if (err < 0) {
  126. perror("SNDCTL_DSP_SPEED");
  127. goto fail;
  128. }
  129. s->rate = tmp;
  130. s->fd = audio_fd;
  131. return 0;
  132. fail:
  133. close(audio_fd);
  134. free(s);
  135. return -EIO;
  136. }
  137. static int audio_close(URLContext *h)
  138. {
  139. AudioData *s = h->priv_data;
  140. close(s->fd);
  141. free(s);
  142. return 0;
  143. }
  144. URLProtocol audio_protocol = {
  145. "audio",
  146. audio_open,
  147. audio_read,
  148. audio_write,
  149. NULL, /* seek */
  150. audio_close,
  151. audio_get_format,
  152. };