Exemple #1
0
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;
}
Exemple #2
0
int main (int argc, char **argv)
{
    state_t * self = (state_t*) calloc (1, sizeof (state_t));
    self->use_gui = 1;

    char *optstring = "hc:p:";
    int c;
    struct option long_opts[] = { 
        { "help", no_argument, 0, 'h' },
        { "chain", required_argument, 0, 'c' },
        { "plugin-path", required_argument, 0, 'p' },
        { "no-gui", no_argument, 0, 'u' },
        { 0, 0, 0, 0 }
    };

    while ((c = getopt_long (argc, argv, optstring, long_opts, 0)) >= 0)
    {
        switch (c) {
            case 'c':
                self->xml_fname = strdup (optarg);
                break;
            case 'u':
                self->use_gui = 0;
                break;
            case 'p':
                self->extra_plugin_path = strdup (optarg);
                break;
            case 'h':
            default:
                usage ();
                break;
        };
    }

    if (!self->use_gui && !self->xml_fname) usage();

    if (self->use_gui) {
        gtk_init (&argc, &argv);
    } else {
        g_type_init ();
    }

    if (!g_thread_supported()) {
        g_thread_init (NULL);
    }

    if (0 != state_setup (self)) {
        state_cleanup(self);
        return 1;
    }

    if (self->use_gui) {
        camview_gtk_quit_on_interrupt ();
        gtk_main ();
        state_cleanup (self);
    } else {
        // did everything start up correctly?
        CamUnit *faulty = cam_unit_chain_all_units_stream_init(self->chain);
        if (faulty) {
            int faulty_index = 0;
            for(GList *uiter = cam_unit_chain_get_units(self->chain);
                    uiter && uiter->data != faulty; uiter = uiter->next)
                faulty_index ++;

            fprintf(stderr, "Unit %d [%s] is not streaming, aborting...\n",
                    faulty_index, cam_unit_get_name (faulty));
            return -1;
        }

        GMainLoop *mainloop = g_main_loop_new (NULL, FALSE);
        camview_g_quit_on_interrupt (mainloop);
        g_main_loop_run (mainloop);
        g_main_loop_unref (mainloop);
        state_cleanup (self);
    }

    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;
}