Statistics
| Branch: | Revision:

streamers / streamer.c @ d8288eb0

History | View | Annotate | Download (8.4 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
#ifndef MONL
61
extern struct timeval print_tdiff;
62
extern struct timeval tstartdiff;
63
#endif
64

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

    
119

    
120

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

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

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

    
198
        exit(-1);
199
    }
200
  }
201

    
202
  if (!channel_get_name()) {
203
    channel_set_name("generic");
204
  }
205

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

    
212
const struct nodeID *get_my_addr(void)
213
{
214
  return my_sock;
215
}
216

    
217
static void init_rand(const char * s)
218
{
219
  long i, l, x;
220
  struct timeval t;
221

    
222
  gettimeofday(&t, NULL);
223

    
224
  x = 1;
225
  l = strlen(s);
226
  for (i = 0; i < l; i++) {
227
    x *= s[i];
228
  }
229

    
230
  srand(t.tv_usec * t.tv_sec * x);
231
}
232

    
233
static struct nodeID *init(void)
234
{
235
  int i;
236
  struct nodeID *myID;
237
  char *my_addr;
238

    
239
  if (my_iface) {
240
    my_addr = iface_addr(my_iface);
241
  } else {
242
    my_addr = default_ip_addr();
243
  }
244

    
245
  if (my_addr == NULL) {
246
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
247

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

    
257
    return NULL;
258
  }
259
  free(my_addr);
260
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
261

    
262
  init_rand(node_addr(myID));
263

    
264
  topologyInit(myID, topo_config);
265

    
266
  return myID;
267
}
268

    
269
void leave(int sig) {
270
  end_measures();
271
  exit(sig);
272
}
273

    
274
static void random_wait(int max) {
275
    struct timespec t;
276
    uint64_t ms;
277

    
278
    ms = (rand()/(RAND_MAX + 1.0)) * max;
279
    t.tv_sec = ms / 1000000;
280
    t.tv_nsec = (ms % 1000000) * 1000;
281
    nanosleep(&t, NULL);
282
}
283

    
284
int main(int argc, char *argv[])
285
{
286

    
287
  (void) signal(SIGTERM,leave);
288

    
289
  cmdline_parse(argc, argv);
290

    
291
  my_sock = init();
292
  if (my_sock == NULL) {
293
    fprintf(stderr, "Cannot initialize streamer, exiting!\n");
294
    return -1;
295
  }
296
  if (srv_port != 0) {
297
    struct nodeID *srv;
298

    
299
    //random wait a bit before starting
300
    if (randomize_start) random_wait(randomize_start);
301

    
302
    output_init(outbuff_size, output_config);
303

    
304
    srv = create_node(srv_ip, srv_port);
305
    if (srv == NULL) {
306
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
307

    
308
      return -1;
309
    }
310
    topoAddNeighbour(srv, NULL, 0);
311

    
312
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
313
  } else {
314
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
315
  }
316
  return 0;
317
}
318

    
319
int am_i_source()
320
{
321
  return (srv_port == 0);
322
}
323

    
324
int get_cb_size()
325
{
326
  return buff_size;
327
}