3 * @author aurelien.morelle@parrot.com
6 * Raw video capture from Mykonos RAM
14 #include <VP_Api/vp_api.h>
15 #include <VP_Api/vp_api_thread_helper.h>
16 #include <VP_Api/vp_api_error.h>
17 #include <VP_Api/vp_api_stage.h>
18 #include <VP_Stages/vp_stages_io_com.h>
19 #include <VP_Stages/vp_stages_io_file.h>
21 #include <VP_Os/vp_os_print.h>
22 #include <VP_Os/vp_os_malloc.h>
24 #include <ardrone_tool/ardrone_tool.h>
25 #include <ardrone_tool/Com/config_com.h>
26 #include <Vision/vision_stage.h>
28 #define NB_STAGES_MAX 10
30 PIPELINE_HANDLE raw_capture_pipeline_handle;
34 extern int exit_ihm_program;
38 raw_capture_output_file_stage_open(vp_stages_output_file_config_t *cfg)
45 raw_capture_output_file_stage_transform(vp_stages_output_file_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
47 static int total_size = 0, local_size, size2read;
52 vp_stages_vision_config_t saved_cfg;
58 static char raw_filename[256];
59 char struct_filename[256];
62 vp_os_mutex_lock(&out->lock);
64 if(in->status == VP_API_STATUS_PROCESSING && in->size > 0)
66 local_size = in->size;
72 gettimeofday(&tv,NULL);
73 localtime_r(&tv.tv_sec, &atm);
74 sprintf(&raw_filename[0], "raw_%04d%02d%02d_%02d%02d%02d.yuv", atm.tm_year+1900, atm.tm_mon+1, atm.tm_mday, atm.tm_hour, atm.tm_min, atm.tm_sec);
75 sprintf(&struct_filename[0], "vision_state_%04d%02d%02d_%02d%02d%02d.raw", atm.tm_year+1900, atm.tm_mon+1, atm.tm_mday, atm.tm_hour, atm.tm_min, atm.tm_sec);
76 cfg->name = &raw_filename[0];
77 cfg->f = fopen(cfg->name, "wb");
78 PRINT("Open %s \n", cfg->name);
81 if(total_size < sizeof(special_struct))
83 vp_os_memcpy(total_size+(int8_t*)&special_struct, &in->buffers[in->indexBuffer][0], min(sizeof(special_struct)-total_size, local_size));
84 local_size -= min(sizeof(special_struct)-total_size, in->size);
85 total_size += min(sizeof(special_struct)-total_size, in->size);
88 if(total_size == sizeof(special_struct))
90 size2read = special_struct.size2read;
91 f = fopen(&struct_filename[0], "wb");
92 fwrite(&special_struct.saved_cfg, sizeof(int8_t), sizeof(special_struct.saved_cfg), f);
99 if(start == 0 && local_size > 0)
101 fwrite(&in->buffers[in->indexBuffer][0], sizeof(int8_t), local_size*sizeof(int8_t), cfg->f);
102 total_size += local_size;
103 PRINT("red size = %07d / total size = %07d\r", total_size, size2read);
104 if(total_size == size2read)
110 PRINT("Close %s \n", cfg->name);
115 out->status = in->status;
117 vp_os_mutex_unlock(&out->lock);
124 raw_capture_output_file_stage_close(vp_stages_output_file_config_t *cfg)
130 const vp_api_stage_funcs_t raw_capture_output_file_funcs =
132 (vp_api_stage_handle_msg_t) NULL,
133 (vp_api_stage_open_t) raw_capture_output_file_stage_open,
134 (vp_api_stage_transform_t) raw_capture_output_file_stage_transform,
135 (vp_api_stage_close_t) raw_capture_output_file_stage_close
139 DEFINE_THREAD_ROUTINE(raw_capture, data)
143 vp_api_io_pipeline_t pipeline;
144 vp_api_io_data_t out;
145 vp_api_io_stage_t stages[NB_STAGES_MAX];
147 vp_stages_input_com_config_t icc;
148 vp_stages_output_file_config_t ofc;
150 vp_os_memset(&icc, 0, sizeof( icc ));
151 vp_os_memset(&ofc, 0, sizeof( ofc ));
153 icc.com = COM_RAW_CAPTURE();
154 icc.config = COM_CONFIG_RAW_CAPTURE();
155 icc.connection = COM_CONNECTION_RAW_CAPTURE();
156 icc.buffer_size = 16*16*3;
157 COM_CONFIG_SOCKET_VIDEO(&icc.socket, VP_COM_CLIENT, RAW_CAPTURE_PORT, wifi_ardrone_ip);
159 ofc.name = "toto.out";
161 pipeline.nb_stages = 0;
163 stages[pipeline.nb_stages].type = VP_API_INPUT_SOCKET;
164 stages[pipeline.nb_stages].cfg = (vp_stages_input_com_config_t *)&icc;
165 stages[pipeline.nb_stages++].funcs = vp_stages_input_com_funcs;
167 stages[pipeline.nb_stages].type = VP_API_OUTPUT_FILE;
168 stages[pipeline.nb_stages].cfg = (vp_stages_output_file_config_t *)&ofc;
169 stages[pipeline.nb_stages++].funcs = raw_capture_output_file_funcs;
171 pipeline.stages = &stages[0];
173 PRINT("\n Raw video capture thread initialisation\n\n");
175 res = vp_api_open(&pipeline, &raw_capture_pipeline_handle);
180 out.status = VP_API_STATUS_PROCESSING;
182 while( !ardrone_tool_exit() && (loop == SUCCESS) )
184 if( SUCCEED(vp_api_run(&pipeline, &out)) ) {
185 if( (out.status == VP_API_STATUS_PROCESSING || out.status == VP_API_STATUS_STILL_RUNNING) ) {
188 else loop = -1; // Finish this thread
192 vp_api_close(&pipeline, &raw_capture_pipeline_handle);
195 PRINT("\n Raw video capture thread closed\n\n");
197 return (THREAD_RET)0;