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