]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - streamer.c
635593f0aea8ed0fafab9b044611bd3e28291c73
[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 avformat_frsh_budget, avformat_frsh_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 = 640;
39 static int height = 480;
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 int
130 timespec_subtract (struct timespec *result,
131                    struct timespec *x,
132                    struct timespec *y)
133 {
134   /* Perform the carry for the later subtraction by updating Y. */
135   if (x->tv_nsec < y->tv_nsec) {
136     int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
137     y->tv_nsec -= 1000000000 * num_sec;
138     y->tv_sec += num_sec;
139   }
140   if (x->tv_nsec - y->tv_nsec > 1000000000) {
141     int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000;
142     y->tv_nsec += 1000000000 * num_sec;
143     y->tv_sec -= num_sec;
144   }
145
146   /* Compute the time remaining to wait.
147      `tv_nsec' is certainly positive. */
148   result->tv_sec = x->tv_sec - y->tv_sec;
149   result->tv_nsec = x->tv_nsec - y->tv_nsec;
150
151   /* Return 1 if result is negative. */
152   return x->tv_sec < y->tv_sec;
153 }
154
155
156 void* streamer_run(void* args)
157 {
158   int done;
159   int frame = 0;
160   int fps_avg = 0;
161   struct timespec start, end, d;
162   clock_gettime(CLOCK_MONOTONIC, &start);
163   done = 0;
164   while (!(done = streamer_run_done_rq)) {
165     AVPacket *pkt;
166     pkt = read_input_packet(s);
167     if (pkt == NULL) {
168       done = 1;
169     } else {
170       AVFrame *f;
171       AVPacket *opkt;
172
173       pkt->pts += s->streams[pkt->stream_index]->start_time;
174       //rt_job_start(pkt->pts);
175       f = pkt_decode(s, pkt);
176       if (f) {
177         opkt = pkt_encode(os, f);
178         if (opkt) {
179           pkt_send(os, opkt);
180
181           clock_gettime(CLOCK_MONOTONIC, &end);
182           timespec_subtract(&d, &end, &start);
183           int fps_now = (1000<<8)/(d.tv_sec*1000+d.tv_nsec/1000000);
184           start = end;
185           fps_avg += (fps_now - fps_avg) >> 3;
186         
187           printf("%5d: %d fps  opkt size: %d\n",
188                  frame,
189                  fps_avg>>8,
190                  opkt->size);
191           frame++;// = (frame + 1) % fps;
192         }
193       }
194       //rt_job_end();
195       av_free_packet(pkt);
196     }
197   }
198
199   return NULL;
200 }
201
202 void wait_for_ending_command(void) {
203   sigset_t sigset;
204   sigemptyset(&sigset);
205   sigaddset(&sigset, SIGINT);
206   sigaddset(&sigset, SIGTERM);
207   sigwaitinfo(&sigset, NULL);
208 }
209 static void block_signals(void) {
210   sigset_t sigset;
211   sigemptyset(&sigset);
212   sigaddset(&sigset, SIGINT);
213   sigaddset(&sigset, SIGTERM);
214   sigprocmask(SIG_BLOCK,&sigset,NULL);
215   pthread_sigmask(SIG_BLOCK,&sigset,NULL);
216 }
217
218
219 int main(int argc, char *argv[])
220 {
221
222   //long int cpu_budget, cpu_period;
223   int ret;
224
225   block_signals();
226
227 #ifdef CONFIG_FFMPEG_WITH_FRSH
228   ret = frsh_init();
229   if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
230
231   /* fill default network contract params */
232   avformat_frsh_budget = 50000; 
233   avformat_frsh_period = 500;
234 #endif /*CONFIG_FFMPEG_WITH_FRSH*/
235
236   avcodec_register_all();
237   av_register_all();
238   avdevice_register_all();
239
240   args_parse(argc, argv);
241
242   s = open_input_stream(vdev, width, height, fps, impform);
243   if (s == NULL) {
244     fprintf(stderr, "Cannot open input file %s\n", vdev);
245
246     return -1;
247   }
248   codec_open(s);
249   os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
250   if (os == NULL) {
251     fprintf(stderr, "Cannot open output stream\n");
252
253     return -1;
254   }
255   os->streams[0]->codec->width = s->streams[0]->codec->width;
256   os->streams[0]->codec->height = s->streams[0]->codec->height;
257   os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
258   os->streams[0]->codec->bit_rate = 1000000;
259   codec_connect(s->streams[0]->codec, os->streams[0]->codec);
260   out_codec_open(os);
261   dump_format(os, 0, os->filename, 1);
262   sdp_print(os, sdp_file);
263
264 #if CONFIG_STREAMER_WITH_FRSH && CONFIG_AQUOSA 
265   frsh_thread_attr_t frsh_attr;
266   frsh_thread_id_t thread;
267   frsh_vres_id_t cpu_vres;
268   frsh_contract_t cpu_contract;
269   frsh_rel_time_t cpu_budget, cpu_period;
270  
271   cpu_budget = fosa_msec_to_rel_time(50);
272   cpu_period = fosa_msec_to_rel_time(100);
273   /* Contract negotiation for CPU */
274   ret = frsh_contract_init(&cpu_contract);
275   if (ret) PERROR_AND_EXIT(ret, "CPU:frsh_contract_init");
276  
277   ret = frsh_contract_set_basic_params(&cpu_contract,
278                                              &cpu_budget,
279                                              &cpu_period,
280                                              FRSH_WT_BOUNDED,
281                                              FRSH_CT_REGULAR);
282         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
283         ret = frsh_contract_set_resource_and_label(&cpu_contract, 
284                         FRSH_RT_PROCESSOR, FRSH_CPU_ID_DEFAULT, "aqcpu_cont");
285         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
286
287         ret = frsh_contract_negotiate(&cpu_contract, &cpu_vres);
288         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
289         printf("Aqcpu vres negotiated\n");
290
291         pthread_attr_init(&frsh_attr);
292         ret = frsh_thread_create_and_bind(cpu_vres, &thread, &frsh_attr, 
293                                 streamer_run, (void*) NULL);
294         if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
295
296         wait_for_ending_command();
297
298         streamer_run_done_rq = 1;
299         
300         pthread_join(thread.pthread_id, (void**) NULL); 
301
302         printf("Ending contracts\n");
303
304         ret = frsh_contract_cancel(cpu_vres);
305         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
306
307         printf("Finishing\n");
308
309         close_output_stream(os);
310 #else
311         pthread_attr_t attr;
312         pthread_t streamer_th;
313
314         pthread_attr_init(&attr);
315
316         ret = pthread_create(&streamer_th, &attr, streamer_run, (void*) NULL);
317         if (ret) 
318                         printf("Failed to create streamer thread\n.");
319
320         wait_for_ending_command();
321
322         streamer_run_done_rq = 1;
323
324         pthread_join(streamer_th, (void**) NULL);
325
326         printf("Finishing\n");
327
328         close_output_stream(os);
329 #endif
330
331   return 0;
332 }
333