Statistics
| Branch: | Revision:

streamers / streamer.c @ 95e6fe96

History | View | Annotate | Download (6.71 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 = 1;
39
#else
40
static int multiply = 1;
41
#endif
42
static int buff_size = 50;
43
static int outbuff_size = 25;
44
static const char *fname = "input.mpg";
45
static const char *output_config;
46
static bool loop_input = false;
47
static const char *net_helper_config = "";
48
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
49
bool chunk_log = false;
50

    
51
extern int NEIGHBORHOOD_TARGET_SIZE;
52

    
53
extern struct timeval print_tdiff;
54
extern struct timeval tstartdiff;
55

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

    
108

    
109

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

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

    
122
    while ((o = getopt_long (argc, argv, "b:o:c:t:p:i:P:I:f:F:m:lC:N:n:M:",long_options, &option_index)) != -1) { //use this function to manage long options
123
    switch(o) {
124
      case 0: //for long options
125
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
126
        if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); }
127
        if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); }
128
        break;
129
      case 'b':
130
        buff_size = atoi(optarg);
131
        break;
132
      case 'o':
133
        outbuff_size = atoi(optarg);
134
        break;
135
        case 'c':
136
        chunks_per_second = atoi(optarg);
137
        break;
138
      case 'm':
139
        multiply = atoi(optarg);
140
        break;
141
      case 't':
142
        period = 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
      case 'n':
168
        net_helper_config = strdup(optarg);
169
        break;
170
      case 'N':
171
        peername = strdup(optarg);
172
        break;
173
      case 'M':
174
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
175
        break;
176
      default:
177
        fprintf(stderr, "Error: unknown option %c\n", o);
178
        print_usage();
179

    
180
        exit(-1);
181
    }
182
  }
183

    
184
  if (!channel_get_name()) {
185
    channel_set_name("generic");
186
  }
187
}
188

    
189
static struct nodeID *init(void)
190
{
191
  int i;
192
  struct nodeID *myID;
193
  char *my_addr;
194

    
195
  if (my_iface) {
196
    my_addr = iface_addr(my_iface);
197
  } else {
198
    my_addr = default_ip_addr();
199
  }
200

    
201
  if (my_addr == NULL) {
202
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
203

    
204
    return NULL;
205
  }
206
  for (i=0;i<2;i++)
207
          bind_msg_type(msgTypes[i]);
208
  myID = net_helper_init(my_addr, port, net_helper_config);
209
  if (myID == NULL) {
210
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
211
    free(my_addr);
212

    
213
    return NULL;
214
  }
215
  free(my_addr);
216
  topologyInit(myID, "");
217

    
218
  return myID;
219
}
220

    
221
void leave(int sig) {
222
  end_measures();
223
  exit(sig);
224
}
225

    
226
int main(int argc, char *argv[])
227
{
228
  struct nodeID *my_sock;
229

    
230
  (void) signal(SIGTERM,leave);
231

    
232
  cmdline_parse(argc, argv);
233

    
234
  my_sock = init();
235
  if (my_sock == NULL) {
236
    return -1;
237
  }
238
  if (srv_port != 0) {
239
    struct nodeID *srv;
240

    
241
    output_init(outbuff_size, output_config);
242

    
243
    srv = create_node(srv_ip, srv_port);
244
    if (srv == NULL) {
245
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
246

    
247
      return -1;
248
    }
249
    topoAddNeighbour(srv, NULL, 0);
250

    
251
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
252
  }
253

    
254
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
255

    
256
  return 0;
257
}