/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_libao.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/rdesktop/rdpsnd_libao.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1345 - (hide annotations)
Thu Dec 7 11:54:29 2006 UTC (17 years, 6 months ago) by ossman_
File MIME type: text/plain
File size: 4179 byte(s)
Restructure driver registration structures a bit so it is easier to add
new fields (and also reduce some memory usage/leaks).

1 astrand 963 /* -*- c-basic-offset: 8 -*-
2 stargo 833 rdesktop: A Remote Desktop Protocol client.
3     Sound Channel Process Functions - libao-driver
4     Copyright (C) Matthew Chapman 2003
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6 stargo 1254 Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006
7 stargo 833
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12    
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17    
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     */
22    
23     #include "rdesktop.h"
24 stargo 1254 #include "rdpsnd.h"
25 stargo 1258 #include "rdpsnd_dsp.h"
26 stargo 833 #include <unistd.h>
27     #include <fcntl.h>
28     #include <errno.h>
29     #include <ao/ao.h>
30 stargo 891 #include <sys/time.h>
31 stargo 833
32 stargo 1295 #define WAVEOUTLEN 16
33 stargo 833
34 stargo 1245 static ao_device *o_device = NULL;
35     static int default_driver;
36     static BOOL reopened;
37 stargo 1255 static char *libao_device = NULL;
38 stargo 833
39     BOOL
40 stargo 1255 libao_open(void)
41 stargo 833 {
42     ao_sample_format format;
43    
44 stargo 1266 ao_initialize();
45    
46     if (libao_device)
47 stargo 1255 {
48 stargo 1266 default_driver = ao_driver_id(libao_device);
49 stargo 1255 }
50 stargo 1266 else
51     {
52     default_driver = ao_default_driver_id();
53     }
54 stargo 1255
55 stargo 833 format.bits = 16;
56     format.channels = 2;
57     format.rate = 44100;
58 stargo 1289 format.byte_format = AO_FMT_NATIVE;
59 stargo 833
60 stargo 1289
61 stargo 833 o_device = ao_open_live(default_driver, &format, NULL);
62     if (o_device == NULL)
63     {
64     return False;
65     }
66    
67     g_dsp_fd = 0;
68    
69 stargo 1245 reopened = True;
70 stargo 891
71 stargo 833 return True;
72     }
73    
74     void
75 stargo 1255 libao_close(void)
76 stargo 833 {
77     /* Ack all remaining packets */
78 stargo 1254 while (!rdpsnd_queue_empty())
79 stargo 833 {
80 ossman_ 1302 rdpsnd_queue_next(0);
81 stargo 833 }
82    
83     if (o_device != NULL)
84     ao_close(o_device);
85 stargo 838
86 stargo 833 ao_shutdown();
87     }
88    
89     BOOL
90 stargo 1255 libao_set_format(WAVEFORMATEX * pwfx)
91 stargo 833 {
92     ao_sample_format format;
93    
94     format.bits = pwfx->wBitsPerSample;
95     format.channels = pwfx->nChannels;
96     format.rate = 44100;
97 stargo 1289 format.byte_format = AO_FMT_NATIVE;
98 stargo 833
99 stargo 837 if (o_device != NULL)
100 stargo 833 ao_close(o_device);
101    
102     o_device = ao_open_live(default_driver, &format, NULL);
103     if (o_device == NULL)
104     {
105     return False;
106     }
107    
108 stargo 1276 if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
109     {
110     return False;
111     }
112    
113 stargo 1245 reopened = True;
114 stargo 833
115     return True;
116     }
117    
118     void
119 stargo 1255 libao_play(void)
120 stargo 833 {
121     struct audio_packet *packet;
122     STREAM out;
123 stargo 1276 int len;
124 stargo 891 static long prev_s, prev_us;
125     unsigned int duration;
126     struct timeval tv;
127     int next_tick;
128 stargo 833
129 stargo 1245 if (reopened)
130 stargo 891 {
131 stargo 1245 reopened = False;
132 stargo 891 gettimeofday(&tv, NULL);
133     prev_s = tv.tv_sec;
134     prev_us = tv.tv_usec;
135     }
136    
137 stargo 1254 if (rdpsnd_queue_empty())
138 stargo 833 {
139 stargo 835 g_dsp_busy = 0;
140     return;
141     }
142 stargo 833
143 stargo 1254 packet = rdpsnd_queue_current_packet();
144 stargo 835 out = &packet->s;
145 stargo 833
146 stargo 1254 next_tick = rdpsnd_queue_next_tick();
147 stargo 891
148 stargo 1290 len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
149 stargo 1276 ao_play(o_device, (char *) out->p, len);
150     out->p += len;
151 stargo 833
152 stargo 891 gettimeofday(&tv, NULL);
153    
154     duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
155    
156     if (packet->tick > next_tick)
157     next_tick += 65536;
158    
159     if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
160 stargo 835 {
161 stargo 891 prev_s = tv.tv_sec;
162     prev_us = tv.tv_usec;
163    
164     if (abs((next_tick - packet->tick) - duration) > 20)
165     {
166     DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
167     DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
168     (packet->tick + duration) % 65536, next_tick % 65536));
169     }
170    
171 ossman_ 1302 rdpsnd_queue_next(duration);
172 stargo 835 }
173    
174     g_dsp_busy = 1;
175     return;
176 stargo 833 }
177 stargo 1255
178 ossman_ 1345 static struct audio_driver libao_driver = {
179     .name = "libao",
180     .description = "libao output driver, default device: system dependent",
181    
182     .wave_out_open = libao_open,
183     .wave_out_close = libao_close,
184     .wave_out_format_supported = rdpsnd_dsp_resample_supported,
185     .wave_out_set_format = libao_set_format,
186     .wave_out_volume = rdpsnd_dsp_softvol_set,
187     .wave_out_play = libao_play,
188    
189     .need_byteswap_on_be = 1,
190     .need_resampling = 1,
191     };
192    
193 stargo 1255 struct audio_driver *
194     libao_register(char *options)
195     {
196     if (options)
197     {
198     libao_device = xstrdup(options);
199     }
200    
201     return &libao_driver;
202     }

  ViewVC Help
Powered by ViewVC 1.1.26