]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/beosaudio.cpp
renamed libav to libavformat
[frescor/ffmpeg.git] / libavformat / beosaudio.cpp
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
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26
27 #include <Application.h>
28 #include <SoundPlayer.h>
29
30 extern "C" {
31 #include "avformat.h"
32 }
33
34 /* enable performance checks */
35 //#define PERF_CHECK
36
37 //const char *audio_device = "/dev/dsp";
38 const char *audio_device = "beosaudio:";
39
40 /* Pipes are 4k in BeOS IIRC */
41 #define AUDIO_BLOCK_SIZE 4096
42 //#define AUDIO_BLOCK_SIZE 2048
43 #define AUDIO_BLOCK_COUNT 8
44
45 #define AUDIO_BUFFER_SIZE (AUDIO_BLOCK_SIZE*AUDIO_BLOCK_COUNT)
46
47 /* pipes suck for realtime */
48 #define USE_RING_BUFFER 1
49
50 typedef struct {
51     int fd;
52     int sample_rate;
53     int channels;
54     int frame_size; /* in bytes ! */
55     CodecID codec_id;
56     int flip_left : 1;
57     UINT8 buffer[AUDIO_BUFFER_SIZE];
58     int buffer_ptr;
59     int pipefd; /* the other end of the pipe */
60     /* ring buffer */
61     sem_id input_sem;
62     int input_index;
63     sem_id output_sem;
64     int output_index;
65     int queued;
66     BSoundPlayer *player;
67     int has_quit; /* signal callbacks not to wait */
68     volatile bigtime_t starve_time;
69 } AudioData;
70
71 static thread_id main_thid;
72 static thread_id bapp_thid;
73 static int own_BApp_created = 0;
74 static int refcount = 0;
75
76 /* create the BApplication and Run() it */
77 static int32 bapp_thread(void *arg)
78 {
79     new BApplication("application/x-vnd.ffmpeg");
80     own_BApp_created = 1;
81     be_app->Run();
82     /* kill the process group */
83 //    kill(0, SIGINT);
84 //    kill(main_thid, SIGHUP);
85     return B_OK;
86 }
87
88 /* create the BApplication only if needed */
89 static void create_bapp_if_needed(void)
90 {
91     if (refcount++ == 0) {
92         /* needed by libmedia */
93         if (be_app == NULL) {
94             bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL);
95             resume_thread(bapp_thid);
96             while (!own_BApp_created)
97                 snooze(50000);
98         }
99     }
100 }
101
102 static void destroy_bapp_if_needed(void)
103 {
104     if (--refcount == 0 && own_BApp_created) {
105         be_app->Lock();
106         be_app->Quit();
107         be_app = NULL;
108     }
109 }
110
111 /* called back by BSoundPlayer */
112 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
113 {
114     AudioData *s;
115     size_t len, amount;
116     unsigned char *buf = (unsigned char *)buffer;
117
118     s = (AudioData *)cookie;
119     if (s->has_quit)
120         return;
121     while (bufferSize > 0) {
122 #ifdef PERF_CHECK
123         bigtime_t t;
124         t = system_time();
125 #endif
126 #ifdef USE_RING_BUFFER
127         len = MIN(AUDIO_BLOCK_SIZE, bufferSize);
128         if (acquire_sem_etc(s->output_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) {
129             s->has_quit = 1;
130             s->player->SetHasData(false);
131             return;
132         }
133         amount = MIN(len, (AUDIO_BUFFER_SIZE - s->output_index));
134         memcpy(buf, &s->buffer[s->output_index], amount);
135         s->output_index += amount;
136         if (s->output_index >= AUDIO_BUFFER_SIZE) {
137             s->output_index %= AUDIO_BUFFER_SIZE;
138             memcpy(buf + amount, &s->buffer[s->output_index], len - amount);
139             s->output_index += len-amount;
140             s->output_index %= AUDIO_BUFFER_SIZE;
141         }
142         release_sem_etc(s->input_sem, len, 0);
143 #else
144         len = read(s->pipefd, buf, bufferSize);
145 #endif
146 #ifdef PERF_CHECK
147         t = system_time() - t;
148         s->starve_time = MAX(s->starve_time, t);
149 #endif
150 #ifndef USE_RING_BUFFER
151         if (len < B_OK) {
152             puts("EPIPE");
153             s->player->SetHasData(false);
154             snooze(100000);
155             return;
156         }
157         if (len == 0) {
158             s->player->SetHasData(false);
159             snooze(100000);
160             return;
161         }
162 #endif
163         buf += len;
164         bufferSize -= len;
165     }
166 }
167
168 static int audio_open(AudioData *s, int is_output)
169 {
170     int p[2];
171     int ret;
172     media_raw_audio_format format;
173
174     if (!is_output)
175         return -EIO; /* not for now */
176 #ifdef USE_RING_BUFFER
177     s->input_sem = create_sem(AUDIO_BUFFER_SIZE, "ffmpeg_ringbuffer_input");
178 //    s->input_sem = create_sem(AUDIO_BLOCK_SIZE, "ffmpeg_ringbuffer_input");
179     if (s->input_sem < B_OK)
180         return -EIO;
181     s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output");
182     if (s->output_sem < B_OK) {
183         delete_sem(s->input_sem);
184         return -EIO;
185     }
186     s->input_index = 0;
187     s->output_index = 0;
188     s->queued = 0;
189 #else
190     ret = pipe(p);
191     if (ret < 0)
192         return -EIO;
193     s->fd = p[is_output?1:0];
194     s->pipefd = p[is_output?0:1];
195     if (s->fd < 0) {
196         perror(is_output?"audio out":"audio in");
197         return -EIO;
198     }
199 #endif
200     create_bapp_if_needed();
201     /* non blocking mode */
202 //    fcntl(s->fd, F_SETFL, O_NONBLOCK);
203 //    fcntl(s->pipefd, F_SETFL, O_NONBLOCK);
204     s->frame_size = AUDIO_BLOCK_SIZE;
205     format = media_raw_audio_format::wildcard;
206     format.format = media_raw_audio_format::B_AUDIO_SHORT;
207     format.byte_order = B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN;
208     format.channel_count = s->channels;
209     format.buffer_size = s->frame_size;
210     format.frame_rate = s->sample_rate;
211     s->player = new BSoundPlayer(&format, "ffmpeg output", audioplay_callback);
212     if (s->player->InitCheck() != B_OK) {
213         delete s->player;
214         s->player = NULL;
215 #ifdef USE_RING_BUFFER
216     if (s->input_sem)
217         delete_sem(s->input_sem);
218     if (s->output_sem)
219         delete_sem(s->output_sem);
220 #else
221         close(s->fd);
222         close(s->pipefd);
223 #endif
224         return -EIO;
225     }
226     s->player->SetCookie(s);
227     s->player->SetVolume(1.0);
228     s->player->Start();
229     s->player->SetHasData(true);
230     /* bump up the priority (avoid realtime though) */
231     set_thread_priority(find_thread(NULL), B_DISPLAY_PRIORITY+1);
232     return 0;
233 }
234
235 static int audio_close(AudioData *s)
236 {
237 #ifdef USE_RING_BUFFER
238     if (s->input_sem)
239         delete_sem(s->input_sem);
240     if (s->output_sem)
241         delete_sem(s->output_sem);
242 #endif
243     s->has_quit = 1;
244     if (s->player) {
245         s->player->Stop();
246     }
247     if (s->player)
248         delete s->player;
249 #ifndef USE_RING_BUFFER
250     close(s->pipefd);
251     close(s->fd);
252 #endif
253     destroy_bapp_if_needed();
254     return 0;
255 }
256
257 /* sound output support */
258 static int audio_write_header(AVFormatContext *s1)
259 {
260     AudioData *s = (AudioData *)s1->priv_data;
261     AVStream *st;
262     int ret;
263
264     st = s1->streams[0];
265     s->sample_rate = st->codec.sample_rate;
266     s->channels = st->codec.channels;
267     ret = audio_open(s, 1);
268     if (ret < 0)
269         return -EIO;
270     return 0;
271 }
272
273 static int audio_write_packet(AVFormatContext *s1, int stream_index,
274                               UINT8 *buf, int size, int force_pts)
275 {
276     AudioData *s = (AudioData *)s1->priv_data;
277     int len, ret;
278 #ifdef PERF_CHECK
279     bigtime_t t = s->starve_time;
280     s->starve_time = 0;
281     printf("starve_time: %lld    \n", t);
282 #endif
283 #ifdef USE_RING_BUFFER
284     while (size > 0) {
285         int amount;
286         len = MIN(size, AUDIO_BLOCK_SIZE);
287         if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK)
288             return -EIO;
289         amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index));
290         memcpy(&s->buffer[s->input_index], buf, amount);
291         s->input_index += amount;
292         if (s->input_index >= AUDIO_BUFFER_SIZE) {
293             s->input_index %= AUDIO_BUFFER_SIZE;
294             memcpy(&s->buffer[s->input_index], buf + amount, len - amount);
295             s->input_index += len - amount;
296         }
297         release_sem_etc(s->output_sem, len, 0);
298         buf += len;
299         size -= len;
300     }
301 #else
302     while (size > 0) {
303         len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
304         if (len > size)
305             len = size;
306         memcpy(s->buffer + s->buffer_ptr, buf, len);
307         s->buffer_ptr += len;
308         if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
309             for(;;) {
310 //snooze(1000);
311                 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
312                 if (ret != 0)
313                     break;
314                 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
315                     return -EIO;
316             }
317             s->buffer_ptr = 0;
318         }
319         buf += len;
320         size -= len;
321     }
322 #endif
323     return 0;
324 }
325
326 static int audio_write_trailer(AVFormatContext *s1)
327 {
328     AudioData *s = (AudioData *)s1->priv_data;
329
330     audio_close(s);
331     return 0;
332 }
333
334 /* grab support */
335
336 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
337 {
338     AudioData *s = (AudioData *)s1->priv_data;
339     AVStream *st;
340     int ret;
341
342     if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
343         return -1;
344
345     st = av_new_stream(s1, 0);
346     if (!st) {
347         return -ENOMEM;
348     }
349     s->sample_rate = ap->sample_rate;
350     s->channels = ap->channels;
351
352     ret = audio_open(s, 0);
353     if (ret < 0) {
354         av_free(st);
355         return -EIO;
356     } else {
357         /* take real parameters */
358         st->codec.codec_type = CODEC_TYPE_AUDIO;
359         st->codec.codec_id = s->codec_id;
360         st->codec.sample_rate = s->sample_rate;
361         st->codec.channels = s->channels;
362         return 0;
363     }
364 }
365
366 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
367 {
368     AudioData *s = (AudioData *)s1->priv_data;
369     int ret;
370
371     if (av_new_packet(pkt, s->frame_size) < 0)
372         return -EIO;
373     for(;;) {
374         ret = read(s->fd, pkt->data, pkt->size);
375         if (ret > 0)
376             break;
377         if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
378             av_free_packet(pkt);
379             pkt->size = 0;
380             return 0;
381         }
382         if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
383             av_free_packet(pkt);
384             return -EIO;
385         }
386     }
387     pkt->size = ret;
388     if (s->flip_left && s->channels == 2) {
389         int i;
390         short *p = (short *) pkt->data;
391
392         for (i = 0; i < ret; i += 4) {
393             *p = ~*p;
394             p += 2;
395         }
396     }
397     return 0;
398 }
399
400 static int audio_read_close(AVFormatContext *s1)
401 {
402     AudioData *s = (AudioData *)s1->priv_data;
403
404     audio_close(s);
405     return 0;
406 }
407
408 AVInputFormat audio_in_format = {
409     "audio_device",
410     "audio grab and output",
411     sizeof(AudioData),
412     NULL,
413     audio_read_header,
414     audio_read_packet,
415     audio_read_close,
416     NULL,
417     AVFMT_NOFILE,
418 };
419
420 AVOutputFormat audio_out_format = {
421     "audio_device",
422     "audio grab and output",
423     "",
424     "",
425     sizeof(AudioData),
426 #ifdef WORDS_BIGENDIAN
427     CODEC_ID_PCM_S16BE,
428 #else
429     CODEC_ID_PCM_S16LE,
430 #endif
431     CODEC_ID_NONE,
432     audio_write_header,
433     audio_write_packet,
434     audio_write_trailer,
435     AVFMT_NOFILE,
436 };
437
438 extern "C" {
439
440 int audio_init(void)
441 {
442     main_thid = find_thread(NULL);
443     av_register_input_format(&audio_in_format);
444     av_register_output_format(&audio_out_format);
445     return 0;
446 }
447
448 } // "C"
449