Statistics
| Branch: | Revision:

grapes / src / Tests / chunkiser_test.c @ eb837bb3

History | View | Annotate | Download (5.06 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *  Copyright (c) 2010 Luca Abeni
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7

    
8
#include <unistd.h>
9
#include <stdio.h>
10
#include <stdint.h>
11
#include <stdlib.h>
12
#include <sys/types.h>
13

    
14
#include "chunk.h"
15
#include "chunkiser.h"
16
#include "net_helper.h"
17

    
18
static char out_opts[1024];
19
static char *out_ptr = out_opts;
20
static char in_opts[1024];
21
static char *in_ptr = in_opts;
22

    
23
static void help(const char *name)
24
{
25
  fprintf(stderr, "Usage: %s [options] <input> <output>\n", name);
26
  fprintf(stderr, "options: u:f:rRdlavVUT\n");
27
  fprintf(stderr, "\t -u <port>: use the UDP chunkiser (on <port>) for input\n");
28
  fprintf(stderr, "\t -f <fmt>: use the <fmt> format for ouptut (libav-based)\n");
29
  fprintf(stderr, "\t -r: use RAW output\n");
30
  fprintf(stderr, "\t -R: use RAW output, removing the libav payload header\n");
31
  fprintf(stderr, "\t -d: use the dummy chunkiser\n");
32
  fprintf(stderr, "\t -l: loop\n");
33
  fprintf(stderr, "\t -a: audio-only in the libav ouptut\n");
34
  fprintf(stderr, "\t -v: video-only in the libav output\n");
35
  fprintf(stderr, "\t -V: audio/video in the libav output\n");
36
  fprintf(stderr, "\t -U: use RAW output, removing the UDP payload header\n");
37
  fprintf(stderr, "\t -T: use RAW output, removing the RTP payload heade\n");
38
  fprintf(stderr, "\t -I <config>: specify the chunkiser config\n");
39
  fprintf(stderr, "\t -O <config>: specify the dechunkiser config\n");
40
}
41

    
42
static char *addopt(char *opts, char *opts_ptr, const char *tag, const char *value)
43
{
44
  if (opts_ptr != opts) {
45
    *opts_ptr++ = ',';
46
  }
47
  opts_ptr += sprintf(opts_ptr, "%s=%s", tag, value);
48
  return opts_ptr;
49
}
50

    
51
static int cmdline_parse(int argc, char *argv[])
52
{
53
  int o;
54

    
55
  while ((o = getopt(argc, argv, "lu:f:rRdlavVUTO:I:")) != -1) {
56
    switch(o) {
57
      case 'l':
58
        in_ptr = addopt(in_opts, in_ptr, "loop", "1");
59
        break;
60
      case 'u':
61
        in_ptr = addopt(in_opts, in_ptr, "chunkiser", "udp");
62
        in_ptr = addopt(in_opts, in_ptr, "port1", optarg);
63
        break;
64
      case 'f':
65
        out_ptr = addopt(out_opts, out_ptr, "format", optarg);
66
        break;
67
      case 'r':
68
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "raw");
69
        break;
70
      case 'R':
71
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "raw");
72
        out_ptr = addopt(out_opts, out_ptr, "payload", "avf");
73
        break;
74
      case 'U':
75
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "raw");
76
        out_ptr = addopt(out_opts, out_ptr, "payload", "udp");
77
        break;
78
      case 'T':
79
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "raw");
80
        out_ptr = addopt(out_opts, out_ptr, "payload", "rtp");
81
        break;
82
      case 'd':
83
        in_ptr = addopt(in_opts, in_ptr, "chunkiser", "dummy");
84
        break;
85
      case 'a':
86
        in_ptr = addopt(in_opts, in_ptr, "chunkiser", "avf");
87
        in_ptr = addopt(in_opts, in_ptr, "media", "audio");
88
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "avf");
89
        out_ptr = addopt(out_opts, out_ptr, "media", "audio");
90
        break;
91
      case 'v':
92
        in_ptr = addopt(in_opts, in_ptr, "chunkiser", "avf");
93
        in_ptr = addopt(in_opts, in_ptr, "media", "video");
94
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "avf");
95
        out_ptr = addopt(out_opts, out_ptr, "media", "video");
96
        break;
97
      case 'V':
98
        in_ptr = addopt(in_opts, in_ptr, "chunkiser", "avf");
99
        in_ptr = addopt(in_opts, in_ptr, "media", "av");
100
        out_ptr = addopt(out_opts, out_ptr, "dechunkiser", "avf");
101
        out_ptr = addopt(out_opts, out_ptr, "media", "av");
102
        break;
103
      case 'O':
104
        out_ptr += sprintf(out_ptr, "%s", optarg);
105
        break;
106
      case 'I':
107
        in_ptr += sprintf(in_ptr, "%s", optarg);
108
        break;
109
      default:
110
        fprintf(stderr, "Error: unknown option %c\n", o);
111

    
112
        exit(-1);
113
    }
114
  }
115

    
116
  return optind - 1;
117
}
118

    
119
static void in_wait(const int *fd)
120
{
121
  int my_fd[10];
122
  int i = 0;
123
  
124
  if (fd == NULL) {
125
    return;
126
  }
127
  while(fd[i] != -1) {
128
    my_fd[i] = fd[i];
129
    i++;
130
  }
131
  my_fd[i] = -1;
132

    
133
  wait4data(NULL, NULL, my_fd);
134
}
135

    
136
int main(int argc, char *argv[])
137
{
138
  int period, done, id;
139
  struct input_stream *input;
140
  struct output_stream *output;
141
  const int *in_fds;
142

    
143
  if (argc < 3) {
144
    help(argv[0]);
145

    
146
    return -1;
147
  }
148
  argv += cmdline_parse(argc, argv);
149
  input = input_stream_open(argv[1], &period, in_opts);
150
  if (input == NULL) {
151
    fprintf(stderr, "Cannot open input %s\n", argv[1]);
152

    
153
    return -1;
154
  }
155
  if (period == 0) {
156
    in_fds = input_get_fds(input);
157
  } else {
158
    in_fds = NULL;
159
  }
160
  output = out_stream_init(argv[2], out_opts);
161
  if (output == NULL) {
162
    fprintf(stderr, "Cannot open output %s\n", argv[2]);
163

    
164
    return -1;
165
  }
166

    
167
  done = 0; id = 0;
168
  while(!done) {
169
    int res;
170
    struct chunk c;
171

    
172
    in_wait(in_fds);
173
    c.id = id;
174
    res = chunkise(input, &c);
175
    if (res > 0) {
176
      fprintf(stderr,"chunk %d: %d %llu\n", id++, c.size, c.timestamp);
177
      chunk_write(output, &c);
178
    } else if (res < 0) {
179
      done = 1;
180
    }
181
    free(c.data);
182
  }
183
  input_stream_close(input);
184
  out_stream_close(output);
185

    
186
  return 0;
187
}