File: poc-2250.c

package info (click to toggle)
poc-streamer 0.4.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 760 kB
  • ctags: 921
  • sloc: ansic: 8,782; makefile: 343; ruby: 152; perl: 135; yacc: 115; lex: 36; sh: 30
file content (409 lines) | stat: -rw-r--r-- 9,072 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*C
 (c) 2005 bl0rg.net
**/

#include "conf.h"

#ifdef WITH_OPENSSL
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#endif

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>

#ifdef NEED_GETOPT_H__
#include <getopt.h>
#endif

#include "mp3.h"
#include "network.h"
#include "rtp.h"
#include "signal.h"
#include "file.h"

#ifdef WITH_IPV6
static int use_ipv6 = 0;
#endif /* WITH_IPV6 */

#define MAX_FILENAME 256

#ifdef WITH_OPENSSL
RSA *rsa = NULL;
#endif /* WITH_OPENSSL */

#ifdef DEBUG_PLOSS
int ploss_rate = 20;
#endif /* DEBUG_PLOSS */

static int finished = 0;

/*M
  \emph{Maximal synchronization latency for sending packets.}

  In usecs.
**/
#define MAX_WAIT_TIME (1 * 1000 * 1000)

rtp_pkt_t pkt;

static void sig_int(int signo) {
  finished = 1;
}

/*M
  \emph{Simple RTP RFC2250 streaming server main loop.}

  The mainloop opens the MPEG Audio file \verb|filename|, reads each frame
  into an rtp packet and sends it out using the UDP socket
  \verb|sock|. After sending a packet, the mainloop sleeps for the duration
  of the packet, synchronizing  itself when the sleep is not accurate
  enough. If the sleep desynchronizes itself from the stream more
  than \verb|MAX_WAIT_TIME|, the synchronization is reset.
**/
int poc_mainloop(int sock, char *filename, int quiet) {
  /*M
    Open file for reading.
  **/
  file_t mp3_file;
  if (!file_open_read(&mp3_file, filename)) {
    fprintf(stderr, "Could not open mp3 file: %s\n", filename);
    return 0;
  }

  /*M
    Set the M-bit of packetheader.
  **/
  pkt.b.m = 1;

  static long wait_time = 0;
  unsigned long rtp_time = 0;

  /*M
    Get start time.
  **/
  struct timeval tv;
  gettimeofday(&tv, NULL);
  unsigned long start_sec, start_usec;
  start_sec = tv.tv_sec;
  start_usec = tv.tv_usec;
  
  /*M
    Cycle through the frames and send them using RTP.
  **/
  mp3_frame_t mp3_frame;
  while ((mp3_next_frame(&mp3_file, &mp3_frame) > 0) && !finished) {
    /*M
      Fill rtp packet.
    **/
    pkt.timestamp = (rtp_time) / 11.1111;
    memcpy(pkt.data + pkt.hlen, mp3_frame.raw, mp3_frame.frame_size);
    pkt.length = mp3_frame.frame_size;

    /*M
      Sign the packet if Openssl is activated.
    **/
#ifdef WITH_OPENSSL
    if (rsa != NULL) {
      if (!rtp_pkt_sign(&pkt, rsa)) {
        fprintf(stderr, "\nCould not sign packet\n");
        return 0;
      }
      pkt.b.pt = RTP_PT_SMPA;
    }
#endif

    /*M
      Simulate packet loss.
    **/
#ifdef DEBUG_PLOSS
    if (((random() % 100) >= ploss_rate) != 0) {
#endif /* DEBUG_PLOSS */
      /* send rtp packet */
      if (rtp_pkt_send(&pkt, sock) < 0) {
	if (errno == ENOBUFS) {
	  fprintf(stderr, "Output buffers full, waiting...\n");
	} else {
	  perror("Error while sending packet");
	  return 0;
	}
      }
#ifdef DEBUG_PLOSS
    }
#endif /* DEBUG_PLOSS */
    
    /*M
      Set M-bit to $0$ after sending the first frame (receiver
      synchronisation).
    **/
    pkt.b.m = 0;

    /*M
      Increment the MPEG Timestamp.
    **/
    rtp_time += mp3_frame.usec;
    wait_time += mp3_frame.usec;

    /*M
      Sender synchronisation (\verb|sleep| until the next frame has
      to be sent.
    **/
    if (wait_time > 0)
      usleep(wait_time);
    
    /*M
      Print sender information.
    **/
    if (!quiet) {
      static int count = 0;
      if ((count++ % 10) == 0) {
        if (mp3_file.size > 0) {
          fprintf(stdout,
                  "\r%02ld:%02ld/%02ld:%02ld %7ld/%7ld (%3ld%%) %3ldkbit/s %4ldb ",
                  (rtp_time/1000000) / 60,
                  (rtp_time/1000000) % 60,
                  (long)((float)(rtp_time/1000) / 
                         ((float)mp3_file.offset+1) * (float)mp3_file.size) / 
                  60000,
                  (long)((float)(rtp_time/1000) / 
                         ((float)mp3_file.offset+1) * (float)mp3_file.size) / 
                  1000 % 60,
                  mp3_file.offset,
                  mp3_file.size,
                  (long)(100*(float)mp3_file.offset/(float)mp3_file.size),
                  mp3_frame.bitrate,
                  mp3_frame.frame_size);
        } else {
          fprintf(stdout, "\r%02ld:%02ld %ld %3ldkbit/s %4ldb ",
                  (rtp_time/1000000) / 60,
                  (rtp_time/1000000) % 60,
                  mp3_file.offset,
                  mp3_frame.bitrate,
                  mp3_frame.frame_size);
        }
      }
      fflush(stdout);
    }

    /*M
      Get length of iteration.
    **/
    gettimeofday(&tv, NULL);
    unsigned long len =
      (tv.tv_sec - start_sec) * 1000000 + (tv.tv_usec - start_usec);

    wait_time -= len;
    if (abs(wait_time) > MAX_WAIT_TIME)
      wait_time = 0;

    start_sec = tv.tv_sec;
    start_usec = tv.tv_usec;
  }

  /*M
    Close the MPEG file.
  **/
  if (!file_close(&mp3_file)) {
    fprintf(stderr, "Could not close mp3 file\n");
    return 0;
  }

  return 1;
}

/*M
  \emph{Print usage information.}
**/
static void usage(void) {
#ifdef WITH_OPENSSL
  fprintf(stderr,
          "Usage: ./poc [-s address] [-p port] [-q] [-t ttl] [-c pem] files...\n");
#else
  fprintf(stderr,
          "Usage: ./poc [-s address] [-p port] [-q] [-t ttl] files...\n");
#endif
  
  fprintf(stderr, "\t-s address : destination address (default 224.0.1.23)\n");
  fprintf(stderr, "\t-p port    : destination port (default 1500)\n");
  fprintf(stderr, "\t-q         : quiet\n");
  fprintf(stderr, "\t-t ttl     : multicast ttl (default 1)\n");
#ifdef WITH_OPENSSL
  fprintf(stderr, "\t-c pem     : sign with private RSA key\n");
#endif /* WITH_OPENSSL */
#ifdef DEBUG_PLOSS
  fprintf(stderr, "\t-P ploss   : packet loss interval\n");
#endif /* DEBUG_PLOSS */  
}

/*M
  \emph{Main server routine.}

  Calls the mainloop for each filename given on the command line.
**/
int main(int argc, char *argv[]) {
  int retval = 0;

  char           *address = NULL;
  unsigned short port     = 1500;
  unsigned int   ttl      = 1;
  int            quiet    = 0;

  /*M
    Process the command line arguments.
  **/
  int c;
#ifdef WITH_OPENSSL
  while ((c = getopt(argc, argv, "hs:p:t:qc:P:")) >= 0) {
#else
  while ((c = getopt(argc, argv, "hs:p:t:qP:")) >= 0) {
#endif
    switch (c) {
    case 's':
      if (address != NULL)
        free(address);

      address = strdup(optarg);
      break;

    case 'p':
      port = (unsigned short)atoi(optarg);
      break;

    case 'q':
      quiet = 1;
      break;

    case 't':
      ttl = (unsigned int)atoi(optarg);
      break;

      /*M
        If Openssl is used, read in the RSA key.
      **/
#ifdef WITH_OPENSSL
    case 'c':
      {
        if (rsa != NULL) {
          RSA_free(rsa);
          rsa = NULL;
        }

        FILE *f = NULL;
        if (!(f = fopen(optarg, "r"))) {
          fprintf(stderr,
                  "Could not open private key %s\n",
                  optarg);
          retval = EXIT_FAILURE;
          goto exit;
        }

        if (!(rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL))) {
          fprintf(stderr,
                  "Could not read private key %s\n",
                  optarg);
          fclose(f);
          retval = EXIT_FAILURE;
          goto exit;
        }

        fclose(f);

        OpenSSL_add_all_digests();
        break;
      }
#endif /* WITH_OPENSSL */

#ifdef DEBUG_PLOSS
    case 'P':
      {
        static struct timeval tv;
        gettimeofday(&tv, NULL);
        srandom(tv.tv_sec);
        ploss_rate = atoi(optarg);
        break;
      }
#endif /* DEBUG_PLOSS */

    case 'h':
    default:
      usage();
      retval = EXIT_FAILURE;
      goto exit;
    }
  }

  if (optind == argc) {
    usage();
    retval = EXIT_FAILURE;
    goto exit;
  }

  if (address == NULL) {
#ifdef WITH_IPV6
    if (use_ipv6)
      address = strdup("ff02::4");
    else
      address = strdup("224.0.1.23");
#else
    address = strdup("224.0.1.23");
#endif /* WITH_IPV6 */
  }


  if (sig_set_handler(SIGINT, sig_int) == SIG_ERR) {
    retval = EXIT_FAILURE;
    goto exit;
  }
  
  /*M
    Open the sending socket.
  **/
  int sock;
#ifdef WITH_IPV6
   if (use_ipv6)
     sock = net_udp6_send_socket(address, port, ttl);
   else
     sock = net_udp4_send_socket(address, port, ttl);
#else
   sock  = net_udp4_send_socket(address, port, ttl);
#endif /* WITH_IPV6 */
   if (sock < 0) {
    fprintf(stderr, "Could not open socket\n");
    retval = EXIT_FAILURE;
    goto exit;
  }

  rtp_rfc2250_pkt_init(&pkt);
  pkt.b.pt = RTP_PT_MPA;
  
  /*M
    Go through all files given on command line and stream them.
  **/
  int i;
  for (i = optind; (i < argc) && !finished; i++) {
    assert(argv[i] != NULL);
    unsigned char filename[MAX_FILENAME];
    strncpy(filename, argv[i], MAX_FILENAME - 1);
    filename[MAX_FILENAME - 1] = '\0';

    if (!poc_mainloop(sock, filename, quiet))
      continue;
  }

 exit:
#ifdef WITH_OPENSSL
  if (rsa != NULL)
    RSA_free(rsa);
#endif
  
  if (address != NULL)
    free(address);
  
  return retval;
}