source: roaraudio/libroar/cdrom.c @ 821:e2cefbe8a430

Last change on this file since 821:e2cefbe8a430 was 821:e2cefbe8a430, checked in by phi, 16 years ago

added a lot new stuff for cd playback including roar_cdrom_run_cdparanoia()

File size: 4.6 KB
Line 
1//cdrom.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37#define ROAR_CDROM_ERROR_NORETURN(format, args...) ROAR_ERR(format, ## args); _exit(3)
38
39#if BYTE_ORDER == BIG_ENDIAN
40#define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-big-endian"
41#elif BYTE_ORDER == LITTLE_ENDIA
42#define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-little-endian"
43#endif
44
45pid_t roar_cdrom_run_cdparanoia (int cdrom, int data, int track, char * pos) {
46#if defined(ROAR_HAVE_BIN_CDPARANOIA) && defined(ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT)
47 char my_pos[32] = {0};
48 pid_t pid;
49 int fh[2];
50
51 if ( cdrom == -1 || data == -1 || (track == -1 && pos == NULL) || (track != -1 && pos != NULL) )
52  return -1;
53
54 if ( track != -1 ) {
55  pos = my_pos;
56  snprintf(pos, 32, "%i", track);
57 }
58
59 if ( (pid = fork()) == -1 ) {
60  return -1;
61 }
62
63 if ( pid )
64  return pid;
65
66 fh[0] = dup(cdrom);
67 fh[1] = dup(data);
68
69 if ( fh[0] == -1 || fh[1] == -1 ) {
70  ROAR_CDROM_ERROR_NORETURN("Can not dup(): %s", strerror(errno));
71 }
72
73 close(ROAR_STDIN);
74 close(ROAR_STDOUT);
75
76 // TODO: should I close some other handles?
77
78 if ( dup2(fh[0], ROAR_STDIN) == -1 || dup2(fh[1], ROAR_STDOUT) == -1 ) {
79  ROAR_CDROM_ERROR_NORETURN("Can not dup2(): %s", strerror(errno));
80 }
81
82 execl(ROAR_HAVE_BIN_CDPARANOIA, "cdparanoia", ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT, pos, "-", NULL);
83
84 ROAR_CDROM_ERROR_NORETURN("We are still alive after exec()!, very bad!, error was: %s", strerror(errno));
85 return -1;
86#else
87 return -1;
88#endif
89}
90
91int roar_cdrom_open (struct roar_connection * con, struct roar_cdrom * cdrom, char * device) {
92 int flags;
93
94 if ( cdrom == NULL )
95  return -1;
96
97 memset((void*)cdrom, 0, sizeof(struct roar_cdrom));
98
99 if ( device == NULL )
100  device = roar_cdromdevice();
101
102 if ( device == NULL )
103  return -1;
104
105 strncpy(cdrom->device, device, ROAR_CDROM_MAX_DEVLEN);
106
107 cdrom->con        = con; // we do not care here if it is set or not as we can operate in local only mode
108
109 cdrom->stream     = -1;
110 cdrom->play_local =  1;
111
112 if ( (cdrom->fh = open(cdrom->device, O_RDONLY, 0644)) == -1 )
113  return -1;
114
115 if ( (flags = fcntl(cdrom->fh, F_GETFL, 0)) == -1 ) {
116  close(cdrom->fh);
117  cdrom->fh  = -1;
118  return -1;
119 }
120
121 flags |= FD_CLOEXEC;
122
123 if ( fcntl(cdrom->fh, F_SETFL, flags) == -1 ) {
124  close(cdrom->fh);
125  cdrom->fh = -1;
126  return -1;
127 }
128
129 return 0;
130}
131
132int roar_cdrom_close(struct roar_cdrom * cdrom) {
133 if ( cdrom == NULL )
134  return -1;
135
136 if ( cdrom->fh != -1 )
137  close(cdrom->fh);
138
139 memset((void*)cdrom, 0, sizeof(struct roar_cdrom));
140
141 return 0;
142}
143
144int roar_cdrom_stop (struct roar_cdrom * cdrom) {
145 int ret;
146
147 if ( cdrom == NULL )
148  return -1;
149
150 if ( cdrom->con == NULL )
151  return -1;
152
153 if ( cdrom->stream == -1 )
154  return -1;
155
156 if ( (ret = roar_kick(cdrom->con, ROAR_OT_STREAM, cdrom->stream)) == -1 ) {
157  return -1;
158 }
159
160 cdrom->stream = -1;
161
162 return ret;
163}
164
165int roar_cdrom_play (struct roar_cdrom * cdrom, int track) {
166 int stream_fh;
167 struct roar_stream stream[1];
168
169 if ( cdrom == NULL )
170  return -1;
171
172 if ( cdrom->con == NULL )
173  return -1;
174
175 if ( cdrom->stream != -1 ) {
176  if ( roar_cdrom_stop(cdrom) == -1 )
177   return -1;
178 }
179
180 if ( cdrom->play_local ) {
181
182  if ( (stream_fh = roar_simple_new_stream_obj(cdrom->con, stream, ROAR_CDROM_STREAMINFO, ROAR_DIR_PLAY)) == -1 ) {
183   return -1;
184  }
185
186  close(stream_fh);
187
188  return -1;
189 } else {
190  // no support for remote playback yet
191  return -1;
192 }
193}
194
195//ll
Note: See TracBrowser for help on using the repository browser.