2 * BeOS audio play interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <Application.h>
30 #include <SoundPlayer.h>
33 #include "libavformat/avformat.h"
36 #ifdef HAVE_BSOUNDRECORDER
37 #include <SoundRecorder.h>
38 using namespace BPrivate::Media::Experimental
;
41 /* enable performance checks */
44 /* enable Media Kit latency checks */
45 //#define LATENCY_CHECK
47 #define AUDIO_BLOCK_SIZE 4096
48 #define AUDIO_BLOCK_COUNT 8
50 #define AUDIO_BUFFER_SIZE (AUDIO_BLOCK_SIZE*AUDIO_BLOCK_COUNT)
56 int frame_size
; /* in bytes ! */
58 uint8_t buffer
[AUDIO_BUFFER_SIZE
];
66 #ifdef HAVE_BSOUNDRECORDER
67 BSoundRecorder
*recorder
;
69 int has_quit
; /* signal callbacks not to wait */
70 volatile bigtime_t starve_time
;
73 static thread_id main_thid
;
74 static thread_id bapp_thid
;
75 static int own_BApp_created
= 0;
76 static int refcount
= 0;
78 /* create the BApplication and Run() it */
79 static int32
bapp_thread(void *arg
)
81 new BApplication("application/x-vnd.ffmpeg");
84 /* kill the process group */
86 // kill(main_thid, SIGHUP);
90 /* create the BApplication only if needed */
91 static void create_bapp_if_needed(void)
93 if (refcount
++ == 0) {
94 /* needed by libmedia */
96 bapp_thid
= spawn_thread(bapp_thread
, "ffmpeg BApplication", B_NORMAL_PRIORITY
, NULL
);
97 resume_thread(bapp_thid
);
98 while (!own_BApp_created
)
104 static void destroy_bapp_if_needed(void)
106 if (--refcount
== 0 && own_BApp_created
) {
113 /* called back by BSoundPlayer */
114 static void audioplay_callback(void *cookie
, void *buffer
, size_t bufferSize
, const media_raw_audio_format
&format
)
118 unsigned char *buf
= (unsigned char *)buffer
;
120 s
= (AudioData
*)cookie
;
123 while (bufferSize
> 0) {
128 len
= MIN(AUDIO_BLOCK_SIZE
, bufferSize
);
129 if (acquire_sem_etc(s
->output_sem
, len
, B_CAN_INTERRUPT
, 0LL) < B_OK
) {
131 s
->player
->SetHasData(false);
134 amount
= MIN(len
, (AUDIO_BUFFER_SIZE
- s
->output_index
));
135 memcpy(buf
, &s
->buffer
[s
->output_index
], amount
);
136 s
->output_index
+= amount
;
137 if (s
->output_index
>= AUDIO_BUFFER_SIZE
) {
138 s
->output_index
%= AUDIO_BUFFER_SIZE
;
139 memcpy(buf
+ amount
, &s
->buffer
[s
->output_index
], len
- amount
);
140 s
->output_index
+= len
-amount
;
141 s
->output_index
%= AUDIO_BUFFER_SIZE
;
143 release_sem_etc(s
->input_sem
, len
, 0);
145 t
= system_time() - t
;
146 s
->starve_time
= MAX(s
->starve_time
, t
);
153 #ifdef HAVE_BSOUNDRECORDER
154 /* called back by BSoundRecorder */
155 static void audiorecord_callback(void *cookie
, bigtime_t timestamp
, void *buffer
, size_t bufferSize
, const media_multi_audio_format
&format
)
159 unsigned char *buf
= (unsigned char *)buffer
;
161 s
= (AudioData
*)cookie
;
165 while (bufferSize
> 0) {
166 len
= MIN(bufferSize
, AUDIO_BLOCK_SIZE
);
167 //printf("acquire_sem(input, %d)\n", len);
168 if (acquire_sem_etc(s
->input_sem
, len
, B_CAN_INTERRUPT
, 0LL) < B_OK
) {
172 amount
= MIN(len
, (AUDIO_BUFFER_SIZE
- s
->input_index
));
173 memcpy(&s
->buffer
[s
->input_index
], buf
, amount
);
174 s
->input_index
+= amount
;
175 if (s
->input_index
>= AUDIO_BUFFER_SIZE
) {
176 s
->input_index
%= AUDIO_BUFFER_SIZE
;
177 memcpy(&s
->buffer
[s
->input_index
], buf
+ amount
, len
- amount
);
178 s
->input_index
+= len
- amount
;
180 release_sem_etc(s
->output_sem
, len
, 0);
181 //printf("release_sem(output, %d)\n", len);
188 static int audio_open(AudioData
*s
, int is_output
, const char *audio_device
)
192 media_raw_audio_format format
;
193 media_multi_audio_format iformat
;
195 #ifndef HAVE_BSOUNDRECORDER
197 return AVERROR(EIO
); /* not for now */
199 s
->input_sem
= create_sem(AUDIO_BUFFER_SIZE
, "ffmpeg_ringbuffer_input");
200 if (s
->input_sem
< B_OK
)
202 s
->output_sem
= create_sem(0, "ffmpeg_ringbuffer_output");
203 if (s
->output_sem
< B_OK
) {
204 delete_sem(s
->input_sem
);
209 create_bapp_if_needed();
210 s
->frame_size
= AUDIO_BLOCK_SIZE
;
211 /* bump up the priority (avoid realtime though) */
212 set_thread_priority(find_thread(NULL
), B_DISPLAY_PRIORITY
+1);
213 #ifdef HAVE_BSOUNDRECORDER
215 bool wait_for_input
= false;
216 if (audio_device
&& !strcmp(audio_device
, "wait:"))
217 wait_for_input
= true;
218 s
->recorder
= new BSoundRecorder(&iformat
, wait_for_input
, "ffmpeg input", audiorecord_callback
);
219 if (wait_for_input
&& (s
->recorder
->InitCheck() == B_OK
)) {
220 s
->recorder
->WaitForIncomingConnection(&iformat
);
222 if (s
->recorder
->InitCheck() != B_OK
|| iformat
.format
!= media_raw_audio_format::B_AUDIO_SHORT
) {
226 delete_sem(s
->input_sem
);
228 delete_sem(s
->output_sem
);
231 s
->codec_id
= (iformat
.byte_order
== B_MEDIA_LITTLE_ENDIAN
)?CODEC_ID_PCM_S16LE
:CODEC_ID_PCM_S16BE
;
232 s
->channels
= iformat
.channel_count
;
233 s
->sample_rate
= (int)iformat
.frame_rate
;
234 s
->frame_size
= iformat
.buffer_size
;
235 s
->recorder
->SetCookie(s
);
236 s
->recorder
->SetVolume(1.0);
237 s
->recorder
->Start();
241 format
= media_raw_audio_format::wildcard
;
242 format
.format
= media_raw_audio_format::B_AUDIO_SHORT
;
243 format
.byte_order
= B_HOST_IS_LENDIAN
? B_MEDIA_LITTLE_ENDIAN
: B_MEDIA_BIG_ENDIAN
;
244 format
.channel_count
= s
->channels
;
245 format
.buffer_size
= s
->frame_size
;
246 format
.frame_rate
= s
->sample_rate
;
247 s
->player
= new BSoundPlayer(&format
, "ffmpeg output", audioplay_callback
);
248 if (s
->player
->InitCheck() != B_OK
) {
252 delete_sem(s
->input_sem
);
254 delete_sem(s
->output_sem
);
257 s
->player
->SetCookie(s
);
258 s
->player
->SetVolume(1.0);
260 s
->player
->SetHasData(true);
264 static int audio_close(AudioData
*s
)
267 delete_sem(s
->input_sem
);
269 delete_sem(s
->output_sem
);
276 #ifdef HAVE_BSOUNDRECORDER
280 destroy_bapp_if_needed();
284 /* sound output support */
285 static int audio_write_header(AVFormatContext
*s1
)
287 AudioData
*s
= (AudioData
*)s1
->priv_data
;
292 s
->sample_rate
= st
->codec
->sample_rate
;
293 s
->channels
= st
->codec
->channels
;
294 ret
= audio_open(s
, 1, NULL
);
300 static int audio_write_packet(AVFormatContext
*s1
, int stream_index
,
301 const uint8_t *buf
, int size
, int64_t force_pts
)
303 AudioData
*s
= (AudioData
*)s1
->priv_data
;
306 bigtime_t lat1
, lat2
;
307 lat1
= s
->player
->Latency();
310 bigtime_t t
= s
->starve_time
;
312 printf("starve_time: %lld \n", t
);
316 len
= MIN(size
, AUDIO_BLOCK_SIZE
);
317 if (acquire_sem_etc(s
->input_sem
, len
, B_CAN_INTERRUPT
, 0LL) < B_OK
)
319 amount
= MIN(len
, (AUDIO_BUFFER_SIZE
- s
->input_index
));
320 memcpy(&s
->buffer
[s
->input_index
], buf
, amount
);
321 s
->input_index
+= amount
;
322 if (s
->input_index
>= AUDIO_BUFFER_SIZE
) {
323 s
->input_index
%= AUDIO_BUFFER_SIZE
;
324 memcpy(&s
->buffer
[s
->input_index
], buf
+ amount
, len
- amount
);
325 s
->input_index
+= len
- amount
;
327 release_sem_etc(s
->output_sem
, len
, 0);
332 lat2
= s
->player
->Latency();
333 printf("#### BSoundPlayer::Latency(): before= %lld, after= %lld\n", lat1
, lat2
);
338 static int audio_write_trailer(AVFormatContext
*s1
)
340 AudioData
*s
= (AudioData
*)s1
->priv_data
;
348 static int audio_read_header(AVFormatContext
*s1
, AVFormatParameters
*ap
)
350 AudioData
*s
= (AudioData
*)s1
->priv_data
;
354 if (!ap
|| ap
->sample_rate
<= 0 || ap
->channels
<= 0)
357 st
= av_new_stream(s1
, 0);
359 return AVERROR(ENOMEM
);
361 s
->sample_rate
= ap
->sample_rate
;
362 s
->channels
= ap
->channels
;
364 ret
= audio_open(s
, 0, s1
->filename
);
369 /* take real parameters */
370 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
371 st
->codec
->codec_id
= s
->codec_id
;
372 st
->codec
->sample_rate
= s
->sample_rate
;
373 st
->codec
->channels
= s
->channels
;
375 av_set_pts_info(s1
, 48, 1, 1000000); /* 48 bits pts in us */
378 static int audio_read_packet(AVFormatContext
*s1
, AVPacket
*pkt
)
380 AudioData
*s
= (AudioData
*)s1
->priv_data
;
386 if (av_new_packet(pkt
, s
->frame_size
) < 0)
388 buf
= (unsigned char *)pkt
->data
;
391 len
= MIN(AUDIO_BLOCK_SIZE
, size
);
392 //printf("acquire_sem(output, %d)\n", len);
393 while ((err
=acquire_sem_etc(s
->output_sem
, len
, B_CAN_INTERRUPT
, 0LL)) == B_INTERRUPTED
);
398 amount
= MIN(len
, (AUDIO_BUFFER_SIZE
- s
->output_index
));
399 memcpy(buf
, &s
->buffer
[s
->output_index
], amount
);
400 s
->output_index
+= amount
;
401 if (s
->output_index
>= AUDIO_BUFFER_SIZE
) {
402 s
->output_index
%= AUDIO_BUFFER_SIZE
;
403 memcpy(buf
+ amount
, &s
->buffer
[s
->output_index
], len
- amount
);
404 s
->output_index
+= len
-amount
;
405 s
->output_index
%= AUDIO_BUFFER_SIZE
;
407 release_sem_etc(s
->input_sem
, len
, 0);
408 //printf("release_sem(input, %d)\n", len);
416 static int audio_read_close(AVFormatContext
*s1
)
418 AudioData
*s
= (AudioData
*)s1
->priv_data
;
424 static AVInputFormat audio_beos_demuxer
= {
426 NULL_IF_CONFIG_SMALL("audio grab and output"),
436 AVOutputFormat audio_beos_muxer
= {
438 NULL_IF_CONFIG_SMALL("audio grab and output"),
442 #ifdef WORDS_BIGENDIAN
458 main_thid
= find_thread(NULL
);
459 av_register_input_format(&audio_beos_demuxer
);
460 av_register_output_format(&audio_beos_muxer
);