]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - streamer.c
Proper CPU contracts in streamer
[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 frsh_rtp_budget, frsh_rtp_period_ms, frsh_rtp_deadline_ms;
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 = 320;
39 static int height = 240;
40 static int bitrate = 1000000;
41 int fps = 30;
42 static const char *impform = "video4linux2";
43 AVFormatContext *s, *os;
44
45 static void sdp_print(AVFormatContext *s, const char *fname)
46 {
47     char sdp[2048];
48     FILE *f;
49
50     f = fopen(fname, "w");
51     avf_sdp_create(&s, 1, sdp, sizeof(sdp));
52     fprintf(f, "%s\n", sdp);
53     fclose(f);
54 }
55
56 static void
57 usage(void)
58 {
59         printf("usage: streamer [ options ]\n");
60         printf("  -w <number>    send image width\n");
61         printf("  -h <number>    send image height\n");
62         printf("  -r <number>    refresh rate\n");
63         printf("  -r <path>      video device [%s]\n", vdev);
64         printf("  -m <addr>      destination IP address\n");
65         printf("  -i <string>    input video device format [%s]\n", impform);
66         printf("  -p <port>      destination port [%d]\n", dport);
67         printf("  -b <bitrate>   bitrate in b/s [%d]\n", bitrate);
68         printf("  -s <sdp_file>  name of output sdp file [%s]\n", sdp_file);
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:s:")) >= 0) {
79     switch (v) {
80       case 's':
81         sdp_file = optarg;
82         break;
83       case 'p':
84         dport = atoi(optarg);
85         break;
86       case 'w':
87         width = atoi(optarg);
88         break;
89       case 'h':
90         height = atoi(optarg);
91         break;
92       case 'r':
93         fps = atoi(optarg);
94         break;
95       case 'b':
96         bitrate = atoi(optarg);
97         break;
98       case 'd':
99         vdev = optarg;
100         break;
101       case 'm':
102         dst = optarg;
103         break;
104       case 'i':
105         impform = optarg;
106         if(!strcmp(impform, "v4l"))
107           impform = "video4linux";
108         else if(!strcmp(impform, "v4l2"))
109           impform = "video4linux2";
110         break;
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   static unsigned max_size = 0, min_size = -1;
163   static double avg_size = 0;
164   clock_gettime(CLOCK_MONOTONIC, &start);
165   start.tv_sec--;               /* Avoid division by zero */
166   done = 0;
167   while (!(done = streamer_run_done_rq)) {
168     AVPacket *pkt;
169     pkt = read_input_packet(s);
170
171     clock_gettime(CLOCK_MONOTONIC, &end);
172     timespec_subtract(&d, &end, &start);
173     start = end;
174     int fps_now = (1000<<16)/(d.tv_sec*1000+d.tv_nsec/1000000);
175     fps_avg += (fps_now - fps_avg) >> 4;
176     
177     if (pkt == NULL) {
178       done = 1;
179     } else {
180       AVFrame *f;
181       AVPacket *opkt;
182
183       pkt->pts += s->streams[pkt->stream_index]->start_time;
184       //rt_job_start(pkt->pts);
185       f = pkt_decode(s, pkt);
186       if (f) {
187         int keyframe;
188         opkt = pkt_encode(os, f);
189         keyframe = os->streams[0]->codec->coded_frame->key_frame;
190         if (opkt) {
191           pkt_send(os, opkt);
192
193           if (opkt->size > max_size)
194                   max_size = opkt->size;
195           if (opkt->size < min_size)
196                   min_size = opkt->size;
197           avg_size = avg_size*frame/(frame+1) + (double)opkt->size/(frame+1);
198           printf("%5d%c: %2d (%4.1f) fps  opkt size: %5d b  max=%5u b min=%5u b avg=%5.0f\n",
199                  frame, keyframe ? '*':' ',
200                  fps_avg>>16, 1000.0/(d.tv_sec*1000+d.tv_nsec/1000000),
201                  opkt->size, max_size, min_size, avg_size);
202           if (frame % 100 == 0) max_size=0;
203           frame++;// = (frame + 1) % fps;
204         }
205       }
206       //rt_job_end();
207       av_free_packet(pkt);
208     }
209   }
210
211   return NULL;
212 }
213
214 void wait_for_ending_command(void) {
215   sigset_t sigset;
216   sigemptyset(&sigset);
217   sigaddset(&sigset, SIGINT);
218   sigaddset(&sigset, SIGTERM);
219   sigwaitinfo(&sigset, NULL);
220 }
221 static void block_signals(void) {
222   sigset_t sigset;
223   sigemptyset(&sigset);
224   sigaddset(&sigset, SIGINT);
225   sigaddset(&sigset, SIGTERM);
226   sigprocmask(SIG_BLOCK,&sigset,NULL);
227   pthread_sigmask(SIG_BLOCK,&sigset,NULL);
228 }
229
230
231 int main(int argc, char *argv[])
232 {
233
234   //long int cpu_budget, cpu_period;
235   int ret;
236
237   block_signals();
238
239 #ifdef CONFIG_FFMPEG_WITH_FRSH
240   ret = frsh_init();
241   if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
242
243   /* fill default network contract params */
244   frsh_rtp_budget = 100*bitrate/8/100; 
245   frsh_rtp_period_ms = 1000;
246   frsh_rtp_deadline_ms = 1000/fps;
247 #endif /*CONFIG_FFMPEG_WITH_FRSH*/
248
249   avcodec_register_all();
250   av_register_all();
251   avdevice_register_all();
252
253   args_parse(argc, argv);
254
255   s = open_input_stream(vdev, width, height, fps, impform);
256   if (s == NULL) {
257     fprintf(stderr, "Cannot open input file %s\n", vdev);
258
259     return -1;
260   }
261   codec_open(s);
262   os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO, fps);
263   if (os == NULL) {
264     fprintf(stderr, "Cannot open output stream\n");
265
266     return -1;
267   }
268   os->streams[0]->codec->width = s->streams[0]->codec->width;
269   os->streams[0]->codec->height = s->streams[0]->codec->height;
270   os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
271   os->streams[0]->codec->bit_rate = bitrate;
272   codec_connect(s->streams[0]->codec, os->streams[0]->codec);
273   out_codec_open(os);
274   dump_format(os, 0, os->filename, 1);
275   sdp_print(os, sdp_file);
276
277 #if CONFIG_FFMPEG_WITH_FRSH
278   frsh_thread_attr_t frsh_attr;
279   frsh_thread_id_t thread;
280   frsh_vres_id_t cpu_vres;
281   frsh_contract_t cpu_contract;
282   frsh_rel_time_t cpu_budget, cpu_period;
283  
284   cpu_budget = fosa_msec_to_rel_time(5);
285   cpu_period = fosa_msec_to_rel_time(1000/fps);
286   /* Contract negotiation for CPU */
287   ret = frsh_contract_init(&cpu_contract);
288   if (ret) PERROR_AND_EXIT(ret, "CPU:frsh_contract_init");
289  
290   ret = frsh_contract_set_basic_params(&cpu_contract,
291                                              &cpu_budget,
292                                              &cpu_period,
293                                              FRSH_WT_BOUNDED,
294                                              FRSH_CT_REGULAR);
295         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
296         ret = frsh_contract_set_resource_and_label(&cpu_contract, 
297                         FRSH_RT_PROCESSOR, FRSH_CPU_ID_DEFAULT, "camera_ctrl");
298         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
299
300         ret = frsh_contract_negotiate(&cpu_contract, &cpu_vres);
301         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
302         printf("Aqcpu vres negotiated\n");
303
304         pthread_attr_init(&frsh_attr);
305         ret = frsh_thread_create_and_bind(cpu_vres, &thread, &frsh_attr, 
306                                 streamer_run, (void*) NULL);
307         if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
308
309         wait_for_ending_command();
310
311         streamer_run_done_rq = 1;
312         
313         pthread_join(thread.pthread_id, (void**) NULL); 
314
315         printf("Ending contracts\n");
316
317         ret = frsh_contract_cancel(cpu_vres);
318         if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
319
320         printf("Finishing\n");
321
322         close_output_stream(os);
323 #else
324         pthread_attr_t attr;
325         pthread_t streamer_th;
326
327         pthread_attr_init(&attr);
328
329         ret = pthread_create(&streamer_th, &attr, streamer_run, (void*) NULL);
330         if (ret) 
331                         printf("Failed to create streamer thread\n.");
332
333         wait_for_ending_command();
334
335         streamer_run_done_rq = 1;
336
337         pthread_join(streamer_th, (void**) NULL);
338
339         printf("Finishing\n");
340
341         close_output_stream(os);
342 #endif
343
344   return 0;
345 }
346