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.

323 lines
7.7KB

  1. /*
  2. * BeOS audio play interface
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. #include <Application.h>
  26. #include <SoundPlayer.h>
  27. extern "C" {
  28. #include "avformat.h"
  29. }
  30. //const char *audio_device = "/dev/dsp";
  31. const char *audio_device = "beosaudio:";
  32. #define AUDIO_BLOCK_SIZE 4096
  33. typedef struct {
  34. int fd;
  35. int sample_rate;
  36. int channels;
  37. int frame_size; /* in bytes ! */
  38. CodecID codec_id;
  39. int flip_left : 1;
  40. UINT8 buffer[AUDIO_BLOCK_SIZE];
  41. int buffer_ptr;
  42. int pipefd; /* the other end of the pipe */
  43. BSoundPlayer *player;
  44. int has_quit; /* signal callbacks not to wait */
  45. } AudioData;
  46. static int own_BApp_created = 0;
  47. /* create the BApplication and Run() it */
  48. static int32 bapp_thread(void *arg)
  49. {
  50. new BApplication("application/x-vnd.ffmpeg");
  51. own_BApp_created = 1;
  52. be_app->Run();
  53. /* kill the process group */
  54. kill(0, SIGINT);
  55. return B_OK;
  56. }
  57. /* called back by BSoundPlayer */
  58. static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
  59. {
  60. AudioData *s;
  61. size_t amount;
  62. unsigned char *buf = (unsigned char *)buffer;
  63. s = (AudioData *)cookie;
  64. if (s->has_quit)
  65. return;
  66. while (bufferSize > 0) {
  67. if ((amount = read(s->pipefd, buf, bufferSize)) < B_OK) {
  68. puts("EPIPE");
  69. snooze(100000);
  70. s->player->SetHasData(false);
  71. return;
  72. }
  73. if (amount == 0) {
  74. snooze(100000);
  75. s->player->SetHasData(false);
  76. return;
  77. }
  78. buf += amount;
  79. bufferSize -= amount;
  80. }
  81. }
  82. static int audio_open(AudioData *s, int is_output)
  83. {
  84. int p[2];
  85. int ret;
  86. media_raw_audio_format format;
  87. if (!is_output)
  88. return -EIO; /* not for now */
  89. ret = pipe(p);
  90. if (ret < 0)
  91. return -EIO;
  92. s->fd = p[is_output?1:0];
  93. s->pipefd = p[is_output?0:1];
  94. if (s->fd < 0) {
  95. perror(is_output?"audio out":"audio in");
  96. return -EIO;
  97. }
  98. /* non blocking mode */
  99. // fcntl(s->fd, F_SETFL, O_NONBLOCK);
  100. // fcntl(s->pipefd, F_SETFL, O_NONBLOCK);
  101. s->frame_size = AUDIO_BLOCK_SIZE;
  102. format = media_raw_audio_format::wildcard;
  103. format.format = media_raw_audio_format::B_AUDIO_SHORT;
  104. format.byte_order = B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN;
  105. format.channel_count = s->channels;
  106. format.buffer_size = s->frame_size;
  107. format.frame_rate = s->sample_rate;
  108. s->player = new BSoundPlayer(&format, "ffmpeg output", audioplay_callback);
  109. if (s->player->InitCheck() != B_OK) {
  110. delete s->player;
  111. s->player = NULL;
  112. close(s->fd);
  113. close(s->pipefd);
  114. return -EIO;
  115. }
  116. s->player->SetCookie(s);
  117. s->player->SetVolume(1.0);
  118. s->player->Start();
  119. s->player->SetHasData(true);
  120. return 0;
  121. }
  122. static int audio_close(AudioData *s)
  123. {
  124. s->has_quit = 1;
  125. if (s->player) {
  126. s->player->Stop();
  127. }
  128. if (s->player)
  129. delete s->player;
  130. close(s->pipefd);
  131. close(s->fd);
  132. return 0;
  133. }
  134. /* sound output support */
  135. static int audio_write_header(AVFormatContext *s1)
  136. {
  137. AudioData *s = (AudioData *)s1->priv_data;
  138. AVStream *st;
  139. int ret;
  140. st = s1->streams[0];
  141. s->sample_rate = st->codec.sample_rate;
  142. s->channels = st->codec.channels;
  143. ret = audio_open(s, 1);
  144. if (ret < 0)
  145. return -EIO;
  146. return 0;
  147. }
  148. static int audio_write_packet(AVFormatContext *s1, int stream_index,
  149. UINT8 *buf, int size, int force_pts)
  150. {
  151. AudioData *s = (AudioData *)s1->priv_data;
  152. int len, ret;
  153. while (size > 0) {
  154. len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
  155. if (len > size)
  156. len = size;
  157. memcpy(s->buffer + s->buffer_ptr, buf, len);
  158. s->buffer_ptr += len;
  159. if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
  160. for(;;) {
  161. snooze(1000);
  162. ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
  163. if (ret != 0)
  164. break;
  165. if (ret < 0 && (errno != EAGAIN && errno != EINTR))
  166. return -EIO;
  167. }
  168. s->buffer_ptr = 0;
  169. }
  170. buf += len;
  171. size -= len;
  172. }
  173. return 0;
  174. }
  175. static int audio_write_trailer(AVFormatContext *s1)
  176. {
  177. AudioData *s = (AudioData *)s1->priv_data;
  178. audio_close(s);
  179. return 0;
  180. }
  181. /* grab support */
  182. static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  183. {
  184. AudioData *s = (AudioData *)s1->priv_data;
  185. AVStream *st;
  186. int ret;
  187. if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
  188. return -1;
  189. st = av_new_stream(s1, 0);
  190. if (!st) {
  191. return -ENOMEM;
  192. }
  193. s->sample_rate = ap->sample_rate;
  194. s->channels = ap->channels;
  195. ret = audio_open(s, 0);
  196. if (ret < 0) {
  197. av_free(st);
  198. return -EIO;
  199. } else {
  200. /* take real parameters */
  201. st->codec.codec_type = CODEC_TYPE_AUDIO;
  202. st->codec.codec_id = s->codec_id;
  203. st->codec.sample_rate = s->sample_rate;
  204. st->codec.channels = s->channels;
  205. return 0;
  206. }
  207. }
  208. static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
  209. {
  210. AudioData *s = (AudioData *)s1->priv_data;
  211. int ret;
  212. if (av_new_packet(pkt, s->frame_size) < 0)
  213. return -EIO;
  214. for(;;) {
  215. ret = read(s->fd, pkt->data, pkt->size);
  216. if (ret > 0)
  217. break;
  218. if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
  219. av_free_packet(pkt);
  220. pkt->size = 0;
  221. return 0;
  222. }
  223. if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
  224. av_free_packet(pkt);
  225. return -EIO;
  226. }
  227. }
  228. pkt->size = ret;
  229. if (s->flip_left && s->channels == 2) {
  230. int i;
  231. short *p = (short *) pkt->data;
  232. for (i = 0; i < ret; i += 4) {
  233. *p = ~*p;
  234. p += 2;
  235. }
  236. }
  237. return 0;
  238. }
  239. static int audio_read_close(AVFormatContext *s1)
  240. {
  241. AudioData *s = (AudioData *)s1->priv_data;
  242. audio_close(s);
  243. return 0;
  244. }
  245. AVInputFormat audio_in_format = {
  246. "audio_device",
  247. "audio grab and output",
  248. sizeof(AudioData),
  249. NULL,
  250. audio_read_header,
  251. audio_read_packet,
  252. audio_read_close,
  253. NULL,
  254. AVFMT_NOFILE,
  255. };
  256. AVOutputFormat audio_out_format = {
  257. "audio_device",
  258. "audio grab and output",
  259. "",
  260. "",
  261. sizeof(AudioData),
  262. #ifdef WORDS_BIGENDIAN
  263. CODEC_ID_PCM_S16BE,
  264. #else
  265. CODEC_ID_PCM_S16LE,
  266. #endif
  267. CODEC_ID_NONE,
  268. audio_write_header,
  269. audio_write_packet,
  270. audio_write_trailer,
  271. AVFMT_NOFILE,
  272. };
  273. extern "C" {
  274. int audio_init(void)
  275. {
  276. /* needed by libmedia */
  277. if (be_app == NULL) {
  278. resume_thread(spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL));
  279. while (!own_BApp_created)
  280. snooze(50000);
  281. }
  282. av_register_input_format(&audio_in_format);
  283. av_register_output_format(&audio_out_format);
  284. return 0;
  285. }
  286. } // "C"