Revision 327fc1a6

View differences:

Makefile
96 96
endif
97 97

  
98 98
IO ?= ffmpeg
99
ifeq ($(IO), grapes)
100
OBJS += input-grapes.o output-grapes.o
101
CPPFLAGS += -I$(FFMPEG_DIR)
102
LDFLAGS += -L$(FFMPEG_DIR)/libavcodec -L$(FFMPEG_DIR)/libavformat -L$(FFMPEG_DIR)/libavutil -L$(FFMPEG_DIR)/libavcore
103
CFLAGS += -pthread
104
LDFLAGS += -pthread
105
LDLIBS += -lavformat -lavcodec -lavutil
106
LDLIBS += $(call ld-option, -lavcore)
107
LDLIBS += -lm
108
LDLIBS += $(call ld-option, -lz)
109
LDLIBS += $(call ld-option, -lbz2)
110
endif
99 111
ifeq ($(IO), ffmpeg)
100 112
OBJS += input.o
101 113
OBJS += Chunkiser/input-stream-avs.o 
......
105 117
LDFLAGS += -L$(FFMPEG_DIR)/lib
106 118
CFLAGS += -pthread
107 119
LDFLAGS += -pthread
108
LDLIBS += -lavformat -lavcodec -lavutil
120
LDLIBS += -lavformat -lavcodec -lavcore -lavutil
109 121
LDLIBS += -lm
110 122
LDLIBS += $(call ld-option, -lz)
111 123
LDLIBS += $(call ld-option, -lbz2)
input-grapes.c
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
}
output-grapes.c
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 <unistd.h>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <stdint.h>
11
#include <stdio.h>
12

  
13
#include <chunk.h>
14
#include <chunkiser.h>
15

  
16
#include "output.h"
17
#include "measures.h"
18
#include "dbg.h"
19

  
20
static int next_chunk = -1;
21
static int buff_size;
22
extern bool chunk_log;
23

  
24
struct outbuf {
25
  struct chunk c;
26
};
27
static struct outbuf *buff;
28
static struct output_stream *out;
29

  
30
void output_init(int bufsize, const char *config)
31
{
32
  out = out_stream_init(config, "media=av");
33
  if (out == NULL) {
34
     fprintf(stderr, "Error: can't initialize output module\n");
35
     exit(1);
36
  }
37
  if (!buff) {
38
    int i;
39

  
40
    buff_size = bufsize;
41
    buff = malloc(sizeof(struct outbuf) * buff_size);
42
    if (!buff) {
43
     fprintf(stderr, "Error: can't allocate output buffer\n");
44
     exit(1);
45
    }
46
    for (i = 0; i < buff_size; i++) {
47
      buff[i].c.data = NULL;
48
    }
49
  } else {
50
   fprintf(stderr, "Error: output buffer re-init not allowed!\n");
51
   exit(1);
52
  }
53
}
54

  
55
static void buffer_print(void)
56
{
57
#ifdef DEBUG
58
  int i;
59

  
60
  if (next_chunk < 0) {
61
    return;
62
  }
63

  
64
  dprintf("\toutbuf: %d-> ",next_chunk);
65
  for (i = next_chunk; i < next_chunk + buff_size; i++) {
66
    if (buff[i % buff_size].c.data) {
67
      dprintf("%d",i % 10);
68
    } else {
69
      dprintf(".");
70
    }
71
  }
72
  dprintf("\n");
73
#endif
74
}
75

  
76
static void buffer_free(int i)
77
{
78
  dprintf("\t\tFlush Buf %d: %s\n", i, buff[i].c.data);
79
  chunk_write(out, &buff[i].c);
80
  free(buff[i].c.data);
81
  buff[i].c.data = NULL;
82
  dprintf("Next Chunk: %d -> %d\n", next_chunk, buff[i].c.id + 1);
83
  reg_chunk_playout(buff[i].c.id, true, buff[i].c.timestamp);
84
  next_chunk = buff[i].c.id + 1;
85
}
86

  
87
static void buffer_flush(int id)
88
{
89
  int i = id % buff_size;
90

  
91
  while(buff[i].c.data) {
92
    buffer_free(i);
93
    i = (i + 1) % buff_size;
94
    if (i == id % buff_size) {
95
      break;
96
    }
97
  }
98
}
99

  
100
void output_deliver(const struct chunk *c)
101
{
102
  if (!buff) {
103
    fprintf(stderr, "Warning: code should use output_init!!! Setting output buffer to 8\n");
104
    output_init(8, NULL);
105
  }
106

  
107
  dprintf("Chunk %d delivered\n", c->id);
108
  buffer_print();
109
  if (c->id < next_chunk) {
110
    return;
111
  }
112

  
113
  /* Initialize buffer with first chunk */
114
  if (next_chunk == -1) {
115
    next_chunk = c->id; // FIXME: could be anything between c->id and (c->id - buff_size + 1 > 0) ? c->id - buff_size + 1 : 0
116
  }
117

  
118
  if (c->id >= next_chunk + buff_size) {
119
    int i;
120

  
121
    /* We might need some space for storing this chunk,
122
     * or the stored chunks are too old
123
     */
124
    for (i = next_chunk; i <= c->id - buff_size; i++) {
125
      if (buff[i % buff_size].c.data) {
126
        buffer_free(i % buff_size);
127
      } else {
128
        reg_chunk_playout(c->id, false, c->timestamp); // FIXME: some chunks could be counted as lost at the beginning, depending on the initialization of next_chunk
129
        next_chunk++;
130
      }
131
    }
132
    buffer_flush(next_chunk);
133
    dprintf("Next is now %d, chunk is %d\n", next_chunk, c->id);
134
  }
135

  
136
  dprintf("%d == %d?\n", c->id, next_chunk);
137
  if (c->id == next_chunk) {
138
    dprintf("\tOut Chunk[%d] - %d: %s\n", c->id, c->id % buff_size, c->data);
139
    chunk_write(out, c);
140
    reg_chunk_playout(c->id, true, c->timestamp);
141
    next_chunk++;
142
    buffer_flush(next_chunk);
143
  } else {
144
    dprintf("Storing %d (in %d)\n", c->id, c->id % buff_size);
145
    if (buff[c->id % buff_size].c.data) {
146
      if (buff[c->id % buff_size].c.id == c->id) {
147
        /* Duplicate of a stored chunk */
148
        if(chunk_log){fprintf(stderr,"Duplicate! chunkID: %d\n", c->id);}
149
        dprintf("\tDuplicate!\n");
150
        reg_chunk_duplicate();
151
        return;
152
      }
153
      fprintf(stderr, "Crap!, chunkid:%d, storedid: %d\n", c->id, buff[c->id % buff_size].c.id);
154
      exit(-1);
155
    }
156
    /* We previously flushed, so we know that c->id is free */
157
    memcpy(&buff[c->id % buff_size].c, c, sizeof(struct chunk));
158
    buff[c->id % buff_size].c.data = malloc(c->size);
159
    memcpy(buff[c->id % buff_size].c.data, c->data, c->size);
160
  }
161
}
utils.mak
2 2
              > /dev/null 2>&1; then echo "$(1)"; fi ;)
3 3

  
4 4
ld-option = $(shell if echo "int main(){return 0;}" | \
5
		$(CC) $(CFLAGS) $(1) -o /dev/null -xc - \
5
		$(CC) $(LDFLAGS) $(CFLAGS) $(1) -o /dev/null -xc - \
6 6
		> /dev/null 2>&1; then echo "$(1)"; fi ;)

Also available in: Unified diff