Debian Codegraph @

rplayd/timer.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /* $Id: timer.c,v 1.4 1999/03/10 07:58:04 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay 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 * rplay 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 rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/signal.h>
30 #include <stdio.h>
31 #include <unistd.h>
•32 #include <errno.h>
33 #include "rplayd.h"
34 #include "timer.h"
35
•36 int rplay_audio_getospace_bytes();
37
38 double timer_rate = 0.0;
39 double timer_count = 0.0;
40 int timer_enabled = 0;
41
42 void
•43 timer_update(int dummy)
44 {
45 #ifdef HAVE_OSS
46 int bytes = rplay_audio_getospace_bytes();
47 /* check bytes and sample size? */
48 if (bytes > 0)
49 {
50 rplayd_write(bytes);
51 }
52 /* if audio device isn't open, just pretend to keep spool going */
53 else if (!rplay_audio_isopen())
54 {
55 /* approximate the amount to be written */
56 int sample_size = (rplay_audio_precision >> 3) * rplay_audio_channels;
57 rplayd_write(rplay_audio_sample_rate / curr_rate * sample_size);
58 }
59 #else
60 rplayd_write(curr_bufsize);
61 #endif
62
63 timer_count += timer_rate;
64 }
65
66 void
67 timer_init()
68 {
69 struct sigaction sa;
70
71 sa.sa_handler = timer_update;
72 sa.sa_flags = 0;
73 #if defined(SA_RESTART) && ! defined(sgi)
74 sa.sa_flags |= SA_RESTART;
75 #endif
76 sigemptyset(&sa.sa_mask);
77 sigaddset(&sa.sa_mask, SIGALRM);
78 if (sigaction(SIGALRM, &sa, (struct sigaction *) NULL) < 0)
79 {
80 report(REPORT_ERROR, "timer_init: sigaction: %s\n", sys_err_str(errno));
81 done(1);
82 }
83
84 timer_enabled = 0;
85 }
86
87 void
88 timer_block()
89 {
90 sigset_t sigset;
91
92 sigemptyset(&sigset);
93 sigaddset(&sigset, SIGALRM);
94
95 if (sigprocmask(SIG_BLOCK, &sigset, NULL) < 0)
96 {
97 report(REPORT_ERROR, "timer_block: sigsetprocmask: %s\n", sys_err_str(errno));
98 done(1);
99 }
100 }
101
102 void
103 timer_unblock()
104 {
105 sigset_t sigset;
106
107 sigemptyset(&sigset);
108 sigaddset(&sigset, SIGALRM);
109
110 if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0)
111 {
112 report(REPORT_ERROR, "timer_block: sigsetprocmask: %s\n", sys_err_str(errno));
113 done(1);
114 }
115 }
116
117 #ifdef __STDC__
118 void
119 timer_restart(int new_rate)
120 #else
121 void
122 timer_restart(new_rate)
123 int new_rate;
124 #endif
125 {
126 struct itimerval it;
127
128 timer_enabled = 1;
129
130 if (new_rate <= 0)
131 {
132 report(REPORT_ERROR, "timer_restart: new_rate=%d, must be >= 1\n", new_rate);
133 done(1);
134 }
135 else if (new_rate == 1)
136 {
137 it.it_interval.tv_sec = 1;
138 it.it_interval.tv_usec = 0;
139 }
140 else
141 {
142 it.it_interval.tv_sec = 0;
143 it.it_interval.tv_usec = 1000000 / new_rate;
144 }
145 it.it_value = it.it_interval;
146 if (setitimer(ITIMER_REAL, &it, NULL) < 0)
147 {
148 report(REPORT_ERROR, "timer_restart: setitimer: %s\n", sys_err_str(errno));
149 done(1);
150 }
151
152 if (it.it_interval.tv_sec)
153 {
154 timer_rate = it.it_interval.tv_sec;
155 }
156 else
157 {
158 timer_rate = (double) it.it_interval.tv_usec / 1000000;
159 }
160 }
161
162 #ifdef __STDC__
163 void
164 timer_start(int new_rate)
165 #else
166 void
167 timer_start(new_rate)
168 int new_rate;
169 #endif
170 {
171 timer_restart(new_rate);
172 }
173
174 void
175 timer_stop()
176 {
177 struct itimerval it;
178
179 timer_enabled = 0;
180
181 it.it_interval.tv_sec = 0;
182 it.it_interval.tv_usec = 0;
183 it.it_value = it.it_interval;
184 if (setitimer(ITIMER_REAL, &it, NULL) < 0)
185 {
186 report(REPORT_ERROR, "timer_stop: setitimer: %s\n", sys_err_str(errno));
187 done(1);
188 }
189 }