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 #ifdef HAVE_BSOUNDRECORDER
35 #include <SoundRecorder.h>
38 /* enable performance checks */
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)
51 int frame_size; /* in bytes ! */
53 uint8_t buffer[AUDIO_BUFFER_SIZE];
62 #ifdef HAVE_BSOUNDRECORDER
63 BSoundRecorder *recorder;
65 int has_quit; /* signal callbacks not to wait */
66 volatile bigtime_t starve_time;
69 static thread_id main_thid;
70 static thread_id bapp_thid;
71 static int own_BApp_created = 0;
72 static int refcount = 0;
74 /* create the BApplication and Run() it */
75 static int32 bapp_thread(void *arg)
77 new BApplication("application/x-vnd.ffmpeg");
80 /* kill the process group */
82 // kill(main_thid, SIGHUP);
86 /* create the BApplication only if needed */
87 static void create_bapp_if_needed(void)
89 if (refcount++ == 0) {
90 /* needed by libmedia */
92 bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL);
93 resume_thread(bapp_thid);
94 while (!own_BApp_created)
100 static void destroy_bapp_if_needed(void)
102 if (--refcount == 0 && own_BApp_created) {
109 /* called back by BSoundPlayer */
110 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
114 unsigned char *buf = (unsigned char *)buffer;
116 s = (AudioData *)cookie;
119 while (bufferSize > 0) {
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 t = system_time() - t;
142 s->starve_time = MAX(s->starve_time, t);
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)
155 unsigned char *buf = (unsigned char *)buffer;
157 s = (AudioData *)cookie;
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) {
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;
176 release_sem_etc(s->output_sem, len, 0);
177 //printf("release_sem(output, %d)\n", len);
184 static int audio_open(AudioData *s, int is_output)
188 media_raw_audio_format format;
189 media_multi_audio_format iformat;
191 #ifndef HAVE_BSOUNDRECORDER
193 return -EIO; /* not for now */
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)
199 s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output");
200 if (s->output_sem < B_OK) {
201 delete_sem(s->input_sem);
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
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) {
218 delete_sem(s->input_sem);
220 delete_sem(s->output_sem);
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();
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) {
244 delete_sem(s->input_sem);
246 delete_sem(s->output_sem);
249 s->player->SetCookie(s);
250 s->player->SetVolume(1.0);
252 s->player->SetHasData(true);
256 static int audio_close(AudioData *s)
259 delete_sem(s->input_sem);
261 delete_sem(s->output_sem);
268 #ifdef HAVE_BSOUNDRECORDER
272 destroy_bapp_if_needed();
276 /* sound output support */
277 static int audio_write_header(AVFormatContext *s1)
279 AudioData *s = (AudioData *)s1->priv_data;
284 s->sample_rate = st->codec.sample_rate;
285 s->channels = st->codec.channels;
286 ret = audio_open(s, 1);
292 static int audio_write_packet(AVFormatContext *s1, int stream_index,
293 uint8_t *buf, int size, int force_pts)
295 AudioData *s = (AudioData *)s1->priv_data;
298 bigtime_t t = s->starve_time;
300 printf("starve_time: %lld \n", t);
304 len = MIN(size, AUDIO_BLOCK_SIZE);
305 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK)
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;
315 release_sem_etc(s->output_sem, len, 0);
322 static int audio_write_trailer(AVFormatContext *s1)
324 AudioData *s = (AudioData *)s1->priv_data;
332 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
334 AudioData *s = (AudioData *)s1->priv_data;
338 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
341 st = av_new_stream(s1, 0);
345 s->sample_rate = ap->sample_rate;
346 s->channels = ap->channels;
348 ret = audio_open(s, 0);
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;
359 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
362 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
364 AudioData *s = (AudioData *)s1->priv_data;
370 if (av_new_packet(pkt, s->frame_size) < 0)
372 buf = (unsigned char *)pkt->data;
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);
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;
391 release_sem_etc(s->input_sem, len, 0);
392 //printf("release_sem(input, %d)\n", len);
400 static int audio_read_close(AVFormatContext *s1)
402 AudioData *s = (AudioData *)s1->priv_data;
408 static 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);