// create the instance of the nui sensor
HRESULT KinectSensor::CreateNuiDevice()
{
    assert( m_wsPortID.size() != 0 );

    // check if we can use it
    CComPtr<INuiSensor> pNuiSensor;
    HRESULT hr = NuiCreateSensorById( m_wsPortID.c_str(), &pNuiSensor );
    if( FAILED(hr) )
    {
        return hr;
    }

    // is this one we already have
    if( m_pNuiSensor == pNuiSensor )
    {
        return S_OK;
    }

    assert( nullptr == m_pNuiSensor );

    // if the device is not in use by another process
    if( !IsSensorConflict(pNuiSensor) )
    {
        // start clean
        ResetDevice();

        m_pNuiSensor = pNuiSensor; // auto refcount with CComPtr
    }
    
    // update internal state and return
    return GetNUISensorStatus(true);
}
// used to notify object to release of create the sensor
void KinectSensor::NuiStatusNotification( _In_z_ const WCHAR* wcPortID, HRESULT hrStatus )
{
    AutoLock lock( m_nuiLock );

    if( E_NUI_NOTCONNECTED == hrStatus )
    {
        ResetDevice();

        // release the instance of the sensor
        m_pNuiSensor.Release();
    }
    else
    {
        if( 0 != m_wsPortID.compare(wcPortID) )
        {
            ResetDevice();
            m_wsPortID = wcPortID;
        }

        // notifaction callback, check the state
        // if there is a non-recovery error, reset
        HRESULT hr = GetNUISensorStatus();
        if( FAILED(hr) && KinectSensorStatusError == m_eStatus )
        {
            ResetDevice();

            // release the instance of the sensor
            m_pNuiSensor.Release();
            
            return;
        }
    }

}
// is the instance of the sensor ready
bool KinectSensor::IsAvailable()
{
    if( SUCCEEDED( GetNUISensorStatus(true) ) && ( m_eStatus == KinectSensorStatusStarted ) )
    {
        return true;
    }
    
    return false;
}
Exemplo n.º 4
0
// is the instance of the sensor ready
bool KinectSensor::IsStarted()
{
    AutoLock lock(m_nuiLock);

    if (SUCCEEDED(GetNUISensorStatus(true)) && (m_eStatus == KinectSensorStatusStarted))
    {
        return true;
    }

    return false;
}
Exemplo n.º 5
0
// updates the instance of the sensor and initializes the stream
HRESULT KinectSensor::UpdateSensor()
{
    // only try to create and initialize for Opened/Selected sensors
    if (!m_bSelected)
    {
        return E_NUI_DEVICE_NOT_READY;
    }

    if (m_bInitialized)
    {
        return GetNUISensorStatus();
    }

    // if the sensor isn't ready, try to create it
    HRESULT hr = S_OK;
    if (!IsStarted() && KinectSensorStatusError != m_eStatus)
    {
        hr = CreateNuiDevice();
        if (FAILED(hr))
        {
            return hr;
        }
    }
   /* else
    {
        return m_hrLast;
    }*/

    // set the parameters based on the streams that were created
    DWORD dwInitFlags = 0;
    if (((nullptr != m_pColorStream) || (nullptr != m_pDepthStream) || (nullptr != m_pSkeletonStream) || (nullptr != m_pAudioStream)))
    {
        if ((nullptr != m_pColorStream))
        {
            dwInitFlags |= NUI_INITIALIZE_FLAG_USES_COLOR;
        }
        if ((nullptr != m_pSkeletonStream))
        {
            dwInitFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX;
            dwInitFlags |= NUI_INITIALIZE_FLAG_USES_SKELETON;
        }
        else if ((nullptr != m_pDepthStream))
        {
            dwInitFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH;
        }
        if (nullptr != m_pAudioStream)
        {
            dwInitFlags |= NUI_INITIALIZE_FLAG_USES_AUDIO;
        }
    }

    // initialize the sensor
    hr = m_pNuiSensor->NuiInitialize(dwInitFlags);
    if (SUCCEEDED(hr))
    {
        m_bInitialized = true;
    }
    else
    {
        ResetDevice();
        return hr;
    }

    // if we initialized the sensor, pass it along to the enabled streams
    if (m_bInitialized)
    {
        if (nullptr != m_pColorStream)
        {
            m_pColorStream->Initialize(m_pNuiSensor);
        }
        if (nullptr != m_pDepthStream)
        {
            m_pDepthStream->Initialize(m_pNuiSensor);
        }
        if (nullptr != m_pSkeletonStream)
        {
            m_pSkeletonStream->Initialize(m_pNuiSensor);
        }
        if (nullptr != m_pAudioStream)
        {
            m_pAudioStream->Initialize(m_pNuiSensor);
        }
    }

    return hr;
}