00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <shouter.h>
00023 #include <jutils.h>
00024 #include <generic.h>
00025 #include <config.h>
00026
00027 extern bool got_sigpipe;
00028
00029 Shouter::Shouter()
00030 : Entry() {
00031
00032
00033
00034
00035
00036
00037
00038 ice = shout_new();
00039
00040
00041
00042
00043 running = false;
00044 retry = 0;
00045 errors = 0;
00046
00047
00048 host("localhost");
00049 ip("127.0.0.1");
00050 port(8000);
00051 pass("hackme");
00052 mount("live");
00053 login(SHOUT_PROTOCOL_HTTP);
00054 name("Streaming with MuSE");
00055 url("http://muse.dyne.org");
00056 desc("Free Software Multiple Streaming Engine");
00057
00058 profile_changed = true;
00059 }
00060
00061 Shouter::~Shouter() {
00062 func("Shouter::~Shouter");
00063 stop();
00064 shout_free(ice);
00065 }
00066
00067 bool Shouter::start() {
00068 int res;
00069 char srv[64];
00070 switch(login()) {
00071 case SHOUT_PROTOCOL_HTTP: sprintf(srv,"icecast2"); break;
00072 case SHOUT_PROTOCOL_ICY: sprintf(srv,"shoutcast"); break;
00073 default: sprintf(srv,"icecast 1"); break;
00074 }
00075
00076 if(shout_get_connected(ice)) {
00077
00078 func("icecast still connected: disconnecting");
00079 shout_close(ice);
00080 shout_sync(ice);
00081 }
00082
00083 notice("Contacting %s server %s on port %u",srv,host(),port());
00084
00085 res = shout_open(ice);
00086 func("Shouter::start() shout_open returns %i",res);
00087 shout_sync(ice);
00088
00089 if(res==SHOUTERR_SUCCESS) {
00090 notice("started streaming on %s",streamurl);
00091 running = true;
00092 } else {
00093 error("shout_open: %s",shout_get_error(ice));
00094 shout_close(ice);
00095 shout_sync(ice);
00096 running = false;
00097 }
00098
00099 return(running);
00100 }
00101
00102 bool Shouter::stop() {
00103 if(running) {
00104 notice("closed stream to %s",streamurl);
00105 shout_close(ice);
00106 running = false;
00107 }
00108 return true;
00109 }
00110
00111 bool Shouter::apply_profile() {
00112 char temp[256];
00113 func("Shouter::apply_profile() on shouter id %i",id);
00114
00115 bool was_running = running, res = true;
00116 if(was_running) stop();
00117
00118 if(shout_set_host(ice,host()))
00119 error("shout_set_host: %s",shout_get_error(ice));
00120
00121 if( shout_set_port(ice,port()) )
00122 error("shout_set_port: %s",shout_get_error(ice));
00123
00124 if( shout_set_password(ice,pass()) )
00125 error("shout_set_password: %s",shout_get_error(ice));
00126
00127
00128 if((mount())[0]!='/') {
00129 char tmp[MAX_VALUE_SIZE];
00130 sprintf(tmp,"/%s",mount());
00131 mount(tmp);
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 if( shout_set_mount(ice,mount()) )
00154 error("shout_set_mount: %s",shout_get_error(ice));
00155
00156 if( shout_set_name(ice,name()) )
00157 error("shout_set_name: %s",shout_get_error(ice));
00158
00159 if( shout_set_url(ice,url()) )
00160 error("shout_set_url: %s",shout_get_error(ice));
00161
00162 if( shout_set_description(ice,desc()) )
00163 error("shout_set_description: %s",shout_get_error(ice));
00164
00165
00166
00167
00168
00169
00170 if( shout_set_public(ice,1) )
00171 error("shout_set_public: %s",shout_get_error(ice));
00172
00173 if( shout_set_protocol(ice,login()) )
00174 error("shout_set_protocol %i: %s",login(),shout_get_error(ice));
00175
00176 if( shout_set_format(ice,format) )
00177 error("shout_set_format: %s",shout_get_error(ice));
00178
00179 if( shout_set_user(ice,"source") )
00180 error("shout_set_user: %s",shout_get_error(ice));
00181
00182 snprintf(temp,256,"%s ver. %s",PACKAGE,VERSION);
00183 if( shout_set_agent(ice,temp) )
00184 error("shout_set_agent: %s",shout_get_error(ice));
00185
00186 snprintf(streamurl,MAX_VALUE_SIZE,
00187 "http://%s:%i%s",host(),port(),mount());
00188
00189 if(was_running) { res = start(); }
00190 profile_changed = false;
00191 return res;
00192 }
00193
00194 int Shouter::send(short int *buf, unsigned int enc) {
00195 int res = 0;
00196 if(!running) return(0);
00197 if(enc<1) return res;
00198
00199 shout_sync(ice);
00200
00201 res = shout_send(ice,(unsigned char*) buf, enc);
00202 if(res) {
00203 error("shout_send: %s",shout_get_error(ice));
00204 if (got_sigpipe && (res==SHOUTERR_SOCKET)) {
00205 errors++;
00206 got_sigpipe = false;
00207 if(errors>10) {
00208 res = -2;
00209 errors = 0;
00210 }
00211 } else res = -1;
00212 } else errors = 0;
00213
00214 return(res);
00215 }