Statistics
| Branch: | Revision:

streamers / streamer.c @ f088e50e

History | View | Annotate | Download (5.99 KB)

1 89e893e2 Luca
/*
2 8fed7779 CsabaKiraly
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4 89e893e2 Luca
 *
5 8fed7779 CsabaKiraly
 *  This is free software; see gpl-3.0.txt
6 89e893e2 Luca
 */
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <stdio.h>
10 46c24e94 Csaba Kiraly
#include <stdbool.h>
11 89e893e2 Luca
#include <string.h>
12
#include <getopt.h>
13 5dcfffbc CsabaKiraly
#include <signal.h>
14 e28a1487 CsabaKiraly
#include <grapes_msg_types.h>
15 89e893e2 Luca
#include <net_helper.h>
16
17
#include "net_helpers.h"
18
#include "loop.h"
19 f0bb64e2 Csaba Kiraly
#include "output.h"
20 581339c4 Csaba Kiraly
#include "channel.h"
21 f088e50e MarcoBiazzini
#include "topology.h"
22 89e893e2 Luca
23 7c21b5b1 CsabaKiraly
const char *peername = NULL;
24
25 613263e9 Csaba Kiraly
static const char *my_iface = NULL;
26 89e893e2 Luca
static int port = 6666;
27
static int srv_port;
28 0f23522a Luca
static const char *srv_ip = "";
29 66f50156 Csaba Kiraly
static int period = 40;
30
static int chunks_per_second = 25;
31 6d0c8116 GiuseppeTropea
#ifdef HTTPIO
32
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
33
int multiply = 1;
34
#else
35 ce80b058 Luca Abeni
static int multiply = 1;
36 6d0c8116 GiuseppeTropea
#endif
37 789d6502 Csaba Kiraly
static int buff_size = 50;
38 f0bb64e2 Csaba Kiraly
static int outbuff_size = 25;
39 7442ecb3 Luca
static const char *fname = "input.mpg";
40 463000a4 CsabaKiraly
static const char *output_config;
41 46c24e94 Csaba Kiraly
static bool loop_input = false;
42 bdd19faf Csaba Kiraly
unsigned char msgTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
43 4aaa8891 MatteoSammarco
bool log_on = false;
44 89e893e2 Luca
45 a02fc4cf MarcoMellia
static void print_usage()
46
{
47
  fprintf (stderr,
48 e63b7606 MatteoSammarco
    "Usage:offerstreamer [-bocmtpiPIflCh --chunk_log]\n"
49 a02fc4cf MarcoMellia
    "\n"
50
    "Peer options\n"
51
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
52
    "\t           Usually it is the source peer port.\n"
53
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
54
    "\t         Usually it is the source peer IP\n"
55
    "\t[-C name]: set the channel name to use on the repository.\n"
56
    "\t           All peers should use the same channel name.\n"
57
    "\n"
58
    "\t[-b size]: set the peer Chunk Buffer size.\n"
59
    "\t           This is also the chunk trading window size.\n"
60
    "\t[-o size]: set the Output Buffer size.\n"
61
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
62
    "\t             it controls the upload capacity of peer as well.\n"
63
    "\t[-t time]: chunk emission period. STILL NEEDED??\n"
64
    "\t[-P port]: local UDP port to be used by the peer.\n"
65
    "\t[-I IP]: local IP address to be used by the peer.\n"
66
    "\t         Useful if the host has several interfaces/addresses.\n"
67 7c21b5b1 CsabaKiraly
    "\t[-N name]: set the name of the peer.\n"
68
    "\t         This name will be used when publishing in the repository.\n"
69 cffbce8e MatteoSammarco
    "\t[--chunk_log]: print a chunk level log on stderr\n"   
70 a02fc4cf MarcoMellia
    "\n"
71
    "Special Source Peer options\n"
72
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
73
    "\t[-f filename]: name of the video stream file to transmit.\n"
74 cffbce8e MatteoSammarco
    "\t[-F config]: configure the output module\n" 
75 4aaa8891 MatteoSammarco
    "\t[-l]: loop the video getopt_long instead of getoptstream.\n"
76 a02fc4cf MarcoMellia
    "\n"
77
    "NOTE: the peer will dump the received video on STDOUT in raw format\n"
78
    "      it can be played by your favourite player simply using a pipe\n"
79
    "      e.g., | vlc -\n"
80
    "\n"
81
    "Examples:\n"
82
    "\n"
83
    "Start a source peer on port 6600:\n"
84
    "\n"
85
    "./offestreamer -m 3 -C MyTest -l -f foreman.avi -P 6600\n"
86
    "\n"
87
    "Start a peer connecting to the previous source, and using videolan as player:\n"
88
    "\n"
89
    "./offerstreamer -i 130.192.9.140 -p 6600 |vlc -\n"
90
    "\n"
91
    
92
    );
93
  }
94
95
96
97 89e893e2 Luca
static void cmdline_parse(int argc, char *argv[])
98
{
99
  int o;
100
101 4aaa8891 MatteoSammarco
  int option_index = 0;
102
  static struct option long_options[] = {
103 e63b7606 MatteoSammarco
        {"chunk_log", no_argument, 0, 0},
104
        {0, 0, 0, 0}
105 4aaa8891 MatteoSammarco
  };
106
107 cffbce8e MatteoSammarco
    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 options
108 4aaa8891 MatteoSammarco
 // while ((o = getopt(argc, argv, "b:o:c:t:p:i:P:I:f:F:m:lC:N:")) != -1) {
109 89e893e2 Luca
    switch(o) {
110 4aaa8891 MatteoSammarco
      case 0: //for long options
111 e63b7606 MatteoSammarco
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { log_on = true; }
112
        break;
113 90f3a290 CsabaKiraly
      case 'b':
114 0a40460a Luca
        buff_size = atoi(optarg);
115
        break;
116 90f3a290 CsabaKiraly
      case 'o':
117 f0bb64e2 Csaba Kiraly
        outbuff_size = atoi(optarg);
118
        break;
119 90f3a290 CsabaKiraly
        case 'c':
120 0a40460a Luca
        chunks_per_second = atoi(optarg);
121
        break;
122 90f3a290 CsabaKiraly
      case 'm':
123 59b7cfc6 Csaba Kiraly
        multiply = atoi(optarg);
124
        break;
125 90f3a290 CsabaKiraly
      case 't':
126 0a40460a Luca
        period = atoi(optarg);
127
        break;
128 90f3a290 CsabaKiraly
      case 'p':
129 89e893e2 Luca
        srv_port = atoi(optarg);
130
        break;
131 90f3a290 CsabaKiraly
      case 'i':
132 89e893e2 Luca
        srv_ip = strdup(optarg);
133
        break;
134 90f3a290 CsabaKiraly
      case 'P':
135 89e893e2 Luca
        port =  atoi(optarg);
136
        break;
137 90f3a290 CsabaKiraly
      case 'I':
138 568bd1bd Luca Abeni
        my_iface = strdup(optarg);
139 89e893e2 Luca
        break;
140 90f3a290 CsabaKiraly
      case 'f':
141 e37511b1 Luca Abeni
        fname = strdup(optarg);
142
        break;
143 463000a4 CsabaKiraly
      case 'F':
144
        output_config = strdup(optarg);
145
        break;
146 90f3a290 CsabaKiraly
      case 'l':
147 46c24e94 Csaba Kiraly
        loop_input = true;
148
        break;
149 90f3a290 CsabaKiraly
      case 'C':
150 581339c4 Csaba Kiraly
        channel_set_name(optarg);
151
        break;
152 7c21b5b1 CsabaKiraly
      case 'N':
153
        peername = strdup(optarg);
154
        break;
155 89e893e2 Luca
      default:
156
        fprintf(stderr, "Error: unknown option %c\n", o);
157 a02fc4cf MarcoMellia
        print_usage();
158 89e893e2 Luca
159
        exit(-1);
160
    }
161
  }
162 581339c4 Csaba Kiraly
163
  if (!channel_get_name()) {
164
    channel_set_name("generic");
165
  }
166 89e893e2 Luca
}
167
168
static struct nodeID *init(void)
169
{
170 bdd19faf Csaba Kiraly
  int i;
171 89e893e2 Luca
  struct nodeID *myID;
172 613263e9 Csaba Kiraly
  char *my_addr;
173
174
  if (my_iface) {
175
    my_addr = iface_addr(my_iface);
176
  } else {
177 371a4639 Csaba Kiraly
    my_addr = default_ip_addr();
178 613263e9 Csaba Kiraly
  }
179 89e893e2 Luca
180 f858e682 Luca Abeni
  if (my_addr == NULL) {
181
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
182
183
    return NULL;
184
  }
185 bdd19faf Csaba Kiraly
  for (i=0;i<3;i++)
186
          bind_msg_type(msgTypes[i]);
187 4136911a Luca Abeni
  myID = net_helper_init(my_addr, port);
188
  if (myID == NULL) {
189 89e893e2 Luca
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
190 568bd1bd Luca Abeni
    free(my_addr);
191 4136911a Luca Abeni
192 e63ea799 Luca
    return NULL;
193 89e893e2 Luca
  }
194 568bd1bd Luca Abeni
  free(my_addr);
195 f088e50e MarcoBiazzini
  topologyInit(myID, "");
196 89e893e2 Luca
197
  return myID;
198
}
199
200 5dcfffbc CsabaKiraly
void leave(int sig) {
201
  exit(sig);
202
}
203 89e893e2 Luca
204
int main(int argc, char *argv[])
205
{
206
  struct nodeID *my_sock;
207
208 5dcfffbc CsabaKiraly
  (void) signal(SIGTERM,leave);
209
210 89e893e2 Luca
  cmdline_parse(argc, argv);
211
212
  my_sock = init();
213 e63ea799 Luca
  if (my_sock == NULL) {
214
    return -1;
215
  }
216 89e893e2 Luca
  if (srv_port != 0) {
217 74a5d4ae CsabaKiraly
    struct nodeID *srv;
218 89e893e2 Luca
219 463000a4 CsabaKiraly
    output_init(outbuff_size, output_config);
220
221 4136911a Luca Abeni
    srv = create_node(srv_ip, srv_port);
222 0f23522a Luca
    if (srv == NULL) {
223
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
224
225
      return -1;
226
    }
227 f088e50e MarcoBiazzini
    topoAddNeighbour(srv, NULL, 0);
228 89e893e2 Luca
229 0a40460a Luca
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
230 89e893e2 Luca
  }
231
232 46c24e94 Csaba Kiraly
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
233 89e893e2 Luca
234
  return 0;
235
}