]> git.sesse.net Git - ffmpeg/blob - libavformat/beosaudio.cpp
experimental BeOS audio input support. (needs unreleased library)
[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 #ifdef HAVE_BSOUNDRECORDER
35 #include <SoundRecorder.h>
36 #endif
37
38 /* enable performance checks */
39 //#define PERF_CHECK
40
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 typedef struct {
48     int fd; // UNUSED
49     int sample_rate;
50     int channels;
51     int frame_size; /* in bytes ! */
52     CodecID codec_id;
53     uint8_t buffer[AUDIO_BUFFER_SIZE];
54     int buffer_ptr;
55     /* ring buffer */
56     sem_id input_sem;
57     int input_index;
58     sem_id output_sem;
59     int output_index;
60     int queued;
61     BSoundPlayer *player;
62 #ifdef HAVE_BSOUNDRECORDER
63     BSoundRecorder *recorder;
64 #endif
65     int has_quit; /* signal callbacks not to wait */
66     volatile bigtime_t starve_time;
67 } AudioData;
68
69 static thread_id main_thid;
70 static thread_id bapp_thid;
71 static int own_BApp_created = 0;
72 static int refcount = 0;
73
74 /* create the BApplication and Run() it */
75 static int32 bapp_thread(void *arg)
76 {
77     new BApplication("application/x-vnd.ffmpeg");
78     own_BApp_created = 1;
79     be_app->Run();
80     /* kill the process group */
81 //    kill(0, SIGINT);
82 //    kill(main_thid, SIGHUP);
83     return B_OK;
84 }
85
86 /* create the BApplication only if needed */
87 static void create_bapp_if_needed(void)
88 {
89     if (refcount++ == 0) {
90         /* needed by libmedia */
91         if (be_app == NULL) {
92             bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL);
93             resume_thread(bapp_thid);
94             while (!own_BApp_created)
95                 snooze(50000);
96         }
97     }
98 }
99
100 static void destroy_bapp_if_needed(void)
101 {
102     if (--refcount == 0 && own_BApp_created) {
103         be_app->Lock();
104         be_app->Quit();
105         be_app = NULL;
106     }
107 }
108
109 /* called back by BSoundPlayer */
110 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
111 {
112     AudioData *s;
113     size_t len, amount;
114     unsigned char *buf = (unsigned char *)buffer;
115
116     s = (AudioData *)cookie;
117     if (s->has_quit)
118         return;
119     while (bufferSize > 0) {
120 #ifdef PERF_CHECK
121         bigtime_t t;
122         t = system_time();
123 #endif
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 #ifdef PERF_CHECK
141         t = system_time() - t;
142         s->starve_time = MAX(s->starve_time, t);
143 #endif
144         buf += len;
145         bufferSize -= len;
146     }
147 }
148
149 #ifdef HAVE_BSOUNDRECORDER
150 /* called back by BSoundRecorder */
151 static void audiorecord_callback(void *cookie, bigtime_t timestamp, void *buffer, size_t bufferSize, const media_multi_audio_format &format)
152 {
153     AudioData *s;
154     size_t len, amount;
155     unsigned char *buf = (unsigned char *)buffer;
156
157     s = (AudioData *)cookie;
158     if (s->has_quit)
159         return;
160
161     while (bufferSize > 0) {
162         len = MIN(bufferSize, AUDIO_BLOCK_SIZE);
163         //printf("acquire_sem(input, %d)\n", len);
164         if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) {
165             s->has_quit = 1;
166             return;
167         }
168         amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index));
169         memcpy(&s->buffer[s->input_index], buf, amount);
170         s->input_index += amount;
171         if (s->input_index >= AUDIO_BUFFER_SIZE) {
172             s->input_index %= AUDIO_BUFFER_SIZE;
173             memcpy(&s->buffer[s->input_index], buf + amount, len - amount);
174             s->input_index += len - amount;
175         }
176         release_sem_etc(s->output_sem, len, 0);
177         //printf("release_sem(output, %d)\n", len);
178         buf += len;
179         bufferSize -= len;
180     }
181 }
182 #endif
183
184 static int audio_open(AudioData *s, int is_output)
185 {
186     int p[2];
187     int ret;
188     media_raw_audio_format format;
189     media_multi_audio_format iformat;
190
191 #ifndef HAVE_BSOUNDRECORDER
192     if (!is_output)
193         return -EIO; /* not for now */
194 #endif
195     s->input_sem = create_sem(AUDIO_BUFFER_SIZE, "ffmpeg_ringbuffer_input");
196 //    s->input_sem = create_sem(AUDIO_BLOCK_SIZE, "ffmpeg_ringbuffer_input");
197     if (s->input_sem < B_OK)
198         return -EIO;
199     s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output");
200     if (s->output_sem < B_OK) {
201         delete_sem(s->input_sem);
202         return -EIO;
203     }
204     s->input_index = 0;
205     s->output_index = 0;
206     s->queued = 0;
207     create_bapp_if_needed();
208     s->frame_size = AUDIO_BLOCK_SIZE;
209     /* bump up the priority (avoid realtime though) */
210     set_thread_priority(find_thread(NULL), B_DISPLAY_PRIORITY+1);
211 #ifdef HAVE_BSOUNDRECORDER
212     if (!is_output) {
213         s->recorder = new BSoundRecorder(&iformat, false, "ffmpeg input", audiorecord_callback);
214         if (s->recorder->InitCheck() != B_OK || iformat.format != media_raw_audio_format::B_AUDIO_SHORT) {
215             delete s->recorder;
216             s->recorder = NULL;
217             if (s->input_sem)
218                 delete_sem(s->input_sem);
219             if (s->output_sem)
220                 delete_sem(s->output_sem);
221             return -EIO;
222         }
223         s->codec_id = (iformat.byte_order == B_MEDIA_LITTLE_ENDIAN)?CODEC_ID_PCM_S16LE:CODEC_ID_PCM_S16BE;
224         s->channels = iformat.channel_count;
225         s->sample_rate = (int)iformat.frame_rate;
226         s->frame_size = iformat.buffer_size;
227         s->recorder->SetCookie(s);
228         s->recorder->SetVolume(1.0);
229         s->recorder->Start();
230         return 0;
231     }
232 #endif
233     format = media_raw_audio_format::wildcard;
234     format.format = media_raw_audio_format::B_AUDIO_SHORT;
235     format.byte_order = B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN;
236     format.channel_count = s->channels;
237     format.buffer_size = s->frame_size;
238     format.frame_rate = s->sample_rate;
239     s->player = new BSoundPlayer(&format, "ffmpeg output", audioplay_callback);
240     if (s->player->InitCheck() != B_OK) {
241         delete s->player;
242         s->player = NULL;
243         if (s->input_sem)
244             delete_sem(s->input_sem);
245         if (s->output_sem)
246             delete_sem(s->output_sem);
247         return -EIO;
248     }
249     s->player->SetCookie(s);
250     s->player->SetVolume(1.0);
251     s->player->Start();
252     s->player->SetHasData(true);
253     return 0;
254 }
255
256 static int audio_close(AudioData *s)
257 {
258     if (s->input_sem)
259         delete_sem(s->input_sem);
260     if (s->output_sem)
261         delete_sem(s->output_sem);
262     s->has_quit = 1;
263     if (s->player) {
264         s->player->Stop();
265     }
266     if (s->player)
267         delete s->player;
268 #ifdef HAVE_BSOUNDRECORDER
269     if (s->recorder)
270         delete s->recorder;
271 #endif
272     destroy_bapp_if_needed();
273     return 0;
274 }
275
276 /* sound output support */
277 static int audio_write_header(AVFormatContext *s1)
278 {
279     AudioData *s = (AudioData *)s1->priv_data;
280     AVStream *st;
281     int ret;
282
283     st = s1->streams[0];
284     s->sample_rate = st->codec.sample_rate;
285     s->channels = st->codec.channels;
286     ret = audio_open(s, 1);
287     if (ret < 0)
288         return -EIO;
289     return 0;
290 }
291
292 static int audio_write_packet(AVFormatContext *s1, int stream_index,
293                               uint8_t *buf, int size, int force_pts)
294 {
295     AudioData *s = (AudioData *)s1->priv_data;
296     int len, ret;
297 #ifdef PERF_CHECK
298     bigtime_t t = s->starve_time;
299     s->starve_time = 0;
300     printf("starve_time: %lld    \n", t);
301 #endif
302     while (size > 0) {
303         int amount;
304         len = MIN(size, AUDIO_BLOCK_SIZE);
305         if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK)
306             return -EIO;
307         amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index));
308         memcpy(&s->buffer[s->input_index], buf, amount);
309         s->input_index += amount;
310         if (s->input_index >= AUDIO_BUFFER_SIZE) {
311             s->input_index %= AUDIO_BUFFER_SIZE;
312             memcpy(&s->buffer[s->input_index], buf + amount, len - amount);
313             s->input_index += len - amount;
314         }
315         release_sem_etc(s->output_sem, len, 0);
316         buf += len;
317         size -= len;
318     }
319     return 0;
320 }
321
322 static int audio_write_trailer(AVFormatContext *s1)
323 {
324     AudioData *s = (AudioData *)s1->priv_data;
325
326     audio_close(s);
327     return 0;
328 }
329
330 /* grab support */
331
332 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
333 {
334     AudioData *s = (AudioData *)s1->priv_data;
335     AVStream *st;
336     int ret;
337
338     if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
339         return -1;
340
341     st = av_new_stream(s1, 0);
342     if (!st) {
343         return -ENOMEM;
344     }
345     s->sample_rate = ap->sample_rate;
346     s->channels = ap->channels;
347
348     ret = audio_open(s, 0);
349     if (ret < 0) {
350         av_free(st);
351         return -EIO;
352     }
353     /* take real parameters */
354     st->codec.codec_type = CODEC_TYPE_AUDIO;
355     st->codec.codec_id = s->codec_id;
356     st->codec.sample_rate = s->sample_rate;
357     st->codec.channels = s->channels;
358     return 0;
359     av_set_pts_info(s1, 48, 1, 1000000);  /* 48 bits pts in us */
360 }
361
362 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
363 {
364     AudioData *s = (AudioData *)s1->priv_data;
365     int size;
366     size_t len, amount;
367     unsigned char *buf;
368     status_t err;
369
370     if (av_new_packet(pkt, s->frame_size) < 0)
371         return -EIO;
372     buf = (unsigned char *)pkt->data;
373     size = pkt->size;
374     while (size > 0) {
375         len = MIN(AUDIO_BLOCK_SIZE, size);
376         //printf("acquire_sem(output, %d)\n", len);
377         while ((err=acquire_sem_etc(s->output_sem, len, B_CAN_INTERRUPT, 0LL)) == B_INTERRUPTED);
378         if (err < B_OK) {
379             av_free_packet(pkt);
380             return -EIO;
381         }
382         amount = MIN(len, (AUDIO_BUFFER_SIZE - s->output_index));
383         memcpy(buf, &s->buffer[s->output_index], amount);
384         s->output_index += amount;
385         if (s->output_index >= AUDIO_BUFFER_SIZE) {
386             s->output_index %= AUDIO_BUFFER_SIZE;
387             memcpy(buf + amount, &s->buffer[s->output_index], len - amount);
388             s->output_index += len-amount;
389             s->output_index %= AUDIO_BUFFER_SIZE;
390         }
391         release_sem_etc(s->input_sem, len, 0);
392         //printf("release_sem(input, %d)\n", len);
393         buf += len;
394         size -= len;
395     }
396     //XXX: add pts info
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 static 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