Debian Codegraph @

mp3fileout~.c

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