PageRenderTime 413ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/externals/mrpeach/udpsend~.c

http://github.com/danomatika/robotcowboy
C | 701 lines | 552 code | 83 blank | 66 comment | 100 complexity | 0faf8e2b445bef78bf8fe1ebf8b39a86 MD5 | raw file
  1. /* udpsend~ started by Martin Peach on 20100110, based on netsend~ */
  2. /* udpsend~ sends audio via udp only.*/
  3. /* It is a PD external, all Max stuff has been removed from the source */
  4. /* ------------------------ netsend~ ------------------------------------------ */
  5. /* */
  6. /* Tilde object to send uncompressed audio data to netreceive~. */
  7. /* Written by Olaf Matthes <olaf.matthes@gmx.de>. */
  8. /* Based on streamout~ by Guenter Geiger. */
  9. /* Get source at http://www.akustische-kunst.org/ */
  10. /* */
  11. /* This program is free software; you can redistribute it and/or */
  12. /* modify it under the terms of the GNU General Public License */
  13. /* as published by the Free Software Foundation; either version 2 */
  14. /* of the License, or (at your option) any later version. */
  15. /* */
  16. /* See file LICENSE for further informations on licensing terms. */
  17. /* */
  18. /* This program is distributed in the hope that it will be useful, */
  19. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  20. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  21. /* GNU General Public License for more details. */
  22. /* */
  23. /* You should have received a copy of the GNU General Public License */
  24. /* along with this program; if not, write to the Free Software */
  25. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  26. /* */
  27. /* Based on PureData by Miller Puckette and others. */
  28. /* */
  29. /* This project was commissioned by the Society for Arts and Technology [SAT], */
  30. /* Montreal, Quebec, Canada, http://www.sat.qc.ca/. */
  31. /* */
  32. /* ---------------------------------------------------------------------------- */
  33. #include "m_pd.h"
  34. #include "udpsend~.h"
  35. #include <sys/types.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39. #if defined(UNIX) || defined(unix)
  40. #include <sys/socket.h>
  41. #include <errno.h>
  42. #include <netinet/in.h>
  43. #include <netinet/tcp.h>
  44. #include <arpa/inet.h>
  45. #include <netdb.h>
  46. #include <sys/time.h>
  47. #include <signal.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <stdio.h>
  51. #include <pthread.h>
  52. #define SOCKET_ERROR -1
  53. #endif
  54. #ifdef _WIN32
  55. #include <winsock2.h>
  56. #include "pthread.h"
  57. #endif
  58. #ifdef MSG_NOSIGNAL
  59. #define SEND_FLAGS /*MSG_DONTWAIT|*/MSG_NOSIGNAL
  60. #else
  61. #define SEND_FLAGS 0
  62. #endif
  63. #ifndef SOL_IP
  64. #define SOL_IP IPPROTO_IP
  65. #endif
  66. /* ------------------------ udpsend~ ----------------------------- */
  67. static t_class *udpsend_tilde_class;
  68. static t_symbol *ps_nothing, *ps_localhost, *ps_vecsize;
  69. static t_symbol *ps_format, *ps_channels, *ps_framesize;
  70. static t_symbol *ps_sf_float, *ps_sf_16bit, *ps_sf_8bit;
  71. static t_symbol *ps_sf_unknown, *ps_bitrate, *ps_hostname;
  72. typedef struct _udpsend_tilde
  73. {
  74. t_object x_obj;
  75. t_outlet *x_outlet;
  76. t_outlet *x_outlet2;
  77. t_clock *x_clock;
  78. int x_fd;
  79. t_tag x_tag;
  80. t_symbol* x_hostname;
  81. int x_portno;
  82. int x_connectstate;
  83. char *x_cbuf;
  84. int x_cbufsize;
  85. int x_blocksize; /* set to DEFAULT_AUDIO_BUFFER_SIZE or user-supplied argument 3 in udpsend_tilde_new() */
  86. int x_blockspersend; /* set to x->x_blocksize / x->x_vecsize in udpsend_tilde_perform() */
  87. int x_blockssincesend;
  88. long x_samplerate; /* samplerate we're running at */
  89. int x_vecsize; /* current DSP signal vector size */
  90. int x_ninlets; /* number of inlets */
  91. int x_channels; /* number of channels we want to stream */
  92. int x_format; /* format of streamed audio data */
  93. int x_count; /* total number of audio frames */
  94. t_int **x_myvec; /* vector we pass on in the DSP routine */
  95. pthread_mutex_t x_mutex;
  96. pthread_t x_childthread; /* a thread to initiate a connection to the remote port */
  97. pthread_attr_t x_childthread_attr; /* child thread should have detached attribute so it can be cleaned up after it exits. */
  98. int x_childthread_result; /* result from pthread_create. Zero if x_childthread represents a valid thread. */
  99. } t_udpsend_tilde;
  100. #define NO_CHILDTHREAD 1 /* not zero */
  101. /* function prototypes */
  102. static int udpsend_tilde_sockerror(char *s);
  103. static void udpsend_tilde_closesocket(int fd);
  104. static void udpsend_tilde_notify(t_udpsend_tilde *x);
  105. static void udpsend_tilde_disconnect(t_udpsend_tilde *x);
  106. static void *udpsend_tilde_doconnect(void *zz);
  107. static void udpsend_tilde_connect(t_udpsend_tilde *x, t_symbol *host, t_floatarg fportno);
  108. static t_int *udpsend_tilde_perform(t_int *w);
  109. static void udpsend_tilde_dsp(t_udpsend_tilde *x, t_signal **sp);
  110. static void udpsend_tilde_channels(t_udpsend_tilde *x, t_floatarg channels);
  111. static void udpsend_tilde_format(t_udpsend_tilde *x, t_symbol* form, t_floatarg bitrate);
  112. static void udpsend_tilde_float(t_udpsend_tilde* x, t_floatarg arg);
  113. static void udpsend_tilde_info(t_udpsend_tilde *x);
  114. static void *udpsend_tilde_new(t_floatarg inlets, t_floatarg blocksize);
  115. static void udpsend_tilde_free(t_udpsend_tilde* x);
  116. void udpsend_tilde_setup(void);
  117. /* functions */
  118. static void udpsend_tilde_notify(t_udpsend_tilde *x)
  119. {
  120. pthread_mutex_lock(&x->x_mutex);
  121. x->x_childthread_result = NO_CHILDTHREAD; /* connection thread has ended */
  122. outlet_float(x->x_outlet, x->x_connectstate); /* we should be connected */
  123. pthread_mutex_unlock(&x->x_mutex);
  124. }
  125. static void udpsend_tilde_disconnect(t_udpsend_tilde *x)
  126. {
  127. pthread_mutex_lock(&x->x_mutex);
  128. if (x->x_fd != -1)
  129. {
  130. udpsend_tilde_closesocket(x->x_fd);
  131. x->x_fd = -1;
  132. x->x_connectstate = 0;
  133. outlet_float(x->x_outlet, 0);
  134. }
  135. pthread_mutex_unlock(&x->x_mutex);
  136. }
  137. /* udpsend_tilde_doconnect runs in the child thread, which terminates as soon as a connection is */
  138. static void *udpsend_tilde_doconnect(void *zz)
  139. {
  140. t_udpsend_tilde *x = (t_udpsend_tilde *)zz;
  141. struct sockaddr_in server;
  142. struct hostent *hp;
  143. int intarg = 1;
  144. int sockfd;
  145. int portno;
  146. int broadcast = 1;/* nonzero is true */
  147. t_symbol *hostname;
  148. pthread_mutex_lock(&x->x_mutex);
  149. hostname = x->x_hostname;
  150. portno = x->x_portno;
  151. pthread_mutex_unlock(&x->x_mutex);
  152. /* create a socket */
  153. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  154. if (sockfd < 0)
  155. {
  156. post("udpsend~: connection to %s on port %d failed", hostname->s_name,portno);
  157. udpsend_tilde_sockerror("socket");
  158. x->x_childthread_result = NO_CHILDTHREAD;
  159. return (0);
  160. }
  161. #ifdef SO_BROADCAST
  162. if( 0 != setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast)))
  163. {
  164. udpsend_tilde_sockerror("setting SO_BROADCAST");
  165. }
  166. #endif /* SO_BROADCAST */
  167. /* connect socket using hostname provided in command line */
  168. server.sin_family = AF_INET;
  169. hp = gethostbyname(x->x_hostname->s_name);
  170. if (hp == 0)
  171. {
  172. post("udpsend~: bad host?");
  173. x->x_childthread_result = NO_CHILDTHREAD;
  174. return (0);
  175. }
  176. #ifdef SO_PRIORITY
  177. /* set high priority, LINUX only */
  178. intarg = 6; /* select a priority between 0 and 7 */
  179. if (setsockopt(sockfd, SOL_SOCKET, SO_PRIORITY, (const char*)&intarg, sizeof(int)) < 0)
  180. {
  181. udpsend_tilde_sockerror("setting SO_PRIORITY");
  182. }
  183. #endif
  184. memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
  185. /* assign client port number */
  186. server.sin_port = htons((unsigned short)portno);
  187. /* try to connect */
  188. if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
  189. {
  190. udpsend_tilde_sockerror("connecting stream socket");
  191. udpsend_tilde_closesocket(sockfd);
  192. x->x_childthread_result = NO_CHILDTHREAD;
  193. return (0);
  194. }
  195. post("udpsend~: connected host %s on port %d", hostname->s_name, portno);
  196. pthread_mutex_lock(&x->x_mutex);
  197. x->x_fd = sockfd;
  198. x->x_connectstate = 1;
  199. clock_delay(x->x_clock, 0);/* udpsend_tilde_notify is called in next clock tick */
  200. pthread_mutex_unlock(&x->x_mutex);
  201. return (0);
  202. }
  203. static void udpsend_tilde_connect(t_udpsend_tilde *x, t_symbol *host, t_floatarg fportno)
  204. {
  205. pthread_mutex_lock(&x->x_mutex);
  206. if (x->x_childthread_result == 0)
  207. {
  208. pthread_mutex_unlock(&x->x_mutex);
  209. post("udpsend~: already trying to connect");
  210. return;
  211. }
  212. if (x->x_fd != -1)
  213. {
  214. pthread_mutex_unlock(&x->x_mutex);
  215. post("udpsend~: already connected");
  216. return;
  217. }
  218. if (host != ps_nothing)
  219. x->x_hostname = host;
  220. else
  221. x->x_hostname = ps_localhost; /* default host */
  222. if (!fportno)
  223. x->x_portno = DEFAULT_PORT;
  224. else
  225. x->x_portno = (int)fportno;
  226. x->x_count = 0;
  227. /* start child thread to connect */
  228. /* sender thread should start out detached so its resouces will be freed when it is done */
  229. if (0!= (x->x_childthread_result = pthread_attr_init(&x->x_childthread_attr)))
  230. {
  231. pthread_mutex_unlock(&x->x_mutex);
  232. post("udpsend~: pthread_attr_init failed: %d", x->x_childthread_result);
  233. return;
  234. }
  235. if(0!= (x->x_childthread_result = pthread_attr_setdetachstate(&x->x_childthread_attr, PTHREAD_CREATE_DETACHED)))
  236. {
  237. pthread_mutex_unlock(&x->x_mutex);
  238. post("udpsend~: pthread_attr_setdetachstate failed: %d", x->x_childthread_result);
  239. return;
  240. }
  241. if (0 != (x->x_childthread_result = pthread_create(&x->x_childthread, &x->x_childthread_attr, udpsend_tilde_doconnect, x)))
  242. {
  243. pthread_mutex_unlock(&x->x_mutex);
  244. post("udpsend~: couldn't create sender thread (%d)", x->x_childthread_result);
  245. return;
  246. }
  247. pthread_mutex_unlock(&x->x_mutex);
  248. }
  249. static t_int *udpsend_tilde_perform(t_int *w)
  250. {
  251. t_udpsend_tilde* x = (t_udpsend_tilde*) (w[1]);
  252. int n = (int)(w[2]);
  253. t_float *in[DEFAULT_AUDIO_CHANNELS];
  254. const int offset = 3;
  255. char* bp = NULL;
  256. int i, length = x->x_blocksize * SF_SIZEOF(x->x_tag.format) * x->x_tag.channels;
  257. int sent = 0;
  258. pthread_mutex_lock(&x->x_mutex);
  259. for (i = 0; i < x->x_ninlets; i++)
  260. in[i] = (t_float *)(w[offset + i]);
  261. if (n != x->x_vecsize) /* resize buffer */
  262. {
  263. x->x_vecsize = n;
  264. x->x_blockspersend = x->x_blocksize / x->x_vecsize;
  265. x->x_blockssincesend = 0;
  266. length = x->x_blocksize * SF_SIZEOF(x->x_tag.format) * x->x_tag.channels;
  267. }
  268. /* format the buffer */
  269. switch (x->x_tag.format)
  270. {
  271. case SF_FLOAT:
  272. {
  273. int32_t* fbuf = (int32_t *)x->x_cbuf + (x->x_blockssincesend * x->x_vecsize * x->x_tag.channels);
  274. flint fl;
  275. while (n--)
  276. for (i = 0; i < x->x_tag.channels; i++)
  277. {
  278. fl.f32 = *(in[i]++);
  279. *fbuf++ = htonl(fl.i32);
  280. }
  281. break;
  282. }
  283. case SF_16BIT:
  284. {
  285. short* cibuf = (short *)x->x_cbuf + (x->x_blockssincesend * x->x_vecsize * x->x_tag.channels);
  286. while (n--)
  287. for (i = 0; i < x->x_tag.channels; i++)
  288. *cibuf++ = htons((short)floor(32767.5 * *(in[i]++)));/* signed binary */
  289. break;
  290. }
  291. case SF_8BIT:
  292. {
  293. unsigned char* cbuf = (unsigned char*)x->x_cbuf + (x->x_blockssincesend * x->x_vecsize * x->x_tag.channels);
  294. while (n--)
  295. for (i = 0; i < x->x_tag.channels; i++)
  296. *cbuf++ = (unsigned char)floor(128. * (1.0 + *(in[i]++))); /* offset binary */
  297. break;
  298. }
  299. default:
  300. break;
  301. }
  302. if (!(x->x_blockssincesend < x->x_blockspersend - 1)) /* time to send the buffer */
  303. {
  304. x->x_blockssincesend = 0;
  305. x->x_count++; /* count data packet we're going to send */
  306. if (x->x_fd != -1)
  307. {
  308. bp = (char *)x->x_cbuf;
  309. /* fill in the header tag */
  310. x->x_tag.tag[0] = 'T';
  311. x->x_tag.tag[1] = 'A';
  312. x->x_tag.tag[2] = 'G';
  313. x->x_tag.tag[3] = '!';
  314. x->x_tag.framesize = htonl(length);
  315. x->x_tag.count = htonl(x->x_count);
  316. /* send the format tag */
  317. if (send(x->x_fd, (char*)&x->x_tag, sizeof(t_tag), SEND_FLAGS) < 0)
  318. {
  319. udpsend_tilde_sockerror("send tag");
  320. pthread_mutex_unlock(&x->x_mutex);
  321. udpsend_tilde_disconnect(x);
  322. return (w + offset + x->x_ninlets);
  323. }
  324. if (length != 0)
  325. /* UDP: max. packet size is 64k (incl. headers) so we have to split */
  326. {
  327. #ifdef __APPLE__
  328. /* WARNING: due to a 'bug' (maybe Apple would call it a feature?) in OS X
  329. send calls with data packets larger than 16k fail with error number 40!
  330. Thus we have to split the data packets into several packets that are
  331. 16k in size. The other side will reassemble them again. */
  332. int size = DEFAULT_UDP_PACKT_SIZE;
  333. if (length < size) /* maybe data fits into one packet? */
  334. size = length;
  335. /* send the buffer */
  336. for (sent = 0; sent < length;)
  337. {
  338. int ret = 0;
  339. ret = send(x->x_fd, bp, size, SEND_FLAGS);
  340. if (ret <= 0)
  341. {
  342. udpsend_tilde_sockerror("send data");
  343. pthread_mutex_unlock(&x->x_mutex);
  344. udpsend_tilde_disconnect(x);
  345. return (w + offset + x->x_ninlets);
  346. }
  347. else
  348. {
  349. bp += ret;
  350. sent += ret;
  351. if ((length - sent) < size)
  352. size = length - sent;
  353. }
  354. }
  355. #else
  356. /* If there is any data, send the buffer, the OS might segment it into smaller packets */
  357. int ret = send(x->x_fd, bp, length, SEND_FLAGS);
  358. if (ret <= 0)
  359. {
  360. post ("udpsend~: sending length %ld", length);
  361. udpsend_tilde_sockerror("send data");
  362. pthread_mutex_unlock(&x->x_mutex);
  363. udpsend_tilde_disconnect(x);
  364. return (w + offset + x->x_ninlets);
  365. }
  366. #endif
  367. }
  368. }
  369. /* check whether user has updated any parameters */
  370. if (x->x_tag.channels != x->x_channels)
  371. {
  372. x->x_tag.channels = x->x_channels;
  373. }
  374. if (x->x_tag.format != x->x_format)
  375. {
  376. x->x_tag.format = x->x_format;
  377. }
  378. }
  379. else
  380. {
  381. x->x_blockssincesend++;
  382. }
  383. pthread_mutex_unlock(&x->x_mutex);
  384. return (w + offset + x->x_ninlets);
  385. }
  386. static void udpsend_tilde_dsp(t_udpsend_tilde *x, t_signal **sp)
  387. {
  388. int i;
  389. pthread_mutex_lock(&x->x_mutex);
  390. x->x_myvec[0] = (t_int*)x;
  391. x->x_myvec[1] = (t_int*)sp[0]->s_n;
  392. x->x_samplerate = sp[0]->s_sr;
  393. for (i = 0; i < x->x_ninlets; i++)
  394. {
  395. x->x_myvec[2 + i] = (t_int*)sp[i]->s_vec;
  396. }
  397. pthread_mutex_unlock(&x->x_mutex);
  398. if (DEFAULT_AUDIO_BUFFER_SIZE % sp[0]->s_n)
  399. {
  400. error("udpsend~: signal vector size too large (needs to be even divisor of %d)", DEFAULT_AUDIO_BUFFER_SIZE);
  401. }
  402. else
  403. {
  404. dsp_addv(udpsend_tilde_perform, x->x_ninlets + 2, (t_int*)x->x_myvec);
  405. }
  406. }
  407. static void udpsend_tilde_channels(t_udpsend_tilde *x, t_floatarg channels)
  408. {
  409. pthread_mutex_lock(&x->x_mutex);
  410. if (channels >= 0 && channels <= x->x_ninlets)
  411. {
  412. x->x_channels = (int)channels;
  413. post("udpsend~: channels set to %d", (int)channels);
  414. }
  415. else post ("udpsend~ number of channels must be between 0 and %d", x->x_ninlets);
  416. pthread_mutex_unlock(&x->x_mutex);
  417. }
  418. static void udpsend_tilde_format(t_udpsend_tilde *x, t_symbol* form, t_floatarg bitrate)
  419. {
  420. pthread_mutex_lock(&x->x_mutex);
  421. if (!strncmp(form->s_name,"float", 5) && x->x_tag.format != SF_FLOAT)
  422. {
  423. x->x_format = (int)SF_FLOAT;
  424. }
  425. else if (!strncmp(form->s_name,"16bit", 5) && x->x_tag.format != SF_16BIT)
  426. {
  427. x->x_format = (int)SF_16BIT;
  428. }
  429. else if (!strncmp(form->s_name,"8bit", 4) && x->x_tag.format != SF_8BIT)
  430. {
  431. x->x_format = (int)SF_8BIT;
  432. }
  433. post("udpsend~: format set to %s", form->s_name);
  434. pthread_mutex_unlock(&x->x_mutex);
  435. }
  436. static void udpsend_tilde_float(t_udpsend_tilde* x, t_floatarg arg)
  437. {
  438. if (arg == 0.0)
  439. udpsend_tilde_disconnect(x);
  440. else
  441. udpsend_tilde_connect(x,x->x_hostname,(float) x->x_portno);
  442. }
  443. /* send stream info */
  444. static void udpsend_tilde_info(t_udpsend_tilde *x)
  445. {
  446. t_atom list[2];
  447. t_symbol *sf_format;
  448. t_float bitrate;
  449. bitrate = (t_float)((SF_SIZEOF(x->x_tag.format) * x->x_samplerate * 8 * x->x_tag.channels) / 1000.);
  450. switch (x->x_tag.format)
  451. {
  452. case SF_FLOAT:
  453. {
  454. sf_format = ps_sf_float;
  455. break;
  456. }
  457. case SF_16BIT:
  458. {
  459. sf_format = ps_sf_16bit;
  460. break;
  461. }
  462. case SF_8BIT:
  463. {
  464. sf_format = ps_sf_8bit;
  465. break;
  466. }
  467. default:
  468. {
  469. sf_format = ps_sf_unknown;
  470. break;
  471. }
  472. }
  473. /* --- stream information (t_tag) --- */
  474. /* audio format */
  475. SETSYMBOL(list, (t_symbol *)sf_format);
  476. outlet_anything(x->x_outlet2, ps_format, 1, list);
  477. /* channels */
  478. SETFLOAT(list, (t_float)x->x_tag.channels);
  479. outlet_anything(x->x_outlet2, ps_channels, 1, list);
  480. /* current signal vector size */
  481. SETFLOAT(list, (t_float)x->x_vecsize);
  482. outlet_anything(x->x_outlet2, ps_vecsize, 1, list);
  483. /* framesize */
  484. SETFLOAT(list, (t_float)(ntohl(x->x_tag.framesize)));
  485. outlet_anything(x->x_outlet2, ps_framesize, 1, list);
  486. /* bitrate */
  487. SETFLOAT(list, (t_float)bitrate);
  488. outlet_anything(x->x_outlet2, ps_bitrate, 1, list);
  489. /* IP address */
  490. SETSYMBOL(list, (t_symbol *)x->x_hostname);
  491. outlet_anything(x->x_outlet2, ps_hostname, 1, list);
  492. }
  493. static void *udpsend_tilde_new(t_floatarg inlets, t_floatarg blocksize)
  494. {
  495. int i;
  496. t_udpsend_tilde *x = (t_udpsend_tilde *)pd_new(udpsend_tilde_class);
  497. if (x)
  498. {
  499. for (i = sizeof(t_object); i < (int)sizeof(t_udpsend_tilde); i++)
  500. ((char *)x)[i] = 0;
  501. if ((int)inlets < 1 || (int)inlets > DEFAULT_AUDIO_CHANNELS)
  502. {
  503. error("udpsend~: Number of channels must be between 1 and %d", DEFAULT_AUDIO_CHANNELS);
  504. return NULL;
  505. }
  506. x->x_ninlets = (int)inlets;
  507. for (i = 1; i < x->x_ninlets; i++)
  508. inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
  509. x->x_outlet = outlet_new(&x->x_obj, &s_float);
  510. x->x_outlet2 = outlet_new(&x->x_obj, &s_list);
  511. x->x_clock = clock_new(x, (t_method)udpsend_tilde_notify);
  512. x->x_myvec = (t_int **)t_getbytes(sizeof(t_int *) * (x->x_ninlets + 3));
  513. if (!x->x_myvec)
  514. {
  515. error("udpsend~: out of memory");
  516. return NULL;
  517. }
  518. pthread_mutex_init(&x->x_mutex, 0);
  519. x->x_hostname = ps_localhost;
  520. x->x_portno = DEFAULT_PORT;
  521. x->x_connectstate = 0;
  522. x->x_childthread_result = NO_CHILDTHREAD;
  523. x->x_fd = -1;
  524. x->x_tag.format = x->x_format = SF_FLOAT;
  525. x->x_tag.channels = x->x_channels = x->x_ninlets;
  526. x->x_vecsize = 64; /* this is updated in the perform routine udpsend_tilde_perform */
  527. x->x_cbuf = NULL;
  528. if (blocksize == 0) x->x_blocksize = DEFAULT_AUDIO_BUFFER_SIZE;
  529. else if (DEFAULT_AUDIO_BUFFER_SIZE%(int)blocksize)
  530. {
  531. error("udpsend~: blocksize must fit snugly in %d", DEFAULT_AUDIO_BUFFER_SIZE);
  532. return NULL;
  533. }
  534. else x->x_blocksize = (int)blocksize; //DEFAULT_AUDIO_BUFFER_SIZE; /* <-- the only place blocksize is set */
  535. x->x_blockspersend = x->x_blocksize / x->x_vecsize; /* 1024/64 = 16 blocks */
  536. x->x_blockssincesend = 0;
  537. x->x_cbufsize = x->x_blocksize * sizeof(t_float) * x->x_ninlets;
  538. x->x_cbuf = (char *)t_getbytes(x->x_cbufsize);
  539. #if defined(UNIX) || defined(unix)
  540. /* we don't want to get signaled in case send() fails */
  541. signal(SIGPIPE, SIG_IGN);
  542. #endif
  543. }
  544. return (x);
  545. }
  546. static void udpsend_tilde_free(t_udpsend_tilde* x)
  547. {
  548. udpsend_tilde_disconnect(x);
  549. /* free the memory */
  550. if (x->x_cbuf)t_freebytes(x->x_cbuf, x->x_cbufsize);
  551. if (x->x_myvec)t_freebytes(x->x_myvec, sizeof(t_int) * (x->x_ninlets + 3));
  552. clock_free(x->x_clock);
  553. pthread_mutex_destroy(&x->x_mutex);
  554. }
  555. void udpsend_tilde_setup(void)
  556. {
  557. udpsend_tilde_class = class_new(gensym("udpsend~"), (t_newmethod)udpsend_tilde_new, (t_method)udpsend_tilde_free,
  558. sizeof(t_udpsend_tilde), 0, A_DEFFLOAT, A_DEFFLOAT, A_NULL);
  559. class_addmethod(udpsend_tilde_class, nullfn, gensym("signal"), 0);
  560. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_dsp, gensym("dsp"), 0);
  561. class_addfloat(udpsend_tilde_class, udpsend_tilde_float);
  562. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_info, gensym("info"), 0);
  563. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_connect, gensym("connect"), A_DEFSYM, A_DEFFLOAT, 0);
  564. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_disconnect, gensym("disconnect"), 0);
  565. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_channels, gensym("channels"), A_FLOAT, 0);
  566. class_addmethod(udpsend_tilde_class, (t_method)udpsend_tilde_format, gensym("format"), A_SYMBOL, A_DEFFLOAT, 0);
  567. class_sethelpsymbol(udpsend_tilde_class, gensym("udpsend~"));
  568. post("udpsend~ v%s, (c) 2004-2005 Olaf Matthes, 2010 Martin Peach", VERSION);
  569. post("udpsend~ Default blocksize is %d", DEFAULT_AUDIO_BUFFER_SIZE);
  570. ps_nothing = gensym("");
  571. ps_localhost = gensym("localhost");
  572. ps_hostname = gensym("ipaddr");
  573. ps_format = gensym("format");
  574. ps_channels = gensym("channels");
  575. ps_vecsize = gensym("vecsize");
  576. ps_framesize = gensym("framesize");
  577. ps_bitrate = gensym("bitrate");
  578. ps_sf_float = gensym("_float_");
  579. ps_sf_16bit = gensym("_16bit_");
  580. ps_sf_8bit = gensym("_8bit_");
  581. ps_sf_unknown = gensym("_unknown_");
  582. }
  583. /* Utility functions */
  584. static int udpsend_tilde_sockerror(char *s)
  585. {
  586. #ifdef _WIN32
  587. int err = WSAGetLastError();
  588. if (err == 10054) return 1;
  589. else if (err == 10053) post("udpsend~: %s: software caused connection abort (%d)", s, err);
  590. else if (err == 10055) post("udpsend~: %s: no buffer space available (%d)", s, err);
  591. else if (err == 10060) post("udpsend~: %s: connection timed out (%d)", s, err);
  592. else if (err == 10061) post("udpsend~: %s: connection refused (%d)", s, err);
  593. else post("udpsend~: %s: %s (%d)", s, strerror(err), err);
  594. #else
  595. int err = errno;
  596. post("udpsend~: %s: %s (%d)", s, strerror(err), err);
  597. #endif
  598. #ifdef _WIN32
  599. if (err == WSAEWOULDBLOCK)
  600. #endif
  601. #if defined(UNIX) || defined(unix)
  602. if (err == EAGAIN)
  603. #endif
  604. {
  605. return 1; /* recoverable error */
  606. }
  607. return 0; /* indicate non-recoverable error */
  608. }
  609. static void udpsend_tilde_closesocket(int fd)
  610. {
  611. #if defined(UNIX) || defined(unix)
  612. close(fd);
  613. #endif
  614. #ifdef _WIN32
  615. closesocket(fd);
  616. #endif
  617. }
  618. /* fin udpsend~.c */