Statistics
| Branch: | Revision:

grapes / src / Chunkiser / output-stream-raw.c @ 997c2b68

History | View | Annotate | Download (2.86 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 "int_coding.h"
17
#include "payload.h"
18
#include "config.h"
19
#include "dechunkiser_iface.h"
20

    
21
enum pt {
22
  raw,
23
  avf,
24
  udp,
25
  rtp,
26
};
27

    
28
struct dechunkiser_ctx {
29
  int fd;
30
  enum pt payload_type;
31
};
32

    
33
static struct dechunkiser_ctx *raw_open(const char *fname, const char *config)
34
{
35
  struct dechunkiser_ctx *res;
36
  struct tag *cfg_tags;
37

    
38
  res = malloc(sizeof(struct dechunkiser_ctx));
39
  if (res == NULL) {
40
    return NULL;
41
  }
42
  res->fd = 1;
43
  res->payload_type = raw;
44
  if (fname) {
45
    res->fd = open(fname, O_WRONLY | O_CREAT, S_IROTH | S_IWUSR | S_IRUSR);
46
    if (res->fd < 0) {
47
      res->fd = 1;
48
    }
49
  }
50
  cfg_tags = config_parse(config);
51
  if (cfg_tags) {
52
    const char *pt;
53

    
54
    pt = config_value_str(cfg_tags, "payload");
55
    if (pt) {
56
      if (!strcmp(pt, "avf")) {
57
        res->payload_type = avf;
58
      } else if (!strcmp(pt, "udp")) {
59
        res->payload_type = udp;
60
      } else if (!strcmp(pt, "rtp")) {
61
        res->payload_type = rtp;
62
      }
63
    }
64
  }
65
  free(cfg_tags);
66

    
67
  return res;
68
}
69

    
70
static void raw_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
71
{
72
  int offset;
73

    
74
  if (o->payload_type == avf) {
75
    int header_size;
76
    int frames;
77
    int i;
78
    uint8_t codec;
79

    
80
    if (data[0] == 0) {
81
      fprintf(stderr, "Error! Strange chunk: %x!!!\n", codec);
82
      return;
83
    } else if (data[0] < 127) {
84
      int width, height, frame_rate_n, frame_rate_d;
85

    
86
      header_size = VIDEO_PAYLOAD_HEADER_SIZE;
87
      video_payload_header_parse(data, &codec, &width, &height, &frame_rate_n, &frame_rate_d);
88
//    dprintf("Frame size: %dx%d -- Frame rate: %d / %d\n", width, height, frame_rate_n, frame_rate_d);
89
    } else {
90
      uint8_t channels;
91
      int sample_rate, frame_size;
92

    
93
      header_size = AUDIO_PAYLOAD_HEADER_SIZE;
94
      audio_payload_header_parse(data, &codec, &channels, &sample_rate, &frame_size);
95
//    dprintf("Frame size: %d Sample rate: %d Channels: %d\n", frame_size, sample_rate, channels);
96
    }
97

    
98
    frames = data[header_size - 1];
99
    for (i = 0; i < frames; i++) {
100
      int frame_size;
101
      int64_t pts, dts;
102

    
103
      frame_header_parse(data, &frame_size, &pts, &dts);
104
//      dprintf("Frame %d has size %d\n", i, frame_size);
105
    }
106
    offset = header_size + frames * FRAME_HEADER_SIZE;
107
  } else if (o->payload_type == udp) {
108
    offset = UDP_CHUNK_HEADER_SIZE; 
109
  } else if (o->payload_type == rtp) {
110
    offset = UDP_CHUNK_HEADER_SIZE + 12; 
111
  } else {
112
    offset = 0;
113
  }
114

    
115
  write(o->fd, data + offset, size - offset);
116
}
117

    
118
static void raw_close(struct dechunkiser_ctx *s)
119
{
120
  close(s->fd);
121
  free(s);
122
}
123

    
124
struct dechunkiser_iface out_raw = {
125
  .open = raw_open,
126
  .write = raw_write,
127
  .close = raw_close,
128
};