| 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 |
#include <m_pd.h> |
| 34 |
#include <m_imp.h> |
| 35 |
#include <g_canvas.h> |
| 36 |
|
| 37 |
#if PD_MINOR_VERSION >=37 |
| 38 |
#include "s_stuff.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
#include <string.h> |
| 43 |
#ifdef _WIN32 |
| 44 |
#include <io.h> |
| 45 |
#include <winsock.h> |
| 46 |
#else |
| 47 |
#include <sys/socket.h> |
| 48 |
#include <errno.h> |
| 49 |
#include <netinet/in.h> |
| 50 |
#include <netinet/tcp.h> |
| 51 |
#include <arpa/inet.h> |
| 52 |
#include <netdb.h> |
| 53 |
#include <sys/time.h> |
| 54 |
#include <unistd.h> |
| 55 |
#include <fcntl.h> |
| 56 |
#include <stdio.h> |
| 57 |
#define SOCKET_ERROR -1 |
| 58 |
#endif |
| 59 |
|
| 60 |
#if defined(__APPLE__) || defined(_WIN32) |
| 61 |
#define MSG_NOSIGNAL 0 |
| 62 |
#define SOL_TCP IPPROTO_TCP |
| 63 |
#endif |
| 64 |
|
| 65 |
#include "mpg123.h" |
| 66 |
#include "mpglib.h" |
| 67 |
#include "interface.h" |
| 68 |
|
| 69 |
#define MIN_AUDIO_INPUT 8064 // we must a least have 8 chunks to play a correct sound |
| 70 |
#define INPUT_BUFFER_SIZE MIN_AUDIO_INPUT |
| 71 |
#define OUTPUT_BUFFER_SIZE 131072 |
| 72 |
#define LAME_AUDIO_CHUNK_SIZE 1152 |
| 73 |
#define BARHEIGHT 10 |
| 74 |
#define MAX_DECODERS 10 |
| 75 |
|
| 76 |
|
| 77 |
extern int decode_header( struct frame* fr, unsigned long newhead ); |
| 78 |
extern void print_header_compact( struct frame* fr ); |
| 79 |
extern int head_check( unsigned long head, int check_layer ); |
| 80 |
|
| 81 |
|
| 82 |
static struct timeval ztout; |
| 83 |
|
| 84 |
static char *mp3streamin_version = "mp3streamin~: mp3 peer-to-peer streamer version 0.3, written by [email protected]"; |
| 85 |
|
| 86 |
|
| 87 |
void mp3streamin_closesocket(int fd) |
| 88 |
{ |
| 89 |
#ifndef _MSC_VER |
| 90 |
if ( close(fd) < 0 ) |
| 91 |
{ |
| 92 |
perror( "close" ); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
post( "mp3streamin~ : closed socket : %d", fd ); |
| 97 |
} |
| 98 |
#endif |
| 99 |
#ifdef _WIN32 |
| 100 |
closesocket(fd); |
| 101 |
#endif |
| 102 |
sys_rmpollfn(fd); |
| 103 |
} |
| 104 |
|
| 105 |
int setsocketoptions(int sockfd) |
| 106 |
{ |
| 107 |
int sockopt = 1; |
| 108 |
if (setsockopt(sockfd, SOL_TCP, TCP_NODELAY, (const char*) &sockopt, sizeof(int)) < 0) |
| 109 |
{ |
| 110 |
post("mp3streamin~ : setsockopt TCP_NODELAY failed"); |
| 111 |
perror( "setsockopt" ); |
| 112 |
return -1; |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
post("mp3streamin~ : TCP_NODELAY set"); |
| 117 |
} |
| 118 |
|
| 119 |
#ifndef _MSC_VER |
| 120 |
sockopt = 1; |
| 121 |
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) < 0) |
| 122 |
{ |
| 123 |
post("mp3streamin~ : setsockopt SO_REUSEADDR failed"); |
| 124 |
perror( "setsockopt" ); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
post("mp3streamin~ : setsockopt SO_REUSEADDR done."); |
| 130 |
} |
| 131 |
#endif |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static t_class *mp3streamin_class; |
| 139 |
|
| 140 |
typedef struct _mp3streamin |
| 141 |
{ |
| 142 |
t_object x_obj; |
| 143 |
t_int x_instance; |
| 144 |
t_int x_socket; |
| 145 |
t_int x_shutdown; |
| 146 |
t_outlet *x_connectionip; |
| 147 |
t_int x_serversocket; |
| 148 |
t_int x_inpackets; |
| 149 |
t_int x_dpacket; |
| 150 |
t_int x_packetsize; |
| 151 |
t_int x_pblocks; |
| 152 |
t_int x_graphic; |
| 153 |
|
| 154 |
void *x_inbuffer; |
| 155 |
t_int x_inwriteposition; |
| 156 |
t_int x_inbuffersize; |
| 157 |
|
| 158 |
t_float *x_outbuffer; |
| 159 |
t_int x_outwriteposition; |
| 160 |
t_int x_outreadposition; |
| 161 |
t_int x_outunread; |
| 162 |
t_int x_outbuffersize; |
| 163 |
|
| 164 |
t_canvas *x_canvas; |
| 165 |
|
| 166 |
t_int x_stream; |
| 167 |
t_int x_newstream; |
| 168 |
t_int x_bitrateindex; |
| 169 |
|
| 170 |
} t_mp3streamin; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static MPSTR mp[MAX_DECODERS]; |
| 175 |
int nbinstances = 0; |
| 176 |
|
| 177 |
void mp3streamin_tilde_mpglib_init(t_mp3streamin *x) |
| 178 |
{ |
| 179 |
int ret; |
| 180 |
|
| 181 |
InitMP3(&mp[x->x_instance]); |
| 182 |
} |
| 183 |
|
| 184 |
static int mp3streamin_decode_input(t_mp3streamin *x) |
| 185 |
{ |
| 186 |
int i; |
| 187 |
int alength = 0; |
| 188 |
struct frame hframe; |
| 189 |
unsigned int a,b,c,d; |
| 190 |
unsigned long cheader; |
| 191 |
static char out[8192]; |
| 192 |
signed short int *p = (signed short int *) out; |
| 193 |
int pbytes; |
| 194 |
int ret; |
| 195 |
|
| 196 |
if ( !x->x_shutdown ) |
| 197 |
{ |
| 198 |
|
| 199 |
a = *((unsigned char*)x->x_inbuffer); |
| 200 |
b = *((unsigned char*)x->x_inbuffer+1); |
| 201 |
c = *((unsigned char*)x->x_inbuffer+2); |
| 202 |
d = *((unsigned char*)x->x_inbuffer+3); |
| 203 |
|
| 204 |
cheader = 0; |
| 205 |
cheader = a; |
| 206 |
cheader <<= 8; |
| 207 |
cheader |= b; |
| 208 |
cheader <<= 8; |
| 209 |
cheader |= c; |
| 210 |
cheader <<= 8; |
| 211 |
cheader |= d; |
| 212 |
if ( head_check( cheader, 0 ) ) |
| 213 |
{ |
| 214 |
|
| 215 |
decode_header( &hframe, cheader ); |
| 216 |
|
| 217 |
|
| 218 |
if ( x->x_bitrateindex != hframe.bitrate_index ) |
| 219 |
{ |
| 220 |
ExitMP3(&mp[x->x_instance]); |
| 221 |
InitMP3(&mp[x->x_instance]); |
| 222 |
x->x_bitrateindex = hframe.bitrate_index; |
| 223 |
} |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
post( "mp3streamin~ : error : mp3 packet received without header" ); |
| 228 |
|
| 229 |
x->x_inwriteposition = 0; |
| 230 |
x->x_inpackets--; |
| 231 |
return( -1 ); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
ret = |
| 236 |
decodeMP3(&mp[x->x_instance], (unsigned char*)x->x_inbuffer, |
| 237 |
x->x_inwriteposition, (char *) p, sizeof(out), &pbytes); |
| 238 |
|
| 239 |
switch (ret) |
| 240 |
{ |
| 241 |
case MP3_OK: |
| 242 |
switch (mp[x->x_instance].fr.stereo) |
| 243 |
{ |
| 244 |
case 1: |
| 245 |
case 2: |
| 246 |
alength = ((mp[x->x_instance].fr.stereo==1)?pbytes >> 1 : pbytes >> 2); |
| 247 |
|
| 248 |
|
| 249 |
for ( i=0; i<alength; i++ ) |
| 250 |
{ |
| 251 |
if ( x->x_outunread >= x->x_outbuffersize-2 ) |
| 252 |
{ |
| 253 |
post( "mp3streamin~ : too much input ... ignored" ); |
| 254 |
continue; |
| 255 |
} |
| 256 |
*(x->x_outbuffer+x->x_outwriteposition) = ((t_float)(*p++))/32767.0; |
| 257 |
x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize; |
| 258 |
*(x->x_outbuffer+x->x_outwriteposition) = |
| 259 |
((mp[x->x_instance].fr.stereo==2)?((t_float)(*p++))/32767.0 : 0.0); |
| 260 |
x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize; |
| 261 |
x->x_outunread+=2; |
| 262 |
|
| 263 |
if ( x->x_outunread > MIN_AUDIO_INPUT && !x->x_stream ) |
| 264 |
{ |
| 265 |
post( "mp3streamin~ : stream connected." ); |
| 266 |
x->x_stream = 1; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
break; |
| 271 |
default: |
| 272 |
alength = -1; |
| 273 |
break; |
| 274 |
} |
| 275 |
break; |
| 276 |
|
| 277 |
case MP3_NEED_MORE: |
| 278 |
post( "mp3streamin~ : retry lame decoding (more data needed)." ); |
| 279 |
alength = 0; |
| 280 |
break; |
| 281 |
|
| 282 |
case MP3_ERR: |
| 283 |
post( "mp3streamin~ : lame decoding failed." ); |
| 284 |
alength = -1; |
| 285 |
break; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
x->x_inwriteposition = 0; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
if ( x->x_outunread == 0 ) |
| 294 |
{ |
| 295 |
post( "mp3streamin~ : connection closed" ); |
| 296 |
mp3streamin_closesocket(x->x_socket); |
| 297 |
x->x_stream = 0; |
| 298 |
x->x_newstream = 0; |
| 299 |
x->x_inpackets = 0; |
| 300 |
x->x_inwriteposition = 0; |
| 301 |
x->x_bitrateindex = -1; |
| 302 |
x->x_socket=-1; |
| 303 |
outlet_symbol( x->x_connectionip, gensym("") ); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if ( x->x_graphic && glist_isvisible( x->x_canvas ) ) |
| 308 |
{ |
| 309 |
|
| 310 |
if ( x->x_inpackets != x->x_dpacket ) |
| 311 |
{ |
| 312 |
char color[32]; |
| 313 |
int minpackets = ( MIN_AUDIO_INPUT/LAME_AUDIO_CHUNK_SIZE )-2; |
| 314 |
|
| 315 |
|
| 316 |
sys_vgui(".x%lx.c delete rectangle %xSTATUS\n", x->x_canvas, x ); |
| 317 |
sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x ); |
| 318 |
if ( x->x_outunread > 0 ) |
| 319 |
{ |
| 320 |
t_int width; |
| 321 |
|
| 322 |
if ( x->x_inpackets < (MIN_AUDIO_INPUT/LAME_AUDIO_CHUNK_SIZE)/2 ) |
| 323 |
{ |
| 324 |
strcpy( color, "red" ); |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
strcpy( color, "lightgreen" ); |
| 329 |
} |
| 330 |
width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); |
| 331 |
sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %xSTATUS\n", |
| 332 |
x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1, |
| 333 |
x->x_obj.te_xpix+(x->x_inpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 334 |
x->x_obj.te_ypix - 1, color, x ); |
| 335 |
sys_vgui(".x%lx.c create line %d %d %d %d -fill red -tags %xTHRESHOLD\n", |
| 336 |
x->x_canvas, x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 337 |
x->x_obj.te_ypix-BARHEIGHT-1, |
| 338 |
x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 339 |
x->x_obj.te_ypix-1, x ); |
| 340 |
x->x_dpacket = x->x_inpackets; |
| 341 |
} |
| 342 |
else |
| 343 |
{ |
| 344 |
if ( x->x_shutdown ) |
| 345 |
{ |
| 346 |
x->x_shutdown=0; |
| 347 |
sys_vgui(".x%lx.c delete rectangle %xPBAR\n", x->x_canvas, x ); |
| 348 |
sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x ); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
return alength; |
| 354 |
} |
| 355 |
|
| 356 |
static void mp3streamin_recv(t_mp3streamin *x) |
| 357 |
{ |
| 358 |
int ret; |
| 359 |
|
| 360 |
if ( ( ret = recv(x->x_socket, (void*) (x->x_inbuffer + x->x_inwriteposition), |
| 361 |
(size_t)((x->x_inbuffersize-x->x_inwriteposition)), |
| 362 |
MSG_NOSIGNAL) ) < 0 ) |
| 363 |
{ |
| 364 |
post( "mp3_streamin~ : receive error" ); |
| 365 |
perror( "recv" ); |
| 366 |
return; |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
if ( ret == 0 ) |
| 375 |
{ |
| 376 |
|
| 377 |
x->x_shutdown=1; |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
|
| 382 |
if ( (x->x_inpackets+1)*x->x_packetsize > x->x_inbuffersize ) |
| 383 |
{ |
| 384 |
post( "mp3streamin~ : too much input...resetting" ); |
| 385 |
x->x_inpackets=0; |
| 386 |
x->x_inwriteposition=0; |
| 387 |
return; |
| 388 |
} |
| 389 |
x->x_inpackets++; |
| 390 |
x->x_packetsize=ret; |
| 391 |
if ( x->x_inpackets % 100 == 0 ) |
| 392 |
{ |
| 393 |
|
| 394 |
} |
| 395 |
x->x_inwriteposition += ret; |
| 396 |
} |
| 397 |
|
| 398 |
mp3streamin_decode_input(x); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
static void mp3streamin_acceptconnection(t_mp3streamin *x) |
| 403 |
{ |
| 404 |
struct sockaddr_in incomer_address; |
| 405 |
int sockaddrl = (int) sizeof( struct sockaddr ); |
| 406 |
|
| 407 |
int fd = accept(x->x_serversocket, (struct sockaddr*)&incomer_address, &sockaddrl ); |
| 408 |
|
| 409 |
if (fd < 0) |
| 410 |
{ |
| 411 |
post("mp3streamin~: accept failed"); |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
if (x->x_socket > 0) |
| 416 |
{ |
| 417 |
sys_addpollfn(fd, (t_fdpollfn)mp3streamin_recv, x); |
| 418 |
if ( x->x_outunread != 0 ) |
| 419 |
{ |
| 420 |
post("mp3streamin~: still have some data to decode, retry later you %s.", |
| 421 |
inet_ntoa( incomer_address.sin_addr )); |
| 422 |
mp3streamin_closesocket( fd ); |
| 423 |
return; |
| 424 |
} |
| 425 |
post("mp3streamin~: the source has changed to %s.", |
| 426 |
inet_ntoa( incomer_address.sin_addr )); |
| 427 |
mp3streamin_closesocket(x->x_socket); |
| 428 |
} |
| 429 |
|
| 430 |
x->x_socket = fd; |
| 431 |
sys_addpollfn(x->x_socket, (t_fdpollfn)mp3streamin_recv, x); |
| 432 |
outlet_symbol( x->x_connectionip, gensym( inet_ntoa( incomer_address.sin_addr) ) ); |
| 433 |
|
| 434 |
if ( x->x_graphic && glist_isvisible( x->x_canvas ) ) |
| 435 |
{ |
| 436 |
t_int width; |
| 437 |
|
| 438 |
width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); |
| 439 |
sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill lightblue -tags %xPBAR\n", |
| 440 |
x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1, |
| 441 |
x->x_obj.te_xpix + width, x->x_obj.te_ypix - 1, x ); |
| 442 |
} |
| 443 |
x->x_stream = 0; |
| 444 |
x->x_newstream = 1; |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
static int mp3streamin_startservice(t_mp3streamin* x, int portno) |
| 450 |
{ |
| 451 |
struct sockaddr_in server; |
| 452 |
int sockfd; |
| 453 |
|
| 454 |
|
| 455 |
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 456 |
|
| 457 |
if (sockfd < 0) |
| 458 |
{ |
| 459 |
sys_sockerror("socket"); |
| 460 |
return (0); |
| 461 |
} |
| 462 |
server.sin_family = AF_INET; |
| 463 |
server.sin_addr.s_addr = INADDR_ANY; |
| 464 |
|
| 465 |
|
| 466 |
server.sin_port = htons((u_short)portno); |
| 467 |
post("listening to port number %d", portno); |
| 468 |
|
| 469 |
setsocketoptions(sockfd); |
| 470 |
|
| 471 |
|
| 472 |
if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) |
| 473 |
{ |
| 474 |
sys_sockerror("bind"); |
| 475 |
mp3streamin_closesocket(sockfd); |
| 476 |
return (0); |
| 477 |
} |
| 478 |
|
| 479 |
if (listen(sockfd, 5) < 0) |
| 480 |
{ |
| 481 |
sys_sockerror("listen"); |
| 482 |
mp3streamin_closesocket(sockfd); |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
x->x_serversocket = sockfd; |
| 487 |
sys_addpollfn(x->x_serversocket, (t_fdpollfn)mp3streamin_acceptconnection, x); |
| 488 |
} |
| 489 |
|
| 490 |
return 1; |
| 491 |
} |
| 492 |
|
| 493 |
static void mp3streamin_free(t_mp3streamin *x) |
| 494 |
{ |
| 495 |
post( "mp3streamin~ : free %x", x ); |
| 496 |
if (x->x_serversocket > 0) |
| 497 |
{ |
| 498 |
post( "mp3streamin~ : closing server socket" ); |
| 499 |
mp3streamin_closesocket(x->x_serversocket); |
| 500 |
x->x_serversocket = -1; |
| 501 |
} |
| 502 |
if (x->x_socket > 0) |
| 503 |
{ |
| 504 |
post( "mp3streamin~ : closing socket" ); |
| 505 |
mp3streamin_closesocket(x->x_socket); |
| 506 |
x->x_socket = -1; |
| 507 |
} |
| 508 |
if ( x->x_inbuffer ) freebytes( x->x_inbuffer, x->x_inbuffersize ); |
| 509 |
if ( x->x_outbuffer ) freebytes( x->x_outbuffer, x->x_outbuffersize*sizeof(t_float) ); |
| 510 |
if ( x->x_instance == nbinstances-1 ) |
| 511 |
{ |
| 512 |
nbinstances--; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
static t_int *mp3streamin_perform(t_int *w) |
| 517 |
{ |
| 518 |
t_mp3streamin *x = (t_mp3streamin*) (w[1]); |
| 519 |
t_float *out1 = (t_float *)(w[2]); |
| 520 |
t_float *out2 = (t_float *)(w[3]); |
| 521 |
int n = (int)(w[4]); |
| 522 |
int bsize = n; |
| 523 |
int ret; |
| 524 |
int i = 0; |
| 525 |
|
| 526 |
while( n-- ) |
| 527 |
{ |
| 528 |
if ( ( ( x->x_outunread > MIN_AUDIO_INPUT ) && x->x_newstream ) || |
| 529 |
( ( x->x_shutdown ) && ( x->x_outunread >= 2 ) ) || |
| 530 |
( x->x_stream ) |
| 531 |
) |
| 532 |
{ |
| 533 |
if ( x->x_newstream && !x->x_shutdown ) |
| 534 |
{ |
| 535 |
x->x_newstream = 0; |
| 536 |
x->x_stream = 1; |
| 537 |
} |
| 538 |
*out1++=*(x->x_outbuffer+x->x_outreadposition); |
| 539 |
x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize; |
| 540 |
*out2++=*(x->x_outbuffer+x->x_outreadposition); |
| 541 |
x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize; |
| 542 |
x->x_outunread-=2; |
| 543 |
if ( n == 1 ) x->x_pblocks++; |
| 544 |
} |
| 545 |
else |
| 546 |
{ |
| 547 |
*out1++=0.0; |
| 548 |
*out2++=0.0; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if ( ( x->x_outunread <= MIN_AUDIO_INPUT/10 ) && ( x->x_stream ) ) |
| 553 |
{ |
| 554 |
post( "mp3streamin~ : stream lost (too little input)" ); |
| 555 |
x->x_stream = 0; |
| 556 |
x->x_newstream = 1; |
| 557 |
} |
| 558 |
|
| 559 |
if ( x->x_pblocks == LAME_AUDIO_CHUNK_SIZE/bsize ) |
| 560 |
{ |
| 561 |
x->x_inpackets--; |
| 562 |
x->x_pblocks = 0; |
| 563 |
} |
| 564 |
|
| 565 |
#ifdef DO_MY_OWN_SELECT |
| 566 |
|
| 567 |
if ( x->x_socket > 0 ) |
| 568 |
{ |
| 569 |
fd_set readset; |
| 570 |
fd_set exceptset; |
| 571 |
|
| 572 |
FD_ZERO(&readset); |
| 573 |
FD_ZERO(&exceptset); |
| 574 |
FD_SET(x->x_socket, &readset ); |
| 575 |
FD_SET(x->x_socket, &exceptset ); |
| 576 |
|
| 577 |
if ( select( maxfd+1, &readset, NULL, &exceptset, &ztout ) >0 ) |
| 578 |
{ |
| 579 |
if ( FD_ISSET( x->x_socket, &readset) || FD_ISSET( x->x_socket, &exceptset ) ) |
| 580 |
{ |
| 581 |
|
| 582 |
mp3streamin_recv(x); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
#endif |
| 587 |
return (w+5); |
| 588 |
} |
| 589 |
|
| 590 |
static void mp3streamin_dsp(t_mp3streamin *x, t_signal **sp) |
| 591 |
{ |
| 592 |
dsp_add(mp3streamin_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
static void *mp3streamin_new(t_floatarg fportno, t_floatarg fdographics) |
| 597 |
{ |
| 598 |
t_mp3streamin *x; |
| 599 |
int i; |
| 600 |
|
| 601 |
if ( fportno < 0 || fportno > 65535 ) |
| 602 |
{ |
| 603 |
post( "mp3streamin~ : error : wrong portnumber : %d", (int)fportno ); |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
if ( ((int)fdographics != 0) && ((int)fdographics != 1.) ) |
| 607 |
{ |
| 608 |
post( "mp3streamin~ : error : constructor : mp3streamin~ <portnumber> [graphic flag = 0 | 1 ] ( got = %f)", fdographics ); |
| 609 |
return NULL; |
| 610 |
} |
| 611 |
|
| 612 |
x = (t_mp3streamin *)pd_new(mp3streamin_class); |
| 613 |
post( "mp3streamin~ : new %x (instance = %d) %d", x, nbinstances, sizeof( MPSTR ) ); |
| 614 |
outlet_new(&x->x_obj, &s_signal); |
| 615 |
outlet_new(&x->x_obj, &s_signal); |
| 616 |
x->x_connectionip = outlet_new(&x->x_obj, &s_symbol); |
| 617 |
|
| 618 |
x->x_serversocket = -1; |
| 619 |
x->x_socket = -1; |
| 620 |
x->x_shutdown = 0; |
| 621 |
x->x_inpackets = 0; |
| 622 |
x->x_dpacket = -1; |
| 623 |
|
| 624 |
x->x_canvas = canvas_getcurrent(); |
| 625 |
|
| 626 |
x->x_inbuffersize = INPUT_BUFFER_SIZE; |
| 627 |
x->x_outbuffersize = OUTPUT_BUFFER_SIZE; |
| 628 |
x->x_inbuffer = (char*) getbytes( x->x_inbuffersize ); |
| 629 |
memset( x->x_inbuffer, 0x0, INPUT_BUFFER_SIZE ); |
| 630 |
x->x_outbuffer = (t_float*) getbytes( x->x_outbuffersize*sizeof(t_float) ); |
| 631 |
memset( x->x_outbuffer, 0x0, OUTPUT_BUFFER_SIZE ); |
| 632 |
|
| 633 |
if ( !x->x_inbuffer || !x->x_outbuffer ) |
| 634 |
{ |
| 635 |
post( "mp3streamin~ : could not allocate buffers." ); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
if ( nbinstances < MAX_DECODERS ) |
| 640 |
{ |
| 641 |
x->x_instance = nbinstances++; |
| 642 |
} |
| 643 |
else |
| 644 |
{ |
| 645 |
post( "mp3streamin~ : cannot create more decoders (memory issues), sorry" ); |
| 646 |
return NULL; |
| 647 |
} |
| 648 |
|
| 649 |
x->x_inwriteposition = 0; |
| 650 |
x->x_outreadposition = 0; |
| 651 |
x->x_outwriteposition = 0; |
| 652 |
x->x_outunread = 0; |
| 653 |
|
| 654 |
ztout.tv_sec = 0; |
| 655 |
ztout.tv_usec = 0; |
| 656 |
|
| 657 |
x->x_graphic = (int)fdographics; |
| 658 |
|
| 659 |
post( "mp3streamin~ : starting service on port %d", (int)fportno ); |
| 660 |
mp3streamin_startservice(x, (int)fportno); |
| 661 |
|
| 662 |
|
| 663 |
mp3streamin_tilde_mpglib_init(x); |
| 664 |
|
| 665 |
return (x); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
void mp3streamin_tilde_setup(void) |
| 670 |
{ |
| •671 |
logpost(NULL, 4, "%s", mp3streamin_version ); |
| 672 |
mp3streamin_class = class_new(gensym("mp3streamin~"), |
| 673 |
(t_newmethod) mp3streamin_new, (t_method) mp3streamin_free, |
| 674 |
sizeof(t_mp3streamin), CLASS_NOINLET, A_DEFFLOAT, A_DEFFLOAT, A_NULL); |
| 675 |
|
| 676 |
class_addmethod(mp3streamin_class, nullfn, gensym("signal"), 0); |
| 677 |
class_addmethod(mp3streamin_class, (t_method) mp3streamin_dsp, gensym("dsp"), 0); |
| 678 |
} |