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