Statistics
| Branch: | Revision:

streamers / streamer.c @ 4aaa8891

History | View | Annotate | Download (5.8 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
#include <topmanager.h>
17

    
18
#include "net_helpers.h"
19
#include "loop.h"
20
#include "output.h"
21
#include "channel.h"
22

    
23
const char *peername = NULL;
24

    
25
static const char *my_iface = NULL;
26
#ifdef HTTPIO
27
int port = 6666;
28
#else
29
static int port = 6666;
30
#endif
31
static int srv_port;
32
static const char *srv_ip = "";
33
static int period = 40;
34
static int chunks_per_second = 25;
35
static int multiply = 1;
36
static int buff_size = 50;
37
static int outbuff_size = 25;
38
static const char *fname = "input.mpg";
39
static const char *output_config;
40
static bool loop_input = false;
41
unsigned char msgTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
42
bool log_on = false;
43

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

    
94

    
95

    
96
static void cmdline_parse(int argc, char *argv[])
97
{
98
  int o;
99

    
100
  int option_index = 0;
101
  static struct option long_options[] = {
102
         {"chunk_log", 0, 0, 0},
103
  };
104

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

    
157
        exit(-1);
158
    }
159
  }
160

    
161
  if (!channel_get_name()) {
162
    channel_set_name("generic");
163
  }
164
}
165

    
166
static struct nodeID *init(void)
167
{
168
  int i;
169
  struct nodeID *myID;
170
  char *my_addr;
171

    
172
  if (my_iface) {
173
    my_addr = iface_addr(my_iface);
174
  } else {
175
    my_addr = default_ip_addr();
176
  }
177

    
178
  if (my_addr == NULL) {
179
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
180

    
181
    return NULL;
182
  }
183
  for (i=0;i<3;i++)
184
          bind_msg_type(msgTypes[i]);
185
  myID = net_helper_init(my_addr, port);
186
  if (myID == NULL) {
187
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
188
    free(my_addr);
189

    
190
    return NULL;
191
  }
192
  free(my_addr);
193
  topInit(myID, NULL, 0, "");
194

    
195
  return myID;
196
}
197

    
198
void leave(int sig) {
199
  exit(sig);
200
}
201

    
202
int main(int argc, char *argv[])
203
{
204
  struct nodeID *my_sock;
205

    
206
  (void) signal(SIGTERM,leave);
207

    
208
  cmdline_parse(argc, argv);
209

    
210
  my_sock = init();
211
  if (my_sock == NULL) {
212
    return -1;
213
  }
214
  if (srv_port != 0) {
215
    struct nodeID *srv;
216

    
217
    output_init(outbuff_size, output_config);
218

    
219
    srv = create_node(srv_ip, srv_port);
220
    if (srv == NULL) {
221
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
222

    
223
      return -1;
224
    }
225
    topAddNeighbour(srv, NULL, 0);
226

    
227
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
228
  }
229

    
230
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
231

    
232
  return 0;
233
}