Statistics
| Branch: | Revision:

streamers / streamer.c @ 5331023a

History | View | Annotate | Download (6.74 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
#include "net_helpers.h"
18
#include "loop.h"
19
#include "output.h"
20
#include "channel.h"
21
#include "topology.h"
22
#include "measures.h"
23

    
24
const char *peername = NULL;
25

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

    
47
extern int NEIGHBORHOOD_TARGET_SIZE;
48

    
49
extern struct timeval print_tdiff;
50
extern struct timeval tstartdiff;
51

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

    
104

    
105

    
106
static void cmdline_parse(int argc, char *argv[])
107
{
108
  int o;
109

    
110
  int option_index = 0;
111
  static struct option long_options[] = {
112
        {"chunk_log", no_argument, 0, 0},
113
        {"measure_start", required_argument, 0, 0},
114
        {"measure_every", required_argument, 0, 0},
115
        {0, 0, 0, 0}
116
  };
117

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

    
177
        exit(-1);
178
    }
179
  }
180

    
181
  if (!channel_get_name()) {
182
    channel_set_name("generic");
183
  }
184
}
185

    
186
static struct nodeID *init(void)
187
{
188
  int i;
189
  struct nodeID *myID;
190
  char *my_addr;
191

    
192
  if (my_iface) {
193
    my_addr = iface_addr(my_iface);
194
  } else {
195
    my_addr = default_ip_addr();
196
  }
197

    
198
  if (my_addr == NULL) {
199
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
200

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

    
210
    return NULL;
211
  }
212
  free(my_addr);
213
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
214
  topologyInit(myID, "");
215

    
216
  return myID;
217
}
218

    
219
void leave(int sig) {
220
  end_measures();
221
  exit(sig);
222
}
223

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

    
228
  (void) signal(SIGTERM,leave);
229

    
230
  cmdline_parse(argc, argv);
231

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

    
239
    output_init(outbuff_size, output_config);
240

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

    
245
      return -1;
246
    }
247
    topoAddNeighbour(srv, NULL, 0);
248

    
249
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
250
  }
251

    
252
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
253

    
254
  return 0;
255
}