[811] | 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" |
---|
[824] | 36 | #include <sys/types.h> |
---|
| 37 | #include <signal.h> |
---|
[811] | 38 | |
---|
[821] | 39 | #define ROAR_CDROM_ERROR_NORETURN(format, args...) ROAR_ERR(format, ## args); _exit(3) |
---|
| 40 | |
---|
| 41 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 42 | #define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-big-endian" |
---|
[822] | 43 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
[821] | 44 | #define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-little-endian" |
---|
| 45 | #endif |
---|
| 46 | |
---|
[1082] | 47 | #ifdef ROAR_TARGET_WIN32 |
---|
| 48 | #undef ROAR_HAVE_BIN_CDPARANOIA |
---|
| 49 | #endif |
---|
| 50 | |
---|
[821] | 51 | pid_t roar_cdrom_run_cdparanoia (int cdrom, int data, int track, char * pos) { |
---|
| 52 | #if defined(ROAR_HAVE_BIN_CDPARANOIA) && defined(ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT) |
---|
| 53 | char my_pos[32] = {0}; |
---|
| 54 | pid_t pid; |
---|
| 55 | int fh[2]; |
---|
| 56 | |
---|
[822] | 57 | ROAR_DBG("roar_cdrom_run_cdparanoia(cdrom=%i, data=%i, track=%i, pos='%s') = ?", cdrom, data, track, pos); |
---|
| 58 | |
---|
[821] | 59 | if ( cdrom == -1 || data == -1 || (track == -1 && pos == NULL) || (track != -1 && pos != NULL) ) |
---|
| 60 | return -1; |
---|
| 61 | |
---|
| 62 | if ( track != -1 ) { |
---|
| 63 | pos = my_pos; |
---|
| 64 | snprintf(pos, 32, "%i", track); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if ( (pid = fork()) == -1 ) { |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if ( pid ) |
---|
| 72 | return pid; |
---|
| 73 | |
---|
| 74 | fh[0] = dup(cdrom); |
---|
| 75 | fh[1] = dup(data); |
---|
| 76 | |
---|
| 77 | if ( fh[0] == -1 || fh[1] == -1 ) { |
---|
| 78 | ROAR_CDROM_ERROR_NORETURN("Can not dup(): %s", strerror(errno)); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | close(ROAR_STDIN); |
---|
| 82 | close(ROAR_STDOUT); |
---|
| 83 | |
---|
| 84 | // TODO: should I close some other handles? |
---|
| 85 | |
---|
| 86 | if ( dup2(fh[0], ROAR_STDIN) == -1 || dup2(fh[1], ROAR_STDOUT) == -1 ) { |
---|
| 87 | ROAR_CDROM_ERROR_NORETURN("Can not dup2(): %s", strerror(errno)); |
---|
| 88 | } |
---|
| 89 | |
---|
[822] | 90 | // new close our backups: |
---|
| 91 | close(fh[0]); |
---|
| 92 | close(fh[1]); |
---|
| 93 | |
---|
| 94 | execl(ROAR_HAVE_BIN_CDPARANOIA, "cdparanoia", "--force-cdrom-device", "/dev/stdin", "-q", |
---|
| 95 | ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT, pos, "-", NULL); |
---|
[821] | 96 | |
---|
| 97 | ROAR_CDROM_ERROR_NORETURN("We are still alive after exec()!, very bad!, error was: %s", strerror(errno)); |
---|
| 98 | return -1; |
---|
| 99 | #else |
---|
[822] | 100 | #ifndef ROAR_HAVE_BIN_CDPARANOIA |
---|
| 101 | ROAR_ERR("roar_cdrom_run_cdparanoia(*): ROAR_HAVE_BIN_CDPARANOIA not defined!"); |
---|
| 102 | #endif |
---|
| 103 | #ifndef ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT |
---|
| 104 | ROAR_ERR("roar_cdrom_run_cdparanoia(*): ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT not defined!"); |
---|
| 105 | #endif |
---|
| 106 | ROAR_ERR("roar_cdrom_run_cdparanoia(cdrom=%i, data=%i, track=%i, pos='%s') = -1 // no cdparanoia support compiled in", |
---|
| 107 | cdrom, data, track, pos); |
---|
[821] | 108 | return -1; |
---|
| 109 | #endif |
---|
| 110 | } |
---|
| 111 | |
---|
[813] | 112 | int roar_cdrom_open (struct roar_connection * con, struct roar_cdrom * cdrom, char * device) { |
---|
[820] | 113 | int flags; |
---|
| 114 | |
---|
[813] | 115 | if ( cdrom == NULL ) |
---|
| 116 | return -1; |
---|
| 117 | |
---|
| 118 | memset((void*)cdrom, 0, sizeof(struct roar_cdrom)); |
---|
| 119 | |
---|
| 120 | if ( device == NULL ) |
---|
| 121 | device = roar_cdromdevice(); |
---|
| 122 | |
---|
| 123 | if ( device == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | strncpy(cdrom->device, device, ROAR_CDROM_MAX_DEVLEN); |
---|
| 127 | |
---|
[820] | 128 | cdrom->con = con; // we do not care here if it is set or not as we can operate in local only mode |
---|
| 129 | |
---|
| 130 | cdrom->stream = -1; |
---|
| 131 | cdrom->play_local = 1; |
---|
[824] | 132 | cdrom->player = -1; |
---|
[820] | 133 | |
---|
| 134 | if ( (cdrom->fh = open(cdrom->device, O_RDONLY, 0644)) == -1 ) |
---|
| 135 | return -1; |
---|
[813] | 136 | |
---|
[1083] | 137 | #ifndef ROAR_TARGET_WIN32 |
---|
[820] | 138 | if ( (flags = fcntl(cdrom->fh, F_GETFL, 0)) == -1 ) { |
---|
| 139 | close(cdrom->fh); |
---|
| 140 | cdrom->fh = -1; |
---|
| 141 | return -1; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | flags |= FD_CLOEXEC; |
---|
| 145 | |
---|
| 146 | if ( fcntl(cdrom->fh, F_SETFL, flags) == -1 ) { |
---|
| 147 | close(cdrom->fh); |
---|
| 148 | cdrom->fh = -1; |
---|
| 149 | return -1; |
---|
| 150 | } |
---|
[1083] | 151 | #endif |
---|
[813] | 152 | |
---|
| 153 | return 0; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | int roar_cdrom_close(struct roar_cdrom * cdrom) { |
---|
| 157 | if ( cdrom == NULL ) |
---|
| 158 | return -1; |
---|
| 159 | |
---|
[824] | 160 | roar_cdrom_stop(cdrom); // stop on close |
---|
| 161 | |
---|
[813] | 162 | if ( cdrom->fh != -1 ) |
---|
| 163 | close(cdrom->fh); |
---|
| 164 | |
---|
| 165 | memset((void*)cdrom, 0, sizeof(struct roar_cdrom)); |
---|
| 166 | |
---|
| 167 | return 0; |
---|
| 168 | } |
---|
| 169 | |
---|
[820] | 170 | int roar_cdrom_stop (struct roar_cdrom * cdrom) { |
---|
| 171 | int ret; |
---|
| 172 | |
---|
| 173 | if ( cdrom == NULL ) |
---|
| 174 | return -1; |
---|
| 175 | |
---|
| 176 | if ( cdrom->con == NULL ) |
---|
| 177 | return -1; |
---|
| 178 | |
---|
| 179 | if ( cdrom->stream == -1 ) |
---|
| 180 | return -1; |
---|
| 181 | |
---|
| 182 | if ( (ret = roar_kick(cdrom->con, ROAR_OT_STREAM, cdrom->stream)) == -1 ) { |
---|
| 183 | return -1; |
---|
| 184 | } |
---|
| 185 | |
---|
[1093] | 186 | #ifndef ROAR_TARGET_WIN32 |
---|
[824] | 187 | if ( cdrom->player != -1 ) |
---|
| 188 | kill(cdrom->player, SIGINT); |
---|
[1093] | 189 | #else |
---|
| 190 | if ( cdrom->player != -1 ) { |
---|
| 191 | ROAR_ERR("roar_cdrom_stop(*): Can not kill player with pid %i, not supported on win32", cdrom->player); |
---|
| 192 | } |
---|
| 193 | #endif |
---|
[824] | 194 | |
---|
| 195 | cdrom->player = -1; |
---|
[820] | 196 | cdrom->stream = -1; |
---|
| 197 | |
---|
| 198 | return ret; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int roar_cdrom_play (struct roar_cdrom * cdrom, int track) { |
---|
[821] | 202 | int stream_fh; |
---|
| 203 | struct roar_stream stream[1]; |
---|
| 204 | |
---|
[820] | 205 | if ( cdrom == NULL ) |
---|
| 206 | return -1; |
---|
| 207 | |
---|
| 208 | if ( cdrom->con == NULL ) |
---|
| 209 | return -1; |
---|
| 210 | |
---|
| 211 | if ( cdrom->stream != -1 ) { |
---|
| 212 | if ( roar_cdrom_stop(cdrom) == -1 ) |
---|
| 213 | return -1; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if ( cdrom->play_local ) { |
---|
[821] | 217 | |
---|
| 218 | if ( (stream_fh = roar_simple_new_stream_obj(cdrom->con, stream, ROAR_CDROM_STREAMINFO, ROAR_DIR_PLAY)) == -1 ) { |
---|
| 219 | return -1; |
---|
| 220 | } |
---|
| 221 | |
---|
[824] | 222 | if ( (cdrom->player = roar_cdrom_run_cdparanoia(cdrom->fh, stream_fh, track, NULL)) != -1 ) { |
---|
| 223 | cdrom->stream = stream->id; |
---|
[822] | 224 | return 0; |
---|
| 225 | } |
---|
| 226 | |
---|
[821] | 227 | close(stream_fh); |
---|
| 228 | |
---|
[820] | 229 | return -1; |
---|
| 230 | } else { |
---|
| 231 | // no support for remote playback yet |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
| 234 | } |
---|
[813] | 235 | |
---|
[811] | 236 | //ll |
---|