ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / elinux / api_v4l_vlib_ethernet.c
1 #include <VP_Api/vp_api.h>
2 #include <VP_Api/vp_api_error.h>
3 #include <VP_Stages/vp_stages_configs.h>
4 #include <VP_Stages/vp_stages_i_v4l.h>
5 #include <VP_Stages/vp_stages_io_com.h>
6 #include <VP_Os/vp_os_print.h>
7 #include <VP_Os/vp_os_malloc.h>
8 #include <VP_Os/vp_os_delay.h>
9 #include <VP_Api/vp_api_thread_helper.h>
10 #include <VLIB/Stages/vlib_stage_encode.h>
11
12
13 #undef BLOCK_MODE
14 #undef ACQ_WIDTH
15 #undef ACQ_HEIGHT
16
17 #define ACQ_WIDTH 320
18 #define ACQ_HEIGHT 240
19
20 #define COM_VIDEO()             wired_com()
21 #define COM_CONFIG_VIDEO()      wired_config()
22 #define COM_CONNECTION_VIDEO()  wired_connection()
23
24
25 PROTO_THREAD_ROUTINE(app,nomParams);
26
27 BEGIN_THREAD_TABLE
28   THREAD_TABLE_ENTRY(app,20)
29 END_THREAD_TABLE
30
31
32 #define NB_STAGES_MAX 10
33
34
35 static vp_stages_input_v4l_config_t  ivc;
36 static vlib_stage_encoding_config_t  vec;
37 static vp_stages_output_com_config_t occ;
38
39 static PIPELINE_HANDLE pipeline_handle;
40
41 static vp_api_picture_t picture;
42
43
44 C_RESULT
45 buffer_to_picture_open(int *cfg)
46 {
47   picture.format = PIX_FMT_YUV420P;
48
49   picture.width = ACQ_WIDTH;
50   picture.height = ACQ_HEIGHT;
51   picture.framerate = 15;
52
53   picture.y_line_size = ACQ_WIDTH;
54   picture.cb_line_size = ACQ_WIDTH/2;
55   picture.cr_line_size = ACQ_WIDTH/2;
56
57   return (SUCCESS);
58 }
59
60 C_RESULT
61 buffer_to_picture_transform(int *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
62 {
63   static int32_t blockline;
64
65   vp_os_mutex_lock(&out->lock);
66
67   if(out->status == VP_API_STATUS_INIT)
68     {
69       blockline = 0;
70       out->numBuffers = 1;
71       out->size = ACQ_WIDTH*ACQ_HEIGHT*3/2;
72       out->buffers = (int8_t **)(int8_t *)&picture;
73       out->indexBuffer = 0;
74       out->status = VP_API_STATUS_PROCESSING;
75     }
76
77   out->status = in->status;
78
79   if(out->status == VP_API_STATUS_ENDED)
80     {
81     }
82
83   if(out->status == VP_API_STATUS_PROCESSING)
84     {
85       picture.y_buf = (uint8_t*)in->buffers[0];
86       picture.cb_buf = (uint8_t*)in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT; 
87       picture.cr_buf = (uint8_t*)in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT*5/4; 
88       picture.complete = 1;
89     }
90   
91   out->status = in->status;
92   
93   vp_os_mutex_unlock(&out->lock);
94   
95   return (SUCCESS);
96 }
97
98 C_RESULT
99 buffer_to_picture_close(int *cfg)
100 {
101   return (SUCCESS);
102 }
103
104 const vp_api_stage_funcs_t buffer_to_picture_funcs =
105 {
106   NULL,
107   (vp_api_stage_open_t)buffer_to_picture_open,
108   (vp_api_stage_transform_t)buffer_to_picture_transform,
109   (vp_api_stage_close_t)buffer_to_picture_close
110 };
111
112
113 vp_com_t* wired_com(void)
114 {
115   static vp_com_t com = {
116     VP_COM_WIRED,
117     FALSE,
118     0,
119     { { 0 } },
120     NULL,
121     NULL,
122     0,
123     NULL,
124     NULL,
125     NULL,
126     NULL,
127     NULL,
128     NULL,
129     NULL,
130     NULL,
131     NULL,
132     NULL,
133     NULL
134   };
135
136   return &com;
137 }
138
139 vp_com_config_t* wired_config(void)
140 {
141   static vp_com_wired_config_t config = {
142     "eth0",
143     "172.20.22.253",
144     "255.255.255.0",
145     "172.20.22.255"
146   };
147
148   return (vp_com_config_t*) &config;
149 }
150
151 vp_com_connection_t* wired_connection(void)
152 {
153   static vp_com_wired_connection_t connection = {
154     0
155   };
156
157   return (vp_com_connection_t*) (void*) &connection;
158 }
159
160
161 int main(int argc, char **argv)
162 {
163   START_THREAD(app, argv);
164   JOIN_THREAD(app);
165
166   return EXIT_SUCCESS;
167 }
168
169
170 PROTO_THREAD_ROUTINE(app,argv)
171 {
172   vp_api_io_pipeline_t pipeline;
173   vp_api_io_data_t out;
174   vp_api_io_stage_t stages[NB_STAGES_MAX];
175
176   uint16_t time1,time2;
177   uint16_t i;
178
179   time1 = clock();
180
181   vp_os_memset(&ivc,0,sizeof(ivc));
182   vp_os_memset(&vec,0,sizeof(vec));
183   vp_os_memset(&occ,0,sizeof(occ));
184
185   ivc.device = "/tmp/video0";
186   ivc.width = ACQ_WIDTH;
187   ivc.height = ACQ_HEIGHT;
188   ivc.vp_api_picture = &picture;
189
190   vec.width                               = ACQ_WIDTH;
191   vec.height                              = ACQ_HEIGHT;
192   vec.block_mode_enable                   = FALSE;
193   vec.picture                             = &picture;
194
195   occ.com                           = COM_VIDEO();
196   occ.config                        = COM_CONFIG_VIDEO();
197   occ.connection                    = COM_CONNECTION_VIDEO();
198   occ.socket.type                   = VP_COM_SERVER;
199   occ.socket.protocol               = VP_COM_TCP;
200   occ.socket.port                   = 5555;
201   occ.buffer_size                   = 1500;
202
203   pipeline.nb_stages = 0;
204
205   stages[pipeline.nb_stages].type    = VP_API_INPUT_FILE;
206   stages[pipeline.nb_stages].cfg     = (void *)&ivc;
207   stages[pipeline.nb_stages++].funcs = vp_stages_input_v4l_funcs;
208   
209   stages[pipeline.nb_stages].type    = VP_API_FILTER_ENCODER;
210   stages[pipeline.nb_stages].cfg     = (void *)NULL;
211   stages[pipeline.nb_stages++].funcs = buffer_to_picture_funcs;
212   
213   stages[pipeline.nb_stages].type    = VP_API_FILTER_ENCODER;
214   stages[pipeline.nb_stages].cfg     = (void*)&vec;
215   stages[pipeline.nb_stages++].funcs = vlib_encoding_funcs;
216
217   stages[pipeline.nb_stages].type    = VP_API_OUTPUT_SOCKET;
218   stages[pipeline.nb_stages].cfg     = (void *)&occ;
219   stages[pipeline.nb_stages++].funcs = vp_stages_output_com_funcs;
220
221   pipeline.stages = &stages[0];
222
223   vp_api_open(&pipeline, &pipeline_handle);
224  
225   out.status = VP_API_STATUS_PROCESSING;
226
227   /////////////////////////////////////////////////////////////////////////////////////////
228   i = 0;
229   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING))
230     {
231       i++;
232       PRINT("image %d \n",i);
233       //vp_os_delay(50);
234     }
235   /////////////////////////////////////////////////////////////////////////////////////////
236
237   time2 = clock();
238   printf("temps ecoule : %d \n",time2-time1);
239     
240   vp_api_close(&pipeline, &pipeline_handle);
241
242   return EXIT_SUCCESS;
243 }
244