void PS3EyeStereoDepthApp::updateCameraRef(ps3eye::PS3EYECam::PS3EYERef ref, int index)
{
    std::lock_guard<std::mutex> lock(frameMemMutex);
    auto frame = cameraSurfaces[index];
    uint8 *frameMemPtr = camPair.framePtrAt(index);
    yuv422_to_rgba(ref->getLastFramePointer(), ref->getRowBytes(), frameMemPtr, frame.getWidth(), frame.getHeight());
    displayTextures[index] = gl::Texture(frame);
}
void PS3EYECaptureApp::guiEvent(ciUIEvent *event)
{
    string name = event->widget->getName();
    if(name == "auto gain")
    {
        ciUIToggle *t = (ciUIToggle * ) event->widget;
        eye->setAutogain(t->getValue());
    }
    else if(name == "auto white balance")
    {
        ciUIToggle *t = (ciUIToggle * ) event->widget;
        eye->setAutoWhiteBalance(t->getValue());
    }
    else if(name == "gain")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setGain(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "sharpness")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setSharpness(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "exposure")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setExposure(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "brightness")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setBrightness(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "contrast")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setContrast(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "hue")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setHue(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "blue balance")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setBlueBalance(static_cast<uint8_t>(s->getScaledValue()));
    }
    else if(name == "red balance")
    {
        ciUISlider *s = (ciUISlider *) event->widget;
        eye->setRedBalance(static_cast<uint8_t>(s->getScaledValue()));
    }
}
void PS3EyeSlowMoApp::setup()
{
    currentFrame = 0;
    mSkippedFrames = 1;
    using namespace ps3eye;
    
    mShouldQuit = false;
    
    // list out the devices
    std::vector<PS3EYECam::PS3EYERef> devices( PS3EYECam::getDevices() );
    console() << "found " << devices.size() << " cameras" << std::endl;
    
    mTimer = Timer(true);
    mCamFrameCount = 0;
    mCamFps = 0;
    mCamFpsLastSampleFrame = 0;
    mCamFpsLastSampleTime = 0;
    
    if(devices.size())
    {
        eye = devices.at(0);
        bool res = eye->init(640, 480, 60);
        console() << "init eye result " << res << std::endl;
        eye->start();

        frame_bgra = new uint8_t[eye->getWidth()*eye->getHeight()*4];
        mFrame = Surface(frame_bgra, eye->getWidth(), eye->getHeight(), eye->getWidth()*4, SurfaceChannelOrder::BGRA);
        memset(frame_bgra, 0, eye->getWidth()*eye->getHeight()*4);
        
        // create and launch the thread
        mThread = thread( bind( &PS3EyeSlowMoApp::eyeUpdateThreadFn, this ) );
    }
    
    mParams = params::InterfaceGl::create( "PS3EYE", toPixels( ivec2( 180, 150 ) ) );
    
    mParams->addParam( "Framerate", &mFrameRate, "", true );
    mParams->addParam( "Queue", &mQueueSize, "", true);
    mParams->addSeparator();
    mParams->addParam( "Skip", &mSkippedFrames).min( 1 ).step( 1 );
    mParams->addParam( "Auto gain", &isAutoGain );
    mParams->addParam( "Auto WB", &isAutoWB );

    //surfaceQueue = new ph::ConcurrentQueue<Surface*>();

    mAccumFbo = gl::Fbo::create( getWindowWidth(), getWindowHeight(),
                                gl::Fbo::Format().colorTexture( gl::Texture::Format().internalFormat( GL_RGB16F ) ).disableDepth() );
    
}
void PS3EYECaptureApp::shutdown()
{
    // You should stop before exiting
    // otherwise the app will keep working
	if (eye)
		eye->stop();
    //
	delete[] frame_bgr;
	delete gui;
}
void PS3EyeSlowMoApp::shutdown()
{
    mShouldQuit = true;
    mThread.join();
    // You should stop before exiting
    // otherwise the app will keep working
    eye->stop();
    //
    delete[] frame_bgra;
    //delete gui;
    
}
void PS3EyeSlowMoApp::update()
{
    if(eye)
    {
        bool isNewFrame = eye->isNewFrame();
        if(isNewFrame)
        {
            yuv422_to_rgba(eye->getLastFramePointer(), eye->getRowBytes(), frame_bgra, mFrame.getWidth(), mFrame.getHeight());
            
            //Surface source = mFrame.clone();
            
            OStreamMemRef os = OStreamMem::create();
            
            DataTargetRef target = DataTargetStream::createRef( os );
            
            writeImage( target, mFrame, ImageTarget::Options(), "jpeg" );

            const void *data = os->getBuffer();
            
            size_t dataSize = os->tell();

            BufferRef buf = Buffer::create(dataSize );
            memcpy(buf->getData(), data, dataSize);
            surfaceVector.push_back(Surface( loadImage( DataSourceBuffer::create(buf)), SurfaceConstraintsDefault(), false ));
        }
        mCamFrameCount += isNewFrame ? 1 : 0;
        double now = mTimer.getSeconds();
        if( now > mCamFpsLastSampleTime + 1 ) {
            uint32_t framesPassed = mCamFrameCount - mCamFpsLastSampleFrame;
            mCamFps = (float)(framesPassed / (now - mCamFpsLastSampleTime));
            
            mCamFpsLastSampleTime = now;
            mCamFpsLastSampleFrame = mCamFrameCount;
        }
        mFrameRate = eye->getFrameRate();
    }
    
    mQueueSize = surfaceVector.size();
    currentFrame++;
}
void PS3EYECaptureApp::update()
{
    if(eye)
    {
		eye->getFrame(frame_bgr);
        mTexture = gl::Texture( mFrame );

        mCamFrameCount++;
        double now = mTimer.getSeconds();
        if( now > mCamFpsLastSampleTime + 1 ) {
            uint32_t framesPassed = mCamFrameCount - mCamFpsLastSampleFrame;
            mCamFps = (float)(framesPassed / (now - mCamFpsLastSampleTime));

            mCamFpsLastSampleTime = now;
            mCamFpsLastSampleFrame = mCamFrameCount;
        }
    
        gui->update();
        eyeFpsLab->update_fps(mCamFps);
    }
}
void PS3EYECaptureApp::setup()
{
    using namespace ps3eye;

    // list out the devices
    std::vector<PS3EYECam::PS3EYERef> devices( PS3EYECam::getDevices() );
	console() << "found " << devices.size() << " cameras" << std::endl;

	mTimer = Timer(true);
	mCamFrameCount = 0;
	mCamFps = 0;
	mCamFpsLastSampleFrame = 0;
	mCamFpsLastSampleTime = 0;

	gui = new ciUICanvas(0,0,320, 480);
    
    float gh = 15;
    float slw = 320 - 20;

    if(devices.size())
    {   
        eye = devices.at(0);
        bool res = eye->init(640, 480, 60);
        console() << "init eye result " << res << std::endl;
        eye->start();
        
		frame_bgr = new uint8_t[eye->getWidth()*eye->getHeight()*3];
		mFrame = Surface(frame_bgr, eye->getWidth(), eye->getHeight(), eye->getWidth()*3, SurfaceChannelOrder::BGR);
		memset(frame_bgr, 0, eye->getWidth()*eye->getHeight()*3);
		        
        gui->addWidgetDown(new ciUILabel("EYE", CI_UI_FONT_MEDIUM));
        
        eyeFpsLab = new eyeFPS(CI_UI_FONT_MEDIUM);
        gui->addWidgetRight(eyeFpsLab);
        
        // controls
        gui->addWidgetDown(new ciUIToggle(gh, gh, false, "auto gain"));
        gui->addWidgetRight(new ciUIToggle(gh, gh, false, "auto white balance"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 63, eye->getGain(), "gain"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 63, eye->getSharpness(), "sharpness"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getExposure(), "exposure"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getBrightness(), "brightness"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getContrast(), "contrast"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getHue(), "hue"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getBlueBalance(), "blue balance"));
        gui->addWidgetDown(new ciUISlider(slw, gh, 0, 255, eye->getRedBalance(), "red balance"));
        
        gui->registerUIEvents(this, &PS3EYECaptureApp::guiEvent);
    }
}