Statistics
| Branch: | Revision:

streamers / input-grapes.c @ 327fc1a6

History | View | Annotate | Download (2.04 KB)

1
/*
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
#include <chunkiser.h>
16

    
17
#include "input.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
  res = malloc(sizeof(struct input_desc));
34
  if (res == NULL) {
35
    return NULL;
36
  }
37
  res->s = input_stream_open(fname, &res->interframe, "media=av");
38
  if (res->s == NULL) {
39
    free(res);
40
    res = NULL;
41
    return res;
42
  }
43
  if (res->interframe == 0) {
44
    const int *my_fds;
45
    int i = 0;
46

    
47
    my_fds = input_get_fds(res->s);
48
    while(my_fds[i] != -1) {
49
      fds[i] = my_fds[i];
50
      i = i + 1;
51
    }
52
    fds[i] = -1;
53
  } else {
54
    if (fds_size >= 1) {
55
      fds[0] = -1; //This input module needs no fds to monitor
56
    }
57
    gettimeofday(&tv, NULL);
58
    res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
59
    res->first_ts = 0;
60
    res->id = (res->start_time / res->interframe) % INT_MAX; //TODO: verify 32/64 bit
61
  }
62

    
63
  return res;
64
}
65

    
66
void input_close(struct input_desc *s)
67
{
68
  input_stream_close(s->s);
69
  free(s);
70
}
71

    
72
int input_get(struct input_desc *s, struct chunk *c)
73
{
74
  struct timeval now;
75
  int64_t delta;
76
  int res;
77

    
78
  res = chunkise(s->s, c);
79
  if (res < 0) {
80
    return -1;
81
  }
82
  if (res > 0) {
83
    c->id = s->id++;
84
  }
85
  c->attributes_size = 0;
86
  c->attributes = NULL;
87
  if (s->first_ts == 0) {
88
    s->first_ts = c->timestamp;
89
  }
90
  delta = c->timestamp - s->first_ts + s->interframe;
91
  gettimeofday(&now, NULL);
92
  delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
93
  dprintf("Delta: %lld\n", delta);
94
  dprintf("Generate Chunk[%d] (TS: %llu)\n", c->id, c->timestamp);
95

    
96
  c->timestamp = now.tv_sec * 1000000ULL + now.tv_usec;
97

    
98
  return delta > 0 ? delta : 0;
99
}