Statistics
| Branch: | Revision:

streamers / streamer.c @ af13201b

History | View | Annotate | Download (5.12 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 <msg_types.h>
14
#include <net_helper.h>
15
#include <topmanager.h>
16

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

    
22
const char *peername = NULL;
23

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

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

    
89

    
90

    
91
static void cmdline_parse(int argc, char *argv[])
92
{
93
  int o;
94

    
95
  while ((o = getopt(argc, argv, "b:o:c:t:p:i:P:I:f:m:lC:N:")) != -1) {
96
    switch(o) {
97
      case 'b':
98
        buff_size = atoi(optarg);
99
        break;
100
      case 'o':
101
        outbuff_size = atoi(optarg);
102
        break;
103
        case 'c':
104
        chunks_per_second = atoi(optarg);
105
        break;
106
      case 'm':
107
        multiply = atoi(optarg);
108
        break;
109
      case 't':
110
        period = atoi(optarg);
111
        break;
112
      case 'p':
113
        srv_port = atoi(optarg);
114
        break;
115
      case 'i':
116
        srv_ip = strdup(optarg);
117
        break;
118
      case 'P':
119
        port =  atoi(optarg);
120
        break;
121
      case 'I':
122
        my_iface = strdup(optarg);
123
        break;
124
      case 'f':
125
        fname = strdup(optarg);
126
        break;
127
      case 'l':
128
        loop_input = true;
129
        break;
130
      case 'C':
131
        channel_set_name(optarg);
132
        break;
133
      case 'N':
134
        peername = strdup(optarg);
135
        break;
136
      default:
137
        fprintf(stderr, "Error: unknown option %c\n", o);
138
        print_usage();
139

    
140
        exit(-1);
141
    }
142
  }
143

    
144
  if (!channel_get_name()) {
145
    channel_set_name("generic");
146
  }
147
}
148

    
149
static struct nodeID *init(void)
150
{
151
  int i;
152
  struct nodeID *myID;
153
  char *my_addr;
154

    
155
  if (my_iface) {
156
    my_addr = iface_addr(my_iface);
157
  } else {
158
    my_addr = default_ip_addr();
159
  }
160

    
161
  if (my_addr == NULL) {
162
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
163

    
164
    return NULL;
165
  }
166
  for (i=0;i<3;i++)
167
          bind_msg_type(msgTypes[i]);
168
  myID = net_helper_init(my_addr, port);
169
  if (myID == NULL) {
170
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
171
    free(my_addr);
172

    
173
    return NULL;
174
  }
175
  free(my_addr);
176
  topInit(myID, NULL, 0, "");
177

    
178
  output_init(outbuff_size);
179

    
180
  return myID;
181
}
182

    
183

    
184
int main(int argc, char *argv[])
185
{
186
  struct nodeID *my_sock;
187

    
188
  cmdline_parse(argc, argv);
189

    
190
  my_sock = init();
191
  if (my_sock == NULL) {
192
    return -1;
193
  }
194
  if (srv_port != 0) {
195
    struct nodeID *srv;
196

    
197
    srv = create_node(srv_ip, srv_port);
198
    if (srv == NULL) {
199
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
200

    
201
      return -1;
202
    }
203
    topAddNeighbour(srv, NULL, 0);
204

    
205
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
206
  }
207

    
208
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
209

    
210
  return 0;
211
}