2 * Copyright (c) 2008 Luca Abeni
4 * This is free software; see GPL.txt
9 #include <libavformat/avformat.h>
10 #include <libavdevice/avdevice.h>
11 #include <libswscale/swscale.h>
18 #include "streamer_config.h"
22 #include <ul_logreg.h>
25 #ifdef CONFIG_STREAMER_WITH_FRSH
29 extern long int udp_budget, udp_period;
31 static const char *sdp_file = "sdp.txt";
32 static const char *vdev = "/dev/video0";
33 static const char *dst = "127.0.0.1";
34 static int dport = 20000;
35 static int width = 352;
36 static int height = 288;
38 static const char *impform = "video4linux2";
39 AVFormatContext *s, *os;
41 static void sdp_print(AVFormatContext *s, const char *fname)
46 f = fopen(fname, "w");
47 avf_sdp_create(&s, 1, sdp, sizeof(sdp));
48 fprintf(f, "%s\n", sdp);
55 printf("usage: streamer [ options ]\n");
56 printf(" -w <number> send image width\n");
57 printf(" -h <number> send image height\n");
58 printf(" -r <number> refresh rate\n");
59 printf(" -r <path> video device [%s]\n", vdev);
60 printf(" -m <addr> destination IP address\n");
61 printf(" -i <string> input video device format [%s]\n", impform);
63 printf(" -l <number>|<domain>=<number>,...\n");
64 #endif /*CONFIG_OC_ULUT*/
67 static int args_parse(int argc, char *argv[])
71 while ((v = getopt(argc, argv, "w:h:r:d:m:i:l:")) >= 0) {
77 height = atoi(optarg);
90 if(!strcmp(impform, "v4l"))
91 impform = "video4linux";
92 else if(!strcmp(impform, "v4l2"))
93 impform = "video4linux2";
97 ul_log_domain_arg2levels(optarg);
99 #endif /*CONFIG_OC_ULUT*/
101 default: /* unknown option */
102 fprintf(stderr, "%s: illegal option %c\n", argv[0], v);
111 int streamer_run_done_rq;
113 void* streamer_run(void* args)
118 while (!(done = streamer_run_done_rq)) {
120 pkt = read_input_packet(s);
127 pkt->pts += s->streams[pkt->stream_index]->start_time;
128 //rt_job_start(pkt->pts);
129 f = pkt_decode(s, pkt);
131 opkt = pkt_encode(os, f);
144 void wait_for_ending_command(void) {
146 sigemptyset(&sigset);
147 sigaddset(&sigset, SIGINT);
148 sigaddset(&sigset, SIGTERM);
149 sigwaitinfo(&sigset, NULL);
151 static void block_signals(void) {
153 sigemptyset(&sigset);
154 sigaddset(&sigset, SIGINT);
155 sigaddset(&sigset, SIGTERM);
156 sigprocmask(SIG_BLOCK,&sigset,NULL);
157 pthread_sigmask(SIG_BLOCK,&sigset,NULL);
161 int main(int argc, char *argv[])
164 //long int cpu_budget, cpu_period;
169 #ifdef CONFIG_STREAMER_WITH_FRSH
171 if (ret) PERROR_AND_EXIT(ret, "frsh_init1");
172 #endif /*CONFIG_STREAMER_WITH_FRSH*/
174 /* fill contract params */
178 avcodec_register_all();
180 avdevice_register_all();
182 args_parse(argc, argv);
184 s = open_input_stream(vdev, width, height, fps, impform);
186 fprintf(stderr, "Cannot open input file %s\n", vdev);
191 os = open_output_stream(dst, dport, CODEC_TYPE_VIDEO);
193 fprintf(stderr, "Cannot open output stream\n");
197 os->streams[0]->codec->width = s->streams[0]->codec->width;
198 os->streams[0]->codec->height = s->streams[0]->codec->height;
199 os->streams[0]->codec->time_base = s->streams[0]->codec->time_base;
200 codec_connect(s->streams[0]->codec, os->streams[0]->codec);
202 dump_format(os, 0, os->filename, 1);
203 sdp_print(os, sdp_file);
206 frsh_thread_attr_t frsh_attr;
207 frsh_thread_id_t thread;
208 frsh_vres_id_t cpu_vres;
209 frsh_contract_t cpu_contract;
210 frsh_rel_time_t cpu_budget, cpu_period;
212 cpu_budget = fosa_msec_to_rel_time(50);
213 cpu_period = fosa_msec_to_rel_time(100);
214 /* Contract negotiation for CPU */
215 ret = frsh_contract_init(&cpu_contract);
216 if (ret) PERROR_AND_EXIT(ret, "CPU:frsh_contract_init");
218 ret = frsh_contract_set_basic_params(&cpu_contract,
223 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
224 ret = frsh_contract_set_resource_and_label(&cpu_contract,
225 FRSH_RT_PROCESSOR, 0,"aqcpu_cont");
226 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
228 ret = frsh_contract_negotiate(&cpu_contract, &cpu_vres);
229 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_negotiate");
230 printf("Aqcpu vres negotiated\n");
232 pthread_attr_init(&frsh_attr);
233 ret = frsh_thread_create_and_bind(cpu_vres, &thread, &frsh_attr,
234 streamer_run, (void*) NULL);
235 if (ret) PERROR_AND_EXIT(ret, "frsh_thread_create_and_bind");
237 wait_for_ending_command();
239 streamer_run_done_rq = 1;
241 pthread_join(thread.pthread_id, (void**) NULL);
243 printf("Ending contracts\n");
245 ret = frsh_contract_cancel(cpu_vres);
246 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_cancel");
248 printf("Finishing\n");
252 pthread_t streamer_th;
254 pthread_attr_init(&attr);
256 ret = pthread_create(&streamer_th, &attr, streamer_run, (void*) NULL);
258 printf("Failed to create streamer thread\n.");
260 wait_for_ending_command();
262 streamer_run_done_rq = 1;
264 pthread_join(streamer_th, (void**) NULL);
266 printf("Finishing\n");