static int state_setup (state_t *self) { // create the image processing chain self->chain = cam_unit_chain_new(); self->manager = cam_unit_manager_get_and_ref(); // search for plugins in non-standard directories if(self->extra_plugin_path) { char **path_dirs = g_strsplit(self->extra_plugin_path, ":", 0); for (int i=0; path_dirs[i]; i++) { cam_unit_manager_add_plugin_dir (self->manager, path_dirs[i]); } g_strfreev (path_dirs); free(self->extra_plugin_path); self->extra_plugin_path = NULL; } // setup the GUI if (self->use_gui) { setup_gtk (self); } else { assert (self->xml_fname); } cam_unit_chain_all_units_stream_init (self->chain); cam_unit_chain_attach_glib (self->chain, 1000, NULL); g_signal_connect (G_OBJECT (self->chain), "frame-ready", G_CALLBACK (on_frame_ready), self); if (self->xml_fname) { char *xml_str = NULL; GError *err = NULL; g_file_get_contents (self->xml_fname, &xml_str, NULL, &err); if(err) { fprintf(stderr, "\n\nError loading chain from file!\n"); fprintf(stderr, "==============================\n"); fprintf(stderr, "%s\n", err->message); free(xml_str); return -1; } cam_unit_chain_load_from_str (self->chain, xml_str, NULL); free (xml_str); } return 0; }
int main(int argc, char **argv) { g_type_init(); CamUnit *resize_unit = NULL; CamUnit *faulty_unit = NULL; CamUnitChain * chain = NULL; const char *input_id = NULL; state_t s; memset (&s, 0, sizeof (s)); sprintf (s.filename, "snapshot-output.ppm"); // create the GLib mainloop s.mainloop = g_main_loop_new (NULL, FALSE); s.lcm = lcm_create ("udpm://?transmit_only=true"); if (!s.lcm) { fprintf (stderr, "Couldn't create LCM\n"); goto failed; } // create the image processing chain chain = cam_unit_chain_new (); // abort if no input unit was specified if (argc < 2) { print_usage_and_inputs (argv[0], chain->manager); goto failed; } input_id = argv[1]; // instantiate the input unit if (! cam_unit_chain_add_unit_by_id (chain, input_id)) { fprintf (stderr, "Oh no! Couldn't create input unit [%s].\n\n", input_id); print_usage_and_inputs (argv[0], chain->manager); goto failed; } // create a unit to convert the input data to 8-bit RGB cam_unit_chain_add_unit_by_id (chain, "convert.to_rgb8"); resize_unit = cam_unit_chain_add_unit_by_id (chain, "filter.ipp.resize"); // cam_unit_set_control_boolean (resize_unit, "aspect-lock", FALSE); cam_unit_set_control_int (resize_unit, "width", 64); // cam_unit_set_control_int (resize_unit, "height", 64); // start the image processing chain faulty_unit = cam_unit_chain_all_units_stream_init (chain); // did everything start up correctly? if (faulty_unit) { fprintf (stderr, "Unit [%s] is not streaming, aborting...\n", cam_unit_get_name (faulty_unit)); goto failed; } // attach the chain to the glib event loop. cam_unit_chain_attach_glib (chain, 1000, NULL); // subscribe to be notified when an image has made its way through the // chain g_signal_connect (G_OBJECT (chain), "frame-ready", G_CALLBACK (on_frame_ready), &s); s.next_publish_utime = _timestamp_now (); // run g_main_loop_run (s.mainloop); // cleanup g_main_loop_unref (s.mainloop); cam_unit_chain_all_units_stream_shutdown (chain); g_object_unref (chain); return 0; failed: g_main_loop_unref (s.mainloop); cam_unit_chain_all_units_stream_shutdown (chain); g_object_unref (chain); return 1; }