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