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 /* Pipes are 4k in BeOS IIRC */
38 #define AUDIO_BLOCK_SIZE 4096
39 //#define AUDIO_BLOCK_SIZE 2048
40 #define AUDIO_BLOCK_COUNT 8
42 #define AUDIO_BUFFER_SIZE (AUDIO_BLOCK_SIZE*AUDIO_BLOCK_COUNT)
44 /* pipes suck for realtime */
45 #define USE_RING_BUFFER 1
51 int frame_size; /* in bytes ! */
54 uint8_t buffer[AUDIO_BUFFER_SIZE];
56 int pipefd; /* the other end of the pipe */
64 int has_quit; /* signal callbacks not to wait */
65 volatile bigtime_t starve_time;
68 static thread_id main_thid;
69 static thread_id bapp_thid;
70 static int own_BApp_created = 0;
71 static int refcount = 0;
73 /* create the BApplication and Run() it */
74 static int32 bapp_thread(void *arg)
76 new BApplication("application/x-vnd.ffmpeg");
79 /* kill the process group */
81 // kill(main_thid, SIGHUP);
85 /* create the BApplication only if needed */
86 static void create_bapp_if_needed(void)
88 if (refcount++ == 0) {
89 /* needed by libmedia */
91 bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL);
92 resume_thread(bapp_thid);
93 while (!own_BApp_created)
99 static void destroy_bapp_if_needed(void)
101 if (--refcount == 0 && own_BApp_created) {
108 /* called back by BSoundPlayer */
109 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
113 unsigned char *buf = (unsigned char *)buffer;
115 s = (AudioData *)cookie;
118 while (bufferSize > 0) {
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) {
127 s->player->SetHasData(false);
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;
139 release_sem_etc(s->input_sem, len, 0);
141 len = read(s->pipefd, buf, bufferSize);
144 t = system_time() - t;
145 s->starve_time = MAX(s->starve_time, t);
147 #ifndef USE_RING_BUFFER
150 s->player->SetHasData(false);
155 s->player->SetHasData(false);
165 static int audio_open(AudioData *s, int is_output)
169 media_raw_audio_format format;
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)
178 s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output");
179 if (s->output_sem < B_OK) {
180 delete_sem(s->input_sem);
190 s->fd = p[is_output?1:0];
191 s->pipefd = p[is_output?0:1];
193 perror(is_output?"audio out":"audio in");
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) {
212 #ifdef USE_RING_BUFFER
214 delete_sem(s->input_sem);
216 delete_sem(s->output_sem);
223 s->player->SetCookie(s);
224 s->player->SetVolume(1.0);
226 s->player->SetHasData(true);
227 /* bump up the priority (avoid realtime though) */
228 set_thread_priority(find_thread(NULL), B_DISPLAY_PRIORITY+1);
232 static int audio_close(AudioData *s)
234 #ifdef USE_RING_BUFFER
236 delete_sem(s->input_sem);
238 delete_sem(s->output_sem);
246 #ifndef USE_RING_BUFFER
250 destroy_bapp_if_needed();
254 /* sound output support */
255 static int audio_write_header(AVFormatContext *s1)
257 AudioData *s = (AudioData *)s1->priv_data;
262 s->sample_rate = st->codec.sample_rate;
263 s->channels = st->codec.channels;
264 ret = audio_open(s, 1);
270 static int audio_write_packet(AVFormatContext *s1, int stream_index,
271 uint8_t *buf, int size, int force_pts)
273 AudioData *s = (AudioData *)s1->priv_data;
276 bigtime_t t = s->starve_time;
278 printf("starve_time: %lld \n", t);
280 #ifdef USE_RING_BUFFER
283 len = MIN(size, AUDIO_BLOCK_SIZE);
284 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK)
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;
294 release_sem_etc(s->output_sem, len, 0);
300 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
303 memcpy(s->buffer + s->buffer_ptr, buf, len);
304 s->buffer_ptr += len;
305 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
308 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
311 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
323 static int audio_write_trailer(AVFormatContext *s1)
325 AudioData *s = (AudioData *)s1->priv_data;
333 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
335 AudioData *s = (AudioData *)s1->priv_data;
339 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
342 st = av_new_stream(s1, 0);
346 s->sample_rate = ap->sample_rate;
347 s->channels = ap->channels;
349 ret = audio_open(s, 0);
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;
363 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
365 AudioData *s = (AudioData *)s1->priv_data;
368 if (av_new_packet(pkt, s->frame_size) < 0)
371 ret = read(s->fd, pkt->data, pkt->size);
374 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
379 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
385 if (s->flip_left && s->channels == 2) {
387 short *p = (short *) pkt->data;
389 for (i = 0; i < ret; i += 4) {
397 static int audio_read_close(AVFormatContext *s1)
399 AudioData *s = (AudioData *)s1->priv_data;
405 AVInputFormat audio_in_format = {
407 "audio grab and output",
417 AVOutputFormat audio_out_format = {
419 "audio grab and output",
423 #ifdef WORDS_BIGENDIAN
439 main_thid = find_thread(NULL);
440 av_register_input_format(&audio_in_format);
441 av_register_output_format(&audio_out_format);