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