]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - streamer.c
Export fps for rt_qosmgr.c (so that it can correctly set server period, etc...)
[frescor/streamer.git] / streamer.c
1 #include <unistd.h>
2 #include <stdlib.h>
3
4 #include "libavformat/avformat.h"
5 #include "libavdevice/avdevice.h"
6
7 #include "input.h"
8 #include "output.h"
9 #include "codec.h"
10 #include "rt.h"
11
12 static const char *sdp_file = "sdp.txt";
13 static const char *vdev = "/dev/video0";
14 static const char *dst = "224.10.20.30";
15 static int dport = 20000;
16 static int width = 352;
17 static int height = 288;
18 int fps = 25;
19
20 static void sdp_print(AVFormatContext *s, const char *fname)
21 {
22     char sdp[2048];
23     FILE *f;
24
25     f = fopen(fname, "w");
26     avf_sdp_create(&s, 1, sdp, sizeof(sdp));
27     fprintf(f, "%s\n", sdp);
28     fclose(f);
29 }
30
31 static int args_parse(int argc, char *argv[])
32 {
33   int v;
34
35   while ((v = getopt(argc, argv, "w:h:r:d:")) >= 0) {
36     switch (v) {
37       case 'w':
38         width = atoi(optarg);
39         break;
40       case 'h':
41         height = atoi(optarg);
42         break;
43       case 'r':
44         fps = atoi(optarg);
45         break;
46       case 'd':
47         vdev = optarg;
48         break;
49       case 'm':
50         dst = optarg;
51         break;
52       default: /* ’?’ */
53         fprintf(stderr, "%s: illegal option %c\n", argv[0], v);
54         exit(-1);
55     }
56   }
57
58   return 0;
59 }
60
61 int main(int argc, char *argv[])
62 {
63   AVFormatContext *s, *os;
64   int done;
65
66   avcodec_register_all();
67   av_register_all();
68   avdevice_register_all();
69
70   args_parse(argc, argv);
71
72   s = open_input_stream(vdev, width, height, fps);
73   if (s == NULL) {
74     fprintf(stderr, "Cannot open input file %s\n", vdev);
75
76     return -1;
77   }
78   codec_open(s);
79   os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
80   if (os == NULL) {
81     fprintf(stderr, "Cannot open output stream\n");
82
83     return -1;
84   }
85   os->streams[0]->codec->width = s->streams[0]->codec->width;
86   os->streams[0]->codec->height = s->streams[0]->codec->height;
87   os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
88   codec_connect(s->streams[0]->codec, os->streams[0]->codec);
89   out_codec_open(os);
90   dump_format(os, 0, os->filename, 1);
91   sdp_print(os, sdp_file);
92   done = 0;
93   while (!done) {
94     AVPacket *pkt;
95     pkt = read_input_packet(s);
96     if (pkt == NULL) {
97       done = 1;
98     } else {
99       AVFrame *f;
100       AVPacket *opkt;
101
102       pkt->pts += s->streams[pkt->stream_index]->start_time;
103       rt_job_start(pkt->pts);
104       f = pkt_decode(s, pkt);
105       if (f) {
106         opkt = pkt_encode(os, f);
107         if (opkt) {
108           pkt_send(os, opkt);
109         }
110       }
111       rt_job_end();
112       av_free_packet(pkt);
113     }
114   }
115
116   return 0;
117 }
118