Statistics
| Branch: | Revision:

grapes / src / Chunkiser / output-stream-raw.c @ 4cdd16a7

History | View | Annotate | Download (2.49 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
#include <stdint.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <stdio.h>
15

    
16
#include "payload.h"
17
#include "config.h"
18
#include "dechunkiser_iface.h"
19

    
20
struct dechunkiser_ctx {
21
  int fd;
22
  int payload_type;
23
};
24

    
25
static struct dechunkiser_ctx *raw_open(const char *fname, const char *config)
26
{
27
  struct dechunkiser_ctx *res;
28
  struct tag *cfg_tags;
29

    
30
  res = malloc(sizeof(struct dechunkiser_ctx));
31
  if (res == NULL) {
32
    return NULL;
33
  }
34
  res->fd = 1;
35
  res->payload_type = 0;
36
  if (fname) {
37
    res->fd = open(fname, O_WRONLY | O_CREAT, S_IROTH | S_IWUSR | S_IRUSR);
38
    if (res->fd < 0) {
39
      res->fd = 1;
40
    }
41
  }
42
  cfg_tags = config_parse(config);
43
  if (cfg_tags) {
44
    const char *pt;
45

    
46
    pt = config_value_str(cfg_tags, "payload");
47
    if (pt) {
48
      if (!strcmp(pt, "avf")) {
49
        res->payload_type = 1;
50
      }
51
    }
52
  }
53
  free(cfg_tags);
54

    
55
  return res;
56
}
57

    
58
static void raw_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
59
{
60
  int offset;
61

    
62
  if (o->payload_type == 1) {
63
    int header_size;
64
    int frames;
65
    int i;
66
    uint8_t codec;
67

    
68
    if (data[0] == 0) {
69
      fprintf(stderr, "Error! Strange chunk: %x!!!\n", codec);
70
      return;
71
    } else if (data[0] < 127) {
72
      int width, height, frame_rate_n, frame_rate_d;
73

    
74
      header_size = VIDEO_PAYLOAD_HEADER_SIZE;
75
      video_payload_header_parse(data, &codec, &width, &height, &frame_rate_n, &frame_rate_d);
76
//    dprintf("Frame size: %dx%d -- Frame rate: %d / %d\n", width, height, frame_rate_n, frame_rate_d);
77
    } else {
78
      uint8_t channels;
79
      int sample_rate, frame_size;
80

    
81
      header_size = AUDIO_PAYLOAD_HEADER_SIZE;
82
      audio_payload_header_parse(data, &codec, &channels, &sample_rate, &frame_size);
83
//    dprintf("Frame size: %d Sample rate: %d Channels: %d\n", frame_size, sample_rate, channels);
84
    }
85

    
86
    frames = data[header_size - 1];
87
    for (i = 0; i < frames; i++) {
88
      int frame_size;
89
      int64_t pts, dts;
90

    
91
      frame_header_parse(data, &frame_size, &pts, &dts);
92
//      dprintf("Frame %d has size %d\n", i, frame_size);
93
    }
94
    offset = header_size + frames * FRAME_HEADER_SIZE;
95
  } else {
96
    offset = 0;
97
  }
98

    
99
  write(o->fd, data + offset, size - offset);
100
}
101

    
102
static void raw_close(struct dechunkiser_ctx *s)
103
{
104
  close(s->fd);
105
  free(s);
106
}
107

    
108
struct dechunkiser_iface out_raw = {
109
  .open = raw_open,
110
  .write = raw_write,
111
  .close = raw_close,
112
};