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