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