streamers / streamer.c @ ef7fb5ca
History | View | Annotate | Download (12.1 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Luca Abeni
|
3 |
* Copyright (c) 2010 Csaba Kiraly
|
4 |
*
|
5 |
* This is free software; see gpl-3.0.txt
|
6 |
*/
|
7 |
#include <stdlib.h> |
8 |
#include <stdint.h> |
9 |
#include <stdio.h> |
10 |
#include <stdbool.h> |
11 |
#include <string.h> |
12 |
#include <getopt.h> |
13 |
#include <signal.h> |
14 |
#include <time.h> |
15 |
#include <errno.h> |
16 |
#include <math.h> |
17 |
#ifndef NAN //NAN is missing in some old math.h versions |
18 |
#define NAN (0.0/0.0) |
19 |
#endif
|
20 |
|
21 |
#include <grapes_msg_types.h> |
22 |
#include <net_helper.h> |
23 |
|
24 |
#ifdef WIN32
|
25 |
#include <winsock2.h> |
26 |
#endif
|
27 |
|
28 |
#include "net_helpers.h" |
29 |
#include "loop.h" |
30 |
#include "output.h" |
31 |
#include "channel.h" |
32 |
#include "topology.h" |
33 |
#include "measures.h" |
34 |
#include "streamer.h" |
35 |
|
36 |
#ifndef EXTRAVERSION
|
37 |
#define EXTRAVERSION "Unknown" |
38 |
#endif
|
39 |
|
40 |
static struct nodeID *my_sock; |
41 |
|
42 |
const char *peername = NULL; |
43 |
|
44 |
static const char *my_iface = NULL; |
45 |
static int port = 6666; |
46 |
static int srv_port = 0; |
47 |
static const char *srv_ip = ""; |
48 |
static int period = 15; |
49 |
static int chunks_per_second = 25; |
50 |
static double capacity_override = NAN; |
51 |
static int multiply = 3; |
52 |
static int buff_size = 50; |
53 |
static int outbuff_size = 50; |
54 |
static const char *fname = "/dev/stdin"; |
55 |
static const char *output_config =""; |
56 |
static bool loop_input = false; |
57 |
static const char *net_helper_config = ""; |
58 |
static const char *topo_config = ""; |
59 |
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING}; |
60 |
bool chunk_log = false; |
61 |
static bool hrc = true; |
62 |
static int number_offer_thread = -1; |
63 |
static int randomize_start = 0; |
64 |
int start_id = -1; |
65 |
int end_id = -1; |
66 |
int initial_id = -1; |
67 |
|
68 |
extern int NEIGHBORHOOD_TARGET_SIZE; |
69 |
extern uint64_t CB_SIZE_TIME;
|
70 |
extern double desired_bw; |
71 |
extern double desired_rtt; |
72 |
extern double alpha_target; |
73 |
extern double topo_mem; |
74 |
extern bool topo_out; |
75 |
extern bool topo_in; |
76 |
extern bool topo_keep_best; |
77 |
extern bool topo_add_best; |
78 |
|
79 |
#ifndef MONL
|
80 |
extern struct timeval print_tdiff; |
81 |
extern struct timeval tstartdiff; |
82 |
#endif
|
83 |
|
84 |
static void print_usage(int argc, char *argv[]) |
85 |
{ |
86 |
fprintf (stderr, |
87 |
"Usage:%s [options]\n"
|
88 |
"\n"
|
89 |
"Peer options\n"
|
90 |
"\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
|
91 |
"\t Usually it is the source peer port.\n"
|
92 |
"\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
|
93 |
"\t Usually it is the source peer IP\n"
|
94 |
"\t[-C name]: set the channel name to use on the repository.\n"
|
95 |
"\t All peers should use the same channel name.\n"
|
96 |
"\n"
|
97 |
"\t[-b size]: set the peer Chunk Buffer size.\n"
|
98 |
"\t This is also the chunk trading window size.\n"
|
99 |
"\t[-o size]: set the Output Buffer size.\n"
|
100 |
"\t[-c thread]: set the number of signalling threads a peer can send per seconds.\n"
|
101 |
"\t Default: set it automatically via HRC.\n"
|
102 |
"\t[-M peers]: neighbourhood target size.\n"
|
103 |
"\t[-t config]: topology config.\n"
|
104 |
"\t[-P port]: local UDP port to be used by the peer.\n"
|
105 |
"\t[-I iface]: local netwok interface to be used by the peer.\n"
|
106 |
"\t Useful if the host has several interfaces/addresses.\n"
|
107 |
"\t[-N name]: set the name of the peer.\n"
|
108 |
"\t This name will be used when publishing in the repository.\n"
|
109 |
"\t[-n options]: pass configuration options to the net-helper\n"
|
110 |
"\t[--chunk_log]: print a chunk level log on stderr\n"
|
111 |
"\t[-F config]: configure the output module\n"
|
112 |
"\t[-a alpha]: set the topology alpha value (from 0 to 100)\n"
|
113 |
"\t[-r rtt]: set the RTT threshold (in ms) for desired neighbours\n"
|
114 |
"\t[--desired_bw bw]: set the BW threshold (in bits/s) for desired neighbours. Use of K(ilo), M(ega) allowed, e.g 0.8M\n"
|
115 |
"\t[--topo_mem p]: keep p (0..1) portion of peers between topology operations\n"
|
116 |
"\t[--topo_out]: peers only choose out-neighbours\n"
|
117 |
"\t[--topo_in]: peers only choose in-neighbours\n"
|
118 |
"\t[--topo_bidir]: peers choose both in- and out-neighbours (bidir)\n"
|
119 |
"\t[--topo_keep_best]: keep best peers, not random subset\n"
|
120 |
"\t[--topo_add_best]: add best peers among desired ones, not random subset\n"
|
121 |
"\n"
|
122 |
"Special Source Peer options\n"
|
123 |
"\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
|
124 |
"\t[-f filename]: name of the video stream file to transmit.\n"
|
125 |
"\t[-l]: loop the video stream.\n"
|
126 |
"\t[-S]: set initial chunk_id (source only).\n"
|
127 |
"\t[-s]: set start_id from which to start output.\n"
|
128 |
"\t[-e]: set end_id at which to end output.\n"
|
129 |
"\n"
|
130 |
"Special options\n"
|
131 |
"\t[--randomize_start us]: random wait before starting [0..us] microseconds.\n"
|
132 |
"\t[-v]: print version.\n"
|
133 |
"\n"
|
134 |
"NOTE: by deafult the peer will dump the received video on STDOUT in raw format\n"
|
135 |
" it can be played by your favourite player simply using a pipe\n"
|
136 |
" e.g., | cvlc /dev/stdin\n"
|
137 |
"\n"
|
138 |
"Examples:\n"
|
139 |
"\n"
|
140 |
"Start a source peer on port 6600:\n"
|
141 |
"\n"
|
142 |
"%s -l -f foreman.mpg -P 6600\n"
|
143 |
"\n"
|
144 |
"Start a peer connecting to the previous source, and using videolan as player:\n"
|
145 |
"\n"
|
146 |
"%s -i <sourceIP> -p <sourcePort> | cvlc /dev/stdin\n"
|
147 |
"=======================================================\n", argv[0], argv[0], argv[0] |
148 |
); |
149 |
} |
150 |
|
151 |
|
152 |
static double atod_kmg(const char *s) { |
153 |
double d;
|
154 |
char *e;
|
155 |
|
156 |
errno = 0;
|
157 |
d = strtod(s, &e); |
158 |
if (errno) {
|
159 |
fprintf(stderr, "Error parsing option: %s\n", s);
|
160 |
exit(-1);
|
161 |
} |
162 |
switch (*e) {
|
163 |
case 'g': |
164 |
case 'G': |
165 |
d *= 1024;
|
166 |
case 'm': |
167 |
case 'M': |
168 |
d *= 1024;
|
169 |
case 'k': |
170 |
case 'K': |
171 |
d *= 1024;
|
172 |
case 0: |
173 |
break;
|
174 |
default:
|
175 |
fprintf(stderr, "Error parsing option: %s\n", s);
|
176 |
exit(-1);
|
177 |
} |
178 |
|
179 |
return d;
|
180 |
} |
181 |
|
182 |
static void cmdline_parse(int argc, char *argv[]) |
183 |
{ |
184 |
int o;
|
185 |
|
186 |
int option_index = 0; |
187 |
static struct option long_options[] = { |
188 |
{"chunk_log", no_argument, 0, 0}, |
189 |
{"measure_start", required_argument, 0, 0}, |
190 |
{"measure_every", required_argument, 0, 0}, |
191 |
{"playout_limit", required_argument, 0, 0}, |
192 |
{"randomize_start", required_argument, 0, 0}, |
193 |
{"capacity_override", required_argument, 0, 0}, |
194 |
{"desired_bw", required_argument, 0, 0}, |
195 |
{"topo_mem", required_argument, 0, 0}, |
196 |
{"topo_in", no_argument, 0, 0}, |
197 |
{"topo_out", no_argument, 0, 0}, |
198 |
{"topo_bidir", no_argument, 0, 0}, |
199 |
{"topo_keep_best", no_argument, 0, 0}, |
200 |
{"topo_add_best", no_argument, 0, 0}, |
201 |
{0, 0, 0, 0} |
202 |
}; |
203 |
|
204 |
while ((o = getopt_long (argc, argv, "r:a:b:o:c:p:i:P:I:f:F:m:lC:N:n:M:t:s:e:S:v",long_options, &option_index)) != -1) { //use this function to manage long options |
205 |
switch(o) {
|
206 |
case 0: //for long options |
207 |
if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; } |
208 |
#ifndef MONL
|
209 |
if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); } |
210 |
if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); } |
211 |
#endif
|
212 |
if( strcmp( "playout_limit", long_options[option_index].name ) == 0 ) { CB_SIZE_TIME = atoi(optarg); } |
213 |
if( strcmp( "randomize_start", long_options[option_index].name ) == 0 ) { randomize_start = atoi(optarg); } |
214 |
if( strcmp( "capacity_override", long_options[option_index].name ) == 0 ) { capacity_override = atod_kmg(optarg); } |
215 |
if( strcmp( "desired_bw", long_options[option_index].name ) == 0 ) { desired_bw = atod_kmg(optarg); } |
216 |
if( strcmp( "topo_mem", long_options[option_index].name ) == 0 ) { topo_mem = atof(optarg); } |
217 |
else if( strcmp( "topo_in", long_options[option_index].name ) == 0 ) { topo_in = true; topo_out = false; } |
218 |
else if( strcmp( "topo_out", long_options[option_index].name ) == 0 ) { topo_in = false; topo_out = true; } |
219 |
else if( strcmp( "topo_bidir", long_options[option_index].name ) == 0 ) { topo_in = true; topo_out = true; } |
220 |
else if( strcmp( "topo_keep_best", long_options[option_index].name ) == 0 ) { topo_keep_best = true; } |
221 |
else if( strcmp( "topo_add_best", long_options[option_index].name ) == 0 ) { topo_add_best = true; } |
222 |
break;
|
223 |
case 'a': |
224 |
alpha_target = (double)atoi(optarg) / 100.0; |
225 |
break;
|
226 |
case 'r': |
227 |
desired_rtt = (double)atoi(optarg) / 1000.0; |
228 |
break;
|
229 |
case 'b': |
230 |
buff_size = atoi(optarg); |
231 |
break;
|
232 |
case 'o': |
233 |
outbuff_size = atoi(optarg); |
234 |
break;
|
235 |
case 'c': |
236 |
number_offer_thread = atoi(optarg); |
237 |
if (number_offer_thread >= 0) |
238 |
hrc = false;
|
239 |
else
|
240 |
hrc = true;
|
241 |
break;
|
242 |
case 'm': |
243 |
multiply = atoi(optarg); |
244 |
break;
|
245 |
case 'p': |
246 |
srv_port = atoi(optarg); |
247 |
break;
|
248 |
case 'i': |
249 |
srv_ip = strdup(optarg); |
250 |
break;
|
251 |
case 'P': |
252 |
port = atoi(optarg); |
253 |
break;
|
254 |
case 'I': |
255 |
my_iface = strdup(optarg); |
256 |
break;
|
257 |
case 'f': |
258 |
fname = strdup(optarg); |
259 |
break;
|
260 |
case 'F': |
261 |
output_config = strdup(optarg); |
262 |
break;
|
263 |
case 'l': |
264 |
loop_input = true;
|
265 |
break;
|
266 |
case 'C': |
267 |
channel_set_name(optarg); |
268 |
break;
|
269 |
case 'n': |
270 |
net_helper_config = strdup(optarg); |
271 |
break;
|
272 |
case 'N': |
273 |
peername = strdup(optarg); |
274 |
break;
|
275 |
case 'M': |
276 |
NEIGHBORHOOD_TARGET_SIZE = atoi(optarg); |
277 |
break;
|
278 |
case 't': |
279 |
topo_config = strdup(optarg); |
280 |
break;
|
281 |
case 'S': |
282 |
initial_id = atoi(optarg); |
283 |
break;
|
284 |
case 's': |
285 |
start_id = atoi(optarg); |
286 |
break;
|
287 |
case 'e': |
288 |
end_id = atoi(optarg); |
289 |
break;
|
290 |
case 'v': |
291 |
fprintf(stderr, "Version: %s\n", EXTRAVERSION);
|
292 |
exit(0);
|
293 |
default:
|
294 |
fprintf(stderr, "Error: unknown option %c\n", o);
|
295 |
print_usage(argc, argv); |
296 |
|
297 |
exit(-1);
|
298 |
} |
299 |
} |
300 |
|
301 |
if (!channel_get_name()) {
|
302 |
channel_set_name("generic");
|
303 |
} |
304 |
|
305 |
if (argc <= 1) { |
306 |
print_usage(argc, argv); |
307 |
fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
|
308 |
} |
309 |
} |
310 |
|
311 |
const struct nodeID *get_my_addr(void) |
312 |
{ |
313 |
return my_sock;
|
314 |
} |
315 |
|
316 |
static void init_rand(const char * s) |
317 |
{ |
318 |
long i, l, x;
|
319 |
struct timeval t;
|
320 |
|
321 |
gettimeofday(&t, NULL);
|
322 |
|
323 |
x = 1;
|
324 |
l = strlen(s); |
325 |
for (i = 0; i < l; i++) { |
326 |
x *= s[i]; |
327 |
} |
328 |
|
329 |
srand(t.tv_usec * t.tv_sec * x); |
330 |
} |
331 |
|
332 |
static struct nodeID *init(void) |
333 |
{ |
334 |
int i;
|
335 |
struct nodeID *myID;
|
336 |
char *my_addr;
|
337 |
|
338 |
if (my_iface) {
|
339 |
my_addr = iface_addr(my_iface); |
340 |
} else {
|
341 |
my_addr = default_ip_addr(); |
342 |
} |
343 |
|
344 |
if (my_addr == NULL) { |
345 |
fprintf(stderr, "Cannot find network interface %s\n", my_iface);
|
346 |
|
347 |
return NULL; |
348 |
} |
349 |
for (i=0;i<2;i++) |
350 |
bind_msg_type(msgTypes[i]); |
351 |
myID = net_helper_init(my_addr, port, net_helper_config); |
352 |
if (myID == NULL) { |
353 |
fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
|
354 |
free(my_addr); |
355 |
|
356 |
return NULL; |
357 |
} |
358 |
free(my_addr); |
359 |
fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
|
360 |
|
361 |
init_rand(node_addr(myID)); |
362 |
|
363 |
topologyInit(myID, topo_config); |
364 |
|
365 |
return myID;
|
366 |
} |
367 |
|
368 |
void leave(int sig) { |
369 |
fprintf(stderr, "Received signal %d, exiting!\n", sig);
|
370 |
end_measures(); |
371 |
exit(sig); |
372 |
} |
373 |
|
374 |
// wait [0..max] microsec
|
375 |
static void random_wait(int max) { |
376 |
struct timespec t;
|
377 |
uint64_t ms; |
378 |
|
379 |
ms = (rand()/(RAND_MAX + 1.0)) * max; |
380 |
t.tv_sec = ms / 1000000;
|
381 |
t.tv_nsec = (ms % 1000000) * 1000; |
382 |
nanosleep(&t, NULL);
|
383 |
} |
384 |
|
385 |
int main(int argc, char *argv[]) |
386 |
{ |
387 |
|
388 |
(void) signal(SIGTERM,leave);
|
389 |
(void) signal(SIGINT,leave);
|
390 |
|
391 |
cmdline_parse(argc, argv); |
392 |
|
393 |
my_sock = init(); |
394 |
if (my_sock == NULL) { |
395 |
fprintf(stderr, "Cannot initialize streamer, exiting!\n");
|
396 |
return -1; |
397 |
} |
398 |
if (srv_port != 0) { |
399 |
struct nodeID *srv;
|
400 |
|
401 |
//random wait a bit before starting
|
402 |
if (randomize_start) random_wait(randomize_start);
|
403 |
|
404 |
output_init(outbuff_size, output_config); |
405 |
|
406 |
srv = create_node(srv_ip, srv_port); |
407 |
if (srv == NULL) { |
408 |
fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
|
409 |
|
410 |
return -1; |
411 |
} |
412 |
|
413 |
topoAddNeighbour(srv, NULL, 0); |
414 |
loop(my_sock, period * 1000, buff_size, hrc, number_offer_thread);
|
415 |
} else {
|
416 |
source_loop(fname, my_sock, period * 1000, multiply, buff_size);
|
417 |
} |
418 |
return 0; |
419 |
} |
420 |
|
421 |
int am_i_source()
|
422 |
{ |
423 |
return (srv_port == 0); |
424 |
} |
425 |
|
426 |
int get_cb_size()
|
427 |
{ |
428 |
return buff_size;
|
429 |
} |
430 |
|
431 |
//capacity in bits/s, or NAN if unknown
|
432 |
double get_capacity()
|
433 |
{ |
434 |
return capacity_override;
|
435 |
} |