1 | //emul_rsound.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
5 | * |
---|
6 | * This file is part of roard 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 | * RoarAudio 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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
29 | |
---|
30 | static int emul_rsound_lastcon = -1; |
---|
31 | |
---|
32 | int emul_rsound_new_client (int client, int data); |
---|
33 | |
---|
34 | int emul_rsound_on_connect (int fh, struct roard_listen * lsock) { |
---|
35 | int oldfh = emul_rsound_lastcon; |
---|
36 | int client; |
---|
37 | union { |
---|
38 | int32_t i[2]; |
---|
39 | char c[8]; |
---|
40 | } buf; |
---|
41 | |
---|
42 | if ( emul_rsound_lastcon == -1 ) { |
---|
43 | emul_rsound_lastcon = fh; |
---|
44 | return -2; |
---|
45 | } else { |
---|
46 | emul_rsound_lastcon = -1; |
---|
47 | |
---|
48 | client = clients_new(); |
---|
49 | |
---|
50 | if ( client == -1 ) |
---|
51 | return -1; |
---|
52 | |
---|
53 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
54 | ROAR_ERR("net_get_new_client(void): Can not set client's fh"); |
---|
55 | |
---|
56 | clients_delete(client); |
---|
57 | close(oldfh); |
---|
58 | close(fh); |
---|
59 | |
---|
60 | ROAR_DBG("net_get_new_client(void) = -1"); |
---|
61 | return -1; |
---|
62 | } |
---|
63 | |
---|
64 | if ( emul_rsound_new_client(client, oldfh) == -1 ) { |
---|
65 | clients_delete(client); |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | buf.i[0] = ROAR_HOST2NET32(0); |
---|
70 | buf.i[1] = ROAR_HOST2NET32(512); |
---|
71 | |
---|
72 | ROAR_NETWORK_WRITE(oldfh, buf.c, 8); |
---|
73 | |
---|
74 | return client; |
---|
75 | } |
---|
76 | |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | int emul_rsound_new_client (int client, int data) { |
---|
81 | struct roar_stream_server * ss; |
---|
82 | struct roar_stream * s; |
---|
83 | struct roar_client * c; |
---|
84 | int stream; |
---|
85 | |
---|
86 | if ( clients_get(client, &c) == -1 ) { |
---|
87 | return -1; |
---|
88 | } |
---|
89 | |
---|
90 | if ((stream = streams_new()) == -1 ) { |
---|
91 | clients_delete(client); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | if ( streams_get(stream, &ss) == -1 ) { |
---|
96 | streams_delete(stream); |
---|
97 | clients_delete(client); |
---|
98 | return -1; |
---|
99 | } |
---|
100 | |
---|
101 | s = ROAR_STREAM(ss); |
---|
102 | |
---|
103 | if ( client_stream_add(client, stream) == -1 ) { |
---|
104 | streams_delete(stream); |
---|
105 | clients_delete(client); |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
110 | |
---|
111 | ss->codec_orgi = s->info.codec = ROAR_CODEC_RIFF_WAVE; |
---|
112 | |
---|
113 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
114 | clients_delete(client); |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | if ( client_stream_exec(client, stream) == -1 ) { |
---|
120 | clients_delete(client); |
---|
121 | return -1; |
---|
122 | } |
---|
123 | */ |
---|
124 | |
---|
125 | if ( client_stream_set_fh(client, stream, data) == -1 ) { |
---|
126 | streams_delete(stream); |
---|
127 | clients_delete(client); |
---|
128 | return -1; |
---|
129 | } |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | int emul_rsound_vrecv_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
135 | ssize_t ret; |
---|
136 | int num; |
---|
137 | |
---|
138 | if ( msg == NULL || vio == NULL ) |
---|
139 | return -1; |
---|
140 | |
---|
141 | ret = roar_vio_read(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
142 | |
---|
143 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
144 | return -1; |
---|
145 | |
---|
146 | msg->header[ret] = 0; |
---|
147 | |
---|
148 | if ( msg->header[0] != 'R' || msg->header[1] != 'S' || msg->header[2] != 'D' ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | if ( sscanf(&(msg->header[3]), "%5d", &num) != 1 ) |
---|
152 | return -1; |
---|
153 | |
---|
154 | if ( num > EMUL_RSOUND_MSG_DATA_LEN ) |
---|
155 | return -1; |
---|
156 | |
---|
157 | msg->datalen = num; |
---|
158 | |
---|
159 | ret = roar_vio_read(vio, msg->data, num); |
---|
160 | |
---|
161 | if ( ret != (ssize_t)num ) |
---|
162 | return -1; |
---|
163 | |
---|
164 | msg->data[num] = 0; |
---|
165 | |
---|
166 | msg->datasp = msg->data; |
---|
167 | msg->dataslen = msg->datalen; |
---|
168 | |
---|
169 | for (num = 0; msg->data[num] == ' '; num++) { |
---|
170 | msg->datasp++; |
---|
171 | msg->dataslen--; |
---|
172 | } |
---|
173 | |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | int emul_rsound_vsend_msg(struct emul_rsound_msg * msg, struct roar_vio_calls * vio) { |
---|
178 | ssize_t ret; |
---|
179 | |
---|
180 | if ( msg == NULL || vio == NULL ) |
---|
181 | return -1; |
---|
182 | |
---|
183 | snprintf(msg->header, EMUL_RSOUND_MSG_HEADER_LEN+1, "RSD%5d", msg->datalen); |
---|
184 | |
---|
185 | ret = roar_vio_write(vio, msg->header, EMUL_RSOUND_MSG_HEADER_LEN); |
---|
186 | |
---|
187 | if ( ret != EMUL_RSOUND_MSG_HEADER_LEN ) |
---|
188 | return -1; |
---|
189 | |
---|
190 | ret = roar_vio_write(vio, msg->data, msg->datalen); |
---|
191 | |
---|
192 | if ( ret != (ssize_t)msg->datalen ) |
---|
193 | return -1; |
---|
194 | |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | int emul_rsound_check_client(int client, struct roar_vio_calls * vio) { |
---|
199 | struct roar_vio_calls rvio; |
---|
200 | struct emul_rsound_msg msg; |
---|
201 | |
---|
202 | if ( vio == NULL ) { |
---|
203 | vio = &rvio; |
---|
204 | roar_vio_open_fh_socket(vio, clients_get_fh(client)); |
---|
205 | } |
---|
206 | |
---|
207 | if ( emul_rsound_vrecv_msg(&msg, vio) == -1 ) |
---|
208 | return clients_delete(client); |
---|
209 | |
---|
210 | if ( !strncmp(msg.datasp, "INFO", 4) ) { |
---|
211 | // TODO: add support for INFO |
---|
212 | return clients_delete(client); |
---|
213 | } else if ( !strncmp(msg.datasp, "NULL", 4) ) { |
---|
214 | // NULL is simular to NOOP |
---|
215 | return 0; |
---|
216 | } else if ( !strncmp(msg.datasp, "STOP", 4) ) { |
---|
217 | // This is quit. |
---|
218 | return clients_delete(client); |
---|
219 | } else { |
---|
220 | return clients_delete(client); |
---|
221 | } |
---|
222 | } |
---|
223 | #endif |
---|
224 | |
---|
225 | //ll |
---|