#include "../tools/tools.h"
#include <EGrabber.h>

using namespace Euresys;

static void sample() {
    EGenTL genTL(Gigelink());
    EGrabberDiscovery discovery(genTL);

    discovery.discover();
    int itf = -1;
    for (int i = 0; i < discovery.interfaceCount(); i++) {
        if (discovery.deviceCount(i) > 0) {
            itf = i;
            break;
        }
    }
    if (itf < 0) {
        throw std::runtime_error("No GigE device found");
    }
    EGrabber<CallbackOnDemand> grabber(discovery.streamInfo(itf, 0, 0), gc::DEVICE_ACCESS_READONLY);

    grabber.runScript(Tools::getSampleFilePath("271-multicast-receiver.setup.js"));
    grabber.reallocBuffers(1);
    grabber.start(1, false);

    std::cout << "Ready to grab the image !\n";
    {
        ScopedBuffer buffer(grabber);
        ConvertedBuffer bgr = buffer.convert("BGR8");
        bgr.saveToDisk(Tools::getEnv("sample-output-path") + "/frame_multicast.jpeg", 1);
    }
}

static Tools::Sample addSample(__FILE__, sample, "Save the image received on the multicast group\n"
    "(call sample 271-multicast-receiver after 270-multicast-master)");
