~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libao2/ao_mpegpes.c

  • Committer: William Grant
  • Date: 2007-02-03 03:16:07 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: william.grant@ubuntu.org.au-20070203031607-08gc2ompbz6spt9i
Update to 1.0rc1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <string.h>
 
4
#include <sys/types.h>
 
5
#include <sys/stat.h>
 
6
#include <fcntl.h>
 
7
#include <unistd.h>
 
8
#include <inttypes.h>
 
9
#include <errno.h>
4
10
 
5
11
#include "config.h"
6
12
 
9
15
#endif
10
16
 
11
17
#ifdef HAVE_DVB
 
18
#include <sys/poll.h>
12
19
#include <sys/ioctl.h>
13
20
#endif
14
21
 
16
23
#include "audio_out_internal.h"
17
24
 
18
25
#include "libaf/af_format.h"
 
26
#include "libmpdemux/mpeg_packetizer.h"
 
27
#include "subopt-helper.h"
19
28
 
20
29
#include "mp_msg.h"
21
30
#include "help_mp.h"
29
38
audio_mixer_t dvb_mixer={255,255};
30
39
#endif
31
40
#endif
 
41
 
 
42
#define true 1
 
43
#define false 0
 
44
 
32
45
extern int vo_mpegpes_fd;
33
 
extern int vo_mpegpes_fd2;
 
46
int vo_mpegpes_fd2 = -1;
34
47
 
35
48
#include <errno.h>
36
49
 
83
96
static int freq=0;
84
97
static int freq_id=0;
85
98
 
 
99
#ifdef HAVE_DVB
 
100
static int init_device(int card)
 
101
{
 
102
        char ao_file[30];
 
103
#ifndef HAVE_DVB_HEAD
 
104
        mp_msg(MSGT_VO,MSGL_INFO, "Opening /dev/ost/audio\n");
 
105
        sprintf(ao_file, "/dev/ost/audio");
 
106
#else
 
107
        mp_msg(MSGT_VO,MSGL_INFO, "Opening /dev/dvb/adapter%d/audio0\n", card);
 
108
        sprintf(ao_file, "/dev/dvb/adapter%d/audio0", card);
 
109
#endif
 
110
        if((vo_mpegpes_fd2 = open(ao_file,O_RDWR|O_NONBLOCK)) < 0)
 
111
        {
 
112
                mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO DEVICE: %s\n", strerror(errno));
 
113
                return -1;
 
114
        }
 
115
        if( (ioctl(vo_mpegpes_fd2,AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0))
 
116
        {
 
117
                mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SELECT SOURCE: %s\n", strerror(errno));
 
118
                return -1;
 
119
        }
 
120
        if((ioctl(vo_mpegpes_fd2,AUDIO_PLAY) < 0))
 
121
        {
 
122
                mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO PLAY: %s\n", strerror(errno));
 
123
                return -1;
 
124
        }
 
125
        if((ioctl(vo_mpegpes_fd2,AUDIO_SET_AV_SYNC, true) < 0))
 
126
        {
 
127
                mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET AV SYNC: %s\n", strerror(errno));
 
128
                return -1;
 
129
        }
 
130
        //FIXME: in vo_mpegpes audio was inited as MUTEd
 
131
        if((ioctl(vo_mpegpes_fd2,AUDIO_SET_MUTE, false) < 0))
 
132
        {
 
133
                mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET MUTE: %s\n", strerror(errno));
 
134
                return -1;
 
135
        }
 
136
        return vo_mpegpes_fd2;
 
137
}
 
138
#endif
 
139
 
 
140
static int preinit(const char *arg)
 
141
{
 
142
        int card = 1;
 
143
        char *ao_file = NULL;
 
144
 
 
145
        opt_t subopts[] = {
 
146
                {"card", OPT_ARG_INT, &card, NULL},
 
147
                {"file", OPT_ARG_MSTRZ, &ao_file, NULL},
 
148
                {NULL}
 
149
        };
 
150
 
 
151
        if(subopt_parse(ao_subdevice, subopts) != 0)
 
152
        {
 
153
                mp_msg(MSGT_VO, MSGL_ERR, "AO_MPEGPES, Unrecognized options\n");
 
154
                return -1;
 
155
        }
 
156
        if((card < 1) || (card > 4))
 
157
        {
 
158
                mp_msg(MSGT_VO, MSGL_ERR, "DVB card number must be between 1 and 4\n");
 
159
                return -1;
 
160
        }
 
161
        card--;
 
162
 
 
163
#ifdef HAVE_DVB
 
164
        if(!ao_file)
 
165
                return init_device(card);
 
166
#else   
 
167
        if(!ao_file)
 
168
                return vo_mpegpes_fd;   //video fd
 
169
#endif
 
170
 
 
171
        vo_mpegpes_fd2=open(ao_file,O_WRONLY|O_CREAT,0666);
 
172
        if(vo_mpegpes_fd2<0)
 
173
        {
 
174
                mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes: %s\n", strerror(errno));
 
175
                return -1;
 
176
        }
 
177
        return vo_mpegpes_fd2;
 
178
}
 
179
 
 
180
static int my_ao_write(unsigned char* data,int len){
 
181
    int orig_len = len;
 
182
#ifdef HAVE_DVB
 
183
#define NFD   1
 
184
    struct pollfd pfd[NFD];
 
185
 
 
186
    pfd[0].fd = vo_mpegpes_fd2;
 
187
    pfd[0].events = POLLOUT;
 
188
 
 
189
    while(len>0){
 
190
        if(poll(pfd,NFD,1)){
 
191
            if(pfd[0].revents & POLLOUT){
 
192
                int ret=write(vo_mpegpes_fd2,data,len);
 
193
                if(ret<=0){
 
194
                    mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes write: %s\n", strerror(errno));
 
195
                    usleep(0);
 
196
                } else {
 
197
                    len-=ret;
 
198
                    data+=ret;
 
199
                }
 
200
            } else usleep(1000);
 
201
        }
 
202
    }
 
203
 
 
204
#else
 
205
    if(vo_mpegpes_fd2<0) return 0; // no file
 
206
    write(vo_mpegpes_fd2,data,len); // write to file
 
207
#endif
 
208
    return orig_len;
 
209
}
 
210
 
 
211
 
86
212
// open & setup audio device
87
213
// return: 1=success 0=fail
88
214
static int init(int rate,int channels,int format,int flags){
89
 
 
90
 
#ifdef HAVE_DVB
91
 
    if(vo_mpegpes_fd2<0) return 0; // couldn't open audio dev
92
 
#else
93
 
    if(vo_mpegpes_fd<0) return 0; // no file
94
 
#endif
 
215
    if(preinit(NULL)<0) return 0;
95
216
 
96
217
    ao_data.channels=2;
97
218
    ao_data.outburst=2000;
161
282
static int get_space(void){
162
283
    float x=(float)(vo_pts-ao_data.pts)/90000.0;
163
284
    int y;
 
285
    //FIXME: is it correct?
 
286
    if(vo_mpegpes_fd < 0) return 32000; //not using -vo mpegpes
164
287
//    printf("vo_pts: %5.3f  ao_pts: %5.3f\n",vo_pts/90000.0,ao_data.pts/90000.0);
165
288
    if(x<=0) return 0;
166
289
    y=freq*4*x;y/=ao_data.outburst;y*=ao_data.outburst;
175
298
static int play(void* data,int len,int flags){
176
299
//    printf("\nao_mpegpes: play(%d) freq=%d\n",len,freq_id);
177
300
    if(ao_data.format==AF_FORMAT_MPEG2)
178
 
        send_pes_packet(data,len,0x1C0,ao_data.pts);
 
301
        send_mpeg_pes_packet (data, len, 0x1C0, ao_data.pts, 1, my_ao_write);
179
302
    else {
180
303
        int i;
181
304
        unsigned short *s=data;
183
306
//      printf("ao_mpegpes: len=%d  \n",len);
184
307
        if(ao_data.format==AF_FORMAT_S16_LE || ao_data.format==AF_FORMAT_AC3)
185
308
            for(i=0;i<len/2;i++) s[i]=(s[i]>>8)|(s[i]<<8); // le<->be
186
 
        send_lpcm_packet(data,len,0xA0,ao_data.pts,freq_id);
 
309
        send_mpeg_lpcm_packet(data, len, 0xA0, ao_data.pts, freq_id, my_ao_write);
187
310
    }
188
311
    return len;
189
312
}