Example #1
0
HRESULT CMFCamCapture::stop()
{
    HRESULT hr = S_OK;
    IMFMediaEvent *pEvent = NULL;
    m_spSession->Stop();
    
    // wait for session to finish.
    while (1) {
        HRESULT hrStatus = S_OK;
        MediaEventType met;

        CHECK_HR(hr = m_spSession->GetEvent(0, &pEvent));
        CHECK_HR(hr = pEvent->GetStatus(&hrStatus));
        CHECK_HR(hr = pEvent->GetType(&met));

        if (FAILED(hrStatus)) {
            DBGMSG(L"Session error: 0x%x (event id: %d)\n", hrStatus, met);
            hr = hrStatus;
            goto done;
        }
        if (met == MESessionEnded || met == MESessionStopped) {
            break;
        }
        SafeRelease(&pEvent);
    }
    done:
    return hr;
}
// Handle an event from the capture engine. 
// NOTE: This method is called from the application's UI thread. 
HRESULT CaptureManager::OnCaptureEvent(WPARAM wParam, LPARAM lParam)
{
    GUID guidType;
    HRESULT hrStatus;

    IMFMediaEvent *pEvent = reinterpret_cast<IMFMediaEvent*>(wParam);

    HRESULT hr = pEvent->GetStatus(&hrStatus);
    if (FAILED(hr))
    {
        hrStatus = hr;
    }

    hr = pEvent->GetExtendedType(&guidType);
    if (SUCCEEDED(hr))
    {

#ifdef _DEBUG
        LPOLESTR str;
        if (SUCCEEDED(StringFromCLSID(guidType, &str)))
        {
            DBGMSG((L"MF_CAPTURE_ENGINE_EVENT: %s (hr = 0x%X)\n", str, hrStatus));
            CoTaskMemFree(str);
        }
#endif

        if (guidType == MF_CAPTURE_ENGINE_INITIALIZED)
        {
            OnCaptureEngineInitialized(hrStatus);
            SetErrorID(hrStatus, IDS_ERR_INITIALIZE);
        }
        else if (guidType == MF_CAPTURE_ENGINE_PREVIEW_STARTED)
        {
            OnPreviewStarted(hrStatus);
            SetErrorID(hrStatus, IDS_ERR_PREVIEW);
        }
        else if (guidType == MF_CAPTURE_ENGINE_PREVIEW_STOPPED)
        {
            OnPreviewStopped(hrStatus);
            SetErrorID(hrStatus, IDS_ERR_PREVIEW);
        }
        else if (guidType == MF_CAPTURE_ENGINE_RECORD_STARTED)
        {
            OnRecordStarted(hrStatus);
            SetErrorID(hrStatus, IDS_ERR_RECORD);
        }
        else if (guidType == MF_CAPTURE_ENGINE_RECORD_STOPPED)
        {
            OnRecordStopped(hrStatus);
            SetErrorID(hrStatus, IDS_ERR_RECORD);
        }
        else if (guidType == MF_CAPTURE_ENGINE_PHOTO_TAKEN)
        {
            m_bPhotoPending = false;
            SetErrorID(hrStatus, IDS_ERR_PHOTO);
        }
        else if (guidType == MF_CAPTURE_ENGINE_ERROR)
        {
            DestroyCaptureEngine();
            SetErrorID(hrStatus, IDS_ERR_CAPTURE);
        }
        else if (FAILED(hrStatus))
        {
            SetErrorID(hrStatus, IDS_ERR_CAPTURE);
        }
    }

    pEvent->Release();
    SetEvent(m_hEvent);
    return hrStatus;
}
HRESULT CPlayer::HandleEvent(UINT_PTR pEventPtr)
{
    HRESULT hrStatus = S_OK;
	HRESULT hr = S_OK;
    MediaEventType meType = MEUnknown;  

    IMFMediaEvent *pEvent = (IMFMediaEvent*)pEventPtr;

    if (pEvent == NULL)
    {
        return E_POINTER;
    }

    // Get the event type.
    hr = pEvent->GetType(&meType);
	CHECK_HR( hr );

    // Get the event status. If the operation that triggered the event 
    // did not succeed, the status is a failure code.
    hr = pEvent->GetStatus(&hrStatus);

    // Check if the async operation succeeded.
    if (SUCCEEDED(hr) && FAILED(hrStatus)) 
    {
        hr = hrStatus;
    }
	CHECK_HR( hr );

    switch(meType)
    {
    case MESessionTopologyStatus:
        hr = OnTopologyStatus(pEvent);
        break;

    case MEEndOfPresentation:
        hr = OnPresentationEnded(pEvent);
		CI_LOG_V("Presentation Ended");
        break;

    case MENewPresentation:
        hr = OnNewPresentation(pEvent);
		CI_LOG_V( "New Presentation" );
        break;

	case MESessionTopologySet:
		IMFTopology * topology;
		GetEventObject<IMFTopology> (pEvent,&topology);
		WORD nodeCount;
		topology->GetNodeCount(&nodeCount);
		CI_LOG_V( "Topo set and we have " << nodeCount << " nodes" );
		//cout << "Topo set and we have "  << nodeCount << " nodes" << endl;
		SafeRelease(&topology);
		break;

	case MESessionStarted:
		CI_LOG_V( "Started Session" );
		break;
	case MEBufferingStarted:
		CI_LOG_I( "Buffering..." );
		break;
	case MEBufferingStopped:
		CI_LOG_I( "Finished Buffering..." );
		break;

    default:
        hr = OnSessionEvent(pEvent, meType);
        break;
    }

done:
    SafeRelease(&pEvent);
    return hr;
}
HRESULT CPlayer::HandleEvent(UINT_PTR pUnkPtr)
{
    HRESULT hr = S_OK;
    HRESULT hrStatus = S_OK;            // Event status
    MediaEventType meType = MEUnknown;  // Event type
    MF_TOPOSTATUS TopoStatus = MF_TOPOSTATUS_INVALID; // Used with MESessionTopologyStatus event.    

    IUnknown *pUnk = NULL;
    IMFMediaEvent *pEvent = NULL;

    // pUnkPtr is really an IUnknown pointer.
    pUnk = (IUnknown*)pUnkPtr;

    if (pUnk == NULL)
    {
        return E_POINTER;
    }

    CHECK_HR(hr = pUnk->QueryInterface(__uuidof(IMFMediaEvent), (void**)&pEvent));

    // Get the event type.
    CHECK_HR(hr = pEvent->GetType(&meType));

    // Get the event status. If the operation that triggered the event did
    // not succeed, the status is a failure code.
    CHECK_HR(hr = pEvent->GetStatus(&hrStatus));

    TRACE((L"Media event: %s\n", EventName(meType)));

    // Check if the async operation succeeded.
    if (SUCCEEDED(hrStatus))
    {
        // Switch on the event type. Update the internal state of the CPlayer as needed.
        switch(meType)
        {
        case MESessionTopologyStatus:
            // Get the status code.
            CHECK_HR(hr = pEvent->GetUINT32(MF_EVENT_TOPOLOGY_STATUS, (UINT32*)&TopoStatus));
            switch (TopoStatus)
            {
            case MF_TOPOSTATUS_READY: 
                hr = OnTopologyReady(pEvent);
                break;
            default: 
                // Nothing to do.
                break;  
            }
            break;

        case MEEndOfPresentation:
            CHECK_HR(hr = OnPresentationEnded(pEvent));
            break;
        }
    }
    else
    {
        hr = hrStatus;
    }

done:
    SAFE_RELEASE(pUnk);
    SAFE_RELEASE(pEvent);
    return hr;
}
HRESULT CTranscoder::Transcode()
{
    assert (m_pSession);
    
    IMFMediaEvent* pEvent = NULL;
    MediaEventType meType = MEUnknown;  // Event type

    HRESULT hr = S_OK;
    HRESULT hrStatus = S_OK;            // Event status

    //Get media session events synchronously
    while (meType != MESessionClosed)
    {
        hr = m_pSession->GetEvent(0, &pEvent);

        if (FAILED(hr)) { break; }

        // Get the event type.
        hr = pEvent->GetType(&meType);
        
        if (FAILED(hr)) { break; }

        hr = pEvent->GetStatus(&hrStatus);
        
        if (FAILED(hr)) { break; }

        if (FAILED(hrStatus))
        {
            wprintf_s(L"Failed. 0x%X error condition triggered this event.\n", hrStatus);
            hr = hrStatus;
            break;
        }

        switch (meType)
        {
        case MESessionTopologySet:
            hr = Start();
            if (SUCCEEDED(hr))
            {
                wprintf_s(L"Ready to start.\n");
            }
            break;

        case MESessionStarted:
            wprintf_s(L"Started encoding...\n");
            break;

        case MESessionEnded:
            hr = m_pSession->Close();
            if (SUCCEEDED(hr))
            {
                wprintf_s(L"Finished encoding.\n");
            }
            break;

        case MESessionClosed:
            wprintf_s(L"Output file created.\n");
            break;
        }

        if (FAILED(hr))
        {
            break;
        }

        SafeRelease(&pEvent);
    }

    SafeRelease(&pEvent);
    return hr;
}
/** Asyncronous callback */
HRESULT FImfVideoPlayer::Invoke( IMFAsyncResult* AsyncResult )
{
	IMFMediaEvent* Event = NULL;
	HRESULT HResult = MediaSession->EndGetEvent( AsyncResult, &Event );

	if( FAILED( HResult ) )
	{
		Event->Release( );
		return S_OK;
	}

	MediaEventType EventType = MEUnknown;
	HResult = Event->GetType( &EventType );
	//Moving lower because it is referenced below to GetStatus if there was a problem: Event->Release( );

	if( FAILED( HResult ) )
	{
		Event->Release( );
		return S_OK;
	}

	/* Closed */
	if( EventType == MESessionClosed )
	{
		MovieIsFinished.Set( 1 );
		CloseIsPosted.Set( 1 );
	}
	else
	{
		HResult = MediaSession->BeginGetEvent( this, NULL );
		if( FAILED( HResult ) )
		{
			Event->Release( );
			return S_OK;
		}

		if( MovieIsRunning( ) )
		{
			/* End of clip */
			if( EventType == MEEndOfPresentation )
			{
				if( Looping )
					StartPlayback( );
				else
					MovieIsFinished.Set( 1 );
			}

			/* Unknown error, dont continue */
			else if( EventType == MEError )
			{
				HRESULT HReturnCode = S_OK;
				Event->GetStatus( &HReturnCode );

				/* Log error HResult */
				UE_LOG( LogImfVideoPlayer, Log, TEXT( "ImfVideoPlayer error recieved: %i" ), HReturnCode );

				MovieIsFinished.Set( 1 );
				CloseIsPosted.Set( 1 );
			}
		}

		/* DEBUG: Displays all event ID's in log */
		//UE_LOG( LogImfVideoPlayer, Log, TEXT( "ImfVideoPlayer event id: %i" ), EventType );
	}
	
	Event->Release( );

	return S_OK;
}