Debian Codegraph @

contrib/cdrplay/cdrplay.c

raw symbols SCIP
1 /* $Id: cdrplay.c,v 1.3 1999/03/10 07:51:42 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 /* A lot of the CDDA code is based on read_cdda 1.01 by Jim Mintha
25 ([email protected]) which seems to have borrowed code from Workman
26 sources written by Steven Grimm ([email protected]). */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef HAVE_CDROM
33
34 #include <sys/types.h>
35 #include <sys/errno.h>
36 #include "rplayd.h"
37 #include "cdrom.h"
38
39 CDROM_TABLE cdrom_table[MAX_CDROMS] =
40 {
41 { "cdrom:", "/vol/dev/aliases/cdrom0", 0, 0 },
42 { "cdrom0:", "/vol/dev/aliases/cdrom0", 0, 0 },
43 { "cdrom1:", "/vol/dev/aliases/cdrom1", 0, 0 },
44 { "cdrom2:", "/vol/dev/aliases/cdrom2", 0, 0 },
45 { "cdrom3:", "/vol/dev/aliases/cdrom3", 0, 0 },
46 };
47
48 /* Internal prototypes. */
49 #ifdef HAVE_CDDA
50 #ifdef __STDC__
51 static void cdda_cdrom_reader (CDROM_TABLE *cdt, int starting_track, int ending_track, int output_fd);
52 #else
53 static void cdda_cdrom_reader (/* CDROM_TABLE *cdt, int starting_track, int ending_track, int output_fd */);
54 #endif
55 #endif /* HAVE_CDDA */
56
57 /* starting_track and ending_track can be 0 which means first and last */
58 #ifdef __STDC__
59 void
60 cdrom_reader (int index, int starting_track, int ending_track, int output_fd)
61 #else
62 void
63 cdrom_reader (index, starting_track, ending_track, output_fd)
64 int index;
65 int starting_track;
66 int ending_track;
67 int output_fd;
68 #endif
69 {
70 CDROM_TABLE *cdt;
71
72 cdt = &cdrom_table[index];
73
74 report (REPORT_DEBUG, "cdrom_reader: %s%s tracks %d..%d\n",
75 cdt->name, cdt->device, starting_track, ending_track);
76
77 #ifdef HAVE_CDDA
78 rplay_audio_close (); /* prevent device busy problems */
79 cdda_cdrom_reader (cdt, starting_track, ending_track, output_fd);
80 #endif /* HAVE_CDDA */
81
82 /* Add more cdrom readers here. */
83
84 exit (0);
85 }
86
87
88 /******************************************************************************/
89
90 #ifdef HAVE_CDDA
91
92 /* A nicer interface to reading from the CDDA interface using the
93 CDDA_INFO structure and the cdda_open, cdda_read, and cdda_close
94 routines. */
95
96 typedef struct
97 {
98 char *device;
99 int starting_track;
100 int ending_track;
101 int num_blocks;
102 int fd;
103 char *buffer;
104 int buffer_length;
105 int starting_block;
106 int current_block;
107 int ending_block;
108 }
109 CDDA_INFO;
110
111 #define CDDA_BLOCK_SIZE 2368
112 #define CDDA_BLOCK_IGNORE 16
113
114 static int cdda_open (CDDA_INFO *info);
115 static int cdda_read (CDDA_INFO *info);
116 static int cdda_close (CDDA_INFO *info);
117 static int internal_cdda_init (CDDA_INFO *info);
118 static int internal_cdda_read (CDDA_INFO *info);
119 static int internal_cdda_toc (CDDA_INFO *info);
120
121 #ifdef __STDC__
122 static void
123 cdda_cdrom_reader (CDROM_TABLE *cdt, int starting_track, int ending_track, int output_fd)
124 #else
125 static void
126 cdda_cdrom_reader (cdt, starting_track, ending_track, output_fd)
127 CDROM_TABLE *cdt;
128 int starting_track;
129 int ending_track;
130 int output_fd;
131 #endif
132 {
133 CDDA_INFO info;
134 int n, i;
135 char *p;
136
137 info.device = cdt->device;
138 info.starting_track = starting_track;
139 info.ending_track = ending_track;
140 info.num_blocks = 30;
141 info.fd = -1;
142 info.buffer = NULL;
143
144 if (cdda_open (&info) < 0)
145 {
146 return;
147 }
148
149 for (;;)
150 {
151 n = cdda_read (&info);
152 if (n <= 0)
153 {
154 break;
155 }
156
157 p = info.buffer;
158 for (i = 0; i < info.num_blocks; i++)
159 {
160 write (output_fd, p, CDDA_BLOCK_SIZE - CDDA_BLOCK_IGNORE);
161 p += CDDA_BLOCK_SIZE;
162 }
163 }
164
165 cdda_close (&info);
166 }
167
168 #ifdef __STDC__
169 static int
170 cdda_open (CDDA_INFO *info)
171 #else
172 static int
173 cdda_open (info)
174 CDDA_INFO *info;
175 #endif
176 {
177 if (info->fd < 0)
178 {
179 if (internal_cdda_init (info) < 0)
180 {
181 return -1;
182 }
183 }
184
185 return internal_cdda_toc (info);
186 }
187
188 #ifdef __STDC__
189 static int
190 cdda_read (CDDA_INFO *info)
191 #else
192 static int
193 cdda_read (info)
194 CDDA_INFO *info;
195 #endif
196 {
197 int n;
198
199 n = internal_cdda_read (info);
200 if (n < 0)
201 {
202 return -1;
203 }
204
205 return n;
206 }
207
208 #ifdef __STDC__
209 static int
210 cdda_close (CDDA_INFO *info)
211 #else
212 static int
213 cdda_close (info)
214 CDDA_INFO *info;
215 #endif
216 {
217 close (info->fd);
218 info->fd = -1;
219 if (info->buffer)
220 {
221 free ((char *) info->buffer);
222 }
223 info->buffer = NULL;
224 return 0;
225 }
226
227 /* End of nicer CDDA interface. */
228
229 #include <sys/cdio.h>
230 #include <sys/ioctl.h>
231 #include <sys/param.h>
232 #include <sys/stat.h>
233 #include <sys/scsi/impl/uscsi.h>
234 #include <sys/time.h>
235 #include <ustat.h>
236
237 #define CDDABLKSIZE 2368
238 #define SAMPLES_PER_BLK 588
239
240 #ifdef __STDC__
241 static int
242 internal_cdda_init (CDDA_INFO *info)
243 #else
244 static int
245 internal_cdda_init (info)
246 CDDA_INFO *info;
247 #endif
248 {
249 struct cdrom_cdda cdda;
250
251 info->fd = open (info->device, 0);
252 if (info->fd < 0)
253 {
254 perror (info->device);
255 return -1;
256 }
257
258 info->buffer = (char *) malloc (info->num_blocks * CDDABLKSIZE + CDDABLKSIZE);
259 if (info->buffer == NULL)
260 {
261 fprintf (stderr, "malloc: out of memory\n");
262 return -1;
263 }
264
265 info->buffer_length = info->num_blocks * CDDABLKSIZE;
266
267 cdda.cdda_addr = 200;
268 cdda.cdda_length = 1;
269 cdda.cdda_data = info->buffer;
270 cdda.cdda_subcode = CDROM_DA_SUBQ;
271
272 if (ioctl (info->fd, CDROMCDDA, &cdda) < 0)
273 {
274 perror ("ioctl: CDROMCDDA");
275 free ((char *) info->buffer);
276 info->buffer = NULL;
277 close (info->fd);
278 return -1;
279 }
280
281 return 0;
282 }
283
284 #ifdef __STDC__
285 static int
286 internal_cdda_read (CDDA_INFO *info)
287 #else
288 static int
289 internal_cdda_read (info)
290 CDDA_INFO *info;
291 #endif
292 {
293 struct cdrom_cdda cdda;
294 int blk;
295 unsigned char *q;
296
297 if (info->current_block >= info->ending_block)
298 {
299 return 0;
300 }
301
302 cdda.cdda_addr = info->current_block;
303 if (info->current_block + info->num_blocks > info->ending_block)
304 {
305 cdda.cdda_length = info->ending_block - info->current_block;
306 }
307 else
308 {
309 cdda.cdda_length = info->num_blocks;
310 }
311
312 cdda.cdda_data = info->buffer;
313 cdda.cdda_subcode = CDROM_DA_SUBQ;
314
315 if (ioctl (info->fd, CDROMCDDA, &cdda) < 0)
316 {
317 if (errno == ENXIO)
318 {
319 perror ("ioctl: CDROMCDDA: CD ejected");
320 return -1;
321 }
322
323 if (info->current_block + info->num_blocks > info->ending_block)
324 {
325 return 0;
326 }
327
328 if (ioctl (info->fd, CDROMCDDA, &cdda) < 0)
329 {
330 if (ioctl (info->fd, CDROMCDDA, &cdda) < 0)
331 {
332 if (ioctl (info->fd, CDROMCDDA, &cdda) < 0)
333 {
334 perror ("ioctl: CDROMCDDA");
335 return -1;
336 }
337 }
338 }
339 }
340
341
342 info->current_block += cdda.cdda_length;
343
344 return cdda.cdda_length * CDDABLKSIZE;
345 }
346
347 #ifdef __STDC__
348 static int
349 internal_cdda_toc (CDDA_INFO *info)
350 #else
351 static int
352 internal_cdda_toc (info)
353 CDDA_INFO *info;
354 #endif
355 {
356 struct cdrom_tochdr hdr;
357 struct cdrom_tocentry entry;
358 int i;
359
360 if (ioctl (info->fd, CDROMREADTOCHDR, &hdr) < 0)
361 {
362 perror ("ioctl: CDROMREADTOCHDR");
363 return -1;
364 }
365
366 info->starting_block = 0;
367 info->ending_block = 0;
368
369 if (info->starting_track == 0)
370 {
371 info->starting_track = hdr.cdth_trk0;
372 }
373 if (info->ending_track == 0)
374 {
375 info->ending_track = hdr.cdth_trk1;
376 }
377
378 for (i = 1; i <= hdr.cdth_trk1; i++)
379 {
380 entry.cdte_track = i;
381 entry.cdte_format = CDROM_MSF;
382
383 if (ioctl (info->fd, CDROMREADTOCENTRY, &entry) < 0)
384 {
385 perror ("ioctl: CDROMREADTOCENTRY");
386 return -1;
387 }
388
389 if (i == info->starting_track)
390 {
391 info->starting_block = entry.cdte_addr.msf.minute * 60 * 75 +
392 entry.cdte_addr.msf.second * 75 + entry.cdte_addr.msf.frame;
393 }
394
395 if (i == info->ending_track + 1)
396 {
397 info->ending_block = entry.cdte_addr.msf.minute * 60 * 75 +
398 entry.cdte_addr.msf.second * 75 + entry.cdte_addr.msf.frame;
399 }
400 }
401
402 entry.cdte_track = CDROM_LEADOUT;
403 entry.cdte_format = CDROM_MSF;
404
405 if (ioctl (info->fd, CDROMREADTOCENTRY, &entry) < 0)
406 {
407 perror ("ioctl: CDROMREADTOCENTRY");
408 return -1;
409 }
410
411 if (info->ending_block == 0)
412 {
413 info->ending_block = entry.cdte_addr.msf.minute * 60 * 75 +
414 entry.cdte_addr.msf.second * 75 + entry.cdte_addr.msf.frame;
415 }
416
417 /* Move back two seconds - don't know why but works */
418 info->starting_block -= 150;
419 info->ending_block -= 150;
420
421 info->current_block = info->starting_block;
422
423 return 0;
424 }
425
426 #endif /* HAVE_CDDA */
427
428 /******************************************************************************/
429
430 #endif /* HAVE_CDROM */