Statistics
| Branch: | Revision:

streamers / input-grapes.c @ 82e914fb

History | View | Annotate | Download (2.48 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
  res = malloc(sizeof(struct input_desc));
35
  if (res == NULL) {
36
    return NULL;
37
  }
38
  if (flags & INPUT_UDP) {
39
    sprintf(cfg, "chunkiser=udp");
40
    sprintf(cfg + strlen(cfg), ",%s", fname);
41
  } else if (flags & INPUT_IPB) {
42
    sprintf(cfg, "chunkiser=ipb,media=v");
43
  } else {
44
    sprintf(cfg, "chunkiser=avf,media=av");
45
  }
46
  if (flags & INPUT_LOOP) {
47
    sprintf(cfg + strlen(cfg), ",loop=1");
48
  }
49
  res->s = input_stream_open(fname, &res->interframe, cfg);
50
  if (res->s == NULL) {
51
    free(res);
52
    res = NULL;
53
    return res;
54
  }
55
  if (res->interframe == 0) {
56
    const int *my_fds;
57
    int i = 0;
58

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

    
75
  return res;
76
}
77

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

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

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

    
116
  return delta;
117
}