Revision 46c24e94

View differences:

Chunkiser/input-stream-avs.c
5 5
 */
6 6

  
7 7
#include <libavformat/avformat.h>
8
#include <stdbool.h>
8 9

  
9 10
#include "../input-stream.h"
11
#include "../input.h"		//TODO: for flags. Check if we can do something smarter
10 12
#define STATIC_BUFF_SIZE 1000 * 1024
11 13
#define HEADER_REFRESH_PERIOD 50
12 14

  
13 15
struct input_stream {
14 16
  AVFormatContext *s;
17
  bool loop;	//loop on input file infinitely
15 18
  int audio_stream;
16 19
  int video_stream;
17 20
  int64_t last_ts;
......
19 22
  int frames_since_global_headers;
20 23
};
21 24

  
22
struct input_stream *input_stream_open(const char *fname, int *period)
25
struct input_stream *input_stream_open(const char *fname, int *period, uint16_t flags)
23 26
{
24 27
  struct input_stream *desc;
25 28
  int i, res;
......
49 52
  desc->last_ts = 0;
50 53
  desc->base_ts = 0;
51 54
  desc->frames_since_global_headers = 0;
55
  desc->loop = flags & INPUT_LOOP;
52 56
  for (i = 0; i < desc->s->nb_streams; i++) {
53 57
    if (desc->video_stream == -1 && desc->s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
54 58
      desc->video_stream = i;
......
162 166

  
163 167
    res = av_read_frame(s->s, &pkt);
164 168
    if (res < 0) {
165
      if (input_stream_rewind(s) < 0) {
166
        fprintf(stderr, "AVPacket read failed: %d!!!\n", res);
167
        *size = -1;
169
      if (s->loop) {
170
        if (input_stream_rewind(s) >= 0) {
171
          *size = 0;
172
          *ts = s->last_ts;
168 173

  
169
        return NULL;
174
          return NULL;
175
        }
170 176
      }
171
      *size = 0;
172
      *ts = s->last_ts;
177
      fprintf(stderr, "AVPacket read failed: %d!!!\n", res);
178
      *size = -1;
173 179

  
174 180
      return NULL;
175 181
    }
dumbstreamer.c
7 7
#include <stdlib.h>
8 8
#include <stdint.h>
9 9
#include <stdio.h>
10
#include <stdbool.h>
10 11
#include <string.h>
11 12
#include <getopt.h>
12 13

  
......
25 26
static int multiply = 1;
26 27
static int buff_size = 8;
27 28
static const char *fname = "input.mpg";
29
static bool loop_input = false;
28 30

  
29 31
static void cmdline_parse(int argc, char *argv[])
30 32
{
31 33
  int o;
32 34

  
33
  while ((o = getopt(argc, argv, "b:c:t:p:i:P:I:f:m:")) != -1) {
35
  while ((o = getopt(argc, argv, "b:c:t:p:i:P:I:f:m:l")) != -1) {
34 36
    switch(o) {
35 37
      case 'b':
36 38
        buff_size = atoi(optarg);
......
59 61
      case 'f':
60 62
        fname = strdup(optarg);
61 63
        break;
64
      case 'l':
65
        loop_input = true;
66
        break;
62 67
      default:
63 68
        fprintf(stderr, "Error: unknown option %c\n", o);
64 69

  
......
116 121
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
117 122
  }
118 123

  
119
  source_loop(fname, my_sock, period * 1000, multiply);
124
  source_loop(fname, my_sock, period * 1000, multiply, loop_input);
120 125

  
121 126
  return 0;
122 127
}
input-stream-dummy.c
7 7
static struct input_stream {
8 8
} fake_descriptor;
9 9

  
10
struct input_stream *input_stream_open(const char *fname, int *period)
10
struct input_stream *input_stream_open(const char *fname, int *period, uint16_t flags)
11 11
{
12 12
  *period = 40000;
13 13
  return &fake_descriptor;
input-stream.h
3 3

  
4 4
struct input_stream;
5 5

  
6
struct input_stream *input_stream_open(const char *fname, int *period);
6
struct input_stream *input_stream_open(const char *fname, int *period, uint16_t flags);
7 7
void input_stream_close(struct input_stream *dummy);
8 8
uint8_t *chunkise(struct input_stream *dummy, int id, int *size, uint64_t *ts);
9 9

  
input.c
18 18
  uint64_t first_ts;
19 19
};
20 20

  
21
struct input_desc *input_open(const char *fname)
21
struct input_desc *input_open(const char *fname, uint16_t flags)
22 22
{
23 23
  struct input_desc *res;
24 24
  struct timeval tv;
......
31 31
  gettimeofday(&tv, NULL);
32 32
  res->start_time = tv.tv_usec + tv.tv_sec * 1000000ULL;
33 33
  res->first_ts = 0;
34
  res->s = input_stream_open(fname, &res->interframe);
34
  res->s = input_stream_open(fname, &res->interframe, flags);
35 35
  if (res->s == NULL) {
36 36
    free(res);
37 37
    res = NULL;
input.h
1 1
#ifndef INPUT_H
2 2
#define INPUT_H
3 3

  
4
#define INPUT_LOOP 0x0001
5

  
4 6
struct input_desc;
5 7

  
6
struct input_desc *input_open(const char *fname);
8
struct input_desc *input_open(const char *fname, uint16_t flags);
7 9
void input_close(struct input_desc *s);
8 10
int input_get(struct input_desc *s, struct chunk *c);
9 11

  
loop.c
10 10
#include <stdint.h>
11 11
#include <stdlib.h>
12 12
#include <stdio.h>
13
#include <stdbool.h>
13 14

  
14 15
#include <net_helper.h>
15 16
#include <topmanager.h>
......
86 87
  }
87 88
}
88 89

  
89
void source_loop(const char *fname, struct nodeID *s, int csize, int chunks)
90
void source_loop(const char *fname, struct nodeID *s, int csize, int chunks, bool loop)
90 91
{
91 92
  int done = 0;
92 93
  static uint8_t buff[BUFFSIZE];
93 94
  int cnt = 0;
94 95

  
95
  source_init(fname, s);
96
  source_init(fname, s, loop);
96 97
  while (!done) {
97 98
    int len, res;
98 99
    struct timeval tv;
loop.h
2 2
#define LOOP_H
3 3

  
4 4
void loop(struct nodeID *s, int period, int buff_size);
5
void source_loop(const char *fname, struct nodeID *s, int csize, int chunks);
5
void source_loop(const char *fname, struct nodeID *s, int csize, int chunks, bool loop);
6 6

  
7 7
#endif	/* LOOP_H */
streaming.c
7 7
#include <stdlib.h>
8 8
#include <stdio.h>
9 9
#include <stdint.h>
10
#include <stdbool.h>
10 11

  
11 12
#include <net_helper.h>
12 13
#include <chunk.h> 
......
31 32
  chunkInit(myID);
32 33
}
33 34

  
34
int source_init(const char *fname, struct nodeID *myID)
35
int source_init(const char *fname, struct nodeID *myID, bool loop)
35 36
{
36
  input = input_open(fname);
37
  input = input_open(fname, loop ? INPUT_LOOP : 0);
37 38
  if (input == NULL) {
38 39
    return -1;
39 40
  }
streaming.h
1 1
#ifndef STREAMING_H
2 2
#define STREAMING_H
3 3

  
4
#include <stdbool.h>
5

  
4 6
void stream_init(int size, struct nodeID *myID);
5
int source_init(const char *fname, struct nodeID *myID);
7
int source_init(const char *fname, struct nodeID *myID, bool loop);
6 8
void received_chunk(const uint8_t *buff, int len);
7 9
void send_chunk(const struct nodeID **neighbours, int n);
8 10
int generated_chunk(suseconds_t *delta);

Also available in: Unified diff