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