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