Statistics
| Branch: | Revision:

streamers / input-grapes.c @ d475e2e5

History | View | Annotate | Download (2.42 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
  char cfg[256];
33

    
34
  memset(cfg, 0, 256);
35
  res = malloc(sizeof(struct input_desc));
36
  if (res == NULL) {
37
    return NULL;
38
  }
39
  if (flags & INPUT_UDP) {
40
    sprintf(cfg, "chunkiser=udp");
41
    sprintf(cfg + strlen(cfg), ",%s", fname);
42
  } else {
43
    sprintf(cfg, "chunkiser=avf,media=av");
44
  }
45
  if (flags & INPUT_LOOP) {
46
    sprintf(cfg + strlen(cfg), ",loop=1");
47
  }
48
  res->s = input_stream_open(fname, &res->interframe, cfg);
49
  if (res->s == NULL) {
50
    free(res);
51
    res = NULL;
52
    return res;
53
  }
54
  if (res->interframe == 0) {
55
    const int *my_fds;
56
    int i = 0;
57

    
58
    my_fds = input_get_fds(res->s);
59
    while(my_fds[i] != -1) {
60
      fds[i] = my_fds[i];
61
      i = i + 1;
62
    }
63
    fds[i] = -1;
64
  } else {
65
    if (fds_size >= 1) {
66
      fds[0] = -1; //This input module needs no fds to monitor
67
    }
68
    gettimeofday(&tv, NULL);
69
    res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
70
    res->first_ts = 0;
71
    res->id = (res->start_time / res->interframe) % INT_MAX; //TODO: verify 32/64 bit
72
  }
73

    
74
  return res;
75
}
76

    
77
void input_close(struct input_desc *s)
78
{
79
  input_stream_close(s->s);
80
  free(s);
81
}
82

    
83
int input_get(struct input_desc *s, struct chunk *c)
84
{
85
  struct timeval now;
86
  int64_t delta;
87
  int res;
88

    
89
  res = chunkise(s->s, c);
90
  if (res < 0) {
91
    return -1;
92
  }
93
  if (res > 0) {
94
    c->id = s->id++;
95
  }
96
  c->attributes_size = 0;
97
  c->attributes = NULL;
98
  if (s->first_ts == 0) {
99
    s->first_ts = c->timestamp;
100
  }
101
  gettimeofday(&now, NULL);
102
  if (s->interframe) {
103
    delta = c->timestamp - s->first_ts + s->interframe;
104
    delta = delta + s->start_time - now.tv_sec * 1000000ULL - now.tv_usec;
105
    dprintf("Delta: %lld\n", delta);
106
    dprintf("Generate Chunk[%d] (TS: %llu)\n", c->id, c->timestamp);
107
    if (delta < 0) {
108
      delta = 0;
109
    }
110
  } else {
111
    delta = 0;                /* Will not be used */
112
  }
113
  c->timestamp = now.tv_sec * 1000000ULL + now.tv_usec;
114

    
115
  return delta;
116
}