Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libao2 / ao_mpegpes.c
blob27b2d792924cfe8ab3845febd57cd101ec5d7d26
1 /*
2 * MPEG-PES audio output driver
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <inttypes.h>
29 #include <errno.h>
31 #include "config.h"
33 #include "audio_out.h"
34 #include "audio_out_internal.h"
36 #include "libaf/af_format.h"
37 #include "libmpdemux/mpeg_packetizer.h"
38 #include "subopt-helper.h"
40 #include "mp_msg.h"
41 #include "help_mp.h"
43 #ifdef CONFIG_DVB
44 #include <poll.h>
45 #include <sys/ioctl.h>
46 #include <linux/dvb/audio.h>
47 audio_mixer_t dvb_mixer={255,255};
48 #endif
50 #define true 1
51 #define false 0
53 extern int vo_mpegpes_fd;
54 int vo_mpegpes_fd2 = -1;
56 #include <errno.h>
58 static const ao_info_t info =
60 #ifdef CONFIG_DVB
61 "DVB audio output",
62 #else
63 "MPEG-PES audio output",
64 #endif
65 "mpegpes",
66 "A'rpi",
70 LIBAO_EXTERN(mpegpes)
73 // to set/get/query special features/parameters
74 static int control(int cmd,void *arg){
75 #ifdef CONFIG_DVB
76 switch(cmd){
77 case AOCONTROL_GET_VOLUME:
78 if(vo_mpegpes_fd2>=0){
79 ((ao_control_vol_t*)(arg))->left=dvb_mixer.volume_left/2.56;
80 ((ao_control_vol_t*)(arg))->right=dvb_mixer.volume_right/2.56;
81 return CONTROL_OK;
83 return CONTROL_ERROR;
84 case AOCONTROL_SET_VOLUME:
85 if(vo_mpegpes_fd2>=0){
86 dvb_mixer.volume_left=((ao_control_vol_t*)(arg))->left*2.56;
87 dvb_mixer.volume_right=((ao_control_vol_t*)(arg))->right*2.56;
88 if(dvb_mixer.volume_left>255) dvb_mixer.volume_left=255;
89 if(dvb_mixer.volume_right>255) dvb_mixer.volume_right=255;
90 // printf("Setting DVB volume: %d ; %d \n",dvb_mixer.volume_left,dvb_mixer.volume_right);
91 if ( (ioctl(vo_mpegpes_fd2,AUDIO_SET_MIXER, &dvb_mixer) < 0)){
92 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_MPEGPES_CantSetMixer,
93 strerror(errno));
94 return CONTROL_ERROR;
96 return CONTROL_OK;
98 return CONTROL_ERROR;
100 #endif
101 return CONTROL_UNKNOWN;
104 static int freq=0;
105 static int freq_id=0;
107 #ifdef CONFIG_DVB
108 static int init_device(int card)
110 char ao_file[30];
111 mp_msg(MSGT_VO,MSGL_INFO, "Opening /dev/dvb/adapter%d/audio0\n", card);
112 sprintf(ao_file, "/dev/dvb/adapter%d/audio0", card);
113 if((vo_mpegpes_fd2 = open(ao_file,O_RDWR|O_NONBLOCK)) < 0)
115 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO DEVICE: %s\n", strerror(errno));
116 return -1;
118 if( (ioctl(vo_mpegpes_fd2,AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0))
120 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SELECT SOURCE: %s\n", strerror(errno));
121 return -1;
123 if((ioctl(vo_mpegpes_fd2,AUDIO_PLAY) < 0))
125 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO PLAY: %s\n", strerror(errno));
126 return -1;
128 if((ioctl(vo_mpegpes_fd2,AUDIO_SET_AV_SYNC, true) < 0))
130 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET AV SYNC: %s\n", strerror(errno));
131 return -1;
133 //FIXME: in vo_mpegpes audio was initialized as MUTEd
134 if((ioctl(vo_mpegpes_fd2,AUDIO_SET_MUTE, false) < 0))
136 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET MUTE: %s\n", strerror(errno));
137 return -1;
139 return vo_mpegpes_fd2;
141 #endif
143 static int preinit(const char *arg)
145 int card = -1;
146 char *ao_file = NULL;
148 const opt_t subopts[] = {
149 {"card", OPT_ARG_INT, &card, NULL},
150 {"file", OPT_ARG_MSTRZ, &ao_file, NULL},
151 {NULL}
154 if(subopt_parse(ao_subdevice, subopts) != 0)
156 mp_msg(MSGT_VO, MSGL_ERR, "AO_MPEGPES, Unrecognized options\n");
157 return -1;
159 if(card==-1)
161 //search the first usable card
162 int n;
163 char file[30];
164 for(n=0; n<4; n++)
166 sprintf(file, "/dev/dvb/adapter%d/audio0", n);
167 if(access(file, F_OK | W_OK)==0)
169 card = n+1;
170 break;
174 if((card < 1) || (card > 4))
176 mp_msg(MSGT_VO, MSGL_ERR, "DVB card number must be between 1 and 4\n");
177 return -1;
179 card--;
181 #ifdef CONFIG_DVB
182 if(!ao_file)
183 return init_device(card);
184 #else
185 if(!ao_file)
186 return vo_mpegpes_fd; //video fd
187 #endif
189 vo_mpegpes_fd2=open(ao_file,O_WRONLY|O_CREAT,0666);
190 if(vo_mpegpes_fd2<0)
192 mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes: %s\n", strerror(errno));
193 return -1;
195 return vo_mpegpes_fd2;
198 static int my_ao_write(const unsigned char* data,int len){
199 int orig_len = len;
200 #ifdef CONFIG_DVB
201 #define NFD 1
202 struct pollfd pfd[NFD];
204 pfd[0].fd = vo_mpegpes_fd2;
205 pfd[0].events = POLLOUT;
207 while(len>0){
208 if(poll(pfd,NFD,1)){
209 if(pfd[0].revents & POLLOUT){
210 int ret=write(vo_mpegpes_fd2,data,len);
211 if(ret<=0){
212 mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes write: %s\n", strerror(errno));
213 usleep(0);
214 } else {
215 len-=ret;
216 data+=ret;
218 } else usleep(1000);
222 #else
223 if(vo_mpegpes_fd2<0) return 0; // no file
224 write(vo_mpegpes_fd2,data,len); // write to file
225 #endif
226 return orig_len;
230 // open & setup audio device
231 // return: 1=success 0=fail
232 static int init(int rate,int channels,int format,int flags){
233 if(preinit(NULL)<0) return 0;
235 ao_data.channels=2;
236 ao_data.outburst=2000;
237 switch(format){
238 case AF_FORMAT_S16_BE:
239 case AF_FORMAT_MPEG2:
240 case AF_FORMAT_AC3_BE:
241 ao_data.format=format;
242 break;
243 case AF_FORMAT_AC3_LE:
244 ao_data.format=AF_FORMAT_AC3_BE;
245 break;
246 default:
247 ao_data.format=AF_FORMAT_S16_BE;
250 switch(rate){
251 case 48000: freq_id=0;break;
252 case 96000: freq_id=1;break;
253 case 44100: freq_id=2;break;
254 case 32000: freq_id=3;break;
255 default:
256 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_MPEGPES_UnsupSamplerate, rate);
257 #if 0
258 if(rate>48000) rate=96000; else
259 if(rate>44100) rate=48000; else
260 if(rate>32000) rate=44100; else
261 rate=32000;
262 goto retry;
263 #else
264 rate=48000; freq_id=0;
265 #endif
268 ao_data.bps=rate*2*2;
269 freq=ao_data.samplerate=rate;
271 return 1;
274 // close audio device
275 static void uninit(int immed){
279 // stop playing and empty buffers (for seeking/pause)
280 static void reset(void){
284 // stop playing, keep buffers (for pause)
285 static void audio_pause(void)
287 // for now, just call reset();
288 reset();
291 // resume playing, after audio_pause()
292 static void audio_resume(void)
296 extern int vo_pts;
298 // return: how many bytes can be played without blocking
299 static int get_space(void){
300 float x=(float)(vo_pts-ao_data.pts)/90000.0;
301 int y;
302 //FIXME: is it correct?
303 if(vo_mpegpes_fd < 0) return 32000; //not using -vo mpegpes
304 // printf("vo_pts: %5.3f ao_pts: %5.3f\n",vo_pts/90000.0,ao_data.pts/90000.0);
305 if(x<=0) return 0;
306 y=freq*4*x;y/=ao_data.outburst;y*=ao_data.outburst;
307 if(y>32000) y=32000;
308 // printf("diff: %5.3f -> %d \n",x,y);
309 return y;
312 // plays 'len' bytes of 'data'
313 // it should round it down to outburst*n
314 // return: number of bytes played
315 static int play(void* data,int len,int flags){
316 // printf("\nao_mpegpes: play(%d) freq=%d\n",len,freq_id);
317 if(ao_data.format==AF_FORMAT_MPEG2)
318 send_mpeg_pes_packet (data, len, 0x1C0, ao_data.pts, 1, my_ao_write);
319 else {
320 // if(len>2000) len=2000;
321 // printf("ao_mpegpes: len=%d \n",len);
322 send_mpeg_lpcm_packet(data, len, 0xA0, ao_data.pts, freq_id, my_ao_write);
324 return len;
327 // return: delay in seconds between first and last sample in buffer
328 static float get_delay(void){
330 return 0.0;