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