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"
45 #include <sys/ioctl.h>
46 #include <linux/dvb/audio.h>
47 audio_mixer_t dvb_mixer
={255,255};
53 extern int vo_mpegpes_fd
;
54 int vo_mpegpes_fd2
= -1;
58 static const ao_info_t info
=
63 "MPEG-PES audio output",
73 // to set/get/query special features/parameters
74 static int control(int cmd
,void *arg
){
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;
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
,
101 return CONTROL_UNKNOWN
;
105 static int freq_id
=0;
108 static int init_device(int card
)
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
));
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
));
123 if((ioctl(vo_mpegpes_fd2
,AUDIO_PLAY
) < 0))
125 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB AUDIO PLAY: %s\n", strerror(errno
));
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
));
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
));
139 return vo_mpegpes_fd2
;
143 static int preinit(const char *arg
)
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
},
154 if(subopt_parse(ao_subdevice
, subopts
) != 0)
156 mp_msg(MSGT_VO
, MSGL_ERR
, "AO_MPEGPES, Unrecognized options\n");
161 //search the first usable card
166 sprintf(file
, "/dev/dvb/adapter%d/audio0", n
);
167 if(access(file
, F_OK
| W_OK
)==0)
174 if((card
< 1) || (card
> 4))
176 mp_msg(MSGT_VO
, MSGL_ERR
, "DVB card number must be between 1 and 4\n");
183 return init_device(card
);
186 return vo_mpegpes_fd
; //video fd
189 vo_mpegpes_fd2
=open(ao_file
,O_WRONLY
|O_CREAT
,0666);
192 mp_msg(MSGT_VO
, MSGL_ERR
, "ao_mpegpes: %s\n", strerror(errno
));
195 return vo_mpegpes_fd2
;
198 static int my_ao_write(const unsigned char* data
,int len
){
202 struct pollfd pfd
[NFD
];
204 pfd
[0].fd
= vo_mpegpes_fd2
;
205 pfd
[0].events
= POLLOUT
;
209 if(pfd
[0].revents
& POLLOUT
){
210 int ret
=write(vo_mpegpes_fd2
,data
,len
);
212 mp_msg(MSGT_VO
, MSGL_ERR
, "ao_mpegpes write: %s\n", strerror(errno
));
223 if(vo_mpegpes_fd2
<0) return 0; // no file
224 write(vo_mpegpes_fd2
,data
,len
); // write to file
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;
236 ao_data
.outburst
=2000;
238 case AF_FORMAT_S16_BE
:
239 case AF_FORMAT_MPEG2
:
240 case AF_FORMAT_AC3_BE
:
241 ao_data
.format
=format
;
243 case AF_FORMAT_AC3_LE
:
244 ao_data
.format
=AF_FORMAT_AC3_BE
;
247 ao_data
.format
=AF_FORMAT_S16_BE
;
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;
256 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_MPEGPES_UnsupSamplerate
, rate
);
258 if(rate
>48000) rate
=96000; else
259 if(rate
>44100) rate
=48000; else
260 if(rate
>32000) rate
=44100; else
264 rate
=48000; freq_id
=0;
268 ao_data
.bps
=rate
*2*2;
269 freq
=ao_data
.samplerate
=rate
;
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();
291 // resume playing, after audio_pause()
292 static void audio_resume(void)
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;
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);
306 y
=freq
*4*x
;y
/=ao_data
.outburst
;y
*=ao_data
.outburst
;
308 // printf("diff: %5.3f -> %d \n",x,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
);
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
);
327 // return: delay in seconds between first and last sample in buffer
328 static float get_delay(void){