2 * Copyright (c) 2008 Luca Abeni
4 * This is free software; see GPL.txt
11 #include <libavformat/avformat.h>
12 #include <libavdevice/avdevice.h>
13 #include <libswscale/swscale.h>
20 #include "streamer_config.h"
24 #include <ul_logreg.h>
27 #ifdef CONFIG_FFMPEG_WITH_FRSH
30 /*temporrary solution to pass network parameters */
31 extern long int avformat_frsh_budget, avformat_frsh_period;
32 #endif /*CONFIG_STREAMER_WITH_FRSH*/
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;
41 static const char *impform = "video4linux2";
42 AVFormatContext *s, *os;
44 static void sdp_print(AVFormatContext *s, const char *fname)
49 f = fopen(fname, "w");
50 avf_sdp_create(&s, 1, sdp, sizeof(sdp));
51 fprintf(f, "%s\n", sdp);
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*/
70 printf(" -l <number>|<domain>=<number>,...\n");
71 #endif /*CONFIG_OC_ULUT*/
74 static int args_parse(int argc, char *argv[])
78 while ((v = getopt(argc, argv, "w:h:r:d:m:i:l:b:p:")) >= 0) {
84 height = atoi(optarg);
97 if(!strcmp(impform, "v4l"))
98 impform = "video4linux";
99 else if(!strcmp(impform, "v4l2"))
100 impform = "video4linux2";
102 #ifdef CONFIG_STREAMER_WITH_FRSH
104 udp_budget = atol(optarg);
107 udp_period = atol(optarg);
109 #endif /*CONFIG_STREAMER_WITH_FRSH*/
111 #ifdef CONFIG_OC_ULUT
113 ul_log_domain_arg2levels(optarg);
115 #endif /*CONFIG_OC_ULUT*/
117 default: /* unknown option */
118 fprintf(stderr, "%s: illegal option %c\n", argv[0], v);
127 int streamer_run_done_rq;
130 timespec_subtract (struct timespec *result,
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;
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;
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;
151 /* Return 1 if result is negative. */
152 return x->tv_sec < y->tv_sec;
156 void* streamer_run(void* args)
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);
166 while (!(done = streamer_run_done_rq)) {
168 pkt = read_input_packet(s);
175 pkt->pts += s->streams[pkt->stream_index]->start_time;
176 //rt_job_start(pkt->pts);
177 f = pkt_decode(s, pkt);
179 opkt = pkt_encode(os, f);
183 clock_gettime(CLOCK_MONOTONIC, &end);
184 timespec_subtract(&d, &end, &start);
185 int fps_now = (1000<<8)/(d.tv_sec*1000+d.tv_nsec/1000000);
187 fps_avg += (fps_now - fps_avg) >> 3;
189 if (opkt->size > max_size)
190 max_size = opkt->size;
191 if (opkt->size < min_size)
192 min_size = opkt->size;
193 avg_size = avg_size*frame/(frame+1) + (double)opkt->size/(frame+1);
194 printf("%5d: %2d fps opkt size: %5d b max=%5u b min=%5u b avg=%5.0f\n",
197 opkt->size, max_size, min_size, avg_size);
198 if (frame % 100 == 0) max_size=0;
199 frame++;// = (frame + 1) % fps;
210 void wait_for_ending_command(void) {
212 sigemptyset(&sigset);
213 sigaddset(&sigset, SIGINT);
214 sigaddset(&sigset, SIGTERM);
215 sigwaitinfo(&sigset, NULL);
217 static void block_signals(void) {
219 sigemptyset(&sigset);
220 sigaddset(&sigset, SIGINT);
221 sigaddset(&sigset, SIGTERM);
222 sigprocmask(SIG_BLOCK,&sigset,NULL);
223 pthread_sigmask(SIG_BLOCK,&sigset,NULL);
227 int main(int argc, char *argv[])
230 //long int cpu_budget, cpu_period;
235 #ifdef CONFIG_FFMPEG_WITH_FRSH
237 if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
239 /* fill default network contract params */
240 avformat_frsh_budget = 50000;
241 avformat_frsh_period = 500;
242 #endif /*CONFIG_FFMPEG_WITH_FRSH*/
244 avcodec_register_all();
246 avdevice_register_all();
248 args_parse(argc, argv);
250 s = open_input_stream(vdev, width, height, fps, impform);
252 fprintf(stderr, "Cannot open input file %s\n", vdev);
257 os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
259 fprintf(stderr, "Cannot open output stream\n");
263 os->streams[0]->codec->width = s->streams[0]->codec->width;
264 os->streams[0]->codec->height = s->streams[0]->codec->height;
265 os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
266 os->streams[0]->codec->bit_rate = 1000000;
267 codec_connect(s->streams[0]->codec, os->streams[0]->codec);
269 dump_format(os, 0, os->filename, 1);
270 sdp_print(os, sdp_file);
272 #if CONFIG_STREAMER_WITH_FRSH && CONFIG_AQUOSA
273 frsh_thread_attr_t frsh_attr;
274 frsh_thread_id_t thread;
275 frsh_vres_id_t cpu_vres;
276 frsh_contract_t cpu_contract;
277 frsh_rel_time_t cpu_budget, cpu_period;
279 cpu_budget = fosa_msec_to_rel_time(50);
280 cpu_period = fosa_msec_to_rel_time(100);
281 /* Contract negotiation for CPU */
282 ret = frsh_contract_init(&cpu_contract);
283 if (ret) PERROR_AND_EXIT(ret, "CPU:frsh_contract_init");
285 ret = frsh_contract_set_basic_params(&cpu_contract,
290 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
291 ret = frsh_contract_set_resource_and_label(&cpu_contract,
292 FRSH_RT_PROCESSOR, FRSH_CPU_ID_DEFAULT, "aqcpu_cont");
293 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
295 ret = frsh_contract_negotiate(&cpu_contract, &cpu_vres);
296 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
297 printf("Aqcpu vres negotiated\n");
299 pthread_attr_init(&frsh_attr);
300 ret = frsh_thread_create_and_bind(cpu_vres, &thread, &frsh_attr,
301 streamer_run, (void*) NULL);
302 if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
304 wait_for_ending_command();
306 streamer_run_done_rq = 1;
308 pthread_join(thread.pthread_id, (void**) NULL);
310 printf("Ending contracts\n");
312 ret = frsh_contract_cancel(cpu_vres);
313 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
315 printf("Finishing\n");
317 close_output_stream(os);
320 pthread_t streamer_th;
322 pthread_attr_init(&attr);
324 ret = pthread_create(&streamer_th, &attr, streamer_run, (void*) NULL);
326 printf("Failed to create streamer thread\n.");
328 wait_for_ending_command();
330 streamer_run_done_rq = 1;
332 pthread_join(streamer_th, (void**) NULL);
334 printf("Finishing\n");
336 close_output_stream(os);