| 1 |
/* |
| 2 |
* mp3rtp command line frontend program |
| 3 |
* |
| 4 |
* initially contributed by Felix von Leitner |
| 5 |
* |
| 6 |
* Copyright (c) 2000 Mark Taylor |
| 7 |
* 2010 Robert Hegemann |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Library General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library 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 GNU |
| 17 |
* Library General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Library General Public |
| 20 |
* License along with this library; if not, write to the |
| 21 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 |
* Boston, MA 02111-1307, USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
/* $Id$ */ |
| 26 |
|
| 27 |
/* Still under work ..., need a client for test, where can I get one? */ |
| 28 |
|
| 29 |
/* An audio player named Zinf (aka freeamp) can play rtp streams */ |
| 30 |
|
| 31 |
/* |
| 32 |
* experimental translation: |
| 33 |
* |
| 34 |
* gcc -I..\include -I..\libmp3lame -o mp3rtp mp3rtp.c ../libmp3lame/libmp3lame.a lametime.c get_audio.c ieeefloat.c timestatus.c parse.c rtp.c -lm |
| 35 |
* |
| 36 |
* wavrec -t 14400 -s 44100 -S /proc/self/fd/1 | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3 |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifdef HAVE_CONFIG_H |
| 40 |
# include <config.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef HAVE_STDINT_H |
| 44 |
# include <stdint.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef STDC_HEADERS |
| 48 |
# include <stdlib.h> |
| 49 |
# include <string.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#include <time.h> |
| 53 |
|
| 54 |
#ifdef HAVE_UNISTD_H |
| 55 |
# include <unistd.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef IPV6 |
| 59 |
#include <netinet/in.h> |
| 60 |
# ifndef INET6_ADDRSTRLEN |
| 61 |
# define INET6_ADDRSTRLEN 46 |
| 62 |
# endif /* INET6_ADDRSTRLEN */ |
| 63 |
#endif |
| 64 |
|
| 65 |
#include "lame.h" |
| 66 |
#include "main.h" |
| 67 |
#include "parse.h" |
| 68 |
#include "lametime.h" |
| 69 |
#include "timestatus.h" |
| 70 |
#include "get_audio.h" |
| 71 |
#include "rtp.h" |
| 72 |
#include "console.h" |
| 73 |
|
| 74 |
#ifdef WITH_DMALLOC |
| 75 |
#include <dmalloc.h> |
| 76 |
#endif |
| 77 |
|
| 78 |
/* |
| 79 |
* Encode (via LAME) to mp3 with RTP streaming of the output. |
| 80 |
* |
| 81 |
* Author: Felix von Leitner <[email protected]> |
| 82 |
* |
| 83 |
* mp3rtp ip [port[:ttl]] [lame encoding options] infile outfile |
| 84 |
* |
| 85 |
* examples: |
| 86 |
* arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42 5004:2 -b 56 - /dev/null |
| 87 |
* arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3 |
| 88 |
* If ipv6 is enabled : |
| 89 |
* arecord -b 16 -s 44100 -w | ./mp3rtp ff00::8 -V2 -b128 -B256 - my_mp3file.mp3 |
| 90 |
* arecord -b 16 -s 44100 -w | ./mp3rtp ff00::8 5004:2 -V2 -b128 -B256 - my_mp3file.mp3 |
| 91 |
* |
| 92 |
*/ |
| 93 |
|
| 94 |
|
| 95 |
static unsigned int |
| 96 |
maxvalue(int Buffer[2][1152]) |
| 97 |
{ |
| 98 |
int max = 0; |
| 99 |
int i; |
| 100 |
|
| 101 |
for (i = 0; i < 1152; i++) { |
| 102 |
if (abs(Buffer[0][i]) > max) |
| 103 |
max = abs(Buffer[0][i]); |
| 104 |
if (abs(Buffer[1][i]) > max) |
| 105 |
max = abs(Buffer[1][i]); |
| 106 |
} |
| 107 |
return max >> 16; |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
levelmessage(unsigned int maxv, int* maxx, int* tmpx) |
| 112 |
{ |
| 113 |
char buff[] = "| . | . | . | . | . | . | . | . | . | . | \r"; |
| 114 |
int tmp = *tmpx, max = *maxx; |
| 115 |
|
| 116 |
buff[tmp] = '+'; |
| 117 |
tmp = (maxv * 61 + 16384) / (32767 + 16384 / 61); |
| 118 |
if (tmp > sizeof(buff) - 2) |
| 119 |
tmp = sizeof(buff) - 2; |
| 120 |
if (max < tmp) |
| 121 |
max = tmp; |
| 122 |
buff[max] = 'x'; |
| 123 |
buff[tmp] = '#'; |
| 124 |
console_printf(buff); |
| 125 |
console_flush(); |
| 126 |
*maxx = max; |
| 127 |
*tmpx = tmp; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/************************************************************************ |
| 132 |
* |
| 133 |
* main |
| 134 |
* |
| 135 |
* PURPOSE: MPEG-1,2 Layer III encoder with GPSYCHO |
| 136 |
* psychoacoustic model. |
| 137 |
* |
| 138 |
************************************************************************/ |
| 139 |
|
| 140 |
int |
| 141 |
lame_main(lame_t gf, int argc, char **argv) |
| 142 |
{ |
| 143 |
unsigned char mp3buffer[LAME_MAXMP3BUFFER]; |
| 144 |
char inPath[PATH_MAX + 1]; |
| 145 |
char outPath[PATH_MAX + 1]; |
| 146 |
int Buffer[2][1152]; |
| 147 |
|
| 148 |
int maxx = 0, tmpx = 0; |
| 149 |
int ret; |
| 150 |
int wavsamples; |
| 151 |
int mp3bytes; |
| 152 |
FILE *outf; |
| 153 |
#ifdef IPV6 |
| 154 |
char ip[INET6_ADDRSTRLEN]; |
| 155 |
#else |
| 156 |
char ip[16]; |
| 157 |
#endif |
| 158 |
unsigned int port = 5004; |
| 159 |
unsigned int ttl = 2; |
| 160 |
char dummy; |
| 161 |
int arg = 0; |
| 162 |
|
| 163 |
if (argc <= 2) { |
| 164 |
console_printf("Encode (via LAME) to mp3 with RTP streaming of the output\n" |
| 165 |
"\n" |
| 166 |
" mp3rtp ip [port[:ttl]] [lame encoding options] infile outfile\n" |
| 167 |
"\n" |
| 168 |
" examples:\n" |
| 169 |
" arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42 5004:2 -b 56 - /dev/null\n" |
| 170 |
" arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3\n" |
| 171 |
" If ipv6 is enabled : \n" |
| 172 |
" arecord -b 16 -s 44100 -w | ./mp3rtp ff00::8 -b 56 - /dev/null\n" |
| 173 |
" arecord -b 16 -s 44100 -w | ./mp3rtp ff00::8 5004:2 -b 56 - /dev/null\n" |
| 174 |
"\n"); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
#ifdef IPV6 |
| 179 |
if((sscanf(argv[1],"%45[.:0-9a-fA-F]", ip)) == 1) |
| 180 |
#else |
| 181 |
if((sscanf(argv[1],"%11[.0-9]", ip)) == 1) |
| 182 |
#endif |
| 183 |
{ |
| 184 |
switch(sscanf(argv[2],"%u:%u%c", &port, &ttl, &dummy)){ |
| 185 |
case 1: |
| 186 |
case 2: |
| 187 |
arg = 1; |
| 188 |
break; |
| 189 |
case 3: |
| 190 |
error_printf("Illegal destination selector '%s', must be ip [port[:ttl]]\n", argv[1]); |
| 191 |
return -1; |
| 192 |
default: |
| 193 |
arg = 0; |
| 194 |
} |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
error_printf("Illegal destination selector '%s', must be ip [port[:ttl]]\n", argv[1]); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
|
| 202 |
rtp_initialization(); |
| 203 |
if (rtp_socket(ip, port, ttl)) { |
| 204 |
rtp_deinitialization(); |
| 205 |
fclose(outf); |
| 206 |
error_printf("fatal error during initialization\n"); |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
lame_set_errorf(gf, &frontend_errorf); |
| 211 |
lame_set_debugf(gf, &frontend_debugf); |
| 212 |
lame_set_msgf(gf, &frontend_msgf); |
| 213 |
|
| 214 |
/* Remove the argumets that are rtp related, and then |
| 215 |
* parse the command line arguments, setting various flags in the |
| 216 |
* struct pointed to by 'gf'. If you want to parse your own arguments, |
| 217 |
* or call libmp3lame from a program which uses a GUI to set arguments, |
| 218 |
* skip this call and set the values of interest in the gf struct. |
| 219 |
* (see lame.h for documentation about these parameters) |
| 220 |
*/ |
| 221 |
{ |
| 222 |
int i; |
| 223 |
int argc_mod = argc-arg-1; /* leaving out one or two arguments */ |
| 224 |
char** argv_mod = calloc(argc_mod, sizeof(char*)); |
| 225 |
argv_mod[0] = argv[0]; |
| 226 |
for (i = 2+arg; i < argc; ++i) { /* leaving out argument number 1, parsed above */ |
| 227 |
argv_mod[i-arg-1] = argv[i]; |
| 228 |
} |
| 229 |
parse_args(gf, argc_mod, argv_mod, inPath, outPath, NULL, NULL); |
| 230 |
free(argv_mod); |
| 231 |
} |
| 232 |
|
| 233 |
/* open the output file. Filename parsed into gf.inPath */ |
| 234 |
if (0 == strcmp(outPath, "-")) { |
| 235 |
lame_set_stream_binary_mode(outf = stdout); |
| 236 |
} |
| 237 |
else { |
| 238 |
if ((outf = lame_fopen(outPath, "wb+")) == NULL) { |
| 239 |
rtp_deinitialization(); |
| 240 |
error_printf("Could not create \"%s\".\n", outPath); |
| 241 |
return 1; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/* open the wav/aiff/raw pcm or mp3 input file. This call will |
| 247 |
* open the file with name gf.inFile, try to parse the headers and |
| 248 |
* set gf.samplerate, gf.num_channels, gf.num_samples. |
| 249 |
* if you want to do your own file input, skip this call and set |
| 250 |
* these values yourself. |
| 251 |
*/ |
| 252 |
if (init_infile(gf, inPath) < 0) { |
| 253 |
rtp_deinitialization(); |
| 254 |
fclose(outf); |
| 255 |
error_printf("Can't init infile '%s'\n", inPath); |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/* Now that all the options are set, lame needs to analyze them and |
| 261 |
* set some more options |
| 262 |
*/ |
| 263 |
ret = lame_init_params(gf); |
| 264 |
if (ret < 0) { |
| 265 |
if (ret == -1) |
| 266 |
display_bitrates(stderr); |
| 267 |
rtp_deinitialization(); |
| 268 |
fclose(outf); |
| 269 |
close_infile(); |
| 270 |
error_printf("fatal error during initialization\n"); |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
lame_print_config(gf); /* print useful information about options being used */ |
| 275 |
|
| 276 |
if (global_ui_config.update_interval < 0.) |
| 277 |
global_ui_config.update_interval = 2.; |
| 278 |
|
| 279 |
/* encode until we hit EOF */ |
| 280 |
while ((wavsamples = get_audio(gf, Buffer)) > 0) { /* read in 'wavsamples' samples */ |
| 281 |
levelmessage(maxvalue(Buffer), &maxx, &tmpx); |
| 282 |
mp3bytes = lame_encode_buffer_int(gf, /* encode the frame */ |
| 283 |
Buffer[0], Buffer[1], wavsamples, |
| 284 |
mp3buffer, sizeof(mp3buffer)); |
| 285 |
rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */ |
| 286 |
fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */ |
| 287 |
} |
| 288 |
|
| 289 |
mp3bytes = lame_encode_flush(gf, /* may return one or more mp3 frame */ |
| 290 |
mp3buffer, sizeof(mp3buffer)); |
| 291 |
rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */ |
| 292 |
fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */ |
| 293 |
|
| 294 |
lame_mp3_tags_fid(gf, outf); /* add VBR tags to mp3 file */ |
| 295 |
|
| 296 |
rtp_deinitialization(); |
| 297 |
fclose(outf); |
| 298 |
close_infile(); /* close the sound input file */ |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
/* end of mp3rtp.c */ |