Mercurial > libavformat.hg
annotate beosaudio.cpp @ 108:c1c8a0777bdb libavformat
Add latency check, the Media Kit shoul repport it, but this seems broken.
| author | mmu_man |
|---|---|
| date | Fri, 18 Apr 2003 16:22:12 +0000 |
| parents | 265d01c2248f |
| children | 41d4f3a86c98 |
| rev | line source |
|---|---|
| 0 | 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 | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
34 #ifdef HAVE_BSOUNDRECORDER |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
35 #include <SoundRecorder.h> |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
36 #endif |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
37 |
| 0 | 38 /* enable performance checks */ |
| 39 //#define PERF_CHECK | |
| 40 | |
|
108
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
41 /* enable Media Kit latency checks */ |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
42 //#define LATENCY_CHECK |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
43 |
| 0 | 44 #define AUDIO_BLOCK_SIZE 4096 |
| 45 //#define AUDIO_BLOCK_SIZE 2048 | |
| 46 #define AUDIO_BLOCK_COUNT 8 | |
| 47 | |
| 48 #define AUDIO_BUFFER_SIZE (AUDIO_BLOCK_SIZE*AUDIO_BLOCK_COUNT) | |
| 49 | |
| 50 typedef struct { | |
| 95 | 51 int fd; // UNUSED |
| 0 | 52 int sample_rate; |
| 53 int channels; | |
| 54 int frame_size; /* in bytes ! */ | |
| 55 CodecID codec_id; | |
|
66
ad9bcf041e8e
Looks like this one was forgotten in the INT -> int_t move
mmu_man
parents:
30
diff
changeset
|
56 uint8_t buffer[AUDIO_BUFFER_SIZE]; |
| 0 | 57 int buffer_ptr; |
| 58 /* ring buffer */ | |
| 59 sem_id input_sem; | |
| 60 int input_index; | |
| 61 sem_id output_sem; | |
| 62 int output_index; | |
| 63 int queued; | |
| 64 BSoundPlayer *player; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
65 #ifdef HAVE_BSOUNDRECORDER |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
66 BSoundRecorder *recorder; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
67 #endif |
| 0 | 68 int has_quit; /* signal callbacks not to wait */ |
| 69 volatile bigtime_t starve_time; | |
| 70 } AudioData; | |
| 71 | |
| 72 static thread_id main_thid; | |
| 73 static thread_id bapp_thid; | |
| 74 static int own_BApp_created = 0; | |
| 75 static int refcount = 0; | |
| 76 | |
| 77 /* create the BApplication and Run() it */ | |
| 78 static int32 bapp_thread(void *arg) | |
| 79 { | |
| 80 new BApplication("application/x-vnd.ffmpeg"); | |
| 81 own_BApp_created = 1; | |
| 82 be_app->Run(); | |
| 83 /* kill the process group */ | |
| 84 // kill(0, SIGINT); | |
| 85 // kill(main_thid, SIGHUP); | |
| 86 return B_OK; | |
| 87 } | |
| 88 | |
| 89 /* create the BApplication only if needed */ | |
| 90 static void create_bapp_if_needed(void) | |
| 91 { | |
| 92 if (refcount++ == 0) { | |
| 93 /* needed by libmedia */ | |
| 94 if (be_app == NULL) { | |
| 95 bapp_thid = spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL); | |
| 96 resume_thread(bapp_thid); | |
| 97 while (!own_BApp_created) | |
| 98 snooze(50000); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 static void destroy_bapp_if_needed(void) | |
| 104 { | |
| 105 if (--refcount == 0 && own_BApp_created) { | |
| 106 be_app->Lock(); | |
| 107 be_app->Quit(); | |
| 108 be_app = NULL; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 /* called back by BSoundPlayer */ | |
| 113 static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format) | |
| 114 { | |
| 115 AudioData *s; | |
| 116 size_t len, amount; | |
| 117 unsigned char *buf = (unsigned char *)buffer; | |
| 118 | |
| 119 s = (AudioData *)cookie; | |
| 120 if (s->has_quit) | |
| 121 return; | |
| 122 while (bufferSize > 0) { | |
| 123 #ifdef PERF_CHECK | |
| 124 bigtime_t t; | |
| 125 t = system_time(); | |
| 126 #endif | |
| 127 len = MIN(AUDIO_BLOCK_SIZE, bufferSize); | |
| 128 if (acquire_sem_etc(s->output_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) { | |
| 129 s->has_quit = 1; | |
| 130 s->player->SetHasData(false); | |
| 131 return; | |
| 132 } | |
| 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; | |
| 141 } | |
| 142 release_sem_etc(s->input_sem, len, 0); | |
| 143 #ifdef PERF_CHECK | |
| 144 t = system_time() - t; | |
| 145 s->starve_time = MAX(s->starve_time, t); | |
| 146 #endif | |
| 147 buf += len; | |
| 148 bufferSize -= len; | |
| 149 } | |
| 150 } | |
| 151 | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
152 #ifdef HAVE_BSOUNDRECORDER |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
153 /* called back by BSoundRecorder */ |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
154 static void audiorecord_callback(void *cookie, bigtime_t timestamp, void *buffer, size_t bufferSize, const media_multi_audio_format &format) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
155 { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
156 AudioData *s; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
157 size_t len, amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
158 unsigned char *buf = (unsigned char *)buffer; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
159 |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
160 s = (AudioData *)cookie; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
161 if (s->has_quit) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
162 return; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
163 |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
164 while (bufferSize > 0) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
165 len = MIN(bufferSize, AUDIO_BLOCK_SIZE); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
166 //printf("acquire_sem(input, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
167 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
168 s->has_quit = 1; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
169 return; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
170 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
171 amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index)); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
172 memcpy(&s->buffer[s->input_index], buf, amount); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
173 s->input_index += amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
174 if (s->input_index >= AUDIO_BUFFER_SIZE) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
175 s->input_index %= AUDIO_BUFFER_SIZE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
176 memcpy(&s->buffer[s->input_index], buf + amount, len - amount); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
177 s->input_index += len - amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
178 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
179 release_sem_etc(s->output_sem, len, 0); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
180 //printf("release_sem(output, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
181 buf += len; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
182 bufferSize -= len; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
183 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
184 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
185 #endif |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
186 |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
187 static int audio_open(AudioData *s, int is_output, const char *audio_device) |
| 0 | 188 { |
| 189 int p[2]; | |
| 190 int ret; | |
| 191 media_raw_audio_format format; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
192 media_multi_audio_format iformat; |
| 0 | 193 |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
194 #ifndef HAVE_BSOUNDRECORDER |
| 0 | 195 if (!is_output) |
| 196 return -EIO; /* not for now */ | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
197 #endif |
| 0 | 198 s->input_sem = create_sem(AUDIO_BUFFER_SIZE, "ffmpeg_ringbuffer_input"); |
| 199 // s->input_sem = create_sem(AUDIO_BLOCK_SIZE, "ffmpeg_ringbuffer_input"); | |
| 200 if (s->input_sem < B_OK) | |
| 201 return -EIO; | |
| 202 s->output_sem = create_sem(0, "ffmpeg_ringbuffer_output"); | |
| 203 if (s->output_sem < B_OK) { | |
| 204 delete_sem(s->input_sem); | |
| 205 return -EIO; | |
| 206 } | |
| 207 s->input_index = 0; | |
| 208 s->output_index = 0; | |
| 209 s->queued = 0; | |
| 210 create_bapp_if_needed(); | |
| 211 s->frame_size = AUDIO_BLOCK_SIZE; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
212 /* bump up the priority (avoid realtime though) */ |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
213 set_thread_priority(find_thread(NULL), B_DISPLAY_PRIORITY+1); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
214 #ifdef HAVE_BSOUNDRECORDER |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
215 if (!is_output) { |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
216 bool wait_for_input = false; |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
217 if (audio_device && !strcmp(audio_device, "wait:")) |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
218 wait_for_input = true; |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
219 s->recorder = new BSoundRecorder(&iformat, wait_for_input, "ffmpeg input", audiorecord_callback); |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
220 if (wait_for_input && (s->recorder->InitCheck() == B_OK)) { |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
221 s->recorder->WaitForIncomingConnection(&iformat); |
|
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
222 } |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
223 if (s->recorder->InitCheck() != B_OK || iformat.format != media_raw_audio_format::B_AUDIO_SHORT) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
224 delete s->recorder; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
225 s->recorder = NULL; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
226 if (s->input_sem) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
227 delete_sem(s->input_sem); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
228 if (s->output_sem) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
229 delete_sem(s->output_sem); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
230 return -EIO; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
231 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
232 s->codec_id = (iformat.byte_order == B_MEDIA_LITTLE_ENDIAN)?CODEC_ID_PCM_S16LE:CODEC_ID_PCM_S16BE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
233 s->channels = iformat.channel_count; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
234 s->sample_rate = (int)iformat.frame_rate; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
235 s->frame_size = iformat.buffer_size; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
236 s->recorder->SetCookie(s); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
237 s->recorder->SetVolume(1.0); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
238 s->recorder->Start(); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
239 return 0; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
240 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
241 #endif |
| 0 | 242 format = media_raw_audio_format::wildcard; |
| 243 format.format = media_raw_audio_format::B_AUDIO_SHORT; | |
| 244 format.byte_order = B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN; | |
| 245 format.channel_count = s->channels; | |
| 246 format.buffer_size = s->frame_size; | |
| 247 format.frame_rate = s->sample_rate; | |
| 248 s->player = new BSoundPlayer(&format, "ffmpeg output", audioplay_callback); | |
| 249 if (s->player->InitCheck() != B_OK) { | |
| 250 delete s->player; | |
| 251 s->player = NULL; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
252 if (s->input_sem) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
253 delete_sem(s->input_sem); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
254 if (s->output_sem) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
255 delete_sem(s->output_sem); |
| 0 | 256 return -EIO; |
| 257 } | |
| 258 s->player->SetCookie(s); | |
| 259 s->player->SetVolume(1.0); | |
| 260 s->player->Start(); | |
| 261 s->player->SetHasData(true); | |
| 262 return 0; | |
| 263 } | |
| 264 | |
| 265 static int audio_close(AudioData *s) | |
| 266 { | |
| 267 if (s->input_sem) | |
| 268 delete_sem(s->input_sem); | |
| 269 if (s->output_sem) | |
| 270 delete_sem(s->output_sem); | |
| 271 s->has_quit = 1; | |
| 272 if (s->player) { | |
| 273 s->player->Stop(); | |
| 274 } | |
| 275 if (s->player) | |
| 276 delete s->player; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
277 #ifdef HAVE_BSOUNDRECORDER |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
278 if (s->recorder) |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
279 delete s->recorder; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
280 #endif |
| 0 | 281 destroy_bapp_if_needed(); |
| 282 return 0; | |
| 283 } | |
| 284 | |
| 285 /* sound output support */ | |
| 286 static int audio_write_header(AVFormatContext *s1) | |
| 287 { | |
| 288 AudioData *s = (AudioData *)s1->priv_data; | |
| 289 AVStream *st; | |
| 290 int ret; | |
| 291 | |
| 292 st = s1->streams[0]; | |
| 293 s->sample_rate = st->codec.sample_rate; | |
| 294 s->channels = st->codec.channels; | |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
295 ret = audio_open(s, 1, NULL); |
| 0 | 296 if (ret < 0) |
| 297 return -EIO; | |
| 298 return 0; | |
| 299 } | |
| 300 | |
| 301 static int audio_write_packet(AVFormatContext *s1, int stream_index, | |
|
66
ad9bcf041e8e
Looks like this one was forgotten in the INT -> int_t move
mmu_man
parents:
30
diff
changeset
|
302 uint8_t *buf, int size, int force_pts) |
| 0 | 303 { |
| 304 AudioData *s = (AudioData *)s1->priv_data; | |
| 305 int len, ret; | |
|
108
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
306 #ifdef LATENCY_CHECK |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
307 bigtime_t lat1, lat2; |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
308 lat1 = s->player->Latency(); |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
309 #endif |
| 0 | 310 #ifdef PERF_CHECK |
| 311 bigtime_t t = s->starve_time; | |
| 312 s->starve_time = 0; | |
| 313 printf("starve_time: %lld \n", t); | |
| 314 #endif | |
| 315 while (size > 0) { | |
| 316 int amount; | |
| 317 len = MIN(size, AUDIO_BLOCK_SIZE); | |
| 318 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) | |
| 319 return -EIO; | |
| 320 amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index)); | |
| 321 memcpy(&s->buffer[s->input_index], buf, amount); | |
| 322 s->input_index += amount; | |
| 323 if (s->input_index >= AUDIO_BUFFER_SIZE) { | |
| 324 s->input_index %= AUDIO_BUFFER_SIZE; | |
| 325 memcpy(&s->buffer[s->input_index], buf + amount, len - amount); | |
| 326 s->input_index += len - amount; | |
| 327 } | |
| 328 release_sem_etc(s->output_sem, len, 0); | |
| 329 buf += len; | |
| 330 size -= len; | |
| 331 } | |
|
108
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
332 #ifdef LATENCY_CHECK |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
333 lat2 = s->player->Latency(); |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
334 printf("#### BSoundPlayer::Latency(): before= %lld, after= %lld\n", lat1, lat2); |
|
c1c8a0777bdb
Add latency check, the Media Kit shoul repport it, but this seems broken.
mmu_man
parents:
97
diff
changeset
|
335 #endif |
| 0 | 336 return 0; |
| 337 } | |
| 338 | |
| 339 static int audio_write_trailer(AVFormatContext *s1) | |
| 340 { | |
| 341 AudioData *s = (AudioData *)s1->priv_data; | |
| 342 | |
| 343 audio_close(s); | |
| 344 return 0; | |
| 345 } | |
| 346 | |
| 347 /* grab support */ | |
| 348 | |
| 349 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap) | |
| 350 { | |
| 351 AudioData *s = (AudioData *)s1->priv_data; | |
| 352 AVStream *st; | |
| 353 int ret; | |
| 354 | |
| 355 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0) | |
| 356 return -1; | |
| 357 | |
| 358 st = av_new_stream(s1, 0); | |
| 359 if (!st) { | |
| 360 return -ENOMEM; | |
| 361 } | |
| 362 s->sample_rate = ap->sample_rate; | |
| 363 s->channels = ap->channels; | |
| 364 | |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
365 ret = audio_open(s, 0, ap->device); |
| 0 | 366 if (ret < 0) { |
| 367 av_free(st); | |
| 368 return -EIO; | |
| 369 } | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
370 /* take real parameters */ |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
371 st->codec.codec_type = CODEC_TYPE_AUDIO; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
372 st->codec.codec_id = s->codec_id; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
373 st->codec.sample_rate = s->sample_rate; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
374 st->codec.channels = s->channels; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
375 return 0; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
376 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */ |
| 0 | 377 } |
| 378 | |
| 379 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
| 380 { | |
| 381 AudioData *s = (AudioData *)s1->priv_data; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
382 int size; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
383 size_t len, amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
384 unsigned char *buf; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
385 status_t err; |
| 0 | 386 |
| 387 if (av_new_packet(pkt, s->frame_size) < 0) | |
| 388 return -EIO; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
389 buf = (unsigned char *)pkt->data; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
390 size = pkt->size; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
391 while (size > 0) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
392 len = MIN(AUDIO_BLOCK_SIZE, size); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
393 //printf("acquire_sem(output, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
394 while ((err=acquire_sem_etc(s->output_sem, len, B_CAN_INTERRUPT, 0LL)) == B_INTERRUPTED); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
395 if (err < B_OK) { |
| 0 | 396 av_free_packet(pkt); |
| 397 return -EIO; | |
| 398 } | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
399 amount = MIN(len, (AUDIO_BUFFER_SIZE - s->output_index)); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
400 memcpy(buf, &s->buffer[s->output_index], amount); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
401 s->output_index += amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
402 if (s->output_index >= AUDIO_BUFFER_SIZE) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
403 s->output_index %= AUDIO_BUFFER_SIZE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
404 memcpy(buf + amount, &s->buffer[s->output_index], len - amount); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
405 s->output_index += len-amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
406 s->output_index %= AUDIO_BUFFER_SIZE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
407 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
408 release_sem_etc(s->input_sem, len, 0); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
409 //printf("release_sem(input, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
410 buf += len; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
411 size -= len; |
| 0 | 412 } |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
413 //XXX: add pts info |
| 0 | 414 return 0; |
| 415 } | |
| 416 | |
| 417 static int audio_read_close(AVFormatContext *s1) | |
| 418 { | |
| 419 AudioData *s = (AudioData *)s1->priv_data; | |
| 420 | |
| 421 audio_close(s); | |
| 422 return 0; | |
| 423 } | |
| 424 | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
425 static AVInputFormat audio_in_format = { |
| 0 | 426 "audio_device", |
| 427 "audio grab and output", | |
| 428 sizeof(AudioData), | |
| 429 NULL, | |
| 430 audio_read_header, | |
| 431 audio_read_packet, | |
| 432 audio_read_close, | |
| 433 NULL, | |
| 434 AVFMT_NOFILE, | |
| 435 }; | |
| 436 | |
| 437 AVOutputFormat audio_out_format = { | |
| 438 "audio_device", | |
| 439 "audio grab and output", | |
| 440 "", | |
| 441 "", | |
| 442 sizeof(AudioData), | |
| 443 #ifdef WORDS_BIGENDIAN | |
| 444 CODEC_ID_PCM_S16BE, | |
| 445 #else | |
| 446 CODEC_ID_PCM_S16LE, | |
| 447 #endif | |
| 448 CODEC_ID_NONE, | |
| 449 audio_write_header, | |
| 450 audio_write_packet, | |
| 451 audio_write_trailer, | |
| 452 AVFMT_NOFILE, | |
| 453 }; | |
| 454 | |
| 455 extern "C" { | |
| 456 | |
| 457 int audio_init(void) | |
| 458 { | |
| 459 main_thid = find_thread(NULL); | |
| 460 av_register_input_format(&audio_in_format); | |
| 461 av_register_output_format(&audio_out_format); | |
| 462 return 0; | |
| 463 } | |
| 464 | |
| 465 } // "C" | |
| 466 |
