Statistics
| Branch: | Revision:

streamers / streamer.c @ 1d21cb23

History | View | Annotate | Download (8.3 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 <grapes_msg_types.h>
16
#include <net_helper.h>
17

    
18
#ifdef WIN32
19
#include <winsock2.h>
20
#endif
21

    
22
#include "net_helpers.h"
23
#include "loop.h"
24
#include "output.h"
25
#include "channel.h"
26
#include "topology.h"
27
#include "measures.h"
28
#include "streamer.h"
29

    
30
static struct nodeID *my_sock;
31

    
32
const char *peername = NULL;
33

    
34
static const char *my_iface = NULL;
35
static int port = 6666;
36
static int srv_port;
37
static const char *srv_ip = "";
38
static int period = 40;
39
static int chunks_per_second = 25;
40
#ifdef HTTPIO
41
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
42
int multiply = 3;
43
#else
44
static int multiply = 3;
45
#endif
46
static int buff_size = 50;
47
static int outbuff_size = 50;
48
static const char *fname = "/dev/stdin";
49
static const char *output_config;
50
static bool loop_input = false;
51
static const char *net_helper_config = "";
52
static const char *topo_config = "";
53
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
54
bool chunk_log = false;
55
static int randomize_start = 0;
56

    
57
extern int NEIGHBORHOOD_TARGET_SIZE;
58
extern uint64_t CB_SIZE_TIME;
59

    
60
extern struct timeval print_tdiff;
61
extern struct timeval tstartdiff;
62

    
63
static void print_usage(int argc, char *argv[])
64
{
65
  fprintf (stderr,
66
    "Usage:%s [options]\n"
67
    "\n"
68
    "Peer options\n"
69
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
70
    "\t           Usually it is the source peer port.\n"
71
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
72
    "\t         Usually it is the source peer IP\n"
73
    "\t[-C name]: set the channel name to use on the repository.\n"
74
    "\t           All peers should use the same channel name.\n"
75
    "\n"
76
    "\t[-b size]: set the peer Chunk Buffer size.\n"
77
    "\t           This is also the chunk trading window size.\n"
78
    "\t[-o size]: set the Output Buffer size.\n"
79
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
80
    "\t             it controls the upload capacity of peer as well.\n"
81
    "\t[-M peers]: neighbourhood target size.\n"
82
    "\t[-t config]: topology config.\n"
83
    "\t[-P port]: local UDP port to be used by the peer.\n"
84
    "\t[-I iface]: local netwok interface to be used by the peer.\n"
85
    "\t         Useful if the host has several interfaces/addresses.\n"
86
    "\t[-N name]: set the name of the peer.\n"
87
    "\t         This name will be used when publishing in the repository.\n"
88
    "\t[-n options]: pass configuration options to the net-helper\n"
89
    "\t[--chunk_log]: print a chunk level log on stderr\n"
90
    "\t[-F config]: configure the output module\n"
91
    "\n"
92
    "Special Source Peer options\n"
93
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
94
    "\t[-f filename]: name of the video stream file to transmit.\n"
95
    "\t[-l]: loop the video stream.\n"
96
    "\n"
97
    "Special options\n"
98
    "\t[--randomize_start ms]: random wait before starting in to ms millisecs.\n"
99
    "\n"
100
    "NOTE: by deafult the peer will dump the received video on STDOUT in raw format\n"
101
    "      it can be played by your favourite player simply using a pipe\n"
102
    "      e.g., | cvlc /dev/stdin\n"
103
    "\n"
104
    "Examples:\n"
105
    "\n"
106
    "Start a source peer on port 6600:\n"
107
    "\n"
108
    "%s -l -f foreman.mpg -P 6600\n"
109
    "\n"
110
    "Start a peer connecting to the previous source, and using videolan as player:\n"
111
    "\n"
112
    "%s -i <sourceIP> -p <sourcePort> | cvlc /dev/stdin\n"
113
    "=======================================================\n", argv[0], argv[0], argv[0]
114
    );
115
  }
116

    
117

    
118

    
119
static void cmdline_parse(int argc, char *argv[])
120
{
121
  int o;
122

    
123
  int option_index = 0;
124
  static struct option long_options[] = {
125
        {"chunk_log", no_argument, 0, 0},
126
        {"measure_start", required_argument, 0, 0},
127
        {"measure_every", required_argument, 0, 0},
128
        {"playout_limit", required_argument, 0, 0},
129
        {"randomize_start", required_argument, 0, 0},
130
        {0, 0, 0, 0}
131
  };
132

    
133
    while ((o = getopt_long (argc, argv, "b:o:c:p:i:P:I:f:F:m:lC:N:n:M:t:",long_options, &option_index)) != -1) { //use this function to manage long options
134
    switch(o) {
135
      case 0: //for long options
136
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
137
        if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); }
138
        if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); }
139
        if( strcmp( "playout_limit", long_options[option_index].name ) == 0 ) { CB_SIZE_TIME = atoi(optarg); }
140
        if( strcmp( "randomize_start", long_options[option_index].name ) == 0 ) { randomize_start = atoi(optarg); }
141
        break;
142
      case 'b':
143
        buff_size = atoi(optarg);
144
        break;
145
      case 'o':
146
        outbuff_size = atoi(optarg);
147
        break;
148
        case 'c':
149
        chunks_per_second = atoi(optarg);
150
        break;
151
      case 'm':
152
        multiply = atoi(optarg);
153
        break;
154
      case 'p':
155
        srv_port = atoi(optarg);
156
        break;
157
      case 'i':
158
        srv_ip = strdup(optarg);
159
        break;
160
      case 'P':
161
        port =  atoi(optarg);
162
        break;
163
      case 'I':
164
        my_iface = strdup(optarg);
165
        break;
166
      case 'f':
167
        fname = strdup(optarg);
168
        break;
169
      case 'F':
170
        output_config = strdup(optarg);
171
        break;
172
      case 'l':
173
        loop_input = true;
174
        break;
175
      case 'C':
176
        channel_set_name(optarg);
177
        break;
178
      case 'n':
179
        net_helper_config = strdup(optarg);
180
        break;
181
      case 'N':
182
        peername = strdup(optarg);
183
        break;
184
      case 'M':
185
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
186
        break;
187
      case 't':
188
        topo_config = strdup(optarg);
189
        break;
190
      default:
191
        fprintf(stderr, "Error: unknown option %c\n", o);
192
        print_usage(argc, argv);
193

    
194
        exit(-1);
195
    }
196
  }
197

    
198
  if (!channel_get_name()) {
199
    channel_set_name("generic");
200
  }
201

    
202
  if (argc <= 1) {
203
    print_usage(argc, argv);
204
    fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
205
  }
206
}
207

    
208
const struct nodeID *get_my_addr(void)
209
{
210
  return my_sock;
211
}
212

    
213
static void init_rand(const char * s)
214
{
215
  long i, l, x;
216
  struct timeval t;
217

    
218
  gettimeofday(&t, NULL);
219

    
220
  x = 1;
221
  l = strlen(s);
222
  for (i = 0; i < l; i++) {
223
    x *= s[i];
224
  }
225

    
226
  srand(t.tv_usec * t.tv_sec * x);
227
}
228

    
229
static struct nodeID *init(void)
230
{
231
  int i;
232
  struct nodeID *myID;
233
  char *my_addr;
234

    
235
  if (my_iface) {
236
    my_addr = iface_addr(my_iface);
237
  } else {
238
    my_addr = default_ip_addr();
239
  }
240

    
241
  if (my_addr == NULL) {
242
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
243

    
244
    return NULL;
245
  }
246
  for (i=0;i<2;i++)
247
          bind_msg_type(msgTypes[i]);
248
  myID = net_helper_init(my_addr, port, net_helper_config);
249
  if (myID == NULL) {
250
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
251
    free(my_addr);
252

    
253
    return NULL;
254
  }
255
  free(my_addr);
256
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
257

    
258
  init_rand(node_addr(myID));
259

    
260
  topologyInit(myID, topo_config);
261

    
262
  return myID;
263
}
264

    
265
void leave(int sig) {
266
  end_measures();
267
  exit(sig);
268
}
269

    
270
static void random_wait(int max) {
271
    struct timespec t;
272
    uint64_t ms;
273

    
274
    ms = (rand()/(RAND_MAX + 1.0)) * max;
275
    t.tv_sec = ms / 1000000;
276
    t.tv_nsec = (ms % 1000000) * 1000;
277
    nanosleep(&t, NULL);
278
}
279

    
280
int main(int argc, char *argv[])
281
{
282

    
283
  (void) signal(SIGTERM,leave);
284

    
285
  cmdline_parse(argc, argv);
286

    
287
  my_sock = init();
288
  if (my_sock == NULL) {
289
    return -1;
290
  }
291
  if (srv_port != 0) {
292
    struct nodeID *srv;
293

    
294
    //random wait a bit before starting
295
    if (randomize_start) random_wait(randomize_start);
296

    
297
    output_init(outbuff_size, output_config);
298

    
299
    srv = create_node(srv_ip, srv_port);
300
    if (srv == NULL) {
301
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
302

    
303
      return -1;
304
    }
305
    topoAddNeighbour(srv, NULL, 0);
306

    
307
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
308
  } else {
309
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
310
  }
311
  return 0;
312
}
313

    
314
int am_i_source()
315
{
316
  return (srv_port == 0);
317
}
318

    
319
int get_cb_size()
320
{
321
  return buff_size;
322
}