]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - streamer.c
bb7f091bf0c881c0fa401b58018d375aad83d7d2
[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 #include <signal.h>
9 #include <pthread.h>
10
11 #include <libavformat/avformat.h>
12 #include <libavdevice/avdevice.h>
13 #include <libswscale/swscale.h>
14
15 #include "input.h"
16 #include "output.h"
17 #include "codec.h"
18 #include "rt.h"
19
20 #include "streamer_config.h"
21
22 #ifdef CONFIG_OC_ULUT
23 #include <ul_log.h>
24 #include <ul_logreg.h>
25 #endif
26
27 #ifdef CONFIG_FFMPEG_WITH_FRSH
28 #include <frsh.h>
29
30 /*temporrary solution to pass network parameters */
31 extern long int udp_budget, udp_period;
32 #endif /*CONFIG_STREAMER_WITH_FRSH*/
33
34 static const char *sdp_file = "sdp.txt";
35 static const char *vdev = "/dev/video0";
36 static const char *dst = "127.0.0.1";
37 static int dport = 20000;
38 static int width = 352;
39 static int height = 288;
40 int fps = 25;
41 static const char *impform = "video4linux2";
42 AVFormatContext *s, *os;
43
44 static void sdp_print(AVFormatContext *s, const char *fname)
45 {
46     char sdp[2048];
47     FILE *f;
48
49     f = fopen(fname, "w");
50     avf_sdp_create(&s, 1, sdp, sizeof(sdp));
51     fprintf(f, "%s\n", sdp);
52     fclose(f);
53 }
54
55 static void
56 usage(void)
57 {
58         printf("usage: streamer [ options ]\n");
59         printf("  -w <number>    send image width\n");
60         printf("  -h <number>    send image height\n");
61         printf("  -r <number>    refresh rate\n");
62         printf("  -r <path>      video device [%s]\n", vdev);
63         printf("  -m <addr>      destination IP address\n");
64         printf("  -i <string>    input video device format [%s]\n", impform);
65       #ifdef CONFIG_STREAMER_WITH_FRSH
66         printf("  -b <number>    network budget\n");
67         printf("  -p <number>    network period for given budget\n");
68       #endif /*CONFIG_STREAMER_WITH_FRSH*/
69       #ifdef CONFIG_OC_ULUT
70         printf("  -l <number>|<domain>=<number>,...\n");
71       #endif /*CONFIG_OC_ULUT*/
72 }
73
74 static int args_parse(int argc, char *argv[])
75 {
76   int v;
77
78   while ((v = getopt(argc, argv, "w:h:r:d:m:i:l:b:p:")) >= 0) {
79     switch (v) {
80       case 'w':
81         width = atoi(optarg);
82         break;
83       case 'h':
84         height = atoi(optarg);
85         break;
86       case 'r':
87         fps = atoi(optarg);
88         break;
89       case 'd':
90         vdev = optarg;
91         break;
92       case 'm':
93         dst = optarg;
94         break;
95       case 'i':
96         impform = optarg;
97         if(!strcmp(impform, "v4l"))
98           impform = "video4linux";
99         else if(!strcmp(impform, "v4l2"))
100           impform = "video4linux2";
101         break;
102       #ifdef CONFIG_STREAMER_WITH_FRSH
103       case 'b':
104         udp_budget = atol(optarg);
105         break;
106       case 'p':
107         udp_period = atol(optarg);
108         break;
109       #endif /*CONFIG_STREAMER_WITH_FRSH*/
110
111       #ifdef CONFIG_OC_ULUT
112       case 'l':
113         ul_log_domain_arg2levels(optarg);
114         break;
115       #endif /*CONFIG_OC_ULUT*/
116
117       default: /* unknown option */
118         fprintf(stderr, "%s: illegal option %c\n", argv[0], v);
119         usage();
120         exit(-1);
121     }
122   }
123
124   return 0;
125 }
126
127 int streamer_run_done_rq;
128
129 void* streamer_run(void* args)
130 {
131   int done;
132
133   done = 0;
134   while (!(done = streamer_run_done_rq)) {
135     AVPacket *pkt;
136     pkt = read_input_packet(s);
137     if (pkt == NULL) {
138       done = 1;
139     } else {
140       AVFrame *f;
141       AVPacket *opkt;
142
143       pkt->pts += s->streams[pkt->stream_index]->start_time;
144       //rt_job_start(pkt->pts);
145       f = pkt_decode(s, pkt);
146       if (f) {
147         opkt = pkt_encode(os, f);
148         if (opkt) {
149           pkt_send(os, opkt);
150           printf("opkt size: %d\n", opkt->size);
151         }
152       }
153       //rt_job_end();
154       av_free_packet(pkt);
155     }
156   }
157
158   return NULL;
159 }
160
161 void wait_for_ending_command(void) {
162   sigset_t sigset;
163   sigemptyset(&sigset);
164   sigaddset(&sigset, SIGINT);
165   sigaddset(&sigset, SIGTERM);
166   sigwaitinfo(&sigset, NULL);
167 }
168 static void block_signals(void) {
169   sigset_t sigset;
170   sigemptyset(&sigset);
171   sigaddset(&sigset, SIGINT);
172   sigaddset(&sigset, SIGTERM);
173   sigprocmask(SIG_BLOCK,&sigset,NULL);
174   pthread_sigmask(SIG_BLOCK,&sigset,NULL);
175 }
176
177
178 int main(int argc, char *argv[])
179 {
180
181   //long int cpu_budget, cpu_period;
182   int ret;
183
184   block_signals();
185
186 #ifdef CONFIG_FFMPEG_WITH_FRSH
187   ret = frsh_init();
188   if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
189
190   /* fill default network contract params */
191   udp_budget = 50000; 
192   udp_period = 500;
193 #endif /*CONFIG_FFMPEG_WITH_FRSH*/
194
195   avcodec_register_all();
196   av_register_all();
197   avdevice_register_all();
198
199   args_parse(argc, argv);
200
201   s = open_input_stream(vdev, width, height, fps, impform);
202   if (s == NULL) {
203     fprintf(stderr, "Cannot open input file %s\n", vdev);
204
205     return -1;
206   }
207   codec_open(s);
208   os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
209   if (os == NULL) {
210     fprintf(stderr, "Cannot open output stream\n");
211
212     return -1;
213   }
214   os->streams[0]->codec->width = s->streams[0]->codec->width;
215   os->streams[0]->codec->height = s->streams[0]->codec->height;
216   os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
217   os->streams[0]->codec->bit_rate = 300000;
218   codec_connect(s->streams[0]->codec, os->streams[0]->codec);
219   out_codec_open(os);
220   dump_format(os, 0, os->filename, 1);
221   sdp_print(os, sdp_file);
222
223 #if CONFIG_STREAMER_WITH_FRSH && CONFIG_AQUOSA 
224   frsh_thread_attr_t frsh_attr;
225   frsh_thread_id_t thread;
226   frsh_vres_id_t cpu_vres;
227   frsh_contract_t cpu_contract;
228   frsh_rel_time_t cpu_budget, cpu_period;
229  
230   cpu_budget = fosa_msec_to_rel_time(50);
231   cpu_period = fosa_msec_to_rel_time(100);
232   /* Contract negotiation for CPU */
233   ret = frsh_contract_init(&cpu_contract);
234   if (ret) PERROR_AND_EXIT(ret, "CPU:frsh_contract_init");
235  
236   ret = frsh_contract_set_basic_params(&cpu_contract,
237                                              &cpu_budget,
238                                              &cpu_period,
239                                              FRSH_WT_BOUNDED,
240                                              FRSH_CT_REGULAR);
241         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
242         ret = frsh_contract_set_resource_and_label(&cpu_contract, 
243                         FRSH_RT_PROCESSOR, FRSH_CPU_ID_DEFAULT, "aqcpu_cont");
244         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
245
246         ret = frsh_contract_negotiate(&cpu_contract, &cpu_vres);
247         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
248         printf("Aqcpu vres negotiated\n");
249
250         pthread_attr_init(&frsh_attr);
251         ret = frsh_thread_create_and_bind(cpu_vres, &thread, &frsh_attr, 
252                                 streamer_run, (void*) NULL);
253         if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
254
255         wait_for_ending_command();
256
257         streamer_run_done_rq = 1;
258         
259         pthread_join(thread.pthread_id, (void**) NULL); 
260
261         printf("Ending contracts\n");
262
263         ret = frsh_contract_cancel(cpu_vres);
264         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
265
266         printf("Finishing\n");
267
268         close_output_stream(os);
269 #else
270         pthread_attr_t attr;
271         pthread_t streamer_th;
272
273         pthread_attr_init(&attr);
274
275         ret = pthread_create(&streamer_th, &attr, streamer_run, (void*) NULL);
276         if (ret) 
277                         printf("Failed to create streamer thread\n.");
278
279         wait_for_ending_command();
280
281         streamer_run_done_rq = 1;
282
283         pthread_join(streamer_th, (void**) NULL);
284
285         printf("Finishing\n");
286
287         close_output_stream(os);
288 #endif
289
290   return 0;
291 }
292