1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
|
/*------------------------------------------------------------------------------
Copyright (c) 2000-2007 Tyrell Corporation. All rights reserved.
Tyrell DarkIce
File : DarkIce.cpp
Version : $Revision: 553 $
Author : $Author: rafael@riseup.net $
Location : $HeadURL: https://darkice.googlecode.com/svn/darkice/tags/darkice-1_2/src/DarkIce.cpp $
Copyright notice:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
#error need stdlib.h
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#error need unistd.h
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#else
#error need sys/types.h
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#else
#error need sys/wait.h
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#else
#error need errno.h
#endif
#ifdef HAVE_SCHED_H
#include <sched.h>
#else
#error need sched.h
#endif
#include "Util.h"
#include "IceCast.h"
#include "IceCast2.h"
#include "ShoutCast.h"
#include "FileCast.h"
#include "MultiThreadedConnector.h"
#include "DarkIce.h"
#ifdef HAVE_LAME_LIB
#include "LameLibEncoder.h"
#endif
#ifdef HAVE_TWOLAME_LIB
#include "TwoLameLibEncoder.h"
#endif
#ifdef HAVE_VORBIS_LIB
#include "VorbisLibEncoder.h"
#endif
#ifdef HAVE_OPUS_LIB
#include "OpusLibEncoder.h"
#endif
#ifdef HAVE_FAAC_LIB
#include "FaacEncoder.h"
#endif
#ifdef HAVE_AACPLUS_LIB
#include "aacPlusEncoder.h"
#endif
/* =================================================== local data structures */
/* ================================================ local constants & macros */
/*------------------------------------------------------------------------------
* File identity
*----------------------------------------------------------------------------*/
static const char fileid[] = "$Id: DarkIce.cpp 553 2013-07-15 05:50:56Z rafael@riseup.net $";
/*------------------------------------------------------------------------------
* Make sure wait-related stuff is what we expect
*----------------------------------------------------------------------------*/
#ifndef WEXITSTATUS
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Initialize the object
*----------------------------------------------------------------------------*/
void
DarkIce :: init ( const Config & config ) throw ( Exception )
{
unsigned int bufferSecs;
const ConfigSection * cs;
const char * str;
unsigned int sampleRate;
unsigned int bitsPerSample;
unsigned int channel;
bool reconnect;
const char * device;
const char * jackClientName;
const char * paSourceName;
// the [general] section
if ( !(cs = config.get( "general")) ) {
throw Exception( __FILE__, __LINE__, "no section [general] in config");
}
str = cs->getForSure( "duration", " missing in section [general]");
duration = Util::strToL( str);
str = cs->getForSure( "bufferSecs", " missing in section [general]");
bufferSecs = Util::strToL( str);
if (bufferSecs == 0) {
throw Exception(__FILE__, __LINE__,
"setting bufferSecs to 0 not supported");
}
str = cs->get( "reconnect");
reconnect = str ? (Util::strEq( str, "yes") ? true : false) : true;
// real-time scheduling is enabled by default
str = cs->get( "realtime" );
enableRealTime = str ? (Util::strEq( str, "yes") ? true : false) : true;
// get realtime scheduling priority. If unspecified, set it to 4.
// Why 4? jackd's default priority is 10, jackd client threads run
// at 5, so make the encoder thread use 4. jackd automatically sets
// the process callback priority to the right value, so all we have
// to care about is the encoder priority.
str = cs->get( "rtprio" );
realTimeSchedPriority = (str != NULL) ? Util::strToL( str ) : 4;
// the [input] section
if ( !(cs = config.get( "input")) ) {
throw Exception( __FILE__, __LINE__, "no section [input] in config");
}
str = cs->getForSure( "sampleRate", " missing in section [input]");
sampleRate = Util::strToL( str);
str = cs->getForSure( "bitsPerSample", " missing in section [input]");
bitsPerSample = Util::strToL( str);
str = cs->getForSure( "channel", " missing in section [input]");
channel = Util::strToL( str);
device = cs->getForSure( "device", " missing in section [input]");
jackClientName = cs->get ( "jackClientName");
paSourceName = cs->get ( "paSourceName");
dsp = AudioSource::createDspSource( device,
jackClientName,
paSourceName,
sampleRate,
bitsPerSample,
channel );
encConnector = new MultiThreadedConnector( dsp.get(), reconnect );
noAudioOuts = 0;
configIceCast( config, bufferSecs);
configIceCast2( config, bufferSecs);
configShoutCast( config, bufferSecs);
configFileCast( config);
}
/*------------------------------------------------------------------------------
* Look for the IceCast stream outputs in the config file
*----------------------------------------------------------------------------*/
void
DarkIce :: configIceCast ( const Config & config,
unsigned int bufferSecs )
throw ( Exception )
{
// look for IceCast encoder output streams,
// sections [icecast-0], [icecast-1], ...
char stream[] = "icecast- ";
size_t streamLen = Util::strLen( stream);
unsigned int u;
for ( u = noAudioOuts; u < maxOutput; ++u ) {
const ConfigSection * cs;
// ugly hack to change the section name to "stream0", "stream1", etc.
stream[streamLen-1] = '0' + (u - noAudioOuts);
if ( !(cs = config.get( stream)) ) {
break;
}
#if !defined HAVE_LAME_LIB && !defined HAVE_TWOLAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with lame or twolame support, "
"thus can't connect to IceCast 1.x, stream: ",
stream);
#else
const char * str;
unsigned int sampleRate = 0;
unsigned int channel = 0;
AudioEncoder::BitrateMode bitrateMode;
unsigned int bitrate = 0;
double quality = 0.0;
const char * server = 0;
unsigned int port = 0;
const char * password = 0;
const char * mountPoint = 0;
const char * remoteDumpFile = 0;
const char * name = 0;
const char * description = 0;
const char * url = 0;
const char * genre = 0;
bool isPublic = false;
int lowpass = 0;
int highpass = 0;
const char * localDumpName = 0;
FileSink * localDumpFile = 0;
bool fileAddDate = false;
const char * fileDateFormat = 0;
AudioEncoder * encoder = 0;
int bufferSize = 0;
str = cs->get( "sampleRate");
sampleRate = str ? Util::strToL( str) : dsp->getSampleRate();
str = cs->get( "channel");
channel = str ? Util::strToL( str) : dsp->getChannel();
str = cs->get( "bitrate");
bitrate = str ? Util::strToL( str) : 0;
str = cs->get( "quality");
quality = str ? Util::strToD( str) : 0.0;
str = cs->getForSure( "bitrateMode",
" not specified in section ",
stream);
if ( Util::strEq( str, "cbr") ) {
bitrateMode = AudioEncoder::cbr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for CBR encoding");
}
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for CBR encoding");
}
} else if ( Util::strEq( str, "abr") ) {
bitrateMode = AudioEncoder::abr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for ABR encoding");
}
} else if ( Util::strEq( str, "vbr") ) {
bitrateMode = AudioEncoder::vbr;
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for VBR encoding");
}
} else {
throw Exception( __FILE__, __LINE__,
"invalid bitrate mode: ", str);
}
server = cs->getForSure( "server", " missing in section ", stream);
str = cs->getForSure( "port", " missing in section ", stream);
port = Util::strToL( str);
password = cs->getForSure("password"," missing in section ",stream);
mountPoint = cs->getForSure( "mountPoint",
" missing in section ",
stream);
remoteDumpFile = cs->get( "remoteDumpFile");
name = cs->get( "name");
description = cs->get("description");
url = cs->get( "url");
genre = cs->get( "genre");
str = cs->get( "public");
isPublic = str ? (Util::strEq( str, "yes") ? true : false) : false;
str = cs->get( "lowpass");
lowpass = str ? Util::strToL( str) : 0;
str = cs->get( "highpass");
highpass = str ? Util::strToL( str) : 0;
str = cs->get("fileAddDate");
fileAddDate = str ? (Util::strEq( str, "yes") ? true : false) : false;
fileDateFormat = cs->get("fileDateFormat");
bufferSize = dsp->getSampleSize() * dsp->getSampleRate() * bufferSecs;
reportEvent( 3, "buffer size: ", bufferSize);
localDumpName = cs->get( "localDumpFile");
// go on and create the things
// check for and create the local dump file if needed
if ( localDumpName != 0 ) {
if ( fileAddDate ) {
if (fileDateFormat == 0) {
localDumpName = Util::fileAddDate(localDumpName);
}
else {
localDumpName = Util::fileAddDate( localDumpName,
fileDateFormat );
}
}
localDumpFile = new FileSink( stream, localDumpName);
if ( !localDumpFile->exists() ) {
if ( !localDumpFile->create() ) {
reportEvent( 1, "can't create local dump file",
localDumpName);
localDumpFile = 0;
}
}
if ( fileAddDate ) {
delete[] localDumpName;
}
}
// streaming related stuff
audioOuts[u].socket = new TcpSocket( server, port);
audioOuts[u].server = new IceCast( audioOuts[u].socket.get(),
password,
mountPoint,
bitrate,
name,
description,
url,
genre,
isPublic,
remoteDumpFile,
localDumpFile);
str = cs->getForSure( "format", " missing in section ", stream);
if (!Util::strEq(str, "mp3") && !Util::strEq(str, "mp2")) {
throw Exception( __FILE__, __LINE__,
"unsupported stream format: ", str);
}
#ifdef HAVE_LAME_LIB
if ( Util::strEq( str, "mp3") ) {
encoder = new LameLibEncoder( audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
channel,
lowpass,
highpass );
}
#endif
#ifdef HAVE_TWOLAME_LIB
if ( Util::strEq( str, "mp2") ) {
encoder = new TwoLameLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
sampleRate,
channel );
}
#endif
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
encConnector->attach( audioOuts[u].encoder.get());
#endif // HAVE_LAME_LIB || HAVE_TWOLAME_LIB
}
noAudioOuts += u;
}
/*------------------------------------------------------------------------------
* Look for the IceCast2 stream outputs in the config file
*----------------------------------------------------------------------------*/
void
DarkIce :: configIceCast2 ( const Config & config,
unsigned int bufferSecs )
throw ( Exception )
{
// look for IceCast2 encoder output streams,
// sections [icecast2-0], [icecast2-1], ...
char stream[] = "icecast2- ";
size_t streamLen = Util::strLen( stream);
unsigned int u;
for ( u = noAudioOuts; u < maxOutput; ++u ) {
const ConfigSection * cs;
// ugly hack to change the section name to "stream0", "stream1", etc.
stream[streamLen-1] = '0' + (u - noAudioOuts);
if ( !(cs = config.get( stream)) ) {
break;
}
const char * str;
IceCast2::StreamFormat format;
unsigned int sampleRate = 0;
unsigned int channel = 0;
AudioEncoder::BitrateMode bitrateMode;
unsigned int bitrate = 0;
unsigned int maxBitrate = 0;
double quality = 0.0;
const char * server = 0;
unsigned int port = 0;
const char * password = 0;
const char * mountPoint = 0;
const char * name = 0;
const char * description = 0;
const char * url = 0;
const char * genre = 0;
bool isPublic = false;
int lowpass = 0;
int highpass = 0;
const char * localDumpName = 0;
FileSink * localDumpFile = 0;
bool fileAddDate = false;
const char * fileDateFormat = 0;
AudioEncoder * encoder = 0;
int bufferSize = 0;
str = cs->getForSure( "format", " missing in section ", stream);
if ( Util::strEq( str, "vorbis") ) {
format = IceCast2::oggVorbis;
} else if ( Util::strEq( str, "opus") ) {
format = IceCast2::oggOpus;
} else if ( Util::strEq( str, "mp3") ) {
format = IceCast2::mp3;
} else if ( Util::strEq( str, "mp2") ) {
format = IceCast2::mp2;
} else if ( Util::strEq( str, "aac") ) {
format = IceCast2::aac;
} else if ( Util::strEq( str, "aacp") ) {
format = IceCast2::aacp;
} else {
throw Exception( __FILE__, __LINE__,
"unsupported stream format: ", str);
}
str = cs->get( "sampleRate");
sampleRate = str ? Util::strToL( str) : dsp->getSampleRate();
str = cs->get( "channel");
channel = str ? Util::strToL( str) : dsp->getChannel();
// determine fixed bitrate or variable bitrate quality
str = cs->get( "bitrate");
bitrate = str ? Util::strToL( str) : 0;
str = cs->get( "maxBitrate");
maxBitrate = str ? Util::strToL( str) : 0;
str = cs->get( "quality");
quality = str ? Util::strToD( str) : 0.0;
str = cs->getForSure( "bitrateMode",
" not specified in section ",
stream);
if ( Util::strEq( str, "cbr") ) {
bitrateMode = AudioEncoder::cbr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for CBR encoding");
}
} else if ( Util::strEq( str, "abr") ) {
bitrateMode = AudioEncoder::abr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for ABR encoding");
}
} else if ( Util::strEq( str, "vbr") ) {
bitrateMode = AudioEncoder::vbr;
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for VBR encoding");
}
} else {
throw Exception( __FILE__, __LINE__,
"invalid bitrate mode: ", str);
}
server = cs->getForSure( "server", " missing in section ", stream);
str = cs->getForSure( "port", " missing in section ", stream);
port = Util::strToL( str);
password = cs->getForSure("password"," missing in section ",stream);
mountPoint = cs->getForSure( "mountPoint",
" missing in section ",
stream);
name = cs->get( "name");
description = cs->get( "description");
url = cs->get( "url");
genre = cs->get( "genre");
str = cs->get( "public");
isPublic = str ? (Util::strEq( str, "yes") ? true : false) : false;
str = cs->get( "lowpass");
lowpass = str ? Util::strToL( str) : 0;
str = cs->get( "highpass");
highpass = str ? Util::strToL( str) : 0;
str = cs->get( "fileAddDate");
fileAddDate = str ? (Util::strEq( str, "yes") ? true : false) : false;
fileDateFormat = cs->get( "fileDateFormat");
bufferSize = dsp->getSampleSize() * dsp->getSampleRate() * bufferSecs;
reportEvent( 3, "buffer size: ", bufferSize);
localDumpName = cs->get( "localDumpFile");
// go on and create the things
// check for and create the local dump file if needed
if ( localDumpName != 0 ) {
if ( fileAddDate ) {
if (fileDateFormat == 0) {
localDumpName = Util::fileAddDate(localDumpName);
}
else {
localDumpName = Util::fileAddDate( localDumpName,
fileDateFormat );
}
}
localDumpFile = new FileSink( stream, localDumpName);
if ( !localDumpFile->exists() ) {
if ( !localDumpFile->create() ) {
reportEvent( 1, "can't create local dump file",
localDumpName);
localDumpFile = 0;
}
}
if ( fileAddDate ) {
delete[] localDumpName;
}
}
// streaming related stuff
audioOuts[u].socket = new TcpSocket( server, port);
audioOuts[u].server = new IceCast2( audioOuts[u].socket.get(),
password,
mountPoint,
format,
bitrate,
name,
description,
url,
genre,
isPublic,
localDumpFile);
switch ( format ) {
case IceCast2::mp3:
#ifndef HAVE_LAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with lame support, "
"thus can't create mp3 stream: ",
stream);
#else
encoder = new LameLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
channel,
lowpass,
highpass );
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
#endif // HAVE_LAME_LIB
break;
case IceCast2::oggVorbis:
#ifndef HAVE_VORBIS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with Ogg Vorbis support, "
"thus can't Ogg Vorbis stream: ",
stream);
#else
encoder = new VorbisLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel(),
maxBitrate);
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
#endif // HAVE_VORBIS_LIB
break;
case IceCast2::oggOpus:
#ifndef HAVE_OPUS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with Ogg Opus support, "
"thus can't Ogg Opus stream: ",
stream);
#else
encoder = new OpusLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel(),
maxBitrate);
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getBitsPerSample() / 8);
#endif // HAVE_OPUS_LIB
break;
case IceCast2::mp2:
#ifndef HAVE_TWOLAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with TwoLame support, "
"thus can't create mp2 stream: ",
stream);
#else
encoder = new TwoLameLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
sampleRate,
channel );
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
#endif // HAVE_TWOLAME_LIB
break;
case IceCast2::aac:
#ifndef HAVE_FAAC_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with AAC support, "
"thus can't aac stream: ",
stream);
#else
encoder = new FaacEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel());
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
#endif // HAVE_FAAC_LIB
break;
case IceCast2::aacp:
#ifndef HAVE_AACPLUS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with AAC+ support, "
"thus can't aacp stream: ",
stream);
#else
encoder = new aacPlusEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
channel );
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
#endif // HAVE_AACPLUS_LIB
break;
default:
throw Exception( __FILE__, __LINE__,
"Illegal stream format: ", format);
}
encConnector->attach( audioOuts[u].encoder.get());
}
noAudioOuts += u;
}
/*------------------------------------------------------------------------------
* Look for the ShoutCast stream outputs in the config file
*----------------------------------------------------------------------------*/
void
DarkIce :: configShoutCast ( const Config & config,
unsigned int bufferSecs )
throw ( Exception )
{
// look for Shoutcast encoder output streams,
// sections [shoutcast-0], [shoutcast-1], ...
char stream[] = "shoutcast- ";
size_t streamLen = Util::strLen( stream);
unsigned int u;
for ( u = noAudioOuts; u < maxOutput; ++u ) {
const ConfigSection * cs;
// ugly hack to change the section name to "stream0", "stream1", etc.
stream[streamLen-1] = '0' + (u - noAudioOuts);
if ( !(cs = config.get( stream)) ) {
break;
}
#ifndef HAVE_LAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with lame support, "
"thus can't connect to ShoutCast, stream: ",
stream);
#else
const char * str;
unsigned int sampleRate = 0;
unsigned int channel = 0;
AudioEncoder::BitrateMode bitrateMode;
unsigned int bitrate = 0;
double quality = 0.0;
const char * server = 0;
unsigned int port = 0;
const char * password = 0;
const char * name = 0;
const char * url = 0;
const char * genre = 0;
bool isPublic = false;
const char * mountPoint = 0;
int lowpass = 0;
int highpass = 0;
const char * irc = 0;
const char * aim = 0;
const char * icq = 0;
const char * localDumpName = 0;
FileSink * localDumpFile = 0;
bool fileAddDate = false;
const char * fileDateFormat = 0;
AudioEncoder * encoder = 0;
int bufferSize = 0;
str = cs->get( "sampleRate");
sampleRate = str ? Util::strToL( str) : dsp->getSampleRate();
str = cs->get( "channel");
channel = str ? Util::strToL( str) : dsp->getChannel();
str = cs->get( "bitrate");
bitrate = str ? Util::strToL( str) : 0;
str = cs->get( "quality");
quality = str ? Util::strToD( str) : 0.0;
str = cs->getForSure( "bitrateMode",
" not specified in section ",
stream);
if ( Util::strEq( str, "cbr") ) {
bitrateMode = AudioEncoder::cbr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for CBR encoding");
}
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for CBR encoding");
}
} else if ( Util::strEq( str, "abr") ) {
bitrateMode = AudioEncoder::abr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for ABR encoding");
}
} else if ( Util::strEq( str, "vbr") ) {
bitrateMode = AudioEncoder::vbr;
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for VBR encoding");
}
} else {
throw Exception( __FILE__, __LINE__,
"invalid bitrate mode: ", str);
}
server = cs->getForSure( "server", " missing in section ", stream);
str = cs->getForSure( "port", " missing in section ", stream);
port = Util::strToL( str);
password = cs->getForSure("password"," missing in section ",stream);
name = cs->get( "name");
mountPoint = cs->get( "mountPoint" );
url = cs->get( "url");
genre = cs->get( "genre");
str = cs->get( "public");
isPublic = str ? (Util::strEq( str, "yes") ? true : false) : false;
str = cs->get( "lowpass");
lowpass = str ? Util::strToL( str) : 0;
str = cs->get( "highpass");
highpass = str ? Util::strToL( str) : 0;
irc = cs->get( "irc");
aim = cs->get( "aim");
icq = cs->get( "icq");
str = cs->get("fileAddDate");
fileAddDate = str ? (Util::strEq( str, "yes") ? true : false) : false;
fileDateFormat = cs->get( "fileDateFormat");
bufferSize = dsp->getBitsPerSample() / 8 * dsp->getSampleRate() * dsp->getChannel() * bufferSecs;
reportEvent( 3, "buffer size: ", bufferSize);
localDumpName = cs->get( "localDumpFile");
// go on and create the things
// check for and create the local dump file if needed
if ( localDumpName != 0 ) {
if ( fileAddDate ) {
if (fileDateFormat == 0) {
localDumpName = Util::fileAddDate(localDumpName);
}
else {
localDumpName = Util::fileAddDate( localDumpName,
fileDateFormat );
}
}
localDumpFile = new FileSink( stream, localDumpName);
if ( !localDumpFile->exists() ) {
if ( !localDumpFile->create() ) {
reportEvent( 1, "can't create local dump file",
localDumpName);
localDumpFile = 0;
}
}
if ( fileAddDate ) {
delete[] localDumpName;
}
}
// streaming related stuff
audioOuts[u].socket = new TcpSocket( server, port);
audioOuts[u].server = new ShoutCast( audioOuts[u].socket.get(),
password,
mountPoint,
bitrate,
name,
url,
genre,
isPublic,
irc,
aim,
icq,
localDumpFile);
encoder = new LameLibEncoder( audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
channel,
lowpass,
highpass );
audioOuts[u].encoder = new BufferedSink(encoder, bufferSize, dsp->getSampleSize());
encConnector->attach( audioOuts[u].encoder.get());
#endif // HAVE_LAME_LIB
}
noAudioOuts += u;
}
/*------------------------------------------------------------------------------
* Look for the FileCast stream outputs in the config file
*----------------------------------------------------------------------------*/
void
DarkIce :: configFileCast ( const Config & config )
throw ( Exception )
{
// look for FileCast encoder output streams,
// sections [file-0], [file-1], ...
char stream[] = "file- ";
size_t streamLen = Util::strLen( stream);
unsigned int u;
for ( u = noAudioOuts; u < maxOutput; ++u ) {
const ConfigSection * cs;
// ugly hack to change the section name to "stream0", "stream1", etc.
stream[streamLen-1] = '0' + (u - noAudioOuts);
if ( !(cs = config.get( stream)) ) {
break;
}
const char * str;
const char * format = 0;
AudioEncoder::BitrateMode bitrateMode;
unsigned int bitrate = 0;
double quality = 0.0;
const char * targetFileName = 0;
unsigned int sampleRate = 0;
int lowpass = 0;
int highpass = 0;
bool fileAddDate = false;
const char * fileDateFormat = 0;
format = cs->getForSure( "format", " missing in section ", stream);
if ( !Util::strEq( format, "vorbis")
&& !Util::strEq( format, "opus")
&& !Util::strEq( format, "mp3")
&& !Util::strEq( format, "mp2")
&& !Util::strEq( format, "aac")
&& !Util::strEq( format, "aacp") ) {
throw Exception( __FILE__, __LINE__,
"unsupported stream format: ", format);
}
str = cs->getForSure("bitrate", " missing in section ", stream);
bitrate = Util::strToL( str);
targetFileName = cs->getForSure( "fileName",
" missing in section ",
stream);
str = cs->get( "fileAddDate");
fileAddDate = str ? (Util::strEq( str, "yes") ? true : false) : false;
fileDateFormat = cs->get( "fileDateFormat");
str = cs->get( "sampleRate");
sampleRate = str ? Util::strToL( str) : dsp->getSampleRate();
str = cs->get( "bitrate");
bitrate = str ? Util::strToL( str) : 0;
str = cs->get( "quality");
quality = str ? Util::strToD( str) : 0.0;
str = cs->getForSure( "bitrateMode",
" not specified in section ",
stream);
if ( Util::strEq( str, "cbr") ) {
bitrateMode = AudioEncoder::cbr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for CBR encoding");
}
} else if ( Util::strEq( str, "abr") ) {
bitrateMode = AudioEncoder::abr;
if ( bitrate == 0 ) {
throw Exception( __FILE__, __LINE__,
"bitrate not specified for ABR encoding");
}
} else if ( Util::strEq( str, "vbr") ) {
bitrateMode = AudioEncoder::vbr;
if ( cs->get( "quality" ) == 0 ) {
throw Exception( __FILE__, __LINE__,
"quality not specified for VBR encoding");
}
} else {
throw Exception( __FILE__, __LINE__,
"invalid bitrate mode: ", str);
}
if (Util::strEq(format, "aac") && bitrateMode != AudioEncoder::abr) {
throw Exception(__FILE__, __LINE__,
"currently the AAC format only supports "
"average bitrate mode");
}
if (Util::strEq(format, "aacp") && bitrateMode != AudioEncoder::cbr) {
throw Exception(__FILE__, __LINE__,
"currently the AAC+ format only supports "
"constant bitrate mode");
}
str = cs->get( "lowpass");
lowpass = str ? Util::strToL( str) : 0;
str = cs->get( "highpass");
highpass = str ? Util::strToL( str) : 0;
// go on and create the things
// the underlying file
if ( fileAddDate ) {
if (fileDateFormat == 0) {
targetFileName = Util::fileAddDate( targetFileName);
}
else {
targetFileName = Util::fileAddDate( targetFileName,
fileDateFormat );
}
}
FileSink * targetFile = new FileSink( stream, targetFileName);
if ( !targetFile->exists() ) {
if ( !targetFile->create() ) {
throw Exception( __FILE__, __LINE__,
"can't create output file", targetFileName);
}
}
// streaming related stuff
audioOuts[u].socket = 0;
audioOuts[u].server = new FileCast( targetFile );
if ( Util::strEq( format, "mp3") ) {
#ifndef HAVE_LAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with lame support, "
"thus can't create mp3 stream: ",
stream);
#else
audioOuts[u].encoder = new LameLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel(),
lowpass,
highpass );
#endif // HAVE_TWOLAME_LIB
} else if ( Util::strEq( format, "mp2") ) {
#ifndef HAVE_TWOLAME_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with TwoLAME support, "
"thus can't create MPEG Audio Layer 2 stream: ",
stream);
#else
audioOuts[u].encoder = new TwoLameLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
sampleRate,
dsp->getChannel() );
#endif // HAVE_TWOLAME_LIB
} else if ( Util::strEq( format, "vorbis") ) {
#ifndef HAVE_VORBIS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with Ogg Vorbis support, "
"thus can't Ogg Vorbis stream: ",
stream);
#else
audioOuts[u].encoder = new VorbisLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
dsp->getSampleRate(),
dsp->getChannel() );
#endif // HAVE_VORBIS_LIB
} else if ( Util::strEq( format, "opus") ) {
#ifndef HAVE_OPUS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with Ogg Opus support, "
"thus can't Ogg Opus stream: ",
stream);
#else
audioOuts[u].encoder = new OpusLibEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
dsp->getSampleRate(),
dsp->getChannel() );
#endif // HAVE_OPUS_LIB
} else if ( Util::strEq( format, "aac") ) {
#ifndef HAVE_FAAC_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with AAC support, "
"thus can't aac stream: ",
stream);
#else
audioOuts[u].encoder = new FaacEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel());
#endif // HAVE_FAAC_LIB
} else if ( Util::strEq( format, "aacp") ) {
#ifndef HAVE_AACPLUS_LIB
throw Exception( __FILE__, __LINE__,
"DarkIce not compiled with AAC+ support, "
"thus can't aacplus stream: ",
stream);
#else
audioOuts[u].encoder = new aacPlusEncoder(
audioOuts[u].server.get(),
dsp.get(),
bitrateMode,
bitrate,
quality,
sampleRate,
dsp->getChannel());
#endif // HAVE_AACPLUS_LIB
} else {
throw Exception( __FILE__, __LINE__,
"Illegal stream format: ", format);
}
encConnector->attach( audioOuts[u].encoder.get());
}
noAudioOuts += u;
}
/*------------------------------------------------------------------------------
* Set POSIX real-time scheduling
*----------------------------------------------------------------------------*/
void
DarkIce :: setRealTimeScheduling ( void ) throw ( Exception )
{
// Only if the OS has the POSIX real-time scheduling functions implemented.
#if defined( HAVE_SCHED_GETSCHEDULER ) && defined( HAVE_SCHED_GETPARAM )
int high_priority;
struct sched_param param;
/* store the old scheduling parameters */
if ( (origSchedPolicy = sched_getscheduler(0)) == -1 ) {
throw Exception( __FILE__, __LINE__, "sched_getscheduler", errno);
}
if ( sched_getparam( 0, ¶m) == -1 ) {
throw Exception( __FILE__, __LINE__, "sched_getparam", errno);
}
origSchedPriority = param.sched_priority;
/* set SCHED_FIFO with max - 1 priority or user configured value */
if ( (high_priority = sched_get_priority_max(SCHED_FIFO)) == -1 ) {
throw Exception(__FILE__,__LINE__,"sched_get_priority_max",errno);
}
reportEvent( 8, "scheduler high priority", high_priority);
if (realTimeSchedPriority > high_priority) {
param.sched_priority = high_priority - 1;
} else {
param.sched_priority = realTimeSchedPriority;
}
if ( sched_setscheduler( 0, SCHED_FIFO, ¶m) == -1 ) {
reportEvent( 1,
"Could not set POSIX real-time scheduling, "
"this may cause recording skips.\n"
"Try to run darkice as the super-user.");
} else {
/* ask the new priortiy and report it */
if ( sched_getparam( 0, ¶m) == -1 ) {
throw Exception( __FILE__, __LINE__, "sched_getparam", errno);
}
reportEvent( 1,
"Using POSIX real-time scheduling, priority",
param.sched_priority );
}
#else
reportEvent( 1, "POSIX scheduling not supported on this system, "
"this may cause recording skips");
#endif // HAVE_SCHED_GETSCHEDULER && HAVE_SCHED_GETPARAM
}
/*------------------------------------------------------------------------------
* Set the original scheduling of the process, the one prior to the
* setRealTimeScheduling call.
* WARNING: make sure you don't call this before setRealTimeScheduling!!
*----------------------------------------------------------------------------*/
void
DarkIce :: setOriginalScheduling ( void ) throw ( Exception )
{
// Only if the OS has the POSIX real-time scheduling functions implemented.
#if defined( HAVE_SCHED_GETSCHEDULER ) && defined( HAVE_SCHED_GETPARAM )
uid_t euid;
euid = geteuid();
if ( euid == 0 ) {
struct sched_param param;
if ( sched_getparam( 0, ¶m) == -1 ) {
throw Exception( __FILE__, __LINE__, "sched_getparam", errno);
}
param.sched_priority = origSchedPriority;
if ( sched_setscheduler( 0, origSchedPolicy, ¶m) == -1 ) {
throw Exception( __FILE__, __LINE__, "sched_setscheduler", errno);
}
reportEvent( 5, "reverted to original scheduling");
}
#endif // HAVE_SCHED_GETSCHEDULER && HAVE_SCHED_GETPARAM
}
/*------------------------------------------------------------------------------
* Run the encoder
*----------------------------------------------------------------------------*/
bool
DarkIce :: encode ( void ) throw ( Exception )
{
unsigned int len;
unsigned long bytes;
if ( !encConnector->open() ) {
throw Exception( __FILE__, __LINE__, "can't open connector");
}
bytes = dsp->getSampleRate() * dsp->getSampleSize() * duration;
len = encConnector->transfer( bytes, 4096, 1, 0 );
reportEvent( 1, len, "bytes transfered to the encoders");
encConnector->close();
return true;
}
/*------------------------------------------------------------------------------
* Run
*----------------------------------------------------------------------------*/
int
DarkIce :: run ( void ) throw ( Exception )
{
reportEvent( 3, "encoding");
if (enableRealTime) {
setRealTimeScheduling();
}
encode();
if (enableRealTime) {
setOriginalScheduling();
}
reportEvent( 3, "encoding ends");
return 0;
}
/*------------------------------------------------------------------------------
* Tell each sink to cut what they are doing, and start again.
*----------------------------------------------------------------------------*/
void
DarkIce :: cut ( void ) throw ()
{
reportEvent( 5, "cutting");
encConnector->cut();
reportEvent( 5, "cutting ends");
}
|