Statistics
| Branch: | Revision:

streamers / streamer.c @ 90f3a290

History | View | Annotate | Download (4.9 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
static const char *my_iface = NULL;
23
#ifdef HTTPIO
24
int port = 6666;
25
#else
26
static int port = 6666;
27
#endif
28
static int srv_port;
29
static const char *srv_ip = "";
30
static int period = 40;
31
static int chunks_per_second = 25;
32
static int multiply = 1;
33
static int buff_size = 50;
34
static int outbuff_size = 25;
35
static const char *fname = "input.mpg";
36
static bool loop_input = false;
37
unsigned char msgTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
38

    
39
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
static void cmdline_parse(int argc, char *argv[])
88
{
89
  int o;
90

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

    
133
        exit(-1);
134
    }
135
  }
136

    
137
  if (!channel_get_name()) {
138
    channel_set_name("generic");
139
  }
140
}
141

    
142
static struct nodeID *init(void)
143
{
144
  int i;
145
  struct nodeID *myID;
146
  char *my_addr;
147

    
148
  if (my_iface) {
149
    my_addr = iface_addr(my_iface);
150
  } else {
151
    my_addr = default_ip_addr();
152
  }
153

    
154
  if (my_addr == NULL) {
155
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
156

    
157
    return NULL;
158
  }
159
  for (i=0;i<3;i++)
160
          bind_msg_type(msgTypes[i]);
161
  myID = net_helper_init(my_addr, port);
162
  if (myID == NULL) {
163
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
164
    free(my_addr);
165

    
166
    return NULL;
167
  }
168
  free(my_addr);
169
  topInit(myID, NULL, 0);
170

    
171
  output_init(outbuff_size);
172

    
173
  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
  if (my_sock == NULL) {
185
    return -1;
186
  }
187
  if (srv_port != 0) {
188
    struct nodeID *srv;
189

    
190
    srv = create_node(srv_ip, srv_port);
191
    if (srv == NULL) {
192
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
193

    
194
      return -1;
195
    }
196
    topAddNeighbour(srv, NULL, 0);
197

    
198
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
199
  }
200

    
201
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
202

    
203
  return 0;
204
}