shouter.cpp Source File

Main Page Modules Class Hierarchy Compound List File List Compound Members File Members

# shouter.cpp

    00001 /* MuSE - Multiple Streaming Engine
    00002  * Copyright (C) 2000-2003 Denis Rojo aka jaromil <jaromil@dyne.org>
    00003  *
    00004  * This source code is free software; you can redistribute it and/or
    00005  * modify it under the terms of the GNU Public License as published
    00006  * by the Free Software Foundation; either version 2 of the License,
    00007  * or (at your option) any later version.
    00008  *
    00009  * This source code is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00012  * Please refer to the GNU Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU Public License along with
    00015  * this source code; if not, write to:
    00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017  *
    00018  * "$Id: shouter.cpp,v 1.3 2004/03/29 10:57:16 jaromil Exp $"
    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   int vermaj,vermin,verpat;
    00034   shout_version(&vermaj,&vermin,&verpat);
    00035   func("Shouter::Shouter() using libshout version %i.%i.%i",vermaj,vermin,verpat);
    00036   */
    00037
    00038   ice = shout_new();
    00039
    00040   //  to do this, more of the code must be changed
    00041   //  shout_set_nonblocking(ice,1);
    00042
    00043   running = false;
    00044   retry = 0;
    00045   errors = 0;
    00046
    00047   /* setup defaults */
    00048   host("localhost");
    00049   ip("127.0.0.1");
    00050   port(8000);
    00051   pass("hackme");
    00052   mount("live");
    00053   login(SHOUT_PROTOCOL_HTTP); // defaults to icecast 2 login now
    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     // if allready connected, reconnect
    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   // === fixes the format of the mountpoint
    00128   if((mount())[0]!='/') {
    00129     char tmp[MAX_VALUE_SIZE];
    00130     sprintf(tmp,"/%s",mount());
    00131     mount(tmp);
    00132   }
    00133   /*
    00134   switch(format) {
    00135
    00136   case SHOUT_FORMAT_VORBIS:
    00137     if(!strstr(mount(),".ogg")) {
    00138       char tmp[MAX_VALUE_SIZE];
    00139       sprintf(tmp,"%s.ogg",mount());
    00140       mount(tmp);
    00141     }
    00142     break;
    00143
    00144   case SHOUT_FORMAT_MP3:
    00145     if(!strstr(mount(),".mp3")) {
    00146       char tmp[MAX_VALUE_SIZE];
    00147       sprintf(tmp,"%s.mp3",mount());
    00148       mount(tmp);
    00149     }
    00150     break;
    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   //if( shout_set_bitrate(ice,_bps) )
    00167   //  error("shout_set_bitrate: %s",shout_get_error(ice));
    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 }