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.
24 #include <sys/types.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"
44 #include <sys/ioctl.h>
45 #include <linux/dvb/audio.h>
46 audio_mixer_t dvb_mixer
={255,255};
52 extern int vo_mpegpes_fd
;
53 int vo_mpegpes_fd2
= -1;
57 static const ao_info_t info
=
62 "MPEG-PES audio output",
72 // to set/get/query special features/parameters
73 static int control(int cmd
,void *arg
){
76 case AOCONTROL_GET_VOLUME
:
77 if(vo_mpegpes_fd2
>=0){
78 ((ao_control_vol_t
*)(arg
))->left
=dvb_mixer
.volume_left
/2.56;
79 ((ao_control_vol_t
*)(arg
))->right
=dvb_mixer
.volume_right
/2.56;
83 case AOCONTROL_SET_VOLUME
:
84 if(vo_mpegpes_fd2
>=0){
85 dvb_mixer
.volume_left
=((ao_control_vol_t
*)(arg
))->left
*2.56;
86 dvb_mixer
.volume_right
=((ao_control_vol_t
*)(arg
))->right
*2.56;
87 if(dvb_mixer
.volume_left
>255) dvb_mixer
.volume_left
=255;
88 if(dvb_mixer
.volume_right
>255) dvb_mixer
.volume_right
=255;
89 // printf("Setting DVB volume: %d ; %d \n",dvb_mixer.volume_left,dvb_mixer.volume_right);
90 if ( (ioctl(vo_mpegpes_fd2
,AUDIO_SET_MIXER
, &dvb_mixer
) < 0)){
91 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO MPEGPES] DVB audio set mixer failed: %s.\n",
100 return CONTROL_UNKNOWN
;
104 static int freq_id
=0;
107 static int init_device(int card
)
110 mp_msg(MSGT_VO
,MSGL_INFO
, "Opening /dev/dvb/adapter%d/audio0\n", card
);
111 sprintf(ao_file
, "/dev/dvb/adapter%d/audio0", card
);
112 if((vo_mpegpes_fd2
= open(ao_file
,O_RDWR
|O_NONBLOCK
)) < 0)
114 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO DEVICE: %s\n", strerror(errno
));
117 if( (ioctl(vo_mpegpes_fd2
,AUDIO_SELECT_SOURCE
, AUDIO_SOURCE_MEMORY
) < 0))
119 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO SELECT SOURCE: %s\n", strerror(errno
));
122 if((ioctl(vo_mpegpes_fd2
,AUDIO_PLAY
) < 0))
124 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO PLAY: %s\n", strerror(errno
));
127 if((ioctl(vo_mpegpes_fd2
,AUDIO_SET_AV_SYNC
, true) < 0))
129 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO SET AV SYNC: %s\n", strerror(errno
));
132 //FIXME: in vo_mpegpes audio was initialized as MUTEd
133 if((ioctl(vo_mpegpes_fd2
,AUDIO_SET_MUTE
, false) < 0))
135 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO SET MUTE: %s\n", strerror(errno
));
138 return vo_mpegpes_fd2
;
142 static int preinit(const char *arg
)
145 char *ao_file
= NULL
;
147 const opt_t subopts
[] = {
148 {"card", OPT_ARG_INT
, &card
, NULL
},
149 {"file", OPT_ARG_MSTRZ
, &ao_file
, NULL
},
153 if(subopt_parse(ao_subdevice
, subopts
) != 0)
155 mp_msg(MSGT_VO
, MSGL_ERR
, "AO_MPEGPES, Unrecognized options\n");
160 //search the first usable card
165 sprintf(file
, "/dev/dvb/adapter%d/audio0", n
);
166 if(access(file
, F_OK
| W_OK
)==0)
173 if((card
< 1) || (card
> 4))
175 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB card number must be between 1 and 4\n");
182 return init_device(card
);
185 return vo_mpegpes_fd
; //video fd
188 vo_mpegpes_fd2
=open(ao_file
,O_WRONLY
|O_CREAT
,0666);
191 mp_msg(MSGT_VO
, MSGL_ERR
, "ao_mpegpes: %s\n", strerror(errno
));
194 return vo_mpegpes_fd2
;
197 static int my_ao_write(const unsigned char* data
,int len
){
201 struct pollfd pfd
[NFD
];
203 pfd
[0].fd
= vo_mpegpes_fd2
;
204 pfd
[0].events
= POLLOUT
;
208 if(pfd
[0].revents
& POLLOUT
){
209 int ret
=write(vo_mpegpes_fd2
,data
,len
);
211 mp_msg(MSGT_VO
, MSGL_ERR
, "ao_mpegpes write: %s\n", strerror(errno
));
222 if(vo_mpegpes_fd2
<0) return 0; // no file
223 write(vo_mpegpes_fd2
,data
,len
); // write to file
229 // open & setup audio device
230 // return: 1=success 0=fail
231 static int init(int rate
,int channels
,int format
,int flags
){
232 if(preinit(NULL
)<0) return 0;
235 ao_data
.outburst
=2000;
237 case AF_FORMAT_S16_BE
:
238 case AF_FORMAT_MPEG2
:
239 case AF_FORMAT_AC3_BE
:
240 ao_data
.format
=format
;
242 case AF_FORMAT_AC3_LE
:
243 ao_data
.format
=AF_FORMAT_AC3_BE
;
246 ao_data
.format
=AF_FORMAT_S16_BE
;
250 case 48000: freq_id
=0;break;
251 case 96000: freq_id
=1;break;
252 case 44100: freq_id
=2;break;
253 case 32000: freq_id
=3;break;
255 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO MPEGPES] %d Hz not supported, try to resample.\n", rate
);
257 if(rate
>48000) rate
=96000; else
258 if(rate
>44100) rate
=48000; else
259 if(rate
>32000) rate
=44100; else
263 rate
=48000; freq_id
=0;
267 ao_data
.bps
=rate
*2*2;
268 freq
=ao_data
.samplerate
=rate
;
273 // close audio device
274 static void uninit(int immed
){
278 // stop playing and empty buffers (for seeking/pause)
279 static void reset(void){
283 // stop playing, keep buffers (for pause)
284 static void audio_pause(void)
286 // for now, just call reset();
290 // resume playing, after audio_pause()
291 static void audio_resume(void)
297 // return: how many bytes can be played without blocking
298 static int get_space(void){
299 float x
=(float)(vo_pts
-ao_data
.pts
)/90000.0;
301 //FIXME: is it correct?
302 if(vo_mpegpes_fd
< 0) return 32000; //not using -vo mpegpes
303 // printf("vo_pts: %5.3f ao_pts: %5.3f\n",vo_pts/90000.0,ao_data.pts/90000.0);
305 y
=freq
*4*x
;y
/=ao_data
.outburst
;y
*=ao_data
.outburst
;
307 // printf("diff: %5.3f -> %d \n",x,y);
311 // plays 'len' bytes of 'data'
312 // it should round it down to outburst*n
313 // return: number of bytes played
314 static int play(void* data
,int len
,int flags
){
315 // printf("\nao_mpegpes: play(%d) freq=%d\n",len,freq_id);
316 if(ao_data
.format
==AF_FORMAT_MPEG2
)
317 send_mpeg_pes_packet (data
, len
, 0x1C0, ao_data
.pts
, 1, my_ao_write
);
319 // if(len>2000) len=2000;
320 // printf("ao_mpegpes: len=%d \n",len);
321 send_mpeg_lpcm_packet(data
, len
, 0xA0, ao_data
.pts
, freq_id
, my_ao_write
);
326 // return: delay in seconds between first and last sample in buffer
327 static float get_delay(void){