[811] | 1 | //cdrom.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[6052] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2015 |
---|
[811] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[811] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[5619] | 38 | #define ROAR_CDROM_ERROR_NORETURN(format, args...) ROAR_ERR(format, ## args); ROAR_U_EXIT(3) |
---|
[821] | 39 | |
---|
| 40 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 41 | #define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-big-endian" |
---|
[822] | 42 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
[821] | 43 | #define ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT "--output-raw-little-endian" |
---|
| 44 | #endif |
---|
| 45 | |
---|
[1082] | 46 | #ifdef ROAR_TARGET_WIN32 |
---|
| 47 | #undef ROAR_HAVE_BIN_CDPARANOIA |
---|
| 48 | #endif |
---|
| 49 | |
---|
[821] | 50 | pid_t roar_cdrom_run_cdparanoia (int cdrom, int data, int track, char * pos) { |
---|
[1394] | 51 | #if defined(ROAR_HAVE_BIN_CDPARANOIA) && defined(ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT) && defined(ROAR_HAVE_CDROM) |
---|
[821] | 52 | char my_pos[32] = {0}; |
---|
| 53 | pid_t pid; |
---|
| 54 | int fh[2]; |
---|
[5895] | 55 | char * bin_cdparanoia = NULL, * dev_stdin = NULL; |
---|
[821] | 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; |
---|
[5896] | 64 | snprintf(pos, sizeof(my_pos), "%i", track); |
---|
[821] | 65 | } |
---|
| 66 | |
---|
[5373] | 67 | if ( (pid = roar_fork(NULL)) == (pid_t)-1 ) { |
---|
[821] | 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 | |
---|
[5241] | 90 | // now close our backups: |
---|
[822] | 91 | close(fh[0]); |
---|
| 92 | close(fh[1]); |
---|
| 93 | |
---|
[5895] | 94 | bin_cdparanoia = roar_libroar_get_path("bin-cdparanoia", 0, NULL, NULL); |
---|
| 95 | if ( bin_cdparanoia == NULL ) |
---|
| 96 | return -1; |
---|
| 97 | dev_stdin = roar_libroar_get_path("dev-stdin", 0, NULL, NULL); |
---|
| 98 | if ( dev_stdin == NULL ) { |
---|
[5901] | 99 | roar_mm_free_noerror(bin_cdparanoia); |
---|
[5895] | 100 | return -1; |
---|
| 101 | } |
---|
| 102 | execl(bin_cdparanoia, bin_cdparanoia, "--force-cdrom-device", dev_stdin, "-q", |
---|
[822] | 103 | ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT, pos, "-", NULL); |
---|
[821] | 104 | |
---|
[5895] | 105 | roar_mm_free(bin_cdparanoia); |
---|
| 106 | roar_mm_free(dev_stdin); |
---|
| 107 | |
---|
[821] | 108 | ROAR_CDROM_ERROR_NORETURN("We are still alive after exec()!, very bad!, error was: %s", strerror(errno)); |
---|
| 109 | return -1; |
---|
| 110 | #else |
---|
[822] | 111 | #ifndef ROAR_HAVE_BIN_CDPARANOIA |
---|
| 112 | ROAR_ERR("roar_cdrom_run_cdparanoia(*): ROAR_HAVE_BIN_CDPARANOIA not defined!"); |
---|
| 113 | #endif |
---|
| 114 | #ifndef ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT |
---|
| 115 | ROAR_ERR("roar_cdrom_run_cdparanoia(*): ROAR_CDROM_CDPARANOIA_OUTPUTFORMAT not defined!"); |
---|
| 116 | #endif |
---|
| 117 | ROAR_ERR("roar_cdrom_run_cdparanoia(cdrom=%i, data=%i, track=%i, pos='%s') = -1 // no cdparanoia support compiled in", |
---|
| 118 | cdrom, data, track, pos); |
---|
[821] | 119 | return -1; |
---|
| 120 | #endif |
---|
| 121 | } |
---|
| 122 | |
---|
[5289] | 123 | int roar_cdrom_open (struct roar_connection * con, struct roar_cdrom * cdrom, const char * device, int mixer) { |
---|
[1394] | 124 | #ifdef ROAR_HAVE_CDROM |
---|
[820] | 125 | int flags; |
---|
| 126 | |
---|
[813] | 127 | if ( cdrom == NULL ) |
---|
| 128 | return -1; |
---|
| 129 | |
---|
| 130 | memset((void*)cdrom, 0, sizeof(struct roar_cdrom)); |
---|
| 131 | |
---|
| 132 | if ( device == NULL ) |
---|
| 133 | device = roar_cdromdevice(); |
---|
| 134 | |
---|
| 135 | if ( device == NULL ) |
---|
| 136 | return -1; |
---|
| 137 | |
---|
| 138 | strncpy(cdrom->device, device, ROAR_CDROM_MAX_DEVLEN); |
---|
| 139 | |
---|
[820] | 140 | cdrom->con = con; // we do not care here if it is set or not as we can operate in local only mode |
---|
[5289] | 141 | cdrom->mixer = mixer; |
---|
[820] | 142 | |
---|
| 143 | cdrom->stream = -1; |
---|
| 144 | cdrom->play_local = 1; |
---|
[824] | 145 | cdrom->player = -1; |
---|
[820] | 146 | |
---|
| 147 | if ( (cdrom->fh = open(cdrom->device, O_RDONLY, 0644)) == -1 ) |
---|
| 148 | return -1; |
---|
[813] | 149 | |
---|
[1083] | 150 | #ifndef ROAR_TARGET_WIN32 |
---|
[820] | 151 | if ( (flags = fcntl(cdrom->fh, F_GETFL, 0)) == -1 ) { |
---|
| 152 | close(cdrom->fh); |
---|
| 153 | cdrom->fh = -1; |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | flags |= FD_CLOEXEC; |
---|
| 158 | |
---|
| 159 | if ( fcntl(cdrom->fh, F_SETFL, flags) == -1 ) { |
---|
| 160 | close(cdrom->fh); |
---|
| 161 | cdrom->fh = -1; |
---|
| 162 | return -1; |
---|
| 163 | } |
---|
[1083] | 164 | #endif |
---|
[813] | 165 | |
---|
| 166 | return 0; |
---|
[1394] | 167 | #else |
---|
| 168 | return -1; |
---|
| 169 | #endif |
---|
[813] | 170 | } |
---|
| 171 | |
---|
| 172 | int roar_cdrom_close(struct roar_cdrom * cdrom) { |
---|
[1394] | 173 | #ifdef ROAR_HAVE_CDROM |
---|
[813] | 174 | if ( cdrom == NULL ) |
---|
| 175 | return -1; |
---|
| 176 | |
---|
[824] | 177 | roar_cdrom_stop(cdrom); // stop on close |
---|
| 178 | |
---|
[813] | 179 | if ( cdrom->fh != -1 ) |
---|
| 180 | close(cdrom->fh); |
---|
| 181 | |
---|
| 182 | memset((void*)cdrom, 0, sizeof(struct roar_cdrom)); |
---|
| 183 | |
---|
| 184 | return 0; |
---|
[1394] | 185 | #else |
---|
| 186 | return -1; |
---|
| 187 | #endif |
---|
[813] | 188 | } |
---|
| 189 | |
---|
[820] | 190 | int roar_cdrom_stop (struct roar_cdrom * cdrom) { |
---|
[1394] | 191 | #ifdef ROAR_HAVE_CDROM |
---|
[820] | 192 | int ret; |
---|
| 193 | |
---|
| 194 | if ( cdrom == NULL ) |
---|
| 195 | return -1; |
---|
| 196 | |
---|
| 197 | if ( cdrom->con == NULL ) |
---|
| 198 | return -1; |
---|
| 199 | |
---|
| 200 | if ( cdrom->stream == -1 ) |
---|
| 201 | return -1; |
---|
| 202 | |
---|
| 203 | if ( (ret = roar_kick(cdrom->con, ROAR_OT_STREAM, cdrom->stream)) == -1 ) { |
---|
| 204 | return -1; |
---|
| 205 | } |
---|
| 206 | |
---|
[1093] | 207 | #ifndef ROAR_TARGET_WIN32 |
---|
[824] | 208 | if ( cdrom->player != -1 ) |
---|
| 209 | kill(cdrom->player, SIGINT); |
---|
[1093] | 210 | #else |
---|
| 211 | if ( cdrom->player != -1 ) { |
---|
| 212 | ROAR_ERR("roar_cdrom_stop(*): Can not kill player with pid %i, not supported on win32", cdrom->player); |
---|
| 213 | } |
---|
| 214 | #endif |
---|
[824] | 215 | |
---|
| 216 | cdrom->player = -1; |
---|
[820] | 217 | cdrom->stream = -1; |
---|
| 218 | |
---|
| 219 | return ret; |
---|
[1394] | 220 | #else |
---|
| 221 | return -1; |
---|
| 222 | #endif |
---|
[820] | 223 | } |
---|
| 224 | |
---|
| 225 | int roar_cdrom_play (struct roar_cdrom * cdrom, int track) { |
---|
[1394] | 226 | #ifdef ROAR_HAVE_CDROM |
---|
[5241] | 227 | struct roar_vio_calls vio; |
---|
| 228 | struct roar_stream stream[1]; |
---|
[821] | 229 | int stream_fh; |
---|
| 230 | |
---|
[820] | 231 | if ( cdrom == NULL ) |
---|
| 232 | return -1; |
---|
| 233 | |
---|
| 234 | if ( cdrom->con == NULL ) |
---|
| 235 | return -1; |
---|
| 236 | |
---|
| 237 | if ( cdrom->stream != -1 ) { |
---|
| 238 | if ( roar_cdrom_stop(cdrom) == -1 ) |
---|
| 239 | return -1; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | if ( cdrom->play_local ) { |
---|
[821] | 243 | |
---|
[5289] | 244 | if ( roar_vio_simple_new_stream_obj(&vio, cdrom->con, stream, ROAR_CDROM_STREAMINFO, ROAR_DIR_PLAY, cdrom->mixer) == -1 ) { |
---|
[5241] | 245 | return -1; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | if ( roar_vio_ctl(&vio, ROAR_VIO_CTL_GET_WRITE_FH, &stream_fh) == -1 ) { |
---|
| 249 | roar_vio_close(&vio); |
---|
[821] | 250 | return -1; |
---|
| 251 | } |
---|
| 252 | |
---|
[824] | 253 | if ( (cdrom->player = roar_cdrom_run_cdparanoia(cdrom->fh, stream_fh, track, NULL)) != -1 ) { |
---|
| 254 | cdrom->stream = stream->id; |
---|
[822] | 255 | return 0; |
---|
| 256 | } |
---|
| 257 | |
---|
[5241] | 258 | roar_vio_close(&vio); |
---|
[821] | 259 | |
---|
[820] | 260 | return -1; |
---|
| 261 | } else { |
---|
| 262 | // no support for remote playback yet |
---|
| 263 | return -1; |
---|
| 264 | } |
---|
[1394] | 265 | #else |
---|
| 266 | return -1; |
---|
| 267 | #endif |
---|
[820] | 268 | } |
---|
[813] | 269 | |
---|
[811] | 270 | //ll |
---|