Statistics
| Branch: | Revision:

streamers / input.c @ afabfeb9

History | View | Annotate | Download (1.4 KB)

1
#include <sys/time.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <stdio.h>
5
#include <string.h>
6

    
7
#include <chunk.h>
8

    
9
#include "input.h"
10
#include "input-stream.h"
11
#include "dbg.h"
12

    
13
struct input_desc {
14
  struct input_stream *s;
15
  int id;
16
  int interframe;
17
  uint64_t start_time;
18
  uint64_t first_ts;
19
};
20

    
21
struct input_desc *input_open(const char *fname)
22
{
23
  struct input_desc *res;
24
  struct timeval tv;
25

    
26
  res = malloc(sizeof(struct input_desc));
27
  if (res == NULL) {
28
    return NULL;
29
  }
30
  res->id = 0;
31
  gettimeofday(&tv, NULL);
32
  res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
33
  res->s = input_stream_open(fname, &res->interframe);
34
  if (res->s == NULL) {
35
    free(res);
36
    res = NULL;
37
  }
38

    
39
  return res;
40
}
41

    
42
void input_close(struct input_desc *s)
43
{
44
  input_stream_close(s->s);
45
  free(s);
46
}
47

    
48
int input_get(struct input_desc *s, struct chunk *c)
49
{
50
  struct timeval now;
51
  int64_t delta;
52

    
53
  c->data = chunkise(s->s, s->id, &c->size, &c->timestamp);
54
  if (c->size == -1) {
55
    return -1;
56
  }
57
  c->id = s->id++;
58
  c->attributes_size = 0;
59
  c->attributes = NULL;
60
  if (s->first_ts == 0) {
61
    s->first_ts = c->timestamp;
62
  }
63
  delta = c->timestamp - s->first_ts + s->interframe;
64
  gettimeofday(&now, NULL);
65
  delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
66
  dprintf("Delta: %lld\n", delta);
67
  dprintf("Generate Chunk[%d] (TS: %llu): %s\n", c->id, c->timestamp, c->data);
68

    
69
  return delta > 0 ? delta : 0;
70
}