Debian Codegraph @

rplayd/timer.c

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