Revision 997c2b68

View differences:

src/Chunkiser/input-stream-ts.c
20 20
struct chunkiser_ctx {
21 21
  int loop;	//loop on input file infinitely
22 22
  int pkts_per_chunk;
23
  int size;
24
  int bufsize;
25
  int pcr_period;
26
  uint8_t *buff;
27
  uint64_t old_pcr;
23 28
  int fds[2];
24 29
};
25 30
#define DEFAULT_PKTS 512
31
#define BUFSIZE_INCR (512 * 188)
26 32

  
27 33
static void ts_resync(uint8_t *buff, int *size)
28 34
{
......
50 56
    return NULL;
51 57
  }
52 58

  
59
  res->size = res->bufsize = 0;
60
  res->buff = NULL;
53 61
  res->loop = 0;
62
  res->pcr_period = 100000 / 100 * 9;
63
  res->old_pcr = 0;
54 64
  res->fds[0] = open(fname, O_RDONLY);
55 65
  if (res->fds[0] < 0) {
56 66
    free(res);
......
59 69
  }
60 70
  res->fds[1] = -1;
61 71

  
62
  *period = 0;
63 72
  res->pkts_per_chunk = DEFAULT_PKTS;
64 73
  cfg_tags = config_parse(config);
65 74
  if (cfg_tags) {
......
67 76

  
68 77
    config_value_int(cfg_tags, "loop", &res->loop);
69 78
    config_value_int(cfg_tags, "pkts", &res->pkts_per_chunk);
79
    config_value_int(cfg_tags, "pcr_period", &res->pcr_period);
70 80
    access_mode = config_value_str(cfg_tags, "mode");
71 81
    if (access_mode && !strcmp(access_mode, "nonblock")) {
72 82
      fcntl(res->fds[0], F_SETFL, O_NONBLOCK);
73 83
    }
74 84
  }
75 85
  free(cfg_tags);
86
  if (res->pcr_period) {
87
    *period = res->pcr_period;
88
  } else {
89
    *period = 0;
90
  }
76 91

  
77 92
  return res;
78 93
}
......
87 102
{
88 103
  uint8_t *res;
89 104

  
90
  res = malloc(s->pkts_per_chunk * 188);
91
  if (res == NULL) {
92
    *size = -1;
105
  if (!s->pcr_period) {
106
    res = malloc(s->pkts_per_chunk * 188);
107
    if (res == NULL) {
108
      *size = -1;
93 109

  
94
    return NULL;
95
  }
96
  *ts = 0;		/* FIXME: Read the PCR!!! */
97
  *size = read(s->fds[0], res, s->pkts_per_chunk * 188);
98
  if (res[0] != 0x47) {
99
    int err;
100

  
101
    ts_resync(res, size);
102
    err = read(s->fds[0], res + *size, s->pkts_per_chunk * 188 - *size);
103
    if (err > 0) {
104
      *size += err;
110
      return NULL;
111
    }
112
    *ts = 0;		/* FIXME: Read the PCR!!! */
113
    *size = read(s->fds[0], res, s->pkts_per_chunk * 188);
114
    if (*size && (res[0] != 0x47)) {
115
      int err;
116

  
117
      ts_resync(res, size);
118
      if (size) {
119
        err = read(s->fds[0], res + *size, s->pkts_per_chunk * 188 - *size);
120
        if (err > 0) {
121
          *size += err;
122
        }
123
      }
124
    }
125
  } else {
126
    int done;
127

  
128
    res = NULL;
129
    *size = 0;
130
    if (s->size + 188 > s->bufsize) {
131
      s->bufsize += BUFSIZE_INCR;
132
      s->buff = realloc(s->buff, s->bufsize);
133
    }
134
    done = 0;
135
    while(!done) {
136
      uint8_t *p;
137
      int err;
138

  
139
      p = s->buff + s->size;
140
      err = read(s->fds[0], p, 188);
141
      if (err == 188) {
142
        if (*p != 0x47) {
143
          ts_resync(p, &err);
144
          if (err) {
145
            err += read(s->fds[0], p + err, 188 - err);
146
          }
147
        }
148
        if (err != 188) {
149
          done = 1;
150
        } else {
151
          s->size += 188;
152
          if (p[3] & 0x20) {
153
            fprintf(stderr, "Adaptation field!!! Size: %d\n", p[4]);
154
            if (p[5] & 0x10) {
155
              unsigned long long int pcr;
156

  
157
              fprintf(stderr, "PCR!!!\n");
158
              pcr = p[6] << 24 | p[7] << 16 | p[8] << 8 | p[9];
159
              pcr = pcr << 1 | p[10] >> 7;
160
              fprintf(stderr, "%llu", pcr);
161
              if (pcr > s->old_pcr + s->pcr_period) {
162
                *ts = pcr / 9 * 100;
163
                *size = s->size;
164
                res = s->buff;
165
                s->old_pcr = pcr;
166
                s->buff = NULL;
167
                s->size = 0;
168
                s->bufsize = 0;
169
                done = 1;
170
              }
171
            }
172
          }
173
        }
174
      } else {
175
        done = 1;
176
      }
105 177
    }
106 178
  }
107 179
  if (*size % 188) {

Also available in: Unified diff