]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - streamer.c
Added parsing of loglevels and options usage text.
[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_OC_ULUT
21 #include <ul_log.h>
22 #include <ul_logreg.h>
23 #endif
24
25 #ifdef CONFIG_STREAMER_WITH_FRSH
26 #include <frsh.h>
27 #endif
28
29 extern long int udp_budget, udp_period;
30
31 static const char *sdp_file = "sdp.txt";
32 static const char *vdev = "/dev/video0";
33 static const char *dst = "127.0.0.1";
34 static int dport = 20000;
35 static int width = 352;
36 static int height = 288;
37 int fps = 25;
38 static const char *impform = "video4linux2";
39
40 static void sdp_print(AVFormatContext *s, const char *fname)
41 {
42     char sdp[2048];
43     FILE *f;
44
45     f = fopen(fname, "w");
46     avf_sdp_create(&s, 1, sdp, sizeof(sdp));
47     fprintf(f, "%s\n", sdp);
48     fclose(f);
49 }
50
51 static void
52 usage(void)
53 {
54         printf("usage: streamer [ options ]\n");
55         printf("  -w <number>    send image width\n");
56         printf("  -h <number>    send image height\n");
57         printf("  -r <number>    refresh rate\n");
58         printf("  -r <path>      video device [%s]\n", vdev);
59         printf("  -m <addr>      destination IP address\n");
60         printf("  -i <string>    input video device format [%s]\n", impform);
61       #ifdef CONFIG_OC_ULUT
62         printf("  -l <number>|<domain>=<number>,...\n");
63       #endif /*CONFIG_OC_ULUT*/
64 }
65
66 static int args_parse(int argc, char *argv[])
67 {
68   int v;
69
70   while ((v = getopt(argc, argv, "w:h:r:d:m:i:l:")) >= 0) {
71     switch (v) {
72       case 'w':
73         width = atoi(optarg);
74         break;
75       case 'h':
76         height = atoi(optarg);
77         break;
78       case 'r':
79         fps = atoi(optarg);
80         break;
81       case 'd':
82         vdev = optarg;
83         break;
84       case 'm':
85         dst = optarg;
86         break;
87       case 'i':
88         impform = optarg;
89         if(!strcmp(impform, "v4l"))
90           impform = "video4linux";
91         else if(!strcmp(impform, "v4l2"))
92           impform = "video4linux2";
93         break;
94       #ifdef CONFIG_OC_ULUT
95       case 'l':
96         ul_log_domain_arg2levels(optarg);
97         break;
98       #endif /*CONFIG_OC_ULUT*/
99
100       default: /* unknown option */
101         fprintf(stderr, "%s: illegal option %c\n", argv[0], v);
102         usage();
103         exit(-1);
104     }
105   }
106
107   return 0;
108 }
109
110 int main(int argc, char *argv[])
111 {
112   AVFormatContext *s, *os;
113   int done, ret;
114
115 #ifdef CONFIG_STREAMER_WITH_FRSH
116   ret = frsh_init();
117   if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
118 #endif /*CONFIG_STREAMER_WITH_FRSH*/
119
120   udp_budget = 50000; 
121   udp_period = 500;
122   
123   avcodec_register_all();
124   av_register_all();
125   avdevice_register_all();
126
127   args_parse(argc, argv);
128
129   s = open_input_stream(vdev, width, height, fps, impform);
130   if (s == NULL) {
131     fprintf(stderr, "Cannot open input file %s\n", vdev);
132
133     return -1;
134   }
135   codec_open(s);
136   os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
137   if (os == NULL) {
138     fprintf(stderr, "Cannot open output stream\n");
139
140     return -1;
141   }
142   os->streams[0]->codec->width = s->streams[0]->codec->width;
143   os->streams[0]->codec->height = s->streams[0]->codec->height;
144   os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
145   codec_connect(s->streams[0]->codec, os->streams[0]->codec);
146   out_codec_open(os);
147   dump_format(os, 0, os->filename, 1);
148   sdp_print(os, sdp_file);
149   done = 0;
150   while (!done) {
151     AVPacket *pkt;
152     pkt = read_input_packet(s);
153     if (pkt == NULL) {
154       done = 1;
155     } else {
156       AVFrame *f;
157       AVPacket *opkt;
158
159       pkt->pts += s->streams[pkt->stream_index]->start_time;
160       //rt_job_start(pkt->pts);
161       f = pkt_decode(s, pkt);
162       if (f) {
163         opkt = pkt_encode(os, f);
164         if (opkt) {
165           pkt_send(os, opkt);
166         }
167       }
168       //rt_job_end();
169       av_free_packet(pkt);
170     }
171   }
172
173   return 0;
174 }
175