source: roaraudio/roarclients/roarshout.c @ 5439:7950543cabbc

Last change on this file since 5439:7950543cabbc was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

File size: 7.6 KB
Line 
1//roarshout.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio 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
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27
28#ifdef ROAR_HAVE_LIBSHOUT
29#include <shout/shout.h>
30
31#define BUFSIZE 2048
32
33void usage (void) {
34 printf("roarshout [OPTIONS]... [address [port [password [mountpoint]]]\n");
35
36 printf("\nRoarAudio Options:\n\n");
37
38 printf("    --server SERVER    - Set server hostname\n"
39        "    --rate   RATE      - Set sample rate\n"
40        "    --bits   BITS      - Set bits per sample\n"
41        "    --chans  CHANNELS  - Set number of channels\n"
42        "    --codec  CODEC     - Set the codec\n"
43        " -h --help             - Show this help\n"
44        "    --pw-arg           - Password is supplied as argument (default).\n"
45        "    --pw-ask           - Ask user for password interactively.\n"
46        "    --pw-dstr          - Read password from file. Filename is supplied\n"
47        "                         as normal password argument.\n"
48       );
49
50 printf("\nlibshout Options:\n\n");
51
52 printf(" -p --public           - Allow listing in stream directory\n"
53        " -d          DESC      - Set stream description\n"
54        " -g          GENRE     - Set stream genre\n"
55        " -n          NAME      - Set stream name\n"
56        " -u          URL       - Set stream URL/homepage\n"
57       );
58
59}
60
61char * read_pw_from_file(char * filename, char * buffer, size_t bufferlen) {
62 struct roar_vio_defaults def;
63 struct roar_vio_calls file;
64 ssize_t len;
65
66 if (filename == NULL || buffer == NULL)
67  return NULL;
68
69 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
70  return NULL;
71
72 if ( roar_vio_open_dstr(&file, filename, &def, 1) == -1 )
73  return NULL;
74
75 len = roar_vio_read(&file, buffer, bufferlen - 1);
76
77 if ( len == -1 )
78  return NULL;
79
80 // strip newlions.
81 for (; buffer[len-1] == '\r' || buffer[len-1] == '\n'; len--);
82
83 buffer[len] = 0;
84
85 roar_vio_close(&file);
86
87 return buffer;
88}
89
90int main (int argc, char * argv[]) {
91 enum { ARG, ASK, DSTR } pw_source = ARG;
92 int    rate     = 44100;
93 int    bits     = 16;
94 int    channels = 2;
95 int    codec    = ROAR_CODEC_OGG_VORBIS;
96 char * server   = NULL;
97 char * k;
98 char * s_server = NULL;
99 char * s_mount  = NULL;
100 char * s_pw     = NULL;
101 int    s_port   = -1;
102 char * s_desc   = NULL;
103 char * s_genre  = NULL;
104 char * s_name   = NULL;
105 char * s_url    = NULL;
106 int    s_public = 0;
107 roar_vs_t * vss;
108 int    err;
109 ssize_t ret;
110 int    i;
111 char buf[BUFSIZE];
112 shout_t * shout;
113 char password_buf[128];
114
115 for (i = 1; i < argc; i++) {
116  k = argv[i];
117
118  if ( strcmp(k, "--server") == 0 ) {
119   server = argv[++i];
120  } else if ( strcmp(k, "--rate") == 0 ) {
121   rate = roar_str2rate(argv[++i]);
122  } else if ( strcmp(k, "--bits") == 0 ) {
123   bits = roar_str2bits(argv[++i]);
124  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
125   channels = roar_str2channels(argv[++i]);
126  } else if ( strcmp(k, "--codec") == 0 ) {
127   codec = roar_str2codec(argv[++i]);
128  } else if ( strcmp(k, "--pw-arg") == 0 ) {
129   pw_source = ARG;
130  } else if ( strcmp(k, "--pw-ask") == 0 ) {
131   pw_source = ASK;
132  } else if ( strcmp(k, "--pw-dstr") == 0 ) {
133   pw_source = DSTR;
134  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--public") == 0 ) {
135   s_public = 1;
136  } else if ( strcmp(k, "-d") == 0 ) {
137   s_desc   = argv[++i];
138  } else if ( strcmp(k, "-g") == 0 ) {
139   s_genre  = argv[++i];
140  } else if ( strcmp(k, "-n") == 0 ) {
141   s_name   = argv[++i];
142  } else if ( strcmp(k, "-u") == 0 ) {
143   s_url    = argv[++i];
144  } else if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
145   usage();
146   return 0;
147  } else if ( s_server == NULL ) {
148   s_server = k;
149  } else if ( s_port   == -1 ) {
150   s_port   = atoi(k);
151  } else if ( s_pw     == NULL ) {
152   s_pw     = k;
153  } else if ( s_mount  == NULL ) {
154   s_mount  = k;
155  } else {
156   fprintf(stderr, "Error: unknown argument: %s\n", k);
157   usage();
158   return 1;
159  }
160 }
161
162 switch (pw_source) {
163  case ARG:
164    // nothing to do
165   break;
166  case ASK:
167    if ( roar_passwd_simple_ask_pw(&s_pw, "Password for icecast server?", NULL) == -1 ) {
168     fprintf(stderr, "Error: unabled to read password from user.\n");
169     return 1;
170    }
171    strncpy(password_buf, s_pw, sizeof(password_buf)-1);
172    roar_mm_free(s_pw);
173    password_buf[sizeof(password_buf)-2] = 0;
174    s_pw = password_buf;
175   break;
176  case DSTR:
177    if ( (s_pw = read_pw_from_file(s_pw, password_buf, sizeof(password_buf))) == NULL ) {
178     fprintf(stderr, "Error: unabled to read password from file.\n");
179     return 1;
180    }
181   break;
182 }
183
184 if ( s_server == NULL )
185  s_server = "localhost";
186
187 if ( s_mount == NULL )
188  s_mount  = "/roar.ogg";
189
190 if ( s_pw == NULL )
191  s_pw     = "hackme";
192
193 if ( s_port == -1 )
194  s_port   = 8000;
195
196 if ( !strcasecmp(s_pw, "hackme") ) {
197  fprintf(stderr, "Warning: Your password is very weak. Change it!\n");
198 }
199
200 shout_init();
201
202 if (!(shout = shout_new())) {
203  ROAR_ERR("Can not create shout object");
204  return 1;
205 }
206
207 if (shout_set_host(shout, s_server) != SHOUTERR_SUCCESS) {
208  ROAR_ERR("Error setting hostname: %s", shout_get_error(shout));
209  return 1;
210 }
211
212 if (shout_set_protocol(shout, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS) {
213  ROAR_ERR("Error setting protocol: %s", shout_get_error(shout));
214  return 1;
215 }
216
217 if (shout_set_port(shout, s_port) != SHOUTERR_SUCCESS) {
218  ROAR_ERR("Error setting port: %s", shout_get_error(shout));
219  return 1;
220 }
221
222 if (shout_set_password(shout, s_pw) != SHOUTERR_SUCCESS) {
223  ROAR_ERR("Error setting password: %s", shout_get_error(shout));
224  return 1;
225 }
226
227 if (shout_set_mount(shout, s_mount) != SHOUTERR_SUCCESS) {
228  ROAR_ERR("Error setting mount: %s", shout_get_error(shout));
229  return 1;
230 }
231
232 if (shout_set_user(shout, "source") != SHOUTERR_SUCCESS) {
233  ROAR_ERR("Error setting user: %s", shout_get_error(shout));
234  return 1;
235 }
236
237 if (shout_set_format(shout, SHOUT_FORMAT_OGG) != SHOUTERR_SUCCESS) {
238  ROAR_ERR("Error setting format: %s", shout_get_error(shout));
239  return 1;
240 }
241
242 shout_set_public(shout, s_public);
243
244 if (s_desc  != NULL)
245  shout_set_description(shout, s_desc);
246
247 if (s_genre != NULL)
248  shout_set_genre(shout, s_genre);
249
250 if (s_name  != NULL)
251  shout_set_name(shout, s_name);
252
253 if (s_url   != NULL)
254  shout_set_url(shout, s_url);
255
256 if ( (vss = roar_vs_new_simple(server, "roarshout", rate, channels, codec, bits, ROAR_DIR_MONITOR, &err)) == NULL ) {
257  fprintf(stderr, "Error: can not start monitoring: %s\n", roar_vs_strerr(err));
258  return 1;
259 }
260
261 if (shout_open(shout) != SHOUTERR_SUCCESS) {
262  ROAR_ERR("Can not open connection via libshout!");
263  return -1;
264 }
265
266 while((ret = roar_vs_read(vss, buf, BUFSIZE, NULL)))
267  if (shout_send(shout, (unsigned char*)buf, i) != SHOUTERR_SUCCESS)
268   break;
269
270 roar_vs_close(vss, ROAR_VS_TRUE, NULL);
271
272 shout_sync(shout);
273
274 shout_close(shout);
275
276 shout_shutdown();
277
278 return 0;
279}
280
281#else
282int main (void) {
283 fprintf(stderr, "No libshout support compiled in!\n");
284 return 1;
285}
286#endif
287//ll
Note: See TracBrowser for help on using the repository browser.