Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.49 KB)

1 91e9c5d5 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
7 df7dffd7 Luca Abeni
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10 91e9c5d5 Luca Abeni
#include <unistd.h>
11
#include <stdint.h>
12
#include <stdlib.h>
13 6b0140b8 Luca Abeni
#include <string.h>
14 91e9c5d5 Luca Abeni
#include <stdio.h>
15
16 6b0140b8 Luca Abeni
#include "payload.h"
17
#include "config.h"
18 91e9c5d5 Luca Abeni
#include "dechunkiser_iface.h"
19
20 8b70c289 Luca Abeni
struct dechunkiser_ctx {
21 91e9c5d5 Luca Abeni
  int fd;
22 6b0140b8 Luca Abeni
  int payload_type;
23 91e9c5d5 Luca Abeni
};
24
25 8b70c289 Luca Abeni
static struct dechunkiser_ctx *raw_open(const char *fname, const char *config)
26 91e9c5d5 Luca Abeni
{
27 8b70c289 Luca Abeni
  struct dechunkiser_ctx *res;
28 6b0140b8 Luca Abeni
  struct tag *cfg_tags;
29 91e9c5d5 Luca Abeni
30 8b70c289 Luca Abeni
  res = malloc(sizeof(struct dechunkiser_ctx));
31 91e9c5d5 Luca Abeni
  if (res == NULL) {
32
    return NULL;
33
  }
34
  res->fd = 1;
35 6b0140b8 Luca Abeni
  res->payload_type = 0;
36 e296176d Luca Abeni
  if (fname) {
37
    res->fd = open(fname, O_WRONLY | O_CREAT, S_IROTH | S_IWUSR | S_IRUSR);
38 df7dffd7 Luca Abeni
    if (res->fd < 0) {
39
      res->fd = 1;
40
    }
41
  }
42 6b0140b8 Luca Abeni
  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 1c6858bc Luca Abeni
      if (!strcmp(pt, "avf")) {
49 6b0140b8 Luca Abeni
        res->payload_type = 1;
50
      }
51
    }
52
  }
53 dcc42706 Luca Abeni
  free(cfg_tags);
54 91e9c5d5 Luca Abeni
55
  return res;
56
}
57
58 8b70c289 Luca Abeni
static void raw_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
59 91e9c5d5 Luca Abeni
{
60 6b0140b8 Luca Abeni
  int offset;
61
62
  if (o->payload_type == 1) {
63 28e6b134 Luca Abeni
    int header_size;
64
    int frames;
65 6b0140b8 Luca Abeni
    int i;
66
    uint8_t codec;
67
68 28e6b134 Luca Abeni
    if (data[0] == 0) {
69
      fprintf(stderr, "Error! Strange chunk: %x!!!\n", codec);
70 6b0140b8 Luca Abeni
      return;
71 28e6b134 Luca Abeni
    } 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 6b0140b8 Luca Abeni
//    dprintf("Frame size: %dx%d -- Frame rate: %d / %d\n", width, height, frame_rate_n, frame_rate_d);
77 28e6b134 Luca Abeni
    } else {
78 64772949 Luca Abeni
      uint8_t channels;
79
      int sample_rate, frame_size;
80
81 28e6b134 Luca Abeni
      header_size = AUDIO_PAYLOAD_HEADER_SIZE;
82 64772949 Luca Abeni
      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 28e6b134 Luca Abeni
    }
85
86
    frames = data[header_size - 1];
87 6b0140b8 Luca Abeni
    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 91e9c5d5 Luca Abeni
}
101
102 8b70c289 Luca Abeni
static void raw_close(struct dechunkiser_ctx *s)
103 386bce8c Luca Abeni
{
104
  close(s->fd);
105
  free(s);
106
}
107
108 91e9c5d5 Luca Abeni
struct dechunkiser_iface out_raw = {
109
  .open = raw_open,
110
  .write = raw_write,
111 386bce8c Luca Abeni
  .close = raw_close,
112 91e9c5d5 Luca Abeni
};