aboutsummaryrefslogtreecommitdiff
path: root/signal/streamin~.c
blob: 87c577c7baac61873df7df0ba9b8fc4db899cf18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

/* Written by Guenter Geiger <geiger@xdv.org> (C) 1999 */
/* Thanks to Anthony Lee for Windows bug fixes */

#include <m_pd.h>
#include "stream.h"

#include <sys/types.h>
#include <string.h>
#ifdef unix
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#define SOCKET_ERROR -1
#else
#include <winsock.h>
#endif

/* these pragmas are only used for MSVC, not MinGW or Cygwin <hans@at.or.at> */
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#ifdef unix
#define NONBLOCKED
#endif

#define MAXFRAMES 128
#define MAXFRAMESIZE 256
#define AVERAGENUM 10

/*#define DEBUGMESS(x) x*/
#define  DEBUGMESS(x) 

/* Utility functions */

/* TODO !!!!
  - check udp support 
*/


#ifdef NT
extern int close(int);
extern void sys_rmpollfn(int fd);
extern sys_addpollfn(int fd, void* fn, void *ptr);
#endif

static void sys_sockerror(char *s)
{
#ifdef unix
    int err = errno;
#else
    int err = WSAGetLastError();
    if (err == 10054) return;
#endif
    post("%s: %s (%d)\n", s, strerror(err), err);
}


static void sys_closesocket(int fd)
{
#ifdef UNIX
    close(fd);
#endif
#ifdef NT
    closesocket(fd);
#endif
}


int setsocketoptions(int sockfd)
{ 
#ifdef unix
    int sockopt = 1;
	 if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (const char*) &sockopt, sizeof(int)) < 0) 
	 {
		 DEBUGMESS(post("setsockopt NODELAY failed\n"));
	 }
	 else
	 {
		 DEBUGMESS(post("TCP_NODELAY set"));
	 }
	 
     
     /* if we don`t use REUSEADDR we have to wait under unix until the 
	address gets freed after a close ... this can be very annoying 
	when working with netsend/netreceive GG
     */

     sockopt = 1;    
     if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) < 0)
	  post("setsockopt failed\n");
#endif
	return 0;
}



/* ------------------------ streamin~ ----------------------------- */


static t_class *streamin_class;



typedef struct _streamin
{
     t_object x_obj;
     int x_socket;
     int x_connectsocket;
     int x_nconnections;
     int x_ndrops;
     int x_fd;
     int x_tcp;

     /* buffering */

     int framein;
     int frameout;
     t_frame frames[MAXFRAMES];
     int maxframes;

     int nbytes;
     int counter;
     int average[AVERAGENUM];
     int averagecur;
     int underflow;
} t_streamin;




static void streamin_datapoll(t_streamin *x)
{
     int ret;
     int n;
     t_tag tag;
     int i;

     n = x->nbytes;
     if (x->nbytes == 0) {      /* get the new tag */
	  ret = recv(x->x_socket, (char*) &x->frames[x->framein].tag,sizeof(t_tag),MSG_PEEK);
	  if (ret != sizeof(t_tag)) {
#ifdef NT
            sys_closesocket(x->x_socket);
            sys_rmpollfn(x->x_socket);
            x->x_socket = -1;
#endif
            return;
          }
	  ret = recv(x->x_socket, (char*) &x->frames[x->framein].tag,sizeof(t_tag),0);

	  x->nbytes = n = x->frames[x->framein].tag.framesize;
     }

     ret = recv(x->x_socket, (char*) x->frames[x->framein].data + x->frames[x->framein].tag.framesize - n, n, 0);
     if (ret > 0)
	  n-=ret;

     x->nbytes = n;
     if (n == 0) {
	  x->counter++;
	  x->framein++;
	  x->framein %= MAXFRAMES;
     }
}

static void streamin_reset(t_streamin* x,t_floatarg frames)
{
     int i;
     x->counter = 0;
     x->nbytes = 0;
     x->framein = 0;
     x->frameout = 0;
     for (i=0;i<AVERAGENUM;i++)
	  x->average[i] = x->maxframes;
     x->averagecur=0;
     if (frames == 0.0)
	  x->maxframes = MAXFRAMES/2;
     else
	  x->maxframes = frames;
     x->underflow = 0;
}


static void streamin_connectpoll(t_streamin *x)
{
    int fd = accept(x->x_connectsocket, 0, 0);

#ifdef NONBLOCKED
    fcntl(fd,F_SETFL,O_NONBLOCK);
#endif
    if (fd < 0) {
	 post("streamin~: accept failed");
	 return;
    }

    if (x->x_socket > 0) {
	 post("streamin~: new connection");
	 sys_closesocket(x->x_socket);
	 sys_rmpollfn(x->x_socket);
    }

    streamin_reset(x,0);
    x->x_socket = fd;
    sys_addpollfn(fd, streamin_datapoll, x);
}


static int streamin_createsocket(t_streamin* x, int portno,t_symbol* prot)
{
    struct sockaddr_in server;
    int sockfd;
    int tcp = x->x_tcp;

     /* create a socket */
    if (!tcp)
      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    else
      sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)
    {
    	sys_sockerror("socket");
    	return (0);
    }
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

    /* assign server port number */

    server.sin_port = htons((u_short)portno);
    post("listening to port number %d", portno);


    setsocketoptions(sockfd);

	 
    /* name the socket */
    
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
	 sys_sockerror("bind");
	 sys_closesocket(sockfd);
	 return (0);
    }


    if (!tcp) {
	 x->x_socket = sockfd;
	 x->nbytes = 0;
	 sys_addpollfn(sockfd, streamin_datapoll, x);
    }
    else {
	 if (listen(sockfd, 5) < 0) {
	      sys_sockerror("listen");
	      sys_closesocket(sockfd);
	 }
	 else {
	      x->x_connectsocket = sockfd;
	      sys_addpollfn(sockfd, streamin_connectpoll, x);
	 }
    }
    return 1;
}



static void streamin_free(t_streamin *x)
{
     if (x->x_connectsocket > 0) {
	  sys_closesocket(x->x_connectsocket);
	  sys_rmpollfn(x->x_connectsocket);
     }
     sys_rmpollfn(x->x_socket);
     sys_closesocket(x->x_socket);
}


#define QUEUESIZE ((x->framein + MAXFRAMES - x->frameout)%MAXFRAMES)

static t_int *streamin_perform(t_int *w)
{
     t_streamin *x = (t_streamin*) (w[1]);
     t_float *out = (t_float *)(w[2]);
     int n = (int)(w[3]);
     int ret;
     int i = 0;

     if (x->counter < x->maxframes) {
	  return (w+4);
     }

     if (x->framein == x->frameout) {
	  x->underflow++;
	  return w+4;
     }


     /* queue balancing */

     x->average[x->averagecur] = QUEUESIZE;
     x->averagecur++;
     x->averagecur %= AVERAGENUM;

     switch (x->frames[x->frameout].tag.format) {
     case SF_FLOAT: {
	  t_float* buf = (t_float*)(x->frames[x->frameout].data);
	  while (n--) 
	       *out++ = *buf++; 
	  x->frameout++;
	  x->frameout %= MAXFRAMES;
	  break;
     }
     case SF_16BIT:     
     {
	  short* buf = (short*)(x->frames[x->frameout].data);

	  while (n--)
	       *out++ = (float) *buf++*3.051850e-05; 
	  x->frameout++;
	  x->frameout %= MAXFRAMES;

	  break;
     }
     case SF_8BIT:     
     {
	  unsigned char* buf = (char*)(x->frames[x->frameout].data);

	  while (n--)
	       *out++ = (float) (0.0078125 * (*buf++)) - 1.0; 
	  x->frameout++;
	  x->frameout %= MAXFRAMES;
	  break;
     }
     default:
	  post("unknown format %d",x->frames[x->frameout].tag.format);
	  break;
     }

     return (w+4);
}



static void streamin_dsp(t_streamin *x, t_signal **sp)
{
    dsp_add(streamin_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}


static void streamin_print(t_streamin* x)
{
     int i;
     int avg = 0;
     for (i=0;i<AVERAGENUM;i++)
	  avg += x->average[i];
     post("last size = %d, avg size = %d, %d underflows",
	  QUEUESIZE,avg/AVERAGENUM,x->underflow);
}




static void *streamin_new(t_floatarg fportno, t_floatarg prot)
{
    t_streamin *x;
    int i;
    
    if (fportno == 0) fportno = 4267;

    post("port %f",fportno);
    x = (t_streamin *)pd_new(streamin_class);
    
    x->x_connectsocket = -1;
    x->x_socket = -1;
    x->x_tcp = 1;
    outlet_new(&x->x_obj, &s_signal);
    x->x_nconnections = 0;
    x->x_ndrops = 0;
    
    for (i=0;i<MAXFRAMES;i++) {
	 x->frames[i].data = getbytes(MAXFRAMESIZE);
    }
    x->framein = 0;
    x->frameout = 0;
    x->maxframes = MAXFRAMES/2;
    
    if (prot)
	 x->x_tcp = 0;

    streamin_createsocket(x, fportno, gensym("tcp"));

    return (x);
}





void streamin_tilde_setup(void)
{
    streamin_class = class_new(gensym("streamin~"), 
    	(t_newmethod) streamin_new, (t_method) streamin_free,
    	sizeof(t_streamin),  0, A_DEFFLOAT,A_DEFFLOAT, A_NULL);

    class_addmethod(streamin_class, nullfn, gensym("signal"), 0);
    class_addmethod(streamin_class, (t_method) streamin_dsp, gensym("dsp"), 0);
    class_addmethod(streamin_class, (t_method) streamin_print, 
		    gensym("print"), 0);
    class_addmethod(streamin_class, (t_method) streamin_reset, 
		    gensym("reset"),A_DEFFLOAT, 0);
}