2 * BeOS audio play interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
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.
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
27 #include <Application.h>
28 #include <SoundPlayer.h>
34 /* enable performance checks */
37 //const char *audio_device = "/dev/dsp";
38 const char *audio_device = "beosaudio:";
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
45 #define AUDIO_BUFFER_SIZE (AUDIO_BLOCK_SIZE*AUDIO_BLOCK_COUNT)
47 /* pipes suck for realtime */
48 #define USE_RING_BUFFER 1
54 int frame_size; /* in bytes ! */
57 UINT8 buffer[AUDIO_BUFFER_SIZE];
59 int pipefd; /* the other end of the pipe */
67 int has_quit; /* signal callbacks not to wait */
68 volatile bigtime_t starve_time;
71 static thread_id main_thid;
72 static thread_id bapp_thid;
73 static int own_BApp_created = 0;
74 static int refcount = 0;
76 /* create the BApplication and Run() it */
77 static int32 bapp_thread(void *arg)
79 new BApplication("application/x-vnd.ffmpeg");
82 /* kill the process group */
84 // kill(main_thid, SIGHUP);
88 /* create the BApplication only if needed */
89 static void create_bapp_if_needed(void)
91 if (refcount++ == 0) {
92 /* needed by libmedia */
94 bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL);
95 resume_thread(bapp_thid);
96 while (!own_BApp_created)
102 static void destroy_bapp_if_needed(void)
104 if (--refcount == 0 && own_BApp_created) {
111 /* called back by BSoundPlayer */
112 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
116 unsigned char *buf = (unsigned char *)buffer;
118 s = (AudioData *)cookie;
121 while (bufferSize > 0) {
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) {
130 s->player->SetHasData(false);
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;
142 release_sem_etc(s->input_sem, len, 0);
144 len = read(s->pipefd, buf, bufferSize);
147 t = system_time() - t;
148 s->starve_time = MAX(s->starve_time, t);
150 #ifndef USE_RING_BUFFER
153 s->player->SetHasData(false);
158 s->player->SetHasData(false);
168 static int audio_open(AudioData *s, int is_output)
172 media_raw_audio_format format;
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)
181 s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output");
182 if (s->output_sem < B_OK) {
183 delete_sem(s->input_sem);
193 s->fd = p[is_output?1:0];
194 s->pipefd = p[is_output?0:1];
196 perror(is_output?"audio out":"audio in");
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) {
215 #ifdef USE_RING_BUFFER
217 delete_sem(s->input_sem);
219 delete_sem(s->output_sem);
226 s->player->SetCookie(s);
227 s->player->SetVolume(1.0);
229 s->player->SetHasData(true);
230 /* bump up the priority (avoid realtime though) */
231 set_thread_priority(find_thread(NULL), B_DISPLAY_PRIORITY+1);
235 static int audio_close(AudioData *s)
237 #ifdef USE_RING_BUFFER
239 delete_sem(s->input_sem);
241 delete_sem(s->output_sem);
249 #ifndef USE_RING_BUFFER
253 destroy_bapp_if_needed();
257 /* sound output support */
258 static int audio_write_header(AVFormatContext *s1)
260 AudioData *s = (AudioData *)s1->priv_data;
265 s->sample_rate = st->codec.sample_rate;
266 s->channels = st->codec.channels;
267 ret = audio_open(s, 1);
273 static int audio_write_packet(AVFormatContext *s1, int stream_index,
274 UINT8 *buf, int size, int force_pts)
276 AudioData *s = (AudioData *)s1->priv_data;
279 bigtime_t t = s->starve_time;
281 printf("starve_time: %lld \n", t);
283 #ifdef USE_RING_BUFFER
286 len = MIN(size, AUDIO_BLOCK_SIZE);
287 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK)
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;
297 release_sem_etc(s->output_sem, len, 0);
303 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
306 memcpy(s->buffer + s->buffer_ptr, buf, len);
307 s->buffer_ptr += len;
308 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
311 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
314 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
326 static int audio_write_trailer(AVFormatContext *s1)
328 AudioData *s = (AudioData *)s1->priv_data;
336 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
338 AudioData *s = (AudioData *)s1->priv_data;
342 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
345 st = av_new_stream(s1, 0);
349 s->sample_rate = ap->sample_rate;
350 s->channels = ap->channels;
352 ret = audio_open(s, 0);
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;
366 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
368 AudioData *s = (AudioData *)s1->priv_data;
371 if (av_new_packet(pkt, s->frame_size) < 0)
374 ret = read(s->fd, pkt->data, pkt->size);
377 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
382 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
388 if (s->flip_left && s->channels == 2) {
390 short *p = (short *) pkt->data;
392 for (i = 0; i < ret; i += 4) {
400 static int audio_read_close(AVFormatContext *s1)
402 AudioData *s = (AudioData *)s1->priv_data;
408 AVInputFormat audio_in_format = {
410 "audio grab and output",
420 AVOutputFormat audio_out_format = {
422 "audio grab and output",
426 #ifdef WORDS_BIGENDIAN
442 main_thid = find_thread(NULL);
443 av_register_input_format(&audio_in_format);
444 av_register_output_format(&audio_out_format);