Statistics
| Branch: | Revision:

streamers / streamer.c @ a8a31ce0

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

    
98

    
99

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

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

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

    
163
        exit(-1);
164
    }
165
  }
166

    
167
  if (!channel_get_name()) {
168
    channel_set_name("generic");
169
  }
170
}
171

    
172
static struct nodeID *init(void)
173
{
174
  int i;
175
  struct nodeID *myID;
176
  char *my_addr;
177

    
178
  if (my_iface) {
179
    my_addr = iface_addr(my_iface);
180
  } else {
181
    my_addr = default_ip_addr();
182
  }
183

    
184
  if (my_addr == NULL) {
185
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
186

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

    
196
    return NULL;
197
  }
198
  free(my_addr);
199
  topologyInit(myID, "");
200

    
201
  return myID;
202
}
203

    
204
void leave(int sig) {
205
  end_measures();
206
  exit(sig);
207
}
208

    
209
int main(int argc, char *argv[])
210
{
211
  struct nodeID *my_sock;
212

    
213
  (void) signal(SIGTERM,leave);
214

    
215
  cmdline_parse(argc, argv);
216

    
217
  my_sock = init();
218
  if (my_sock == NULL) {
219
    return -1;
220
  }
221
  if (srv_port != 0) {
222
    struct nodeID *srv;
223

    
224
    output_init(outbuff_size, output_config);
225

    
226
    srv = create_node(srv_ip, srv_port);
227
    if (srv == NULL) {
228
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
229

    
230
      return -1;
231
    }
232
    topoAddNeighbour(srv, NULL, 0);
233

    
234
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
235
  }
236

    
237
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
238

    
239
  return 0;
240
}