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"
|
|
16
|
|
17 static ao_info_t info =
|
|
18 {
|
|
19 "OSS/ioctl audio output",
|
|
20 "null",
|
|
21 "A'rpi",
|
|
22 ""
|
|
23 };
|
|
24
|
|
25 LIBAO_EXTERN(oss)
|
|
26
|
|
27 // there are some globals:
|
|
28 // ao_samplerate
|
|
29 // ao_channels
|
|
30 // ao_format
|
|
31 // ao_bps
|
|
32 // ao_outburst
|
|
33 // ao_buffersize
|
|
34
|
|
35 static char* dsp="/dev/dsp";
|
|
36 static int audio_fd=-1;
|
|
37 static audio_buf_info zz;
|
|
38
|
|
39 // to set/get/query special features/parameters
|
|
40 static int control(int cmd,int arg){
|
|
41 switch(cmd){
|
|
42 case AOCONTROL_SET_DEVICE:
|
|
43 dsp=(char*)arg;
|
|
44 return CONTROL_OK;
|
|
45 case AOCONTROL_QUERY_FORMAT:
|
|
46 return CONTROL_TRUE;
|
|
47 }
|
|
48 return CONTROL_UNKNOWN;
|
|
49 }
|
|
50
|
|
51 // open & setup audio device
|
|
52 // return: 1=success 0=fail
|
|
53 static int init(int rate,int channels,int format,int flags){
|
|
54
|
|
55 printf("ao2: %d Hz %d chans 0x%X\n",rate,channels,format);
|
|
56
|
|
57 audio_fd=open(dsp, O_WRONLY);
|
|
58 if(audio_fd<0){
|
|
59 printf("Can't open audio device %s -> nosound\n",dsp);
|
|
60 return 0;
|
|
61 }
|
|
62
|
|
63 ao_format=format;
|
|
64 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format);
|
|
65 printf("audio_setup: sample format: 0x%X (requested: 0x%X)\n",ao_format,format);
|
|
66
|
|
67 ao_channels=channels-1;
|
|
68 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels);
|
|
69
|
|
70 // set rate
|
|
71 ao_samplerate=rate;
|
|
72 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate);
|
|
73 printf("audio_setup: using %d Hz samplerate (requested: %d)\n",ao_samplerate,rate);
|
|
74
|
|
75 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
|
|
76 int r=0;
|
|
77 printf("audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n");
|
|
78 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){
|
|
79 printf("audio_setup: %d bytes/frag (config.h)\n",ao_outburst);
|
|
80 } else {
|
|
81 ao_outburst=r;
|
|
82 printf("audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_outburst);
|
|
83 }
|
|
84 } else {
|
|
85 printf("audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
|
|
86 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
|
|
87 if(ao_buffersize==-1) ao_buffersize=zz.bytes;
|
|
88 ao_outburst=zz.fragsize;
|
|
89 }
|
|
90
|
|
91 if(ao_buffersize==-1){
|
|
92 // Measuring buffer size:
|
|
93 void* data;
|
|
94 ao_buffersize=0;
|
|
95 #ifdef HAVE_AUDIO_SELECT
|
|
96 data=malloc(ao_outburst); memset(data,0,ao_outburst);
|
|
97 while(ao_buffersize<0x40000){
|
|
98 fd_set rfds;
|
|
99 struct timeval tv;
|
|
100 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
|
|
101 tv.tv_sec=0; tv.tv_usec = 0;
|
|
102 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
|
|
103 write(audio_fd,data,ao_outburst);
|
|
104 ao_buffersize+=ao_outburst;
|
|
105 }
|
|
106 free(data);
|
|
107 if(ao_buffersize==0){
|
|
108 printf("\n *** Your audio driver DOES NOT support select() ***\n");
|
|
109 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n");
|
|
110 return 0;
|
|
111 }
|
|
112 #endif
|
|
113 }
|
|
114
|
|
115 return 1;
|
|
116 }
|
|
117
|
|
118 // close audio device
|
|
119 static void uninit(){
|
|
120 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
|
|
121 close(audio_fd);
|
|
122 }
|
|
123
|
|
124 // stop playing and empty buffers (for seeking/pause)
|
|
125 static void reset(){
|
|
126 uninit();
|
|
127 audio_fd=open(dsp, O_WRONLY);
|
|
128 if(audio_fd<0){
|
|
129 printf("\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE ***\n");
|
|
130 return;
|
|
131 }
|
|
132
|
|
133 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &ao_format);
|
|
134 ioctl (audio_fd, SNDCTL_DSP_STEREO, &ao_channels);
|
|
135 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_samplerate);
|
|
136
|
|
137 }
|
|
138
|
|
139 // return: how many bytes can be played without blocking
|
|
140 static int get_space(){
|
|
141 int playsize=ao_outburst;
|
|
142
|
|
143 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
|
|
144 // calculate exact buffer space:
|
|
145 return zz.fragments*zz.fragsize;
|
|
146 }
|
|
147
|
|
148 // check buffer
|
|
149 #ifdef HAVE_AUDIO_SELECT
|
|
150 { fd_set rfds;
|
|
151 struct timeval tv;
|
|
152 FD_ZERO(&rfds);
|
|
153 FD_SET(audio_fd, &rfds);
|
|
154 tv.tv_sec = 0;
|
|
155 tv.tv_usec = 0;
|
|
156 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
|
|
157 }
|
|
158 #endif
|
|
159
|
|
160 return ao_outburst;
|
|
161 }
|
|
162
|
|
163 // plays 'len' bytes of 'data'
|
|
164 // it should round it down to outburst*n
|
|
165 // return: number of bytes played
|
|
166 static int play(void* data,int len,int flags){
|
|
167 len/=ao_outburst;
|
|
168 len=write(audio_fd,data,len*ao_outburst);
|
|
169 return len;
|
|
170 }
|
|
171
|
|
172 static int audio_delay_method=2;
|
|
173
|
|
174 // return: how many unplayed bytes are in the buffer
|
|
175 static int get_delay(){
|
|
176 if(audio_delay_method==2){
|
|
177 //
|
|
178 int r=0;
|
|
179 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
|
|
180 return r;
|
|
181 audio_delay_method=1; // fallback if not supported
|
|
182 }
|
|
183 if(audio_delay_method==1){
|
|
184 // SNDCTL_DSP_GETOSPACE
|
|
185 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
|
|
186 return ao_buffersize-zz.bytes;
|
|
187 audio_delay_method=0; // fallback if not supported
|
|
188 }
|
|
189 return ao_buffersize;
|
|
190 }
|
|
191
|