28 |
|
|
29 |
// Local prototypes. |
// Local prototypes. |
30 |
|
|
31 |
static void _lscp_client_set_result (lscp_client_t *pClient, char *pszResult, int iErrno); |
static void _lscp_client_evt_proc (void *pvClient); |
|
static void _lscp_client_udp_proc (void *pvClient); |
|
32 |
|
|
33 |
|
static lscp_status_t _lscp_client_evt_connect (lscp_client_t *pClient); |
34 |
//------------------------------------------------------------------------- |
static lscp_status_t _lscp_client_evt_request (lscp_client_t *pClient, int iSubscribe, lscp_event_t event); |
|
// Helper functions. |
|
|
|
|
|
// Result buffer internal settler. |
|
|
static void _lscp_client_set_result ( lscp_client_t *pClient, char *pszResult, int iErrno ) |
|
|
{ |
|
|
if (pClient->pszResult) |
|
|
free(pClient->pszResult); |
|
|
pClient->pszResult = NULL; |
|
|
|
|
|
pClient->iErrno = iErrno; |
|
|
|
|
|
if (pszResult) |
|
|
pClient->pszResult = strdup(lscp_ltrim(pszResult)); |
|
|
} |
|
35 |
|
|
36 |
|
|
37 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
38 |
// UDP service (datagram oriented). |
// Event service (datagram oriented). |
39 |
|
|
40 |
static void _lscp_client_udp_proc ( void *pvClient ) |
static void _lscp_client_evt_proc ( void *pvClient ) |
41 |
{ |
{ |
42 |
lscp_client_t *pClient = (lscp_client_t *) pvClient; |
lscp_client_t *pClient = (lscp_client_t *) pvClient; |
|
struct sockaddr_in addr; |
|
|
int cAddr; |
|
43 |
char achBuffer[LSCP_BUFSIZ]; |
char achBuffer[LSCP_BUFSIZ]; |
44 |
int cchBuffer; |
int cchBuffer; |
45 |
const char *pszSeps = " \r\n"; |
const char *pszSeps = ":\r\n"; |
46 |
char *pszToken; |
char *pszToken; |
47 |
char *pch; |
char *pch; |
48 |
|
int cchToken; |
49 |
|
lscp_event_t event = LSCP_EVENT_NONE; |
50 |
|
|
51 |
#ifdef DEBUG |
#ifdef DEBUG |
52 |
fprintf(stderr, "_lscp_client_udp_proc: Client waiting for events.\n"); |
fprintf(stderr, "_lscp_client_evt_proc: Client waiting for events.\n"); |
53 |
#endif |
#endif |
54 |
|
|
55 |
while (pClient->udp.iState) { |
while (pClient->evt.iState) { |
56 |
cAddr = sizeof(struct sockaddr_in); |
// Wait for event... |
57 |
cchBuffer = recvfrom(pClient->udp.sock, achBuffer, sizeof(achBuffer), 0, (struct sockaddr *) &addr, &cAddr); |
cchBuffer = recv(pClient->evt.sock, achBuffer, sizeof(achBuffer), 0); |
58 |
if (cchBuffer > 0) { |
if (cchBuffer > 0) { |
59 |
#ifdef DEBUG |
// Make sure received buffer it's null terminated. |
60 |
lscp_socket_trace("_lscp_client_udp_proc: recvfrom", &addr, achBuffer, cchBuffer); |
achBuffer[cchBuffer] = (char) 0; |
61 |
#endif |
// Parse for the notification event message... |
62 |
if (strncasecmp(achBuffer, "PING ", 5) == 0) { |
pszToken = lscp_strtok(achBuffer, pszSeps, &(pch)); // Have "NOTIFY". |
63 |
// Make sure received buffer it's null terminated. |
if (strcasecmp(pszToken, "NOTIFY") == 0) { |
64 |
achBuffer[cchBuffer] = (char) 0; |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
65 |
lscp_strtok(achBuffer, pszSeps, &(pch)); // Skip "PING" |
if (strcasecmp(pszToken, "CHANNELS") == 0) |
66 |
lscp_strtok(NULL, pszSeps, &(pch)); // Skip port (must be the same as in addr) |
event = LSCP_EVENT_CHANNELS; |
67 |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); // Have session-id. |
else if (strcasecmp(pszToken, "VOICE_COUNT") == 0) |
68 |
if (pszToken) { |
event = LSCP_EVENT_VOICE_COUNT; |
69 |
// Set now client's session-id, if not already |
else if (strcasecmp(pszToken, "STREAM_COUNT") == 0) |
70 |
if (pClient->sessid == NULL) |
event = LSCP_EVENT_STREAM_COUNT; |
71 |
pClient->sessid = strdup(pszToken); |
else if (strcasecmp(pszToken, "BUFFER_FILL") == 0) |
72 |
if (pClient->sessid && strcmp(pszToken, pClient->sessid) == 0) { |
event = LSCP_EVENT_BUFFER_FILL; |
73 |
sprintf(achBuffer, "PONG %s\r\n", pClient->sessid); |
else if (strcasecmp(pszToken, "CHANNEL_INFO") == 0) |
74 |
cchBuffer = strlen(achBuffer); |
event = LSCP_EVENT_CHANNEL_INFO; |
75 |
if (sendto(pClient->udp.sock, achBuffer, cchBuffer, 0, (struct sockaddr *) &addr, cAddr) < cchBuffer) |
else if (strcasecmp(pszToken, "MISCELLANEOUS") == 0) |
76 |
lscp_socket_perror("_lscp_client_udp_proc: sendto"); |
event = LSCP_EVENT_MISCELLANEOUS; |
77 |
#ifdef DEBUG |
// And pick the rest of data... |
78 |
fprintf(stderr, "> %s", achBuffer); |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
79 |
#endif |
cchToken = (pszToken == NULL ? 0 : strlen(pszToken)); |
80 |
|
// Double-check if we're really up to it... |
81 |
|
if (pClient->events & event) { |
82 |
|
// Invoke the client event callback... |
83 |
|
if ((*pClient->pfnCallback)( |
84 |
|
pClient, |
85 |
|
event, |
86 |
|
pszToken, |
87 |
|
cchToken, |
88 |
|
pClient->pvData) != LSCP_OK) { |
89 |
|
pClient->evt.iState = 0; |
90 |
} |
} |
91 |
} |
} |
|
// Done with life proof. |
|
|
} else { |
|
|
// |
|
|
if ((*pClient->pfnCallback)( |
|
|
pClient, |
|
|
achBuffer, |
|
|
cchBuffer, |
|
|
pClient->pvData) != LSCP_OK) { |
|
|
pClient->udp.iState = 0; |
|
|
} |
|
92 |
} |
} |
93 |
} else { |
} else { |
94 |
lscp_socket_perror("_lscp_client_udp_proc: recvfrom"); |
lscp_socket_perror("_lscp_client_evt_proc: recv"); |
95 |
pClient->udp.iState = 0; |
pClient->evt.iState = 0; |
96 |
} |
} |
97 |
} |
} |
98 |
|
|
99 |
#ifdef DEBUG |
#ifdef DEBUG |
100 |
fprintf(stderr, "_lscp_client_udp_proc: Client closing.\n"); |
fprintf(stderr, "_lscp_client_evt_proc: Client closing.\n"); |
101 |
|
#endif |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
//------------------------------------------------------------------------- |
106 |
|
// Event subscription helpers. |
107 |
|
|
108 |
|
// Open the event service socket connection. |
109 |
|
static lscp_status_t _lscp_client_evt_connect ( lscp_client_t *pClient ) |
110 |
|
{ |
111 |
|
lscp_socket_t sock; |
112 |
|
struct sockaddr_in addr; |
113 |
|
int cAddr; |
114 |
|
#if defined(WIN32) |
115 |
|
int iSockOpt = (-1); |
116 |
|
#endif |
117 |
|
|
118 |
|
// Prepare the event connection socket... |
119 |
|
sock = socket(AF_INET, SOCK_STREAM, 0); |
120 |
|
if (sock == INVALID_SOCKET) { |
121 |
|
lscp_socket_perror("_lscp_client_evt_connect: socket"); |
122 |
|
return LSCP_FAILED; |
123 |
|
} |
124 |
|
|
125 |
|
#if defined(WIN32) |
126 |
|
if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) |
127 |
|
lscp_socket_perror("lscp_client_evt_connect: setsockopt(SO_DONTLINGER)"); |
128 |
|
#endif |
129 |
|
|
130 |
|
#ifdef DEBUG |
131 |
|
lscp_socket_getopts("_lscp_client_evt_connect:", sock); |
132 |
#endif |
#endif |
133 |
|
|
134 |
|
// Use same address of the command connection. |
135 |
|
cAddr = sizeof(struct sockaddr_in); |
136 |
|
memmove((char *) &addr, &(pClient->cmd.addr), cAddr); |
137 |
|
|
138 |
|
// Start the connection... |
139 |
|
if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { |
140 |
|
lscp_socket_perror("_lscp_client_evt_connect: connect"); |
141 |
|
closesocket(sock); |
142 |
|
return LSCP_FAILED; |
143 |
|
} |
144 |
|
|
145 |
|
// Set our socket agent struct... |
146 |
|
lscp_socket_agent_init(&(pClient->evt), sock, &addr, cAddr); |
147 |
|
|
148 |
|
// And finally the service thread... |
149 |
|
return lscp_socket_agent_start(&(pClient->evt), _lscp_client_evt_proc, pClient, 0); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
// Subscribe to a single event. |
154 |
|
static lscp_status_t _lscp_client_evt_request ( lscp_client_t *pClient, int iSubscribe, lscp_event_t event ) |
155 |
|
{ |
156 |
|
char *pszEvent; |
157 |
|
char szQuery[LSCP_BUFSIZ]; |
158 |
|
int cchQuery; |
159 |
|
|
160 |
|
if (pClient == NULL) |
161 |
|
return LSCP_FAILED; |
162 |
|
|
163 |
|
// Which (single) event? |
164 |
|
switch (event) { |
165 |
|
case LSCP_EVENT_CHANNELS: |
166 |
|
pszEvent = "CHANNELS"; |
167 |
|
break; |
168 |
|
case LSCP_EVENT_VOICE_COUNT: |
169 |
|
pszEvent = "VOICE_COUNT"; |
170 |
|
break; |
171 |
|
case LSCP_EVENT_STREAM_COUNT: |
172 |
|
pszEvent = "STREAM_COUNT"; |
173 |
|
break; |
174 |
|
case LSCP_EVENT_BUFFER_FILL: |
175 |
|
pszEvent = "BUFFER_FILL"; |
176 |
|
break; |
177 |
|
case LSCP_EVENT_CHANNEL_INFO: |
178 |
|
pszEvent = "CHANNEL_INFO"; |
179 |
|
break; |
180 |
|
case LSCP_EVENT_MISCELLANEOUS: |
181 |
|
pszEvent = "CHANNEL_INFO"; |
182 |
|
break; |
183 |
|
default: |
184 |
|
return LSCP_FAILED; |
185 |
|
} |
186 |
|
|
187 |
|
// Build the query string... |
188 |
|
cchQuery = sprintf(szQuery, "%sSUBSCRIBE %s\n\n", (iSubscribe == 0 ? "UN" : ""), pszEvent); |
189 |
|
// Just send data, forget result... |
190 |
|
if (send(pClient->evt.sock, szQuery, cchQuery, 0) < cchQuery) { |
191 |
|
lscp_socket_perror("_lscp_client_evt_request: send"); |
192 |
|
return LSCP_FAILED; |
193 |
|
} |
194 |
|
|
195 |
|
// Update as naively as we can... |
196 |
|
if (iSubscribe) |
197 |
|
pClient->events |= event; |
198 |
|
else |
199 |
|
pClient->events &= ~event; |
200 |
|
|
201 |
|
return LSCP_OK; |
202 |
} |
} |
203 |
|
|
204 |
|
|
240 |
lscp_socket_t sock; |
lscp_socket_t sock; |
241 |
struct sockaddr_in addr; |
struct sockaddr_in addr; |
242 |
int cAddr; |
int cAddr; |
243 |
|
#if defined(WIN32) |
244 |
int iSockOpt = (-1); |
int iSockOpt = (-1); |
245 |
|
#endif |
246 |
|
|
247 |
if (pfnCallback == NULL) { |
if (pfnCallback == NULL) { |
248 |
fprintf(stderr, "lscp_client_create: Invalid client callback function.\n"); |
fprintf(stderr, "lscp_client_create: Invalid client callback function.\n"); |
251 |
|
|
252 |
pHost = gethostbyname(pszHost); |
pHost = gethostbyname(pszHost); |
253 |
if (pHost == NULL) { |
if (pHost == NULL) { |
254 |
lscp_socket_perror("lscp_client_create: gethostbyname"); |
lscp_socket_herror("lscp_client_create: gethostbyname"); |
255 |
return NULL; |
return NULL; |
256 |
} |
} |
257 |
|
|
271 |
fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort); |
fprintf(stderr, "lscp_client_create: pClient=%p: pszHost=%s iPort=%d.\n", pClient, pszHost, iPort); |
272 |
#endif |
#endif |
273 |
|
|
274 |
// Prepare the TCP connection socket... |
// Prepare the command connection socket... |
275 |
|
|
276 |
sock = socket(AF_INET, SOCK_STREAM, 0); |
sock = socket(AF_INET, SOCK_STREAM, 0); |
277 |
if (sock == INVALID_SOCKET) { |
if (sock == INVALID_SOCKET) { |
278 |
lscp_socket_perror("lscp_client_create: tcp: socket"); |
lscp_socket_perror("lscp_client_create: cmd: socket"); |
279 |
free(pClient); |
free(pClient); |
280 |
return NULL; |
return NULL; |
281 |
} |
} |
282 |
|
|
283 |
#if defined(WIN32) |
#if defined(WIN32) |
284 |
if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) |
if (setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) |
285 |
lscp_socket_perror("lscp_client_create: tcp: setsockopt(SO_DONTLINGER)"); |
lscp_socket_perror("lscp_client_create: cmd: setsockopt(SO_DONTLINGER)"); |
286 |
#endif |
#endif |
287 |
|
|
288 |
#ifdef DEBUG |
#ifdef DEBUG |
289 |
lscp_socket_getopts("lscp_client_create: tcp", sock); |
lscp_socket_getopts("lscp_client_create: cmd", sock); |
290 |
#endif |
#endif |
291 |
|
|
292 |
cAddr = sizeof(struct sockaddr_in); |
cAddr = sizeof(struct sockaddr_in); |
296 |
addr.sin_port = htons((short) iPort); |
addr.sin_port = htons((short) iPort); |
297 |
|
|
298 |
if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { |
if (connect(sock, (struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { |
299 |
lscp_socket_perror("lscp_client_create: tcp: connect"); |
lscp_socket_perror("lscp_client_create: cmd: connect"); |
300 |
closesocket(sock); |
closesocket(sock); |
301 |
free(pClient); |
free(pClient); |
302 |
return NULL; |
return NULL; |
303 |
} |
} |
304 |
|
|
305 |
lscp_socket_agent_init(&(pClient->tcp), sock, &addr, cAddr); |
// Initialize the command socket agent struct... |
306 |
|
lscp_socket_agent_init(&(pClient->cmd), sock, &addr, cAddr); |
307 |
|
|
308 |
#ifdef DEBUG |
#ifdef DEBUG |
309 |
fprintf(stderr, "lscp_client_create: tcp: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->tcp.sock, inet_ntoa(pClient->tcp.addr.sin_addr), ntohs(pClient->tcp.addr.sin_port)); |
fprintf(stderr, "lscp_client_create: cmd: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->cmd.sock, inet_ntoa(pClient->cmd.addr.sin_addr), ntohs(pClient->cmd.addr.sin_port)); |
310 |
#endif |
#endif |
311 |
|
|
312 |
// Prepare the UDP datagram service socket... |
// Initialize the event service socket struct... |
313 |
|
lscp_socket_agent_init(&(pClient->evt), INVALID_SOCKET, NULL, 0); |
314 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
// No events subscribed, yet. |
315 |
if (sock == INVALID_SOCKET) { |
pClient->events = LSCP_EVENT_NONE; |
|
lscp_socket_perror("lscp_client_create: udp: socket"); |
|
|
lscp_socket_agent_free(&(pClient->tcp)); |
|
|
free(pClient); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &iSockOpt, sizeof(int)) == SOCKET_ERROR) |
|
|
lscp_socket_perror("lscp_client_create: udp: setsockopt(SO_REUSEADDR)"); |
|
|
|
|
|
#ifdef DEBUG |
|
|
lscp_socket_getopts("lscp_client_create: udp", sock); |
|
|
#endif |
|
|
|
|
|
cAddr = sizeof(struct sockaddr_in); |
|
|
memset((char *) &addr, 0, cAddr); |
|
|
addr.sin_family = AF_INET; |
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
|
addr.sin_port = htons(0); |
|
|
|
|
|
if (bind(sock, (const struct sockaddr *) &addr, cAddr) == SOCKET_ERROR) { |
|
|
lscp_socket_perror("lscp_client_create: udp: bind"); |
|
|
lscp_socket_agent_free(&(pClient->tcp)); |
|
|
closesocket(sock); |
|
|
free(pClient); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
if (getsockname(sock, (struct sockaddr *) &addr, &cAddr) == SOCKET_ERROR) { |
|
|
lscp_socket_perror("lscp_client_create: udp: getsockname"); |
|
|
lscp_socket_agent_free(&(pClient->tcp)); |
|
|
closesocket(sock); |
|
|
free(pClient); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
lscp_socket_agent_init(&(pClient->udp), sock, &addr, cAddr); |
|
|
|
|
|
#ifdef DEBUG |
|
|
fprintf(stderr, "lscp_client_create: udp: pClient=%p: sock=%d addr=%s port=%d.\n", pClient, pClient->udp.sock, inet_ntoa(pClient->udp.addr.sin_addr), ntohs(pClient->udp.addr.sin_port)); |
|
|
#endif |
|
|
|
|
|
// No session id, yet. |
|
|
pClient->sessid = NULL; |
|
316 |
// Initialize cached members. |
// Initialize cached members. |
317 |
pClient->audio_drivers = NULL; |
pClient->audio_drivers = NULL; |
318 |
pClient->midi_drivers = NULL; |
pClient->midi_drivers = NULL; |
319 |
|
pClient->audio_devices = NULL; |
320 |
|
pClient->midi_devices = NULL; |
321 |
pClient->engines = NULL; |
pClient->engines = NULL; |
322 |
|
pClient->channels = NULL; |
323 |
lscp_driver_info_init(&(pClient->audio_info)); |
lscp_driver_info_init(&(pClient->audio_info)); |
324 |
lscp_driver_info_init(&(pClient->midi_info)); |
lscp_driver_info_init(&(pClient->midi_info)); |
325 |
lscp_engine_info_init(&(pClient->engine_info)); |
lscp_engine_info_init(&(pClient->engine_info)); |
336 |
// Initialize the transaction mutex. |
// Initialize the transaction mutex. |
337 |
lscp_mutex_init(pClient->mutex); |
lscp_mutex_init(pClient->mutex); |
338 |
|
|
|
// Now's finally time to startup threads... |
|
|
// UDP service thread... |
|
|
if (lscp_socket_agent_start(&(pClient->udp), _lscp_client_udp_proc, pClient, 0) != LSCP_OK) { |
|
|
lscp_socket_agent_free(&(pClient->tcp)); |
|
|
lscp_socket_agent_free(&(pClient->udp)); |
|
|
lscp_mutex_destroy(pClient->mutex); |
|
|
free(pClient); |
|
|
return NULL; |
|
|
} |
|
|
|
|
339 |
// Finally we've some success... |
// Finally we've some success... |
340 |
return pClient; |
return pClient; |
341 |
} |
} |
355 |
fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient); |
fprintf(stderr, "lscp_client_join: pClient=%p.\n", pClient); |
356 |
#endif |
#endif |
357 |
|
|
358 |
// lscp_socket_agent_join(&(pClient->udp)); |
// lscp_socket_agent_join(&(pClient->evt)); |
359 |
lscp_socket_agent_join(&(pClient->tcp)); |
lscp_socket_agent_join(&(pClient->cmd)); |
360 |
|
|
361 |
return LSCP_OK; |
return LSCP_OK; |
362 |
} |
} |
378 |
fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient); |
fprintf(stderr, "lscp_client_destroy: pClient=%p.\n", pClient); |
379 |
#endif |
#endif |
380 |
|
|
381 |
// Free session-id, if any. |
// Lock this section up. |
382 |
if (pClient->sessid) |
lscp_mutex_lock(pClient->mutex); |
383 |
free(pClient->sessid); |
|
|
pClient->sessid = NULL; |
|
384 |
// Free up all cached members. |
// Free up all cached members. |
385 |
lscp_channel_info_reset(&(pClient->channel_info)); |
lscp_channel_info_reset(&(pClient->channel_info)); |
386 |
lscp_engine_info_reset(&(pClient->engine_info)); |
lscp_engine_info_reset(&(pClient->engine_info)); |
389 |
// Free available engine table. |
// Free available engine table. |
390 |
lscp_szsplit_destroy(pClient->audio_drivers); |
lscp_szsplit_destroy(pClient->audio_drivers); |
391 |
lscp_szsplit_destroy(pClient->midi_drivers); |
lscp_szsplit_destroy(pClient->midi_drivers); |
392 |
|
lscp_isplit_destroy(pClient->audio_devices); |
393 |
|
lscp_isplit_destroy(pClient->midi_devices); |
394 |
lscp_szsplit_destroy(pClient->engines); |
lscp_szsplit_destroy(pClient->engines); |
395 |
|
lscp_isplit_destroy(pClient->channels); |
396 |
// Make them null. |
// Make them null. |
397 |
pClient->audio_drivers = NULL; |
pClient->audio_drivers = NULL; |
398 |
pClient->midi_drivers = NULL; |
pClient->midi_drivers = NULL; |
399 |
pClient->engines = NULL; |
pClient->engines = NULL; |
400 |
// Free result error stuff. |
// Free result error stuff. |
401 |
_lscp_client_set_result(pClient, NULL, 0); |
lscp_client_set_result(pClient, NULL, 0); |
402 |
// Frre stream usage stuff. |
// Frre stream usage stuff. |
403 |
if (pClient->buffer_fill) |
if (pClient->buffer_fill) |
404 |
free(pClient->buffer_fill); |
free(pClient->buffer_fill); |
407 |
pClient->iTimeout = 0; |
pClient->iTimeout = 0; |
408 |
|
|
409 |
// Free socket agents. |
// Free socket agents. |
410 |
lscp_socket_agent_free(&(pClient->udp)); |
lscp_socket_agent_free(&(pClient->evt)); |
411 |
lscp_socket_agent_free(&(pClient->tcp)); |
lscp_socket_agent_free(&(pClient->cmd)); |
412 |
|
|
413 |
// Last but not least, free good ol'transaction mutex. |
// Last but not least, free good ol'transaction mutex. |
414 |
|
lscp_mutex_unlock(pClient->mutex); |
415 |
lscp_mutex_destroy(pClient->mutex); |
lscp_mutex_destroy(pClient->mutex); |
416 |
|
|
417 |
free(pClient); |
free(pClient); |
459 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
460 |
// Client common protocol functions. |
// Client common protocol functions. |
461 |
|
|
|
|
|
462 |
/** |
/** |
463 |
* Submit a command query line string to the server. The query string |
* Submit a command query line string to the server. The query string |
464 |
* must be cr/lf and null terminated. Besides the return code, the |
* must be cr/lf and null terminated. Besides the return code, the |
474 |
*/ |
*/ |
475 |
lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery ) |
lscp_status_t lscp_client_query ( lscp_client_t *pClient, const char *pszQuery ) |
476 |
{ |
{ |
477 |
fd_set fds; // File descriptor list for select(). |
lscp_status_t ret; |
478 |
int fd, fdmax; // Maximum file descriptor number. |
|
|
struct timeval tv; // For specifying a timeout value. |
|
|
int iSelect; // Holds select return status. |
|
|
int iTimeout; |
|
|
int cchQuery; |
|
|
char achResult[LSCP_BUFSIZ]; |
|
|
int cchResult; |
|
|
const char *pszSeps = ":[]"; |
|
|
char *pszResult; |
|
|
char *pszToken; |
|
|
char *pch; |
|
|
int iErrno; |
|
|
|
|
|
lscp_status_t ret = LSCP_FAILED; |
|
|
|
|
|
if (pClient == NULL) |
|
|
return ret; |
|
|
|
|
479 |
// Lock this section up. |
// Lock this section up. |
480 |
lscp_mutex_lock(pClient->mutex); |
lscp_mutex_lock(pClient->mutex); |
481 |
|
|
482 |
pszResult = NULL; |
// Just make the now guarded call. |
483 |
iErrno = -1; |
ret = lscp_client_call(pClient, pszQuery); |
484 |
|
|
485 |
// Send data, and then, wait for the result... |
// Unlock this section down. |
|
cchQuery = strlen(pszQuery); |
|
|
if (send(pClient->tcp.sock, pszQuery, cchQuery, 0) < cchQuery) { |
|
|
lscp_socket_perror("lscp_client_query: send"); |
|
|
lscp_mutex_unlock(pClient->mutex); |
|
|
return ret; |
|
|
} |
|
|
|
|
|
// Prepare for waiting on select... |
|
|
fd = (int) pClient->tcp.sock; |
|
|
FD_ZERO(&fds); |
|
|
FD_SET((unsigned int) fd, &fds); |
|
|
fdmax = fd; |
|
|
|
|
|
// Use the timeout select feature... |
|
|
iTimeout = pClient->iTimeout; |
|
|
if (iTimeout > 1000) { |
|
|
tv.tv_sec = iTimeout / 1000; |
|
|
iTimeout -= tv.tv_sec * 1000; |
|
|
} |
|
|
else tv.tv_sec = 0; |
|
|
tv.tv_usec = iTimeout * 1000; |
|
|
|
|
|
// Wait for event... |
|
|
iSelect = select(fdmax + 1, &fds, NULL, NULL, &tv); |
|
|
if (iSelect > 0 && FD_ISSET(fd, &fds)) { |
|
|
// May recv now... |
|
|
cchResult = recv(pClient->tcp.sock, achResult, sizeof(achResult), 0); |
|
|
if (cchResult > 0) { |
|
|
// Assume early success. |
|
|
ret = LSCP_OK; |
|
|
// Always force the result to be null terminated (and trim trailing CRLFs)! |
|
|
while (cchResult > 0 && (achResult[cchResult - 1] == '\n' || achResult[cchResult- 1] == '\r')) |
|
|
cchResult--; |
|
|
achResult[cchResult] = (char) 0; |
|
|
// Check if the response it's an error or warning message. |
|
|
if (strncasecmp(achResult, "WRN:", 4) == 0) |
|
|
ret = LSCP_WARNING; |
|
|
else if (strncasecmp(achResult, "ERR:", 4) == 0) |
|
|
ret = LSCP_ERROR; |
|
|
// So we got a result... |
|
|
if (ret == LSCP_OK) { |
|
|
// Reset errno in case of success. |
|
|
iErrno = 0; |
|
|
// Is it a special successful response? |
|
|
if (strncasecmp(achResult, "OK[", 3) == 0) { |
|
|
// Parse the OK message, get the return string under brackets... |
|
|
pszToken = lscp_strtok(achResult, pszSeps, &(pch)); |
|
|
if (pszToken) |
|
|
pszResult = lscp_strtok(NULL, pszSeps, &(pch)); |
|
|
} |
|
|
else pszResult = achResult; |
|
|
// The result string is now set to the command response, if any. |
|
|
} else { |
|
|
// Parse the error/warning message, skip first colon... |
|
|
pszToken = lscp_strtok(achResult, pszSeps, &(pch)); |
|
|
if (pszToken) { |
|
|
// Get the error number... |
|
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
|
|
if (pszToken) { |
|
|
iErrno = atoi(pszToken); |
|
|
// And make the message text our final result. |
|
|
pszResult = lscp_strtok(NULL, pszSeps, &(pch)); |
|
|
} |
|
|
} |
|
|
// The result string is set to the error/warning message text. |
|
|
} |
|
|
} |
|
|
else if (cchResult == 0) { |
|
|
// Fake a result message. |
|
|
pszResult = "Server terminated the connection"; |
|
|
ret = LSCP_QUIT; |
|
|
} |
|
|
else lscp_socket_perror("lscp_client_query: recv"); |
|
|
} // Check if select has timed out. |
|
|
else if (iSelect == 0) { |
|
|
// Fake a result message. |
|
|
pszResult = "Timeout during receive operation"; |
|
|
ret = LSCP_TIMEOUT; |
|
|
} |
|
|
else lscp_socket_perror("lscp_client_query: select"); |
|
|
|
|
|
// Make the result official... |
|
|
_lscp_client_set_result(pClient, pszResult, iErrno); |
|
|
|
|
|
// Can go on with it... |
|
486 |
lscp_mutex_unlock(pClient->mutex); |
lscp_mutex_unlock(pClient->mutex); |
487 |
|
|
488 |
return ret; |
return ret; |
489 |
} |
} |
490 |
|
|
|
|
|
491 |
/** |
/** |
492 |
* Get the last received result string. In case of error or warning, |
* Get the last received result string. In case of error or warning, |
493 |
* this is the text of the error or warning message issued. |
* this is the text of the error or warning message issued. |
527 |
// Client registration protocol functions. |
// Client registration protocol functions. |
528 |
|
|
529 |
/** |
/** |
530 |
* Register frontend for receiving UDP event messages: |
* Register frontend for receiving event messages: |
531 |
* SUBSCRIBE NOTIFICATION <udp-port> |
* SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL |
532 |
|
* | CHANNEL_INFO | MISCELLANEOUS |
533 |
* |
* |
534 |
* @param pClient Pointer to client instance structure. |
* @param pClient Pointer to client instance structure. |
535 |
|
* @param events Bit-wise OR'ed event flags to subscribe. |
536 |
* |
* |
537 |
* @returns LSCP_OK on success, LSCP_FAILED otherwise. |
* @returns LSCP_OK on success, LSCP_FAILED otherwise. |
538 |
*/ |
*/ |
539 |
lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient ) |
lscp_status_t lscp_client_subscribe ( lscp_client_t *pClient, lscp_event_t events ) |
540 |
{ |
{ |
541 |
lscp_status_t ret; |
lscp_status_t ret = LSCP_FAILED; |
|
char szQuery[LSCP_BUFSIZ]; |
|
|
const char *pszResult; |
|
|
const char *pszSeps = "[]"; |
|
|
char *pszToken; |
|
|
char *pch; |
|
542 |
|
|
543 |
if (pClient == NULL || pClient->sessid) |
if (pClient == NULL) |
544 |
return LSCP_FAILED; |
return LSCP_FAILED; |
545 |
|
|
546 |
sprintf(szQuery, "SUBSCRIBE NOTIFICATION %d\r\n", ntohs(pClient->udp.addr.sin_port)); |
// Lock this section up. |
547 |
ret = lscp_client_query(pClient, szQuery); |
lscp_mutex_lock(pClient->mutex); |
548 |
if (ret == LSCP_OK) { |
|
549 |
pszResult = lscp_client_get_result(pClient); |
// If applicable, start the alternate connection... |
550 |
#ifdef DEBUG |
if (pClient->events == LSCP_EVENT_NONE) |
551 |
fprintf(stderr, "lscp_client_subscribe: %s\n", pszResult); |
ret = _lscp_client_evt_connect(pClient); |
552 |
#endif |
|
553 |
// Check for the session-id on "OK[sessid]" response. |
// Send the subscription commands. |
554 |
pszToken = lscp_strtok(pszResult, pszSeps, &(pch)); |
if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS)) |
555 |
if (pszToken && strcasecmp(pszToken, "OK") == 0) { |
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNELS); |
556 |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) |
557 |
if (pszToken) |
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_VOICE_COUNT); |
558 |
pClient->sessid = strdup(pszToken); |
if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) |
559 |
} |
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_STREAM_COUNT); |
560 |
} |
if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) |
561 |
|
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_BUFFER_FILL); |
562 |
|
if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) |
563 |
|
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_CHANNEL_INFO); |
564 |
|
if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) |
565 |
|
ret = _lscp_client_evt_request(pClient, 1, LSCP_EVENT_MISCELLANEOUS); |
566 |
|
|
567 |
|
// Unlock this section down. |
568 |
|
lscp_mutex_unlock(pClient->mutex); |
569 |
|
|
570 |
return ret; |
return ret; |
571 |
} |
} |
572 |
|
|
573 |
|
|
574 |
/** |
/** |
575 |
* Deregister frontend for not receiving UDP event messages anymore: |
* Deregister frontend from receiving UDP event messages anymore: |
576 |
* UNSUBSCRIBE NOTIFICATION <session-id> |
* SUBSCRIBE CHANNELS | VOICE_COUNT | STREAM_COUNT | BUFFER_FILL |
577 |
|
* | CHANNEL_INFO | MISCELLANEOUS |
578 |
* |
* |
579 |
* @param pClient Pointer to client instance structure. |
* @param pClient Pointer to client instance structure. |
580 |
|
* @param events Bit-wise OR'ed event flags to unsubscribe. |
581 |
* |
* |
582 |
* @returns LSCP_OK on success, LSCP_FAILED otherwise. |
* @returns LSCP_OK on success, LSCP_FAILED otherwise. |
583 |
*/ |
*/ |
584 |
lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient ) |
lscp_status_t lscp_client_unsubscribe ( lscp_client_t *pClient, lscp_event_t events ) |
585 |
{ |
{ |
586 |
lscp_status_t ret; |
lscp_status_t ret = LSCP_OK; |
|
char szQuery[LSCP_BUFSIZ]; |
|
587 |
|
|
588 |
if (pClient == NULL) |
if (pClient == NULL) |
589 |
return LSCP_FAILED; |
return LSCP_FAILED; |
|
if (pClient->sessid == NULL) |
|
|
return LSCP_FAILED; |
|
590 |
|
|
591 |
sprintf(szQuery, "UNSUBSCRIBE NOTIFICATION %s\n\n", pClient->sessid); |
// Lock this section up. |
592 |
ret = lscp_client_query(pClient, szQuery); |
lscp_mutex_lock(pClient->mutex); |
593 |
if (ret == LSCP_OK) { |
|
594 |
#ifdef DEBUG |
// Send the unsubscription commands. |
595 |
fprintf(stderr, "lscp_client_unsubscribe: %s\n", lscp_client_get_result(pClient)); |
if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNELS)) |
596 |
#endif |
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNELS); |
597 |
// Bail out session-id string. |
if (ret == LSCP_OK && (events & LSCP_EVENT_VOICE_COUNT)) |
598 |
free(pClient->sessid); |
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_VOICE_COUNT); |
599 |
pClient->sessid = NULL; |
if (ret == LSCP_OK && (events & LSCP_EVENT_STREAM_COUNT)) |
600 |
} |
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_STREAM_COUNT); |
601 |
|
if (ret == LSCP_OK && (events & LSCP_EVENT_BUFFER_FILL)) |
602 |
|
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_BUFFER_FILL); |
603 |
|
if (ret == LSCP_OK && (events & LSCP_EVENT_CHANNEL_INFO)) |
604 |
|
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_CHANNEL_INFO); |
605 |
|
if (ret == LSCP_OK && (events & LSCP_EVENT_MISCELLANEOUS)) |
606 |
|
ret = _lscp_client_evt_request(pClient, 0, LSCP_EVENT_MISCELLANEOUS); |
607 |
|
|
608 |
|
// If necessary, close the alternate connection... |
609 |
|
if (pClient->events == LSCP_EVENT_NONE) |
610 |
|
lscp_socket_agent_free(&(pClient->evt)); |
611 |
|
|
612 |
|
// Unlock this section down. |
613 |
|
lscp_mutex_unlock(pClient->mutex); |
614 |
|
|
615 |
return ret; |
return ret; |
616 |
} |
} |
637 |
if (pszFileName == NULL || iSamplerChannel < 0) |
if (pszFileName == NULL || iSamplerChannel < 0) |
638 |
return LSCP_FAILED; |
return LSCP_FAILED; |
639 |
|
|
640 |
sprintf(szQuery, "LOAD INSTRUMENT %s %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); |
sprintf(szQuery, "LOAD INSTRUMENT '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); |
641 |
|
return lscp_client_query(pClient, szQuery); |
642 |
|
} |
643 |
|
|
644 |
|
|
645 |
|
/** |
646 |
|
* Loading an instrument in the background (non modal): |
647 |
|
* LOAD INSTRUMENT NON_MODAL <filename> <instr-index> <sampler-channel> |
648 |
|
* |
649 |
|
* @param pClient Pointer to client instance structure. |
650 |
|
* @param pszFileName Instrument file name. |
651 |
|
* @param iInstrIndex Instrument index number. |
652 |
|
* @param iSamplerChannel Sampler Channel. |
653 |
|
* |
654 |
|
* @returns LSCP_OK on success, LSCP_FAILED otherwise. |
655 |
|
*/ |
656 |
|
lscp_status_t lscp_load_instrument_non_modal ( lscp_client_t *pClient, const char *pszFileName, int iInstrIndex, int iSamplerChannel ) |
657 |
|
{ |
658 |
|
char szQuery[LSCP_BUFSIZ]; |
659 |
|
|
660 |
|
if (pszFileName == NULL || iSamplerChannel < 0) |
661 |
|
return LSCP_FAILED; |
662 |
|
|
663 |
|
sprintf(szQuery, "LOAD INSTRUMENT NON_MODAL '%s' %d %d\r\n", pszFileName, iInstrIndex, iSamplerChannel); |
664 |
return lscp_client_query(pClient, szQuery); |
return lscp_client_query(pClient, szQuery); |
665 |
} |
} |
666 |
|
|
699 |
int lscp_get_channels ( lscp_client_t *pClient ) |
int lscp_get_channels ( lscp_client_t *pClient ) |
700 |
{ |
{ |
701 |
int iChannels = -1; |
int iChannels = -1; |
702 |
if (lscp_client_query(pClient, "GET CHANNELS\r\n") == LSCP_OK) |
|
703 |
|
// Lock this section up. |
704 |
|
lscp_mutex_lock(pClient->mutex); |
705 |
|
|
706 |
|
if (lscp_client_call(pClient, "GET CHANNELS\r\n") == LSCP_OK) |
707 |
iChannels = atoi(lscp_client_get_result(pClient)); |
iChannels = atoi(lscp_client_get_result(pClient)); |
708 |
|
|
709 |
|
// Unlock this section doen. |
710 |
|
lscp_mutex_unlock(pClient->mutex); |
711 |
|
|
712 |
return iChannels; |
return iChannels; |
713 |
} |
} |
714 |
|
|
715 |
|
|
716 |
/** |
/** |
717 |
|
* List current sampler channels number identifiers: |
718 |
|
* LIST CHANNELS |
719 |
|
* |
720 |
|
* @param pClient Pointer to client instance structure. |
721 |
|
* |
722 |
|
* @returns An array of the sampler channels identifiers as positive integers, |
723 |
|
* terminated with -1 on success, NULL otherwise. |
724 |
|
*/ |
725 |
|
int *lscp_list_channels ( lscp_client_t *pClient ) |
726 |
|
{ |
727 |
|
const char *pszSeps = ","; |
728 |
|
|
729 |
|
if (pClient == NULL) |
730 |
|
return NULL; |
731 |
|
|
732 |
|
// Lock this section up. |
733 |
|
lscp_mutex_lock(pClient->mutex); |
734 |
|
|
735 |
|
if (pClient->channels) { |
736 |
|
lscp_isplit_destroy(pClient->channels); |
737 |
|
pClient->channels = NULL; |
738 |
|
} |
739 |
|
|
740 |
|
if (lscp_client_call(pClient, "LIST CHANNELS\r\n") == LSCP_OK) |
741 |
|
pClient->channels = lscp_isplit_create(lscp_client_get_result(pClient), pszSeps); |
742 |
|
|
743 |
|
// Unlock this section down. |
744 |
|
lscp_mutex_unlock(pClient->mutex); |
745 |
|
|
746 |
|
return pClient->channels; |
747 |
|
} |
748 |
|
|
749 |
|
|
750 |
|
/** |
751 |
* Adding a new sampler channel: |
* Adding a new sampler channel: |
752 |
* ADD CHANNEL |
* ADD CHANNEL |
753 |
* |
* |
759 |
int lscp_add_channel ( lscp_client_t *pClient ) |
int lscp_add_channel ( lscp_client_t *pClient ) |
760 |
{ |
{ |
761 |
int iSamplerChannel = -1; |
int iSamplerChannel = -1; |
762 |
if (lscp_client_query(pClient, "ADD CHANNEL\r\n") == LSCP_OK) |
|
763 |
|
// Lock this section up. |
764 |
|
lscp_mutex_lock(pClient->mutex); |
765 |
|
|
766 |
|
if (lscp_client_call(pClient, "ADD CHANNEL\r\n") == LSCP_OK) |
767 |
iSamplerChannel = atoi(lscp_client_get_result(pClient)); |
iSamplerChannel = atoi(lscp_client_get_result(pClient)); |
768 |
|
|
769 |
|
// Unlock this section down. |
770 |
|
lscp_mutex_unlock(pClient->mutex); |
771 |
|
|
772 |
return iSamplerChannel; |
return iSamplerChannel; |
773 |
} |
} |
774 |
|
|
807 |
{ |
{ |
808 |
const char *pszSeps = ","; |
const char *pszSeps = ","; |
809 |
|
|
810 |
|
// Lock this section up. |
811 |
|
lscp_mutex_lock(pClient->mutex); |
812 |
|
|
813 |
if (pClient->engines) { |
if (pClient->engines) { |
814 |
lscp_szsplit_destroy(pClient->engines); |
lscp_szsplit_destroy(pClient->engines); |
815 |
pClient->engines = NULL; |
pClient->engines = NULL; |
816 |
} |
} |
817 |
|
|
818 |
if (lscp_client_query(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK) |
if (lscp_client_call(pClient, "GET AVAILABLE_ENGINES\r\n") == LSCP_OK) |
819 |
pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps); |
pClient->engines = lscp_szsplit_create(lscp_client_get_result(pClient), pszSeps); |
820 |
|
|
821 |
|
// Unlock this section down. |
822 |
|
lscp_mutex_unlock(pClient->mutex); |
823 |
|
|
824 |
return (const char **) pClient->engines; |
return (const char **) pClient->engines; |
825 |
} |
} |
826 |
|
|
848 |
if (pszEngineName == NULL) |
if (pszEngineName == NULL) |
849 |
return NULL; |
return NULL; |
850 |
|
|
851 |
|
// Lock this section up. |
852 |
|
lscp_mutex_lock(pClient->mutex); |
853 |
|
|
854 |
pEngineInfo = &(pClient->engine_info); |
pEngineInfo = &(pClient->engine_info); |
855 |
lscp_engine_info_reset(pEngineInfo); |
lscp_engine_info_reset(pEngineInfo); |
856 |
|
|
857 |
sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName); |
sprintf(szQuery, "GET ENGINE INFO %s\r\n", pszEngineName); |
858 |
if (lscp_client_query(pClient, szQuery) != LSCP_OK) |
if (lscp_client_call(pClient, szQuery) == LSCP_OK) { |
859 |
return NULL; |
pszResult = lscp_client_get_result(pClient); |
860 |
|
pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); |
861 |
pszResult = lscp_client_get_result(pClient); |
while (pszToken) { |
862 |
pszToken = lscp_strtok(pszResult, pszSeps, &(pch)); |
if (strcasecmp(pszToken, "DESCRIPTION") == 0) { |
863 |
while (pszToken) { |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
864 |
if (strcasecmp(pszToken, "DESCRIPTION") == 0) { |
if (pszToken) |
865 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pEngineInfo->description = lscp_unquote(&pszToken, 1); |
866 |
if (pszToken) |
} |
867 |
pEngineInfo->description = lscp_unquote(&pszToken, 1); |
else if (strcasecmp(pszToken, "VERSION") == 0) { |
868 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
869 |
else if (strcasecmp(pszToken, "VERSION") == 0) { |
if (pszToken) |
870 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pEngineInfo->version = lscp_unquote(&pszToken, 1); |
871 |
if (pszToken) |
} |
872 |
pEngineInfo->version = lscp_unquote(&pszToken, 1); |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
873 |
} |
} |
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
|
874 |
} |
} |
875 |
|
else pEngineInfo = NULL; |
876 |
|
|
877 |
|
// Unlock this section down. |
878 |
|
lscp_mutex_unlock(pClient->mutex); |
879 |
|
|
880 |
return pEngineInfo; |
return pEngineInfo; |
881 |
} |
} |
904 |
if (iSamplerChannel < 0) |
if (iSamplerChannel < 0) |
905 |
return NULL; |
return NULL; |
906 |
|
|
907 |
|
// Lock this section up. |
908 |
|
lscp_mutex_lock(pClient->mutex); |
909 |
|
|
910 |
pChannelInfo = &(pClient->channel_info); |
pChannelInfo = &(pClient->channel_info); |
911 |
lscp_channel_info_reset(pChannelInfo); |
lscp_channel_info_reset(pChannelInfo); |
912 |
|
|
913 |
sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel); |
sprintf(szQuery, "GET CHANNEL INFO %d\r\n", iSamplerChannel); |
914 |
if (lscp_client_query(pClient, szQuery) != LSCP_OK) |
if (lscp_client_call(pClient, szQuery) == LSCP_OK) { |
915 |
return NULL; |
pszResult = lscp_client_get_result(pClient); |
916 |
|
pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); |
917 |
pszResult = lscp_client_get_result(pClient); |
while (pszToken) { |
918 |
pszToken = lscp_strtok(pszResult, pszSeps, &(pch)); |
if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { |
919 |
while (pszToken) { |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
920 |
if (strcasecmp(pszToken, "ENGINE_NAME") == 0) { |
if (pszToken) |
921 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->engine_name = lscp_unquote(&pszToken, 1); |
922 |
if (pszToken) |
} |
923 |
pChannelInfo->engine_name = lscp_unquote(&pszToken, 1); |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) { |
924 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
925 |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_DEVICE") == 0) { |
if (pszToken) |
926 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken)); |
927 |
if (pszToken) |
} |
928 |
pChannelInfo->audio_device = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) { |
929 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
930 |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_CHANNELS") == 0) { |
if (pszToken) |
931 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken)); |
932 |
if (pszToken) |
} |
933 |
pChannelInfo->audio_channels = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) { |
934 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
935 |
else if (strcasecmp(pszToken, "AUDIO_OUTPUT_ROUTING") == 0) { |
if (pszToken) |
936 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ","); |
937 |
if (pszToken) |
} |
938 |
pChannelInfo->audio_routing = lscp_szsplit_create(pszToken, ","); |
else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { |
939 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
940 |
else if (strcasecmp(pszToken, "INSTRUMENT_FILE") == 0) { |
if (pszToken) |
941 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1); |
942 |
if (pszToken) |
} |
943 |
pChannelInfo->instrument_file = lscp_unquote(&pszToken, 1); |
else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { |
944 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
945 |
else if (strcasecmp(pszToken, "INSTRUMENT_NR") == 0) { |
if (pszToken) |
946 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); |
947 |
if (pszToken) |
} |
948 |
pChannelInfo->instrument_nr = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "INSTRUMENT_STATUS") == 0) { |
949 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
950 |
else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) { |
if (pszToken) |
951 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->instrument_status = atoi(lscp_ltrim(pszToken)); |
952 |
if (pszToken) |
} |
953 |
pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "MIDI_INPUT_DEVICE") == 0) { |
954 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
955 |
else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) { |
if (pszToken) |
956 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->midi_device = atoi(lscp_ltrim(pszToken)); |
957 |
if (pszToken) |
} |
958 |
pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "MIDI_INPUT_PORT") == 0) { |
959 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
960 |
else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) { |
if (pszToken) |
961 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->midi_port = atoi(lscp_ltrim(pszToken)); |
962 |
if (pszToken) |
} |
963 |
pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "MIDI_INPUT_CHANNEL") == 0) { |
964 |
} |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
965 |
else if (strcasecmp(pszToken, "VOLUME") == 0) { |
if (pszToken) |
966 |
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
pChannelInfo->midi_channel = atoi(lscp_ltrim(pszToken)); |
967 |
if (pszToken) |
} |
968 |
pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken)); |
else if (strcasecmp(pszToken, "VOLUME") == 0) { |
969 |
|
pszToken = lscp_strtok(NULL, pszCrlf, &(pch)); |
970 |
|
if (pszToken) |
971 |
|
pChannelInfo->volume = (float) atof(lscp_ltrim(pszToken)); |
972 |
|
} |
973 |
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
974 |
} |
} |
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
|
975 |
} |
} |
976 |
|
else pChannelInfo = NULL; |
977 |
|
|
978 |
|
// Unlock this section up. |
979 |
|
lscp_mutex_unlock(pClient->mutex); |
980 |
|
|
981 |
return pChannelInfo; |
return pChannelInfo; |
982 |
} |
} |
999 |
if (iSamplerChannel < 0) |
if (iSamplerChannel < 0) |
1000 |
return iVoiceCount; |
return iVoiceCount; |
1001 |
|
|
1002 |
|
// Lock this section up. |
1003 |
|
lscp_mutex_lock(pClient->mutex); |
1004 |
|
|
1005 |
sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel); |
sprintf(szQuery, "GET CHANNEL VOICE_COUNT %d\r\n", iSamplerChannel); |
1006 |
if (lscp_client_query(pClient, szQuery) == LSCP_OK) |
if (lscp_client_call(pClient, szQuery) == LSCP_OK) |
1007 |
iVoiceCount = atoi(lscp_client_get_result(pClient)); |
iVoiceCount = atoi(lscp_client_get_result(pClient)); |
1008 |
|
|
1009 |
|
// Unlock this section down. |
1010 |
|
lscp_mutex_unlock(pClient->mutex); |
1011 |
|
|
1012 |
return iVoiceCount; |
return iVoiceCount; |
1013 |
} |
} |
1014 |
|
|
1027 |
if (iSamplerChannel < 0) |
if (iSamplerChannel < 0) |
1028 |
return iStreamCount; |
return iStreamCount; |
1029 |
|
|
1030 |
|
// Lock this section up. |
1031 |
|
lscp_mutex_lock(pClient->mutex); |
1032 |
|
|
1033 |
sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel); |
sprintf(szQuery, "GET CHANNEL STREAM_COUNT %d\r\n", iSamplerChannel); |
1034 |
if (lscp_client_query(pClient, szQuery) == LSCP_OK) |
if (lscp_client_call(pClient, szQuery) == LSCP_OK) |
1035 |
iStreamCount = atoi(lscp_client_get_result(pClient)); |
iStreamCount = atoi(lscp_client_get_result(pClient)); |
1036 |
|
|
1037 |
|
// Unlock this section down. |
1038 |
|
lscp_mutex_unlock(pClient->mutex); |
1039 |
|
|
1040 |
return iStreamCount; |
return iStreamCount; |
1041 |
} |
} |
1042 |
|
|
1067 |
char *pch; |
char *pch; |
1068 |
int iStream; |
int iStream; |
1069 |
|
|
1070 |
|
// Retrieve a channel stream estimation. |
1071 |
iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel); |
iStreamCount = lscp_get_channel_stream_count(pClient, iSamplerChannel); |
1072 |
|
if (pClient->iStreamCount < 0) |
1073 |
|
return NULL; |
1074 |
|
|
1075 |
|
// Lock this section up. |
1076 |
|
lscp_mutex_lock(pClient->mutex); |
1077 |
|
|
1078 |
|
// Check if we need to reallocate the stream usage array. |
1079 |
if (pClient->iStreamCount != iStreamCount) { |
if (pClient->iStreamCount != iStreamCount) { |
1080 |
if (pClient->buffer_fill) |
if (pClient->buffer_fill) |
1081 |
free(pClient->buffer_fill); |
free(pClient->buffer_fill); |
1086 |
pClient->iStreamCount = iStreamCount; |
pClient->iStreamCount = iStreamCount; |
1087 |
} |
} |
1088 |
|
|
|
if (pClient->iStreamCount < 1) |
|
|
return NULL; |
|
|
|
|
|
iStream = 0; |
|
|
pBufferFill = pClient->buffer_fill; |
|
|
|
|
1089 |
// Get buffer fill usage... |
// Get buffer fill usage... |
1090 |
sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel); |
pBufferFill = pClient->buffer_fill; |
1091 |
if (lscp_client_query(pClient, szQuery) == LSCP_OK) { |
if (pBufferFill && iStreamCount > 0) { |
1092 |
pszResult = lscp_client_get_result(pClient); |
iStream = 0; |
1093 |
pszToken = lscp_strtok(pszResult, pszSeps, &(pch)); |
pBufferFill = pClient->buffer_fill; |
1094 |
while (pszToken && iStream < pClient->iStreamCount) { |
sprintf(szQuery, "GET CHANNEL BUFFER_FILL %s %d\r\n", pszUsageType, iSamplerChannel); |
1095 |
if (*pszToken) { |
if (lscp_client_call(pClient, szQuery) == LSCP_OK) { |
1096 |
pBufferFill[iStream].stream_id = atol(pszToken); |
pszResult = lscp_client_get_result(pClient); |
1097 |
|
pszToken = lscp_strtok((char *) pszResult, pszSeps, &(pch)); |
1098 |
|
while (pszToken && iStream < pClient->iStreamCount) { |
1099 |
|
if (*pszToken) { |
1100 |
|
pBufferFill[iStream].stream_id = atol(pszToken); |
1101 |
|
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
1102 |
|
if (pszToken == NULL) |
1103 |
|
break; |
1104 |
|
pBufferFill[iStream].stream_usage = atol(pszToken); |
1105 |
|
iStream++; |
1106 |
|
} |
1107 |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
|
if (pszToken == NULL) |
|
|
break; |
|
|
pBufferFill[iStream].stream_usage = atol(pszToken); |
|
|
iStream++; |
|
1108 |
} |
} |
1109 |
pszToken = lscp_strtok(NULL, pszSeps, &(pch)); |
} // Reset the usage, whatever it was before. |
1110 |
} |
else while (iStream < pClient->iStreamCount) |
1111 |
} // Reset the usage, whatever it was before. |
pBufferFill[iStream++].stream_usage = 0; |
1112 |
else while (iStream < pClient->iStreamCount) |
} |
1113 |
pBufferFill[iStream++].stream_usage = 0; |
|
1114 |
|
// Unlock this section down. |
1115 |
|
lscp_mutex_unlock(pClient->mutex); |
1116 |
|
|
1117 |
return pBufferFill; |
return pBufferFill; |
1118 |
} |
} |
1139 |
|
|
1140 |
|
|
1141 |
/** |
/** |
1142 |
|
* Setting audio output device: |
1143 |
|
* SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id> |
1144 |
|
* |
1145 |
|
* @param pClient Pointer to client instance structure. |
1146 |
|
* @param iSamplerChannel Sampler channel number. |
1147 |
|
* @param iAudioDevice Audio output device number identifier. |
1148 |
|
*/ |
1149 |
|
lscp_status_t lscp_set_channel_audio_device ( lscp_client_t *pClient, int iSamplerChannel, int iAudioDevice ) |
1150 |
|
{ |
1151 |
|
char szQuery[LSCP_BUFSIZ]; |
1152 |
|
|
1153 |
|
if (iSamplerChannel < 0 || iAudioDevice < 0) |
1154 |
|
return LSCP_FAILED; |
1155 |
|
|
1156 |
|
sprintf(szQuery, "SET CHANNEL AUDIO_OUTPUT_DEVICE %d %d\r\n", iSamplerChannel, iAudioDevice); |
1157 |
|
return lscp_client_query(pClient, szQuery); |
1158 |
|
} |
1159 |
|
|
1160 |
|
|
1161 |
|
/** |
1162 |
* Setting audio output channel: |
* Setting audio output channel: |
1163 |
* SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan> |
* SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-output-chan> <audio-input-chan> |
1164 |
* |
* |
1202 |
return lscp_client_query(pClient, szQuery); |
return lscp_client_query(pClient, szQuery); |
1203 |
} |
} |
1204 |
|
|
1205 |
|
|
1206 |
|
/** |
1207 |
|
* Setting MIDI input device: |
1208 |
|
* SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id> |
1209 |
|
* |
1210 |
|
* @param pClient Pointer to client instance structure. |
1211 |
|
* @param iSamplerChannel Sampler channel number. |
1212 |
|
* @param iMidiDevice MIDI input device number identifier. |
1213 |
|
*/ |
1214 |
|
lscp_status_t lscp_set_channel_midi_device ( lscp_client_t *pClient, int iSamplerChannel, int iMidiDevice ) |
1215 |
|
{ |
1216 |
|
char szQuery[LSCP_BUFSIZ]; |
1217 |
|
|
1218 |
|
if (iSamplerChannel < 0 || iMidiDevice < 0) |
1219 |
|
return LSCP_FAILED; |
1220 |
|
|
1221 |
|
sprintf(szQuery, "SET CHANNEL MIDI_INPUT_DEVICE %d %d\r\n", iSamplerChannel, iMidiDevice); |
1222 |
|
return lscp_client_query(pClient, szQuery); |
1223 |
|
} |
1224 |
|
|
1225 |
|
|
1226 |
/** |
/** |
1227 |
* Setting MIDI input port: |
* Setting MIDI input port: |