[343] | 1 | //playback.c: |
---|
| 2 | |
---|
[688] | 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014 |
---|
[688] | 5 | * |
---|
[4037] | 6 | * The code (may) include prototypes and comments (and maybe |
---|
| 7 | * other code fragements) from the yiff sound system. |
---|
| 8 | * According to the debian/copyright file upstream author is |
---|
| 9 | * Tara Milana <learfox@twu.net>. Also copyright is listed as: |
---|
| 10 | * Copyright (C) 1997-2003 WolfPack Entertainment |
---|
| 11 | * |
---|
[688] | 12 | * This file is part of libroaryiff a part of RoarAudio, |
---|
| 13 | * a cross-platform sound system for both, home and professional use. |
---|
| 14 | * See README for details. |
---|
| 15 | * |
---|
| 16 | * This file is free software; you can redistribute it and/or modify |
---|
| 17 | * it under the terms of the GNU General Public License version 3 |
---|
| 18 | * as published by the Free Software Foundation. |
---|
| 19 | * |
---|
| 20 | * libroaryiff is distributed in the hope that it will be useful, |
---|
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 23 | * GNU General Public License for more details. |
---|
| 24 | * |
---|
| 25 | * You should have received a copy of the GNU General Public License |
---|
| 26 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 27 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 28 | * Boston, MA 02110-1301, USA. |
---|
[688] | 29 | * |
---|
[4037] | 30 | * NOTE: code fragments (like prototypes) taken from the yiff package |
---|
| 31 | * are 'GPLv2 or later' and are upgraded to GPLv3 by being used |
---|
| 32 | * within this document. |
---|
| 33 | * |
---|
[688] | 34 | */ |
---|
| 35 | |
---|
[343] | 36 | #include <libroaryiff.h> |
---|
| 37 | |
---|
[5221] | 38 | #define BUFFERSIZE 8192 |
---|
| 39 | |
---|
| 40 | #ifdef ROAR_HAVE_IO_POSIX |
---|
| 41 | #define _CAN_OPERATE |
---|
| 42 | #endif |
---|
| 43 | |
---|
[343] | 44 | YID YStartPlaySoundObjectSimple (YConnection *con, const char *path) { |
---|
| 45 | return YStartPlaySoundObject(con, path, NULL); |
---|
| 46 | } |
---|
| 47 | |
---|
[5221] | 48 | static inline ssize_t _file_play (struct roar_connection * con, const char * file, struct roar_stream * s) { |
---|
| 49 | #ifdef _CAN_OPERATE |
---|
| 50 | int codec = -1; |
---|
| 51 | int in, out = -1; |
---|
| 52 | ssize_t r = 0; |
---|
| 53 | int seek; |
---|
| 54 | int len; |
---|
| 55 | char buf[BUFFERSIZE]; |
---|
| 56 | int rate = ROAR_RATE_DEFAULT, channels = ROAR_CHANNELS_DEFAULT, bits = ROAR_BITS_DEFAULT; |
---|
| 57 | |
---|
| 58 | // FIXME: check error cases |
---|
| 59 | |
---|
| 60 | ROAR_DBG("roar_file_play_full(*) = ?"); |
---|
| 61 | |
---|
| 62 | #ifdef ROAR_TARGET_WIN32 |
---|
| 63 | if ( (in = open(file, O_RDONLY|O_BINARY, 0644)) == -1 ) { |
---|
| 64 | #else |
---|
| 65 | if ( (in = open(file, O_RDONLY, 0644)) == -1 ) { |
---|
| 66 | #endif |
---|
| 67 | roar_err_from_errno(); |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | if ((len = read(in, buf, BUFFERSIZE)) < 1) { |
---|
| 72 | roar_err_from_errno(); |
---|
| 73 | close(in); |
---|
| 74 | return -1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | codec = roar_file_codecdetect(buf, len); |
---|
| 78 | |
---|
| 79 | ROAR_DBG("roar_file_play_full(*): codec=%i(%s)", codec, roar_codec2str(codec)); |
---|
| 80 | |
---|
| 81 | seek = lseek(in, 0, SEEK_SET) == (off_t) -1 ? 0 : 1; |
---|
| 82 | |
---|
| 83 | if ( codec == -1 ) { |
---|
| 84 | ROAR_WARN("roar_file_play_full(*): Unknown codec of file: %s", file); |
---|
| 85 | close(in); |
---|
| 86 | roar_err_set(ROAR_ERROR_BADMAGIC); |
---|
| 87 | return -1; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if ( !seek ) { |
---|
| 91 | ROAR_WARN("roar_file_play_full(*): passfh on non seekable file: this may produce incorrect playback"); |
---|
| 92 | close(in); |
---|
| 93 | roar_err_set(ROAR_ERROR_NOSEEK); |
---|
| 94 | return -1; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
| 98 | close(in); |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
| 101 | |
---|
[5238] | 102 | if ( roar_stream_connect(con, s, ROAR_DIR_PLAY, -1) == -1 ) { |
---|
[5221] | 103 | close(in); |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if ( roar_stream_passfh(con, s, in) == -1 ) { |
---|
| 108 | close(in); |
---|
| 109 | roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s)); |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | close(out); |
---|
| 114 | close(in); |
---|
| 115 | return 0; |
---|
| 116 | |
---|
| 117 | return r; |
---|
| 118 | #else |
---|
| 119 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 120 | return -1; |
---|
| 121 | #endif |
---|
| 122 | } |
---|
| 123 | |
---|
[343] | 124 | YID YStartPlaySoundObject (YConnection *con, const char *path, YEventSoundPlay *value) { |
---|
[790] | 125 | struct roar_connection rcon; |
---|
| 126 | struct roar_stream stream[1]; |
---|
| 127 | |
---|
[5221] | 128 | if ( con == NULL ) |
---|
[343] | 129 | return YIDNULL; |
---|
| 130 | |
---|
[5221] | 131 | if ( path == NULL ) |
---|
[343] | 132 | return YIDNULL; |
---|
| 133 | |
---|
[1660] | 134 | roar_connect_fh(&rcon, con->fd); |
---|
[790] | 135 | |
---|
[343] | 136 | // hm,... find out how to do this. |
---|
| 137 | // need to start ssize_t roar_file_play (struct roar_connection * con, char * file, int exec) |
---|
| 138 | // in background |
---|
| 139 | |
---|
[5221] | 140 | if ( _file_play(&rcon, path, stream) == -1 ) { |
---|
[790] | 141 | ROAR_ERR("Can not start playback"); |
---|
| 142 | return YIDNULL; |
---|
| 143 | } |
---|
[343] | 144 | |
---|
[794] | 145 | return con->prev_generated_yid = ROARYIFF_ROAR2YID(stream->id); |
---|
[343] | 146 | } |
---|
| 147 | |
---|
| 148 | void YDestroyPlaySoundObject(YConnection *con, YID yid) { |
---|
| 149 | struct roar_connection rcon; |
---|
| 150 | |
---|
| 151 | if ( !con ) |
---|
| 152 | return ; |
---|
| 153 | |
---|
| 154 | if ( yid == YIDNULL ) |
---|
| 155 | return; |
---|
| 156 | |
---|
[1660] | 157 | roar_connect_fh(&rcon, con->fd); |
---|
[343] | 158 | |
---|
| 159 | roar_kick(&rcon, ROAR_OT_STREAM, ROARYIFF_YID2ROAR(yid)); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | //ll |
---|