/// <summary>
/// Main processing function
/// </summary>
void KinectEasyGrabber::Record()
{
    if (NULL == m_pNuiSensor)
    {
        return;
    }

    bool needToDraw = false;

    if ( WAIT_OBJECT_0 == WaitForSingleObject(m_hNextDepthFrameEvent, 0) )
    {
        // if we have received any valid new depth data we may need to draw
        if ( SUCCEEDED(ProcessDepth()) )
        {
            needToDraw = true;
        }
    }

    if ( WAIT_OBJECT_0 == WaitForSingleObject(m_hNextColorFrameEvent, 0) )
    {
        // if we have received any valid new color data we may need to draw
        if ( SUCCEEDED(ProcessColor()) )
        {
            needToDraw = true;
        }
    }

    // Depth is 30 fps.  For any given combination of FPS, we should ensure we are within half a frame of the more frequent of the two.  
    // But depth is always the greater (or equal) of the two, so just use depth FPS.
    const int depthFps = 30;
    const int halfADepthFrameMs = (1000 / depthFps) / 2;

    // If we have not yet received any data for either color or depth since we started up, we shouldn't draw
    if (m_colorTimeStamp.QuadPart == 0 || m_depthTimeStamp.QuadPart == 0)
    {
        needToDraw = false;
    }

    // If the color frame is more than half a depth frame ahead of the depth frame we have,
    // then we should wait for another depth frame.  Otherwise, just go with what we have.
    if (m_colorTimeStamp.QuadPart - m_depthTimeStamp.QuadPart > halfADepthFrameMs)
    {
        needToDraw = false;
    }

	if (needToDraw)
    {
		if(m_frameIndex >= 50) return;

		dumpToDisk(m_frameIndex, m_frameBasename, m_depthD16, m_colorRGBX, m_colorCoordinates, m_depthTimeStamp, m_colorTimeStamp);

		// Draw the data with Direct2D
        m_pDrawKinectEasyGrabber->Draw(m_colorRGBX, m_colorWidth * m_colorHeight * cBytesPerPixel);

		m_frameIndex++;
	}
}
void StatsCollector::startNewRecord() {
    if (_enabled) {
        if (_dumpEveryXRecord && ++_recordsSinceLastDump >= _dumpEveryXRecord) {
            dumpToDisk();
            _recordsSinceLastDump = 0;
        }

        i.startNewRecord();
        d.startNewRecord();
    }
}
/// <summary>
/// Main processing function
/// </summary>
void KinectEasyGrabber::RecordArrayToDisk(){
	
	if(m_frameIndex < m_totalFrames) return;
	if(m_dumped) return;

	m_pDrawKinectEasyGrabber->Draw(m_colorRGBX, m_colorWidth * m_colorHeight * cBytesPerPixel);

	for(int i=0; i < m_totalFrames; i++){
		dumpToDisk(i, m_frameBasename, m_outputArrayDepthD16[i], m_outputArrayRGBX[i], m_outputArrayColorCoordinates[i], m_depthArrayTimeStamp[i], m_colorArrayTimeStamp[i]);
		delete [] m_outputArrayDepthD16[i];
		delete [] m_outputArrayColorCoordinates[i];
		delete [] m_outputArrayRGBX[i];
		m_outputArrayDepthD16[i] = NULL;
		m_outputArrayColorCoordinates[i] = NULL;
		m_outputArrayRGBX[i] = NULL;
	}

	//-------------------------------------------------------
	// TIMES STAMP --------------------------------
	//-------------------------------------------------------
	FILE* fid = 0;
	sprintf(m_frameBasename,"data/video_ts.txt");			
	fid = fopen(m_frameBasename,"w");
	fprintf(fid,"Frames=%d, Time=%fseg, Depth size=%dx%d, RGB size=%dx%d\n",
		m_totalFrames, (m_depthArrayTimeStamp[m_totalFrames-1].QuadPart - m_depthArrayTimeStamp[0].QuadPart)/1000.0f,
		m_depthWidth,m_depthHeight,m_colorWidth,m_colorHeight);
	fprintf(fid, "Frame\tDepth\tColor\tD_i-D_i-1\tC_i-C_i-1\tC_i-D_i\n");
	if(!fid){
		printf("ERROR abriendo el archivo %d\n",m_frameBasename);
		exit(-1);
	}

	LONGLONG dprev=0, dact=0, cprev=0, cact=0;
	for(int i=0; i < m_totalFrames; i++){
		dprev = dact; 
		cprev = cact;
		dact = m_depthArrayTimeStamp[i].QuadPart;
		cact = m_colorArrayTimeStamp[i].QuadPart;
		fprintf(fid, "%d\t%llu\t%llu\t%llu\t%llu\t%llu\n", i, dact, cact, (dact-dprev), (cact-cprev), abs(cact-dact));
	}
	fclose(fid);
	
	m_dumped = true;
	m_pDrawKinectEasyGrabber->Draw(m_backgroundRGBX, m_colorWidth * m_colorHeight * cBytesPerPixel);

	delete [] m_outputArrayDepthD16;
	delete [] m_outputArrayColorCoordinates;
	delete [] m_outputArrayRGBX;
	delete [] m_colorArrayTimeStamp;
	delete [] m_depthArrayTimeStamp;
}
StatsCollector::~StatsCollector() {
    dumpToDisk();
}