Exemple #1
0
Glib::RefPtr<Gdk::Pixbuf> webcam::extract_frame(Glib::RefPtr<Gst::Sample> sample,
                                                std::shared_ptr<std::pair<int, int>> resolution) {
    utils::debug::scope_log log(DBG_LVL_5("gtox"), {});
    static auto null = Glib::RefPtr<Gdk::Pixbuf>();
    if (!sample) {
        return null;
    }

    auto buffer = sample->get_buffer();
    if (!buffer) {
        return null;
    }
    auto map = Glib::RefPtr<Gst::MapInfo>(new Gst::MapInfo);
    if (!buffer->map(map, Gst::MAP_READ)) {
        return null;
    }
    auto mem = new uint8_t[map->get_size()];
    std::copy(map->get_data(), map->get_data() + map->get_size(), mem);
    buffer->unmap(map);

    return Gdk::Pixbuf::create_from_data(mem,
                                         Gdk::COLORSPACE_RGB,
                                         false,
                                         8,
                                         resolution->first,
                                         resolution->second,
                                         GST_ROUND_UP_4( resolution->first*3 ),
                                         [](const guint8* data) {
        delete[] data;
    });
}
Exemple #2
0
Glib::RefPtr<Gdk::Pixbuf> gstreamer::extract_frame(Glib::RefPtr<Gst::Sample> sample,
                                                   std::shared_ptr<std::pair<int, int>> resolution) {
    static auto null = Glib::RefPtr<Gdk::Pixbuf>();
    if (!sample) {
        return null;
    }
    /*
    Generates crashes, not idea why
    using "resolution" now..
    gets filled by preroll

    auto caps = sample->get_caps();
    if (!caps) {
        return null;
    }
    if (caps->empty()) {
        return null;
    }
    if (caps->empty()) {
        return null;
    }
    auto struc = caps->get_structure(0);
    int w,h;
    if (!struc.get_field("width", w)) {
        return null;
    }
    if (!struc.get_field("height", h)) {
        return null;
    }
    */
    auto buffer = sample->get_buffer();
    if (!buffer) {
        return null;
    }
    auto map = Glib::RefPtr<Gst::MapInfo>(new Gst::MapInfo);
    if (!buffer->map(map, Gst::MAP_READ)) {
        return null;
    }
    auto mem = new uint8_t[map->get_size()];
    std::copy(map->get_data(), map->get_data() + map->get_size(), mem);
    buffer->unmap(map);

    return Gdk::Pixbuf::create_from_data(mem,
                                               Gdk::COLORSPACE_RGB,
                                               false,
                                               8,
                                               resolution->first,
                                               resolution->second,
                                               GST_ROUND_UP_4( resolution->first*3 ),
                                               [](const guint8* data) {
        delete[] data;
    });
}