Statistics
| Branch: | Revision:

streamers / input.c @ 46c24e94

History | View | Annotate | Download (1.47 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, uint16_t flags)
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->first_ts = 0;
34
  res->s = input_stream_open(fname, &res->interframe, flags);
35
  if (res->s == NULL) {
36
    free(res);
37
    res = NULL;
38
  }
39

    
40
  return res;
41
}
42

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

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

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

    
72
  return delta > 0 ? delta : 0;
73
}