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