Debian Codegraph @

mp3fileout~.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* ------------------------ mp3fileout~ --------------------------------------- */
2 /* */
3 /* Tilde object to send an mp3 file to a peer using mp3streamin~ */
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 /* "See mass murder on a scale you've never seen" */
28 /* "And all the one who tried hard to succeed" */
29 /* You know who, don't you ??? */
30 /* ---------------------------------------------------------------------------- */
31
32
33 #include <m_pd.h>
34 #include <m_imp.h>
35 #include <s_stuff.h>
36 #include <g_canvas.h>
37
38 #include <sys/types.h>
39 #include <string.h>
40 #ifdef _WIN32
41 #include <winsock.h>
42 #include <io.h>
43 #include <fcntl.h>
44 #else
45 #include <sys/socket.h>
46 #include <errno.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <sys/time.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <stdio.h>
55 #define SOCKET_ERROR -1
56 #endif /* _WIN32 */
57
58 #if defined(__APPLE__) || defined(_WIN32)
59 #define MSG_NOSIGNAL 0
60 #endif
61
62 /* support older Pd versions without sys_fopen(), sys_fclose(), and, sys_close() */
63 #if PD_MAJOR_VERSION == 0 && PD_MINOR_VERSION < 44
64 #define sys_fopen fopen
65 #define sys_fclose fclose
66 #define sys_close close
67 #endif
68
69 #include "mpg123.h" /* mpg123 decoding library from lame 3.92 */
70 #include "mpglib.h" /* mpglib decoding library from lame 3.92 */
71 #include "interface.h" /* mpglib decoding library from lame 3.92 */
72
73 #define INPUT_BUFFER_SIZE 32768
74 #define OUTPUT_BUFFER_SIZE 32768
75 #define MAX_FRAME_SIZE 1152
76
77 /* useful debugging functions from mpglib */
78 extern int decode_header( struct frame* fr, unsigned long newhead );
79 extern void print_header_compact( struct frame* fr );
80 extern int head_check( unsigned long head, int check_layer );
81
82 /* time-out used for select() call */
83 static struct timeval ztout;
84
85 static char *mp3fileout_version = "mp3fileout~: mp3 file streamer version 0.2, written by [email protected]";
86
87 extern void sys_sockerror(char *s);
88
89 void mp3fileout_closesocket(int fd)
90 {
91 #ifndef _MSC_VER
92 if ( close(fd) < 0 )
93 {
94 perror( "close" );
95 }
96 else
97 {
98 post( "mp3fileout~ : closed socket : %d", fd );
99 }
100 #endif
101 #ifdef _WIN32
102 closesocket(fd);
103 #else
104 sys_rmpollfn(fd);
105 #endif
106 }
107
108 /* ------------------------ mp3fileout~ ----------------------------- */
109
110 static t_class *mp3fileout_class;
111
112 typedef struct _mp3fileout
113 {
114 t_object x_obj;
115 t_int x_socket;
116 t_int x_fd; /* file descriptor for the mp3 file */
117 t_int x_eof; /* end of file is reached */
118 t_int x_emit; /* indicates the ability to emit */
119 t_int x_nbwaitloops;/* synchronization cycles count */
120 t_int x_blocksize; /* actual blocksize */
121
122 void *x_inbuffer; /* accumulation buffer for read mp3 frames */
123 t_int x_inwriteposition;
124 t_int x_inbuffersize;
125 t_int x_framesize;
126 t_int x_offset; /* offset used for decoding */
127 t_int x_nbloops; /* number of perform loops */
128
129 void *x_outbuffer; /* buffer to be emitted */
130 t_int x_outframes; /* number of frames emitted */
131 t_int x_outbuffersize;
132 t_int x_outavable; /* number of available bytes to emit */
133
134 t_canvas *x_canvas;
135
136 t_outlet *x_connected; /* indicates state of the connection */
137 t_outlet *x_endreached;/* indicates the end of file */
138 t_outlet *x_frames; /* indicates the number of frames emitted */
139
140 } t_mp3fileout;
141
142 static int mp3fileout_search_header(t_mp3fileout *x)
143 {
144 t_int i;
145 t_int length = 0;
146 struct frame hframe;
147 unsigned long cheader;
148 t_int ret = sizeof( unsigned long);
149 t_int foffset = 0;
150 unsigned int a,b,c,d;
151 unsigned char buf[sizeof(unsigned long)];
152 t_float nbsamplesframe = 0;
153
154 while( ret>0 )
155 {
156 ret = read( x->x_fd, (void *)buf, sizeof( unsigned long ) );
157
158 foffset+=ret;
159
160 if ( ret>0 )
161 {
162 /* check for a valid header */
163 a = buf[0];
164 b = buf[1];
165 c = buf[2];
166 d = buf[3];
167
168 cheader = 0;
169 cheader = a;
170 cheader <<= 8;
171 cheader |= b;
172 cheader <<= 8;
173 cheader |= c;
174 cheader <<= 8;
175 cheader |= d;
176 if ( head_check( cheader, 0 ) )
177 {
178 decode_header( &hframe, cheader );
179 // print_header_compact( &hframe );
180 x->x_framesize = hframe.framesize;
181 nbsamplesframe = ( hframe.mpeg25 ? 576 : 1152 );
182 x->x_nbwaitloops = (int)(nbsamplesframe/x->x_blocksize);
183 if ( x->x_nbwaitloops == 0 ) x->x_nbwaitloops = 1;
184 // post ( "mp3fileout~ : will wait %d loops", x->x_nbwaitloops );
185
186 // rewind file to the start of the frame
187 if ( lseek( x->x_fd, -sizeof(unsigned long), SEEK_CUR ) < 0 )
188 {
189 post( "mp3fileout~ : could not rewind file." );
190 }
191 if ( x->x_outframes == 0 )
192 {
193 post( "mp3fileout~ : found firstframe @ %d", foffset );
194 }
195 break;
196 }
197 // post( "mp3fileout~ : read %d bytes.", ret );
198 }
199 else
200 {
201 if ( ret < 0 )
202 {
203 post( "mp3fileout~ : error encountered ( ret=%d )...file reading done.", ret );
204 perror( "read" );
205 x->x_eof = 1;
206 }
207 else
208 {
209 post( "mp3fileout~ : file reading done.", ret );
210 x->x_eof = 1;
211 outlet_bang( x->x_endreached );
212 }
213 return -1;
214 }
215
216 }
217
218 return x->x_framesize;
219 }
220
221 static int mp3fileout_read_frame(t_mp3fileout *x)
222 {
223 int size, ret;
224
225 if ( x->x_fd > 0 && !x->x_eof)
226 {
227 if ( ( size = mp3fileout_search_header( x ) ) > 0 )
228 {
229 if ( size+sizeof(unsigned long) > INPUT_BUFFER_SIZE )
230 {
231 post( "mp3fileout~ : cannot read frame : size too big : %d", size );
232 return -1;
233 }
234 // post( "mp3fileout~ : reading a frame : size : %d", size );
235 ret = read( x->x_fd, x->x_inbuffer, size+sizeof(unsigned long) );
236
237 if ( ret>0 )
238 {
239 memcpy( x->x_outbuffer, x->x_inbuffer, ret );
240 x->x_outavable += ret;
241 return ret;
242 }
243 else
244 {
245 return -1;
246 }
247 }
248 else
249 {
250 return -1;
251 }
252 }
253 else
254 {
255 return -1;
256 }
257 }
258
259 static int mp3fileout_send_frame(t_mp3fileout *x)
260 {
261 int ret=0;
262
263 if ( x->x_socket > 0 && x->x_emit )
264 {
265 if ( ( ret = send( x->x_socket, x->x_outbuffer, x->x_outavable, MSG_NOSIGNAL ) ) < 0 )
266 {
267 post( "mp3fileout~ : connection lost." );
268 perror( "send" );
269 x->x_socket = -1;
270 x->x_emit = 0;
271 return -1;
272 }
273 else
274 {
275 memcpy( x->x_outbuffer, x->x_outbuffer+ret, x->x_outbuffersize-ret );
276 x->x_outavable -= ret;
277 x->x_outframes++;
278 outlet_float( x->x_frames, x->x_outframes );
279 // post( "mp3fileout~ : sent %d bytes, x->x_outavable : %d", ret, x->x_outavable );
280 }
281
282 }
283 else
284 {
285 // artificially empty buffer
286 x->x_outavable = 0;
287 }
288 return ret;
289 }
290
291 static void mp3fileout_free(t_mp3fileout *x)
292 {
293 if (x->x_socket > 0)
294 {
295 post( "mp3fileout~ : closing socket" );
296 mp3fileout_closesocket(x->x_socket);
297 x->x_socket = -1;
298 }
299 if ( x->x_fd > 0 )
300 {
301 if ( close( x->x_fd ) < 0 )
302 {
303 post( "mp3fileout~ : could not close file." );
304 perror( "close" );
305 }
306 }
307 if ( x->x_inbuffer ) freebytes( x->x_inbuffer, x->x_inbuffersize );
308 }
309
310 static t_int *mp3fileout_perform(t_int *w)
311 {
312 t_mp3fileout *x = (t_mp3fileout*) (w[1]);
313 int ret;
314 int i = 0;
315
316 x->x_blocksize = (t_int)(w[2]);
317 // check new incoming data
318 if ( x->x_socket > 0 )
319 {
320 if ( x->x_nbloops % x->x_nbwaitloops == 0 )
321 {
322 /* read a frame in the file */
323 if ( mp3fileout_read_frame(x) > 0 )
324 {
325 /* send the frame to the peer */
326 mp3fileout_send_frame(x);
327 }
328 }
329 x->x_nbloops = ( x->x_nbloops+1 ) % x->x_nbwaitloops;
330 }
331 return (w+3);
332 }
333
334 static void mp3fileout_dsp(t_mp3fileout *x, t_signal **sp)
335 {
336 dsp_add(mp3fileout_perform, 2, x, sp[0]->s_n);
337 }
338
339 /* start streaming */
340 static void mp3fileout_start(t_mp3fileout *x)
341 {
342 x->x_emit = 1;
343 if ( x->x_fd > 0 )
344 {
345 // reset file pointer
346 if ( lseek( x->x_fd, 0, SEEK_SET ) < 0 )
347 {
348 post ( "mp3fileout~ : could not reset file pointer.");
349 x->x_eof = 1;
350 return;
351 }
352 x->x_eof = 0;
353 x->x_outframes = 0;
354 outlet_float( x->x_frames, x->x_outframes );
355 }
356 }
357
358 /* resume file reading */
359 static void mp3fileout_resume(t_mp3fileout *x)
360 {
361 x->x_emit = 1;
362 }
363
364 /* seek in file */
365 static void mp3fileout_seek(t_mp3fileout *x, t_floatarg foffset)
366 {
367 if ( foffset < 0 )
368 {
369 post( "mp3fileout~ : wrong offset.");
370 return;
371 }
372 if ( x->x_fd > 0 )
373 {
374 // reset file pointer
375 if ( lseek( x->x_fd, (int)foffset, SEEK_SET ) < 0 )
376 {
377 post ( "mp3fileout~ : could not reset file pointer.");
378 x->x_eof = 1;
379 return;
380 }
381 x->x_eof = 0;
382 }
383 }
384
385 /* stop streaming */
386 static void mp3fileout_stop(t_mp3fileout *x)
387 {
388 x->x_emit = 0;
389 }
390
391 /* open mp3 file */
392 static void mp3fileout_open(t_mp3fileout *x, t_symbol *filename)
393 {
394 // first close previous file
395 if ( x->x_fd > 0 )
396 {
397 if ( close( x->x_fd ) < 0 )
398 {
399 post( "mp3fileout~ : could not close file." );
400 perror( "close" );
401 }
402 x->x_outframes = 0;
403 outlet_float( x->x_frames, x->x_outframes );
404 }
405
406 if ( ( x->x_fd = sys_open( filename->s_name, O_RDONLY ) ) < 0 )
407 {
408 post( "mp3fileout~ : could not open file : %s", filename->s_name );
409 perror( "open" );
410 x->x_eof = 1;
411 }
412 else
413 {
414 x->x_eof = 0;
415 post( "mp3fileout~ : opened file : %s ( fd = %d )", filename->s_name, x->x_fd );
416 }
417 }
418
419 /* connect to the peer */
420 static void mp3fileout_connect(t_mp3fileout *x, t_symbol *hostname, t_floatarg fportno)
421 {
422 struct sockaddr_in csocket;
423 struct hostent *hp;
424 int portno = fportno; /* get port from message box */
425
426 /* variables used for communication with the peer */
427 unsigned int len;
428 int sockfd;
429
430 #ifdef _WIN32
431 unsigned int ret;
432 #else
433 int ret;
434 #endif
435
436 if (x->x_socket >= 0)
437 {
438 error("mp3fileout~: already connected");
439 return;
440 }
441
442 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
443 if (sockfd < 0)
444 {
445 error("mp3fileout~: internal error while attempting to open socket");
446 return;
447 }
448
449 /* connect socket using hostname provided in command line */
450 csocket.sin_family = AF_INET;
451 hp = gethostbyname(hostname->s_name);
452 if (hp == 0)
453 {
454 post("mp3fileout~: bad host?");
455 #ifdef _WIN32
456 closesocket(sockfd);
457 #else
458 close(sockfd);
459 #endif
460 return;
461 }
462 memcpy((char *)&csocket.sin_addr, (char *)hp->h_addr, hp->h_length);
463
464 /* assign client port number */
465 csocket.sin_port = htons((unsigned short)portno);
466
467 /* try to connect. */
468 post("mp3fileout~: connecting to port %d", portno);
469 if (connect(sockfd, (struct sockaddr *) &csocket, sizeof (csocket)) < 0)
470 {
471 error("mp3fileout~: connection failed!\n");
472 #ifdef _WIN32
473 closesocket(sockfd);
474 #else
475 close(sockfd);
476 #endif
477 return;
478 }
479
480 x->x_socket = sockfd;
481 x->x_outframes = 0;
482 outlet_float( x->x_frames, x->x_outframes );
483 outlet_float( x->x_connected, 1 );
484 post( "mp3fileout~ : connected to peer" );
485
486 }
487
488 /* close connection to the peer */
489 static void mp3fileout_disconnect(t_mp3fileout *x)
490 {
491
492 int err = -1;
493 if(x->x_socket >= 0) /* close socket */
494 {
495 #ifdef _WIN32
496 closesocket(x->x_socket);
497 #else
498 close(x->x_socket);
499 #endif
500 x->x_socket = -1;
501 outlet_float( x->x_connected, 0 );
502 x->x_outframes = 0;
503 outlet_float( x->x_frames, x->x_outframes );
504 post("mp3fileout~: connection closed");
505 }
506 }
507
508 static void *mp3fileout_new(void)
509 {
510 t_mp3fileout *x;
511 int i;
512
513 x = (t_mp3fileout *)pd_new(mp3fileout_class);
514 x->x_connected = outlet_new( &x->x_obj, &s_float );
515 x->x_frames = outlet_new( &x->x_obj, &s_float );
516 x->x_endreached = outlet_new( &x->x_obj, &s_bang );
517
518 x->x_socket = -1;
519 x->x_fd = -1;
520 x->x_eof = 0;
521 x->x_canvas = canvas_getcurrent();
522
523 x->x_offset = 0;
524 x->x_inbuffersize = INPUT_BUFFER_SIZE;
525 x->x_inbuffer = (char*) getbytes( x->x_inbuffersize );
526 if ( !x->x_inbuffer )
527 {
528 post( "mp3fileout~ : could not allocate buffers." );
529 return NULL;
530 }
531 memset( x->x_inbuffer, 0x0, INPUT_BUFFER_SIZE );
532
533 x->x_outbuffersize = OUTPUT_BUFFER_SIZE;
534 x->x_outbuffer = (char*) getbytes( x->x_outbuffersize );
535 if ( !x->x_outbuffer )
536 {
537 post( "mp3fileout~ : could not allocate buffers." );
538 return NULL;
539 }
540 memset( x->x_outbuffer, 0x0, OUTPUT_BUFFER_SIZE );
541 x->x_outavable = 0;
542
543 x->x_inwriteposition = 0;
544 x->x_nbloops = 0;
545 x->x_nbwaitloops = 1;
546
547 return (x);
548 }
549
550 void mp3fileout_tilde_setup(void)
551 {
552 logpost(NULL, 4, "%s", mp3fileout_version );
553 mp3fileout_class = class_new(gensym("mp3fileout~"),
554 (t_newmethod) mp3fileout_new, (t_method) mp3fileout_free,
555 sizeof(t_mp3fileout), 0, A_NULL);
556
557 class_addmethod(mp3fileout_class, nullfn, gensym("signal"), 0);
558 class_addmethod(mp3fileout_class, (t_method) mp3fileout_dsp, gensym("dsp"), 0);
559 class_addmethod(mp3fileout_class, (t_method)mp3fileout_connect, gensym("connect"), A_SYMBOL, A_FLOAT, 0);
560 class_addmethod(mp3fileout_class, (t_method)mp3fileout_open, gensym("open"), A_SYMBOL, 0);
561 class_addmethod(mp3fileout_class, (t_method)mp3fileout_disconnect, gensym("disconnect"), 0);
562 class_addmethod(mp3fileout_class, (t_method)mp3fileout_start, gensym("start"), 0);
563 class_addmethod(mp3fileout_class, (t_method)mp3fileout_resume, gensym("resume"), 0);
564 class_addmethod(mp3fileout_class, (t_method)mp3fileout_seek, gensym("seek"), A_DEFFLOAT, 0);
565 class_addmethod(mp3fileout_class, (t_method)mp3fileout_stop, gensym("stop"), 0);
566 }