| 1 |
/* ------------------------ mp3streamin~ -------------------------------------- */ |
| 2 |
/* */ |
| 3 |
/* Tilde object to receive an mp3-stream sent by a peer using mp3streamout~. */ |
| 4 |
/* Written by Yves Degoyon ([email protected]). */ |
| 5 |
/* Tarballs and updates @ http://ydegoyon.free.fr */ |
| 6 |
/* */ |
| 7 |
/* This program is free software; you can redistribute it and/or */ |
| 8 |
/* modify it under the terms of the GNU General Public License */ |
| 9 |
/* as published by the Free Software Foundation; either version 2 */ |
| 10 |
/* of the License, or (at your option) any later version. */ |
| 11 |
/* */ |
| 12 |
/* See file LICENSE for further informations on licensing terms. */ |
| 13 |
/* */ |
| 14 |
/* This program is distributed in the hope that it will be useful, */ |
| 15 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 16 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 17 |
/* GNU General Public License for more details. */ |
| 18 |
/* */ |
| 19 |
/* You should have received a copy of the GNU General Public License */ |
| 20 |
/* along with this program; if not, write to the Free Software */ |
| 21 |
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 22 |
/* */ |
| 23 |
/* Based on PureData by Miller Puckette and others. */ |
| 24 |
/* Uses the LAME MPEG 1 Layer 3 encoding library (lame_enc.dll) which can */ |
| 25 |
/* be found at http://www.cdex.n3.net. */ |
| 26 |
/* */ |
| 27 |
/* "Repackage sex, your interests." */ |
| 28 |
/* "Somehow, maintain the interest." */ |
| 29 |
/* Gang Of Four -- Natural's Not In It */ |
| 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 /* _WIN32 */ |
| 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" /* mpg123 decoding library from lame 3.92 */ |
| 66 |
#include "mpglib.h" /* mpglib decoding library from lame 3.92 */ |
| 67 |
#include "interface.h" /* mpglib decoding library from lame 3.92 */ |
| 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 /* 128k*/ |
| 72 |
#define LAME_AUDIO_CHUNK_SIZE 1152 |
| 73 |
#define BARHEIGHT 10 |
| 74 |
#define MAX_DECODERS 10 |
| 75 |
|
| 76 |
/* useful debugging functions from mpglib */ |
| 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 |
/* time-out used for select() call */ |
| 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 |
extern void sys_sockerror(char *s); |
| 87 |
|
| 88 |
void mp3streamin_closesocket(int fd) |
| 89 |
{ |
| 90 |
#ifndef _MSC_VER |
| 91 |
if ( close(fd) < 0 ) |
| 92 |
{ |
| 93 |
perror( "close" ); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
post( "mp3streamin~ : closed socket : %d", fd ); |
| 98 |
} |
| 99 |
#endif |
| 100 |
#ifdef _WIN32 |
| 101 |
closesocket(fd); |
| 102 |
#endif |
| 103 |
sys_rmpollfn(fd); |
| 104 |
} |
| 105 |
|
| 106 |
int setsocketoptions(int sockfd) |
| 107 |
{ |
| 108 |
int sockopt = 1; |
| 109 |
if (setsockopt(sockfd, SOL_TCP, TCP_NODELAY, (const char*) &sockopt, sizeof(int)) < 0) |
| 110 |
{ |
| 111 |
post("mp3streamin~ : setsockopt TCP_NODELAY failed"); |
| 112 |
perror( "setsockopt" ); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
post("mp3streamin~ : TCP_NODELAY set"); |
| 118 |
} |
| 119 |
|
| 120 |
#ifndef _MSC_VER |
| 121 |
sockopt = 1; |
| 122 |
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(int)) < 0) |
| 123 |
{ |
| 124 |
post("mp3streamin~ : setsockopt SO_REUSEADDR failed"); |
| 125 |
perror( "setsockopt" ); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
post("mp3streamin~ : setsockopt SO_REUSEADDR done."); |
| 131 |
} |
| 132 |
#endif |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/* ------------------------ mp3streamin~ ----------------------------- */ |
| 138 |
|
| 139 |
static t_class *mp3streamin_class; |
| 140 |
|
| 141 |
typedef struct _mp3streamin |
| 142 |
{ |
| 143 |
t_object x_obj; |
| 144 |
t_int x_instance; |
| 145 |
t_int x_socket; |
| 146 |
t_int x_shutdown; |
| 147 |
t_outlet *x_connectionip; |
| 148 |
t_int x_serversocket; |
| 149 |
t_int x_inpackets; /* number of packets received */ |
| 150 |
t_int x_dpacket; /* displayed packet in status bar */ |
| 151 |
t_int x_packetsize; /* size of the packets */ |
| 152 |
t_int x_pblocks; /* processed blocks */ |
| 153 |
t_int x_graphic; /* indicates if we show a graphic bar */ |
| 154 |
|
| 155 |
void *x_inbuffer; /* accumulation buffer for incoming mp3 frames */ |
| 156 |
t_int x_inwriteposition; |
| 157 |
t_int x_inbuffersize; |
| 158 |
|
| 159 |
t_float *x_outbuffer; /* buffer to store audio decoded data */ |
| 160 |
t_int x_outwriteposition; |
| 161 |
t_int x_outreadposition; |
| 162 |
t_int x_outunread; |
| 163 |
t_int x_outbuffersize; |
| 164 |
|
| 165 |
t_canvas *x_canvas; |
| 166 |
|
| 167 |
t_int x_stream; /* indicates if a stream is connected ( meaning correct input flow ) */ |
| 168 |
t_int x_newstream; /* at first, the stream must provide enough data to start */ |
| 169 |
t_int x_bitrateindex; /* remember the bitrate index */ |
| 170 |
|
| 171 |
} t_mp3streamin; |
| 172 |
|
| 173 |
/* too bad, this needs to be static, |
| 174 |
handling an array to enable several decoders in pd */ |
| 175 |
static MPSTR mp[MAX_DECODERS]; /* decoder buffer */ |
| 176 |
int nbinstances = 0; |
| 177 |
|
| 178 |
void mp3streamin_tilde_mpglib_init(t_mp3streamin *x) |
| 179 |
{ |
| 180 |
int ret; |
| 181 |
|
| 182 |
InitMP3(&mp[x->x_instance]); |
| 183 |
} |
| 184 |
|
| 185 |
static int mp3streamin_decode_input(t_mp3streamin *x) |
| 186 |
{ |
| 187 |
int i; |
| 188 |
int alength = 0; |
| 189 |
struct frame hframe; |
| 190 |
unsigned int a,b,c,d; |
| 191 |
unsigned long cheader; |
| 192 |
static char out[8192]; |
| 193 |
signed short int *p = (signed short int *) out; |
| 194 |
int pbytes; |
| 195 |
int ret; |
| 196 |
|
| 197 |
if ( !x->x_shutdown ) |
| 198 |
{ |
| 199 |
/* decode first 4 bytes as the header */ |
| 200 |
a = *((unsigned char*)x->x_inbuffer); |
| 201 |
b = *((unsigned char*)x->x_inbuffer+1); |
| 202 |
c = *((unsigned char*)x->x_inbuffer+2); |
| 203 |
d = *((unsigned char*)x->x_inbuffer+3); |
| 204 |
|
| 205 |
cheader = 0; |
| 206 |
cheader = a; |
| 207 |
cheader <<= 8; |
| 208 |
cheader |= b; |
| 209 |
cheader <<= 8; |
| 210 |
cheader |= c; |
| 211 |
cheader <<= 8; |
| 212 |
cheader |= d; |
| 213 |
if ( head_check( cheader, 0 ) ) |
| 214 |
{ |
| 215 |
// post( "mp3streamin~ : valid header ( packet=%d)", x->x_inpackets ); |
| 216 |
decode_header( &hframe, cheader ); |
| 217 |
// print_header_compact( &hframe ); |
| 218 |
// when the bitrate change, reinit decoder |
| 219 |
if ( x->x_bitrateindex != hframe.bitrate_index ) |
| 220 |
{ |
| 221 |
ExitMP3(&mp[x->x_instance]); |
| 222 |
InitMP3(&mp[x->x_instance]); |
| 223 |
x->x_bitrateindex = hframe.bitrate_index; |
| 224 |
} |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
post( "mp3streamin~ : error : mp3 packet received without header" ); |
| 229 |
// ignore data |
| 230 |
x->x_inwriteposition = 0; |
| 231 |
x->x_inpackets--; |
| 232 |
return( -1 ); |
| 233 |
} |
| 234 |
|
| 235 |
// post( "mp3streamin~ : decoding %d bytes framesize=%d", x->x_inwriteposition, hframe.framesize ); |
| 236 |
ret = |
| 237 |
decodeMP3(&mp[x->x_instance], (unsigned char*)x->x_inbuffer, |
| 238 |
x->x_inwriteposition, (char *) p, sizeof(out), &pbytes); |
| 239 |
|
| 240 |
switch (ret) |
| 241 |
{ |
| 242 |
case MP3_OK: |
| 243 |
switch (mp[x->x_instance].fr.stereo) |
| 244 |
{ |
| 245 |
case 1: |
| 246 |
case 2: |
| 247 |
alength = ((mp[x->x_instance].fr.stereo==1)?pbytes >> 1 : pbytes >> 2); |
| 248 |
// post( "mp3streamin~ : processed %d samples", alength ); |
| 249 |
// update outbuffer contents |
| 250 |
for ( i=0; i<alength; i++ ) |
| 251 |
{ |
| 252 |
if ( x->x_outunread >= x->x_outbuffersize-2 ) |
| 253 |
{ |
| 254 |
post( "mp3streamin~ : too much input ... ignored" ); |
| 255 |
continue; |
| 256 |
} |
| 257 |
*(x->x_outbuffer+x->x_outwriteposition) = ((t_float)(*p++))/32767.0; |
| 258 |
x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize; |
| 259 |
*(x->x_outbuffer+x->x_outwriteposition) = |
| 260 |
((mp[x->x_instance].fr.stereo==2)?((t_float)(*p++))/32767.0 : 0.0); |
| 261 |
x->x_outwriteposition = (x->x_outwriteposition + 1)%x->x_outbuffersize; |
| 262 |
x->x_outunread+=2; |
| 263 |
|
| 264 |
if ( x->x_outunread > MIN_AUDIO_INPUT && !x->x_stream ) |
| 265 |
{ |
| 266 |
post( "mp3streamin~ : stream connected." ); |
| 267 |
x->x_stream = 1; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
break; |
| 272 |
default: |
| 273 |
alength = -1; |
| 274 |
break; |
| 275 |
} |
| 276 |
break; |
| 277 |
|
| 278 |
case MP3_NEED_MORE: |
| 279 |
post( "mp3streamin~ : retry lame decoding (more data needed)." ); |
| 280 |
alength = 0; |
| 281 |
break; |
| 282 |
|
| 283 |
case MP3_ERR: |
| 284 |
post( "mp3streamin~ : lame decoding failed." ); |
| 285 |
alength = -1; |
| 286 |
break; |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
x->x_inwriteposition = 0; |
| 291 |
} |
| 292 |
else |
| 293 |
{ |
| 294 |
if ( x->x_outunread == 0 ) |
| 295 |
{ |
| 296 |
post( "mp3streamin~ : connection closed" ); |
| 297 |
mp3streamin_closesocket(x->x_socket); |
| 298 |
x->x_stream = 0; |
| 299 |
x->x_newstream = 0; |
| 300 |
x->x_inpackets = 0; |
| 301 |
x->x_inwriteposition = 0; |
| 302 |
x->x_bitrateindex = -1; |
| 303 |
x->x_socket=-1; |
| 304 |
outlet_symbol( x->x_connectionip, gensym("") ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if ( x->x_graphic && glist_isvisible( x->x_canvas ) ) |
| 309 |
{ |
| 310 |
/* update graphical read status */ |
| 311 |
if ( x->x_inpackets != x->x_dpacket ) |
| 312 |
{ |
| 313 |
char color[32]; |
| 314 |
int minpackets = ( MIN_AUDIO_INPUT/LAME_AUDIO_CHUNK_SIZE )-2; // audio loop has eaten some already |
| 315 |
|
| 316 |
|
| 317 |
sys_vgui(".x%lx.c delete rectangle %xSTATUS\n", x->x_canvas, x ); |
| 318 |
sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x ); |
| 319 |
if ( x->x_outunread > 0 ) |
| 320 |
{ |
| 321 |
t_int width; |
| 322 |
|
| 323 |
if ( x->x_inpackets < (MIN_AUDIO_INPUT/LAME_AUDIO_CHUNK_SIZE)/2 ) |
| 324 |
{ |
| 325 |
strcpy( color, "red" ); |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
strcpy( color, "lightgreen" ); |
| 330 |
} |
| 331 |
width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); |
| 332 |
sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %xSTATUS\n", |
| 333 |
x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1, |
| 334 |
x->x_obj.te_xpix+(x->x_inpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 335 |
x->x_obj.te_ypix - 1, color, x ); |
| 336 |
sys_vgui(".x%lx.c create line %d %d %d %d -fill red -tags %xTHRESHOLD\n", |
| 337 |
x->x_canvas, x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 338 |
x->x_obj.te_ypix-BARHEIGHT-1, |
| 339 |
x->x_obj.te_xpix+(minpackets*x->x_packetsize*width)/INPUT_BUFFER_SIZE, |
| 340 |
x->x_obj.te_ypix-1, x ); |
| 341 |
x->x_dpacket = x->x_inpackets; |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
if ( x->x_shutdown ) |
| 346 |
{ |
| 347 |
x->x_shutdown=0; |
| 348 |
sys_vgui(".x%lx.c delete rectangle %xPBAR\n", x->x_canvas, x ); |
| 349 |
sys_vgui(".x%lx.c delete line %xTHRESHOLD\n", x->x_canvas, x ); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
return alength; |
| 355 |
} |
| 356 |
|
| 357 |
static void mp3streamin_recv(t_mp3streamin *x) |
| 358 |
{ |
| 359 |
int ret; |
| 360 |
|
| 361 |
if ( ( ret = recv(x->x_socket, (void*) (x->x_inbuffer + x->x_inwriteposition), |
| 362 |
(size_t)((x->x_inbuffersize-x->x_inwriteposition)), |
| 363 |
MSG_NOSIGNAL) ) < 0 ) |
| 364 |
{ |
| 365 |
post( "mp3_streamin~ : receive error" ); |
| 366 |
perror( "recv" ); |
| 367 |
return; |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
// post( "streamin~ : received %d bytes at %d on %d ( up to %d)", |
| 372 |
// ret, x->x_inwriteposition, x->x_socket, |
| 373 |
// x->x_inbuffersize-x->x_inwriteposition*sizeof( unsigned long) ); |
| 374 |
|
| 375 |
if ( ret == 0 ) |
| 376 |
{ |
| 377 |
/* initiate the shutdown phase */ |
| 378 |
x->x_shutdown=1; |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
// check we don't overflow input buffer |
| 383 |
if ( (x->x_inpackets+1)*x->x_packetsize > x->x_inbuffersize ) |
| 384 |
{ |
| 385 |
post( "mp3streamin~ : too much input...resetting" ); |
| 386 |
x->x_inpackets=0; |
| 387 |
x->x_inwriteposition=0; |
| 388 |
return; |
| 389 |
} |
| 390 |
x->x_inpackets++; |
| 391 |
x->x_packetsize=ret; |
| 392 |
if ( x->x_inpackets % 100 == 0 ) |
| 393 |
{ |
| 394 |
// post( "mp3streamin~ : received %d packets", x->x_inpackets ); |
| 395 |
} |
| 396 |
x->x_inwriteposition += ret; |
| 397 |
} |
| 398 |
|
| 399 |
mp3streamin_decode_input(x); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static void mp3streamin_acceptconnection(t_mp3streamin *x) |
| 404 |
{ |
| 405 |
struct sockaddr_in incomer_address; |
| 406 |
int sockaddrl = (int) sizeof( struct sockaddr ); |
| 407 |
|
| 408 |
int fd = accept(x->x_serversocket, (struct sockaddr*)&incomer_address, &sockaddrl ); |
| 409 |
|
| 410 |
if (fd < 0) |
| 411 |
{ |
| 412 |
post("mp3streamin~: accept failed"); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
if (x->x_socket > 0) |
| 417 |
{ |
| 418 |
sys_addpollfn(fd, (t_fdpollfn)mp3streamin_recv, x); |
| 419 |
if ( x->x_outunread != 0 ) |
| 420 |
{ |
| 421 |
post("mp3streamin~: still have some data to decode, retry later you %s.", |
| 422 |
inet_ntoa( incomer_address.sin_addr )); |
| 423 |
mp3streamin_closesocket( fd ); |
| 424 |
return; |
| 425 |
} |
| 426 |
post("mp3streamin~: the source has changed to %s.", |
| 427 |
inet_ntoa( incomer_address.sin_addr )); |
| 428 |
mp3streamin_closesocket(x->x_socket); |
| 429 |
} |
| 430 |
|
| 431 |
x->x_socket = fd; |
| 432 |
sys_addpollfn(x->x_socket, (t_fdpollfn)mp3streamin_recv, x); |
| 433 |
outlet_symbol( x->x_connectionip, gensym( inet_ntoa( incomer_address.sin_addr) ) ); |
| 434 |
|
| 435 |
if ( x->x_graphic && glist_isvisible( x->x_canvas ) ) |
| 436 |
{ |
| 437 |
t_int width; |
| 438 |
|
| 439 |
width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); |
| 440 |
sys_vgui(".x%lx.c create rectangle %d %d %d %d -fill lightblue -tags %xPBAR\n", |
| 441 |
x->x_canvas, x->x_obj.te_xpix, x->x_obj.te_ypix-BARHEIGHT-1, |
| 442 |
x->x_obj.te_xpix + width, x->x_obj.te_ypix - 1, x ); |
| 443 |
} |
| 444 |
x->x_stream = 0; |
| 445 |
x->x_newstream = 1; |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
static int mp3streamin_startservice(t_mp3streamin* x, int portno) |
| 451 |
{ |
| 452 |
struct sockaddr_in server; |
| 453 |
int sockfd; |
| 454 |
|
| 455 |
/* create a socket */ |
| 456 |
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 457 |
|
| 458 |
if (sockfd < 0) |
| 459 |
{ |
| 460 |
sys_sockerror("socket"); |
| 461 |
return (0); |
| 462 |
} |
| 463 |
server.sin_family = AF_INET; |
| 464 |
server.sin_addr.s_addr = INADDR_ANY; |
| 465 |
|
| 466 |
/* assign server port number */ |
| 467 |
server.sin_port = htons((u_short)portno); |
| 468 |
post("listening to port number %d", portno); |
| 469 |
|
| 470 |
setsocketoptions(sockfd); |
| 471 |
|
| 472 |
/* name the socket */ |
| 473 |
if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) |
| 474 |
{ |
| 475 |
sys_sockerror("bind"); |
| 476 |
mp3streamin_closesocket(sockfd); |
| 477 |
return (0); |
| 478 |
} |
| 479 |
|
| 480 |
if (listen(sockfd, 5) < 0) |
| 481 |
{ |
| 482 |
sys_sockerror("listen"); |
| 483 |
mp3streamin_closesocket(sockfd); |
| 484 |
} |
| 485 |
else |
| 486 |
{ |
| 487 |
x->x_serversocket = sockfd; |
| 488 |
sys_addpollfn(x->x_serversocket, (t_fdpollfn)mp3streamin_acceptconnection, x); |
| 489 |
} |
| 490 |
|
| 491 |
return 1; |
| 492 |
} |
| 493 |
|
| 494 |
static void mp3streamin_free(t_mp3streamin *x) |
| 495 |
{ |
| 496 |
post( "mp3streamin~ : free %x", x ); |
| 497 |
if (x->x_serversocket > 0) |
| 498 |
{ |
| 499 |
post( "mp3streamin~ : closing server socket" ); |
| 500 |
mp3streamin_closesocket(x->x_serversocket); |
| 501 |
x->x_serversocket = -1; |
| 502 |
} |
| 503 |
if (x->x_socket > 0) |
| 504 |
{ |
| 505 |
post( "mp3streamin~ : closing socket" ); |
| 506 |
mp3streamin_closesocket(x->x_socket); |
| 507 |
x->x_socket = -1; |
| 508 |
} |
| 509 |
if ( x->x_inbuffer ) freebytes( x->x_inbuffer, x->x_inbuffersize ); |
| 510 |
if ( x->x_outbuffer ) freebytes( x->x_outbuffer, x->x_outbuffersize*sizeof(t_float) ); |
| 511 |
if ( x->x_instance == nbinstances-1 ) |
| 512 |
{ |
| 513 |
nbinstances--; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
static t_int *mp3streamin_perform(t_int *w) |
| 518 |
{ |
| 519 |
t_mp3streamin *x = (t_mp3streamin*) (w[1]); |
| 520 |
t_float *out1 = (t_float *)(w[2]); |
| 521 |
t_float *out2 = (t_float *)(w[3]); |
| 522 |
int n = (int)(w[4]); |
| 523 |
int bsize = n; |
| 524 |
int ret; |
| 525 |
int i = 0; |
| 526 |
|
| 527 |
while( n-- ) |
| 528 |
{ |
| 529 |
if ( ( ( x->x_outunread > MIN_AUDIO_INPUT ) && x->x_newstream ) || // wait the buffer to load |
| 530 |
( ( x->x_shutdown ) && ( x->x_outunread >= 2 ) ) || // clean disconnection |
| 531 |
( x->x_stream ) // check that the stream provides enough data |
| 532 |
) |
| 533 |
{ |
| 534 |
if ( x->x_newstream && !x->x_shutdown ) |
| 535 |
{ |
| 536 |
x->x_newstream = 0; |
| 537 |
x->x_stream = 1; |
| 538 |
} |
| 539 |
*out1++=*(x->x_outbuffer+x->x_outreadposition); |
| 540 |
x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize; |
| 541 |
*out2++=*(x->x_outbuffer+x->x_outreadposition); |
| 542 |
x->x_outreadposition = (x->x_outreadposition + 1)%x->x_outbuffersize; |
| 543 |
x->x_outunread-=2; |
| 544 |
if ( n == 1 ) x->x_pblocks++; |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
*out1++=0.0; |
| 549 |
*out2++=0.0; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if ( ( x->x_outunread <= MIN_AUDIO_INPUT/10 ) && ( x->x_stream ) ) |
| 554 |
{ |
| 555 |
post( "mp3streamin~ : stream lost (too little input)" ); |
| 556 |
x->x_stream = 0; |
| 557 |
x->x_newstream = 1; // waiting for a new stream |
| 558 |
} |
| 559 |
|
| 560 |
if ( x->x_pblocks == LAME_AUDIO_CHUNK_SIZE/bsize ) |
| 561 |
{ |
| 562 |
x->x_inpackets--; |
| 563 |
x->x_pblocks = 0; |
| 564 |
} |
| 565 |
|
| 566 |
#ifdef DO_MY_OWN_SELECT |
| 567 |
// check new incoming data |
| 568 |
if ( x->x_socket > 0 ) |
| 569 |
{ |
| 570 |
fd_set readset; |
| 571 |
fd_set exceptset; |
| 572 |
|
| 573 |
FD_ZERO(&readset); |
| 574 |
FD_ZERO(&exceptset); |
| 575 |
FD_SET(x->x_socket, &readset ); |
| 576 |
FD_SET(x->x_socket, &exceptset ); |
| 577 |
|
| 578 |
if ( select( maxfd+1, &readset, NULL, &exceptset, &ztout ) >0 ) |
| 579 |
{ |
| 580 |
if ( FD_ISSET( x->x_socket, &readset) || FD_ISSET( x->x_socket, &exceptset ) ) |
| 581 |
{ |
| 582 |
/* receive data or error and decode it */ |
| 583 |
mp3streamin_recv(x); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
#endif |
| 588 |
return (w+5); |
| 589 |
} |
| 590 |
|
| 591 |
static void mp3streamin_dsp(t_mp3streamin *x, t_signal **sp) |
| 592 |
{ |
| 593 |
dsp_add(mp3streamin_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
static void *mp3streamin_new(t_floatarg fportno, t_floatarg fdographics) |
| 598 |
{ |
| 599 |
t_mp3streamin *x; |
| 600 |
int i; |
| 601 |
|
| 602 |
if ( fportno < 0 || fportno > 65535 ) |
| 603 |
{ |
| 604 |
post( "mp3streamin~ : error : wrong portnumber : %d", (int)fportno ); |
| 605 |
return NULL; |
| 606 |
} |
| 607 |
if ( ((int)fdographics != 0) && ((int)fdographics != 1.) ) |
| 608 |
{ |
| 609 |
post( "mp3streamin~ : error : constructor : mp3streamin~ <portnumber> [graphic flag = 0 | 1 ] ( got = %f)", fdographics ); |
| 610 |
return NULL; |
| 611 |
} |
| 612 |
|
| 613 |
x = (t_mp3streamin *)pd_new(mp3streamin_class); |
| 614 |
post( "mp3streamin~ : new %x (instance = %d) %d", x, nbinstances, sizeof( MPSTR ) ); |
| 615 |
outlet_new(&x->x_obj, &s_signal); |
| 616 |
outlet_new(&x->x_obj, &s_signal); |
| 617 |
x->x_connectionip = outlet_new(&x->x_obj, &s_symbol); |
| 618 |
|
| 619 |
x->x_serversocket = -1; |
| 620 |
x->x_socket = -1; |
| 621 |
x->x_shutdown = 0; |
| 622 |
x->x_inpackets = 0; |
| 623 |
x->x_dpacket = -1; |
| 624 |
|
| 625 |
x->x_canvas = canvas_getcurrent(); |
| 626 |
|
| 627 |
x->x_inbuffersize = INPUT_BUFFER_SIZE; |
| 628 |
x->x_outbuffersize = OUTPUT_BUFFER_SIZE; |
| 629 |
x->x_inbuffer = (char*) getbytes( x->x_inbuffersize ); |
| 630 |
memset( x->x_inbuffer, 0x0, INPUT_BUFFER_SIZE ); |
| 631 |
x->x_outbuffer = (t_float*) getbytes( x->x_outbuffersize*sizeof(t_float) ); |
| 632 |
memset( x->x_outbuffer, 0x0, OUTPUT_BUFFER_SIZE ); |
| 633 |
|
| 634 |
if ( !x->x_inbuffer || !x->x_outbuffer ) |
| 635 |
{ |
| 636 |
post( "mp3streamin~ : could not allocate buffers." ); |
| 637 |
return NULL; |
| 638 |
} |
| 639 |
|
| 640 |
if ( nbinstances < MAX_DECODERS ) |
| 641 |
{ |
| 642 |
x->x_instance = nbinstances++; |
| 643 |
} |
| 644 |
else |
| 645 |
{ |
| 646 |
post( "mp3streamin~ : cannot create more decoders (memory issues), sorry" ); |
| 647 |
return NULL; |
| 648 |
} |
| 649 |
|
| 650 |
x->x_inwriteposition = 0; |
| 651 |
x->x_outreadposition = 0; |
| 652 |
x->x_outwriteposition = 0; |
| 653 |
x->x_outunread = 0; |
| 654 |
|
| 655 |
ztout.tv_sec = 0; |
| 656 |
ztout.tv_usec = 0; |
| 657 |
|
| 658 |
x->x_graphic = (int)fdographics; |
| 659 |
|
| 660 |
post( "mp3streamin~ : starting service on port %d", (int)fportno ); |
| 661 |
mp3streamin_startservice(x, (int)fportno); |
| 662 |
|
| 663 |
// init lame decoder |
| 664 |
mp3streamin_tilde_mpglib_init(x); |
| 665 |
|
| 666 |
return (x); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
void mp3streamin_tilde_setup(void) |
| 671 |
{ |
| 672 |
logpost(NULL, 4, mp3streamin_version ); |
| 673 |
mp3streamin_class = class_new(gensym("mp3streamin~"), |
| 674 |
(t_newmethod) mp3streamin_new, (t_method) mp3streamin_free, |
| 675 |
sizeof(t_mp3streamin), CLASS_NOINLET, A_DEFFLOAT, A_DEFFLOAT, A_NULL); |
| 676 |
|
| 677 |
class_addmethod(mp3streamin_class, nullfn, gensym("signal"), 0); |
| 678 |
class_addmethod(mp3streamin_class, (t_method) mp3streamin_dsp, gensym("dsp"), 0); |
| 679 |
} |