Statistics
| Branch: | Revision:

streamers / streamer.c @ 90f3a290

History | View | Annotate | Download (4.9 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 bdd19faf Csaba Kiraly
#include <msg_types.h>
14 89e893e2 Luca
#include <net_helper.h>
15
#include <topmanager.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 89e893e2 Luca
22 613263e9 Csaba Kiraly
static const char *my_iface = NULL;
23 49026e80 GiuseppeTropea
#ifdef HTTPIO
24
int port = 6666;
25
#else
26 89e893e2 Luca
static int port = 6666;
27 49026e80 GiuseppeTropea
#endif
28 89e893e2 Luca
static int srv_port;
29 0f23522a Luca
static const char *srv_ip = "";
30 66f50156 Csaba Kiraly
static int period = 40;
31
static int chunks_per_second = 25;
32 ce80b058 Luca Abeni
static int multiply = 1;
33 789d6502 Csaba Kiraly
static int buff_size = 50;
34 f0bb64e2 Csaba Kiraly
static int outbuff_size = 25;
35 7442ecb3 Luca
static const char *fname = "input.mpg";
36 46c24e94 Csaba Kiraly
static bool loop_input = false;
37 bdd19faf Csaba Kiraly
unsigned char msgTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
38 89e893e2 Luca
39 a02fc4cf MarcoMellia
static void print_usage()
40
{
41
  fprintf (stderr,
42
    "Usage:offerstreamer [-bocmtpiPIflCh]\n"
43
    "\n"
44
    "Peer options\n"
45
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
46
    "\t           Usually it is the source peer port.\n"
47
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
48
    "\t         Usually it is the source peer IP\n"
49
    "\t[-C name]: set the channel name to use on the repository.\n"
50
    "\t           All peers should use the same channel name.\n"
51
    "\n"
52
    "\t[-b size]: set the peer Chunk Buffer size.\n"
53
    "\t           This is also the chunk trading window size.\n"
54
    "\t[-o size]: set the Output Buffer size.\n"
55
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
56
    "\t             it controls the upload capacity of peer as well.\n"
57
    "\t[-t time]: chunk emission period. STILL NEEDED??\n"
58
    "\t[-P port]: local UDP port to be used by the peer.\n"
59
    "\t[-I IP]: local IP address to be used by the peer.\n"
60
    "\t         Useful if the host has several interfaces/addresses.\n"
61
    "\n"
62
    "Special Source Peer options\n"
63
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
64
    "\t[-f filename]: name of the video stream file to transmit.\n"
65
    "\t[-l]: loop the video stream.\n"
66
    "\n"
67
    "NOTE: the peer will dump the received video on STDOUT in raw format\n"
68
    "      it can be played by your favourite player simply using a pipe\n"
69
    "      e.g., | vlc -\n"
70
    "\n"
71
    "Examples:\n"
72
    "\n"
73
    "Start a source peer on port 6600:\n"
74
    "\n"
75
    "./offestreamer -m 3 -C MyTest -l -f foreman.avi -P 6600\n"
76
    "\n"
77
    "Start a peer connecting to the previous source, and using videolan as player:\n"
78
    "\n"
79
    "./offerstreamer -i 130.192.9.140 -p 6600 |vlc -\n"
80
    "\n"
81
    
82
    );
83
  }
84
85
86
87 89e893e2 Luca
static void cmdline_parse(int argc, char *argv[])
88
{
89
  int o;
90
91 581339c4 Csaba Kiraly
  while ((o = getopt(argc, argv, "b:o:c:t:p:i:P:I:f:m:lC:")) != -1) {
92 89e893e2 Luca
    switch(o) {
93 90f3a290 CsabaKiraly
      case 'b':
94 0a40460a Luca
        buff_size = atoi(optarg);
95
        break;
96 90f3a290 CsabaKiraly
      case 'o':
97 f0bb64e2 Csaba Kiraly
        outbuff_size = atoi(optarg);
98
        break;
99 90f3a290 CsabaKiraly
        case 'c':
100 0a40460a Luca
        chunks_per_second = atoi(optarg);
101
        break;
102 90f3a290 CsabaKiraly
      case 'm':
103 59b7cfc6 Csaba Kiraly
        multiply = atoi(optarg);
104
        break;
105 90f3a290 CsabaKiraly
      case 't':
106 0a40460a Luca
        period = atoi(optarg);
107
        break;
108 90f3a290 CsabaKiraly
      case 'p':
109 89e893e2 Luca
        srv_port = atoi(optarg);
110
        break;
111 90f3a290 CsabaKiraly
      case 'i':
112 89e893e2 Luca
        srv_ip = strdup(optarg);
113
        break;
114 90f3a290 CsabaKiraly
      case 'P':
115 89e893e2 Luca
        port =  atoi(optarg);
116
        break;
117 90f3a290 CsabaKiraly
      case 'I':
118 568bd1bd Luca Abeni
        my_iface = strdup(optarg);
119 89e893e2 Luca
        break;
120 90f3a290 CsabaKiraly
      case 'f':
121 e37511b1 Luca Abeni
        fname = strdup(optarg);
122
        break;
123 90f3a290 CsabaKiraly
      case 'l':
124 46c24e94 Csaba Kiraly
        loop_input = true;
125
        break;
126 90f3a290 CsabaKiraly
      case 'C':
127 581339c4 Csaba Kiraly
        channel_set_name(optarg);
128
        break;
129 89e893e2 Luca
      default:
130
        fprintf(stderr, "Error: unknown option %c\n", o);
131 a02fc4cf MarcoMellia
        print_usage();
132 89e893e2 Luca
133
        exit(-1);
134
    }
135
  }
136 581339c4 Csaba Kiraly
137
  if (!channel_get_name()) {
138
    channel_set_name("generic");
139
  }
140 89e893e2 Luca
}
141
142
static struct nodeID *init(void)
143
{
144 bdd19faf Csaba Kiraly
  int i;
145 89e893e2 Luca
  struct nodeID *myID;
146 613263e9 Csaba Kiraly
  char *my_addr;
147
148
  if (my_iface) {
149
    my_addr = iface_addr(my_iface);
150
  } else {
151 371a4639 Csaba Kiraly
    my_addr = default_ip_addr();
152 613263e9 Csaba Kiraly
  }
153 89e893e2 Luca
154 f858e682 Luca Abeni
  if (my_addr == NULL) {
155
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
156
157
    return NULL;
158
  }
159 bdd19faf Csaba Kiraly
  for (i=0;i<3;i++)
160
          bind_msg_type(msgTypes[i]);
161 4136911a Luca Abeni
  myID = net_helper_init(my_addr, port);
162
  if (myID == NULL) {
163 89e893e2 Luca
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
164 568bd1bd Luca Abeni
    free(my_addr);
165 4136911a Luca Abeni
166 e63ea799 Luca
    return NULL;
167 89e893e2 Luca
  }
168 568bd1bd Luca Abeni
  free(my_addr);
169 c3d27400 Csaba Kiraly
  topInit(myID, NULL, 0);
170 89e893e2 Luca
171 f0bb64e2 Csaba Kiraly
  output_init(outbuff_size);
172
173 89e893e2 Luca
  return myID;
174
}
175
176
177
int main(int argc, char *argv[])
178
{
179
  struct nodeID *my_sock;
180
181
  cmdline_parse(argc, argv);
182
183
  my_sock = init();
184 e63ea799 Luca
  if (my_sock == NULL) {
185
    return -1;
186
  }
187 89e893e2 Luca
  if (srv_port != 0) {
188 74a5d4ae CsabaKiraly
    struct nodeID *srv;
189 89e893e2 Luca
190 4136911a Luca Abeni
    srv = create_node(srv_ip, srv_port);
191 0f23522a Luca
    if (srv == NULL) {
192
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
193
194
      return -1;
195
    }
196 c3d27400 Csaba Kiraly
    topAddNeighbour(srv, NULL, 0);
197 89e893e2 Luca
198 0a40460a Luca
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
199 89e893e2 Luca
  }
200
201 46c24e94 Csaba Kiraly
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
202 89e893e2 Luca
203
  return 0;
204
}