Statistics
| Branch: | Revision:

streamers / streamer.c @ 3730fda0

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

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

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

    
28
const char *peername = NULL;
29

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

    
52
extern int NEIGHBORHOOD_TARGET_SIZE;
53
extern uint64_t CB_SIZE_TIME;
54

    
55
extern struct timeval print_tdiff;
56
extern struct timeval tstartdiff;
57

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

    
109

    
110

    
111
static void cmdline_parse(int argc, char *argv[])
112
{
113
  int o;
114

    
115
  int option_index = 0;
116
  static struct option long_options[] = {
117
        {"chunk_log", no_argument, 0, 0},
118
        {"measure_start", required_argument, 0, 0},
119
        {"measure_every", required_argument, 0, 0},
120
        {"playout_limit", required_argument, 0, 0},
121
        {0, 0, 0, 0}
122
  };
123

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

    
184
        exit(-1);
185
    }
186
  }
187

    
188
  if (!channel_get_name()) {
189
    channel_set_name("generic");
190
  }
191

    
192
  if (argc <= 1) {
193
    print_usage(argc, argv);
194
    fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
195
  }
196
}
197

    
198
static void init_rand(const char * s)
199
{
200
  long i, l, x;
201
  struct timeval t;
202

    
203
  gettimeofday(&t, NULL);
204

    
205
  x = 1;
206
  l = strlen(s);
207
  for (i = 0; i < l; i++) {
208
    x *= s[i];
209
  }
210

    
211
  srand(t.tv_usec * t.tv_sec * x);
212
}
213

    
214
static struct nodeID *init(void)
215
{
216
  int i;
217
  struct nodeID *myID;
218
  char *my_addr;
219

    
220
  if (my_iface) {
221
    my_addr = iface_addr(my_iface);
222
  } else {
223
    my_addr = default_ip_addr();
224
  }
225

    
226
  if (my_addr == NULL) {
227
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
228

    
229
    return NULL;
230
  }
231
  for (i=0;i<2;i++)
232
          bind_msg_type(msgTypes[i]);
233
  myID = net_helper_init(my_addr, port, net_helper_config);
234
  if (myID == NULL) {
235
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
236
    free(my_addr);
237

    
238
    return NULL;
239
  }
240
  free(my_addr);
241
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
242

    
243
  init_rand(node_addr(myID));
244

    
245
  topologyInit(myID, topo_config);
246

    
247
  return myID;
248
}
249

    
250
void leave(int sig) {
251
  end_measures();
252
  exit(sig);
253
}
254

    
255
int main(int argc, char *argv[])
256
{
257
  struct nodeID *my_sock;
258

    
259
  (void) signal(SIGTERM,leave);
260

    
261
  cmdline_parse(argc, argv);
262

    
263
  my_sock = init();
264
  if (my_sock == NULL) {
265
    return -1;
266
  }
267
  if (srv_port != 0) {
268
    struct nodeID *srv;
269

    
270
    output_init(outbuff_size, output_config);
271

    
272
    srv = create_node(srv_ip, srv_port);
273
    if (srv == NULL) {
274
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
275

    
276
      return -1;
277
    }
278
    topoAddNeighbour(srv, NULL, 0);
279

    
280
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
281
  } else {
282
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
283
  }
284
  return 0;
285
}