Debian Codegraph @

rplayd/timer.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
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 double timer_rate = 0.0;
37 double timer_count = 0.0;
38 int timer_enabled = 0;
39
40 void
41 timer_update()
42 {
43 #ifdef HAVE_OSS
44 int bytes = rplay_audio_getospace_bytes();
45 /* check bytes and sample size? */
46 if (bytes > 0)
47 {
48 rplayd_write(bytes);
49 }
50 /* if audio device isn't open, just pretend to keep spool going */
51 else if (!rplay_audio_isopen())
52 {
53 /* approximate the amount to be written */
54 int sample_size = (rplay_audio_precision >> 3) * rplay_audio_channels;
55 rplayd_write(rplay_audio_sample_rate / curr_rate * sample_size);
56 }
57 #else
58 rplayd_write(curr_bufsize);
59 #endif
60
61 timer_count += timer_rate;
62 }
63
64 void
65 timer_init()
66 {
67 struct sigaction sa;
68
69 sa.sa_handler = timer_update;
70 sa.sa_flags = 0;
71 #if defined(SA_RESTART) && ! defined(sgi)
72 sa.sa_flags |= SA_RESTART;
73 #endif
74 sigemptyset(&sa.sa_mask);
75 sigaddset(&sa.sa_mask, SIGALRM);
76 if (sigaction(SIGALRM, &sa, (struct sigaction *) NULL) < 0)
77 {
78 report(REPORT_ERROR, "timer_init: sigaction: %s\n", sys_err_str(errno));
79 done(1);
80 }
81
82 timer_enabled = 0;
83 }
84
85 void
86 timer_block()
87 {
88 sigset_t sigset;
89
90 sigemptyset(&sigset);
91 sigaddset(&sigset, SIGALRM);
92
93 if (sigprocmask(SIG_BLOCK, &sigset, NULL) < 0)
94 {
95 report(REPORT_ERROR, "timer_block: sigsetprocmask: %s\n", sys_err_str(errno));
96 done(1);
97 }
98 }
99
100 void
101 timer_unblock()
102 {
103 sigset_t sigset;
104
105 sigemptyset(&sigset);
106 sigaddset(&sigset, SIGALRM);
107
108 if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) < 0)
109 {
110 report(REPORT_ERROR, "timer_block: sigsetprocmask: %s\n", sys_err_str(errno));
111 done(1);
112 }
113 }
114
115 #ifdef __STDC__
116 void
117 timer_restart(int new_rate)
118 #else
119 void
120 timer_restart(new_rate)
121 int new_rate;
122 #endif
123 {
124 struct itimerval it;
125
126 timer_enabled = 1;
127
128 if (new_rate <= 0)
129 {
130 report(REPORT_ERROR, "timer_restart: new_rate=%d, must be >= 1\n", new_rate);
131 done(1);
132 }
133 else if (new_rate == 1)
134 {
135 it.it_interval.tv_sec = 1;
136 it.it_interval.tv_usec = 0;
137 }
138 else
139 {
140 it.it_interval.tv_sec = 0;
141 it.it_interval.tv_usec = 1000000 / new_rate;
142 }
143 it.it_value = it.it_interval;
144 if (setitimer(ITIMER_REAL, &it, NULL) < 0)
145 {
146 report(REPORT_ERROR, "timer_restart: setitimer: %s\n", sys_err_str(errno));
147 done(1);
148 }
149
150 if (it.it_interval.tv_sec)
151 {
152 timer_rate = it.it_interval.tv_sec;
153 }
154 else
155 {
156 timer_rate = (double) it.it_interval.tv_usec / 1000000;
157 }
158 }
159
160 #ifdef __STDC__
161 void
162 timer_start(int new_rate)
163 #else
164 void
165 timer_start(new_rate)
166 int new_rate;
167 #endif
168 {
169 timer_restart(new_rate);
170 }
171
172 void
173 timer_stop()
174 {
175 struct itimerval it;
176
177 timer_enabled = 0;
178
179 it.it_interval.tv_sec = 0;
180 it.it_interval.tv_usec = 0;
181 it.it_value = it.it_interval;
182 if (setitimer(ITIMER_REAL, &it, NULL) < 0)
183 {
184 report(REPORT_ERROR, "timer_stop: setitimer: %s\n", sys_err_str(errno));
185 done(1);
186 }
187 }