Statistics
| Branch: | Revision:

streamers / streamer.c @ 20663df3

History | View | Annotate | Download (6.23 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

    
23
const char *peername = NULL;
24

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

    
46
extern int NEIGHBORHOOD_TARGET_SIZE;
47

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

    
100

    
101

    
102
static void cmdline_parse(int argc, char *argv[])
103
{
104
  int o;
105

    
106
  int option_index = 0;
107
  static struct option long_options[] = {
108
        {"chunk_log", no_argument, 0, 0},
109
        {0, 0, 0, 0}
110
  };
111

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

    
168
        exit(-1);
169
    }
170
  }
171

    
172
  if (!channel_get_name()) {
173
    channel_set_name("generic");
174
  }
175
}
176

    
177
static struct nodeID *init(void)
178
{
179
  int i;
180
  struct nodeID *myID;
181
  char *my_addr;
182

    
183
  if (my_iface) {
184
    my_addr = iface_addr(my_iface);
185
  } else {
186
    my_addr = default_ip_addr();
187
  }
188

    
189
  if (my_addr == NULL) {
190
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
191

    
192
    return NULL;
193
  }
194
  for (i=0;i<2;i++)
195
          bind_msg_type(msgTypes[i]);
196
  myID = net_helper_init(my_addr, port, net_helper_config);
197
  if (myID == NULL) {
198
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
199
    free(my_addr);
200

    
201
    return NULL;
202
  }
203
  free(my_addr);
204
  topologyInit(myID, "");
205

    
206
  return myID;
207
}
208

    
209
void leave(int sig) {
210
  exit(sig);
211
}
212

    
213
int main(int argc, char *argv[])
214
{
215
  struct nodeID *my_sock;
216

    
217
  (void) signal(SIGTERM,leave);
218

    
219
  cmdline_parse(argc, argv);
220

    
221
  my_sock = init();
222
  if (my_sock == NULL) {
223
    return -1;
224
  }
225
  if (srv_port != 0) {
226
    struct nodeID *srv;
227

    
228
    output_init(outbuff_size, output_config);
229

    
230
    srv = create_node(srv_ip, srv_port);
231
    if (srv == NULL) {
232
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
233

    
234
      return -1;
235
    }
236
    topoAddNeighbour(srv, NULL, 0);
237

    
238
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
239
  }
240

    
241
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
242

    
243
  return 0;
244
}