Mercurial > libavformat.hg
annotate beosaudio.cpp @ 97:265d01c2248f libavformat
the media node now won't connect itself to the default audio input with -ad wait:
(allows redirecting another node to it)
| author | mmu_man |
|---|---|
| date | Thu, 27 Mar 2003 15:08:16 +0000 |
| parents | 2d3083edb99f |
| children | c1c8a0777bdb |
| 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 |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
184 static int audio_open(AudioData *s, int is_output, const char *audio_device) |
| 0 | 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) { |
|
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]; | |
| 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, | |
|
66
ad9bcf041e8e
Looks like this one was forgotten in the INT -> int_t move
mmu_man
parents:
30
diff
changeset
|
299 uint8_t *buf, int size, int force_pts) |
| 0 | 300 { |
| 301 AudioData *s = (AudioData *)s1->priv_data; | |
| 302 int len, ret; | |
| 303 #ifdef PERF_CHECK | |
| 304 bigtime_t t = s->starve_time; | |
| 305 s->starve_time = 0; | |
| 306 printf("starve_time: %lld \n", t); | |
| 307 #endif | |
| 308 while (size > 0) { | |
| 309 int amount; | |
| 310 len = MIN(size, AUDIO_BLOCK_SIZE); | |
| 311 if (acquire_sem_etc(s->input_sem, len, B_CAN_INTERRUPT, 0LL) < B_OK) | |
| 312 return -EIO; | |
| 313 amount = MIN(len, (AUDIO_BUFFER_SIZE - s->input_index)); | |
| 314 memcpy(&s->buffer[s->input_index], buf, amount); | |
| 315 s->input_index += amount; | |
| 316 if (s->input_index >= AUDIO_BUFFER_SIZE) { | |
| 317 s->input_index %= AUDIO_BUFFER_SIZE; | |
| 318 memcpy(&s->buffer[s->input_index], buf + amount, len - amount); | |
| 319 s->input_index += len - amount; | |
| 320 } | |
| 321 release_sem_etc(s->output_sem, len, 0); | |
| 322 buf += len; | |
| 323 size -= len; | |
| 324 } | |
| 325 return 0; | |
| 326 } | |
| 327 | |
| 328 static int audio_write_trailer(AVFormatContext *s1) | |
| 329 { | |
| 330 AudioData *s = (AudioData *)s1->priv_data; | |
| 331 | |
| 332 audio_close(s); | |
| 333 return 0; | |
| 334 } | |
| 335 | |
| 336 /* grab support */ | |
| 337 | |
| 338 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap) | |
| 339 { | |
| 340 AudioData *s = (AudioData *)s1->priv_data; | |
| 341 AVStream *st; | |
| 342 int ret; | |
| 343 | |
| 344 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0) | |
| 345 return -1; | |
| 346 | |
| 347 st = av_new_stream(s1, 0); | |
| 348 if (!st) { | |
| 349 return -ENOMEM; | |
| 350 } | |
| 351 s->sample_rate = ap->sample_rate; | |
| 352 s->channels = ap->channels; | |
| 353 | |
|
97
265d01c2248f
the media node now won't connect itself to the default audio input with -ad wait:
mmu_man
parents:
96
diff
changeset
|
354 ret = audio_open(s, 0, ap->device); |
| 0 | 355 if (ret < 0) { |
| 356 av_free(st); | |
| 357 return -EIO; | |
| 358 } | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
359 /* take real parameters */ |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
360 st->codec.codec_type = CODEC_TYPE_AUDIO; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
361 st->codec.codec_id = s->codec_id; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
362 st->codec.sample_rate = s->sample_rate; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
363 st->codec.channels = s->channels; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
364 return 0; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
365 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */ |
| 0 | 366 } |
| 367 | |
| 368 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
| 369 { | |
| 370 AudioData *s = (AudioData *)s1->priv_data; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
371 int size; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
372 size_t len, amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
373 unsigned char *buf; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
374 status_t err; |
| 0 | 375 |
| 376 if (av_new_packet(pkt, s->frame_size) < 0) | |
| 377 return -EIO; | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
378 buf = (unsigned char *)pkt->data; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
379 size = pkt->size; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
380 while (size > 0) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
381 len = MIN(AUDIO_BLOCK_SIZE, size); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
382 //printf("acquire_sem(output, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
383 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
|
384 if (err < B_OK) { |
| 0 | 385 av_free_packet(pkt); |
| 386 return -EIO; | |
| 387 } | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
388 amount = MIN(len, (AUDIO_BUFFER_SIZE - s->output_index)); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
389 memcpy(buf, &s->buffer[s->output_index], amount); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
390 s->output_index += amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
391 if (s->output_index >= AUDIO_BUFFER_SIZE) { |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
392 s->output_index %= AUDIO_BUFFER_SIZE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
393 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
|
394 s->output_index += len-amount; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
395 s->output_index %= AUDIO_BUFFER_SIZE; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
396 } |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
397 release_sem_etc(s->input_sem, len, 0); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
398 //printf("release_sem(input, %d)\n", len); |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
399 buf += len; |
|
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
400 size -= len; |
| 0 | 401 } |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
402 //XXX: add pts info |
| 0 | 403 return 0; |
| 404 } | |
| 405 | |
| 406 static int audio_read_close(AVFormatContext *s1) | |
| 407 { | |
| 408 AudioData *s = (AudioData *)s1->priv_data; | |
| 409 | |
| 410 audio_close(s); | |
| 411 return 0; | |
| 412 } | |
| 413 | |
|
96
2d3083edb99f
experimental BeOS audio input support. (needs unreleased library)
mmu_man
parents:
95
diff
changeset
|
414 static AVInputFormat audio_in_format = { |
| 0 | 415 "audio_device", |
| 416 "audio grab and output", | |
| 417 sizeof(AudioData), | |
| 418 NULL, | |
| 419 audio_read_header, | |
| 420 audio_read_packet, | |
| 421 audio_read_close, | |
| 422 NULL, | |
| 423 AVFMT_NOFILE, | |
| 424 }; | |
| 425 | |
| 426 AVOutputFormat audio_out_format = { | |
| 427 "audio_device", | |
| 428 "audio grab and output", | |
| 429 "", | |
| 430 "", | |
| 431 sizeof(AudioData), | |
| 432 #ifdef WORDS_BIGENDIAN | |
| 433 CODEC_ID_PCM_S16BE, | |
| 434 #else | |
| 435 CODEC_ID_PCM_S16LE, | |
| 436 #endif | |
| 437 CODEC_ID_NONE, | |
| 438 audio_write_header, | |
| 439 audio_write_packet, | |
| 440 audio_write_trailer, | |
| 441 AVFMT_NOFILE, | |
| 442 }; | |
| 443 | |
| 444 extern "C" { | |
| 445 | |
| 446 int audio_init(void) | |
| 447 { | |
| 448 main_thid = find_thread(NULL); | |
| 449 av_register_input_format(&audio_in_format); | |
| 450 av_register_output_format(&audio_out_format); | |
| 451 return 0; | |
| 452 } | |
| 453 | |
| 454 } // "C" | |
| 455 |
