home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / mp3rtp.c < prev    next >
C/C++ Source or Header  |  2000-07-03  |  4KB  |  174 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include "lame.h"
  6. #include "rtp.h"
  7.  
  8.  
  9. /*
  10.  
  11. encode (via LAME) to mp3 with RTP streaming of the output.
  12.  
  13. Author:  Felix von Leitner <leitner@vim.org>
  14.  
  15. mp3rtp  ip:port:ttl  [lame encoding options]  infile outfile
  16.  
  17. example:
  18.  
  19. arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null
  20.  
  21.  
  22.  
  23. */
  24.  
  25.  
  26. struct rtpheader RTPheader;
  27. struct sockaddr_in rtpsi;
  28. int rtpsocket;
  29.  
  30. void rtp_output(char *mp3buffer,int mp3size)
  31. {
  32.   sendrtp(rtpsocket,&rtpsi,&RTPheader,mp3buffer,mp3size);
  33.   RTPheader.timestamp+=5;
  34.   RTPheader.b.sequence++;
  35. }
  36.  
  37. void rtp_usage(void) {
  38.     fprintf(stderr,"usage: mp3rtp ip:port:ttl  [encoder options] <infile> <outfile>\n");
  39.     exit(1);
  40. }
  41.  
  42.  
  43.  
  44. char mp3buffer[LAME_MAXMP3BUFFER];
  45.  
  46.  
  47. /************************************************************************
  48. *
  49. * main
  50. *
  51. * PURPOSE:  MPEG-1,2 Layer III encoder with GPSYCHO 
  52. * psychoacoustic model.
  53. *
  54. ************************************************************************/
  55.  
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.  
  60.   int i,port,ttl;
  61.   char *tmp,*Arg;
  62.   lame_global_flags gf;
  63.   int iread,imp3;
  64.   FILE *outf;
  65.   short int Buffer[2][1152];
  66.  
  67.   if(argc<=2) {
  68.     rtp_usage();
  69.     exit(1);
  70.   }
  71.  
  72.   /* process args */
  73.   Arg = argv[1];
  74.   tmp=strchr(Arg,':');
  75.  
  76.   if (!tmp) {
  77.     rtp_usage();
  78.     exit(1);
  79.   }
  80.   *tmp++=0;
  81.   port=atoi(tmp);
  82.   if (port<=0) {
  83.     rtp_usage();
  84.     exit(1);
  85.   }
  86.   tmp=strchr(tmp,':');
  87.   if (!tmp) {
  88.     rtp_usage();
  89.     exit(1);
  90.   }
  91.   *tmp++=0;
  92.   ttl=atoi(tmp);
  93.   if (tmp<=0) {
  94.     rtp_usage();
  95.     exit(1);
  96.   }
  97.   rtpsocket=makesocket(Arg,port,ttl,&rtpsi);
  98.   srand(getpid() ^ time(0));
  99.   initrtp(&RTPheader);
  100.  
  101.  
  102.   /* initialize encoder */
  103.   lame_init(&gf);
  104.  
  105.   /* Remove the argumets that are rtp related, and then 
  106.    * parse the command line arguments, setting various flags in the
  107.    * struct pointed to by 'gf'.  If you want to parse your own arguments,
  108.    * or call libmp3lame from a program which uses a GUI to set arguments,
  109.    * skip this call and set the values of interest in the gf struct.  
  110.    * (see lame.h for documentation about these parameters)
  111.    */
  112.   for (i=1; i<argc-1; i++)  /* remove first argument, it was for rtp */
  113.     argv[i]=argv[i+1];
  114.   lame_parse_args(&gf,argc-1, argv); 
  115.  
  116.   /* open the output file.  Filename parsed into gf.inPath */
  117.   if (!strcmp(gf.outPath, "-")) {
  118. #ifdef __EMX__
  119.     _fsetmode(stdout,"b");
  120. #elif (defined  __BORLANDC__)
  121.     setmode(_fileno(stdout), O_BINARY);
  122. #elif (defined  __CYGWIN__)
  123.     setmode(fileno(stdout), _O_BINARY);
  124. #elif (defined _WIN32)
  125.     _setmode(_fileno(stdout), _O_BINARY);
  126. #endif
  127.     outf = stdout;
  128.   } else {
  129.     if ((outf = fopen(gf.outPath, "wb")) == NULL) {
  130.       fprintf(stderr,"Could not create \"%s\".\n", gf.outPath);
  131.       exit(1);
  132.     }
  133.   }
  134.  
  135.  
  136.   /* open the wav/aiff/raw pcm or mp3 input file.  This call will
  137.    * open the file with name gf.inFile, try to parse the headers and
  138.    * set gf.samplerate, gf.num_channels, gf.num_samples.
  139.    * if you want to do your own file input, skip this call and set
  140.    * these values yourself.  
  141.    */
  142.   lame_init_infile(&gf);
  143.  
  144.  
  145.   /* Now that all the options are set, lame needs to analyze them and
  146.    * set some more options 
  147.    */
  148.   lame_init_params(&gf);
  149.   lame_print_config(&gf);   /* print usefull information about options being used */
  150.  
  151.   lame_id3v2_tag(&gf,outf); /* add ID3 version 2 tag to mp3 file */
  152.  
  153.   /* encode until we hit eof */
  154.   do {
  155.     /* read in 'iread' samples */
  156.     iread=lame_readframe(&gf,Buffer);
  157.     /* encode the frame */
  158.     imp3=lame_encode_buffer(&gf,Buffer[0],Buffer[1],iread,
  159.                 mp3buffer,sizeof(mp3buffer));
  160.     fwrite(mp3buffer,1,imp3,outf);       /* write the MP3 output to file  */
  161.     rtp_output(mp3buffer,imp3);          /* write MP3 output to RTP port */    
  162.   } while (iread);
  163.   
  164.  
  165.   imp3=lame_encode_finish(&gf,mp3buffer,sizeof(mp3buffer));   /* may return one or more mp3 frame */
  166.   fwrite(mp3buffer,1,imp3,outf);  
  167.   rtp_output(mp3buffer,imp3);
  168.   fclose(outf);
  169.   lame_close_infile(&gf);             /* close the sound input file */
  170.   lame_mp3_tags(&gf);       /* add ID3 version 1 or VBR tags to mp3 file */
  171.   return 0;
  172. }
  173.  
  174.