fixes the frame number counter for -vcodec copy patch by (mru at users dot sourceforg...
[libav.git] / libav / beosaudio.cpp
CommitLineData
dfdfa47c
FR
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
30extern "C" {
31#include "avformat.h"
32}
33
34
35//const char *audio_device = "/dev/dsp";
36const char *audio_device = "beosaudio:";
37
38#define AUDIO_BLOCK_SIZE 4096
39
40typedef struct {
41 int fd;
42 int sample_rate;
43 int channels;
44 int frame_size; /* in bytes ! */
45 CodecID codec_id;
46 int flip_left : 1;
47 UINT8 buffer[AUDIO_BLOCK_SIZE];
48 int buffer_ptr;
49 int pipefd; /* the other end of the pipe */
50 BSoundPlayer *player;
51 int has_quit; /* signal callbacks not to wait */
52} AudioData;
53
54static int own_BApp_created = 0;
55
56/* create the BApplication and Run() it */
57static int32 bapp_thread(void *arg)
58{
59 new BApplication("application/x-vnd.ffmpeg");
60 own_BApp_created = 1;
61 be_app->Run();
62 /* kill the process group */
63 kill(0, SIGINT);
64 return B_OK;
65}
66
67/* called back by BSoundPlayer */
68static void audioplay_callback(void *cookie, void *buffer, size_t bufferSize, const media_raw_audio_format &format)
69{
70 AudioData *s;
71 size_t amount;
72 unsigned char *buf = (unsigned char *)buffer;
73
74 s = (AudioData *)cookie;
75 if (s->has_quit)
76 return;
77 while (bufferSize > 0) {
78 if ((amount = read(s->pipefd, buf, bufferSize)) < B_OK) {
79 puts("EPIPE");
80 snooze(100000);
81 s->player->SetHasData(false);
82 return;
83 }
84 if (amount == 0) {
85 snooze(100000);
86 s->player->SetHasData(false);
87 return;
88 }
89 buf += amount;
90 bufferSize -= amount;
91 }
92}
93
94static int audio_open(AudioData *s, int is_output)
95{
96 int p[2];
97 int ret;
98 media_raw_audio_format format;
99
100 if (!is_output)
101 return -EIO; /* not for now */
102 ret = pipe(p);
103 if (ret < 0)
104 return -EIO;
105 s->fd = p[is_output?1:0];
106 s->pipefd = p[is_output?0:1];
107 if (s->fd < 0) {
108 perror(is_output?"audio out":"audio in");
109 return -EIO;
110 }
111 /* non blocking mode */
112// fcntl(s->fd, F_SETFL, O_NONBLOCK);
113// fcntl(s->pipefd, F_SETFL, O_NONBLOCK);
114 s->frame_size = AUDIO_BLOCK_SIZE;
115 format = media_raw_audio_format::wildcard;
116 format.format = media_raw_audio_format::B_AUDIO_SHORT;
117 format.byte_order = B_HOST_IS_LENDIAN ? B_MEDIA_LITTLE_ENDIAN : B_MEDIA_BIG_ENDIAN;
118 format.channel_count = s->channels;
119 format.buffer_size = s->frame_size;
120 format.frame_rate = s->sample_rate;
121 s->player = new BSoundPlayer(&format, "ffmpeg output", audioplay_callback);
122 if (s->player->InitCheck() != B_OK) {
123 delete s->player;
124 s->player = NULL;
125 close(s->fd);
126 close(s->pipefd);
127 return -EIO;
128 }
129 s->player->SetCookie(s);
130 s->player->SetVolume(1.0);
131 s->player->Start();
132 s->player->SetHasData(true);
133 return 0;
134}
135
136static int audio_close(AudioData *s)
137{
138 s->has_quit = 1;
139 if (s->player) {
140 s->player->Stop();
141 }
142 if (s->player)
143 delete s->player;
144 close(s->pipefd);
145 close(s->fd);
146 return 0;
147}
148
149/* sound output support */
150static int audio_write_header(AVFormatContext *s1)
151{
152 AudioData *s = (AudioData *)s1->priv_data;
153 AVStream *st;
154 int ret;
155
156 st = s1->streams[0];
157 s->sample_rate = st->codec.sample_rate;
158 s->channels = st->codec.channels;
159 ret = audio_open(s, 1);
160 if (ret < 0)
161 return -EIO;
162 return 0;
163}
164
165static int audio_write_packet(AVFormatContext *s1, int stream_index,
166 UINT8 *buf, int size, int force_pts)
167{
168 AudioData *s = (AudioData *)s1->priv_data;
169 int len, ret;
170
171 while (size > 0) {
172 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
173 if (len > size)
174 len = size;
175 memcpy(s->buffer + s->buffer_ptr, buf, len);
176 s->buffer_ptr += len;
177 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
178 for(;;) {
179snooze(1000);
180 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
181 if (ret != 0)
182 break;
183 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
184 return -EIO;
185 }
186 s->buffer_ptr = 0;
187 }
188 buf += len;
189 size -= len;
190 }
191 return 0;
192}
193
194static int audio_write_trailer(AVFormatContext *s1)
195{
196 AudioData *s = (AudioData *)s1->priv_data;
197
198 audio_close(s);
199 return 0;
200}
201
202/* grab support */
203
204static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
205{
206 AudioData *s = (AudioData *)s1->priv_data;
207 AVStream *st;
208 int ret;
209
210 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
211 return -1;
212
213 st = av_new_stream(s1, 0);
214 if (!st) {
215 return -ENOMEM;
216 }
217 s->sample_rate = ap->sample_rate;
218 s->channels = ap->channels;
219
220 ret = audio_open(s, 0);
221 if (ret < 0) {
222 av_free(st);
223 return -EIO;
224 } else {
225 /* take real parameters */
226 st->codec.codec_type = CODEC_TYPE_AUDIO;
227 st->codec.codec_id = s->codec_id;
228 st->codec.sample_rate = s->sample_rate;
229 st->codec.channels = s->channels;
230 return 0;
231 }
232}
233
234static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
235{
236 AudioData *s = (AudioData *)s1->priv_data;
237 int ret;
238
239 if (av_new_packet(pkt, s->frame_size) < 0)
240 return -EIO;
241 for(;;) {
242 ret = read(s->fd, pkt->data, pkt->size);
243 if (ret > 0)
244 break;
245 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
246 av_free_packet(pkt);
247 pkt->size = 0;
248 return 0;
249 }
250 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
251 av_free_packet(pkt);
252 return -EIO;
253 }
254 }
255 pkt->size = ret;
256 if (s->flip_left && s->channels == 2) {
257 int i;
258 short *p = (short *) pkt->data;
259
260 for (i = 0; i < ret; i += 4) {
261 *p = ~*p;
262 p += 2;
263 }
264 }
265 return 0;
266}
267
268static int audio_read_close(AVFormatContext *s1)
269{
270 AudioData *s = (AudioData *)s1->priv_data;
271
272 audio_close(s);
273 return 0;
274}
275
276AVInputFormat audio_in_format = {
277 "audio_device",
278 "audio grab and output",
279 sizeof(AudioData),
280 NULL,
281 audio_read_header,
282 audio_read_packet,
283 audio_read_close,
284 NULL,
285 AVFMT_NOFILE,
286};
287
288AVOutputFormat audio_out_format = {
289 "audio_device",
290 "audio grab and output",
291 "",
292 "",
293 sizeof(AudioData),
294#ifdef WORDS_BIGENDIAN
295 CODEC_ID_PCM_S16BE,
296#else
297 CODEC_ID_PCM_S16LE,
298#endif
299 CODEC_ID_NONE,
300 audio_write_header,
301 audio_write_packet,
302 audio_write_trailer,
303 AVFMT_NOFILE,
304};
305
306extern "C" {
307
308int audio_init(void)
309{
310 /* needed by libmedia */
311 if (be_app == NULL) {
312 resume_thread(spawn_thread(bapp_thread, "ffmpeg BApplication", B_NORMAL_PRIORITY, NULL));
313 while (!own_BApp_created)
314 snooze(50000);
315 }
316 av_register_input_format(&audio_in_format);
317 av_register_output_format(&audio_out_format);
318 return 0;
319}
320
321} // "C"
322