Mercurial > mplayer.hg
annotate libao2/ao_oss.c @ 1094:7a6be90408ea
possible buffer overflows fixed
author | al3x |
---|---|
date | Mon, 11 Jun 2001 12:34:46 +0000 |
parents | 1e0da351feaa |
children | f9a46e7843ee |
rev | line source |
---|---|
954 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 | |
4 #include <sys/ioctl.h> | |
5 #include <unistd.h> | |
6 #include <sys/time.h> | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
9 #include <fcntl.h> | |
10 #include <sys/soundcard.h> | |
11 | |
12 #include "../config.h" | |
13 | |
14 #include "audio_out.h" | |
15 #include "audio_out_internal.h" | |
1058 | 16 //#include "afmt.h" |
954 | 17 |
18 static ao_info_t info = | |
19 { | |
20 "OSS/ioctl audio output", | |
956
a6cecd9a1bad
'-ao' switch (including '-ao help'), fixing Arpi's bug (short name 'null' for both of oss and null driver ;)
lgb
parents:
954
diff
changeset
|
21 "oss", |
954 | 22 "A'rpi", |
23 "" | |
24 }; | |
25 | |
26 LIBAO_EXTERN(oss) | |
27 | |
28 // there are some globals: | |
29 // ao_samplerate | |
30 // ao_channels | |
31 // ao_format | |
32 // ao_bps | |
33 // ao_outburst | |
34 // ao_buffersize | |
35 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
36 static char *dsp="/dev/dsp"; |
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
37 static audio_buf_info zz; |
954 | 38 static int audio_fd=-1; |
39 | |
40 // to set/get/query special features/parameters | |
41 static int control(int cmd,int arg){ | |
42 switch(cmd){ | |
43 case AOCONTROL_SET_DEVICE: | |
44 dsp=(char*)arg; | |
45 return CONTROL_OK; | |
46 case AOCONTROL_QUERY_FORMAT: | |
47 return CONTROL_TRUE; | |
48 } | |
49 return CONTROL_UNKNOWN; | |
50 } | |
51 | |
52 // open & setup audio device | |
53 // return: 1=success 0=fail | |
54 static int init(int rate,int channels,int format,int flags){ | |
55 | |
1079 | 56 printf("ao2: %d Hz %d chans %s\n",rate,channels, |
57 audio_out_format_name(format)); | |
954 | 58 |
59 audio_fd=open(dsp, O_WRONLY); | |
60 if(audio_fd<0){ | |
61 printf("Can't open audio device %s -> nosound\n",dsp); | |
62 return 0; | |
63 } | |
64 | |
65 ao_format=format; | |
66 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); | |
1079 | 67 printf("audio_setup: sample format: %s (requested: %s)\n", |
68 audio_out_format_name(ao_format), audio_out_format_name(format)); | |
954 | 69 |
70 ao_channels=channels-1; | |
71 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
72 | |
73 // set rate | |
74 ao_samplerate=rate; | |
75 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
76 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_samplerate,rate); | |
77 | |
78 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){ | |
79 int r=0; | |
80 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n"); | |
81 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){ | |
82 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst); | |
83 } else { | |
84 ao_outburst=r; | |
85 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst); | |
86 } | |
87 } else { | |
88 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n", | |
89 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes); | |
90 if(ao_buffersize==-1) ao_buffersize=zz.bytes; | |
91 ao_outburst=zz.fragsize; | |
92 } | |
93 | |
94 if(ao_buffersize==-1){ | |
95 // Measuring buffer size: | |
96 void* data; | |
97 ao_buffersize=0; | |
98 #ifdef HAVE_AUDIO_SELECT | |
99 data=malloc(ao_outburst); memset(data,0,ao_outburst); | |
100 while(ao_buffersize<0x40000){ | |
101 fd_set rfds; | |
102 struct timeval tv; | |
103 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
104 tv.tv_sec=0; tv.tv_usec = 0; | |
105 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
106 write(audio_fd,data,ao_outburst); | |
107 ao_buffersize+=ao_outburst; | |
108 } | |
109 free(data); | |
110 if(ao_buffersize==0){ | |
111 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
112 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
113 return 0; | |
114 } | |
115 #endif | |
116 } | |
117 | |
118 return 1; | |
119 } | |
120 | |
121 // close audio device | |
122 static void uninit(){ | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
123 #ifdef SNDCTL_DSP_RESET |
954 | 124 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL); |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
125 #endif |
954 | 126 close(audio_fd); |
127 } | |
128 | |
129 // stop playing and empty buffers (for seeking/pause) | |
130 static void reset(){ | |
131 uninit(); | |
132 audio_fd=open(dsp, O_WRONLY); | |
133 if(audio_fd<0){ | |
134 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n"); | |
135 return; | |
136 } | |
137 | |
138 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format); | |
139 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels); | |
140 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate); | |
141 | |
142 } | |
143 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
144 // stop playing, keep buffers (for pause) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
145 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
146 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
147 // for now, just call reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
148 reset(); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
149 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
150 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
151 // resume playing, after audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
152 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
153 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
154 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
155 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
1020
diff
changeset
|
156 |
954 | 157 // return: how many bytes can be played without blocking |
158 static int get_space(){ | |
159 int playsize=ao_outburst; | |
160 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
161 #ifdef SNDCTL_DSP_GETOSPACE |
954 | 162 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){ |
163 // calculate exact buffer space: | |
164 return zz.fragments*zz.fragsize; | |
165 } | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
166 #endif |
954 | 167 |
168 // check buffer | |
169 #ifdef HAVE_AUDIO_SELECT | |
170 { fd_set rfds; | |
171 struct timeval tv; | |
172 FD_ZERO(&rfds); | |
173 FD_SET(audio_fd, &rfds); | |
174 tv.tv_sec = 0; | |
175 tv.tv_usec = 0; | |
176 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block! | |
177 } | |
178 #endif | |
179 | |
1020
72cacd3b8f30
Solaris 8 support - patch by Marcus Comstedt <marcus@idonex.se>
arpi_esp
parents:
956
diff
changeset
|
180 return ao_outburst; |
954 | 181 } |
182 | |
183 // plays 'len' bytes of 'data' | |
184 // it should round it down to outburst*n | |
185 // return: number of bytes played | |
186 static int play(void* data,int len,int flags){ | |
187 len/=ao_outburst; | |
188 len=write(audio_fd,data,len*ao_outburst); | |
189 return len; | |
190 } | |
191 | |
192 static int audio_delay_method=2; | |
193 | |
194 // return: how many unplayed bytes are in the buffer | |
195 static int get_delay(){ | |
196 if(audio_delay_method==2){ | |
197 // | |
198 int r=0; | |
199 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1) | |
200 return r; | |
201 audio_delay_method=1; // fallback if not supported | |
202 } | |
203 if(audio_delay_method==1){ | |
204 // SNDCTL_DSP_GETOSPACE | |
205 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1) | |
206 return ao_buffersize-zz.bytes; | |
207 audio_delay_method=0; // fallback if not supported | |
208 } | |
209 return ao_buffersize; | |
210 } | |
211 |