Statistics
| Branch: | Revision:

streamers / input.c @ da25233b

History | View | Annotate | Download (1.84 KB)

1 8a49328f CsabaKiraly
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <sys/time.h>
8
#include <stdlib.h>
9
#include <stdint.h>
10
#include <stdio.h>
11
#include <string.h>
12
#include <limits.h>
13
14
#include <chunk.h>
15
16
#include "input.h"
17
#include "input-stream.h"
18
#include "dbg.h"
19
20
struct input_desc {
21
  struct input_stream *s;
22
  int id;
23
  int interframe;
24
  uint64_t start_time;
25
  uint64_t first_ts;
26
};
27
28
struct input_desc *input_open(const char *fname, uint16_t flags, int *fds, int fds_size)
29
{
30
  struct input_desc *res;
31
  struct timeval tv;
32
33
  if (fds_size >= 1) {
34
    *fds = -1; //This input module needs no fds to monitor
35
  }
36
37
  res = malloc(sizeof(struct input_desc));
38
  if (res == NULL) {
39
    return NULL;
40
  }
41
  res->s = input_stream_open(fname, &res->interframe, flags);
42
  if (res->s == NULL) {
43
    free(res);
44
    res = NULL;
45
    return res;
46
  }
47
  gettimeofday(&tv, NULL);
48
  res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
49
  res->first_ts = 0;
50
  res->id = (res->start_time / res->interframe) % INT_MAX; //TODO: verify 32/64 bit
51
52
  return res;
53
}
54
55
void input_close(struct input_desc *s)
56
{
57
  input_stream_close(s->s);
58
  free(s);
59
}
60
61
int input_get(struct input_desc *s, struct chunk *c)
62
{
63
  struct timeval now;
64
  int64_t delta;
65
66
  c->data = chunkise(s->s, s->id, &c->size, &c->timestamp);
67
  if (c->size == -1) {
68
    return -1;
69
  }
70
  if (c->data) {
71
    c->id = s->id++;
72
  }
73
  c->attributes_size = 0;
74
  c->attributes = NULL;
75
  if (s->first_ts == 0) {
76
    s->first_ts = c->timestamp;
77
  }
78
  delta = c->timestamp - s->first_ts + s->interframe;
79
  gettimeofday(&now, NULL);
80
  delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
81
  dprintf("Delta: %lld\n", delta);
82
  dprintf("Generate Chunk[%d] (TS: %llu)\n", c->id, c->timestamp);
83
84
  c->timestamp = now.tv_sec * 1000000ULL + now.tv_usec;
85
86
  return delta > 0 ? delta : 0;
87
}